aboutsummaryrefslogtreecommitdiff
path: root/src/parse-fastcgi.rl
blob: a9e76360288b546d7007f0ad9268ef99f09c8be8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

#include "debug-fastcgi.h"

fcgi_context* fcgi_context_new(gboolean from_server, guint con_id) {
	fcgi_context *ctx = g_slice_new0(fcgi_context);
	ctx->buffer = g_string_sized_new(0);
	ctx->from_server = from_server;
	ctx->con_id = con_id;

	ctx->s_params = g_string_sized_new(0);

	return ctx;
}

void fcgi_context_free(gpointer _ctx) {
	fcgi_context* ctx = (fcgi_context*) _ctx;
	g_string_free(ctx->buffer, TRUE);
	g_string_free(ctx->s_params, TRUE);
	g_slice_free(fcgi_context, ctx);
}

#define USE_STREAM(s, p, pe) do {\
	g_string_append_len(ctx->s, (gchar*) p, pe - p); \
	p = (guint8*) ctx->s->str; \
	pe = p + ctx->s->len; \
} while(0)

#define EAT_STREAM(s, p) do {\
	g_string_erase(ctx->s, 0, p - (guint8*) ctx->s->str); \
	p = (guint8*) ctx->s->str; \
	pe = p + ctx->s->len; \
} while(0)

static gboolean get_key_value_pair_len(guint8 **_p, guint8 *pe, guint *_len1, guint *_len2) {
	guint8 *p = *_p;
	guint len1, len2;
	/* need at least 2 bytes */
	if (p + 2 >= pe) return FALSE;
	len1 = *p++;
	if (len1 & 0x80) {
		/* need at least 3+1 bytes */
		if (p + 4 >= pe) return FALSE;
		len1 = ((len1 & 0x7F) << 24) | (p[0] << 16) | (p[1] << 8) | p[2];
		p += 3;
	}
	len2 = *p++;
	if (len2 & 0x80) {
		/* need at least 3 bytes */
		if (p + 3 >= pe) return FALSE;
		len2 = ((len2 & 0x7F) << 24) | (p[0] << 16) | (p[1] << 8) | p[2];
		p += 3;
	}
	*_len1 = len1;
	*_len2 = len2;
	*_p = p;
	return TRUE;
}

void fcgi_packet_parse(fcgi_context *ctx, guint8 *p, guint8 *pe) {
	guint8 *eof = pe;
	GString tmp1, tmp2;
	UNUSED(eof);

	switch (ctx->FCGI_Record.type) {
	case FCGI_BEGIN_REQUEST: {
		guint role;
		guint8 flags;
		if (ctx->FCGI_Record.contentLength != 8) {
			g_print("wrong FCGI_BEGIN_REQUEST size from %s (%u): %u\n",
				from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.contentLength);
			return;
		}
		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,
			fcgi_role2string(role),
			fcgi_flags2string(flags)
		);
		break;
	}
	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);
			return;
		}
		g_print("abort request from %s (%u)\n",
			from_server_to_string(ctx->from_server), ctx->con_id);
		break;
	}
	case FCGI_END_REQUEST: {
		guint appStatus;
		guint8 protocolStatus;
		if (ctx->FCGI_Record.contentLength != 8) {
			g_print("wrong FCGI_END_REQUEST size from %s (%u): %u\n",
				from_server_to_string(ctx->from_server), ctx->con_id, (guint) ctx->FCGI_Record.contentLength);
			return;
		}
		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,
			appStatus,
			fcgi_protocol_status2string(protocolStatus)
		);
		break;
	}
	case FCGI_PARAMS: {
		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)" : "");
			return;
		}
		USE_STREAM(s_params, p, pe);
		while (get_key_value_pair_len(&p, pe, &len1, &len2)) {
			if (p + len1 + len2 > pe) {
				return;
			}
			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,
				s1->str, s2->str);
			g_string_free(s1, TRUE);
			g_string_free(s2, TRUE);
			p += len1 + len2;
			EAT_STREAM(s_params, p);
		}
		break;
	}
	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);
			return;
		}
		log_raw_split("stdin", ctx->from_server, ctx->con_id, 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);
			return;
		}
		log_raw_split("stdout", ctx->from_server, ctx->con_id, 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);
			return;
		}
		log_raw_split("stderr", ctx->from_server, ctx->con_id, 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);
			return;
		}
		log_raw_split("data", ctx->from_server, ctx->con_id, 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);
			return;
		}
		while (get_key_value_pair_len(&p, pe, &len1, &len2)) {
			if (p + len1 + len2 > pe) {
				return;
			}
			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,
					s1->str, s2->str);
			} else {
				g_print("get values from %s (%u): '%s'\n",
					from_server_to_string(ctx->from_server), ctx->con_id,
					s1->str);
			}
			g_string_free(s1, TRUE);
			g_string_free(s2, TRUE);
			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);
		}
		break;
	}
	
	case FCGI_GET_VALUES_RESULT: {
		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);
			return;
		}
		while (get_key_value_pair_len(&p, pe, &len1, &len2)) {
			if (p + len1 + len2 > pe) {
				return;
			}
			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,
				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);
		}
		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);
			return;
		}
		g_print("unknown type %u from %s (%u)\n", (guint) p[0],
			from_server_to_string(ctx->from_server), ctx->con_id);
		break;
	
	default:
		g_print("packet from %s (%u): type: %s, id: 0x%x, 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,
			(guint) ctx->FCGI_Record.contentLength
			);
		log_raw("packet data", ctx->from_server, ctx->con_id, g_string_set_const(&tmp1, (gchar*) p, pe - p));
		break;
	}
}

void fcgi_context_append(gpointer _ctx, const gchar* buf, gssize buflen) {
	fcgi_context *ctx = (fcgi_context*) _ctx;
	guint8* data;
	guint total_len;

	if (ctx->error) return;
	g_string_append_len(ctx->buffer, buf, 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);
	}
}