Enable erasing HTTP message's header.

This commit is contained in:
Xie Han
2024-02-14 01:12:03 +08:00
committed by xiehan
parent bae54fdf96
commit 00a3267c8d
5 changed files with 63 additions and 9 deletions
+17 -8
View File
@@ -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, ',');
@@ -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()
@@ -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,
+10 -1
View File
@@ -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))
@@ -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))
+16
View File
@@ -150,12 +150,15 @@ public:
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;
@@ -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);
+19
View File
@@ -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
View File
@@ -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
}