Browse Source

Added FastCGI request id to log output

+ [cmake]: added extra warnings and fixed them.
master
Stefan Bühler 13 years ago
parent
commit
0bf277af0d
  1. 14
      src/CMakeLists.txt
  2. 8
      src/connection.c
  3. 8
      src/fcgi-debug.c
  4. 4
      src/fcgi-debug.h
  5. 8
      src/log.c
  6. 106
      src/parse-fastcgi.rl
  7. 8
      src/stream.c

14
src/CMakeLists.txt

@ -14,6 +14,18 @@ cmake_policy(SET CMP0005 OLD)
OPTION(BUILD_STATIC "build a static fcgi-debug")
IF(CMAKE_COMPILER_IS_GNUCC)
OPTION(BUILD_EXTRA_WARNINGS "extra warnings")
IF(BUILD_EXTRA_WARNINGS)
SET(WARN_FLAGS "-g -O2 -g2 -Wall -Wmissing-declarations -Wdeclaration-after-statement -Wcast-align -Winline -Wsign-compare -Wnested-externs -Wpointer-arith -Wformat-security")
# -Wl,--as-needed
# -Werror -Wbad-function-cast -Wmissing-prototypes
ELSE(BUILD_EXTRA_WARNINGS)
SET(WARN_FLAGS "")
ENDIF(BUILD_EXTRA_WARNINGS)
ENDIF(CMAKE_COMPILER_IS_GNUCC)
ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGE_FILES)
# libev
@ -95,7 +107,7 @@ ELSE(BUILD_STATIC)
ENDIF(BUILD_STATIC)
IF(CMAKE_COMPILER_IS_GNUCC)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wall -g -Wshadow -W -pedantic")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wall -g -Wshadow -W -pedantic ${WARN_FLAGS}")
SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0")
SET(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_WITHDEBINFO} -O2")

8
src/connection.c

