Skip to content
Merged
Next Next commit
Grab HTTP protocol version from the underlying request message and ex…
…pose in http_request
  • Loading branch information
garethsb committed Oct 24, 2017
commit f8b50f9da4c4ff8c6be198ce4156e895235437b4
28 changes: 28 additions & 0 deletions Release/include/cpprest/http_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ namespace client
class http_client;
}

/// <summary>
/// Represents the HTTP protocol version of a message, as {major, minor}.
/// </summary>
typedef std::pair<uint16_t, uint16_t> http_version;

/// <summary>
/// Predefined HTTP protocol versions.
/// </summary>
class http_versions
{
public:
_ASYNCRTIMP const static http_version HTTP_0_9;
_ASYNCRTIMP const static http_version HTTP_1_0;
_ASYNCRTIMP const static http_version HTTP_1_1;
};

/// <summary>
/// Predefined method strings for the standard HTTP methods mentioned in the
/// HTTP 1.1 specification.
Expand Down Expand Up @@ -715,6 +731,8 @@ class _http_request final : public http::details::http_msg_base, public std::ena

_ASYNCRTIMP void set_request_uri(const uri&);

const http_version& http_version() const { return m_http_version; }

const utility::string_t& remote_address() const { return m_remote_address; }

const pplx::cancellation_token &cancellation_token() const { return m_cancellationToken; }
Expand Down Expand Up @@ -757,6 +775,8 @@ class _http_request final : public http::details::http_msg_base, public std::ena

void _set_base_uri(const http::uri &base_uri) { m_base_uri = base_uri; }

void _set_http_version(const http::http_version &http_version) { m_http_version = http_version; }

void _set_remote_address(const utility::string_t &remote_address) { m_remote_address = remote_address; }

private:
Expand All @@ -783,6 +803,8 @@ class _http_request final : public http::details::http_msg_base, public std::ena

pplx::task_completion_event<http_response> m_response;

http::http_version m_http_version;

utility::string_t m_remote_address;
};

Expand Down Expand Up @@ -875,6 +897,12 @@ class http_request
/// </remarks>
const http_headers &headers() const { return _m_impl->headers(); }

/// <summary>
/// Returns the HTTP protocol version of this request message.
/// </summary>
/// <returns>The HTTP protocol version.</returns>
const http_version& get_http_version() const { return _m_impl->http_version(); }

/// <summary>
/// Returns a string representation of the remote IP address.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions Release/src/http/common/http_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,10 @@ details::_http_request::_http_request(std::unique_ptr<http::details::_http_serve
{
}

const http_version http_versions::HTTP_0_9{ 0, 9 };
const http_version http_versions::HTTP_1_0{ 1, 0 };
const http_version http_versions::HTTP_1_1{ 1, 1 };

#define _METHODS
#define DAT(a,b) const method methods::a = b;
#include "cpprest/details/http_constants.dat"
Expand Down
10 changes: 10 additions & 0 deletions Release/src/http/listener/http_server_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,16 @@ will_deref_and_erase_t asio_server_connection::handle_http_line(const boost::sys

// Get the version
std::string http_version = http_path_and_version.substr(http_path_and_version.size() - VersionPortionSize + 1, VersionPortionSize - 2);

if (boost::starts_with(http_version, "HTTP/"))
{
std::istringstream version{ http_version.substr(5) };
unsigned int major = 0; version >> major;
char dot; version >> dot;
unsigned int minor = 0; version >> minor;
m_request._get_impl()->_set_http_version({ (uint16_t)major, (uint16_t)minor });
}

// if HTTP version is 1.0 then disable pipelining
if (http_version == "HTTP/1.0")
{
Expand Down
3 changes: 3 additions & 0 deletions Release/src/http/listener/http_server_httpsys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,9 @@ void windows_request_context::read_headers_io_completion(DWORD error_code, DWORD
m_msg.set_method(parse_request_method(m_request));
parse_http_headers(m_request->Headers, m_msg.headers());

// Get the version
m_msg._get_impl()->_set_http_version({ m_request->Version.MajorVersion, m_request->Version.MinorVersion });

// Retrieve the remote IP address
std::vector<wchar_t> remoteAddressBuffer(50);

Expand Down