|
@@ -123,12 +123,6 @@ void libpyfcgi_set_headers_buf()
|
123
|
123
|
return;
|
124
|
124
|
}
|
125
|
125
|
|
126
|
|
-repr = PyObject_ASCII(libpyfcgi.headers);
|
127
|
|
-Py_INCREF(repr);
|
128
|
|
-pyfcgi_log(LOG_DEBUG, "Sending headers : '%s'", PyUnicode_AsUTF8(repr));
|
129
|
|
-Py_DECREF(repr);
|
130
|
|
-
|
131
|
|
-
|
132
|
126
|
heads_iter = PyObject_GetIter(libpyfcgi.headers);
|
133
|
127
|
if(!heads_iter)
|
134
|
128
|
{
|
|
@@ -257,6 +251,9 @@ head_decode_err:
|
257
|
251
|
|
258
|
252
|
void libpyfcgi_send_headers()
|
259
|
253
|
{
|
|
254
|
+ size_t sz;
|
|
255
|
+ int ret;
|
|
256
|
+
|
260
|
257
|
if(!libpyfcgi.out)
|
261
|
258
|
{
|
262
|
259
|
// invalid context
|
|
@@ -279,11 +276,20 @@ void libpyfcgi_send_headers()
|
279
|
276
|
log_expt(LOG_ALERT);
|
280
|
277
|
exit(PYFCGI_FATAL);
|
281
|
278
|
}
|
282
|
|
-pyfcgi_log(LOG_DEBUG, "Headers ready to be sent : '%s'", libpyfcgi.heads_buf);
|
283
|
|
- FCGX_PutStr(libpyfcgi.heads_buf, strlen(libpyfcgi.heads_buf),
|
284
|
|
- libpyfcgi.out);
|
285
|
|
-pyfcgi_log(LOG_DEBUG, "Headers sent...");
|
286
|
|
- FCGX_PutStr("\r\n", 2, libpyfcgi.out);
|
|
279
|
+ sz = strlen(libpyfcgi.heads_buf);
|
|
280
|
+ ret = FCGX_PutStr(libpyfcgi.heads_buf, sz, libpyfcgi.out);
|
|
281
|
+ if(ret < 0 )
|
|
282
|
+ {
|
|
283
|
+ pyfcgi_log(LOG_ERR, "Unable to send headers");
|
|
284
|
+ return;
|
|
285
|
+ }
|
|
286
|
+ libpyfcgi.rep_sz += ret;
|
|
287
|
+ ret = FCGX_PutStr("\r\n", 2, libpyfcgi.out);
|
|
288
|
+ if(ret < 0)
|
|
289
|
+ {
|
|
290
|
+ pyfcgi_log(LOG_ALERT, "Unable to send last \r\n from headers !");
|
|
291
|
+ }
|
|
292
|
+ libpyfcgi.rep_sz += ret;
|
287
|
293
|
libpyfcgi.headers_sent = 1;
|
288
|
294
|
}
|
289
|
295
|
|
|
@@ -297,6 +303,7 @@ PyInit_libpyfcgi(void)
|
297
|
303
|
// init module & globals
|
298
|
304
|
libpyfcgi.status = NULL;
|
299
|
305
|
libpyfcgi.headers = NULL;
|
|
306
|
+ libpyfcgi.rep_sz = 0;
|
300
|
307
|
libpyfcgi.self = PyModule_Create(&pyfcgimodule);
|
301
|
308
|
if(libpyfcgi.self == NULL) { return NULL; }
|
302
|
309
|
return libpyfcgi.self;
|
|
@@ -336,10 +343,6 @@ PyObject* pyfcgi_start_response(PyObject* self, PyObject** argv, Py_ssize_t argc
|
336
|
343
|
exc_info */
|
337
|
344
|
}
|
338
|
345
|
|
339
|
|
-PyObject *repr = PyObject_ASCII(libpyfcgi.headers);
|
340
|
|
-pyfcgi_log(LOG_DEBUG, "start_response called, headers : '%s'", PyUnicode_AsUTF8(repr));
|
341
|
|
-Py_DECREF(repr);
|
342
|
|
-
|
343
|
346
|
return PyObject_GetAttrString(self, "write_body");
|
344
|
347
|
}
|
345
|
348
|
|
|
@@ -347,7 +350,6 @@ PyObject* pyfcgi_write_body(PyObject* self, PyObject** argv, Py_ssize_t argc)
|
347
|
350
|
{
|
348
|
351
|
char err[128];
|
349
|
352
|
|
350
|
|
-pyfcgi_log(LOG_DEBUG, "Write body called...");
|
351
|
353
|
if(argc != 1)
|
352
|
354
|
{
|
353
|
355
|
snprintf(err, 128,
|
|
@@ -366,6 +368,7 @@ PyObject* _pyfcgi_write_body(PyObject *body_data)
|
366
|
368
|
const char *dat;
|
367
|
369
|
PyObject *bytes, *cur, *iter, *repr;
|
368
|
370
|
Py_ssize_t sz;
|
|
371
|
+ int ret;
|
369
|
372
|
|
370
|
373
|
|
371
|
374
|
if(!libpyfcgi.out)
|
|
@@ -397,7 +400,6 @@ PyObject* _pyfcgi_write_body(PyObject *body_data)
|
397
|
400
|
{
|
398
|
401
|
Py_INCREF(iter);
|
399
|
402
|
cur = PyIter_Next(iter);
|
400
|
|
-pyfcgi_log(LOG_DEBUG, "Got iter for writing body...");
|
401
|
403
|
}
|
402
|
404
|
|
403
|
405
|
if(!cur)
|
|
@@ -409,7 +411,7 @@ pyfcgi_log(LOG_DEBUG, "Got iter for writing body...");
|
409
|
411
|
// if headers not sent yet, send them....
|
410
|
412
|
if(!libpyfcgi.headers_sent)
|
411
|
413
|
{
|
412
|
|
-pyfcgi_log(LOG_DEBUG, "Headers not sent... sending them...");
|
|
414
|
+ pyfcgi_log(LOG_DEBUG, "Headers not sent... sending them...");
|
413
|
415
|
libpyfcgi_send_headers();
|
414
|
416
|
}
|
415
|
417
|
|
|
@@ -422,13 +424,14 @@ pyfcgi_log(LOG_DEBUG, "Headers not sent... sending them...");
|
422
|
424
|
dat = PyUnicode_AsUTF8AndSize(cur, &sz);
|
423
|
425
|
if(!dat)
|
424
|
426
|
{
|
|
427
|
+ if(PyErr_Occurred()) { goto err_clean; }
|
425
|
428
|
repr = PyObject_ASCII(cur);
|
426
|
429
|
snprintf(err, 128,
|
427
|
430
|
"libpyfcgi.__write_body unable to encode string as UTF-8 : %s",
|
428
|
431
|
PyUnicode_AsUTF8(repr));
|
429
|
432
|
Py_DECREF(repr);
|
430
|
433
|
PyErr_SetString(PyExc_ValueError, err);
|
431
|
|
- Py_RETURN_NONE;
|
|
434
|
+ goto err_clean;
|
432
|
435
|
}
|
433
|
436
|
}
|
434
|
437
|
else if(PyBytes_Check(cur))
|
|
@@ -436,8 +439,12 @@ pyfcgi_log(LOG_DEBUG, "Headers not sent... sending them...");
|
436
|
439
|
dat = PyBytes_AsString(cur);
|
437
|
440
|
if(!dat)
|
438
|
441
|
{
|
439
|
|
- /**@todo if no expt set, set error str */
|
440
|
|
- Py_RETURN_NONE;
|
|
442
|
+ if(PyErr_Occurred()) { goto err_clean; }
|
|
443
|
+
|
|
444
|
+ snprintf(err, 128,
|
|
445
|
+ "Unable to get bytes buffer when sending body data");
|
|
446
|
+ PyErr_SetString(PyExc_ValueError, err);
|
|
447
|
+ goto err_clean;
|
441
|
448
|
}
|
442
|
449
|
sz = PyBytes_GET_SIZE(cur);
|
443
|
450
|
}
|
|
@@ -446,25 +453,36 @@ pyfcgi_log(LOG_DEBUG, "Headers not sent... sending them...");
|
446
|
453
|
bytes = PyObject_Bytes(cur);
|
447
|
454
|
if(!bytes)
|
448
|
455
|
{
|
|
456
|
+ if(PyErr_Occurred()) { goto err_clean; }
|
|
457
|
+
|
449
|
458
|
repr = PyObject_ASCII(cur);
|
450
|
459
|
snprintf(err, 128,
|
451
|
460
|
"libpyfcgi.__write_body expected str or bytes but %s given",
|
452
|
461
|
PyUnicode_AsUTF8(repr));
|
453
|
462
|
Py_DECREF(repr);
|
454
|
463
|
PyErr_SetString(PyExc_ValueError, err);
|
455
|
|
- Py_RETURN_NONE;
|
|
464
|
+ goto err_clean;
|
456
|
465
|
}
|
457
|
466
|
dat = PyBytes_AsString(cur);
|
458
|
467
|
if(!dat)
|
459
|
468
|
{
|
460
|
|
- /**@todo if no expt set, set error str */
|
461
|
|
- Py_RETURN_NONE;
|
|
469
|
+ if(PyErr_Occurred()) { goto err_clean; }
|
|
470
|
+
|
|
471
|
+ snprintf(err, 128,
|
|
472
|
+ "Unable to get bytes buffer when sending body data");
|
|
473
|
+ PyErr_SetString(PyExc_ValueError, err);
|
|
474
|
+ goto err_clean;
|
462
|
475
|
}
|
463
|
476
|
sz = PyBytes_GET_SIZE(cur);
|
464
|
477
|
}
|
465
|
478
|
|
466
|
|
-pyfcgi_log(LOG_DEBUG, "Sending '%s'", dat);
|
467
|
|
- FCGX_PutStr(dat, sz, libpyfcgi.out);
|
|
479
|
+ ret = FCGX_PutStr(dat, sz, libpyfcgi.out);
|
|
480
|
+ if(ret < 0)
|
|
481
|
+ {
|
|
482
|
+ pyfcgi_log(LOG_ALERT, "Unable to send reply");
|
|
483
|
+ goto err_clean;
|
|
484
|
+ }
|
|
485
|
+ libpyfcgi.rep_sz += ret;
|
468
|
486
|
|
469
|
487
|
if(bytes) { Py_DECREF(bytes); }
|
470
|
488
|
Py_DECREF(cur);
|
|
@@ -475,4 +493,10 @@ pyfcgi_log(LOG_DEBUG, "Sending '%s'", dat);
|
475
|
493
|
Py_DECREF(iter);
|
476
|
494
|
}
|
477
|
495
|
Py_RETURN_NONE;
|
|
496
|
+
|
|
497
|
+err_clean:
|
|
498
|
+ if(bytes) { Py_DECREF(bytes); }
|
|
499
|
+ Py_DECREF(cur);
|
|
500
|
+ if(iter) { Py_DECREF(iter); }
|
|
501
|
+ Py_RETURN_NONE;
|
478
|
502
|
}
|