Compare commits
3 Commits
9ea40b4793
...
629c66d62b
| Author | SHA1 | Date | |
|---|---|---|---|
| 629c66d62b | |||
| 75716075ba | |||
| 78ee3dc755 |
+1
-1
@@ -132,7 +132,7 @@ int http_parse_header_field(http_hdr *hdr, const char *buf, const char *end_ptr,
|
||||
if (hdr->last_field_num > list_size(hdr->fields))
|
||||
return http_error(HTTP_ERROR_GENERAL);
|
||||
|
||||
char *pos1 = (char *) buf, *pos2 = (char *) end_ptr;
|
||||
const char *pos1 = buf, *pos2 = end_ptr;
|
||||
if (buf[0] == ' ' || buf[0] == '\t') {
|
||||
if (hdr->last_field_num == -1)
|
||||
return http_error(HTTP_ERROR_GENERAL);
|
||||
|
||||
+4
-4
@@ -211,13 +211,13 @@ int proxy_request_header(http_req *req, sock *sock) {
|
||||
if (forwarded == NULL) {
|
||||
http_add_header_field(&req->hdr, "X-Forwarded-Host", http_get_header_field(&req->hdr, "Host"));
|
||||
} else {
|
||||
char *ptr = strchr(forwarded, ',');
|
||||
const char *ptr = strchr(forwarded, ',');
|
||||
unsigned long len;
|
||||
if (ptr != NULL) len = ptr - forwarded;
|
||||
else len = strlen(forwarded);
|
||||
ptr = strstr(forwarded, "host=");
|
||||
if ((ptr - forwarded) < len) {
|
||||
char *end = strchr(ptr, ';');
|
||||
const char *end = strchr(ptr, ';');
|
||||
if (end == NULL) len -= (ptr - forwarded);
|
||||
else len = (end - ptr);
|
||||
len -= 5;
|
||||
@@ -233,13 +233,13 @@ int proxy_request_header(http_req *req, sock *sock) {
|
||||
if (forwarded == NULL) {
|
||||
http_add_header_field(&req->hdr, "X-Forwarded-Proto", sock->enc ? "https" : "http");
|
||||
} else {
|
||||
char *ptr = strchr(forwarded, ',');
|
||||
const char *ptr = strchr(forwarded, ',');
|
||||
unsigned long len;
|
||||
if (ptr != NULL) len = ptr - forwarded;
|
||||
else len = strlen(forwarded);
|
||||
ptr = strstr(forwarded, "proto=");
|
||||
if ((ptr - forwarded) < len) {
|
||||
char *end = strchr(ptr, ';');
|
||||
const char *end = strchr(ptr, ';');
|
||||
if (end == NULL) len -= (ptr - forwarded);
|
||||
else len = (end - ptr);
|
||||
len -= 6;
|
||||
|
||||
+1
-1
@@ -430,7 +430,7 @@ long sock_recv_chunk_header(sock *s) {
|
||||
buf_ptr[ret1] = 0;
|
||||
|
||||
if ((ret2 = parse_chunk_header(buf, (buf_ptr - buf) + ret1, &len)) == -1) {
|
||||
if (errno == EPROTO) {
|
||||
if (errno != EAGAIN) {
|
||||
return -1;
|
||||
} else {
|
||||
if (sock_recv_x(s, buf_ptr, ret1, 0) == -1)
|
||||
|
||||
+1
-1
@@ -38,7 +38,7 @@ int path_exists(const char *path) {
|
||||
return ret == 0;
|
||||
}
|
||||
|
||||
int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mode) {
|
||||
int uri_init(http_uri *uri, const char *webroot, char *uri_str, int dir_mode) {
|
||||
char buf0[1024], buf1[1024], buf2[1024], buf3[1024], buf4[1024];
|
||||
int p_len;
|
||||
|
||||
|
||||
+1
-1
@@ -42,7 +42,7 @@ typedef struct {
|
||||
} http_uri;
|
||||
|
||||
|
||||
int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mode);
|
||||
int uri_init(http_uri *uri, const char *webroot, char *uri_str, int dir_mode);
|
||||
|
||||
int uri_init_cache(http_uri *uri);
|
||||
|
||||
|
||||
+5
-4
@@ -193,7 +193,7 @@ int strcpy_rem_webroot(char *dst, const char *src, const char *webroot) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int str_trim(char **start, char **end) {
|
||||
int str_trim(const char **start, const char **end) {
|
||||
if (start == NULL || end == NULL || *start == NULL || *end == NULL)
|
||||
return -1;
|
||||
|
||||
@@ -204,7 +204,7 @@ int str_trim(char **start, char **end) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int str_trim_lws(char **start, char **end) {
|
||||
int str_trim_lws(const char **start, const char **end) {
|
||||
if (start == NULL || end == NULL || *start == NULL || *end == NULL)
|
||||
return -1;
|
||||
|
||||
@@ -424,9 +424,9 @@ long ftelll(FILE *file) {
|
||||
return lines + 1;
|
||||
}
|
||||
|
||||
long parse_chunk_header(const char *buf, size_t len, size_t *ret_len) {
|
||||
long parse_chunk_header(const char *buf, const size_t len, size_t *ret_len) {
|
||||
for (int i = 0; i < len; i++) {
|
||||
char ch = buf[i];
|
||||
const char ch = buf[i];
|
||||
if (ch == '\r') {
|
||||
continue;
|
||||
} else if (ch == '\n' && i > 1 && buf[i - 1] == '\r') {
|
||||
@@ -437,5 +437,6 @@ long parse_chunk_header(const char *buf, size_t len, size_t *ret_len) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
errno = EAGAIN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
+2
-2
@@ -33,9 +33,9 @@ int mime_is_text(const char *restrict type);
|
||||
|
||||
int strcpy_rem_webroot(char *dst, const char *str, const char *webroot);
|
||||
|
||||
int str_trim(char **start, char **end);
|
||||
int str_trim(const char **start, const char **end);
|
||||
|
||||
int str_trim_lws(char **start, char **end);
|
||||
int str_trim_lws(const char **start, const char **end);
|
||||
|
||||
int streq(const char *restrict str1, const char *restrict str2);
|
||||
|
||||
|
||||
@@ -16,30 +16,27 @@
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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);
|
||||
static int fastcgi_handler_1(client_ctx_t *ctx);
|
||||
static int fastcgi_handler_2(client_ctx_t *ctx);
|
||||
|
||||
void fastcgi_handler_func(client_ctx_t *ctx) {
|
||||
logger_set_prefix("[%s%*s%s]%s", BLD_STR, ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix);
|
||||
|
||||
if (!ctx->chunks_transferred) {
|
||||
fastcgi_cnx_t *fcgi_cnx = NULL;
|
||||
int ret = fastcgi_handler_1(ctx, &fcgi_cnx);
|
||||
respond(ctx);
|
||||
if (ret == 0) {
|
||||
fastcgi_handler_2(ctx, fcgi_cnx);
|
||||
return;
|
||||
} else if (ctx->fcgi_ctx != NULL) {
|
||||
fastcgi_close(ctx->fcgi_ctx);
|
||||
ctx->fcgi_ctx = NULL;
|
||||
}
|
||||
const int ret = fastcgi_handler_1(ctx);
|
||||
respond(ctx);
|
||||
|
||||
if (ret == 0) {
|
||||
fastcgi_handler_2(ctx);
|
||||
return;
|
||||
}
|
||||
|
||||
request_complete(ctx);
|
||||
handle_request(ctx);
|
||||
if (ctx->fcgi_ctx != NULL) {
|
||||
fastcgi_close(ctx->fcgi_ctx);
|
||||
ctx->fcgi_ctx = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t **fcgi_cnx) {
|
||||
static int fastcgi_handler_1(client_ctx_t *ctx) {
|
||||
http_res *res = &ctx->res;
|
||||
http_req *req = &ctx->req;
|
||||
http_uri *uri = &ctx->uri;
|
||||
@@ -69,8 +66,8 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t **fcgi_cnx) {
|
||||
return 3;
|
||||
}
|
||||
|
||||
(*fcgi_cnx) = &fcgi_cnx_buf;
|
||||
fastcgi_handle_connection(ctx, fcgi_cnx);
|
||||
fastcgi_cnx_t *fcgi_cnx = &fcgi_cnx_buf;
|
||||
fastcgi_handle_connection(ctx, &fcgi_cnx);
|
||||
|
||||
int expect_100_continue = 0;
|
||||
const char *client_expect = http_get_header_field(&req->hdr, "Expect");
|
||||
@@ -89,13 +86,13 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t **fcgi_cnx) {
|
||||
http_send_100_continue(client);
|
||||
}
|
||||
unsigned long client_content_len = strtoul(client_content_length, NULL, 10);
|
||||
ret = fastcgi_receive(*fcgi_cnx, client, client_content_len);
|
||||
ret = fastcgi_receive(fcgi_cnx, client, client_content_len);
|
||||
} else if (strcontains(client_transfer_encoding, "chunked")) {
|
||||
if (expect_100_continue) {
|
||||
info(HTTP_1XX_STR "100 Continue" CLR_STR);
|
||||
http_send_100_continue(client);
|
||||
}
|
||||
ret = fastcgi_receive_chunked(*fcgi_cnx, client);
|
||||
ret = fastcgi_receive_chunked(fcgi_cnx, client);
|
||||
} else if (expect_100_continue) {
|
||||
fastcgi_close_cnx((&fcgi_cnx_buf));
|
||||
res->status = http_get_status(417);
|
||||
@@ -112,9 +109,9 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t **fcgi_cnx) {
|
||||
res->status = http_get_status(502);
|
||||
return 2;
|
||||
}
|
||||
fastcgi_close_stdin(*fcgi_cnx);
|
||||
fastcgi_close_stdin(fcgi_cnx);
|
||||
|
||||
if ((ret = fastcgi_header(*fcgi_cnx, res, err_msg)) != 0) {
|
||||
if ((ret = fastcgi_header(fcgi_cnx, res, err_msg)) != 0) {
|
||||
if (ret == -1) res->status = http_get_status(502);
|
||||
return ret;
|
||||
}
|
||||
@@ -147,7 +144,7 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t **fcgi_cnx) {
|
||||
ctx->content_length != -1 &&
|
||||
ctx->content_length < sizeof(ctx->msg_content))
|
||||
{
|
||||
fastcgi_dump(*fcgi_cnx, ctx->msg_content, ctx->content_length);
|
||||
fastcgi_dump(fcgi_cnx, ctx->msg_content, ctx->content_length);
|
||||
ctx->msg_content[ctx->content_length] = 0;
|
||||
return 1;
|
||||
}
|
||||
@@ -163,12 +160,15 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t **fcgi_cnx) {
|
||||
}
|
||||
|
||||
static void fastcgi_next_cb(chunk_ctx_t *ctx) {
|
||||
logger_set_prefix("[%s%*s%s]%s", BLD_STR, ADDRSTRLEN, ctx->client->req_host, CLR_STR, ctx->client->log_prefix);
|
||||
|
||||
if (ctx->client->fcgi_ctx) {
|
||||
fastcgi_close(ctx->client->fcgi_ctx);
|
||||
ctx->client->fcgi_ctx = NULL;
|
||||
}
|
||||
|
||||
fastcgi_handle(ctx->client);
|
||||
request_complete(ctx->client);
|
||||
handle_request(ctx->client);
|
||||
}
|
||||
|
||||
static void fastcgi_error_cb(chunk_ctx_t *ctx) {
|
||||
@@ -178,18 +178,19 @@ static void fastcgi_error_cb(chunk_ctx_t *ctx) {
|
||||
logger_set_prefix("[%s%*s%s]%s", BLD_STR, ADDRSTRLEN, ctx->client->req_host, CLR_STR, ctx->client->log_prefix);
|
||||
|
||||
warning("Closing connection due to FastCGI error");
|
||||
errno = 0;
|
||||
|
||||
if(ctx->client->fcgi_ctx) {
|
||||
fastcgi_close_error(ctx->client->fcgi_ctx);
|
||||
ctx->client->fcgi_ctx = NULL;
|
||||
}
|
||||
|
||||
request_complete(ctx->client);
|
||||
tcp_close(ctx->client);
|
||||
|
||||
errno = 0;
|
||||
}
|
||||
|
||||
static int fastcgi_handler_2(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
|
||||
int chunked = strcontains(http_get_header_field(&ctx->res.hdr, "Transfer-Encoding"), "chunked");
|
||||
handle_chunks(ctx, &fcgi_cnx->out, chunked ? SOCK_CHUNKED : 0, fastcgi_next_cb, fastcgi_error_cb);
|
||||
static int fastcgi_handler_2(client_ctx_t *ctx) {
|
||||
const int chunked = strcontains(http_get_header_field(&ctx->res.hdr, "Transfer-Encoding"), "chunked");
|
||||
handle_chunks(ctx, &ctx->fcgi_ctx->cnx.out, chunked ? SOCK_CHUNKED : 0, fastcgi_next_cb, fastcgi_error_cb);
|
||||
return 1;
|
||||
}
|
||||
|
||||
+7
-7
@@ -17,6 +17,12 @@
|
||||
#include "../lib/proxy.h"
|
||||
#include "../lib/fastcgi.h"
|
||||
|
||||
typedef struct {
|
||||
unsigned char closed:4;
|
||||
char log_prefix[128];
|
||||
fastcgi_cnx_t cnx;
|
||||
} fastcgi_ctx_t;
|
||||
|
||||
typedef struct {
|
||||
sock socket;
|
||||
int req_num;
|
||||
@@ -36,7 +42,7 @@ typedef struct {
|
||||
long content_length, transferred_length;
|
||||
char *msg_buf, *msg_buf_ptr, msg_content[1024];
|
||||
proxy_ctx_t *proxy;
|
||||
void *fcgi_ctx;
|
||||
fastcgi_ctx_t *fcgi_ctx;
|
||||
} client_ctx_t;
|
||||
|
||||
typedef struct {
|
||||
@@ -45,12 +51,6 @@ typedef struct {
|
||||
void *other;
|
||||
} ws_ctx_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned char closed:4;
|
||||
char log_prefix[128];
|
||||
fastcgi_cnx_t cnx;
|
||||
} fastcgi_ctx_t;
|
||||
|
||||
typedef struct {
|
||||
client_ctx_t *client;
|
||||
sock *socket;
|
||||
|
||||
@@ -39,7 +39,7 @@ void local_handler_func(client_ctx_t *ctx) {
|
||||
static int range_handler(client_ctx_t *ctx) {
|
||||
char buf[64];
|
||||
long num0, num1, num2;
|
||||
char *ptr;
|
||||
const char *ptr;
|
||||
int mode;
|
||||
const char *range = http_get_header_field(&ctx->req.hdr, "Range");
|
||||
|
||||
|
||||
@@ -118,6 +118,7 @@ static int proxy_handler_1(client_ctx_t *ctx) {
|
||||
}
|
||||
|
||||
static void proxy_chunk_next_cb(chunk_ctx_t *ctx) {
|
||||
logger_set_prefix("[%s%*s%s]%s", BLD_STR, ADDRSTRLEN, ctx->client->req_host, CLR_STR, ctx->client->log_prefix);
|
||||
if (proxy_unlock_ctx(ctx->client->proxy) == 1)
|
||||
proxy_peer_handle(ctx->client->proxy);
|
||||
|
||||
@@ -127,6 +128,8 @@ static void proxy_chunk_next_cb(chunk_ctx_t *ctx) {
|
||||
}
|
||||
|
||||
static void proxy_chunk_err_cb(chunk_ctx_t *ctx) {
|
||||
logger_set_prefix("[%s%*s%s]%s", BLD_STR, ADDRSTRLEN, ctx->client->req_host, CLR_STR, ctx->client->log_prefix);
|
||||
|
||||
proxy_close(ctx->client->proxy);
|
||||
proxy_unlock_ctx(ctx->client->proxy);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user