Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions include/tscore/HTTPVersion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/** @file

HTTPVersion - class to track the HTTP version

@section license License

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

class HTTPVersion
{
public:
HTTPVersion() {}
HTTPVersion(HTTPVersion const &that) = default;
explicit HTTPVersion(int version);
constexpr HTTPVersion(uint8_t ver_major, uint8_t ver_minor);

int operator==(const HTTPVersion &hv) const;
int operator!=(const HTTPVersion &hv) const;
int operator>(const HTTPVersion &hv) const;
int operator<(const HTTPVersion &hv) const;
int operator>=(const HTTPVersion &hv) const;
int operator<=(const HTTPVersion &hv) const;

uint8_t get_major() const;
uint8_t get_minor() const;
int get_flat_version() const;

private:
uint8_t vmajor = 0;
uint8_t vminor = 0;
};

/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/

inline HTTPVersion::HTTPVersion(int version)
{
vmajor = (version >> 16) & 0xFFFF;
vminor = version & 0xFFFF;
}

/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/

inline constexpr HTTPVersion::HTTPVersion(uint8_t ver_major, uint8_t ver_minor) : vmajor(ver_major), vminor(ver_minor) {}

/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/

inline uint8_t
HTTPVersion::get_major() const
{
return vmajor;
}

/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/

inline uint8_t
HTTPVersion::get_minor() const
{
return vminor;
}

/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/

inline int
HTTPVersion::get_flat_version() const
{
return vmajor << 16 | vminor;
}

/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/

inline int
HTTPVersion::operator==(const HTTPVersion &hv) const
{
return vmajor == hv.get_major() && vminor == hv.get_minor();
}

/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/

inline int
HTTPVersion::operator!=(const HTTPVersion &hv) const
{
return !(*this == hv);
}

/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/

inline int
HTTPVersion::operator>(const HTTPVersion &hv) const
{
return vmajor > hv.get_major() || (vmajor == hv.get_major() && vminor > hv.get_minor());
}

/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/

inline int
HTTPVersion::operator<(const HTTPVersion &hv) const
{
return vmajor < hv.get_major() || (vmajor == hv.get_major() && vminor < hv.get_minor());
}

/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/

inline int
HTTPVersion::operator>=(const HTTPVersion &hv) const
{
return vmajor > hv.get_major() || (vmajor == hv.get_major() && vminor >= hv.get_minor());
}

/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/

inline int
HTTPVersion::operator<=(const HTTPVersion &hv) const
{
return vmajor < hv.get_major() || (vmajor == hv.get_major() && vminor <= hv.get_minor());
}

/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/

constexpr HTTPVersion HTTP_INVALID{0, 0};
constexpr HTTPVersion HTTP_0_9{0, 9};
constexpr HTTPVersion HTTP_1_0{1, 0};
constexpr HTTPVersion HTTP_1_1{1, 1};
constexpr HTTPVersion HTTP_2_0{2, 0};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not need a HTTP_3_0 for QUIC?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will once we support QUIC to origin.

14 changes: 5 additions & 9 deletions iocore/hostdb/I_HostDBProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "tscore/CryptoHash.h"
#include "tscore/ink_align.h"
#include "tscore/ink_resolver.h"
#include "tscore/HTTPVersion.h"
#include "I_EventSystem.h"
#include "SRV.h"
#include "P_RefCountCache.h"
Expand Down Expand Up @@ -99,31 +100,26 @@ union HostDBApplicationInfo {
unsigned int application2;
} allotment;

enum HttpVersion : uint8_t {
HTTP_VERSION_UNDEFINED = 0,
HTTP_VERSION_09 = 1,
HTTP_VERSION_10 = 2,
HTTP_VERSION_11 = 3,
};

//////////////////////////////////////////////////////////
// http server attributes in the host database //
// //
// http_version - one of HttpVersion //
// http_version - one of HTTPVersion //
// last_failure - UNIX time for the last time //
// we tried the server & failed //
// fail_count - Number of times we tried and //
// and failed to contact the host //
//////////////////////////////////////////////////////////
struct http_server_attr {
uint32_t last_failure;
HttpVersion http_version;
HTTPVersion http_version;
uint8_t fail_count;
http_server_attr() : http_version() {}
} http_data;

struct application_data_rr {
unsigned int offset;
} rr;
HostDBApplicationInfo() : http_data() {}
};

struct HostDBRoundRobin;
Expand Down
39 changes: 19 additions & 20 deletions proxy/hdrs/HTTP.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ http_hdr_init(HdrHeap *heap, HTTPHdrImpl *hh, HTTPType polarity)
{
memset(&(hh->u), 0, sizeof(hh->u));
hh->m_polarity = polarity;
hh->m_version = HTTP_VERSION(1, 0);
hh->m_version = HTTP_1_0;
hh->m_fields_impl = mime_hdr_create(heap);
if (polarity == HTTP_TYPE_REQUEST) {
hh->u.req.m_url_impl = url_create(heap);
Expand Down Expand Up @@ -353,19 +353,19 @@ http_hdr_clone(HTTPHdrImpl *s_hh, HdrHeap *s_heap, HdrHeap *d_heap)
-------------------------------------------------------------------------*/

