summaryrefslogtreecommitdiff
path: root/src/libfast/request.c
blob: 5d03227af604ba75ed0751281fa70d3680287dc3 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
 * Copyright (C) 2007 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#define _GNU_SOURCE

#include "request.h"

#include <library.h>
#include <utils/debug.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <ClearSilver/ClearSilver.h>

#include <threading/thread.h>
#include <threading/thread_value.h>

typedef struct private_request_t private_request_t;

/**
 * private data of the task manager
 */
struct private_request_t {

	/**
	 * public functions
	 */
	request_t public;

	/**
	 * FastCGI request object
	 */
	FCGX_Request req;

	/**
	 * length of the req.envp array
	 */
	int req_env_len;

	/**
	 * ClearSilver CGI Kit context
	 */
	CGI *cgi;

	/**
	 * ClearSilver HDF dataset for this request
	 */
	HDF *hdf;

	/**
	 * close the session?
	 */
	bool closed;

	/**
	 * reference count
	 */
	refcount_t ref;
};

/**
 * ClearSilver cgiwrap is not threadsave, so we use a private
 * context for each thread.
 */
static thread_value_t *thread_this;

/**
 * control variable for pthread_once
 */
pthread_once_t once = PTHREAD_ONCE_INIT;

/**
 * fcgiwrap read callback
 */
static int read_cb(void *null, char *buf, int size)
{
	private_request_t *this = (private_request_t*)thread_this->get(thread_this);

	return FCGX_GetStr(buf, size, this->req.in);
}

/**
 * fcgiwrap writef callback
 */
static int writef_cb(void *null, const char *format, va_list args)
{
	private_request_t *this = (private_request_t*)thread_this->get(thread_this);

	FCGX_VFPrintF(this->req.out, format, args);
	return 0;
}
/**
 * fcgiwrap write callback
 */
static int write_cb(void *null, const char *buf, int size)
{
	private_request_t *this = (private_request_t*)thread_this->get(thread_this);

	return FCGX_PutStr(buf, size, this->req.out);
}

/**
 * fcgiwrap getenv callback
 */
static char *getenv_cb(void *null, const char *key)
{
	char *value;
	private_request_t *this = (private_request_t*)thread_this->get(thread_this);

	value = FCGX_GetParam(key, this->req.envp);
	return strdupnull(value);
}

/**
 * fcgiwrap getenv callback
 */
static int putenv_cb(void *null, const char *key, const char *value)
{
	/* not supported */
	return 1;
}

/**
 * fcgiwrap iterenv callback
 */
static int iterenv_cb(void *null, int num, char **key, char **value)
{
	*key = NULL;
	*value = NULL;
	private_request_t *this = (private_request_t*)thread_this->get(thread_this);
	if (num < this->req_env_len)
	{
		char *eq;

		eq = strchr(this->req.envp[num], '=');
		if (eq)
		{
			*key = strndup(this->req.envp[num], eq - this->req.envp[num]);
			*value = strdup(eq + 1);
		}
		if (*key == NULL || *value == NULL)
		{
			free(*key);
			free(*value);
			return 1;
		}
	}
	return 0;
}

METHOD(request_t, get_cookie, char*,
	private_request_t *this, char *name)
{
	return hdf_get_valuef(this->hdf, "Cookie.%s", name);
}

METHOD(request_t, get_path, char*,
	private_request_t *this)
{
	char * path = FCGX_GetParam("PATH_INFO", this->req.envp);
	return path ? path : "";
}

METHOD(request_t, get_host, char*,
	private_request_t *this)
{
	char *addr = FCGX_GetParam("REMOTE_ADDR", this->req.envp);
	return addr ? addr : "";
}

METHOD(request_t, get_user_agent, char*,
	private_request_t *this)
{
	char *agent = FCGX_GetParam("HTTP_USER_AGENT", this->req.envp);
	return agent ? agent : "";
}

METHOD(request_t, get_query_data, char*,
	private_request_t *this, char *name)
{
	return hdf_get_valuef(this->hdf, "Query.%s", name);
}

METHOD(request_t, get_env_var, char*,
	private_request_t *this, char *name)
{
	return FCGX_GetParam(name, this->req.envp);
}

METHOD(request_t, read_data, int,
	private_request_t *this, char *buf, int len)
{
	return FCGX_GetStr(buf, len, this->req.in);
}

METHOD(request_t, get_base, char*,
	private_request_t *this)
{
	return FCGX_GetParam("SCRIPT_NAME", this->req.envp);
}

METHOD(request_t, add_cookie, void,
	private_request_t *this, char *name, char *value)
{
	thread_this->set(thread_this, this);
	cgi_cookie_set(this->cgi, name, value, NULL, NULL, NULL, 0, 0);
}

METHOD(request_t, redirect, void,
	private_request_t *this, char *fmt, ...)
{
	va_list args;

	FCGX_FPrintF(this->req.out, "Status: 303 See Other\n");
	FCGX_FPrintF(this->req.out, "Location: %s%s", get_base(this),
				 *fmt == '/' ? "" : "/");
	va_start(args, fmt);
	FCGX_VFPrintF(this->req.out, fmt, args);
	va_end(args);
	FCGX_FPrintF(this->req.out, "\n\n");
}

METHOD(request_t, get_referer, char*,
	private_request_t *this)
{
	return FCGX_GetParam("HTTP_REFERER", this->req.envp);
}

METHOD(request_t, to_referer, void,
	private_request_t *this)
{
	char *referer;

	referer = get_referer(this);
	if (referer)
	{
		FCGX_FPrintF(this->req.out, "Status: 303 See Other\n");
		FCGX_FPrintF(this->req.out, "Location: %s\n\n", referer);
	}
	else
	{
		redirect(this, "/");
	}
}

