fcgi.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_END /* Must be the last line in fcgi_functions[] */
  36. };
  37. zend_module_entry fcgi_module_entry = {
  38. #if ZEND_MODULE_API_NO >= 20010901
  39. STANDARD_MODULE_HEADER,
  40. #endif
  41. "fcgi",
  42. fcgi_functions,
  43. PHP_MINIT(fcgi),
  44. PHP_MSHUTDOWN(fcgi),
  45. PHP_RINIT(fcgi), /* Replace with NULL if there's nothing to do at request start */
  46. PHP_RSHUTDOWN(fcgi), /* Replace with NULL if there's nothing to do at request end */
  47. PHP_MINFO(fcgi),
  48. #if ZEND_MODULE_API_NO >= 20010901
  49. PHP_FCGI_VERSION,
  50. #endif
  51. STANDARD_MODULE_PROPERTIES
  52. };
  53. /* }}} */
  54. #ifdef COMPILE_DL_FCGI
  55. ZEND_GET_MODULE(fcgi)
  56. #endif
  57. PHP_MINIT_FUNCTION(fcgi)
  58. {
  59. /* If you have INI entries, uncomment these lines
  60. REGISTER_INI_ENTRIES();
  61. */
  62. return SUCCESS;
  63. }
  64. PHP_MSHUTDOWN_FUNCTION(fcgi)
  65. {
  66. /* uncomment this line if you have INI entries
  67. UNREGISTER_INI_ENTRIES();
  68. */
  69. return SUCCESS;
  70. }
  71. PHP_RINIT_FUNCTION(fcgi)
  72. {
  73. return SUCCESS;
  74. }
  75. PHP_RSHUTDOWN_FUNCTION(fcgi)
  76. {
  77. return SUCCESS;
  78. }
  79. PHP_MINFO_FUNCTION(fcgi)
  80. {
  81. php_info_print_table_start();
  82. php_info_print_table_header(2, "fcgi support", "enabled");
  83. php_info_print_table_end();
  84. }
  85. PHP_FUNCTION(confirm_fcgi_compiled)
  86. {
  87. char *arg = NULL;
  88. int arg_len, len;
  89. char *strg;
  90. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
  91. return;
  92. }
  93. len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "fcgi", arg);
  94. RETURN_STRINGL(strg, len, 0);
  95. }
  96. extern void swap_init();
  97. extern void fcgi_log(const char* szBuf);
  98. extern void swap_fini();
  99. extern void start_accept();
  100. extern void fini_accept(void (* pFun)(char*) );
  101. PHP_FUNCTION(fcgi_init)
  102. {
  103. char *arg = NULL;
  104. int arg_len;
  105. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
  106. return;
  107. }
  108. swap_init(arg);
  109. }
  110. PHP_FUNCTION(fcgi_fini)
  111. {
  112. swap_fini();
  113. }
  114. static FCGX_Stream* stInstream;
  115. static FCGX_Stream* stOutstream;
  116. static FCGX_Stream* stErrstream;
  117. static FCGX_ParamArray stParams;
  118. PHP_FUNCTION(fcgi_accept)
  119. {
  120. fcgi_log("\r\n\r\nstart fcgi_accept:\r\n");
  121. start_accept();
  122. int ret = FCGX_Accept(&stInstream,&stOutstream,&stErrstream,&stParams);
  123. printf("Content-Type: text/html; charset=UTF-8\r\n\r\n");
  124. if(stInstream == NULL) {
  125. fcgi_log("Error: in stream = null \r\n");
  126. }
  127. if(stOutstream == NULL) {
  128. fcgi_log("Error: out stream = null \r\n");
  129. }
  130. if(stErrstream == NULL) {
  131. fcgi_log("Error: err stream = null \r\n");
  132. }
  133. if(ret < 0) {
  134. fcgi_log("Error: fcgi_accept ret < 0.\r\n");
  135. }
  136. RETVAL_LONG(ret);
  137. }
  138. PHP_FUNCTION(fcgi_getparam)
  139. {
  140. char *arg = NULL;
  141. int arg_len;
  142. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
  143. return;
  144. }
  145. const char* ret = FCGX_GetParam(arg, stParams);
  146. ret == NULL ? "" : ret;
  147. char* result = estrdup(ret);
  148. RETURN_STRINGL(result,strlen(result),0);
  149. }
  150. void print_fun(char* szBuf)
  151. {
  152. if(stOutstream && szBuf) {
  153. FCGX_PutS(szBuf,stOutstream);
  154. }
  155. }
  156. PHP_FUNCTION(fcgi_finish)
  157. {
  158. fini_accept(print_fun);
  159. FCGX_Finish();
  160. fcgi_log("fcgi_finish\r\n");
  161. }