Add more utils

This commit is contained in:
2023-01-11 11:06:36 +01:00
parent 7699583c5f
commit 764b754a6f
15 changed files with 133 additions and 100 deletions

View File

@ -46,9 +46,9 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
char buf[1024];
int mode, ret;
if (strcmp(uri->filename + strlen(uri->filename) - 4, ".ncr") == 0) {
if (strends(uri->filename, ".ncr")) {
mode = FASTCGI_SESIMOS;
} else if (strcmp(uri->filename + strlen(uri->filename) - 4, ".php") == 0) {
} else if (strends(uri->filename, ".php")) {
mode = FASTCGI_PHP;
} else {
res->status = http_get_status(500);
@ -73,7 +73,7 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
if (client_content_length != NULL) {
unsigned long client_content_len = strtoul(client_content_length, NULL, 10);
ret = fastcgi_receive(fcgi_cnx, client, client_content_len);
} else if (client_transfer_encoding != NULL && strstr(client_transfer_encoding, "chunked") != NULL) {
} else if (strcontains(client_transfer_encoding, "chunked")) {
ret = fastcgi_receive_chunked(fcgi_cnx, client);
} else {
ret = 0;
@ -118,7 +118,7 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
const char *content_encoding = http_get_header_field(&res->hdr, "Content-Encoding");
if (content_encoding == NULL &&
content_type != NULL &&
strncmp(content_type, "text/html", 9) == 0 &&
strstarts(content_type, "text/html") &&
ctx->content_length != -1 &&
ctx->content_length <= sizeof(ctx->msg_content) - 1)
{
@ -157,7 +157,7 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
static int fastcgi_handler_2(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
const char *transfer_encoding = http_get_header_field(&ctx->res.hdr, "Transfer-Encoding");
int chunked = (transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL);
int chunked = strcontains(transfer_encoding, "chunked");
int flags = (chunked ? FASTCGI_CHUNKED : 0) | (ctx->use_fastcgi & (FASTCGI_COMPRESS | FASTCGI_COMPRESS_HOLD));
int ret = fastcgi_send(fcgi_cnx, &ctx->socket, flags);

View File

@ -45,7 +45,7 @@ static int local_handler(client_ctx_t *ctx) {
char buf1[1024], buf2[1024];
int accept_if_modified_since = 0;
if (strcmp(req->method, "TRACE") == 0) {
if (streq(req->method, "TRACE")) {
res->status = http_get_status(200);
http_add_header_field(&res->hdr, "Content-Type", "message/http");
@ -60,11 +60,11 @@ static int local_handler(client_ctx_t *ctx) {
return 0;
}
if (strncmp(uri->req_path, "/.well-known/", 13) == 0) {
if (strstarts(uri->req_path, "/.well-known/")) {
http_add_header_field(&res->hdr, "Access-Control-Allow-Origin", "*");
}
if (strncmp(uri->req_path, "/.well-known/", 13) != 0 && strstr(uri->path, "/.") != NULL) {
if (!strstarts(uri->req_path, "/.well-known/") && strcontains(uri->path, "/.")) {
res->status = http_get_status(403);
sprintf(err_msg, "Parts of this URI are hidden.");
return 0;
@ -88,7 +88,7 @@ static int local_handler(client_ctx_t *ctx) {
if (uri->is_static) {
res->status = http_get_status(200);
http_add_header_field(&res->hdr, "Accept-Ranges", "bytes");
if (strcmp(req->method, "GET") != 0 && strcmp(req->method, "HEAD") != 0) {
if (!streq(req->method, "GET") && !streq(req->method, "HEAD")) {
res->status = http_get_status(405);
return 0;
}
@ -101,7 +101,7 @@ static int local_handler(client_ctx_t *ctx) {
cache_init_uri(ctx->conf->cache, uri);
const char *last_modified = http_format_date(uri->meta->stat.st_mtime, buf1, sizeof(buf1));
const char *last_modified = http_format_date(uri->meta->mtime, buf1, sizeof(buf1));
http_add_header_field(&res->hdr, "Last-Modified", last_modified);
sprintf(buf2, "%s; charset=%s", uri->meta->type, uri->meta->charset);
http_add_header_field(&res->hdr, "Content-Type", buf2);
@ -110,7 +110,7 @@ static int local_handler(client_ctx_t *ctx) {
const char *accept_encoding = http_get_header_field(&req->hdr, "Accept-Encoding");
int enc = 0;
if (accept_encoding != NULL) {
if (uri->meta->filename_comp_br[0] != 0 && strstr(accept_encoding, "br") != NULL) {
if (uri->meta->filename_comp_br[0] != 0 && strcontains(accept_encoding, "br")) {
ctx->file = fopen(uri->meta->filename_comp_br, "rb");
if (ctx->file == NULL) {
cache_mark_dirty(ctx->conf->cache, uri->filename);
@ -119,7 +119,7 @@ static int local_handler(client_ctx_t *ctx) {
http_add_header_field(&res->hdr, "Content-Encoding", "br");
enc = COMPRESS_BR;
}
} else if (uri->meta->filename_comp_gz[0] != 0 && strstr(accept_encoding, "gzip") != NULL) {
} else if (uri->meta->filename_comp_gz[0] != 0 && strcontains(accept_encoding, "gzip")) {
ctx->file = fopen(uri->meta->filename_comp_gz, "rb");
if (ctx->file == NULL) {
cache_mark_dirty(ctx->conf->cache, uri->filename);
@ -143,7 +143,7 @@ static int local_handler(client_ctx_t *ctx) {
}
}
if (strncmp(uri->meta->type, "text/", 5) == 0) {
if (strstarts(uri->meta->type, "text/")) {
http_add_header_field(&res->hdr, "Cache-Control", "public, max-age=3600");
} else {
http_add_header_field(&res->hdr, "Cache-Control", "public, max-age=86400");
@ -151,8 +151,8 @@ static int local_handler(client_ctx_t *ctx) {
const char *if_modified_since = http_get_header_field(&req->hdr, "If-Modified-Since");
const char *if_none_match = http_get_header_field(&req->hdr, "If-None-Match");
if ((if_none_match != NULL && strstr(if_none_match, uri->meta->etag) == NULL) ||
(accept_if_modified_since && if_modified_since != NULL && strcmp(if_modified_since, last_modified) == 0))
if ((if_none_match != NULL && !strcontains(if_none_match, uri->meta->etag)) ||
(accept_if_modified_since && streq(if_modified_since, last_modified)))
{
res->status = http_get_status(304);
return 0;
@ -160,7 +160,7 @@ static int local_handler(client_ctx_t *ctx) {
const char *range = http_get_header_field(&req->hdr, "Range");
if (range != NULL) {
if (strlen(range) <= 6 || strncmp(range, "bytes=", 6) != 0) {
if (!strstarts(range, "bytes=")) {
res->status = http_get_status(416);
http_remove_header_field(&res->hdr, "Content-Type", HTTP_REMOVE_ALL);
http_remove_header_field(&res->hdr, "Last-Modified", HTTP_REMOVE_ALL);

View File

@ -63,12 +63,12 @@ static int proxy_handler_1(client_ctx_t *ctx) {
const char *connection = http_get_header_field(&res->hdr, "Connection");
const char *upgrade = http_get_header_field(&res->hdr, "Upgrade");
if (connection != NULL && upgrade != NULL &&
(strstr(connection, "upgrade") != NULL || strstr(connection, "Upgrade") != NULL) &&
strcmp(upgrade, "websocket") == 0)
(strcontains(connection, "upgrade") || strcontains(connection, "Upgrade")) &&
streq(upgrade, "websocket"))
{
const char *ws_accept = http_get_header_field(&res->hdr, "Sec-WebSocket-Accept");
if (ws_calc_accept_key(status->ws_key, buf) == 0) {
ctx->use_proxy = (strcmp(buf, ws_accept) == 0) ? 2 : 1;
ctx->use_proxy = streq(buf, ws_accept) ? 2 : 1;
}
} else {
status->status = 101;
@ -84,10 +84,10 @@ static int proxy_handler_1(client_ctx_t *ctx) {
const char *content_encoding = http_get_header_field(&res->hdr, "Content-Encoding");
if (content_encoding == NULL && (
content_length_f == NULL ||
(content_length_f != NULL && strcmp(content_length_f, "0") == 0) ||
(content_type != NULL && content_length_f != NULL && strncmp(content_type, "text/html", 9) == 0)))
streq(content_length_f, "0") ||
(content_length_f != NULL && strstarts(content_type, "text/html"))))
{
long content_len = (strcmp(ctx->req.method, "HEAD") != 0 && content_length_f != NULL) ? strtol(content_length_f, NULL, 10) : 0;
long content_len = (!streq(ctx->req.method, "HEAD") && content_length_f != NULL) ? strtol(content_length_f, NULL, 10) : 0;
if (content_len <= sizeof(ctx->msg_content) - 1) {
if (status->status != 101) {
status->status = res->status->code;
@ -103,7 +103,7 @@ static int proxy_handler_1(client_ctx_t *ctx) {
}
}
if (strcmp(ctx->req.method, "HEAD") == 0) {
if (streq(ctx->req.method, "HEAD")) {
return 1;
}
@ -119,7 +119,7 @@ static int proxy_handler_1(client_ctx_t *ctx) {
}
char *transfer_encoding = http_get_header_field(&res->hdr, "Transfer-Encoding");
int chunked = transfer_encoding != NULL && strcmp(transfer_encoding, "chunked") == 0;
int chunked = transfer_encoding != NULL && streq(transfer_encoding, "chunked");
http_remove_header_field(&res->hdr, "Transfer-Encoding", HTTP_REMOVE_ALL);
ret = sprintf(buf, "%s%s%s",
(use_proxy & PROXY_COMPRESS_BR) ? "br" :
@ -136,7 +136,7 @@ static int proxy_handler_1(client_ctx_t *ctx) {
static int proxy_handler_2(client_ctx_t *ctx) {
const char *transfer_encoding = http_get_header_field(&ctx->res.hdr, "Transfer-Encoding");
int chunked = transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL;
int chunked = strcontains(transfer_encoding, "chunked");
const char *content_len = http_get_header_field(&ctx->res.hdr, "Content-Length");
unsigned long len_to_send = 0;

View File

@ -108,7 +108,7 @@ static int request_handler(client_ctx_t *ctx) {
}
const char *hdr_connection = http_get_header_field(&req->hdr, "Connection");
ctx->c_keep_alive = (hdr_connection != NULL && (strstr(hdr_connection, "keep-alive") != NULL || strstr(hdr_connection, "Keep-Alive") != NULL));
ctx->c_keep_alive = (strcontains(hdr_connection, "keep-alive") || strcontains(hdr_connection, "Keep-Alive"));
const char *host_ptr = http_get_header_field(&req->hdr, "Host");
if (host_ptr != NULL && strlen(host_ptr) > 255) {
ctx->req_host[0] = 0;
@ -131,8 +131,8 @@ static int request_handler(client_ctx_t *ctx) {
logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix);
info(BLD_STR "%s %s", req->method, req->uri);
if (strncmp(req->uri, "/.sesimos/res/", 14) == 0) {
if (strcmp(req->method, "GET") != 0 && strcmp(req->method, "HEAD") != 0) {
if (strstarts(req->uri, "/.sesimos/res/")) {
if (!streq(req->method, "GET") && !streq(req->method, "HEAD")) {
res->status = http_get_status(405);
http_add_header_field(&res->hdr, "Allow", "GET, HEAD");
return 0;
@ -150,7 +150,7 @@ static int request_handler(client_ctx_t *ctx) {
res->status = http_get_status(404);
for (int i = 0; i < sizeof(resources) / sizeof(res_t); i++) {
const res_t *r = &resources[i];
if (strcmp(req->uri + 14, r->name) == 0) {
if (streq(req->uri + 14, r->name)) {
res->status = http_get_status(203);
http_add_header_field(&res->hdr, "Content-Type", r->type);
http_add_header_field(&res->hdr, "Cache-Control", "public, max-age=86400");
@ -189,13 +189,13 @@ static int request_handler(client_ctx_t *ctx) {
return 0;
}
if (ctx->conf->type == CONFIG_TYPE_LOCAL && strcmp(req->method, "TRACE") == 0) {
if (ctx->conf->type == CONFIG_TYPE_LOCAL && streq(req->method, "TRACE")) {
return 1;
} else if (dir_mode != URI_DIR_MODE_NO_VALIDATION) {
ssize_t size = sizeof(buf0);
url_decode(req->uri, buf0, &size);
int change_proto = (!client->enc && strncmp(uri->uri, "/.well-known/", 13) != 0);
if (strcmp(uri->uri, buf0) != 0 || change_proto) {
int change_proto = (!client->enc && !strstarts(uri->uri, "/.well-known/"));
if (!streq(uri->uri, buf0) || change_proto) {
res->status = http_get_status(308);
size = url_encode(uri->uri, strlen(uri->uri), buf0, sizeof(buf0));
if (change_proto) {
@ -271,7 +271,7 @@ int respond(client_ctx_t *ctx) {
snprintf(ctx->msg_content, sizeof(ctx->msg_content), " <ul>\n <li><a href=\"%s\">%s</a></li>\n </ul>\n", location, location);
}
}
} else if (strncmp(ctx->msg_content, "<!DOCTYPE html>", 15) == 0 || strncmp(ctx->msg_content, "<html", 5) == 0) {
} else if (strstarts(ctx->msg_content, "<!DOCTYPE html>") || strstarts(ctx->msg_content, "<html")) {
ctx->msg_content[0] = 0;
// TODO let relevant information pass?
}
@ -340,7 +340,7 @@ int respond(client_ctx_t *ctx) {
if (ctx->use_proxy) {
// reverse proxy
return 3;
} else if (strcmp(req->method, "HEAD") != 0) {
} else if (!streq(req->method, "HEAD")) {
// default response
if (ctx->msg_buf != NULL) {
ret = sock_send(client, ctx->msg_buf, ctx->content_length, 0);

View File

@ -33,7 +33,7 @@ static int tcp_acceptor(client_ctx_t *ctx) {
struct sockaddr_in6 server_addr;
inet_ntop(ctx->socket._addr.ipv6.sin6_family, &ctx->socket._addr.ipv6.sin6_addr, ctx->_c_addr, sizeof(ctx->_c_addr));
if (strncmp(ctx->_c_addr, "::ffff:", 7) == 0) {
if (strstarts(ctx->_c_addr, "::ffff:")) {
ctx->socket.addr = ctx->_c_addr + 7;
} else {
ctx->socket.addr = ctx->_c_addr;
@ -42,7 +42,7 @@ static int tcp_acceptor(client_ctx_t *ctx) {
socklen_t len = sizeof(server_addr);
getsockname(ctx->socket.socket, (struct sockaddr *) &server_addr, &len);
inet_ntop(server_addr.sin6_family, (void *) &server_addr.sin6_addr, ctx->_s_addr, sizeof(ctx->_s_addr));
if (strncmp(ctx->_s_addr, "::ffff:", 7) == 0) {
if (strstarts(ctx->_s_addr, "::ffff:")) {
ctx->socket.s_addr = ctx->_s_addr + 7;
} else {
ctx->socket.s_addr = ctx->_s_addr;