Skip to content

Commit

Permalink
Merge pull request #123 from IronsDu/feature/support-http-head
Browse files Browse the repository at this point in the history
http: support HTTP HEAD method.
  • Loading branch information
IronsDu authored Jun 19, 2022
2 parents 415151c + 8679631 commit ed2441f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/brynet/net/http/HttpFormat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class HttpRequest final
void setMethod(HTTP_METHOD protocol)
{
mMethod = protocol;
assert(mMethod > HTTP_METHOD::HTTP_METHOD_HEAD &&
assert(mMethod >= HTTP_METHOD::HTTP_METHOD_HEAD &&
mMethod < HTTP_METHOD::HTTP_METHOD_MAX);
}

Expand Down
19 changes: 18 additions & 1 deletion include/brynet/net/http/HttpParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,26 @@ class HTTPParser
return mIsKeepAlive;
}


bool isCompleted() const
{
return mISCompleted;
}

bool isHeadersCompleted() const
{
return mHeadersISCompleted;
}

bool isHeaderCompletedHandled() const
{
return mHasHandleHeadersCompleted;
}

void setHeaderCompletedIsHandled()
{
mHasHandleHeadersCompleted = true;
}

int method() const
{
// mMethod's value defined in http_method, such as HTTP_GET、HTTP_POST.
Expand Down Expand Up @@ -210,6 +224,7 @@ class HTTPParser
static int sHeadComplete(http_parser* hp)
{
HTTPParser* httpParser = (HTTPParser*) hp->data;
httpParser->mHeadersISCompleted = true;

if (httpParser->mUrl.empty())
{
Expand Down Expand Up @@ -303,6 +318,8 @@ class HTTPParser
bool mIsWebSocket = false;
bool mIsKeepAlive;
bool mISCompleted;
bool mHeadersISCompleted = false;
bool mHasHandleHeadersCompleted = false;

bool mLastWasValue;
std::string mCurrentField;
Expand Down
10 changes: 10 additions & 0 deletions include/brynet/net/http/HttpService.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ class HttpService
if (!httpParser->isCompleted())
{
retlen = httpParser->tryParse(buffer, len);
if (httpParser->isHeadersCompleted() && !httpParser->isHeaderCompletedHandled())
{
httpParser->setHeaderCompletedIsHandled();
const auto& httpCallback = httpSession->getHttpCallback();
if (httpCallback != nullptr)
{
httpCallback(*httpParser, httpSession);
}
}

if (!httpParser->isCompleted())
{
return retlen;
Expand Down

0 comments on commit ed2441f

Please sign in to comment.