fcgi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2015 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #include "php_ini.h"
  24. #include "ext/standard/info.h"
  25. #include "php_fcgi.h"
  26. #include <fcgi_stdio.h>
  27. static int le_fcgi;
  28. const zend_function_entry fcgi_functions[] = {
  29. PHP_FE(confirm_fcgi_compiled, NULL) /* For testing, remove later. */
  30. PHP_FE(fcgi_init, NULL)
  31. PHP_FE(fcgi_fini, NULL)
  32. PHP_FE(fcgi_accept, NULL)
  33. PHP_FE(fcgi_finish, NULL)
  34. PHP_FE(fcgi_getparam, NULL)
  35. PHP_FE(fcgi_echo, NULL)
  36. PHP_FE(fcgi_getcontent, NULL)
  37. PHP_FE_END /* Must be the last line in fcgi_functions[] */
  38. };
  39. zend_module_entry fcgi_module_entry = {
  40. #if ZEND_MODULE_API_NO >= 20010901
  41. STANDARD_MODULE_HEADER,
  42. #endif
  43. "fcgi",
  44. fcgi_functions,
  45. PHP_MINIT(fcgi),
  46. PHP_MSHUTDOWN(fcgi),
  47. PHP_RINIT(fcgi), /* Replace with NULL if there's nothing to do at request start */
  48. PHP_RSHUTDOWN(fcgi), /* Replace with NULL if there's nothing to do at request end */
  49. PHP_MINFO(fcgi),
  50. #if ZEND_MODULE_API_NO >= 20010901
  51. PHP_FCGI_VERSION,
  52. #endif
  53. STANDARD_MODULE_PROPERTIES
  54. };
  55. /* }}} */
  56. #ifdef COMPILE_DL_FCGI
  57. ZEND_GET_MODULE(fcgi)
  58. #endif
  59. PHP_MINIT_FUNCTION(fcgi)
  60. {
  61. /* If you have INI entries, uncomment these lines
  62. REGISTER_INI_ENTRIES();
  63. */
  64. return SUCCESS;
  65. }
  66. PHP_MSHUTDOWN_FUNCTION(fcgi)
  67. {
  68. /* uncomment this line if you have INI entries
  69. UNREGISTER_INI_ENTRIES();
  70. */
  71. return SUCCESS;
  72. }
  73. PHP_RINIT_FUNCTION(fcgi)
  74. {
  75. return SUCCESS;
  76. }
  77. PHP_RSHUTDOWN_FUNCTION(fcgi)
  78. {
  79. return SUCCESS;
  80. }
  81. PHP_MINFO_FUNCTION(fcgi)
  82. {
  83. php_info_print_table_start();
  84. php_info_print_table_header(2, "fcgi support", "enabled");
  85. php_info_print_table_end();
  86. }
  87. PHP_FUNCTION(confirm_fcgi_compiled)
  88. {
  89. char *arg = NULL;
  90. int arg_len, len;
  91. char *strg;
  92. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
  93. return;
  94. }
  95. len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "fcgi", arg);
  96. RETURN_STRINGL(strg, len, 0);
  97. }
  98. PHP_FUNCTION(fcgi_init)
  99. {
  100. }
  101. PHP_FUNCTION(fcgi_fini)
  102. {
  103. }
  104. static FCGX_Stream* stInstream;
  105. static FCGX_Stream* stOutstream;
  106. static FCGX_Stream* stErrstream;
  107. static FCGX_ParamArray stParams;
  108. PHP_FUNCTION(fcgi_accept)
  109. {
  110. int ret = FCGX_Accept(&stInstream,&stOutstream,&stErrstream,&stParams);
  111. if(stInstream == NULL) {
  112. RETVAL_LONG(-1);
  113. }
  114. if(stOutstream == NULL) {
  115. RETVAL_LONG(-1);
  116. }
  117. if(stErrstream == NULL) {
  118. RETVAL_LONG(-1);
  119. }
  120. RETVAL_LONG(ret);
  121. }
  122. PHP_FUNCTION(fcgi_getparam)
  123. {
  124. char *arg = NULL;
  125. int arg_len;
  126. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
  127. return;
  128. }
  129. const char* ret = FCGX_GetParam(arg, stParams);
  130. ret = (ret == NULL) ? "" : ret;
  131. char* result = estrdup(ret);
  132. RETURN_STRINGL(result,strlen(result),0);
  133. }
  134. static int content_length()
  135. {
  136. const char* pbuf = FCGX_GetParam("CONTENT_LENGTH", stParams);
  137. if(pbuf)
  138. {
  139. if(strlen(pbuf) == 0)
  140. return 0;
  141. else
  142. return atoi(pbuf);
  143. } else {
  144. return 0;
  145. }
  146. }
  147. PHP_FUNCTION(fcgi_getcontent)
  148. {
  149. int con_len = content_length();
  150. if(con_len) {
  151. char* pCon = (char*)malloc(con_len + 1);
  152. con_len = FCGX_GetStr(pCon, con_len, stInstream);
  153. pCon[con_len] = '\0';
  154. char* result = estrdup(pCon);
  155. RETURN_STRINGL(result,strlen(result),0);
  156. } else {
  157. char* result = estrdup("");
  158. RETURN_STRINGL(result,strlen(result),0);
  159. }
  160. }
  161. PHP_FUNCTION(fcgi_echo)
  162. {
  163. char *arg = NULL;
  164. int arg_len;
  165. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
  166. return;
  167. }
  168. if(arg != NULL) {
  169. FCGX_PutS(arg,stOutstream);
  170. }
  171. }
  172. PHP_FUNCTION(fcgi_finish)
  173. {
  174. FCGX_Finish();
  175. }