Fixes #231: added checks to getline calls in parse functions so that they can be used outside the Server/Client classes
This commit is contained in:
parent
5686165f35
commit
8b75c14aef
2 changed files with 30 additions and 10 deletions
|
|
@ -203,6 +203,31 @@ int main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
SimpleWeb::CaseInsensitiveMultimap solution;
|
||||||
|
std::stringstream header;
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::parse(header);
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SimpleWeb::CaseInsensitiveMultimap solution = {{"Content-Type", "application/json"}};
|
||||||
|
std::stringstream header("Content-Type: application/json");
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::parse(header);
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SimpleWeb::CaseInsensitiveMultimap solution = {{"Content-Type", "application/json"}};
|
||||||
|
std::stringstream header("Content-Type: application/json\r");
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::parse(header);
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SimpleWeb::CaseInsensitiveMultimap solution = {{"Content-Type", "application/json"}};
|
||||||
|
std::stringstream header("Content-Type: application/json\r\n");
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::parse(header);
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
SimpleWeb::CaseInsensitiveMultimap solution;
|
SimpleWeb::CaseInsensitiveMultimap solution;
|
||||||
|
|
|
||||||
15
utility.hpp
15
utility.hpp
|
|
@ -138,16 +138,13 @@ namespace SimpleWeb {
|
||||||
static CaseInsensitiveMultimap parse(std::istream &stream) noexcept {
|
static CaseInsensitiveMultimap parse(std::istream &stream) noexcept {
|
||||||
CaseInsensitiveMultimap result;
|
CaseInsensitiveMultimap result;
|
||||||
std::string line;
|
std::string line;
|
||||||
getline(stream, line);
|
|
||||||
std::size_t param_end;
|
std::size_t param_end;
|
||||||
while((param_end = line.find(':')) != std::string::npos) {
|
while(getline(stream, line) && (param_end = line.find(':')) != std::string::npos) {
|
||||||
std::size_t value_start = param_end + 1;
|
std::size_t value_start = param_end + 1;
|
||||||
while(value_start + 1 < line.size() && line[value_start] == ' ')
|
while(value_start + 1 < line.size() && line[value_start] == ' ')
|
||||||
++value_start;
|
++value_start;
|
||||||
if(value_start < line.size())
|
if(value_start < line.size())
|
||||||
result.emplace(line.substr(0, param_end), line.substr(value_start, line.size() - value_start - 1));
|
result.emplace(line.substr(0, param_end), line.substr(value_start, line.size() - value_start - (line.back() == '\r' ? 1 : 0)));
|
||||||
|
|
||||||
getline(stream, line);
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -216,9 +213,8 @@ namespace SimpleWeb {
|
||||||
static bool parse(std::istream &stream, std::string &method, std::string &path, std::string &query_string, std::string &version, CaseInsensitiveMultimap &header) noexcept {
|
static bool parse(std::istream &stream, std::string &method, std::string &path, std::string &query_string, std::string &version, CaseInsensitiveMultimap &header) noexcept {
|
||||||
header.clear();
|
header.clear();
|
||||||
std::string line;
|
std::string line;
|
||||||
getline(stream, line);
|
|
||||||
std::size_t method_end;
|
std::size_t method_end;
|
||||||
if((method_end = line.find(' ')) != std::string::npos) {
|
if(getline(stream, line) && (method_end = line.find(' ')) != std::string::npos) {
|
||||||
method = line.substr(0, method_end);
|
method = line.substr(0, method_end);
|
||||||
|
|
||||||
std::size_t query_start = std::string::npos;
|
std::size_t query_start = std::string::npos;
|
||||||
|
|
@ -265,9 +261,8 @@ namespace SimpleWeb {
|
||||||
static bool parse(std::istream &stream, std::string &version, std::string &status_code, CaseInsensitiveMultimap &header) noexcept {
|
static bool parse(std::istream &stream, std::string &version, std::string &status_code, CaseInsensitiveMultimap &header) noexcept {
|
||||||
header.clear();
|
header.clear();
|
||||||
std::string line;
|
std::string line;
|
||||||
getline(stream, line);
|
std::size_t version_end;
|
||||||
std::size_t version_end = line.find(' ');
|
if(getline(stream, line) && (version_end = line.find(' ')) != std::string::npos) {
|
||||||
if(version_end != std::string::npos) {
|
|
||||||
if(5 < line.size())
|
if(5 < line.size())
|
||||||
version = line.substr(5, version_end - 5);
|
version = line.substr(5, version_end - 5);
|
||||||
else
|
else
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue