mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-14 04:57:21 +01:00
Implement files aswell.
This commit is contained in:
parent
4c891a373e
commit
5d07b6a233
@ -195,20 +195,21 @@ HttpResponsePtr HttpResponse::newFileResponse(
|
||||
return resp;
|
||||
}
|
||||
|
||||
HttpResponsePtr HttpResponse::newFileResponse(
|
||||
const std::string &fullPath,
|
||||
const std::string &attachmentFileName,
|
||||
ContentType type) {
|
||||
HttpResponsePtr HttpResponse::newFileResponse(const std::string &fullPath, const std::string &attachmentFileName, ContentType type) {
|
||||
|
||||
std::ifstream infile(fullPath, std::ifstream::binary);
|
||||
LOG_TRACE << "send http file:" << fullPath;
|
||||
|
||||
if (!infile) {
|
||||
auto resp = HttpResponse::newNotFoundResponse();
|
||||
return resp;
|
||||
}
|
||||
|
||||
auto resp = std::make_shared<HttpResponseImpl>();
|
||||
std::streambuf *pbuf = infile.rdbuf();
|
||||
std::streamsize filesize = pbuf->pubseekoff(0, std::ifstream::end);
|
||||
pbuf->pubseekoff(0, std::ifstream::beg); // rewind
|
||||
|
||||
if (HttpAppFrameworkImpl::instance().useSendfile() && filesize > 1024 * 200)
|
||||
// TODO : Is 200k an appropriate value? Or set it to be configurable
|
||||
{
|
||||
|
@ -8,6 +8,8 @@ void DRequest::send() {
|
||||
// return;
|
||||
//}
|
||||
|
||||
HttpResponsePtr response = HttpResponse::newHttpResponse();
|
||||
|
||||
response->setBody(compiled_body);
|
||||
|
||||
response->setExpiredTime(0);
|
||||
@ -17,34 +19,9 @@ void DRequest::send() {
|
||||
}
|
||||
|
||||
void DRequest::send_file(const std::string &p_file_path) {
|
||||
//if (connection_closed) {
|
||||
// DRequestPool::return_request(this);
|
||||
// return;
|
||||
//}
|
||||
/*
|
||||
file_path = p_file_path;
|
||||
HttpResponsePtr response = HttpResponse::newFileResponse(p_file_path ,"",drogon::getContentType(p_file_path));
|
||||
|
||||
FILE *f = fopen(file_path.c_str(), "rb");
|
||||
|
||||
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);
|
||||
callback(response);
|
||||
|
||||
pool();
|
||||
}
|
||||
@ -52,7 +29,6 @@ void DRequest::send_file(const std::string &p_file_path) {
|
||||
void DRequest::reset() {
|
||||
Request::reset();
|
||||
|
||||
response.reset();
|
||||
request.reset();
|
||||
|
||||
//response = new HttpResponse();
|
||||
|
@ -17,7 +17,6 @@ class DWebApplication;
|
||||
|
||||
class DRequest : public Request {
|
||||
public:
|
||||
HttpResponsePtr response;
|
||||
HttpRequestImplPtr request;
|
||||
std::function<void(const HttpResponsePtr &)> callback;
|
||||
|
||||
|
@ -544,7 +544,6 @@ void DWebApplication::on_async_request(const HttpRequestImplPtr &req, std::funct
|
||||
|
||||
DRequest *request = DRequest::get();
|
||||
request->application = this;
|
||||
request->response = HttpResponse::newHttpResponse();
|
||||
request->request = std::shared_ptr<drogon::HttpRequestImpl>(req);
|
||||
request->callback = callback;//std::move(callback);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user