2 Commits

Author SHA1 Message Date
add135b3ad Use htons and ntohs in fastcgi 2023-01-12 20:40:40 +01:00
2d250621c8 Add time to log prefix 2023-01-12 20:29:34 +01:00
5 changed files with 44 additions and 36 deletions

View File

@@ -17,6 +17,8 @@
#include <string.h>
#include <unistd.h>
// TODO use pipes for stdin, stdout, stderr in FastCGI
char *fastcgi_add_param(char *buf, const char *key, const char *value) {
char *ptr = buf;
unsigned long key_len = strlen(key);
@@ -77,15 +79,13 @@ int fastcgi_init(fastcgi_cnx_t *conn, int mode, unsigned int req_num, const sock
FCGI_Header header = {
.version = FCGI_VERSION_1,
.requestIdB1 = conn->req_id >> 8,
.requestIdB0 = conn->req_id & 0xFF,
.requestId = htons(conn->req_id),
.paddingLength = 0,
.reserved = 0
};
header.type = FCGI_BEGIN_REQUEST;
header.contentLengthB1 = 0;
header.contentLengthB0 = sizeof(FCGI_BeginRequestBody);
header.contentLength = htons(sizeof(FCGI_BeginRequestBody));
FCGI_BeginRequestRecord begin = {
header,
{.roleB1 = (FCGI_RESPONDER >> 8) & 0xFF, .roleB0 = FCGI_RESPONDER & 0xFF, .flags = 0}
@@ -172,8 +172,7 @@ int fastcgi_init(fastcgi_cnx_t *conn, int mode, unsigned int req_num, const sock
unsigned short param_len = param_ptr - param_buf - sizeof(header);
header.type = FCGI_PARAMS;
header.contentLengthB1 = param_len >> 8;
header.contentLengthB0 = param_len & 0xFF;
header.contentLength = htons(param_len);
memcpy(param_buf, &header, sizeof(header));
if (send(conn->socket, param_buf, param_len + sizeof(header), 0) != param_len + sizeof(header)) {
error("Unable to send to FastCGI socket");
@@ -181,8 +180,7 @@ int fastcgi_init(fastcgi_cnx_t *conn, int mode, unsigned int req_num, const sock
}
header.type = FCGI_PARAMS;
header.contentLengthB1 = 0;
header.contentLengthB0 = 0;
header.contentLength = htons(0);
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) {
error("Unable to send to FastCGI socket");
return -2;
@@ -195,10 +193,8 @@ int fastcgi_close_stdin(fastcgi_cnx_t *conn) {
FCGI_Header header = {
.version = FCGI_VERSION_1,
.type = FCGI_STDIN,
.requestIdB1 = conn->req_id >> 8,
.requestIdB0 = conn->req_id & 0xFF,
.contentLengthB1 = 0,
.contentLengthB0 = 0,
.requestId = htons(conn->req_id),
.contentLength = htons(0),
.paddingLength = 0,
.reserved = 0
};
@@ -293,8 +289,8 @@ int fastcgi_header(fastcgi_cnx_t *conn, http_res *res, char *err_msg) {
error("Unable to receive from FastCGI socket");
return 1;
}
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
content_len = (header.contentLengthB1 << 8) | header.contentLengthB0;
req_id = ntohs(header.requestId);
content_len = ntohs(header.contentLength);
content = malloc(content_len + header.paddingLength);
ret = recv(conn->socket, content, content_len + header.paddingLength, 0);
if (ret != (content_len + header.paddingLength)) {
@@ -402,8 +398,8 @@ int fastcgi_send(fastcgi_cnx_t *conn, sock *client, int flags) {
return -1;
}
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
content_len = (header.contentLengthB1 << 8) | header.contentLengthB0;
req_id = ntohs(header.requestId);
content_len = ntohs(header.contentLength);
content = malloc(content_len + header.paddingLength);
ptr = content;
@@ -477,8 +473,8 @@ int fastcgi_dump(fastcgi_cnx_t *conn, char *buf, long len) {
return -1;
}
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
content_len = (header.contentLengthB1 << 8) | header.contentLengthB0;
req_id = ntohs(header.requestId);
content_len = ntohs(header.contentLength);
content = malloc(content_len + header.paddingLength);
long rcv_len = 0;
@@ -527,10 +523,8 @@ int fastcgi_receive(fastcgi_cnx_t *conn, sock *client, unsigned long len) {
FCGI_Header header = {
.version = FCGI_VERSION_1,
.type = FCGI_STDIN,
.requestIdB1 = conn->req_id >> 8,
.requestIdB0 = conn->req_id & 0xFF,
.contentLengthB1 = 0,
.contentLengthB0 = 0,
.requestId = htons(conn->req_id),
.contentLength = htons(0),
.paddingLength = 0,
.reserved = 0
};
@@ -543,8 +537,7 @@ int fastcgi_receive(fastcgi_cnx_t *conn, sock *client, unsigned long len) {
}
rcv_len += ret;
header.contentLengthB1 = (ret >> 8) & 0xFF;
header.contentLengthB0 = ret & 0xFF;
header.contentLength = htons(ret);
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) goto err;
if (send(conn->socket, buf, ret, 0) != ret) {
err:

View File

@@ -408,9 +408,9 @@ 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 ts, char *buf, size_t size) {
struct tm time_info;
strftime(buf, size, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r(&time, &time_info));
strftime(buf, size, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r(&ts, &time_info));
return buf;
}

View File

@@ -15,10 +15,8 @@
typedef struct {
unsigned char version;
unsigned char type;
unsigned char requestIdB1;
unsigned char requestIdB0;
unsigned char contentLengthB1;
unsigned char contentLengthB0;
unsigned short requestId;
unsigned short contentLength;
unsigned char paddingLength;
unsigned char reserved;
} FCGI_Header;

View File

@@ -109,7 +109,7 @@ long sock_send(sock *s, void *buf, unsigned long len, int flags) {
long sock_send_x(sock *s, void *buf, unsigned long len, int flags) {
long sent = 0;
for (long ret; sent < len; sent += ret) {
ret = sock_send(s, buf + sent, len - sent, flags);
ret = sock_send(s, (unsigned char *) buf + sent, len - sent, flags);
if (ret <= 0)
return ret;
}

View File

@@ -24,10 +24,13 @@
#define LOG_NAME_LEN 12
#define LOG_PREFIX_LEN 256
#define LOG_PREFIX "[%-8s][%-6s]"
#define LOG_PREFIX "[%8s][%-8s][%-6s]"
#define LOG_TIME_BUF_SIZE 9
#define LOG_TIME_FMT "%H:%M:%S"
typedef struct {
log_lvl_t lvl;
long time;
char name[LOG_NAME_LEN];
char prefix[LOG_PREFIX_LEN];
char txt[LOG_MAX_MSG_SIZE];
@@ -58,14 +61,20 @@ static const char *level_keywords[] = {
"DEBUG"
};
static const char *timestr(time_t ts, char *buf) {
struct tm time_info;
strftime(buf, LOG_TIME_BUF_SIZE, LOG_TIME_FMT, localtime_r(&ts, &time_info));
return buf;
}
static void err(const char *restrict msg) {
char err_buf[64];
fprintf(stderr, ERR_STR LOG_PREFIX " %s: %s" CLR_STR "\n", "logger",
char err_buf[64], time_buf[LOG_TIME_BUF_SIZE];
fprintf(stderr, ERR_STR LOG_PREFIX " %s: %s" CLR_STR "\n", timestr(time(NULL), time_buf), "logger",
level_keywords[LOG_CRITICAL], msg, error_str(errno, err_buf, sizeof(err_buf)));
}
void logmsgf(log_lvl_t level, const char *restrict format, ...) {
char buf[256], err_buf[256];
char buf[256], err_buf[256], time_buf[LOG_TIME_BUF_SIZE];
va_list args;
va_start(args, format);
@@ -84,7 +93,11 @@ void logmsgf(log_lvl_t level, const char *restrict format, ...) {
if (!alive) {
// no logger thread running
// simply write to stdout without synchronization
printf("%s" LOG_PREFIX "%s%s ", color, (name != NULL) ? (char *) name : "", level_keywords[level], CLR_STR, (prefix != NULL) ? (char *) prefix : "");
printf("%s" LOG_PREFIX "%s%s ", color,
timestr(time(NULL), time_buf),
(name != NULL) ? (char *) name : "",
level_keywords[level], CLR_STR,
(prefix != NULL) ? (char *) prefix : "");
vprintf(buf, args);
printf("\n");
} else {
@@ -125,6 +138,7 @@ void logmsgf(log_lvl_t level, const char *restrict format, ...) {
vsnprintf(msg->txt, sizeof(msg->txt), buf, args);
msg->lvl = level;
msg->time = time(NULL);
if (name != NULL) {
snprintf(msg->name, sizeof(msg->name), "%s", (char *) name);
@@ -220,6 +234,8 @@ void logger_set_prefix(const char *restrict format, ...) {
}
static void *logger_thread(void *arg) {
char time_buf[LOG_TIME_BUF_SIZE];
logger_set_name("logger");
alive = 1;
@@ -241,6 +257,7 @@ static void *logger_thread(void *arg) {
printf("%s" LOG_PREFIX "%s%s %s\n",
(msg->lvl <= LOG_ERROR) ? ERR_STR : ((msg->lvl <= LOG_WARNING) ? WRN_STR : ""),
(timestr(msg->time, time_buf)),
(msg->name[0] != 0) ? (char *) msg->name : "", level_keywords[msg->lvl], CLR_STR,
(msg->prefix[0] != 0) ? (char *) msg->prefix : "", msg->txt);