METHOD(request_t, session_closed, bool,
	private_request_t *this)
{
	return this->closed;
}

METHOD(request_t, close_session, void,
	private_request_t *this)
{
	this->closed = TRUE;
}

METHOD(request_t, serve, void,
	private_request_t *this, char *headers, chunk_t chunk)
{
	FCGX_FPrintF(this->req.out, "%s\n\n", headers);

	FCGX_PutStr(chunk.ptr, chunk.len, this->req.out);
}

METHOD(request_t, sendfile, bool,
	private_request_t *this, char *path, char *mime)
{
	struct stat sb;
	chunk_t data;
	void *addr;
	int fd, written;
	char buf[24];

	fd = open(path, O_RDONLY);
	if (fd == -1)
	{
		return FALSE;
	}
	if (fstat(fd, &sb) == -1)
	{
		close(fd);
		return FALSE;
	}
	addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
	if (addr == MAP_FAILED)
	{
		close(fd);
		return FALSE;
	}

	/* FCGX does not like large integers, print to a buffer using libc */
	snprintf(buf, sizeof(buf), "%lld", (int64_t)sb.st_size);
	FCGX_FPrintF(this->req.out, "Content-Length: %s\n", buf);
	if (mime)
	{
		FCGX_FPrintF(this->req.out, "Content-Type: %s\n", mime);
	}
	FCGX_FPrintF(this->req.out, "\n");

	data = chunk_create(addr, sb.st_size);

	while (data.len)
	{
		written = FCGX_PutStr(data.ptr, data.len, this->req.out);
		if (written == -1)
		{
			munmap(addr, sb.st_size);
			close(fd);
			return FALSE;
		}
		data = chunk_skip(data, written);
	}

	munmap(addr, sb.st_size);
	close(fd);
	return TRUE;
}

METHOD(request_t, render, void,
	private_request_t *this, char *template)
{
	NEOERR* err;

	thread_this->set(thread_this, this);
	err = cgi_display(this->cgi, template);
	if (err)
	{
		cgi_neo_error(this->cgi, err);
		nerr_log_error(err);
	}
}

METHOD(request_t, streamf, int,
	private_request_t *this, char *format, ...)
{
	va_list args;
	int written;

	va_start(args, format);
	written = FCGX_VFPrintF(this->req.out, format, args);
	va_end(args);
	if (written >= 0 &&
		FCGX_FFlush(this->req.out) == -1)
	{
		return -1;
	}
	return written;
}

METHOD(request_t, set, void,
	private_request_t *this, char *key, char *value)
{
	hdf_set_value(this->hdf, key, value);
}

METHOD(request_t, setf, void,
	private_request_t *this, char *format, ...)
{
	va_list args;

	va_start(args, format);
	hdf_set_valuevf(this->hdf, format, args);
	va_end(args);
}

METHOD(request_t, get_ref, request_t*,
	private_request_t *this)
{
	ref_get(&this->ref);
	return &this->public;
}

METHOD(request_t, destroy, void,
	private_request_t *this)
{
	if (ref_put(&this->ref))
	{
		thread_this->set(thread_this, this);
		cgi_destroy(&this->cgi);
		FCGX_Finish_r(&this->req);
		free(this);
	}
}

/**
 * This initialization method is guaranteed to run only once
 * for all threads.
 */
static void init(void)
{
	cgiwrap_init_emu(NULL, read_cb, writef_cb, write_cb,
					 getenv_cb, putenv_cb, iterenv_cb);
	thread_this = thread_value_create(NULL);
}

/*
 * see header file
 */
request_t *request_create(int fd, bool debug)
{
	NEOERR* err;
	private_request_t *this;
	bool failed = FALSE;

	INIT(this,
		.public = {
			.get_path = _get_path,
			.get_base = _get_base,
			.get_host = _get_host,
			.get_user_agent = _get_user_agent,
			.add_cookie = _add_cookie,
			.get_cookie = _get_cookie,
			.get_query_data = _get_query_data,
			.get_env_var = _get_env_var,
			.read_data = _read_data,
			.session_closed = _session_closed,
			.close_session = _close_session,
			.redirect = _redirect,
			.get_referer = _get_referer,
			.to_referer = _to_referer,
			.render = _render,
			.streamf = _streamf,
			.serve = _serve,
			.sendfile = _sendfile,
			.set = _set,
			.setf = _setf,
			.get_ref = _get_ref,
			.destroy = _destroy,
		},
		.ref = 1,
	);

	thread_cleanup_push(free, this);
	if (FCGX_InitRequest(&this->req, fd, 0) != 0 ||
		FCGX_Accept_r(&this->req) != 0)
	{
		failed = TRUE;
	}
	thread_cleanup_pop(failed);
	if (failed)
	{
		return NULL;
	}

	pthread_once(&once, init);
	thread_this->set(thread_this, this);

	while (this->req.envp[this->req_env_len] != NULL)
	{
		this->req_env_len++;
	}

	err = hdf_init(&this->hdf);
	if (!err)
	{
		hdf_set_value(this->hdf, "base", get_base(this));
		hdf_set_value(this->hdf, "Config.NoCache", "true");
		if (!debug)
		{
			hdf_set_value(this->hdf, "Config.TimeFooter", "0");
			hdf_set_value(this->hdf, "Config.CompressionEnabled", "1");
			hdf_set_value(this->hdf, "Config.WhiteSpaceStrip", "2");
		}

		err = cgi_init(&this->cgi, this->hdf);
		if (!err)
		{
			err = cgi_parse(this->cgi);
			if (!err)
			{
				return &this->public;
			}
			cgi_destroy(&this->cgi);
		}
	}
	nerr_log_error(err);
	FCGX_Finish_r(&this->req);
	free(this);
	return NULL;
}