|
@@ -19,30 +19,453 @@
|
19
|
19
|
|
20
|
20
|
#include "python_pyfcgi.h"
|
21
|
21
|
|
|
22
|
+/* Globals definitions */
|
|
23
|
+/* libpyfcgi context */
|
|
24
|
+libpyfcgi_context_t libpyfcgi = { NULL, NULL, NULL, 0, NULL, NULL, 0 };
|
|
25
|
+/* Python module methods specs */
|
|
26
|
+PyMethodDef pyfcgimodule_methods[] = {
|
|
27
|
+ {"start_response", (PyCFunction)pyfcgi_start_response, METH_FASTCALL, NULL},
|
|
28
|
+ {"write_body", (PyCFunction)pyfcgi_write_body, METH_FASTCALL, NULL},
|
|
29
|
+ {NULL} // Sentinel
|
|
30
|
+};
|
|
31
|
+/* Python module specs */
|
|
32
|
+PyModuleDef pyfcgimodule = {
|
|
33
|
+ PyModuleDef_HEAD_INIT,
|
|
34
|
+ "libpyfcgi",
|
|
35
|
+ "Python librarie for PyFCGi",
|
|
36
|
+ -1,
|
|
37
|
+ pyfcgimodule_methods,
|
|
38
|
+ NULL, NULL, NULL, NULL
|
|
39
|
+};
|
|
40
|
+
|
|
41
|
+/* Function definition */
|
|
42
|
+
|
|
43
|
+/**@brief Forge & send default headers */
|
|
44
|
+static void _default_headers()
|
|
45
|
+{
|
|
46
|
+ size_t sz;
|
|
47
|
+ void *tmp;
|
|
48
|
+ while(1)
|
|
49
|
+ {
|
|
50
|
+ sz = snprintf(libpyfcgi.heads_buf, libpyfcgi.heads_buf_sz,
|
|
51
|
+ LIBPYFCGI_DEFAULT_HEADERS, libpyfcgi.status_buf);
|
|
52
|
+
|
|
53
|
+ if(sz < libpyfcgi.heads_buf_sz) { break; }
|
|
54
|
+
|
|
55
|
+ sz = ((sz>>11)+1)<<11; // 2048 alloc blocks
|
|
56
|
+ tmp = realloc(libpyfcgi.heads_buf, sz);
|
|
57
|
+ if(!tmp)
|
|
58
|
+ {
|
|
59
|
+ pyfcgi_log(LOG_ALERT,
|
|
60
|
+ "Unable to realloc headers buffer : %s",
|
|
61
|
+ strerror(errno));
|
|
62
|
+ exit(PYFCGI_FATAL);
|
|
63
|
+ }
|
|
64
|
+ libpyfcgi.heads_buf = tmp;
|
|
65
|
+ libpyfcgi.heads_buf_sz = sz;
|
|
66
|
+ }
|
|
67
|
+}
|
|
68
|
+
|
|
69
|
+static void _set_status_buf()
|
|
70
|
+{
|
|
71
|
+ PyObject *bytes;
|
|
72
|
+
|
|
73
|
+ short decref = 1;
|
|
74
|
+ if(!libpyfcgi.status)
|
|
75
|
+ {
|
|
76
|
+ libpyfcgi.status = Py_BuildValue(LIBPYFCGI_DEFAULT_STATUS);
|
|
77
|
+ return;
|
|
78
|
+ }
|
|
79
|
+ if(PyBytes_Check(libpyfcgi.status))
|
|
80
|
+ {
|
|
81
|
+ bytes = libpyfcgi.status;
|
|
82
|
+ decref = 0;
|
|
83
|
+ }
|
|
84
|
+ else if(PyUnicode_Check(libpyfcgi.status))
|
|
85
|
+ {
|
|
86
|
+
|
|
87
|
+ bytes = PyUnicode_AsUTF8String(libpyfcgi.status);
|
|
88
|
+ if(!bytes)
|
|
89
|
+ {
|
|
90
|
+ pyfcgi_log(LOG_ERR, "Unable to encode status using UTF8 codec");
|
|
91
|
+ log_expt(LOG_ERR);
|
|
92
|
+ }
|
|
93
|
+ }
|
|
94
|
+ else
|
|
95
|
+ {
|
|
96
|
+ bytes = PyObject_Bytes(libpyfcgi.status);
|
|
97
|
+ if(!bytes)
|
|
98
|
+ {
|
|
99
|
+ pyfcgi_log(LOG_ERR, "Unable to convert status to bytes");
|
|
100
|
+ log_expt(LOG_ERR);
|
|
101
|
+ }
|
|
102
|
+ }
|
|
103
|
+ strncpy(libpyfcgi.status_buf, PyBytes_AsString(bytes), LIBPYFCGI_STATUS_SZ);
|
|
104
|
+ libpyfcgi.status_buf[LIBPYFCGI_STATUS_SZ-1] = '\0';
|
|
105
|
+ if(decref) { Py_DECREF(bytes); }
|
|
106
|
+}
|
|
107
|
+
|
|
108
|
+void libpyfcgi_set_headers_buf()
|
|
109
|
+{
|
|
110
|
+ PyObject *heads_iter, *head_tuple, *head_seq, **head, *bytes[2], *repr;
|
|
111
|
+ Py_ssize_t tsz, n_head, sz, buf_sz, buf_left, buf_off;
|
|
112
|
+ char errstr[256];
|
|
113
|
+ char *head_str[2], *buf_ptr;
|
|
114
|
+ int i;
|
|
115
|
+ void *tmp;
|
|
116
|
+ short content_type; // indicate if headers contains content-type field
|
|
117
|
+
|
|
118
|
+ _set_status_buf();
|
|
119
|
+
|
|
120
|
+ if(!libpyfcgi.headers)
|
|
121
|
+ {
|
|
122
|
+ _default_headers();
|
|
123
|
+ return;
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+repr = PyObject_ASCII(libpyfcgi.headers);
|
|
127
|
+pyfcgi_log(LOG_DEBUG, "Sending headers : '%s'", PyUnicode_AsUTF8(repr));
|
|
128
|
+Py_DECREF(repr);
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+ heads_iter = PyObject_GetIter(libpyfcgi.headers);
|
|
132
|
+ if(!heads_iter)
|
|
133
|
+ {
|
|
134
|
+ pyfcgi_log(LOG_ERR, "Unable to get iterator from given headers");
|
|
135
|
+ return;
|
|
136
|
+ }
|
|
137
|
+
|
|
138
|
+ n_head = 0;
|
|
139
|
+ buf_ptr = libpyfcgi.heads_buf; //buffer pointer
|
|
140
|
+ buf_sz = libpyfcgi.heads_buf_sz; //buffer size
|
|
141
|
+ buf_left = buf_sz; //buffer size left
|
|
142
|
+ buf_off = 0; //buffer pointer offset
|
|
143
|
+ while((head_tuple = PyIter_Next(heads_iter)))
|
|
144
|
+ {
|
|
145
|
+ head_seq = PySequence_Fast(head_tuple, "Headers element is not a sequence");
|
|
146
|
+ if((tsz = PySequence_Fast_GET_SIZE(head_seq)) != 2)
|
|
147
|
+ {
|
|
148
|
+ repr = PyObject_ASCII(head_tuple);
|
|
149
|
+ snprintf(errstr, 256,
|
|
150
|
+ "Headers element expected to have len == 2, but got len %ld for element %ld : %s", tsz, n_head, PyUnicode_AsUTF8(repr));
|
|
151
|
+ Py_DECREF(repr);
|
|
152
|
+ PyErr_SetString(PyExc_TypeError, errstr);
|
|
153
|
+ return;
|
|
154
|
+ }
|
|
155
|
+ head = PySequence_Fast_ITEMS(head_seq);
|
|
156
|
+ for(i=0; i<2; i++)
|
|
157
|
+ {
|
|
158
|
+ if(PyUnicode_Check(libpyfcgi.status))
|
|
159
|
+ {
|
|
160
|
+ bytes[i] = PyUnicode_AsUTF8String(head[i]);
|
|
161
|
+ if(!bytes[i])
|
|
162
|
+ {
|
|
163
|
+ //Error encoding header field
|
|
164
|
+ pyfcgi_log(LOG_ERR, "Unable to encode header field using UTF8 codec");
|
|
165
|
+ goto head_decode_err;
|
|
166
|
+ }
|
|
167
|
+ }
|
|
168
|
+ else
|
|
169
|
+ {
|
|
170
|
+ bytes[i] = PyObject_Bytes(head[i]);
|
|
171
|
+ if(!bytes[i])
|
|
172
|
+ {
|
|
173
|
+ pyfcgi_log(LOG_ERR, "Unable to convert header field to bytes");
|
|
174
|
+ goto head_decode_err;
|
|
175
|
+ }
|
|
176
|
+ }
|
|
177
|
+ head_str[i] = PyBytes_AsString(bytes[i]);
|
|
178
|
+
|
|
179
|
+ }
|
|
180
|
+ if(!strcmp(head_str[0], "Content-Type"))
|
|
181
|
+ {
|
|
182
|
+ content_type = 1;
|
|
183
|
+ }
|
|
184
|
+ // Writing current headers field & value in header buffer
|
|
185
|
+ while(1)
|
|
186
|
+ {
|
|
187
|
+ sz = snprintf(buf_ptr, buf_left,
|
|
188
|
+ "%s: %s\r\n", head_str[0], head_str[1]);
|
|
189
|
+ if(sz < buf_left)
|
|
190
|
+ {
|
|
191
|
+ buf_off += sz+1;
|
|
192
|
+ buf_ptr += sz;
|
|
193
|
+ break;
|
|
194
|
+ }
|
|
195
|
+
|
|
196
|
+ // 2048 alloc bloc
|
|
197
|
+ sz = (((buf_sz+sz)>>11)+1)<<11;
|
|
198
|
+ tmp = realloc(libpyfcgi.heads_buf, sz);
|
|
199
|
+ if(!tmp)
|
|
200
|
+ {
|
|
201
|
+ pyfcgi_log(LOG_ALERT,
|
|
202
|
+ "Unable to realloc headers buffer from iterator : %s",
|
|
203
|
+ strerror(errno));
|
|
204
|
+ exit(PYFCGI_FATAL);
|
|
205
|
+ }
|
|
206
|
+ buf_ptr = tmp + buf_off;
|
|
207
|
+ buf_left = sz - buf_off;
|
|
208
|
+ libpyfcgi.heads_buf = tmp;
|
|
209
|
+ libpyfcgi.heads_buf_sz = sz;
|
|
210
|
+ }
|
|
211
|
+
|
|
212
|
+ n_head++;
|
|
213
|
+ Py_DECREF(head_seq);
|
|
214
|
+ Py_DECREF(head_tuple);
|
|
215
|
+ }
|
|
216
|
+ Py_DECREF(heads_iter);
|
|
217
|
+ while(!content_type)
|
|
218
|
+ {
|
|
219
|
+ //Append text/html default content-type
|
|
220
|
+ /**@todo Autodetect content type ?? **/
|
|
221
|
+ sz = sizeof(LIBPYFCGI_DEFAULT_CTYPE)-1;
|
|
222
|
+ if(sz < buf_left)
|
|
223
|
+ {
|
|
224
|
+ buf_off += sz +1;
|
|
225
|
+ strncpy(buf_ptr, LIBPYFCGI_DEFAULT_CTYPE, buf_left);
|
|
226
|
+ buf_ptr += sz;
|
|
227
|
+ break;
|
|
228
|
+ }
|
|
229
|
+ sz = (((buf_sz+sz)>>11)+1)<<11;
|
|
230
|
+ tmp = realloc(libpyfcgi.heads_buf, sz);
|
|
231
|
+ if(!tmp)
|
|
232
|
+ {
|
|
233
|
+ pyfcgi_log(LOG_ALERT,
|
|
234
|
+ "Unable to realloc headers buffer from default Content-Type",
|
|
235
|
+ strerror(errno));
|
|
236
|
+ exit(PYFCGI_FATAL);
|
|
237
|
+ }
|
|
238
|
+ buf_ptr = tmp+buf_off;
|
|
239
|
+ buf_left = sz - buf_off;
|
|
240
|
+ libpyfcgi.heads_buf = tmp;
|
|
241
|
+ libpyfcgi.heads_buf_sz = sz;
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ return;
|
|
245
|
+head_decode_err:
|
|
246
|
+ Py_DECREF(head_seq);
|
|
247
|
+ Py_DECREF(head_tuple);
|
|
248
|
+ Py_DECREF(heads_iter);
|
|
249
|
+}
|
|
250
|
+
|
|
251
|
+void libpyfcgi_send_headers()
|
|
252
|
+{
|
|
253
|
+ if(!libpyfcgi.out)
|
|
254
|
+ {
|
|
255
|
+ // invalid context
|
|
256
|
+ PyErr_SetString(PyExc_RuntimeError,
|
|
257
|
+ "Trying to send headers but not in FCGI context");
|
|
258
|
+ return;
|
|
259
|
+ }
|
|
260
|
+
|
|
261
|
+ if(libpyfcgi.headers_sent)
|
|
262
|
+ {
|
|
263
|
+ PyErr_SetString(PyExc_RuntimeError,
|
|
264
|
+ "Headers allready sent...");
|
|
265
|
+ return;
|
|
266
|
+ }
|
|
267
|
+
|
|
268
|
+ libpyfcgi_set_headers_buf();
|
|
269
|
+ if(PyErr_Occurred())
|
|
270
|
+ {
|
|
271
|
+ /**@todo Better exception/error handling ? */
|
|
272
|
+ log_expt(LOG_ALERT);
|
|
273
|
+ exit(PYFCGI_FATAL);
|
|
274
|
+ }
|
|
275
|
+pyfcgi_log(LOG_DEBUG, "Headers ready to be sent : '%s'", libpyfcgi.heads_buf);
|
|
276
|
+ FCGX_PutStr(libpyfcgi.heads_buf, strlen(libpyfcgi.heads_buf),
|
|
277
|
+ libpyfcgi.out);
|
|
278
|
+pyfcgi_log(LOG_DEBUG, "Headers sent...");
|
|
279
|
+ FCGX_PutStr("\r\n", 2, libpyfcgi.out);
|
|
280
|
+ libpyfcgi.headers_sent = 1;
|
|
281
|
+}
|
|
282
|
+
|
22
|
283
|
PyMODINIT_FUNC
|
23
|
284
|
PyInit_libpyfcgi(void)
|
24
|
285
|
{
|
25
|
|
- PyObject *module;
|
26
|
|
- module = PyModule_Create(&pyfcgimodule);
|
27
|
|
- if(module == NULL) { return NULL; }
|
28
|
|
- return module;
|
|
286
|
+ if(libpyfcgi.self != NULL)
|
|
287
|
+ {
|
|
288
|
+ return libpyfcgi.self;
|
|
289
|
+ }
|
|
290
|
+ // init module & globals
|
|
291
|
+ libpyfcgi.status = NULL;
|
|
292
|
+ libpyfcgi.headers = NULL;
|
|
293
|
+ libpyfcgi.self = PyModule_Create(&pyfcgimodule);
|
|
294
|
+ if(libpyfcgi.self == NULL) { return NULL; }
|
|
295
|
+ return libpyfcgi.self;
|
29
|
296
|
}
|
30
|
297
|
|
31
|
298
|
PyObject* pyfcgi_start_response(PyObject* self, PyObject** argv, Py_ssize_t argc)
|
32
|
299
|
{
|
33
|
|
- char err[64];
|
34
|
|
- if(argc != 2)
|
|
300
|
+ char err[128];
|
|
301
|
+ PyObject *exc_info;
|
|
302
|
+ if(argc != 2 && argc != 3)
|
35
|
303
|
{
|
36
|
|
- snprintf(err, 64,
|
37
|
|
- "libpyfcgi.start_response() expected 2 arguments but %d given",
|
|
304
|
+ snprintf(err, 128,
|
|
305
|
+ "libpyfcgi.start_response() expected 2 to 3 arguments but %ld given",
|
38
|
306
|
argc);
|
39
|
307
|
PyErr_SetString(PyExc_ValueError, err);
|
40
|
308
|
Py_RETURN_NONE;
|
41
|
309
|
}
|
42
|
310
|
|
43
|
|
- libpyfcgi_clean_response();
|
|
311
|
+ if(libpyfcgi.status)
|
|
312
|
+ {
|
|
313
|
+ Py_DECREF(libpyfcgi.status);
|
|
314
|
+ }
|
|
315
|
+ if(libpyfcgi.headers)
|
|
316
|
+ {
|
|
317
|
+ Py_DECREF(libpyfcgi.headers);
|
|
318
|
+ }
|
|
319
|
+
|
|
320
|
+ libpyfcgi.status = argv[0];
|
|
321
|
+ libpyfcgi.headers = argv[1];
|
|
322
|
+ Py_INCREF(argv[0]);
|
|
323
|
+ Py_INCREF(argv[1]);
|
|
324
|
+ exc_info = argc>2?argv[2]:NULL;
|
|
325
|
+ if(exc_info)
|
|
326
|
+ {
|
|
327
|
+ Py_INCREF(argv[2]);
|
|
328
|
+ /* check if headers are sent, if yes re-raise the exception using
|
|
329
|
+ exc_info */
|
|
330
|
+ }
|
|
331
|
+
|
|
332
|
+PyObject *repr = PyObject_ASCII(libpyfcgi.headers);
|
|
333
|
+pyfcgi_log(LOG_DEBUG, "start_response called, headers : '%s'", PyUnicode_AsUTF8(repr));
|
|
334
|
+Py_DECREF(repr);
|
|
335
|
+
|
|
336
|
+ return PyObject_GetAttrString(self, "write_body");
|
|
337
|
+}
|
|
338
|
+
|
|
339
|
+PyObject* pyfcgi_write_body(PyObject* self, PyObject** argv, Py_ssize_t argc)
|
|
340
|
+{
|
|
341
|
+ char err[128];
|
|
342
|
+
|
|
343
|
+pyfcgi_log(LOG_DEBUG, "Write body called...");
|
|
344
|
+ if(argc != 1)
|
|
345
|
+ {
|
|
346
|
+ snprintf(err, 128,
|
|
347
|
+ "libpyfcgi.write_body() excpeted 1 argument but %ld given",
|
|
348
|
+ argc);
|
|
349
|
+ PyErr_SetString(PyExc_ValueError, err);
|
|
350
|
+ Py_RETURN_NONE;
|
|
351
|
+ }
|
|
352
|
+
|
|
353
|
+ return _pyfcgi_write_body(argv[0]);
|
|
354
|
+}
|
|
355
|
+
|
|
356
|
+PyObject* _pyfcgi_write_body(PyObject *body_data)
|
|
357
|
+{
|
|
358
|
+ char err[128];
|
|
359
|
+ const char *dat;
|
|
360
|
+ PyObject *bytes, *cur, *iter, *repr;
|
|
361
|
+ Py_ssize_t sz;
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+ if(!libpyfcgi.out)
|
|
365
|
+ {
|
|
366
|
+ // invalid context
|
|
367
|
+ PyErr_SetString(PyExc_RuntimeError,
|
|
368
|
+ "Trying to send body data but not in FCGI context");
|
|
369
|
+ Py_RETURN_NONE;
|
|
370
|
+ }
|
|
371
|
+
|
|
372
|
+ cur = NULL;
|
|
373
|
+ iter = NULL;
|
|
374
|
+ if(PyUnicode_Check(body_data) || PyBytes_Check(body_data))
|
|
375
|
+ {
|
|
376
|
+ cur = body_data;
|
|
377
|
+ }
|
|
378
|
+ else if (!(iter = PyObject_GetIter(body_data)))
|
|
379
|
+ {
|
|
380
|
+ log_expt(LOG_ERR);
|
|
381
|
+ repr = PyObject_ASCII(body_data);
|
|
382
|
+ snprintf(err, 128,
|
|
383
|
+ "libpyfcgi.write_body() expect argument to be a str a bytes or an iterator, but %s given",
|
|
384
|
+ PyUnicode_AsUTF8(repr));
|
|
385
|
+ Py_DECREF(repr);
|
|
386
|
+ PyErr_SetString(PyExc_ValueError, err);
|
|
387
|
+ Py_RETURN_NONE;
|
|
388
|
+ }
|
|
389
|
+ else
|
|
390
|
+ {
|
|
391
|
+ Py_INCREF(iter);
|
|
392
|
+ cur = PyIter_Next(iter);
|
|
393
|
+pyfcgi_log(LOG_DEBUG, "Got iter for writing body...");
|
|
394
|
+ }
|
|
395
|
+
|
|
396
|
+ if(!cur)
|
|
397
|
+ {
|
|
398
|
+ /**@todo trigger pyhon warning : empty body ? **/
|
|
399
|
+ Py_RETURN_NONE;
|
|
400
|
+ }
|
|
401
|
+
|
|
402
|
+ // if headers not sent yet, send them....
|
|
403
|
+ if(!libpyfcgi.headers_sent)
|
|
404
|
+ {
|
|
405
|
+pyfcgi_log(LOG_DEBUG, "Headers not sent... sending them...");
|
|
406
|
+ libpyfcgi_send_headers();
|
|
407
|
+ }
|
|
408
|
+
|
|
409
|
+ while(cur)
|
|
410
|
+ {
|
|
411
|
+ Py_INCREF(cur);
|
|
412
|
+ bytes = NULL;
|
|
413
|
+ if(PyUnicode_Check(cur))
|
|
414
|
+ {
|
|
415
|
+ dat = PyUnicode_AsUTF8AndSize(cur, &sz);
|
|
416
|
+ if(!dat)
|
|
417
|
+ {
|
|
418
|
+ repr = PyObject_ASCII(cur);
|
|
419
|
+ snprintf(err, 128,
|
|
420
|
+ "libpyfcgi.__write_body unable to encode string as UTF-8 : %s",
|
|
421
|
+ PyUnicode_AsUTF8(repr));
|
|
422
|
+ Py_DECREF(repr);
|
|
423
|
+ PyErr_SetString(PyExc_ValueError, err);
|
|
424
|
+ Py_RETURN_NONE;
|
|
425
|
+ }
|
|
426
|
+ }
|
|
427
|
+ else if(PyBytes_Check(cur))
|
|
428
|
+ {
|
|
429
|
+ dat = PyBytes_AsString(cur);
|
|
430
|
+ if(!dat)
|
|
431
|
+ {
|
|
432
|
+ /**@todo if no expt set, set error str */
|
|
433
|
+ Py_RETURN_NONE;
|
|
434
|
+ }
|
|
435
|
+ sz = PyBytes_GET_SIZE(cur);
|
|
436
|
+ }
|
|
437
|
+ else
|
|
438
|
+ {
|
|
439
|
+ bytes = PyObject_Bytes(cur);
|
|
440
|
+ if(!bytes)
|
|
441
|
+ {
|
|
442
|
+ repr = PyObject_ASCII(cur);
|
|
443
|
+ snprintf(err, 128,
|
|
444
|
+ "libpyfcgi.__write_body expected str or bytes but %s given",
|
|
445
|
+ PyUnicode_AsUTF8(repr));
|
|
446
|
+ Py_DECREF(repr);
|
|
447
|
+ PyErr_SetString(PyExc_ValueError, err);
|
|
448
|
+ Py_RETURN_NONE;
|
|
449
|
+ }
|
|
450
|
+ dat = PyBytes_AsString(cur);
|
|
451
|
+ if(!dat)
|
|
452
|
+ {
|
|
453
|
+ /**@todo if no expt set, set error str */
|
|
454
|
+ Py_RETURN_NONE;
|
|
455
|
+ }
|
|
456
|
+ sz = PyBytes_GET_SIZE(cur);
|
|
457
|
+ }
|
|
458
|
+
|
|
459
|
+pyfcgi_log(LOG_DEBUG, "Sending '%s'", dat);
|
|
460
|
+ FCGX_PutStr(dat, sz, libpyfcgi.out);
|
44
|
461
|
|
45
|
|
- response_status = argv[0];
|
46
|
|
- response_headers = argv[1];
|
|
462
|
+ if(bytes) { Py_DECREF(bytes); }
|
|
463
|
+ Py_DECREF(cur);
|
|
464
|
+ cur = iter?PyIter_Next(iter):NULL;
|
|
465
|
+ }
|
|
466
|
+ if(iter)
|
|
467
|
+ {
|
|
468
|
+ Py_DECREF(iter);
|
|
469
|
+ }
|
47
|
470
|
Py_RETURN_NONE;
|
48
|
471
|
}
|