Forward status code and content type properly.

This commit is contained in:
kishorenc 2020-09-05 17:55:36 +05:30
parent 58b52687ea
commit 695c44dc17
5 changed files with 18 additions and 45 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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<deferred_req_res_t *>(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<std::string> 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<std::string> 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);

View File

@ -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;

View File

@ -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.";