text
stringlengths
0
2.2M
total_readbytes += readbytes;
int cur_progress = total_readbytes * 100 / filesize;
if (cur_progress > last_progress) {
// printf("<< %s progress: %ld/%ld = %d%%\n",
// ctx->request->path.c_str(), (long)total_readbytes, (long)filesize, (int)cur_progress);
last_progress = cur_progress;
}
end_time += std::chrono::milliseconds(sleep_ms);
std::this_thread::sleep_until(end_time);
}
ctx->writer->End();
SAFE_FREE(buf);
// auto elapsed_time = std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time);
// printf("<< %s taked %ds\n", ctx->request->path.c_str(), (int)elapsed_time.count());
}).detach();
return HTTP_STATUS_UNFINISHED;
}
int Handler::sleep(const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
writer->WriteHeader("X-Response-tid", hv_gettid());
unsigned long long start_ms = gettimeofday_ms();
writer->response->Set("start_ms", start_ms);
std::string strTime = req->GetParam("t", "1000");
if (!strTime.empty()) {
int ms = atoi(strTime.c_str());
if (ms > 0) {
hv_delay(ms);
}
}
unsigned long long end_ms = gettimeofday_ms();
writer->response->Set("end_ms", end_ms);
writer->response->Set("cost_ms", end_ms - start_ms);
response_status(writer, 0, "OK");
return 200;
}
int Handler::setTimeout(const HttpContextPtr& ctx) {
unsigned long long start_ms = gettimeofday_ms();
ctx->set("start_ms", start_ms);
std::string strTime = ctx->param("t", "1000");
if (!strTime.empty()) {
int ms = atoi(strTime.c_str());
if (ms > 0) {
hv::setTimeout(ms, [ctx, start_ms](hv::TimerID timerID){
unsigned long long end_ms = gettimeofday_ms();
ctx->set("end_ms", end_ms);
ctx->set("cost_ms", end_ms - start_ms);
response_status(ctx, 0, "OK");
});
}
}
return HTTP_STATUS_UNFINISHED;
}
int Handler::query(const HttpContextPtr& ctx) {
// scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
// ?query => HttpRequest::query_params
for (auto& param : ctx->params()) {
ctx->set(param.first.c_str(), param.second);
}
response_status(ctx, 0, "OK");
return 200;
}
int Handler::kv(HttpRequest* req, HttpResponse* resp) {
if (req->content_type != APPLICATION_URLENCODED) {
return response_status(resp, HTTP_STATUS_BAD_REQUEST);
}
resp->content_type = APPLICATION_URLENCODED;
resp->kv = req->GetUrlEncoded();
resp->SetUrlEncoded("int", 123);
resp->SetUrlEncoded("float", 3.14);
resp->SetUrlEncoded("string", "hello");
return 200;
}
int Handler::json(HttpRequest* req, HttpResponse* resp) {
if (req->content_type != APPLICATION_JSON) {
return response_status(resp, HTTP_STATUS_BAD_REQUEST);
}
resp->content_type = APPLICATION_JSON;
resp->json = req->GetJson();
resp->json["int"] = 123;
resp->json["float"] = 3.14;
resp->json["string"] = "hello";
return 200;
}
int Handler::form(HttpRequest* req, HttpResponse* resp) {
if (req->content_type != MULTIPART_FORM_DATA) {
return response_status(resp, HTTP_STATUS_BAD_REQUEST);
}
resp->content_type = MULTIPART_FORM_DATA;
resp->form = req->GetForm();
resp->SetFormData("int", 123);
resp->SetFormData("float", 3.14);
resp->SetFormData("string", "hello");
// resp->SetFormFile("file", "test.jpg");
return 200;
}