mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-05-10 21:52:08 +02:00
Implement files aswell.
This commit is contained in:
parent
4c891a373e
commit
5d07b6a233
@ -195,20 +195,21 @@ HttpResponsePtr HttpResponse::newFileResponse(
|
|||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpResponsePtr HttpResponse::newFileResponse(
|
HttpResponsePtr HttpResponse::newFileResponse(const std::string &fullPath, const std::string &attachmentFileName, ContentType type) {
|
||||||
const std::string &fullPath,
|
|
||||||
const std::string &attachmentFileName,
|
|
||||||
ContentType type) {
|
|
||||||
std::ifstream infile(fullPath, std::ifstream::binary);
|
std::ifstream infile(fullPath, std::ifstream::binary);
|
||||||
LOG_TRACE << "send http file:" << fullPath;
|
LOG_TRACE << "send http file:" << fullPath;
|
||||||
|
|
||||||
if (!infile) {
|
if (!infile) {
|
||||||
auto resp = HttpResponse::newNotFoundResponse();
|
auto resp = HttpResponse::newNotFoundResponse();
|
||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto resp = std::make_shared<HttpResponseImpl>();
|
auto resp = std::make_shared<HttpResponseImpl>();
|
||||||
std::streambuf *pbuf = infile.rdbuf();
|
std::streambuf *pbuf = infile.rdbuf();
|
||||||
std::streamsize filesize = pbuf->pubseekoff(0, std::ifstream::end);
|
std::streamsize filesize = pbuf->pubseekoff(0, std::ifstream::end);
|
||||||
pbuf->pubseekoff(0, std::ifstream::beg); // rewind
|
pbuf->pubseekoff(0, std::ifstream::beg); // rewind
|
||||||
|
|
||||||
if (HttpAppFrameworkImpl::instance().useSendfile() && filesize > 1024 * 200)
|
if (HttpAppFrameworkImpl::instance().useSendfile() && filesize > 1024 * 200)
|
||||||
// TODO : Is 200k an appropriate value? Or set it to be configurable
|
// TODO : Is 200k an appropriate value? Or set it to be configurable
|
||||||
{
|
{
|
||||||
|
@ -8,6 +8,8 @@ void DRequest::send() {
|
|||||||
// return;
|
// return;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
HttpResponsePtr response = HttpResponse::newHttpResponse();
|
||||||
|
|
||||||
response->setBody(compiled_body);
|
response->setBody(compiled_body);
|
||||||
|
|
||||||
response->setExpiredTime(0);
|
response->setExpiredTime(0);
|
||||||
@ -17,34 +19,9 @@ void DRequest::send() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DRequest::send_file(const std::string &p_file_path) {
|
void DRequest::send_file(const std::string &p_file_path) {
|
||||||
//if (connection_closed) {
|
HttpResponsePtr response = HttpResponse::newFileResponse(p_file_path ,"",drogon::getContentType(p_file_path));
|
||||||
// DRequestPool::return_request(this);
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
/*
|
|
||||||
file_path = p_file_path;
|
|
||||||
|
|
||||||
FILE *f = fopen(file_path.c_str(), "rb");
|
callback(response);
|
||||||
|
|
||||||
if (!f) {
|
|
||||||
printf("send_file: Error: Download: file doesn't exists! %s\n", file_path.c_str());
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
fseek(f, 0, SEEK_END);
|
|
||||||
file_size = ftell(f);
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
response->addHeadValue("Connection", "Close");
|
|
||||||
std::string result = "HTTP/1.1 200 OK\r\nConnection: Close\r\n\r\n";
|
|
||||||
|
|
||||||
application->register_request_update(this);
|
|
||||||
|
|
||||||
session->send(result.c_str(), result.size(), [this]() { this->_file_chunk_sent(); });
|
|
||||||
*/
|
|
||||||
|
|
||||||
send_error(404);
|
|
||||||
|
|
||||||
pool();
|
pool();
|
||||||
}
|
}
|
||||||
@ -52,7 +29,6 @@ void DRequest::send_file(const std::string &p_file_path) {
|
|||||||
void DRequest::reset() {
|
void DRequest::reset() {
|
||||||
Request::reset();
|
Request::reset();
|
||||||
|
|
||||||
response.reset();
|
|
||||||
request.reset();
|
request.reset();
|
||||||
|
|
||||||
//response = new HttpResponse();
|
//response = new HttpResponse();
|
||||||
|
@ -17,7 +17,6 @@ class DWebApplication;
|
|||||||
|
|
||||||
class DRequest : public Request {
|
class DRequest : public Request {
|
||||||
public:
|
public:
|
||||||
HttpResponsePtr response;
|
|
||||||
HttpRequestImplPtr request;
|
HttpRequestImplPtr request;
|
||||||
std::function<void(const HttpResponsePtr &)> callback;
|
std::function<void(const HttpResponsePtr &)> callback;
|
||||||
|
|
||||||
|
@ -544,7 +544,6 @@ void DWebApplication::on_async_request(const HttpRequestImplPtr &req, std::funct
|
|||||||
|
|
||||||
DRequest *request = DRequest::get();
|
DRequest *request = DRequest::get();
|
||||||
request->application = this;
|
request->application = this;
|
||||||
request->response = HttpResponse::newHttpResponse();
|
|
||||||
request->request = std::shared_ptr<drogon::HttpRequestImpl>(req);
|
request->request = std::shared_ptr<drogon::HttpRequestImpl>(req);
|
||||||
request->callback = callback;//std::move(callback);
|
request->callback = callback;//std::move(callback);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user