ソースを参照

Deleted loglines, updated README + add check on FCGX_PutStr calls

Yann Weber 5年前
コミット
de7269f833
4個のファイルの変更68行の追加40行の削除
  1. 14
    8
      README
  2. 2
    0
      include/python_pyfcgi.h
  3. 50
    26
      src/python_pyfcgi.c
  4. 2
    6
      src/pyworker.c

+ 14
- 8
README ファイルの表示

@@ -8,17 +8,23 @@ Building & running pyfcgi :
8 8
 	$ ./autogen.sh
9 9
 	$ ./configure
10 10
 	$ make
11
-
12
-	$ spawn-fcgi -d . -n -p 9000 -a 127.0.0.1 -- src/pyfcgi -S -e foo -E entrypoint -A
13
-or
11
+	# To run foo_pep333.entrypoint() PEP333 application
14 12
 	$ spawn-fcgi -d . -n -p 9000 -a 127.0.0.1 -- src/pyfcgi -S -e foo_pep333 -E entrypoint
13
+or
14
+	# To run foo.entrypoint() sending to FCGI python stdout
15
+	$ spawn-fcgi -d . -n -p 9000 -a 127.0.0.1 -- src/pyfcgi -S -e foo -E entrypoint -A
15 16
 
16
-logging to file example :
17
--------------------------
18
--L '/tmp/foo.log;0xff;{datetime} {msg} {ident}'
19 17
 
20
-Debugging :
21
------------
18
+configure script determine python flags, libs & includes paths using
19
+python3-config programm. The path can be specified using the
20
+PYTHON_CONFIG_PATH environment variable.
21
+
22
+Example : linking against a debug build of python :
23
+---------
22 24
 	$ ./configure PYTHON_CONFIG_PATH=/usr/bin/python3dm-config --enable-debug
23 25
 	$ make clean && make
24 26
 	$ valgrind --log-file=/tmp/val.log --trace-children=yes spawn-fcgi -d . -n -p 9000 -a 127.0.0.1 -- src/pyfcgi -S -e foo_pep333 -E entrypoint -L '/tmp/foo.log;0xff;{datetime} {msg} {ident}'
27
+
28
+logging to file example :
29
+-------------------------
30
+-L '/tmp/foo.log;0xff;{datetime} {msg} {ident}'

+ 2
- 0
include/python_pyfcgi.h ファイルの表示

@@ -72,6 +72,7 @@ struct libpyfcgi_context_s
72 72
 	size_t heads_buf_sz;
73 73
 	/**@brief Persistent buffer for PEP333 status */
74 74
 	char status_buf[LIBPYFCGI_STATUS_SZ];
75
+	size_t rep_sz;
75 76
 };
76 77
 
77 78
 typedef struct libpyfcgi_context_s libpyfcgi_context_t;
@@ -91,6 +92,7 @@ inline void libpyfcgi_clean_response()
91 92
 	if(libpyfcgi.headers) { Py_DECREF(libpyfcgi.headers); }
92 93
 	libpyfcgi.headers = NULL;
93 94
 	libpyfcgi.headers_sent = 0;
95
+	libpyfcgi.rep_sz = 0;
94 96
 }
95 97
 
96 98
 /**@brief Send headers stored in @ref libpyfcgi context

+ 50
- 26
src/python_pyfcgi.c ファイルの表示

@@ -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
 }

+ 2
- 6
src/pyworker.c ファイルの表示

@@ -85,14 +85,12 @@ int work333(int wrk_id, int semid)
85 85
 	{
86 86
 		gettimeofday(&start, NULL);
87 87
 		worker_set_busy(semid);
88
-pyfcgi_log(LOG_DEBUG, "Working !!");
89 88
 		count++;
90 89
 		environ = update_pyenv(py_osmod, envp);
91 90
 
92 91
 		libpyfcgi_clean_response();
93 92
 		libpyfcgi.out = out_stream;
94 93
 		args = Py_BuildValue("OO", environ, start_response);
95
-pyfcgi_log(LOG_DEBUG, "Calling entrypoint :D");
96 94
 		entry_ret = PyObject_CallObject(entry_fun, args);
97 95
 		Py_INCREF(entry_ret);
98 96
 
@@ -102,13 +100,11 @@ pyfcgi_log(LOG_DEBUG, "Calling entrypoint :D");
102 100
 		}
103 101
 		// able to process returned value
104 102
 		// Simulate python call of libpyfcgi.write_body()
105
-pyfcgi_log(LOG_DEBUG, "Writing body !");
106 103
 		_pyfcgi_write_body(entry_ret);
107 104
 		if(PyErr_Occurred())
108 105
 		{
109 106
 			log_expt(LOG_ERR);
110 107
 		}
111
-pyfcgi_log(LOG_DEBUG, "Cleaning...");
112 108
 
113 109
 		// clean stuffs
114 110
 		Py_DECREF(args);
@@ -131,8 +127,8 @@ pyfcgi_log(LOG_DEBUG, "Cleaning...");
131 127
 			stop.tv_usec += 1000000;
132 128
 			stop.tv_sec -= 1;
133 129
 		}
134
-		pyfcgi_log(LOG_DEBUG, "Worker[%d] request %d END [OK] in %ld.%06lds",
135
-			wrk_id, count, stop.tv_sec, stop.tv_usec);
130
+		pyfcgi_log(LOG_DEBUG, "Worker[%d] request %d END [OK] %lu bytes in %ld.%06lds",
131
+			wrk_id, count, libpyfcgi.rep_sz, stop.tv_sec, stop.tv_usec);
136 132
 	}
137 133
 	return 0;
138 134
 }

読み込み中…
キャンセル
保存