Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve response content type #504

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int main() {
coro_http_server server(std::thread::hardware_concurrency(), 8090);
server.set_http_handler<GET>(
"/plaintext", [](coro_http_request& req, coro_http_response& resp) {
resp.need_date_head(false);
resp.set_content_type<resp_content_type::txt>();
resp.set_status_and_content(status_type::ok, "Hello, world!");
});
server.sync_start();
Expand Down
13 changes: 13 additions & 0 deletions include/cinatra/coro_http_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class coro_http_response {
void set_delay(bool r) { delay_ = r; }
bool get_delay() const { return delay_; }
void set_format_type(format_type type) { fmt_type_ = type; }
template <size_t N>
void set_content_type() {
content_type_ = get_content_type<N>();
}

status_type status() { return status_; }
std::string_view content() { return content_; }
Expand Down Expand Up @@ -134,6 +138,10 @@ class coro_http_response {
: resp_str.append(CONN_CLOSE_SV);
}

if (content_type_.empty()) {
resp_str.append(content_type_);
}

for (auto &[k, v] : resp_headers_) {
resp_str.append(k);
resp_str.append(COLON_SV);
Expand Down Expand Up @@ -202,6 +210,10 @@ class coro_http_response {
: buffers.emplace_back(asio::buffer(CONN_CLOSE_SV));
}

if (content_type_.empty()) {
buffers.emplace_back(asio::buffer(content_type_));
}

for (auto &[k, v] : resp_headers_) {
buffers.emplace_back(asio::buffer(k));
buffers.emplace_back(asio::buffer(COLON_SV));
Expand Down Expand Up @@ -250,5 +262,6 @@ class coro_http_response {
bool need_shrink_every_time_ = false;
bool need_date_ = true;
std::unordered_map<std::string, cookie> cookies_;
std::string_view content_type_;
};
} // namespace cinatra
107 changes: 106 additions & 1 deletion include/cinatra/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const static inline std::string BOUNDARY = "--CinatraBoundary2B8FAF4A80EDB307";
const static inline std::string MULTIPART_END =
CRCF + "--" + BOUNDARY + "--" + CRCF;
constexpr std::string_view LAST_CHUNK = "0\r\n";
constexpr std::string_view CINATRA_HOST_SV = "Host: cinatra\r\n";
constexpr std::string_view CINATRA_HOST_SV = "Server: cinatra\r\n";
constexpr std::string_view TRANSFER_ENCODING_SV =
"Transfer-Encoding: chunked\r\n";
constexpr std::string_view CONTENT_LENGTH_SV = "Content-Length: ";
Expand All @@ -117,6 +117,111 @@ struct part_head_t {
std::string filename;
};

enum resp_content_type {
css,
csv,
htm,
html,
js,
mjs,
txt,
vtt,
apng,
avif,
bmp,
gif,
png,
svg,
webp,
ico,
tif,
tiff,
jpg,
jpeg,
mp4,
mpeg,
webm,
mp3,
mpga,
weba,
wav,
otf,
ttf,
woff,
woff2,
x7z,
atom,
pdf,
json,
rss,
tar,
xht,
xhtml,
xslt,
xml,
gz,
zip,
wasm,
unknown
};

constexpr std::array<std::string_view, 45> content_type_arr{
"Content-Type: text/css\r\n",
"Content-Type: text/csv\r\n",
"Content-Type: text/html\r\n",
"Content-Type: text/html\r\n",
"Content-Type: text/javascript\r\n",
"Content-Type: text/javascript\r\n",
"Content-Type: text/plain\r\n",
"Content-Type: text/vtt\r\n",
"Content-Type: image/apng\r\n",
"Content-Type: image/avif\r\n",
"Content-Type: image/bmp\r\n",
"Content-Type: image/gif\r\n",
"Content-Type: image/png\r\n",
"Content-Type: image/svg+xml\r\n",
"Content-Type: image/webp\r\n",
"Content-Type: image/x-icon\r\n",
"Content-Type: image/tiff\r\n",
"Content-Type: image/tiff\r\n",
"Content-Type: image/jpeg\r\n",
"Content-Type: image/jpeg\r\n",
"Content-Type: video/mp4\r\n",
"Content-Type: video/mpeg\r\n",
"Content-Type: video/webm\r\n",
"Content-Type: audio/mp3\r\n",
"Content-Type: audio/mpeg\r\n",
"Content-Type: audio/webm\r\n",
"Content-Type: audio/wave\r\n",
"Content-Type: font/otf\r\n",
"Content-Type: font/ttf\r\n",
"Content-Type: font/woff\r\n",
"Content-Type: font/woff2\r\n",
"Content-Type: application/x-7z-compressed\r\n",
"Content-Type: application/atom+xml\r\n",
"Content-Type: application/pdf\r\n",
"Content-Type: application/json\r\n",
"Content-Type: application/rss+xml\r\n",
"Content-Type: application/x-tar\r\n",
"Content-Type: application/xhtml+xml\r\n",
"Content-Type: application/xhtml+xml\r\n",
"Content-Type: application/xslt+xml\r\n",
"Content-Type: application/xml\r\n",
"Content-Type: application/gzip\r\n",
"Content-Type: application/zip\r\n",
"Content-Type: application/wasm\r\n",
"Content-Type: unknown\r\n"};

template <size_t N>
inline constexpr std::string_view get_content_type() {
if constexpr (N > 43) {
return content_type_arr[44];
}
else {
return content_type_arr[N];
}
}

inline std::unordered_map<std::string, std::string> g_content_type_map = {
{".css", "text/css"},
{".csv", "text/csv"},
Expand Down
Loading