Skip to content

Commit 1d35847

Browse files
committed
Use struct for http_version instead of pair.
1 parent bc71f40 commit 1d35847

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

Release/include/cpprest/http_msg.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,29 @@ namespace client
4646
/// <summary>
4747
/// Represents the HTTP protocol version of a message, as {major, minor}.
4848
/// </summary>
49-
typedef std::pair<uint16_t, uint16_t> http_version;
49+
struct http_version
50+
{
51+
uint8_t major;
52+
uint8_t minor;
53+
54+
inline bool operator==(const http_version& other) const { return major == other.major && minor == other.minor; }
55+
inline bool operator<(const http_version& other) const { return major < other.major || (major == other.major && minor < other.minor); }
56+
57+
inline bool operator!=(const http_version& other) const { return !(*this == other); }
58+
inline bool operator>=(const http_version& other) const { return !(*this < other); }
59+
inline bool operator>(const http_version& other) const { return !(*this < other || *this == other); }
60+
inline bool operator<=(const http_version& other) const { return *this < other || *this == other; }
61+
};
5062

5163
/// <summary>
5264
/// Predefined HTTP protocol versions.
5365
/// </summary>
5466
class http_versions
5567
{
5668
public:
57-
_ASYNCRTIMP const static http_version HTTP_0_9;
58-
_ASYNCRTIMP const static http_version HTTP_1_0;
59-
_ASYNCRTIMP const static http_version HTTP_1_1;
69+
_ASYNCRTIMP static const http_version HTTP_0_9;
70+
_ASYNCRTIMP static const http_version HTTP_1_0;
71+
_ASYNCRTIMP static const http_version HTTP_1_1;
6072
};
6173

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

732744
_ASYNCRTIMP void set_request_uri(const uri&);
733745

734-
const http::http_version& http_version() const { return m_http_version; }
746+
http::http_version http_version() const { return m_http_version; }
735747

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

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

906918
/// <summary>
907919
/// Returns a string representation of the remote IP address.

Release/src/http/common/http_msg.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,8 @@ details::_http_request::_http_request(http::method mtd)
995995
: m_method(std::move(mtd)),
996996
m_initiated_response(0),
997997
m_server_context(),
998-
m_cancellationToken(pplx::cancellation_token::none())
998+
m_cancellationToken(pplx::cancellation_token::none()),
999+
m_http_version(http::http_version{0, 0})
9991000
{
10001001
if(m_method.empty())
10011002
{
@@ -1006,13 +1007,14 @@ details::_http_request::_http_request(http::method mtd)
10061007
details::_http_request::_http_request(std::unique_ptr<http::details::_http_server_context> server_context)
10071008
: m_initiated_response(0),
10081009
m_server_context(std::move(server_context)),
1009-
m_cancellationToken(pplx::cancellation_token::none())
1010+
m_cancellationToken(pplx::cancellation_token::none()),
1011+
m_http_version(http::http_version{0, 0})
10101012
{
10111013
}
10121014

1013-
const http_version http_versions::HTTP_0_9{ 0, 9 };
1014-
const http_version http_versions::HTTP_1_0{ 1, 0 };
1015-
const http_version http_versions::HTTP_1_1{ 1, 1 };
1015+
const http_version http_versions::HTTP_0_9 = { 0, 9 };
1016+
const http_version http_versions::HTTP_1_0 = { 1, 0 };
1017+
const http_version http_versions::HTTP_1_1 = { 1, 1 };
10161018

10171019
#define _METHODS
10181020
#define DAT(a,b) const method methods::a = b;

Release/src/http/listener/http_server_asio.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,17 +649,20 @@ will_deref_and_erase_t asio_server_connection::handle_http_line(const boost::sys
649649
// Get the version
650650
std::string http_version = http_path_and_version.substr(http_path_and_version.size() - VersionPortionSize + 1, VersionPortionSize - 2);
651651

652+
auto m_request_impl = m_request._get_impl().get();
653+
web::http::http_version parsed_version = { 0, 0 };
652654
if (boost::starts_with(http_version, "HTTP/"))
653655
{
654656
std::istringstream version{ http_version.substr(5) };
655-
unsigned int major = 0; version >> major;
657+
version >> parsed_version.major;
656658
char dot; version >> dot;
657-
unsigned int minor = 0; version >> minor;
658-
m_request._get_impl()->_set_http_version({ (uint16_t)major, (uint16_t)minor });
659+
version >> parsed_version.minor;
660+
661+
m_request_impl->_set_http_version(parsed_version);
659662
}
660663

661664
// if HTTP version is 1.0 then disable pipelining
662-
if (http_version == "HTTP/1.0")
665+
if (parsed_version == web::http::http_versions::HTTP_1_0)
663666
{
664667
m_close = true;
665668
}

Release/src/http/listener/http_server_httpsys.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,7 @@ void windows_request_context::read_headers_io_completion(DWORD error_code, DWORD
557557
m_msg.set_method(parse_request_method(m_request));
558558
parse_http_headers(m_request->Headers, m_msg.headers());
559559

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

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

0 commit comments

Comments
 (0)