feat: add mime types
This commit is contained in:
parent
0d5c6d0f39
commit
89fb77db7f
7 changed files with 86 additions and 14 deletions
|
|
@ -1,6 +0,0 @@
|
|||
#include "http.h"
|
||||
|
||||
// Very important that this is in the same order as http_request_method
|
||||
const char *request_method_names[] = {"GET", "POST", "PUT", "PATCH", "DELETE"};
|
||||
const size_t request_method_names_len =
|
||||
sizeof(request_method_names) / sizeof(request_method_names[0]);
|
||||
|
|
@ -1,8 +1,13 @@
|
|||
#include "http.h"
|
||||
|
||||
// Very important that this is in the same order as http_request_method
|
||||
const char *request_method_names[] = {"GET", "POST", "PUT", "PATCH", "DELETE"};
|
||||
const size_t request_method_names_len =
|
||||
sizeof(request_method_names) / sizeof(request_method_names[0]);
|
||||
|
||||
// clang-format off
|
||||
|
||||
const char *http_response_type_names[5][32] = {
|
||||
const char *http_response_type_names[][32] = {
|
||||
// 1xx
|
||||
{
|
||||
"Continue", // 100
|
||||
|
|
@ -88,7 +93,36 @@ const char *http_response_type_names[5][32] = {
|
|||
|
||||
const char *http_header_names[] = {
|
||||
"Connection",
|
||||
"Location"
|
||||
"Location",
|
||||
"Content-Type"
|
||||
};
|
||||
|
||||
const char *http_mime_type_names[][2] = {
|
||||
{ "aac", "audio/aac" },
|
||||
{ "bz", "application/x-bzip" },
|
||||
{ "bz2", "application/x-bzip2" },
|
||||
{ "css", "text/css" },
|
||||
{ "csv", "text/csv" },
|
||||
{ "gz", "application/gzip" },
|
||||
{ "gif", "image/gif" },
|
||||
{ "htm", "text/html" },
|
||||
{ "html", "text/html" },
|
||||
{ "jar", "application/java-archive" },
|
||||
{ "jpeg", "image/jpeg" },
|
||||
{ "jpg", "image/jpeg" },
|
||||
{ "js", "text/javascript" },
|
||||
{ "json", "application/json" },
|
||||
{ "mp3", "audio/mpeg" },
|
||||
{ "mp4", "video/mp4" },
|
||||
{ "png", "image/png" },
|
||||
{ "pdf", "application/pdf" },
|
||||
{ "rar", "application/vnd.rar" },
|
||||
{ "sh", "application/x-sh" },
|
||||
{ "svg", "image/svg+xml" },
|
||||
{ "tar", "application/x-tar" },
|
||||
{ "txt", "text/plain" },
|
||||
{ "wav", "audio/wav" },
|
||||
{ "7z", "application/x-7z-compressed" },
|
||||
};
|
||||
|
||||
// clang-format on
|
||||
|
|
@ -40,7 +40,8 @@ void http_loop_ctx_reset(http_loop_ctx *ctx) {
|
|||
ctx->res.head = NULL;
|
||||
}
|
||||
|
||||
if (ctx->res.body_type == http_body_buf && ctx->res.body.buf != NULL) {
|
||||
if (ctx->res.body_type == http_body_buf && ctx->res.body.buf != NULL &&
|
||||
ctx->res.owns_body) {
|
||||
free(ctx->res.body.buf);
|
||||
ctx->res.body.buf = NULL;
|
||||
} else if (ctx->res.body_type == http_body_file &&
|
||||
|
|
|
|||
|
|
@ -30,3 +30,8 @@ void http_loop_res_add_header(http_loop_ctx *ctx, http_header type,
|
|||
|
||||
ctx->res.header_count++;
|
||||
}
|
||||
|
||||
void http_loop_res_set_mime_type(http_loop_ctx *ctx, http_mime_type mime_type) {
|
||||
http_loop_res_add_header(ctx, http_header_content_type,
|
||||
http_mime_type_names[mime_type][1], false);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ bool lander_get_index(event_loop_conn *conn) {
|
|||
http_loop_ctx *ctx = conn->ctx;
|
||||
|
||||
http_loop_res_set_body_buf(ctx, index_page, sizeof(index_page) - 1, false);
|
||||
http_loop_res_set_mime_type(ctx, http_mime_html);
|
||||
|
||||
conn->state = event_loop_conn_state_res;
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue