Optimize request headers processing (#228)

This commit is contained in:
An Tao 2019-08-27 13:55:52 +08:00 committed by GitHub
parent 7186a74590
commit a0b68fb3c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 13 deletions

View File

@ -42,6 +42,10 @@ std::string nameTransform(const std::string &origName, bool isType)
do
{
pos = str.find("_", startPos);
if (pos == std::string::npos)
{
pos = str.find(".", startPos);
}
if (pos != std::string::npos)
ret += str.substr(startPos, pos - startPos);
else
@ -49,7 +53,7 @@ std::string nameTransform(const std::string &origName, bool isType)
ret += str.substr(startPos);
break;
}
while (str[pos] == '_')
while (str[pos] == '_' || str[pos] == '.')
pos++;
if (str[pos] >= 'a' && str[pos] <= 'z')
str[pos] += ('A' - 'a');

View File

@ -309,7 +309,7 @@ void HttpRequestImpl::addHeader(const char *start,
{
value.resize(value.size() - 1);
}
if (field == "cookie")
if (field.length() == 6 && field == "cookie")
{
LOG_TRACE << "cookies!!!:" << value;
std::string::size_type pos;
@ -349,13 +349,39 @@ void HttpRequestImpl::addHeader(const char *start,
}
else
{
if (field == "content-length")
switch (field.length())
{
_contentLen = std::stoull(value.c_str());
}
else if (field == "expect")
{
_expect = value;
case 6:
if (field == "expect")
{
_expect = value;
}
break;
case 10:
{
if (field == "connection")
{
if (_version == kHttp11)
{
if (value.length() == 5 && value == "close")
_keepAlive = false;
}
else if (value.length() == 10 &&
(value == "Keep-Alive" || value == "keep-alive"))
{
_keepAlive = true;
}
}
}
break;
case 14:
if (field == "content-length")
{
_contentLen = std::stoull(value.c_str());
}
break;
default:
break;
}
_headers.emplace(std::move(field), std::move(value));
}

View File

@ -65,6 +65,7 @@ class HttpRequestImpl : public HttpRequest
_content.clear();
_contentType = CT_TEXT_PLAIN;
_contentTypeString.clear();
_keepAlive = true;
}
trantor::EventLoop *getLoop()
{
@ -74,6 +75,10 @@ class HttpRequestImpl : public HttpRequest
void setVersion(Version v)
{
_version = v;
if (v == kHttp10)
{
_keepAlive = false;
}
}
virtual Version version() const override
@ -378,6 +383,10 @@ class HttpRequestImpl : public HttpRequest
{
return _expect;
}
bool keepAlive() const
{
return _keepAlive;
}
~HttpRequestImpl();
protected:
@ -419,6 +428,7 @@ class HttpRequestImpl : public HttpRequest
trantor::Date _date;
std::unique_ptr<CacheFile> _cacheFilePtr;
std::string _expect;
bool _keepAlive = true;
protected:
std::string _content;

View File

@ -271,11 +271,7 @@ void HttpServer::onRequests(
for (auto &req : requests)
{
const std::string &connection = req->getHeaderBy("connection");
bool _close = connection == "close" ||
(req->getVersion() == HttpRequestImpl::kHttp10 &&
connection != "Keep-Alive");
bool _close = (!req->keepAlive());
bool isHeadMethod = (req->method() == Head);
if (isHeadMethod)
{