text
stringlengths
0
2.2M
int Handler::grpc(HttpRequest* req, HttpResponse* resp) {
if (req->content_type != APPLICATION_GRPC) {
return response_status(resp, HTTP_STATUS_BAD_REQUEST);
}
// parse protobuf
// ParseFromString(req->body);
// resp->content_type = APPLICATION_GRPC;
// serailize protobuf
// resp->body = SerializeAsString(xxx);
response_status(resp, 0, "OK");
return 200;
}
int Handler::test(const HttpContextPtr& ctx) {
ctx->setContentType(ctx->type());
ctx->set("bool", ctx->get<bool>("bool"));
ctx->set("int", ctx->get<int>("int"));
ctx->set("float", ctx->get<float>("float"));
ctx->set("string", ctx->get("string"));
response_status(ctx, 0, "OK");
return 200;
}
int Handler::restful(const HttpContextPtr& ctx) {
// RESTful /:field/ => HttpRequest::query_params
// path=/group/:group_name/user/:user_id
std::string group_name = ctx->param("group_name");
std::string user_id = ctx->param("user_id");
ctx->set("group_name", group_name);
ctx->set("user_id", user_id);
response_status(ctx, 0, "OK");
return 200;
}
int Handler::login(const HttpContextPtr& ctx) {
std::string username = ctx->get("username");
std::string password = ctx->get("password");
if (username.empty() || password.empty()) {
response_status(ctx, 10001, "Miss username or password");
return HTTP_STATUS_BAD_REQUEST;
}
else if (strcmp(username.c_str(), "admin") != 0) {
response_status(ctx, 10002, "Username not exist");
return HTTP_STATUS_BAD_REQUEST;
}
else if (strcmp(password.c_str(), "123456") != 0) {
response_status(ctx, 10003, "Password wrong");
return HTTP_STATUS_BAD_REQUEST;
}
else {
ctx->set("token", "abcdefg");
response_status(ctx, 0, "OK");
return HTTP_STATUS_OK;
}
}
int Handler::upload(const HttpContextPtr& ctx) {
int status_code = 200;
if (ctx->is(MULTIPART_FORM_DATA)) {
status_code = ctx->request->SaveFormFile("file", "html/uploads/");
} else {
status_code = ctx->request->SaveFile("html/uploads/upload.txt");
}
return response_status(ctx, status_code);
}
int Handler::sse(const HttpContextPtr& ctx) {
ctx->writer->EndHeaders("Content-Type", "text/event-stream");
// send(message) every 1s
hv::setInterval(1000, [ctx](hv::TimerID timerID) {
char szTime[DATETIME_FMT_BUFLEN] = {0};
datetime_t now = datetime_now();
datetime_fmt(&now, szTime);
// @test html/EventSource.html EventSource.onmessage
std::string msg("event: message\n");
msg += "data: ";
msg += szTime;
msg += "\n\n";
ctx->writer->write(msg);
});
return HTTP_STATUS_UNFINISHED;
}
/*******************************************************************************
* Copyright 2017-2022 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/