Socket and HTTP Response bug fixes
fr::TcpSocket checked if the recv response was greater than 0, which created issues as 0 is a valid result if no data was received and no error occurred. fr::HttpResponse::construct() now adds in a content-length header. fr::HttpResponse::parse trims the body if it's larger than content length. fr::Http::set_uri() now prepends a '/' if one's not provided. Fixed fr::Http::parse_header from cutting off the end of headers.
This commit is contained in:
parent
36f4006343
commit
df509a42f5
@ -41,7 +41,7 @@ namespace fr
|
|||||||
*
|
*
|
||||||
* @param header_end_pos The position in 'body' of the end of the header
|
* @param header_end_pos The position in 'body' of the end of the header
|
||||||
*/
|
*/
|
||||||
bool parse_header(int32_t header_end_pos);
|
bool parse_header(int64_t header_end_pos);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Parses the POST data from the body
|
* Parses the POST data from the body
|
||||||
|
|||||||
10
src/Http.cpp
10
src/Http.cpp
@ -119,7 +119,15 @@ namespace fr
|
|||||||
|
|
||||||
void Http::set_uri(const std::string &str)
|
void Http::set_uri(const std::string &str)
|
||||||
{
|
{
|
||||||
|
//Don't do anything if there's no URI provided
|
||||||
|
if(str.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
//Ensure that URI begins with a /
|
||||||
|
if(str.front() == '/')
|
||||||
uri = str;
|
uri = str;
|
||||||
|
else
|
||||||
|
uri = "/" + str;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Http::request_type_to_string(RequestType type) const
|
std::string Http::request_type_to_string(RequestType type) const
|
||||||
@ -234,7 +242,7 @@ namespace fr
|
|||||||
{
|
{
|
||||||
std::string header_name = str.substr(0, colon_pos);
|
std::string header_name = str.substr(0, colon_pos);
|
||||||
std::transform(header_name.begin(), header_name.end(), header_name.begin(), ::tolower);
|
std::transform(header_name.begin(), header_name.end(), header_name.begin(), ::tolower);
|
||||||
header_data.emplace(std::move(header_name), str.substr(data_begin, str.size() - (data_begin + 1)));
|
header_data.emplace(std::move(header_name), str.substr(data_begin, str.size() - data_begin));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -53,13 +53,13 @@ namespace fr
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HttpRequest::parse_header(int32_t header_end_pos)
|
bool HttpRequest::parse_header(int64_t header_end_pos)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//Split the header into lines
|
//Split the header into lines
|
||||||
size_t line = 0;
|
size_t line = 0;
|
||||||
std::vector<std::string> header_lines = split_string(body.substr(0, header_end_pos));
|
std::vector<std::string> header_lines = split_string(body.substr(0, (unsigned long)header_end_pos));
|
||||||
if(header_lines.empty())
|
if(header_lines.empty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|||||||
@ -46,6 +46,8 @@ namespace fr
|
|||||||
body += response_data.substr(header_end + 4, response_data.size() - header_end - 4);
|
body += response_data.substr(header_end + 4, response_data.size() - header_end - 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(body.size() > content_length)
|
||||||
|
body.resize(content_length);
|
||||||
return body.size() < content_length;
|
return body.size() < content_length;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -67,12 +69,13 @@ namespace fr
|
|||||||
response += "connection: close_socket\r\n";
|
response += "connection: close_socket\r\n";
|
||||||
if(header_data.find("content-type") == header_data.end())
|
if(header_data.find("content-type") == header_data.end())
|
||||||
response += "content-type: text/html\r\n";
|
response += "content-type: text/html\r\n";
|
||||||
|
response += "content-length: " + std::to_string(body.size()) + "\r\n";
|
||||||
|
|
||||||
//Add in space
|
//Add in space
|
||||||
response += "\r\n";
|
response += "\r\n";
|
||||||
|
|
||||||
//Add in the body
|
//Add in the body
|
||||||
response += body + "\r\n";
|
response += body;
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -53,9 +53,9 @@ namespace fr
|
|||||||
received = 0;
|
received = 0;
|
||||||
|
|
||||||
//Read RECV_CHUNK_SIZE bytes into the recv buffer
|
//Read RECV_CHUNK_SIZE bytes into the recv buffer
|
||||||
int32_t status = ::recv(socket_descriptor, (char*)data, buffer_size, 0);
|
int64_t status = ::recv(socket_descriptor, (char*)data, buffer_size, 0);
|
||||||
|
|
||||||
if(status > 0)
|
if(status >= 0)
|
||||||
{
|
{
|
||||||
received += status;
|
received += status;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user