feat: add mime types
							parent
							
								
									0d5c6d0f39
								
							
						
					
					
						commit
						89fb77db7f
					
				|  | @ -11,9 +11,19 @@ | |||
| #define HTTP_MAX_ALLOWED_HEADERS 16 | ||||
| #define HTTP_MAX_REGEX_GROUPS 4 | ||||
| 
 | ||||
| extern const char *http_response_type_names[5][32]; | ||||
| // Array mapping the http_response_type enum to strings
 | ||||
| extern const char *http_response_type_names[][32]; | ||||
| 
 | ||||
| // Array mapping the http_header enum to strings
 | ||||
| extern const char *http_header_names[]; | ||||
| 
 | ||||
| // Array mapping the http_mime_type enum to strings
 | ||||
| extern const char *http_mime_type_names[][2]; | ||||
| 
 | ||||
| // Array mapping the http_request_method enum to strings
 | ||||
| extern const char *request_method_names[]; | ||||
| extern const size_t request_method_names_len; | ||||
| 
 | ||||
| typedef enum http_request_method { | ||||
|   http_get = 0, | ||||
|   http_post = 1, | ||||
|  | @ -22,9 +32,6 @@ typedef enum http_request_method { | |||
|   http_delete = 4 | ||||
| } http_request_method; | ||||
| 
 | ||||
| extern const char *request_method_names[]; | ||||
| extern const size_t request_method_names_len; | ||||
| 
 | ||||
| typedef enum http_body_type { | ||||
|   http_body_buf = 0, | ||||
|   http_body_file = 1 | ||||
|  | @ -132,7 +139,8 @@ typedef enum http_response_type { | |||
| 
 | ||||
| typedef enum http_header { | ||||
|   http_header_connection = 0, | ||||
|   http_header_location = 1 | ||||
|   http_header_location, | ||||
|   http_header_content_type | ||||
| } http_header; | ||||
| 
 | ||||
| typedef struct http_response_header { | ||||
|  | @ -159,4 +167,31 @@ typedef struct http_response { | |||
|   size_t header_count; | ||||
| } http_response; | ||||
| 
 | ||||
| typedef enum http_mime_type { | ||||
|   http_mime_aac = 0, | ||||
|   http_mime_bz, | ||||
|   http_mime_bz2, | ||||
|   http_mime_css, | ||||
|   http_mime_csv, | ||||
|   http_mime_gz, | ||||
|   http_mime_gif, | ||||
|   http_mime_htm, | ||||
|   http_mime_html, | ||||
|   http_mime_jar, | ||||
|   http_mime_jpeg, | ||||
|   http_mime_js, | ||||
|   http_mime_json, | ||||
|   http_mime_mp3, | ||||
|   http_mime_mp4, | ||||
|   http_mime_png, | ||||
|   http_mime_pdf, | ||||
|   http_mime_rar, | ||||
|   http_mime_sh, | ||||
|   http_mime_svg, | ||||
|   http_mime_tar, | ||||
|   http_mime_txt, | ||||
|   http_mime_wav, | ||||
|   http_mime_7z | ||||
| } http_mime_type; | ||||
| 
 | ||||
| #endif | ||||
|  |  | |||
|  | @ -106,6 +106,8 @@ void http_loop_res_set_body_file(http_loop_ctx *ctx, const char *filename); | |||
| void http_loop_res_add_header(http_loop_ctx *ctx, http_header type, | ||||
|                               const char *value, bool owned); | ||||
| 
 | ||||
| void http_loop_res_set_mime_type(http_loop_ctx *ctx, http_mime_type mime_type); | ||||
| 
 | ||||
| /*
 | ||||
|  * Request step that consumes the request body and stores it in a buffer | ||||
|  */ | ||||
|  |  | |||
|  | @ -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…
	
		Reference in New Issue