From eebc6c90bbac577b852b192a3627158727f06189 Mon Sep 17 00:00:00 2001 From: "Matteo Nastasi (mop)" Date: Thu, 28 Jun 2012 18:07:06 +0200 Subject: [PATCH] headers via ctrl_socket and a php variable --- php-ancillary.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/php-ancillary.c b/php-ancillary.c index 4907812..ea64de4 100644 --- a/php-ancillary.c +++ b/php-ancillary.c @@ -43,20 +43,23 @@ zend_module_entry ancillary_module_entry = { }; ZEND_GET_MODULE(ancillary) + +#define CTRL_BUFF_MAX_SZ (8*1024) // starting from a unix socket receive a socket from an external process PHP_FUNCTION(ancillary_getstream) { zval *zstream; php_stream *stream; - int fd_in, fd_out[2]; + int fd_in, fd_out[2], retrecv, curpos = 0; char *headers; + zval *zheaders; - if ((headers = calloc(8*1024, 1)) == NULL) { + if ((headers = calloc(CTRL_BUFF_MAX_SZ, 1)) == NULL) { return; } - if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream)) + if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &zstream, &zheaders)) return; php_stream_from_zval(stream, &zstream); @@ -68,17 +71,43 @@ PHP_FUNCTION(ancillary_getstream) if(ancil_recv_fds(fd_in, fd_out, 2) == 0) { RETURN_FALSE; } + { char bf[2048]; sprintf(bf, "zero: [%d] uno: [%d]\n", fd_out[0], fd_out[1]); write(1, bf, strlen(bf)); } + while(1) { + write(1, "LOOP\n", 5); + retrecv = recv(fd_out[1], &(headers[curpos]), CTRL_BUFF_MAX_SZ - curpos - 1, 0); + if (retrecv < 0) { + RETURN_FALSE; + } + else if (retrecv == 0) { + break; + } + else { + char bf[1024]; + curpos += retrecv; + sprintf(bf, "CURPOS: %d\n", curpos); + write(1, bf, strlen(bf)); + if (curpos == CTRL_BUFF_MAX_SZ - 1) { + free(headers); + RETURN_FALSE; + } + } + } + shutdown(fd_out[1], SHUT_RDWR); + + ZVAL_STRING(zheaders, headers, 1); + free(headers); + { int fh; if ((fh = open("/tmp/out_php-anc.txt", O_WRONLY | O_CREAT | O_APPEND)) > -1) { - // write(fh, headers, 8*1024); + write(fh, headers, curpos); close(fh); } } -- 2.17.1