Skip to content

Commit

Permalink
trim header value when space or tab
Browse files Browse the repository at this point in the history
  • Loading branch information
ggjulio committed Oct 27, 2021
1 parent 72c7301 commit 3727f6f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions includes/ft/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ void lowerStringInPlace(std::string& s);
void upperStringInPlace(std::string& s);
bool isInteger(std::string& s);
std::string getDate();
std::string trim(const std::string& str,
const std::string& whitespace = " \t");


/// return the number of consecutives same elements in two paths.
bool pathsComponentsAreEqual(const filesystem::path& one, const filesystem::path& two, size_t& nSameComp);
Expand Down
14 changes: 14 additions & 0 deletions src/ft/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ std::string getDate()
return date;
}

std::string trim(const std::string& str,
const std::string& whitespace)
{
const size_t strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return "";

const size_t strEnd = str.find_last_not_of(whitespace);
const size_t strRange = strEnd - strBegin + 1;

return str.substr(strBegin, strRange);
}


// Return true if both paths are equal, else false and tell how many comp are the same by variable nSameComp
bool pathsComponentsAreEqual(const filesystem::path& one, const filesystem::path& two, size_t& nSameComp)
{
Expand Down
2 changes: 1 addition & 1 deletion src/http/messageParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ namespace http
return;
}
if (!name.empty() && isValueField)
req.setHeader(name, value);
req.setHeader(name, ft::trim(value));
else if (name.empty())
areHeadersParsed = true;
name.clear();
Expand Down
2 changes: 1 addition & 1 deletion tests/http_requests/chunked_post
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ POST /test.php HTTP/1.1
Host: localhost:83
Date: Mon, 22 Mar 2004 11:15:03 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Transfer-Encoding: chunked

29
<html><body><p>The file you requested is
Expand Down
2 changes: 1 addition & 1 deletion tests/http_requests/simple_get
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ User-Agent: PostmanRuntime/7.26.10
Accept: */*
Postman-Token: ec250329-5eb0-4d4b-8150-39f294b6aea2
Host: dynamicdns.park-your-domain.com:8080
Accept-Encoding: gzip, deflate, br
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 4
Cookie: ASPSESSIONIDQADTQAQR=JNJLAIGBPIMBDAJPJNIFKIEK
Expand Down

0 comments on commit 3727f6f

Please sign in to comment.