text
stringlengths 0
2.2M
|
---|
resp->headers["Access-Control-Allow-Headers"] = req->GetHeader("Access-Control-Request-Headers", "Content-Type");
|
return HTTP_STATUS_NO_CONTENT;
|
}
|
// Unified verification request Content-Type?
|
// if (req->content_type != APPLICATION_JSON) {
|
// return response_status(resp, HTTP_STATUS_BAD_REQUEST);
|
// }
|
// Deserialize request body to json, form, etc.
|
req->ParseBody();
|
// Unified setting response Content-Type?
|
resp->content_type = APPLICATION_JSON;
|
#if 0
|
// authentication sample code
|
if (strcmp(req->path.c_str(), "/login") != 0) {
|
string token = req->GetHeader("token");
|
if (token.empty()) {
|
response_status(resp, 10011, "Miss token");
|
return HTTP_STATUS_UNAUTHORIZED;
|
}
|
else if (strcmp(token.c_str(), "abcdefg") != 0) {
|
response_status(resp, 10012, "Token wrong");
|
return HTTP_STATUS_UNAUTHORIZED;
|
}
|
return HTTP_STATUS_UNFINISHED;
|
}
|
#endif
|
return HTTP_STATUS_UNFINISHED;
|
}
|
int Handler::postprocessor(HttpRequest* req, HttpResponse* resp) {
|
// printf("%s\n", resp->Dump(true, true).c_str());
|
return resp->status_code;
|
}
|
int Handler::errorHandler(const HttpContextPtr& ctx) {
|
int error_code = ctx->response->status_code;
|
return response_status(ctx, error_code);
|
}
|
int Handler::largeFileHandler(const HttpContextPtr& ctx) {
|
std::thread([ctx](){
|
ctx->writer->Begin();
|
std::string filepath = ctx->service->document_root + ctx->request->Path();
|
HFile file;
|
if (file.open(filepath.c_str(), "rb") != 0) {
|
ctx->writer->WriteStatus(HTTP_STATUS_NOT_FOUND);
|
ctx->writer->WriteHeader("Content-Type", "text/html");
|
ctx->writer->WriteBody("<center><h1>404 Not Found</h1></center>");
|
ctx->writer->End();
|
return;
|
}
|
http_content_type content_type = CONTENT_TYPE_NONE;
|
const char* suffix = hv_suffixname(filepath.c_str());
|
if (suffix) {
|
content_type = http_content_type_enum_by_suffix(suffix);
|
}
|
if (content_type == CONTENT_TYPE_NONE || content_type == CONTENT_TYPE_UNDEFINED) {
|
content_type = APPLICATION_OCTET_STREAM;
|
}
|
size_t filesize = file.size();
|
ctx->writer->WriteHeader("Content-Type", http_content_type_str(content_type));
|
ctx->writer->WriteHeader("Content-Length", filesize);
|
// ctx->writer->WriteHeader("Transfer-Encoding", "chunked");
|
ctx->writer->EndHeaders();
|
char* buf = NULL;
|
int len = 4096; // 4K
|
SAFE_ALLOC(buf, len);
|
size_t total_readbytes = 0;
|
int last_progress = 0;
|
int sendbytes_per_ms = 1024; // 1KB/ms = 1MB/s = 8Mbps
|
int sleep_ms_per_send = len / sendbytes_per_ms; // 4ms
|
int sleep_ms = sleep_ms_per_send;
|
auto start_time = std::chrono::steady_clock::now();
|
auto end_time = start_time;
|
while (total_readbytes < filesize) {
|
size_t readbytes = file.read(buf, len);
|
if (readbytes <= 0) {
|
// read file error!
|
ctx->writer->close();
|
break;
|
}
|
int nwrite = ctx->writer->WriteBody(buf, readbytes);
|
if (nwrite < 0) {
|
// disconnected!
|
break;
|
} else if (nwrite == 0) {
|
// send too fast or peer recv too slow
|
// reduce speed of send
|
sleep_ms *= 2;
|
// size_t write_backlog = hio_write_bufsize(ctx->writer->io());
|
} else {
|
// restore speed of send
|
sleep_ms = sleep_ms_per_send;
|
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.