@ -46,7 +46,7 @@ static char readbuf[4096];
static void fd_server_cb(struct ev_loop *loop, struct ev_io *w, int revents) {
connection *con = (connection*) w->data;
server *srv = con->srv;
// GString s;
/* GString s; */
UNUSED(loop);
if (revents & EV_READ) {
@ -56,7 +56,7 @@ static void fd_server_cb(struct ev_loop *loop, struct ev_io *w, int revents) {
connection_close(con);
return;
}
// log_raw("raw", TRUE, con->con_id, g_string_set_const(&s, readbuf, len));
/* log_raw("raw", TRUE, con->con_id, g_string_set_const(&s, readbuf, len)); */
stream_append(srv, &con->s_client, readbuf, len);
if (con->da_server) {
con->da_server(con->ctx_server, readbuf, len);
@ -74,7 +74,7 @@ static void fd_server_cb(struct ev_loop *loop, struct ev_io *w, int revents) {
static void fd_client_cb(struct ev_loop *loop, struct ev_io *w, int revents) {
connection *con = (connection*) w->data;
server *srv = con->srv;
// GString s;
/* GString s; */
UNUSED(loop);
if (!connection_connect(con)) return;
@ -85,7 +85,7 @@ static void fd_client_cb(struct ev_loop *loop, struct ev_io *w, int revents) {
connection_close(con);
return;
}
// log_raw("raw", FALSE, con->con_id, g_string_set_const(&s, readbuf, len));
/* log_raw("raw", FALSE, con->con_id, g_string_set_const(&s, readbuf, len)); */
stream_append(srv, &con->s_server, readbuf, len);
if (con->da_client) {
con->da_client(con->ctx_client, readbuf, len);

8
src/fcgi-debug.c

@ -28,7 +28,7 @@ static void accept_cb(struct ev_loop *loop, struct ev_io *w, int revents) {
ev_signal_stop(loop, &srv->sig_w_##n); \
} while (0)
void server_stop(server *srv) {
static void server_stop(server *srv) {
if (srv->tmpfile_name) {
unlink(srv->tmpfile_name);
g_free(srv->tmpfile_name);
@ -88,7 +88,7 @@ static void sigpipe_cb(struct ev_loop *loop, struct ev_signal *w, int revents) {
UNUSED(loop); UNUSED(w); UNUSED(revents);
}
void create_tmp_addr(server *srv) {
static void create_tmp_addr(server *srv) {
gchar *fn = g_strdup("/tmp/fcgi-debug.XXXXXX");
int fd;
@ -110,7 +110,7 @@ void create_tmp_addr(server *srv) {
srv->client.addr_len = len;
}
int client_bind(server *srv) {
static int client_bind(server *srv) {
int s;
if (-1 == (s = socket(AF_UNIX, SOCK_STREAM, 0))) {
@ -128,7 +128,7 @@ int client_bind(server *srv) {
return s;
}
pid_t spawn(char **argv, int s) {
static pid_t spawn(char **argv, int s) {
pid_t child;
switch (child = fork()) {

4
src/fcgi-debug.h

@ -107,8 +107,8 @@ void stream_append(server *srv, stream *s, char *buf, gssize bufsize);
gssize stream_write(server *srv, stream *s);
/* log.c */
void log_raw(const gchar *head, gboolean from_server, guint con_id, GString *data);
void log_raw_split(const gchar *head, gboolean from_server, guint con_id, GString *data);
void log_raw(const gchar *head, gboolean from_server, guint con_id, guint req_id, GString *data);
void log_raw_split(const gchar *head, gboolean from_server, guint con_id, guint req_id, GString *data);
/* debug-fastcgi.c */
void setup_debug_fastcgi(connection *con);

8
src/log.c

@ -1,6 +1,6 @@
#include "fcgi-debug.h"
void log_raw_split(const gchar *head, gboolean from_server, guint con_id, GString *data) {
void log_raw_split(const gchar *head, gboolean from_server, guint con_id, guint req_id, GString *data) {
const gchar *start = data->str, *end = start+data->len, *i;
GString *line = g_string_sized_new(0);
for ( ; start < end; ) {
@ -30,14 +30,14 @@ void log_raw_split(const gchar *head, gboolean from_server, guint con_id, GStrin
g_string_append_c(line, c);
}
}
g_print("%s from %s (%u): %s\n", head, from_server_to_string(from_server), con_id, line->str);
g_print("%s from %s (%u, %u): %s\n", head, from_server_to_string(from_server), con_id, req_id, line->str);
start = i;
}
g_string_free(line, TRUE);
}
void log_raw(const gchar *head, gboolean from_server, guint con_id, GString *data) {
void log_raw(const gchar *head, gboolean from_server, guint con_id, guint req_id, GString *data) {
GString *line = g_string_escape(data);
g_print("%s from %s (%u): %s\n", head, from_server_to_string(from_server), con_id, line->str);
g_print("%s from %s (%u, %u): %s\n", head, from_server_to_string(from_server), con_id, req_id, line->str);
g_string_free(line, TRUE);
}

106
src/parse-fastcgi.rl

@ -56,7 +56,7 @@ static gboolean get_key_value_pair_len(guint8 **_p, guint8 *pe, guint *_len1, gu
return TRUE;
}
void fcgi_packet_parse(fcgi_context *ctx, guint8 *p, guint8 *pe) {
static void fcgi_packet_parse(fcgi_context *ctx, guint8 *p, guint8 *pe) {
guint8 *eof = pe;
GString tmp1, tmp2;
UNUSED(eof);
@ -70,10 +70,14 @@ void fcgi_packet_parse(fcgi_context *ctx, guint8 *p, guint8 *pe) {
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.contentLength);
return;
}
if (0 == ctx->FCGI_Record.requestID) {
g_print("invalid requestID from %s (%u): %u\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID);
}
role = (p[0] << 8) | p[1];
flags = p[2];
g_print("begin request from %s (%u): role: %s, flags: %s\n",
from_server_to_string(ctx->from_server), ctx->con_id,
g_print("begin request from %s (%u, %u): role: %s, flags: %s\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID,
fcgi_role2string(role),
fcgi_flags2string(flags)
);
@ -81,12 +85,13 @@ void fcgi_packet_parse(fcgi_context *ctx, guint8 *p, guint8 *pe) {
}
case FCGI_ABORT_REQUEST: {
if (ctx->FCGI_Record.contentLength) {
g_print("wrong FCGI_ABORT_REQUEST size from %s (%u): %u\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.contentLength);
g_print("wrong FCGI_ABORT_REQUEST size from %s (%u, %u): %u\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.contentLength,
(guint) ctx->FCGI_Record.requestID);
return;
}
g_print("abort request from %s (%u)\n",
from_server_to_string(ctx->from_server), ctx->con_id);
g_print("abort request from %s (%u, %u)\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID);
break;
}
case FCGI_END_REQUEST: {
@ -99,8 +104,8 @@ void fcgi_packet_parse(fcgi_context *ctx, guint8 *p, guint8 *pe) {
}
appStatus = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
protocolStatus = p[4];
g_print("end request from %s (%u): applicationStatus: %u, protocolStatus: %s\n",
from_server_to_string(ctx->from_server), ctx->con_id,
g_print("end request from %s (%u, %u): applicationStatus: %u, protocolStatus: %s\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID,
appStatus,
fcgi_protocol_status2string(protocolStatus)
);
@ -110,8 +115,9 @@ void fcgi_packet_parse(fcgi_context *ctx, guint8 *p, guint8 *pe) {
guint len1, len2;
GString *s1, *s2;
if (!ctx->FCGI_Record.contentLength) {
g_print("params end from %s (%u)%s\n",
from_server_to_string(ctx->from_server), ctx->con_id, ctx->s_params->len ? " (unexpected)" : "");
g_print("params end from %s (%u, %u)%s\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID,
ctx->s_params->len ? " (unexpected)" : "");
return;
}
USE_STREAM(s_params, p, pe);
@ -121,8 +127,8 @@ void fcgi_packet_parse(fcgi_context *ctx, guint8 *p, guint8 *pe) {
}
s1 = g_string_escape(g_string_set_const(&tmp1, (gchar*) p, len1));
s2 = g_string_escape(g_string_set_const(&tmp2, (gchar*) p+len1, len2));
g_print("param from %s (%u): '%s' = '%s'\n",
from_server_to_string(ctx->from_server), ctx->con_id,
g_print("param from %s (%u, %u): '%s' = '%s'\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID,
s1->str, s2->str);
g_string_free(s1, TRUE);
g_string_free(s2, TRUE);
@ -133,43 +139,43 @@ void fcgi_packet_parse(fcgi_context *ctx, guint8 *p, guint8 *pe) {
}
case FCGI_STDIN:
if (!ctx->FCGI_Record.contentLength) {
g_print("stdin closed from %s (%u)\n",
from_server_to_string(ctx->from_server), ctx->con_id);
g_print("stdin closed from %s (%u, %u)\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID);
return;
}
log_raw_split("stdin", ctx->from_server, ctx->con_id, g_string_set_const(&tmp1, (gchar*) p, pe - p));
log_raw_split("stdin", ctx->from_server, ctx->con_id, ctx->FCGI_Record.requestID, g_string_set_const(&tmp1, (gchar*) p, pe - p));
break;
case FCGI_STDOUT:
if (!ctx->FCGI_Record.contentLength) {
g_print("stdout closed from %s (%u)\n",
from_server_to_string(ctx->from_server), ctx->con_id);
g_print("stdout closed from %s (%u, %u)\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID);
return;
}
log_raw_split("stdout", ctx->from_server, ctx->con_id, g_string_set_const(&tmp1, (gchar*) p, pe - p));
log_raw_split("stdout", ctx->from_server, ctx->con_id, ctx->FCGI_Record.requestID, g_string_set_const(&tmp1, (gchar*) p, pe - p));
break;
case FCGI_STDERR:
if (!ctx->FCGI_Record.contentLength) {
g_print("stderr closed from %s (%u)\n",
from_server_to_string(ctx->from_server), ctx->con_id);
g_print("stderr closed from %s (%u, %u)\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID);
return;
}
log_raw_split("stderr", ctx->from_server, ctx->con_id, g_string_set_const(&tmp1, (gchar*) p, pe - p));
log_raw_split("stderr", ctx->from_server, ctx->con_id, ctx->FCGI_Record.requestID, g_string_set_const(&tmp1, (gchar*) p, pe - p));
break;
case FCGI_DATA:
if (!ctx->FCGI_Record.contentLength) {
g_print("data closed from %s (%u)\n",
from_server_to_string(ctx->from_server), ctx->con_id);
g_print("data closed from %s (%u, %u)\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID);
return;
}
log_raw_split("data", ctx->from_server, ctx->con_id, g_string_set_const(&tmp1, (gchar*) p, pe - p));
log_raw_split("data", ctx->from_server, ctx->con_id, ctx->FCGI_Record.requestID, g_string_set_const(&tmp1, (gchar*) p, pe - p));
break;
case FCGI_GET_VALUES: {
guint len1, len2;
GString *s1, *s2;
if (!ctx->FCGI_Record.contentLength) {
g_print("empty get values from %s (%u)\n",
from_server_to_string(ctx->from_server), ctx->con_id);
g_print("empty get values from %s (%u, %u)\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID);
return;
}
while (get_key_value_pair_len(&p, pe, &len1, &len2)) {
@ -179,12 +185,12 @@ void fcgi_packet_parse(fcgi_context *ctx, guint8 *p, guint8 *pe) {
s1 = g_string_escape(g_string_set_const(&tmp1, (gchar*) p, len1));
s2 = g_string_escape(g_string_set_const(&tmp2, (gchar*) p+len1, len2));
if (len2) {
g_print("get values from %s (%u): '%s' = '%s'?\n",
from_server_to_string(ctx->from_server), ctx->con_id,
g_print("get values from %s (%u, %u): '%s' = '%s'?\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID,
s1->str, s2->str);
} else {
g_print("get values from %s (%u): '%s'\n",
from_server_to_string(ctx->from_server), ctx->con_id,
g_print("get values from %s (%u, %u): '%s'\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID,
s1->str);
}
g_string_free(s1, TRUE);
@ -192,8 +198,8 @@ void fcgi_packet_parse(fcgi_context *ctx, guint8 *p, guint8 *pe) {
p += len1 + len2;
}
if (p != pe) {
g_print("unexpected end of get values from %s (%u)\n",
from_server_to_string(ctx->from_server), ctx->con_id);
g_print("unexpected end of get values from %s (%u, %u)\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID);
}
break;
}
@ -202,8 +208,8 @@ void fcgi_packet_parse(fcgi_context *ctx, guint8 *p, guint8 *pe) {
guint len1, len2;
GString *s1, *s2;
if (!ctx->FCGI_Record.contentLength) {
g_print("empty get values result from %s (%u)\n",
from_server_to_string(ctx->from_server), ctx->con_id);
g_print("empty get values result from %s (%u, %u)\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID);
return;
}
while (get_key_value_pair_len(&p, pe, &len1, &len2)) {
@ -212,38 +218,38 @@ void fcgi_packet_parse(fcgi_context *ctx, guint8 *p, guint8 *pe) {
}
s1 = g_string_escape(g_string_set_const(&tmp1, (gchar*) p, len1));
s2 = g_string_escape(g_string_set_const(&tmp2, (gchar*) p+len1, len2));
g_print("get values result from %s (%u): '%s' = '%s'\n",
from_server_to_string(ctx->from_server), ctx->con_id,
g_print("get values result from %s (%u, %u): '%s' = '%s'\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID,
s1->str, s2->str);
g_string_free(s1, TRUE);
g_string_free(s2, TRUE);
p += len1 + len2;
}
if (p != pe) {
g_print("unexpected end of get values result from %s (%u)\n",
from_server_to_string(ctx->from_server), ctx->con_id);
g_print("unexpected end of get values result from %s (%u, %u)\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID);
}
break;
}
case FCGI_UNKNOWN_TYPE:
if (ctx->FCGI_Record.contentLength != 8) {
g_print("wrong FCGI_UNKNOWN_TYPE size from %s (%u): %u\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.contentLength);
g_print("wrong FCGI_UNKNOWN_TYPE size from %s (%u, %u): %u\n",
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID, (guint) ctx->FCGI_Record.contentLength);
return;
}
g_print("unknown type %u from %s (%u)\n", (guint) p[0],
from_server_to_string(ctx->from_server), ctx->con_id);
g_print("unknown type %u from %s (%u, %u)\n", (guint) p[0],
from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.requestID);
break;
default:
g_print("packet from %s (%u): type: %s, id: 0x%x, contentLength: 0x%x\n",
g_print("packet from %s (%u, %u): type: %s, contentLength: 0x%x\n",
from_server_to_string(ctx->from_server), ctx->con_id,
fcgi_type2string(ctx->FCGI_Record.type),
(guint) ctx->FCGI_Record.requestID,
fcgi_type2string(ctx->FCGI_Record.type),
(guint) ctx->FCGI_Record.contentLength
);
log_raw("packet data", ctx->from_server, ctx->con_id, g_string_set_const(&tmp1, (gchar*) p, pe - p));
log_raw("packet data", ctx->from_server, ctx->con_id, ctx->FCGI_Record.requestID, g_string_set_const(&tmp1, (gchar*) p, pe - p));
break;
}
}
@ -258,19 +264,19 @@ void fcgi_context_append(gpointer _ctx, const gchar* buf, gssize buflen) {
for (;;) {
data = (guint8*) ctx->buffer->str;
if (ctx->buffer->len < FCGI_HEADER_LEN) return;
ctx->FCGI_Record.version = data[0];
ctx->FCGI_Record.type = data[1];
ctx->FCGI_Record.requestID = (data[2] << 8) | (data[3]);
ctx->FCGI_Record.contentLength = (data[4] << 8) | (data[5]);
ctx->FCGI_Record.paddingLength = data[6];
/* ignore data[7] */
total_len = FCGI_HEADER_LEN + ctx->FCGI_Record.contentLength + ctx->FCGI_Record.paddingLength;
if (ctx->buffer->len < total_len) return;
fcgi_packet_parse(ctx, (guint8*) (FCGI_HEADER_LEN + ctx->buffer->str), (guint8*) (ctx->FCGI_Record.contentLength + FCGI_HEADER_LEN + ctx->buffer->str));
g_string_erase(ctx->buffer, 0, total_len);
}

8
src/stream.c

@ -1,18 +1,22 @@
#include "fcgi-debug.h"
void stream_init(server *srv, stream *s1, stream *s2, int fd1, int fd2, ev_io_cb cb1, ev_io_cb cb2, void* data) {
ev_io *w1, *w2;
UNUSED(srv);
fd_init(fd1);
fd_init(fd2);
s1->other = s2;
s2->other = s1;
s1->fd = fd1;
s2->fd = fd2;
ev_io *w1 = &s1->watcher, *w2 = &s2->watcher;
w1 = &s1->watcher; w2 = &s2->watcher;
ev_io_init(w1, cb1, fd1, 0);
ev_io_init(w2, cb2, fd2, 0);
w1->data = data;
w2->data = data;
s1->buffer = g_string_sized_new(0);
s2->buffer = g_string_sized_new(0);
}
@ -44,7 +48,7 @@ void stream_start(server *srv, stream *s) {
ev_io_add_events(srv->loop, &s->watcher, events);
}
gssize stream_closed(server *srv, stream *s) {
static gssize stream_closed(server *srv, stream *s) {
ev_io_stop(srv->loop, &s->watcher);
if (s->fd != -1) {
shutdown(s->fd, SHUT_RDWR);

Loading…
Cancel
Save