Skip to content
Merged
Prev Previous commit
Next Next commit
Use struct for http_version instead of pair.
  • Loading branch information
ras0219-msft committed Jan 24, 2018
commit 1d358474c02e2c4c3e2043b97e04d0777be2fb90
24 changes: 18 additions & 6 deletions Release/include/cpprest/http_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,29 @@ namespace client
/// <summary>
/// Represents the HTTP protocol version of a message, as {major, minor}.
/// </summary>
typedef std::pair<uint16_t, uint16_t> http_version;
struct http_version
{
uint8_t major;
uint8_t minor;

inline bool operator==(const http_version& other) const { return major == other.major && minor == other.minor; }
inline bool operator<(const http_version& other) const { return major < other.major || (major == other.major && minor < other.minor); }

inline bool operator!=(const http_version& other) const { return !(*this == other); }
inline bool operator>=(const http_version& other) const { return !(*this < other); }
inline bool operator>(const http_version& other) const { return !(*this < other || *this == other); }
inline bool operator<=(const http_version& other) const { return *this < other || *this == other; }
};

/// <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;
_ASYNCRTIMP static const http_version HTTP_0_9;
_ASYNCRTIMP static const http_version HTTP_1_0;
_ASYNCRTIMP static const http_version HTTP_1_1;
};

/// <summary>
Expand Down Expand Up @@ -731,7 +743,7 @@ class _http_request final : public http::details::http_msg_base, public std::ena

_ASYNCRTIMP void set_request_uri(const uri&);

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

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

Expand Down Expand Up @@ -901,7 +913,7 @@ class http_request
/// Returns the HTTP protocol version of this request message.
/// </summary>
/// <returns>The HTTP protocol version.</returns>
const http::http_version& http_version() const { return _m_impl->http_version(); }
http::http_version http_version() const { return _m_impl->http_version(); }

/// <summary>
/// Returns a string representation of the remote IP address.
Expand Down
12 changes: 7 additions & 5 deletions Release/src/http/common/http_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,8 @@ details::_http_request::_http_request(http::method mtd)
: m_method(std::move(mtd)),
m_initiated_response(0),
m_server_context(),
m_cancellationToken(pplx::cancellation_token::none())
m_cancellationToken(pplx::cancellation_token::none()),
m_http_version(http::http_version{0, 0})
{
if(m_method.empty())
{
Expand All @@ -1006,13 +1007,14 @@ details::_http_request::_http_request(http::method mtd)
details::_http_request::_http_request(std::unique_ptr<http::details::_http_server_context> server_context)
: m_initiated_response(0),
m_server_context(std::move(server_context)),
m_cancellationToken(pplx::cancellation_token::none())
m_cancellationToken(pplx::cancellation_token::none()),
m_http_version(http::http_version{0, 0})
{
}

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 };
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;
Expand Down
11 changes: 7 additions & 4 deletions Release/src/http/listener/http_server_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,17 +649,20 @@ 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);

auto m_request_impl = m_request._get_impl().get();
web::http::http_version parsed_version = { 0, 0 };
if (boost::starts_with(http_version, "HTTP/"))
{
std::istringstream version{ http_version.substr(5) };
unsigned int major = 0; version >> major;
version >> parsed_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 });
version >> parsed_version.minor;

m_request_impl->_set_http_version(parsed_version);
}

// if HTTP version is 1.0 then disable pipelining
if (http_version == "HTTP/1.0")
if (parsed_version == web::http::http_versions::HTTP_1_0)
{
m_close = true;
}
Expand Down
3 changes: 1 addition & 2 deletions Release/src/http/listener/http_server_httpsys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,7 @@ 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 });
m_msg._get_impl()->_set_http_version({ (uint8_t)m_request->Version.MajorVersion, (uint8_t)m_request->Version.MinorVersion });

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