diff --git a/src/factory/HttpTaskImpl.cc b/src/factory/HttpTaskImpl.cc index d44c5324..ffcd7711 100644 --- a/src/factory/HttpTaskImpl.cc +++ b/src/factory/HttpTaskImpl.cc @@ -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 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, diff --git a/src/protocol/HttpUtil.cc b/src/protocol/HttpUtil.cc index 683b3aca..58244ca4 100644 --- a/src/protocol/HttpUtil.cc +++ b/src/protocol/HttpUtil.cc @@ -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)) diff --git a/src/protocol/HttpUtil.h b/src/protocol/HttpUtil.h index b5c36b51..6306a9ae 100644 --- a/src/protocol/HttpUtil.h +++ b/src/protocol/HttpUtil.h @@ -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); diff --git a/src/protocol/http_parser.c b/src/protocol/http_parser.c index a109a94b..1bb551b0 100644 --- a/src/protocol/http_parser.c +++ b/src/protocol/http_parser.c @@ -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; +} + diff --git a/src/protocol/http_parser.h b/src/protocol/http_parser.h index 8614ec1c..0897ede9 100644 --- a/src/protocol/http_parser.h +++ b/src/protocol/http_parser.h @@ -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 }