From 8b75c14aeff6da2b623e7e132ec97e3a46ca6367 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 4 Jul 2018 20:47:47 +0200 Subject: [PATCH] Fixes #231: added checks to getline calls in parse functions so that they can be used outside the Server/Client classes --- tests/parse_test.cpp | 25 +++++++++++++++++++++++++ utility.hpp | 15 +++++---------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/tests/parse_test.cpp b/tests/parse_test.cpp index 3bbd9b1..e324376 100644 --- a/tests/parse_test.cpp +++ b/tests/parse_test.cpp @@ -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; diff --git a/utility.hpp b/utility.hpp index e46c0aa..2c0d146 100644 --- a/utility.hpp +++ b/utility.hpp @@ -138,16 +138,13 @@ namespace SimpleWeb { static CaseInsensitiveMultimap parse(std::istream &stream) noexcept { CaseInsensitiveMultimap result; std::string line; - getline(stream, line); 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; while(value_start + 1 < line.size() && line[value_start] == ' ') ++value_start; if(value_start < line.size()) - result.emplace(line.substr(0, param_end), line.substr(value_start, line.size() - value_start - 1)); - - getline(stream, line); + result.emplace(line.substr(0, param_end), line.substr(value_start, line.size() - value_start - (line.back() == '\r' ? 1 : 0))); } 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 { header.clear(); std::string line; - getline(stream, line); 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); 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 { header.clear(); std::string line; - getline(stream, line); - std::size_t version_end = line.find(' '); - if(version_end != std::string::npos) { + std::size_t version_end; + if(getline(stream, line) && (version_end = line.find(' ')) != std::string::npos) { if(5 < line.size()) version = line.substr(5, version_end - 5); else