Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Commit

Permalink
Add support for CULRINFO_HTTP_VERSION
Browse files Browse the repository at this point in the history
Closes #16.
  • Loading branch information
bassosimone committed Dec 5, 2018
1 parent fdbac4e commit dea2a85
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mkcurl-client.cpp
Expand Up @@ -59,6 +59,9 @@ static void summary(mkcurl_response_uptr &res) {
<< "Content Type: " << "Content Type: "
<< mkcurl_response_get_content_type_v2(res.get()) << mkcurl_response_get_content_type_v2(res.get())
<< std::endl << std::endl
<< "HTTP version: "
<< mkcurl_response_get_http_version(res.get())
<< std::endl
<< "=== END SUMMARY ===" << "=== END SUMMARY ==="
<< std::endl << std::endl
<< std::endl; << std::endl;
Expand Down
37 changes: 37 additions & 0 deletions mkcurl.h
Expand Up @@ -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. /// 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); 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. /// mkcurl_response_delete deletes @p res. Note that @p res MAY be null.
void mkcurl_response_delete(mkcurl_response_t *res); void mkcurl_response_delete(mkcurl_response_t *res);


Expand Down Expand Up @@ -358,6 +364,8 @@ struct mkcurl_response {
std::string certs; std::string certs;
// content_type is the response content type. // content_type is the response content type.
std::string 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 // mkcurl_log appends @p line to @p logs. It adds information on the current
Expand Down Expand Up @@ -455,6 +463,13 @@ const char *mkcurl_response_get_content_type_v2(const mkcurl_response_t *res) {
return res->content_type.c_str(); 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; } void mkcurl_response_delete(mkcurl_response_t *res) { delete res; }


// mkcurl_deleter is a custom deleter for a CURL handle. // mkcurl_deleter is a custom deleter for a CURL handle.
Expand Down Expand Up @@ -842,6 +857,28 @@ mkcurl_response_t *mkcurl_request_perform_nonnull(const mkcurl_request_t *req) {
} }
if (ct != nullptr) res->content_type = ct; 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"); mkcurl_log(res->logs, "curl_easy_perform() success");
return res.release(); return res.release();
} }
Expand Down

0 comments on commit dea2a85

Please sign in to comment.