Add status_code_t in http.h
This commit is contained in:
2
Makefile
2
Makefile
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
CC=gcc
|
CC=gcc
|
||||||
CFLAGS=-std=gnu11 -Wno-unused-but-set-variable -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809L
|
CFLAGS=-std=gnu11 -Wno-unused-but-set-variable -D_DEFAULT_SOURCE -D_GNU_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809L
|
||||||
LDFLAGS=-pthread -lssl -lcrypto -lmagic -lz -lmaxminddb -lbrotlienc
|
LDFLAGS=-pthread -lssl -lcrypto -lmagic -lz -lmaxminddb -lbrotlienc
|
||||||
|
|
||||||
DEBIAN_OPTS=-D CACHE_MAGIC_FILE="\"/usr/share/file/magic.mgc\"" -D PHP_FPM_SOCKET="\"/var/run/php/php7.4-fpm.sock\""
|
DEBIAN_OPTS=-D CACHE_MAGIC_FILE="\"/usr/share/file/magic.mgc\"" -D PHP_FPM_SOCKET="\"/var/run/php/php7.4-fpm.sock\""
|
||||||
|
@ -349,7 +349,7 @@ int http_send_request(sock *server, http_req *req) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const http_status *http_get_status(unsigned short status_code) {
|
const http_status *http_get_status(status_code_t status_code) {
|
||||||
for (int i = 0; i < http_statuses_size; i++) {
|
for (int i = 0; i < http_statuses_size; i++) {
|
||||||
if (http_statuses[i].code == status_code) {
|
if (http_statuses[i].code == status_code) {
|
||||||
return &http_statuses[i];
|
return &http_statuses[i];
|
||||||
@ -358,30 +358,25 @@ const http_status *http_get_status(unsigned short status_code) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
const http_status_msg *http_get_error_msg(const http_status *status) {
|
const http_status_msg *http_get_error_msg(status_code_t status_code) {
|
||||||
unsigned short code = status->code;
|
|
||||||
for (int i = 0; i < http_status_messages_size; i++) {
|
for (int i = 0; i < http_status_messages_size; i++) {
|
||||||
if (http_status_messages[i].code == code) {
|
if (http_status_messages[i].code == status_code) {
|
||||||
return &http_status_messages[i];
|
return &http_status_messages[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *http_get_status_color(const http_status *status) {
|
const char *http_get_status_color(status_code_t status_code) {
|
||||||
unsigned short code = status->code;
|
if (status_code == 304) return HTTP_2XX_STR;
|
||||||
if (code >= 100 && code < 200) {
|
switch (status_code / 100) {
|
||||||
return HTTP_1XX_STR;
|
case 1: return HTTP_1XX_STR;
|
||||||
} else if ((code >= 200 && code < 300) || code == 304) {
|
case 2: return HTTP_2XX_STR;
|
||||||
return HTTP_2XX_STR;
|
case 3: return HTTP_3XX_STR;
|
||||||
} else if (code >= 300 && code < 400) {
|
case 4: return HTTP_4XX_STR;
|
||||||
return HTTP_3XX_STR;
|
case 5: return HTTP_5XX_STR;
|
||||||
} else if (code >= 400 && code < 500) {
|
default: return "";
|
||||||
return HTTP_4XX_STR;
|
|
||||||
} else if (code >= 500 && code < 600) {
|
|
||||||
return HTTP_5XX_STR;
|
|
||||||
}
|
}
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *http_format_date(time_t time, char *buf, size_t size) {
|
char *http_format_date(time_t time, char *buf, size_t size) {
|
||||||
@ -396,24 +391,22 @@ char *http_get_date(char *buf, size_t size) {
|
|||||||
return http_format_date(raw_time, buf, size);
|
return http_format_date(raw_time, buf, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
const http_doc_info *http_get_status_info(const http_status *status) {
|
const http_doc_info *http_get_status_info(status_code_t status_code) {
|
||||||
unsigned short code = status->code;
|
static const http_doc_info info[] = {
|
||||||
static http_doc_info info[] = {
|
|
||||||
{"info", HTTP_COLOR_INFO, "/.sesimos/res/icon-info.svg", http_info_doc},
|
{"info", HTTP_COLOR_INFO, "/.sesimos/res/icon-info.svg", http_info_doc},
|
||||||
{"success", HTTP_COLOR_SUCCESS, "/.sesimos/res/icon-success.svg", http_success_doc},
|
{"success", HTTP_COLOR_SUCCESS, "/.sesimos/res/icon-success.svg", http_success_doc},
|
||||||
{"warning", HTTP_COLOR_WARNING, "/.sesimos/res/icon-warning.svg", http_warning_doc},
|
{"warning", HTTP_COLOR_WARNING, "/.sesimos/res/icon-warning.svg", http_warning_doc},
|
||||||
{"error", HTTP_COLOR_ERROR, "/.sesimos/res/icon-error.svg", http_error_doc}
|
{"error", HTTP_COLOR_ERROR, "/.sesimos/res/icon-error.svg", http_error_doc}
|
||||||
};
|
};
|
||||||
if (code >= 100 && code < 200) {
|
if (status_code == 304) return &info[1];
|
||||||
return &info[0];
|
switch (status_code / 100) {
|
||||||
} else if ((code >= 200 && code < 300) || code == 304) {
|
case 1: return &info[0];
|
||||||
return &info[1];
|
case 2: return &info[1];
|
||||||
} else if (code >= 300 && code < 400) {
|
case 3: return &info[2];
|
||||||
return &info[2];
|
case 4: // see case 5
|
||||||
} else if (code >= 400 && code < 600) {
|
case 5: return &info[3];
|
||||||
return &info[3];
|
default: return NULL;
|
||||||
}
|
}
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int http_get_compression(const http_req *req, const http_res *res) {
|
int http_get_compression(const http_req *req, const http_res *res) {
|
||||||
|
@ -61,14 +61,16 @@
|
|||||||
# define SERVER_STR_HTML "sesimos web server"
|
# define SERVER_STR_HTML "sesimos web server"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
typedef unsigned short status_code_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned short code:10;
|
status_code_t code:10;
|
||||||
unsigned char type:3;
|
unsigned char type:3;
|
||||||
char msg[64];
|
char msg[64];
|
||||||
} http_status;
|
} http_status;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned short code:10;
|
status_code_t code:10;
|
||||||
const char *msg;
|
const char *msg;
|
||||||
} http_status_msg;
|
} http_status_msg;
|
||||||
|
|
||||||
@ -120,7 +122,7 @@ typedef enum {
|
|||||||
} http_error_origin;
|
} http_error_origin;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned short status;
|
status_code_t status;
|
||||||
http_error_origin origin;
|
http_error_origin origin;
|
||||||
const char* ws_key;
|
const char* ws_key;
|
||||||
} http_status_ctx;
|
} http_status_ctx;
|
||||||
@ -170,17 +172,17 @@ int http_send_response(sock *client, http_res *res);
|
|||||||
|
|
||||||
int http_send_request(sock *server, http_req *req);
|
int http_send_request(sock *server, http_req *req);
|
||||||
|
|
||||||
const http_status *http_get_status(unsigned short status_code);
|
const http_status *http_get_status(status_code_t status_code);
|
||||||
|
|
||||||
const http_status_msg *http_get_error_msg(const http_status *status);
|
const http_status_msg *http_get_error_msg(status_code_t status_code);
|
||||||
|
|
||||||
const char *http_get_status_color(const http_status *status);
|
const char *http_get_status_color(status_code_t status_code);
|
||||||
|
|
||||||
char *http_format_date(time_t time, char *buf, size_t size);
|
char *http_format_date(time_t time, char *buf, size_t size);
|
||||||
|
|
||||||
char *http_get_date(char *buf, size_t size);
|
char *http_get_date(char *buf, size_t size);
|
||||||
|
|
||||||
const http_doc_info *http_get_status_info(const http_status *status);
|
const http_doc_info *http_get_status_info(status_code_t status_code);
|
||||||
|
|
||||||
int http_get_compression(const http_req *req, const http_res *res);
|
int http_get_compression(const http_req *req, const http_res *res);
|
||||||
|
|
||||||
|
@ -259,8 +259,8 @@ int respond(client_ctx_t *ctx) {
|
|||||||
http_add_header_field(&res->hdr, "Content-Type", "text/html; charset=UTF-8");
|
http_add_header_field(&res->hdr, "Content-Type", "text/html; charset=UTF-8");
|
||||||
|
|
||||||
// TODO list Locations on 3xx Redirects
|
// TODO list Locations on 3xx Redirects
|
||||||
const http_doc_info *http_info = http_get_status_info(res->status);
|
const http_doc_info *http_info = http_get_status_info(res->status->code);
|
||||||
const http_status_msg *http_msg = http_get_error_msg(res->status);
|
const http_status_msg *http_msg = http_get_error_msg(res->status->code);
|
||||||
|
|
||||||
if (ctx->msg_content[0] == 0) {
|
if (ctx->msg_content[0] == 0) {
|
||||||
if (res->status->code >= 300 && res->status->code < 400) {
|
if (res->status->code >= 300 && res->status->code < 400) {
|
||||||
@ -329,7 +329,7 @@ int respond(client_ctx_t *ctx) {
|
|||||||
http_send_response(client, res);
|
http_send_response(client, res);
|
||||||
ctx->res_ts = clock_micros();
|
ctx->res_ts = clock_micros();
|
||||||
const char *location = http_get_header_field(&res->hdr, "Location");
|
const char *location = http_get_header_field(&res->hdr, "Location");
|
||||||
info("%s%s%03i %s%s%s (%s)%s", http_get_status_color(res->status), ctx->use_proxy ? "-> " : "", res->status->code,
|
info("%s%s%03i %s%s%s (%s)%s", http_get_status_color(res->status->code), ctx->use_proxy ? "-> " : "", res->status->code,
|
||||||
res->status->msg, location != NULL ? " -> " : "", location != NULL ? location : "",
|
res->status->msg, location != NULL ? " -> " : "", location != NULL ? location : "",
|
||||||
format_duration(ctx->res_ts - ctx->req_s, buf0), CLR_STR);
|
format_duration(ctx->res_ts - ctx->req_s, buf0), CLR_STR);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user