Skip to content

Commit

Permalink
added reply::custom_reply to CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbald committed Apr 19, 2024
1 parent d5e654a commit 4ff7575
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 3 additions & 1 deletion CRUD/service/mime_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ struct mapping
{ "jpg", "image/jpeg" },
{ "png", "image/png" },
{ "json", "application/json"},
{ "css", "text/css"}
{ "css", "text/css"},
{ "txt", "text/plain"}
};


Expand Down Expand Up @@ -64,6 +65,7 @@ constexpr const char* JPG = mime_types::extension_to_type("jpg");
constexpr const char* PNG = mime_types::extension_to_type("png");
constexpr const char* JSON = mime_types::extension_to_type("json");
constexpr const char* CSS = mime_types::extension_to_type("css");
constexpr const char* TXT = mime_types::extension_to_type("txt");

/// Convert a file extension into a MIME type.
std::string extension_to_type(const std::string& extension);
Expand Down
21 changes: 16 additions & 5 deletions CRUD/service/reply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,22 @@ reply reply::stock_reply(reply::status_type status, const char* mime)
rep.status = status;
if ( status != reply::no_content) {
rep.content = stock_replies::to_string(status);
rep.headers.resize(2);
rep.headers[0].name = "Content-Length";
rep.headers[0].value = std::to_string(rep.content.size());
rep.headers[1].name = "Content-Type";
rep.headers[1].value = mime;
rep.headers.reserve(2);
rep.headers.emplace_back("Content-Length", std::to_string(rep.content.size()));
rep.headers.emplace_back("Content-Type", mime);
}
return rep;
}

reply reply::custom_reply(reply::status_type status, const char* mime, std::string_view payload)
{
reply rep;
rep.status = status;
if ( status != reply::no_content) {
rep.content = payload;
rep.headers.reserve(2);
rep.headers.emplace_back("Content-Length", std::to_string(rep.content.size()));
rep.headers.emplace_back("Content-Type", mime);
}
return rep;
}
Expand Down
3 changes: 3 additions & 0 deletions CRUD/service/reply.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ struct reply

/// Get a stock reply.
static reply stock_reply(status_type status, const char* mime = mime_types::HTML);

/// Build a custom reply.
static reply custom_reply(status_type status, const char* mime, std::string_view payload);

/// flush function to be used by reply & operator<<(reply &r , const reply::flush &)
template<typename FlushT>
Expand Down

0 comments on commit 4ff7575

Please sign in to comment.