Add status_code_t in http.h
This commit is contained in:
2
Makefile
2
Makefile
@ -1,6 +1,6 @@
|
||||
|
||||
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
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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++) {
|
||||
if (http_statuses[i].code == status_code) {
|
||||
return &http_statuses[i];
|
||||
@ -358,30 +358,25 @@ const http_status *http_get_status(unsigned short status_code) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const http_status_msg *http_get_error_msg(const http_status *status) {
|
||||
unsigned short code = status->code;
|
||||
const http_status_msg *http_get_error_msg(status_code_t status_code) {
|
||||
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 NULL;
|
||||
}
|
||||
|
||||
const char *http_get_status_color(const http_status *status) {
|
||||
unsigned short code = status->code;
|
||||
if (code >= 100 && code < 200) {
|
||||
return HTTP_1XX_STR;
|
||||
} else if ((code >= 200 && code < 300) || code == 304) {
|
||||
return HTTP_2XX_STR;
|
||||
} else if (code >= 300 && code < 400) {
|
||||
return HTTP_3XX_STR;
|
||||
} else if (code >= 400 && code < 500) {
|
||||
return HTTP_4XX_STR;
|
||||
} else if (code >= 500 && code < 600) {
|
||||
return HTTP_5XX_STR;
|
||||
const char *http_get_status_color(status_code_t status_code) {
|
||||
if (status_code == 304) return HTTP_2XX_STR;
|
||||
switch (status_code / 100) {
|
||||
case 1: return HTTP_1XX_STR;
|
||||
case 2: return HTTP_2XX_STR;
|
||||
case 3: return HTTP_3XX_STR;
|
||||
case 4: return HTTP_4XX_STR;
|
||||
case 5: return HTTP_5XX_STR;
|
||||
default: return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
const http_doc_info *http_get_status_info(const http_status *status) {
|
||||
unsigned short code = status->code;
|
||||
static http_doc_info info[] = {
|
||||
const http_doc_info *http_get_status_info(status_code_t status_code) {
|
||||
static const http_doc_info info[] = {
|
||||
{"info", HTTP_COLOR_INFO, "/.sesimos/res/icon-info.svg", http_info_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},
|
||||
{"error", HTTP_COLOR_ERROR, "/.sesimos/res/icon-error.svg", http_error_doc}
|
||||
};
|
||||
if (code >= 100 && code < 200) {
|
||||
return &info[0];
|
||||
} else if ((code >= 200 && code < 300) || code == 304) {
|
||||
return &info[1];
|
||||
} else if (code >= 300 && code < 400) {
|
||||
return &info[2];
|
||||
} else if (code >= 400 && code < 600) {
|
||||
return &info[3];
|
||||
if (status_code == 304) return &info[1];
|
||||
switch (status_code / 100) {
|
||||
case 1: return &info[0];
|
||||
case 2: return &info[1];
|
||||
case 3: return &info[2];
|
||||
case 4: // see case 5
|
||||
case 5: return &info[3];
|
||||
default: return NULL;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int http_get_compression(const http_req *req, const http_res *res) {
|
||||
|
@ -61,14 +61,16 @@
|
||||
# define SERVER_STR_HTML "sesimos web server"
|
||||
#endif
|
||||
|
||||
typedef unsigned short status_code_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned short code:10;
|
||||
status_code_t code:10;
|
||||
unsigned char type:3;
|
||||
char msg[64];
|
||||
} http_status;
|
||||
|
||||
typedef struct {
|
||||
unsigned short code:10;
|
||||
status_code_t code:10;
|
||||
const char *msg;
|
||||
} http_status_msg;
|
||||
|
||||
@ -120,7 +122,7 @@ typedef enum {
|
||||
} http_error_origin;
|
||||
|
||||
typedef struct {
|
||||
unsigned short status;
|
||||
status_code_t status;
|
||||
http_error_origin origin;
|
||||
const char* ws_key;
|
||||
} 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);
|
||||
|
||||
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_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);
|
||||
|
||||
|
@ -259,8 +259,8 @@ int respond(client_ctx_t *ctx) {
|
||||
http_add_header_field(&res->hdr, "Content-Type", "text/html; charset=UTF-8");
|
||||
|
||||
// TODO list Locations on 3xx Redirects
|
||||
const http_doc_info *http_info = http_get_status_info(res->status);
|
||||
const http_status_msg *http_msg = http_get_error_msg(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->code);
|
||||
|
||||
if (ctx->msg_content[0] == 0) {
|
||||
if (res->status->code >= 300 && res->status->code < 400) {
|
||||
@ -329,7 +329,7 @@ int respond(client_ctx_t *ctx) {
|
||||
http_send_response(client, res);
|
||||
ctx->res_ts = clock_micros();
|
||||
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 : "",
|
||||
format_duration(ctx->res_ts - ctx->req_s, buf0), CLR_STR);
|
||||
|
||||
|
Reference in New Issue
Block a user