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
|
/**
* @file request.c
*
* @brief Implementation of request_t.
*
*/
/*
* 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 <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <ClearSilver/ClearSilver.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;
/**
* ClearSilver CGI Kit context
*/
CGI *cgi;
/**
* ClearSilver HDF dataset for this request
*/
HDF *hdf;
};
/**
* key to a thread specific FCGX_Request, used for ClearSilver cgiwrap callbacks.
* ClearSilver cgiwrap is not threadsave, so we use a private
* context for each thread.
*/
static pthread_key_t req_key;
/**
* length of param list in req->envp
*/
static pthread_key_t req_env_len_key;
/**
* 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)
{
FCGX_Request *req = (FCGX_Request*)pthread_getspecific(req_key);
return FCGX_GetStr(buf, size, req->in);
}
/**
* fcgiwrap writef callback
*/
static int writef_cb(void *null, const char *format, va_list args)
{
FCGX_Request *req = (FCGX_Request*)pthread_getspecific(req_key);
FCGX_VFPrintF(req->out, format, args);
return 0;
}
/**
* fcgiwrap write callback
*/
static int write_cb(void *null, const char *buf, int size)
{
FCGX_Request *req = (FCGX_Request*)pthread_getspecific(req_key);
return FCGX_PutStr(buf, size, req->out);
}
/**
* fcgiwrap getenv callback
*/
static char *getenv_cb(void *null, const char *key)
{
char *value;
FCGX_Request *req = (FCGX_Request*)pthread_getspecific(req_key);
value = FCGX_GetParam(key, req->envp);
return value ? strdup(value) : NULL;
}
/**
* 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;
FCGX_Request *req = (FCGX_Request*)pthread_getspecific(req_key);
int req_env_len = (int)pthread_getspecific(req_env_len_key);
if (num < req_env_len)
{
char *eq;
eq = strchr(req->envp[num], '=');
if (eq)
{
*key = strndup(req->envp[num], eq - req->envp[num]);
*value = strdup(eq + 1);
}
if (*key == NULL || *value == NULL)
{
free(*key);
free(*value);
return 1;
}
}
return 0;
}
/**
* Implementation of request_t.get_cookie.
*/
static char* get_cookie(private_request_t *this, char *name)
{
return hdf_get_valuef(this->hdf, "Cookie.%s", name);
}
/**
* Implementation of request_t.get_path.
*/
static char* get_path(private_request_t *this)
{
char * path = FCGX_GetParam("PATH_INFO", this->req->envp);
return path ? path : "";
}
/**
* Implementation of request_t.get_post_data.
*/
static char* get_query_data(private_request_t *this, char *name)
{
return hdf_get_valuef(this->hdf, "Query.%s", name);
}
/**
* Implementation of request_t.add_cookie.
*/
static void add_cookie(private_request_t *this, char *name, char *value)
{
cgi_cookie_set (this->cgi, name, value,
FCGX_GetParam("SCRIPT_NAME", this->req->envp),
NULL, NULL, 0, 0);
}
/**
* Implementation of request_t.redirect.
*/
static void redirect(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",
FCGX_GetParam("SCRIPT_NAME", this->req->envp),
*fmt == '/' ? "" : "/");
va_start(args, fmt);
FCGX_VFPrintF(this->req->out, fmt, args);
va_end(args);
FCGX_FPrintF(this->req->out, "\n\n");
}
/**
* Implementation of request_t.get_base.
*/
static char* get_base(private_request_t *this)
{
return FCGX_GetParam("SCRIPT_NAME", this->req->envp);
}
/**
* Implementation of request_t.serve.
*/
static void serve(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);
}
/**
* Implementation of request_t.render.
*/
static void render(private_request_t *this, char *template)
{
NEOERR* err;
err = cgi_display(this->cgi, template);
if (err)
{
cgi_neo_error(this->cgi, err);
nerr_log_error(err);
}
return;
}
/**
* Implementation of request_t.set.
*/
static void set(private_request_t *this, char *key, char *value)
{
hdf_set_value(this->hdf, key, value);
}
/**
* Implementation of request_t.setf.
*/
static void setf(private_request_t *this, char *format, ...)
{
va_list args;
va_start(args, format);
hdf_set_valuevf(this->hdf, format, args);
va_end(args);
}
/**
* Implementation of request_t.destroy
*/
static void destroy(private_request_t *this)
{
cgi_destroy(&this->cgi);
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);
pthread_key_create(&req_key, NULL);
pthread_key_create(&req_env_len_key, NULL);
}
/*
* see header file
*/
request_t *request_create(FCGX_Request *request, bool debug)
{
NEOERR* err;
private_request_t *this = malloc_thing(private_request_t);
this->public.get_path = (char*(*)(request_t*))get_path;
this->public.get_base = (char*(*)(request_t*))get_base;
this->public.add_cookie = (void(*)(request_t*, char *name, char *value))add_cookie;
this->public.get_cookie = (char*(*)(request_t*,char*))get_cookie;
this->public.get_query_data = (char*(*)(request_t*, char *name))get_query_data;
this->public.redirect = (void(*)(request_t*, char *fmt,...))redirect;
this->public.render = (void(*)(request_t*,char*))render;
this->public.serve = (void(*)(request_t*,char*,chunk_t))serve;
this->public.set = (void(*)(request_t*, char *, char*))set;
this->public.setf = (void(*)(request_t*, char *format, ...))setf;
this->public.destroy = (void(*)(request_t*))destroy;
pthread_once(&once, init);
this->req = request;
pthread_setspecific(req_key, (void*)request);
int req_env_len = 0;
while (request->envp[req_env_len] != NULL)
{
req_env_len++;
}
pthread_setspecific(req_env_len_key, (void*)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);
free(this);
return NULL;
}
|