From 695c44dc1776665333f132fcd6566002a640eebb Mon Sep 17 00:00:00 2001 From: kishorenc Date: Sat, 5 Sep 2020 17:55:36 +0530 Subject: [PATCH] Forward status code and content type properly. --- include/http_client.h | 2 -- include/http_server.h | 1 + src/http_client.cpp | 50 +++++++++++-------------------------------- src/http_server.cpp | 4 ++++ src/raft_server.cpp | 6 ------ 5 files changed, 18 insertions(+), 45 deletions(-) diff --git a/include/http_client.h b/include/http_client.h index b57a2916..cc8d2af4 100644 --- a/include/http_client.h +++ b/include/http_client.h @@ -20,8 +20,6 @@ private: static size_t curl_write(char *contents, size_t size, size_t nmemb, std::string *s); - static size_t curl_header(char *buffer, size_t size, size_t nmemb, void* context); - static size_t curl_write_async(char *buffer, size_t size, size_t nmemb, void* context); static size_t curl_write_async_done(void* context, curl_socket_t item); diff --git a/include/http_server.h b/include/http_server.h index 58180d45..0a30e4b5 100644 --- a/include/http_server.h +++ b/include/http_server.h @@ -47,6 +47,7 @@ private: h2o_socket_t* listener_socket; static const size_t ACTIVE_STREAM_WINDOW_SIZE = 196605; + static const size_t REQ_TIMEOUT_MS = 60000; static const uint64_t SSL_REFRESH_INTERVAL_MS = 8 * 60 * 60 * 1000; h2o_custom_timer_t ssl_refresh_timer; diff --git a/src/http_client.cpp b/src/http_client.cpp index 8788c11a..e7486a87 100644 --- a/src/http_client.cpp +++ b/src/http_client.cpp @@ -174,37 +174,6 @@ size_t HttpClient::curl_req_send_callback(char* buffer, size_t size, size_t nite return bytes_to_read; } -size_t HttpClient::curl_header(char *buffer, size_t size, size_t nmemb, void *context) { - deferred_req_res_t* req_res = static_cast(context); - size_t header_size = size * nmemb; - - std::string header(buffer, header_size); - - if(header.rfind("HTTP", 0) == 0) { - // status field, e.g. "HTTP/1.1 404 Not Found" - std::vector parts; - StringUtils::split(header, parts, " "); - if(parts.size() >= 2 && StringUtils::is_uint32_t(parts[1])) { - req_res->res->status_code = std::stoi(parts[1]); - } else { - req_res->res->status_code = 500; - } - } else if(header.rfind("content-type", 0) == 0) { - // e.g. "content-type: application/json; charset=utf-8" - std::vector parts; - StringUtils::split(header, parts, ":"); - if(parts.size() == 2) { - req_res->res->content_type_header = parts[1]; - } else { - req_res->res->content_type_header = "application/json; charset=utf-8"; - } - } - - LOG(INFO) << "header:|" << header << "|"; - - return header_size; -} - size_t HttpClient::curl_write_async(char *buffer, size_t size, size_t nmemb, void *context) { // callback for response body to be sent back to client LOG(INFO) << "curl_write_async"; @@ -217,10 +186,20 @@ size_t HttpClient::curl_write_async(char *buffer, size_t size, size_t nmemb, voi size_t res_size = size * nmemb; - // FIXME: use header from remote response + // set headers if not already set + if(req_res->res->status_code == 0) { + CURL* curl = req_res->req->data; + long http_code = 500; + curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code); + + char* content_type; + curl_easy_getinfo (curl, CURLINFO_CONTENT_TYPE, &content_type); + + req_res->res->status_code = http_code; + req_res->res->content_type_header = content_type; + } + // we've got response from remote host: write to client and ask for more request body - req_res->res->content_type_header = "text/plain; charset=utf8"; - req_res->res->status_code = 200; req_res->res->body = std::string(buffer, res_size); req_res->res->final = false; @@ -288,9 +267,6 @@ CURL *HttpClient::init_curl_async(const std::string& url, deferred_req_res_t* re curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); - //curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, curl_header); - //curl_easy_setopt(curl, CURLOPT_HEADERDATA, req_res); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HttpClient::curl_write_async); curl_easy_setopt(curl, CURLOPT_WRITEDATA, req_res); diff --git a/src/http_server.cpp b/src/http_server.cpp index 5096368f..c3dbd9d3 100644 --- a/src/http_server.cpp +++ b/src/http_server.cpp @@ -128,6 +128,10 @@ int HttpServer::create_listener() { ctx.globalconf->server_name = h2o_strdup(nullptr, "", SIZE_MAX); ctx.globalconf->http2.active_stream_window_size = ACTIVE_STREAM_WINDOW_SIZE; + ctx.globalconf->http2.idle_timeout = REQ_TIMEOUT_MS; + + ctx.globalconf->http1.req_timeout = REQ_TIMEOUT_MS; + ctx.globalconf->http1.req_io_timeout = REQ_TIMEOUT_MS; accept_ctx->ctx = &ctx; accept_ctx->hosts = config.hosts; diff --git a/src/raft_server.cpp b/src/raft_server.cpp index a840a3a1..945c2609 100644 --- a/src/raft_server.cpp +++ b/src/raft_server.cpp @@ -122,13 +122,7 @@ void ReplicationState::write(http_req* request, http_res* response) { return node->apply(task); } -size_t follower_write_count = 0; - void ReplicationState::follower_write(http_req *request, http_res *response) const { - follower_write_count++; - - LOG(INFO) << "follower_write_count: " << follower_write_count; - if(node->leader_id().is_empty()) { // Handle no leader scenario LOG(ERROR) << "Rejecting write: could not find a leader.";