Tests about a simple python3 fastcgi runner using libfcgi and the Python-C API.
python
c
wsgi
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

python_ioin.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. #include "python_ioin.h"
  2. /**@file python_ioin.c
  3. * @ingroup lib_ioin
  4. */
  5. /**@brief If given object io_stream is NULL set error indicator
  6. * @param self The IoIn instance
  7. * @return 0 if no error else -1 or -2
  8. */
  9. static int _check_nullin(PyObject *self);
  10. /**@brief Request a new buffer size.
  11. *
  12. * @param self The IoIn instance
  13. * @param nreq number of bytes required in buffer
  14. * @return 0 on error else buffer size
  15. */
  16. static int pyfcgi_io__reqbuf(PyObject *self, int nreq);
  17. /**@brief Concat two bytes/unicode given IoIn configuration flag
  18. */
  19. static PyObject* IoIn__Concat(PyObject *self, PyObject *left, PyObject *right);
  20. PyObject* pyfcgi_io_true(PyObject *self, PyObject **argv, Py_ssize_t argc);
  21. PyObject* pyfcgi_io_false(PyObject *self, PyObject **argv, Py_ssize_t argc);
  22. PyMethodDef IoIn_methods[] = {
  23. {"close", (PyCFunction)pyfcgi_io_close, METH_FASTCALL, NULL},
  24. {"fileno", (PyCFunction)pyfcgi_io_fileno, METH_FASTCALL, NULL},
  25. {"flush", (PyCFunction)pyfcgi_io_flush, METH_FASTCALL, NULL},
  26. {"isatty", (PyCFunction)pyfcgi_io_isatty, METH_FASTCALL, NULL},
  27. {"readable", (PyCFunction)pyfcgi_ioin_readable, METH_FASTCALL, NULL},
  28. {"readline", (PyCFunction)pyfcgi_ioin_readline, METH_FASTCALL, NULL},
  29. {"readlines", (PyCFunction)pyfcgi_ioin_readlines, METH_FASTCALL, NULL},
  30. {"seek", (PyCFunction)pyfcgi_io_seek, METH_FASTCALL, NULL},
  31. {"seekable", (PyCFunction)pyfcgi_io_seekable, METH_FASTCALL, NULL},
  32. {"tell", (PyCFunction)pyfcgi_io_tell, METH_FASTCALL, NULL},
  33. {"truncate", (PyCFunction)pyfcgi_io_truncate, METH_FASTCALL, NULL},
  34. {"writable", (PyCFunction)pyfcgi_ioin_writable, METH_FASTCALL, NULL},
  35. {"writelines", (PyCFunction)pyfcgi_ioin_writelines, METH_FASTCALL, NULL},
  36. {"read", (PyCFunction)pyfcgi_ioin_read, METH_FASTCALL, NULL},
  37. {"readall", (PyCFunction)pyfcgi_ioin_readall, METH_FASTCALL, NULL},
  38. {"readinto", (PyCFunction)pyfcgi_ioin_readinto, METH_FASTCALL, NULL},
  39. {"write", (PyCFunction)pyfcgi_ioin_write, METH_FASTCALL, NULL},
  40. {NULL} //Sentinel
  41. };
  42. PyMemberDef IoIn_members[] = {
  43. {"closed", T_OBJECT, offsetof(PyIO_t, closed), READONLY, "True if the stream is closed"},
  44. {NULL}
  45. };
  46. PyTypeObject IoInType = {
  47. PyVarObject_HEAD_INIT(NULL, 0)
  48. "libpyfcgi.IoIn", /* tp_name */
  49. sizeof(PyIO_t), /* tp_basicsize */
  50. 0, /* tp_itemsize */
  51. (destructor)pyfcgi_io_del, /* tp_dealloc */
  52. 0, /* tp_print */
  53. 0, /* tp_getattr */
  54. 0, /* tp_setattr */
  55. 0, /* tp_reserved */
  56. 0, /* tp_repr */
  57. 0, /* tp_as_number */
  58. 0, /* tp_as_sequence */
  59. 0, /* tp_as_mapping */
  60. 0, /* tp_hash */
  61. 0, /* tp_call */
  62. 0, /* tp_str */
  63. 0, /* tp_getattro */
  64. 0, /* tp_setattro */
  65. 0, /* tp_as_buffer */
  66. Py_TPFLAGS_DEFAULT |
  67. Py_TPFLAGS_BASETYPE, /* tp_flags */
  68. "RawIo interface to FCGI input stream", /* tp_doc */
  69. 0, /* tp_traverse */
  70. 0, /* tp_clear */
  71. 0, /* tp_richcompare */
  72. 0, /* tp_weaklistoffset */
  73. 0, /* tp_iter */
  74. 0, /* tp_iternext */
  75. IoIn_methods, /* tp_methods */
  76. IoIn_members, /* tp_members */
  77. 0, /* tp_getset */
  78. 0, /* tp_base */
  79. 0, /* tp_dict */
  80. 0, /* tp_descr_get */
  81. 0, /* tp_descr_set */
  82. 0, /* tp_dictoffset */
  83. pyfcgi_ioin_init, /* tp_init */
  84. 0, /* tp_alloc */
  85. 0, /* tp_new */
  86. };
  87. PyMethodDef IoOut_methods[] = {
  88. {"close", (PyCFunction)pyfcgi_io_close, METH_FASTCALL, NULL},
  89. {"fileno", (PyCFunction)pyfcgi_io_fileno, METH_FASTCALL, NULL},
  90. {"flush", (PyCFunction)pyfcgi_io_flush, METH_FASTCALL, NULL},
  91. {"isatty", (PyCFunction)pyfcgi_io_isatty, METH_FASTCALL, NULL},
  92. {"readable", (PyCFunction)pyfcgi_ioout_readable, METH_FASTCALL, NULL},
  93. {"readline", (PyCFunction)pyfcgi_ioout_readline, METH_FASTCALL, NULL},
  94. {"readlines", (PyCFunction)pyfcgi_ioout_readlines, METH_FASTCALL, NULL},
  95. {"seek", (PyCFunction)pyfcgi_io_seek, METH_FASTCALL, NULL},
  96. {"seekable", (PyCFunction)pyfcgi_io_seekable, METH_FASTCALL, NULL},
  97. {"tell", (PyCFunction)pyfcgi_io_tell, METH_FASTCALL, NULL},
  98. {"truncate", (PyCFunction)pyfcgi_io_truncate, METH_FASTCALL, NULL},
  99. {"writable", (PyCFunction)pyfcgi_ioout_writable, METH_FASTCALL, NULL},
  100. {"writelines", (PyCFunction)pyfcgi_ioout_writelines, METH_FASTCALL, NULL},
  101. {"read", (PyCFunction)pyfcgi_ioout_read, METH_FASTCALL, NULL},
  102. {"readall", (PyCFunction)pyfcgi_ioout_readall, METH_FASTCALL, NULL},
  103. {"readinto", (PyCFunction)pyfcgi_ioout_readinto, METH_FASTCALL, NULL},
  104. {"write", (PyCFunction)pyfcgi_ioout_write, METH_FASTCALL, NULL},
  105. {NULL} //Sentinel
  106. };
  107. PyMemberDef IoOut_members[] = {
  108. {"closed", T_OBJECT, offsetof(PyIO_t, closed), READONLY, "True if the stream is closed"},
  109. {NULL}
  110. };
  111. PyTypeObject IoOutType = {
  112. PyVarObject_HEAD_INIT(NULL, 0)
  113. "libpyfcgi.IoOut", /* tp_name */
  114. sizeof(PyIO_t), /* tp_basicsize */
  115. 0, /* tp_itemsize */
  116. (destructor)pyfcgi_io_del, /* tp_dealloc */
  117. 0, /* tp_print */
  118. 0, /* tp_getattr */
  119. 0, /* tp_setattr */
  120. 0, /* tp_reserved */
  121. 0, /* tp_repr */
  122. 0, /* tp_as_number */
  123. 0, /* tp_as_sequence */
  124. 0, /* tp_as_mapping */
  125. 0, /* tp_hash */
  126. 0, /* tp_call */
  127. 0, /* tp_str */
  128. 0, /* tp_getattro */
  129. 0, /* tp_setattro */
  130. 0, /* tp_as_buffer */
  131. Py_TPFLAGS_DEFAULT |
  132. Py_TPFLAGS_BASETYPE, /* tp_flags */
  133. "RawIo interface to FCGI input stream", /* tp_doc */
  134. 0, /* tp_traverse */
  135. 0, /* tp_clear */
  136. 0, /* tp_richcompare */
  137. 0, /* tp_weaklistoffset */
  138. 0, /* tp_iter */
  139. 0, /* tp_iternext */
  140. IoOut_methods, /* tp_methods */
  141. IoOut_members, /* tp_members */
  142. 0, /* tp_getset */
  143. 0, /* tp_base */
  144. 0, /* tp_dict */
  145. 0, /* tp_descr_get */
  146. 0, /* tp_descr_set */
  147. 0, /* tp_dictoffset */
  148. pyfcgi_ioout_init, /* tp_init */
  149. 0, /* tp_alloc */
  150. 0, /* tp_new */
  151. };
  152. int pyfcgi_io_init(PyObject *self)
  153. {
  154. PyIO_t *ioin = (void*)self;
  155. ioin->io_stream = NULL;
  156. ioin->buff = NULL;
  157. ioin->buff_sz = 0;
  158. ioin->eof=0;
  159. ioin->bin=1;
  160. ioin->write = NULL;
  161. return 0;
  162. }
  163. int pyfcgi_ioin_init(PyObject *self, PyObject *args, PyObject *kwds)
  164. {
  165. pyfcgi_io_init(self);
  166. return 0;
  167. }
  168. int pyfcgi_ioout_init(PyObject *self, PyObject *args, PyObject *kwds)
  169. {
  170. pyfcgi_io_init(self);
  171. return 0;
  172. }
  173. void pyfcgi_io_del(PyIO_t *self)
  174. {
  175. PyIO_t *ioin = self;
  176. if(ioin->buff) { free(ioin->buff); }
  177. Py_TYPE(self)->tp_free((PyObject*)self);
  178. }
  179. PyObject* pyfcgi_io_close(PyObject *self, PyObject **argv, Py_ssize_t argc)
  180. {
  181. if(_check_nullin(self)) { Py_RETURN_NONE; }
  182. if(argc)
  183. {
  184. PyErr_Format(PyExc_ValueError,
  185. "libpyfcgi.IoIn.close() not expecting any argument but %zd given",
  186. argc);
  187. Py_RETURN_NONE;
  188. }
  189. if(FCGX_FClose(*((PyIO_t*)self)->io_stream) < 0)
  190. {
  191. PyErr_Format(PyExc_OSError,
  192. "%A unable to close", self);
  193. }
  194. ((PyIO_t*)self)->closed = Py_True;
  195. Py_RETURN_NONE;
  196. }
  197. PyObject* pyfcgi_io_fileno(PyObject *self, PyObject **argv, Py_ssize_t argc)
  198. {
  199. PyErr_SetString(PyExc_OSError, "libpyfcgi.IoIn has no fileno");
  200. Py_RETURN_NONE;
  201. }
  202. PyObject* pyfcgi_io_flush(PyObject *self, PyObject **argv, Py_ssize_t argc)
  203. {
  204. if(_check_nullin(self)) { Py_RETURN_NONE; }
  205. if(argc)
  206. {
  207. PyErr_Format(PyExc_ValueError,
  208. "libpyfcgi.IoIn.close() not expecting any argument but %zd given",
  209. argc);
  210. Py_RETURN_NONE;
  211. }
  212. if(FCGX_FFlush(*((PyIO_t*)self)->io_stream) < 0)
  213. {
  214. PyErr_Format(PyExc_OSError,
  215. "%A unable to flush", self);
  216. }
  217. Py_RETURN_NONE;
  218. }
  219. PyObject* pyfcgi_ioin_readline(PyObject *self, PyObject **argv, Py_ssize_t argc)
  220. {
  221. int read_n;
  222. long arg;
  223. char *ret;
  224. if(_check_nullin(self)) { Py_RETURN_NONE; }
  225. if(argc > 1)
  226. {
  227. PyErr_Format(PyExc_ValueError,
  228. "libpyfcgi.IoIn.close() expecting 0 or 1 argument but %zd given",
  229. argc);
  230. Py_RETURN_NONE;
  231. }
  232. if(!argc || !argv[0])
  233. {
  234. read_n = pyfcgi_io__reqbuf(self, 0);
  235. }
  236. else
  237. {
  238. arg = PyLong_AsLong(argv[0]);
  239. if(PyErr_Occurred())
  240. {
  241. Py_RETURN_NONE;
  242. }
  243. read_n = pyfcgi_io__reqbuf(self, (arg>INT_MAX)?INT_MAX:arg);
  244. }
  245. ret = ((PyIO_t*)self)->buff;
  246. if(!FCGX_GetLine(ret, read_n, *((PyIO_t*)self)->io_stream))
  247. {
  248. ((PyIO_t*)self)->eof = 1;
  249. ret = "";
  250. }
  251. //dprintf(2, "readline : '%s'\n", ret);
  252. return IoIn__FromString(self, ret);
  253. }
  254. PyObject* pyfcgi_ioin_readlines(PyObject *self, PyObject **argv, Py_ssize_t argc)
  255. {
  256. size_t hint, left;
  257. int read_n, toread;
  258. size_t sz;
  259. PyObject *res, *cur_str, *read_str, *tmp;
  260. char *buff;
  261. if(_check_nullin(self)) { Py_RETURN_NONE; }
  262. if(argc > 1)
  263. {
  264. PyErr_Format(PyExc_ValueError,
  265. "libpyfcgi.IoIn.close() expecting 0 or 1 argument but %zd given",
  266. argc);
  267. Py_RETURN_NONE;
  268. }
  269. if(!argc || !argv[0])
  270. {
  271. hint = 0;
  272. left = 0;
  273. }
  274. else
  275. {
  276. hint = PyLong_AsSize_t(argv[0]);
  277. if(PyErr_Occurred())
  278. {
  279. Py_RETURN_NONE;
  280. }
  281. left = hint;
  282. }
  283. res = PyList_New(0);
  284. if(!res)
  285. {
  286. Py_RETURN_NONE;
  287. }
  288. Py_INCREF(res);
  289. read_n = pyfcgi_io__reqbuf(self, 0);
  290. buff = ((PyIO_t*)self)->buff;
  291. cur_str = NULL;
  292. while((hint && left) || !hint)
  293. {
  294. toread = (hint&&((size_t)read_n > left))?(int)left:read_n;
  295. if(!FCGX_GetLine(buff, toread,
  296. *((PyIO_t*)self)->io_stream))
  297. {
  298. ((PyIO_t*)self)->eof = 1;
  299. break;
  300. }
  301. sz = strlen(buff);
  302. left -= sz;
  303. read_str = IoIn__FromBuff(self);
  304. if(!read_str)
  305. {
  306. Py_RETURN_NONE;
  307. }
  308. Py_INCREF(read_str);
  309. if(cur_str)
  310. {
  311. tmp = IoIn__Concat(self, cur_str, read_str);
  312. Py_INCREF(tmp);
  313. cur_str = tmp;
  314. }
  315. else
  316. {
  317. cur_str = read_str;
  318. }
  319. if(buff[sz-1] == '\n')
  320. {
  321. //dprintf(2, "readlines : '%s'\n", PyUnicode_AsUTF8(cur_str));
  322. if(PyList_Append(res, cur_str))
  323. {
  324. Py_DECREF(cur_str);
  325. Py_RETURN_NONE;
  326. }
  327. Py_DECREF(cur_str);
  328. cur_str = NULL;
  329. }
  330. }
  331. if(cur_str)
  332. {
  333. //dprintf(2, "readlines(post) : '%s'\n", PyUnicode_AsUTF8(cur_str));
  334. if(PyList_Append(res, cur_str))
  335. {
  336. Py_DECREF(cur_str);
  337. Py_RETURN_NONE;
  338. }
  339. Py_DECREF(cur_str);
  340. }
  341. Py_DECREF(res);
  342. return res;
  343. }
  344. PyObject* pyfcgi_ioin_read(PyObject *self, PyObject **argv, Py_ssize_t argc)
  345. {
  346. size_t left, max;
  347. long l;
  348. int read_n, toread, sz;
  349. PyObject *res, *read_str;
  350. char *buff;
  351. if(_check_nullin(self)) { Py_RETURN_NONE; }
  352. if(argc > 1)
  353. {
  354. PyErr_Format(PyExc_ValueError,
  355. "libpyfcgi.IoIn.read() expecting at most 1 argument but %zd given",
  356. argc);
  357. Py_RETURN_NONE;
  358. }
  359. if(((PyIO_t*)self)->eof)
  360. {
  361. return IoIn__FromString(self, "");
  362. }
  363. max = 0;
  364. if(argc)
  365. {
  366. l = PyLong_AsLong(argv[0]);
  367. if(PyErr_Occurred() || l > 0)
  368. {
  369. PyErr_Clear();
  370. max = PyLong_AsSize_t(argv[0]);
  371. if(PyErr_Occurred())
  372. {
  373. Py_RETURN_NONE;
  374. }
  375. }
  376. }
  377. left = max;
  378. read_n = pyfcgi_io__reqbuf(self, 0);
  379. buff = ((PyIO_t*)self)->buff;
  380. if(!(res = IoIn__FromString(self, "")))
  381. {
  382. Py_RETURN_NONE;
  383. }
  384. Py_INCREF(res);
  385. while((max && left) || !max)
  386. {
  387. toread = (max&&((size_t)read_n > left))?(int)left:read_n;
  388. sz = FCGX_GetStr(buff, toread, *((PyIO_t*)self)->io_stream);
  389. if(sz)
  390. {
  391. if(sz == toread) { buff[sz] = '\0'; }
  392. left -= sz;
  393. if(!(read_str = IoIn__FromBuff(self)))
  394. {
  395. Py_RETURN_NONE;
  396. }
  397. Py_INCREF(read_str);
  398. if(!(res = IoIn__Concat(self, res, read_str)))
  399. {
  400. Py_RETURN_NONE;
  401. }
  402. Py_INCREF(res);
  403. }
  404. if( sz < toread)
  405. {
  406. ((PyIO_t*)self)->eof = 1;
  407. break;
  408. }
  409. }
  410. //dprintf(2, "read : %s\n", PyBytes_AsString(res));
  411. Py_DECREF(res);
  412. return res;
  413. }
  414. PyObject* pyfcgi_ioin_readall(PyObject *self, PyObject **argv, Py_ssize_t argc)
  415. {
  416. if(_check_nullin(self)) { Py_RETURN_NONE; }
  417. if(argc)
  418. {
  419. PyErr_Format(PyExc_ValueError,
  420. "libpyfcgi.IoIn.readall() not expecting any argument but %zd given",
  421. argc);
  422. Py_RETURN_NONE;
  423. }
  424. dprintf(2, "readall calling read : ");
  425. return pyfcgi_ioin_read(self, NULL, 0);
  426. }
  427. PyObject* pyfcgi_ioin_readinto(PyObject *self, PyObject **argv, Py_ssize_t argc)
  428. {
  429. PyObject *b;
  430. Py_ssize_t max, left, read_n;
  431. char *buff, *buff_ptr;
  432. int toread, ret;
  433. if(_check_nullin(self)) { Py_RETURN_NONE; }
  434. if(argc != 1)
  435. {
  436. PyErr_Format(PyExc_ValueError,
  437. "libpyfcgi.IoIn.readinto() expecting 1 argument but %zd given",
  438. argc);
  439. Py_RETURN_NONE;
  440. }
  441. b = argv[0];
  442. if(!PyByteArray_Check(b))
  443. {
  444. PyErr_Format(PyExc_ValueError,
  445. "libpyfcgi.IoIn.readinto() expected bytearray as argument but %A given",
  446. b);
  447. Py_RETURN_NONE;
  448. }
  449. left = max = PyByteArray_Size(b);
  450. buff = PyByteArray_AsString(b);
  451. buff_ptr = buff;
  452. read_n = pyfcgi_io__reqbuf(self, 0);
  453. while(left)
  454. {
  455. toread = left>read_n?read_n:left;
  456. if((ret = FCGX_GetStr(buff_ptr, toread,
  457. *((PyIO_t*)self)->io_stream)) < toread)
  458. {
  459. ((PyIO_t*)self)->eof = 1;
  460. break;
  461. }
  462. buff_ptr += ret;
  463. left -= ret;
  464. }
  465. //dprintf(2, "readinto chr buff : '%s'\n", buff);
  466. //dprintf(2, "readinto bytes repr : '%s'\n", PyByteArray_AsString(b));
  467. return b;
  468. }
  469. PyObject* pyfcgi_ioout_writelines(PyObject *self, PyObject **argv, Py_ssize_t argc)
  470. {
  471. PyObject *lines, *iter, *line;
  472. const char *bytes;
  473. Py_ssize_t bytes_len, lineno;
  474. if( ! *((PyIO_t*)self)->write)
  475. {
  476. PyErr_Format(PyExc_RuntimeError, "%A write function not set",
  477. self);
  478. Py_RETURN_NONE;
  479. }
  480. if(argc != 1)
  481. {
  482. PyErr_Format(PyExc_ValueError,
  483. "libpyfcgi.IoOut.writelines() expected 1 argument but %zd given",
  484. argc);
  485. Py_RETURN_NONE;
  486. }
  487. lines = argv[0];
  488. iter = PyObject_GetIter(lines);
  489. if(!iter)
  490. {
  491. // should drop exception raised by GetIter ??
  492. PyErr_Format(PyExc_ValueError,
  493. "libpyfcgi.IoOut.writelines() first argument does not support iterator protocol : %A",
  494. lines);
  495. Py_RETURN_NONE;
  496. }
  497. lineno = 0;
  498. Py_INCREF(iter);
  499. while( (line = PyIter_Next(lines)) )
  500. {
  501. lineno++;
  502. Py_INCREF(line);
  503. if(PyUnicode_Check(line))
  504. {
  505. bytes = PyUnicode_AsUTF8AndSize(line, &bytes_len);
  506. if(!bytes) { Py_RETURN_NONE; } //forward error
  507. }
  508. else if(PyBytes_Check(line))
  509. {
  510. if(PyBytes_AsStringAndSize(line, (char**)&bytes, &bytes_len) == -1)
  511. {
  512. Py_RETURN_NONE; //forward error
  513. }
  514. }
  515. else
  516. {
  517. PyErr_Format(PyExc_TypeError,
  518. "libpyfcgi.IoOut.writelines() expected argument to be\
  519. str or bytes but %A given on line %zd",
  520. line, lineno);
  521. Py_RETURN_NONE;
  522. }
  523. if( ((PyIO_t*)self)->write(bytes, bytes_len) == -1)
  524. {
  525. PyErr_Format(PyExc_EOFError,
  526. "libpyfcgi.IoOut.writelines() EOF error when writing line %zd",
  527. lineno);
  528. Py_RETURN_NONE;
  529. }
  530. Py_DECREF(line);
  531. }
  532. Py_DECREF(iter);
  533. Py_RETURN_NONE;
  534. }
  535. PyObject* pyfcgi_ioout_write(PyObject *self, PyObject **argv, Py_ssize_t argc)
  536. {
  537. PyObject *b;
  538. const char *bytes;
  539. Py_ssize_t bytes_len;
  540. if( ! *((PyIO_t*)self)->write)
  541. {
  542. PyErr_Format(PyExc_RuntimeError, "%A write function not set",
  543. self);
  544. Py_RETURN_NONE;
  545. }
  546. if(argc != 1)
  547. {
  548. PyErr_Format(PyExc_ValueError,
  549. "libpyfcgi.IoOut.writelines() expected 1 argument but %zd given",
  550. argc);
  551. Py_RETURN_NONE;
  552. }
  553. b = argv[0];
  554. if(PyBytes_Check(b))
  555. {
  556. if(PyBytes_AsStringAndSize(b, (char**)&bytes, &bytes_len) == -1)
  557. {
  558. Py_RETURN_NONE; //forward error
  559. }
  560. }
  561. else if(PyUnicode_Check(b))
  562. {
  563. bytes = PyUnicode_AsUTF8AndSize(b, &bytes_len);
  564. if(!bytes) { Py_RETURN_NONE; } //forward error
  565. }
  566. else
  567. {
  568. PyErr_Format(PyExc_TypeError,
  569. "libpyfcgi.IoOut.write() expected argument to be bytes\
  570. or str but %A given on line %zd",
  571. b);
  572. Py_RETURN_NONE;
  573. }
  574. if( ((PyIO_t*)self)->write(bytes, bytes_len) == -1)
  575. {
  576. PyErr_Format(PyExc_EOFError,
  577. "libpyfcgi.IoOut.writelines() EOF error when calling write");
  578. Py_RETURN_NONE;
  579. }
  580. Py_RETURN_NONE;
  581. }
  582. PyObject* pyfcgi_io_false(PyObject *self, PyObject **argv, Py_ssize_t argc)
  583. {
  584. return Py_False;
  585. }
  586. PyObject* pyfcgi_io_true(PyObject *self, PyObject **argv, Py_ssize_t argc)
  587. {
  588. return Py_True;
  589. }
  590. PyObject* pyfcgi_io_truncate(PyObject *self, PyObject **argv, Py_ssize_t argc)
  591. {
  592. PyErr_SetString(PyExc_OSError, "libpyfcgi.Io cannot be truncated");
  593. Py_RETURN_NONE;
  594. }
  595. PyObject* pyfcgi_io_SeekError(PyObject *self, PyObject **argv, Py_ssize_t argc)
  596. {
  597. PyErr_SetString(PyExc_OSError, "libpyfcgi.Io is not seekable");
  598. Py_RETURN_NONE;
  599. }
  600. PyObject* pyfcgi_io_WriteError(PyObject *self, PyObject **argv, Py_ssize_t argc)
  601. {
  602. PyErr_SetString(PyExc_OSError, "libpyfcgi.IoIn is not writable");
  603. Py_RETURN_NONE;
  604. }
  605. PyObject* pyfcgi_io_ReadError(PyObject *self, PyObject **argv, Py_ssize_t argc)
  606. {
  607. PyErr_SetString(PyExc_OSError, "libpyfcgi.IoOut is not readable");
  608. Py_RETURN_NONE;
  609. }
  610. static int _check_nullin(PyObject *self)
  611. {
  612. if(!((PyIO_t*)self)->io_stream)
  613. {
  614. PyErr_SetString(PyExc_RuntimeError,
  615. "pyfcgi.IoIn called in wrong context : FGCI input not set");
  616. return -1;
  617. }
  618. else if(!(*(((PyIO_t*)self)->io_stream)))
  619. {
  620. PyErr_SetString(PyExc_RuntimeError,
  621. "pyfcgi.IoIn called in wrong context : FGCI input is NULL");
  622. return -2;
  623. }
  624. return 0;
  625. }
  626. static int pyfcgi_io__reqbuf(PyObject *self, int nreq)
  627. {
  628. PyIO_t *ioin;
  629. void *tmp;
  630. int err;
  631. ioin = (PyIO_t*)self;
  632. if(ioin->buff_sz > nreq && ioin->buff)
  633. {
  634. return ioin->buff_sz;
  635. }
  636. ioin->buff_sz = ((nreq>>12)+1)<<12;
  637. if(ioin->buff_sz < nreq) { ioin->buff_sz = nreq; }
  638. if(!(tmp = realloc(ioin->buff, ioin->buff_sz)))
  639. {
  640. err = errno;
  641. PyErr_Format(PyExc_OSError,
  642. "%A error reallocating internal buffer : %s",
  643. self, strerror(err));
  644. errno = err;
  645. return 0;
  646. }
  647. ioin->buff = tmp;
  648. return ioin->buff_sz;
  649. }
  650. /**@brief Concat two bytes/unicode given IoIn configuration flag
  651. * @warning Py_DECREF both arguments
  652. */
  653. static PyObject* IoIn__Concat(PyObject *self, PyObject *left, PyObject *right)
  654. {
  655. if(((PyIO_t*)self)->bin)
  656. {
  657. PyBytes_Concat(&left, right);
  658. Py_DECREF(right);
  659. return left;
  660. }
  661. PyObject *res;
  662. res = PyUnicode_Concat(left, right);
  663. Py_DECREF(left);
  664. Py_DECREF(right);
  665. return res;
  666. }