Permalink
Browse files

Add support for CULRINFO_HTTP_VERSION

Closes #16.
  • Loading branch information...
bassosimone committed Dec 5, 2018
1 parent fdbac4e commit dea2a85fd535192b1cf35af164c97c9eff202de4
Showing with 40 additions and 0 deletions.
  1. +3 −0 mkcurl-client.cpp
  2. +37 −0 mkcurl.h
@@ -59,6 +59,9 @@ static void summary(mkcurl_response_uptr &res) {
<< "Content Type: "
<< mkcurl_response_get_content_type_v2(res.get())
<< std::endl
<< "HTTP version: "
<< mkcurl_response_get_http_version(res.get())
<< std::endl
<< "=== END SUMMARY ==="
<< std::endl
<< std::endl;
@@ -152,6 +152,12 @@ const char *mkcurl_response_get_certificate_chain_v2(
/// or an empty string. Calls abort if @p res is a null pointer.
const char *mkcurl_response_get_content_type_v2(const mkcurl_response_t *res);

/// mkcurl_response_get_http_version returns the HTTP version being used as a
/// string. The returned string will be a static string, so you don't have
/// to worry about owning it. If the version is not known, an empty string is
/// returned. This function will call abort if passed a null pointer @p res.
const char *mkcurl_response_get_http_version(const mkcurl_response_t *res);

/// mkcurl_response_delete deletes @p res. Note that @p res MAY be null.
void mkcurl_response_delete(mkcurl_response_t *res);

@@ -358,6 +364,8 @@ struct mkcurl_response {
std::string certs;
// content_type is the response content type.
std::string content_type;
// http_version is the HTTP version as a static string.
const char *http_version = "";
};

// mkcurl_log appends @p line to @p logs. It adds information on the current
@@ -455,6 +463,13 @@ const char *mkcurl_response_get_content_type_v2(const mkcurl_response_t *res) {
return res->content_type.c_str();
}

const char *mkcurl_response_get_http_version(const mkcurl_response_t *res) {
if (res == nullptr) {
MKCURL_ABORT();
}
return res->http_version;
}

void mkcurl_response_delete(mkcurl_response_t *res) { delete res; }

// mkcurl_deleter is a custom deleter for a CURL handle.
@@ -842,6 +857,28 @@ mkcurl_response_t *mkcurl_request_perform_nonnull(const mkcurl_request_t *req) {
}
if (ct != nullptr) res->content_type = ct;
}
{
long httpv = 0L;
if ((res->error = MKCURL_EASY_GETINFO(
handle.get(), CURLINFO_HTTP_VERSION, &httpv)) != CURLE_OK) {
mkcurl_log(res->logs, "curl_easy_getinfo(CURLINFO_HTTP_VERSION) failed");
return res.release();
}
switch (httpv) {
case CURL_HTTP_VERSION_1_0:
res->http_version = "HTTP/1.0";
break;
case CURL_HTTP_VERSION_1_1:
res->http_version = "HTTP/1.1";
break;
case CURL_HTTP_VERSION_2_0:
res->http_version = "HTTP/2";
break;
default:
res->http_version = "";
break;
}
}
mkcurl_log(res->logs, "curl_easy_perform() success");
return res.release();
}

0 comments on commit dea2a85

Please sign in to comment.