Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable erasing HTTP message's header. #1492

Merged
merged 2 commits into from
Feb 14, 2024
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
25 changes: 17 additions & 8 deletions src/factory/HttpTaskImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,15 @@ CommMessageOut *ComplexHttpTask::message_out()
this->keep_alive_timeo = 0;
else if (req->has_keep_alive_header())
{
HttpHeaderCursor req_cursor(req);
HttpHeaderCursor cursor(req);

//req---Connection: Keep-Alive
//req---Keep-Alive: timeout=0,max=100
header.name = "Keep-Alive";
header.name_len = strlen("Keep-Alive");
if (req_cursor.find(&header))
header.value = NULL;
header.value_len = 0;
if (cursor.find(&header))
{
std::string keep_alive((const char *)header.value, header.value_len);
std::vector<std::string> params = StringUtil::split(keep_alive, ',');
Expand Down Expand Up @@ -174,8 +176,14 @@ int ComplexHttpTask::keep_alive_timeout()
void ComplexHttpTask::set_empty_request()
{
HttpRequest *client_req = this->get_req();
HttpHeaderCursor cursor(client_req);
struct HttpMessageHeader header = {
.name = "Host",
.name_len = strlen("Host"),
};

client_req->set_request_uri("/");
client_req->set_header_pair("Host", "");
cursor.find_and_erase(&header);
}

void ComplexHttpTask::init_failed()
Expand Down Expand Up @@ -811,12 +819,13 @@ void WFHttpServerTask::handle(int state, int error)
req_is_alive_ = this->req.is_keep_alive();
if (req_is_alive_ && this->req.has_keep_alive_header())
{
HttpHeaderCursor req_cursor(&this->req);
struct HttpMessageHeader header;
HttpHeaderCursor cursor(&this->req);
struct HttpMessageHeader header = {
.name = "Keep-Alive",
.name_len = strlen("Keep-Alive"),
};

header.name = "Keep-Alive";
header.name_len = strlen("Keep-Alive");
req_has_keep_alive_header_ = req_cursor.find(&header);
req_has_keep_alive_header_ = cursor.find(&header);
if (req_has_keep_alive_header_)
{
req_keep_alive_.assign((const char *)header.value,
Expand Down
11 changes: 10 additions & 1 deletion src/protocol/HttpUtil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ bool HttpHeaderCursor::find(const std::string& name, std::string& value)
{
struct HttpMessageHeader header = {
.name = name.c_str(),
.name_len = name.size()
.name_len = name.size(),
};

if (this->find(&header))
Expand All @@ -420,6 +420,15 @@ bool HttpHeaderCursor::find(const std::string& name, std::string& value)
return false;
}

bool HttpHeaderCursor::find_and_erase(const std::string& name)
{
struct HttpMessageHeader header = {
.name = name.c_str(),
.name_len = name.size(),
};
return this->find_and_erase(&header);
}

HttpChunkCursor::HttpChunkCursor(const HttpMessage *msg)
{
if (msg->get_parsed_body(&this->body, &this->body_len))
Expand Down
16 changes: 16 additions & 0 deletions src/protocol/HttpUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,15 @@ class HttpHeaderCursor
public:
bool next(struct HttpMessageHeader *header);
bool find(struct HttpMessageHeader *header);
bool erase();
bool find_and_erase(struct HttpMessageHeader *header);
void rewind();

/* std::string interface */
public:
bool next(std::string& name, std::string& value);
bool find(const std::string& name, std::string& value);
bool find_and_erase(const std::string& name);

protected:
http_header_cursor_t cursor;
Expand Down Expand Up @@ -205,6 +208,19 @@ inline bool HttpHeaderCursor::find(struct HttpMessageHeader *header)
&this->cursor) == 0;
}

inline bool HttpHeaderCursor::erase()
{
return http_header_cursor_erase(&this->cursor) == 0;
}

inline bool HttpHeaderCursor::find_and_erase(struct HttpMessageHeader *header)
{
if (this->find(header))
return this->erase();

return false;
}

inline void HttpHeaderCursor::rewind()
{
http_header_cursor_rewind(&this->cursor);
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/TLVMessage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int TLVMessage::append(const void *buf, size_t *size)
*size = n + head_left;
}

this->value.append((const char *)buf, (const char *)buf + n);
this->value.append((const char *)buf, n);
return this->value.size() == *this->head;
}

Expand Down
19 changes: 19 additions & 0 deletions src/protocol/http_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,3 +832,22 @@ int http_header_cursor_find(const void *name, size_t name_len,
return 1;
}

int http_header_cursor_erase(http_header_cursor_t *cursor)
{
struct __header_line *line;

if (cursor->next != cursor->head)
{
line = list_entry(cursor->next, struct __header_line, list);
cursor->next = cursor->next->prev;
list_del(&line->list);
if (line->buf != (char *)(line + 1))
free(line->buf);

free(line);
return 0;
}

return 1;
}

1 change: 1 addition & 0 deletions src/protocol/http_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ int http_header_cursor_next(const void **name, size_t *name_len,
int http_header_cursor_find(const void *name, size_t name_len,
const void **value, size_t *value_len,
http_header_cursor_t *cursor);
int http_header_cursor_erase(http_header_cursor_t *cursor);

#ifdef __cplusplus
}
Expand Down
Loading