static inline char *
http_hdr_version_to_string(int32_t version, char *buf9)
http_hdr_version_to_string(const HTTPVersion &version, char *buf9)
{
ink_assert(HTTP_MAJOR(version) < 10);
ink_assert(HTTP_MINOR(version) < 10);
ink_assert(version.get_major() < 10);
ink_assert(version.get_minor() < 10);

buf9[0] = 'H';
buf9[1] = 'T';
buf9[2] = 'T';
buf9[3] = 'P';
buf9[4] = '/';
buf9[5] = '0' + HTTP_MAJOR(version);
buf9[5] = '0' + version.get_major();
buf9[6] = '.';
buf9[7] = '0' + HTTP_MINOR(version);
buf9[7] = '0' + version.get_minor();
buf9[8] = '\0';

return (buf9);
Expand All @@ -375,7 +375,7 @@ http_hdr_version_to_string(int32_t version, char *buf9)
-------------------------------------------------------------------------*/

int
http_version_print(int32_t version, char *buf, int bufsize, int *bufindex, int *dumpoffset)
http_version_print(const HTTPVersion &version, char *buf, int bufsize, int *bufindex, int *dumpoffset)
{
#define TRY(x) \
if (!x) \
Expand Down Expand Up @@ -626,7 +626,7 @@ http_hdr_type_set(HTTPHdrImpl *hh, HTTPType type)
-------------------------------------------------------------------------*/

void
http_hdr_version_set(HTTPHdrImpl *hh, int32_t ver)
http_hdr_version_set(HTTPHdrImpl *hh, const HTTPVersion &ver)
{
hh->m_version = ver;
}
Expand Down Expand Up @@ -929,7 +929,7 @@ http_parser_parse_req(HTTPParser *parser, HdrHeap *heap, HTTPHdrImpl *hh, const
goto slow_case;
}

int32_t version = HTTP_VERSION(end[-5] - '0', end[-3] - '0');
HTTPVersion version{static_cast<uint8_t>(end[-5] - '0'), static_cast<uint8_t>(end[-3] - '0')};

http_hdr_method_set(heap, hh, &(cur[0]), hdrtoken_wks_to_index(HTTP_METHOD_GET), 3, must_copy_strings);
ink_assert(hh->u.req.m_url_impl != nullptr);
Expand All @@ -943,7 +943,7 @@ http_parser_parse_req(HTTPParser *parser, HdrHeap *heap, HTTPHdrImpl *hh, const

end = real_end;
parser->m_parsing_http = false;
if (version == HTTP_VERSION(0, 9)) {
if (version == HTTP_0_9) {
return PARSE_RESULT_ERROR;
}

Expand Down Expand Up @@ -1087,14 +1087,14 @@ http_parser_parse_req(HTTPParser *parser, HdrHeap *heap, HTTPHdrImpl *hh, const
return err;
}

int32_t version;
HTTPVersion version;
if (version_start && version_end) {
version = http_parse_version(version_start, version_end);
} else {
return PARSE_RESULT_ERROR;
}

if (version == HTTP_VERSION(0, 9)) {
if (version == HTTP_0_9) {
return PARSE_RESULT_ERROR;
}

Expand Down Expand Up @@ -1270,7 +1270,7 @@ http_parser_parse_resp(HTTPParser *parser, HdrHeap *heap, HTTPHdrImpl *hh, const
--reason_end;
}

int32_t version = HTTP_VERSION(cur[5] - '0', cur[7] - '0');
HTTPVersion version(cur[5] - '0', cur[7] - '0');
HTTPStatus status = static_cast<HTTPStatus>((cur[9] - '0') * 100 + (cur[10] - '0') * 10 + (cur[11] - '0'));

http_hdr_version_set(hh, version);
Expand Down Expand Up @@ -1379,10 +1379,9 @@ http_parser_parse_resp(HTTPParser *parser, HdrHeap *heap, HTTPHdrImpl *hh, const
return PARSE_RESULT_ERROR;
}

int32_t version;
version = http_parse_version(version_start, version_end);
HTTPVersion version = http_parse_version(version_start, version_end);

if (version == HTTP_VERSION(0, 9)) {
if (version == HTTP_0_9) {
return PARSE_RESULT_ERROR;
}

Expand Down Expand Up @@ -1429,14 +1428,14 @@ http_parse_status(const char *start, const char *end)
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/

int32_t
HTTPVersion
http_parse_version(const char *start, const char *end)
{
int maj;
int min;

if ((end - start) < 8) {
return HTTP_VERSION(0, 9);
return HTTP_0_9;
}

if (((start[0] == 'H') || (start[0] == 'h')) && ((start[1] == 'T') || (start[1] == 't')) &&
Expand All @@ -1460,10 +1459,10 @@ http_parse_version(const char *start, const char *end)
start += 1;
}

return HTTP_VERSION(maj, min);
return HTTPVersion(maj, min);
}

return HTTP_VERSION(0, 9);
return HTTP_0_9;
}

/*-------------------------------------------------------------------------
Expand Down
Loading