Compare commits
2 Commits
f96dc46ea7
...
add135b3ad
| Author | SHA1 | Date | |
|---|---|---|---|
|
add135b3ad
|
|||
|
2d250621c8
|
+17
-24
@@ -17,6 +17,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.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 *fastcgi_add_param(char *buf, const char *key, const char *value) {
|
||||||
char *ptr = buf;
|
char *ptr = buf;
|
||||||
unsigned long key_len = strlen(key);
|
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 = {
|
FCGI_Header header = {
|
||||||
.version = FCGI_VERSION_1,
|
.version = FCGI_VERSION_1,
|
||||||
.requestIdB1 = conn->req_id >> 8,
|
.requestId = htons(conn->req_id),
|
||||||
.requestIdB0 = conn->req_id & 0xFF,
|
|
||||||
.paddingLength = 0,
|
.paddingLength = 0,
|
||||||
.reserved = 0
|
.reserved = 0
|
||||||
};
|
};
|
||||||
|
|
||||||
header.type = FCGI_BEGIN_REQUEST;
|
header.type = FCGI_BEGIN_REQUEST;
|
||||||
header.contentLengthB1 = 0;
|
header.contentLength = htons(sizeof(FCGI_BeginRequestBody));
|
||||||
header.contentLengthB0 = sizeof(FCGI_BeginRequestBody);
|
|
||||||
FCGI_BeginRequestRecord begin = {
|
FCGI_BeginRequestRecord begin = {
|
||||||
header,
|
header,
|
||||||
{.roleB1 = (FCGI_RESPONDER >> 8) & 0xFF, .roleB0 = FCGI_RESPONDER & 0xFF, .flags = 0}
|
{.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);
|
unsigned short param_len = param_ptr - param_buf - sizeof(header);
|
||||||
header.type = FCGI_PARAMS;
|
header.type = FCGI_PARAMS;
|
||||||
header.contentLengthB1 = param_len >> 8;
|
header.contentLength = htons(param_len);
|
||||||
header.contentLengthB0 = param_len & 0xFF;
|
|
||||||
memcpy(param_buf, &header, sizeof(header));
|
memcpy(param_buf, &header, sizeof(header));
|
||||||
if (send(conn->socket, param_buf, param_len + sizeof(header), 0) != param_len + sizeof(header)) {
|
if (send(conn->socket, param_buf, param_len + sizeof(header), 0) != param_len + sizeof(header)) {
|
||||||
error("Unable to send to FastCGI socket");
|
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.type = FCGI_PARAMS;
|
||||||
header.contentLengthB1 = 0;
|
header.contentLength = htons(0);
|
||||||
header.contentLengthB0 = 0;
|
|
||||||
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) {
|
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) {
|
||||||
error("Unable to send to FastCGI socket");
|
error("Unable to send to FastCGI socket");
|
||||||
return -2;
|
return -2;
|
||||||
@@ -195,10 +193,8 @@ int fastcgi_close_stdin(fastcgi_cnx_t *conn) {
|
|||||||
FCGI_Header header = {
|
FCGI_Header header = {
|
||||||
.version = FCGI_VERSION_1,
|
.version = FCGI_VERSION_1,
|
||||||
.type = FCGI_STDIN,
|
.type = FCGI_STDIN,
|
||||||
.requestIdB1 = conn->req_id >> 8,
|
.requestId = htons(conn->req_id),
|
||||||
.requestIdB0 = conn->req_id & 0xFF,
|
.contentLength = htons(0),
|
||||||
.contentLengthB1 = 0,
|
|
||||||
.contentLengthB0 = 0,
|
|
||||||
.paddingLength = 0,
|
.paddingLength = 0,
|
||||||
.reserved = 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");
|
error("Unable to receive from FastCGI socket");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
|
req_id = ntohs(header.requestId);
|
||||||
content_len = (header.contentLengthB1 << 8) | header.contentLengthB0;
|
content_len = ntohs(header.contentLength);
|
||||||
content = malloc(content_len + header.paddingLength);
|
content = malloc(content_len + header.paddingLength);
|
||||||
ret = recv(conn->socket, content, content_len + header.paddingLength, 0);
|
ret = recv(conn->socket, content, content_len + header.paddingLength, 0);
|
||||||
if (ret != (content_len + header.paddingLength)) {
|
if (ret != (content_len + header.paddingLength)) {
|
||||||
@@ -402,8 +398,8 @@ int fastcgi_send(fastcgi_cnx_t *conn, sock *client, int flags) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
|
req_id = ntohs(header.requestId);
|
||||||
content_len = (header.contentLengthB1 << 8) | header.contentLengthB0;
|
content_len = ntohs(header.contentLength);
|
||||||
content = malloc(content_len + header.paddingLength);
|
content = malloc(content_len + header.paddingLength);
|
||||||
ptr = content;
|
ptr = content;
|
||||||
|
|
||||||
@@ -477,8 +473,8 @@ int fastcgi_dump(fastcgi_cnx_t *conn, char *buf, long len) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
|
req_id = ntohs(header.requestId);
|
||||||
content_len = (header.contentLengthB1 << 8) | header.contentLengthB0;
|
content_len = ntohs(header.contentLength);
|
||||||
content = malloc(content_len + header.paddingLength);
|
content = malloc(content_len + header.paddingLength);
|
||||||
|
|
||||||
long rcv_len = 0;
|
long rcv_len = 0;
|
||||||
@@ -527,10 +523,8 @@ int fastcgi_receive(fastcgi_cnx_t *conn, sock *client, unsigned long len) {
|
|||||||
FCGI_Header header = {
|
FCGI_Header header = {
|
||||||
.version = FCGI_VERSION_1,
|
.version = FCGI_VERSION_1,
|
||||||
.type = FCGI_STDIN,
|
.type = FCGI_STDIN,
|
||||||
.requestIdB1 = conn->req_id >> 8,
|
.requestId = htons(conn->req_id),
|
||||||
.requestIdB0 = conn->req_id & 0xFF,
|
.contentLength = htons(0),
|
||||||
.contentLengthB1 = 0,
|
|
||||||
.contentLengthB0 = 0,
|
|
||||||
.paddingLength = 0,
|
.paddingLength = 0,
|
||||||
.reserved = 0
|
.reserved = 0
|
||||||
};
|
};
|
||||||
@@ -543,8 +537,7 @@ int fastcgi_receive(fastcgi_cnx_t *conn, sock *client, unsigned long len) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rcv_len += ret;
|
rcv_len += ret;
|
||||||
header.contentLengthB1 = (ret >> 8) & 0xFF;
|
header.contentLength = htons(ret);
|
||||||
header.contentLengthB0 = ret & 0xFF;
|
|
||||||
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) goto err;
|
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) goto err;
|
||||||
if (send(conn->socket, buf, ret, 0) != ret) {
|
if (send(conn->socket, buf, ret, 0) != ret) {
|
||||||
err:
|
err:
|
||||||
|
|||||||
+2
-2
@@ -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;
|
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;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,10 +15,8 @@
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned char version;
|
unsigned char version;
|
||||||
unsigned char type;
|
unsigned char type;
|
||||||
unsigned char requestIdB1;
|
unsigned short requestId;
|
||||||
unsigned char requestIdB0;
|
unsigned short contentLength;
|
||||||
unsigned char contentLengthB1;
|
|
||||||
unsigned char contentLengthB0;
|
|
||||||
unsigned char paddingLength;
|
unsigned char paddingLength;
|
||||||
unsigned char reserved;
|
unsigned char reserved;
|
||||||
} FCGI_Header;
|
} FCGI_Header;
|
||||||
|
|||||||
+1
-1
@@ -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 sock_send_x(sock *s, void *buf, unsigned long len, int flags) {
|
||||||
long sent = 0;
|
long sent = 0;
|
||||||
for (long ret; sent < len; sent += ret) {
|
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)
|
if (ret <= 0)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-5
@@ -24,10 +24,13 @@
|
|||||||
#define LOG_NAME_LEN 12
|
#define LOG_NAME_LEN 12
|
||||||
#define LOG_PREFIX_LEN 256
|
#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 {
|
typedef struct {
|
||||||
log_lvl_t lvl;
|
log_lvl_t lvl;
|
||||||
|
long time;
|
||||||
char name[LOG_NAME_LEN];
|
char name[LOG_NAME_LEN];
|
||||||
char prefix[LOG_PREFIX_LEN];
|
char prefix[LOG_PREFIX_LEN];
|
||||||
char txt[LOG_MAX_MSG_SIZE];
|
char txt[LOG_MAX_MSG_SIZE];
|
||||||
@@ -58,14 +61,20 @@ static const char *level_keywords[] = {
|
|||||||
"DEBUG"
|
"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) {
|
static void err(const char *restrict msg) {
|
||||||
char err_buf[64];
|
char err_buf[64], time_buf[LOG_TIME_BUF_SIZE];
|
||||||
fprintf(stderr, ERR_STR LOG_PREFIX " %s: %s" CLR_STR "\n", "logger",
|
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)));
|
level_keywords[LOG_CRITICAL], msg, error_str(errno, err_buf, sizeof(err_buf)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void logmsgf(log_lvl_t level, const char *restrict format, ...) {
|
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_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
|
|
||||||
@@ -84,7 +93,11 @@ void logmsgf(log_lvl_t level, const char *restrict format, ...) {
|
|||||||
if (!alive) {
|
if (!alive) {
|
||||||
// no logger thread running
|
// no logger thread running
|
||||||
// simply write to stdout without synchronization
|
// 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);
|
vprintf(buf, args);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
} else {
|
} else {
|
||||||
@@ -125,6 +138,7 @@ void logmsgf(log_lvl_t level, const char *restrict format, ...) {
|
|||||||
|
|
||||||
vsnprintf(msg->txt, sizeof(msg->txt), buf, args);
|
vsnprintf(msg->txt, sizeof(msg->txt), buf, args);
|
||||||
msg->lvl = level;
|
msg->lvl = level;
|
||||||
|
msg->time = time(NULL);
|
||||||
|
|
||||||
if (name != NULL) {
|
if (name != NULL) {
|
||||||
snprintf(msg->name, sizeof(msg->name), "%s", (char *) name);
|
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) {
|
static void *logger_thread(void *arg) {
|
||||||
|
char time_buf[LOG_TIME_BUF_SIZE];
|
||||||
|
|
||||||
logger_set_name("logger");
|
logger_set_name("logger");
|
||||||
alive = 1;
|
alive = 1;
|
||||||
|
|
||||||
@@ -241,6 +257,7 @@ static void *logger_thread(void *arg) {
|
|||||||
|
|
||||||
printf("%s" LOG_PREFIX "%s%s %s\n",
|
printf("%s" LOG_PREFIX "%s%s %s\n",
|
||||||
(msg->lvl <= LOG_ERROR) ? ERR_STR : ((msg->lvl <= LOG_WARNING) ? WRN_STR : ""),
|
(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->name[0] != 0) ? (char *) msg->name : "", level_keywords[msg->lvl], CLR_STR,
|
||||||
(msg->prefix[0] != 0) ? (char *) msg->prefix : "", msg->txt);
|
(msg->prefix[0] != 0) ? (char *) msg->prefix : "", msg->txt);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user