diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/HttpResponse.cpp | 26 | ||||
-rw-r--r-- | src/HttpResponse.h | 1 | ||||
-rw-r--r-- | src/main.cpp | 3 |
3 files changed, 28 insertions, 2 deletions
diff --git a/src/HttpResponse.cpp b/src/HttpResponse.cpp index 2d69583..c07178f 100644 --- a/src/HttpResponse.cpp +++ b/src/HttpResponse.cpp @@ -2,6 +2,32 @@ #include <sstream> +void HttpResponse::add_headers_and_content(const std::string& input) +{ + bool is_parsing_headers = true; + + size_t pos = 0; + std::string s = input; + std::string line; + while ((pos = s.find("\n")) != std::string::npos) { + line = s.substr(0, pos + 1); + + if (is_parsing_headers) { + size_t delim_pos = 0; + if ((delim_pos = line.find(":")) != std::string::npos) { + std::string header_key = s.substr(0, delim_pos); + std::string header_value = s.substr(delim_pos + 2, s.find("\n") - delim_pos - 2); + m_headers[header_key] = header_value; + } else { + is_parsing_headers = false; + } + } else { + m_content += line; + } + s.erase(0, pos + 1); + } +} + static std::string status_code_string(HttpStatusCode status_code) { switch (status_code) { diff --git a/src/HttpResponse.h b/src/HttpResponse.h index b0c0cd9..61b5ea4 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -19,6 +19,7 @@ public: void set_status_code(HttpStatusCode status_code) { m_status_code = status_code; } void set_content(const std::string& content) { m_content = content; } + void add_headers_and_content(const std::string&); std::string to_string() const; private: diff --git a/src/main.cpp b/src/main.cpp index cec6d3e..2b93b07 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -74,8 +74,7 @@ static HttpResponse serve_from_cgi(const std::string& executable_path, HttpReque // TODO: We should be able to construct a repsonse from an entire string // instead of always needing to individually set headers and content. response.set_status_code(HttpStatusCode::OK); - response.add_header("Content-Type", "text/plain"); - response.set_content(sstream.str()); + response.add_headers_and_content(sstream.str()); return response; } |