Skip to content

Commit

Permalink
minor improvements to http (#235)
Browse files Browse the repository at this point in the history
* minor improvements to http
  • Loading branch information
lihuiba authored Nov 7, 2023
1 parent ec0f92d commit 8a48dae
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 34 deletions.
8 changes: 2 additions & 6 deletions common/estring.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,8 @@ class rstring_view // relative string_view, that stores values relative
}

public:
constexpr rstring_view() = default; // note: don't give _offset or _length
// an initial value and do not
// assigned them in default ctor()
// it would caused verb_init() error
rstring_view(uint64_t offset, uint64_t length)
{
constexpr rstring_view() = default;
constexpr rstring_view(uint64_t offset, uint64_t length) {
assert(offset <= std::numeric_limits<OffsetType>::max());
assert(length <= std::numeric_limits<LengthType>::max());
_offset = (OffsetType)offset;
Expand Down
5 changes: 0 additions & 5 deletions net/http/headers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ HeadersBase::iterator HeadersBase::find(std::string_view key) const {
return {this, (uint16_t)(it - kv_begin())};
}

void buf_append(char*& ptr, std::string_view sv) {
memcpy(ptr, sv.data(), sv.size());
ptr += sv.size();
}

void buf_append(char*& ptr, uint64_t x) {
auto begin = ptr;
do {
Expand Down
6 changes: 5 additions & 1 deletion net/http/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ namespace photon {
namespace net {
namespace http {

void buf_append(char*& ptr, std::string_view sv);
void buf_append(char*& ptr, uint64_t x);

inline void buf_append(char*& ptr, std::string_view sv) {
memcpy(ptr, sv.data(), sv.size());
ptr += sv.size();
}

class HeadersBase {
public:
HeadersBase() = default;
Expand Down
35 changes: 16 additions & 19 deletions net/http/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Message::~Message() {
delete m_stream;
}

if (m_buf_ownership && m_buf)
if (m_buf_ownership)
free(m_buf);
}

Expand Down Expand Up @@ -105,11 +105,14 @@ int Message::append_bytes(uint16_t size) {
return parse_res;
}

LOG_DEBUG("add headers, header buf size `", m_buf + m_buf_capacity - p.cur());
if (headers.reset(p.cur(), m_buf + m_buf_capacity - p.cur(), m_buf + m_buf_size - p.cur()) < 0)
auto buf_cap = m_buf + m_buf_capacity - p.cur();
auto buf_size = m_buf + m_buf_size - p.cur();
LOG_DEBUG("add headers: ", VALUE(buf_cap), VALUE(buf_size));
if (headers.reset(p.cur(), buf_cap, buf_size) < 0)
LOG_ERRNO_RETURN(0, -1, "failed to parse headers");

m_abandon = (headers.get_value("Connection") == "close") || (!headers.get_value("Trailer").empty());
m_abandon = (headers["Connection"] == "close" ||
!headers["Trailer"].empty());
message_status = HEADER_PARSED;
LOG_DEBUG("header parsed");
return 0;
Expand All @@ -118,11 +121,9 @@ int Message::append_bytes(uint16_t size) {
int Message::send_header(net::ISocketStream* stream) {
if (stream != nullptr) m_stream = stream; // update stream if needed

if (m_keep_alive)
headers.insert("Connection", "keep-alive");
else
headers.insert("Connection", "close");

using SV = std::string_view;
headers.insert("Connection", m_keep_alive ? SV("keep-alive") :
SV("close"));
if (headers.space_remain() < 2)
LOG_ERRNO_RETURN(ENOBUFS, -1, "no buffer");

Expand Down Expand Up @@ -322,17 +323,15 @@ int Request::reset(Verb v, std::string_view url, bool enable_proxy) {

int Request::redirect(Verb v, estring_view location, bool enable_proxy) {
estring full_location;
if (!location.starts_with(http_url_scheme) && (!location.starts_with(https_url_scheme))) {
if (!location.starts_with(http_url_scheme) &&
!location.starts_with(https_url_scheme)) {
full_location.appends(secure() ? https_url_scheme : http_url_scheme,
host(), location);
location = full_location;
}
StoredURL u(location);
auto new_request_line_size = verbstr[v].size() + sizeof(" HTTP/1.1\r\n");
if (enable_proxy)
new_request_line_size += full_url_size(u);
else
new_request_line_size += u.target().size();
auto new_request_line_size = verbstr[v].size() + sizeof(" HTTP/1.1\r\n") +
enable_proxy ? full_url_size(u) : u.target().size();

auto delta = new_request_line_size - m_buf_size;
LOG_DEBUG(VALUE(delta));
Expand Down Expand Up @@ -381,11 +380,9 @@ int Response::set_result(int code, std::string_view reason) {
char* buf = m_buf;
m_status_code = code;
buf_append(buf, "HTTP/1.1 ");
buf_append(buf, std::to_string(code));
buf_append(buf, code);
buf_append(buf, " ");
auto message = reason;
if (message.empty()) message = obsolete_reason(code);
buf_append(buf, message);
buf_append(buf, reason.size() ? reason : obsolete_reason(code));
buf_append(buf, "\r\n");
m_buf_size = buf - m_buf;
headers.reset(m_buf + m_buf_size, m_buf_capacity - m_buf_size);
Expand Down
2 changes: 1 addition & 1 deletion net/http/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Message : public IStream {
void reset(void* buf, uint16_t buf_capacity, bool buf_ownership = false,
ISocketStream* s = nullptr, bool stream_ownership = false) {
reset(s, stream_ownership);
if (m_buf_ownership && m_buf) {
if (m_buf_ownership) {
free(m_buf);
}
m_buf = (char*)buf;
Expand Down
101 changes: 99 additions & 2 deletions net/http/status.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,103 @@ limitations under the License.
#include "photon/common/string_view.h"
#include "message.h"

struct SV : public std::string_view {
template<size_t N>
constexpr SV(const char(&s)[N]) : SV(s, N) { }
constexpr SV(const char* s, size_t n) : std::string_view(s, n) { }
};

constexpr static SV code_str[] = {
/*100*/ "Continue",
/*101*/ "Switching Protocols",
/*102*/ "Processing",
/*103*/ "Early Hints",

/*200*/ "OK",
/*201*/ "Created",
/*202*/ "Accepted",
/*203*/ "Non-Authoritative Information",
/*204*/ "No Content",
/*205*/ "Reset Content",
/*206*/ "Partial Content",
/*207*/ "Multi-Status",
/*208*/ "Already Reported",
// /*226*/ "IM Used",

/*300*/ "Multiple Choices",
/*301*/ "Moved Permanently",
/*302*/ "Found",
/*303*/ "See Other",
/*304*/ "Not Modified",
/*305*/ "Use Proxy",
/*306*/ {0, 0},
/*307*/ "Temporary Redirect",
/*308*/ "Permanent Redirect",

/*400*/ "Bad Request",
/*401*/ "Unauthorized",
/*402*/ "Payment Required",
/*403*/ "Forbidden",
/*404*/ "Not Found",
/*405*/ "Method Not Allowed",
/*406*/ "Not Acceptable",
/*407*/ "Proxy Authentication Required",
/*408*/ "Request Timeout",
/*409*/ "Conflict",
/*410*/ "Gone",
/*411*/ "Length Required",
/*412*/ "Precondition Failed",
/*413*/ "Content Too Large",
/*414*/ "URI Too Long",
/*415*/ "Unsupported Media Type",
/*416*/ "Range Not Satisfiable",
/*417*/ "Expectation Failed",
/*418*/ "I'm a teapot",
/*419*/ {0, 0},
/*420*/ {0, 0},
/*421*/ "Misdirected Request",
/*422*/ "Unprocessable Content",
/*423*/ "Locked",
/*424*/ "Failed Dependency",
/*425*/ "Too Early",
/*426*/ "Upgrade Required",
/*427*/ {0, 0},
/*428*/ "Precondition Required",
/*429*/ "Too Many Requests",
/*430*/ {0, 0},
/*431*/ "Request Header Fields Too Large",
// /*451*/ "Unavailable For Legal Reasons",

/*500*/ "Internal Server Error",
/*501*/ "Not Implemented",
/*502*/ "Bad Gateway",
/*503*/ "Service Unavailable",
/*504*/ "Gateway Timeout",
/*505*/ "HTTP Version Not Supported",
/*506*/ "Variant Also Negotiates",
/*507*/ "Insufficient Storage",
/*508*/ "Loop Detected",
/*509*/ {0, 0},
/*510*/ "Not Extended",
/*511*/ "Network Authentication Required",
};

const static uint8_t LEN1xx = 4;
const static uint8_t LEN2xx = 9 + LEN1xx;
const static uint8_t LEN3xx = 9 + LEN2xx;
const static uint8_t LEN4xx = 32 + LEN3xx;
const static uint8_t LEN5xx = 12 + LEN4xx;
uint8_t entries[] = {0, LEN1xx, LEN2xx, LEN3xx, LEN4xx, LEN5xx};

std::string_view photon::net::http::obsolete_reason(int code) {
uint8_t major = code / 100 - 1;
uint8_t minor = code % 100;
if (unlikely(major > 4)) return {};
uint8_t max = entries[major+1] - entries[major];
if (unlikely(minor >= max)) return {};
return code_str[entries[major] + minor];

/*
switch (code){
case 100: return "Continue";
Expand Down Expand Up @@ -87,6 +183,7 @@ std::string_view photon::net::http::obsolete_reason(int code) {
case 510: return "Not Extended";
case 511: return "Network Authentication Required";
default: return "";
default: return { };
}
}
*/
}

0 comments on commit 8a48dae

Please sign in to comment.