Skip to content

Commit

Permalink
Deduplicate http header formatting and move to http file
Browse files Browse the repository at this point in the history
  • Loading branch information
Woazboat committed Nov 13, 2023
1 parent ec13ff2 commit dfff59b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
2 changes: 2 additions & 0 deletions include/cgimap/http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace http {

using headers_t = std::vector<std::pair<std::string, std::string> >;

std::string format_header(int status, const headers_t &headers);

/**
* return a static string description for an HTTP status code.
*/
Expand Down
3 changes: 3 additions & 0 deletions include/cgimap/output_buffer.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#ifndef OUTPUT_BUFFER_HPP
#define OUTPUT_BUFFER_HPP

#include <string_view>

/**
* Implement this interface to provide custom output.
*/
struct output_buffer {
virtual int write(const char *buffer, int len) = 0;
virtual int write(std::string_view str) { return write(str.data(), str.size()); }
virtual int written() = 0;
virtual int close() = 0;
virtual void flush() = 0;
Expand Down
9 changes: 1 addition & 8 deletions src/fcgi_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,7 @@ void fcgi_request::set_current_time(const std::chrono::system_clock::time_point
}

void fcgi_request::write_header_info(int status, const http::headers_t &headers) {
std::ostringstream ostr;
ostr << "Status: " << status << " " << http::status_message(status) << "\r\n";
for (const auto& [name, value] : headers) {
ostr << name << ": " << value << "\r\n";
}
ostr << "\r\n";
std::string data(ostr.str());
m_buffer->write(&data[0], data.size());
m_buffer->write(http::format_header(status, headers));
}

output_buffer& fcgi_request::get_buffer_internal() {
Expand Down
10 changes: 10 additions & 0 deletions src/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ const char *status_message(int code) {
}
}

std::string format_header(int status, const headers_t &headers) {
std::string hdr{};
hdr += fmt::format("Status: {} {}\r\n", status, status_message(status));
for (const auto& [name, value] : headers) {
hdr += fmt::format("{}: {}\r\n", name, value);
}
hdr += "\r\n";
return hdr;
}

exception::exception(int c, string m)
: code_(c), message_(std::move(m)) {}

Expand Down
12 changes: 3 additions & 9 deletions test/test_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,9 @@ void test_request::write_header_info(int status, const http::headers_t &headers)
assert(m_output.tellp() == 0);
m_status = status;

std::stringstream hdr;
hdr << "Status: " << status << " " << http::status_message(status) << "\r\n";
for (const auto& [name, value] : headers) {
hdr << name << ": " << value << "\r\n";
}
hdr << "\r\n";

m_output << hdr.str();
m_header << hdr.str();
auto hdr = http::format_header(status, headers);
m_output << hdr;
m_header << hdr;
}

int test_request::response_status() const {
Expand Down

0 comments on commit dfff59b

Please sign in to comment.