fcgi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include "multipart_parser.h"
  28. #include "multipart_parser.c"
  29. static int le_fcgi;
  30. const zend_function_entry fcgi_functions[] = {
  31. PHP_FE(confirm_fcgi_compiled, NULL) /* For testing, remove later. */
  32. PHP_FE(fcgi_init, NULL)
  33. PHP_FE(fcgi_fini, NULL)
  34. PHP_FE(fcgi_accept, NULL)
  35. PHP_FE(fcgi_finish, NULL)
  36. PHP_FE(fcgi_getparam, NULL)
  37. PHP_FE(fcgi_echo, NULL)
  38. PHP_FE(fcgi_getcontent, NULL)
  39. PHP_FE_END /* Must be the last line in fcgi_functions[] */
  40. };
  41. zend_module_entry fcgi_module_entry = {
  42. #if ZEND_MODULE_API_NO >= 20010901
  43. STANDARD_MODULE_HEADER,
  44. #endif
  45. "fcgi",
  46. fcgi_functions,
  47. PHP_MINIT(fcgi),
  48. PHP_MSHUTDOWN(fcgi),
  49. PHP_RINIT(fcgi), /* Replace with NULL if there's nothing to do at request start */
  50. PHP_RSHUTDOWN(fcgi), /* Replace with NULL if there's nothing to do at request end */
  51. PHP_MINFO(fcgi),
  52. #if ZEND_MODULE_API_NO >= 20010901
  53. PHP_FCGI_VERSION,
  54. #endif
  55. STANDARD_MODULE_PROPERTIES
  56. };
  57. /* }}} */
  58. #ifdef COMPILE_DL_FCGI
  59. ZEND_GET_MODULE(fcgi)
  60. #endif
  61. PHP_MINIT_FUNCTION(fcgi)
  62. {
  63. /* If you have INI entries, uncomment these lines
  64. REGISTER_INI_ENTRIES();
  65. */
  66. return SUCCESS;
  67. }
  68. PHP_MSHUTDOWN_FUNCTION(fcgi)
  69. {
  70. /* uncomment this line if you have INI entries
  71. UNREGISTER_INI_ENTRIES();
  72. */
  73. return SUCCESS;
  74. }
  75. PHP_RINIT_FUNCTION(fcgi)
  76. {
  77. return SUCCESS;
  78. }
  79. PHP_RSHUTDOWN_FUNCTION(fcgi)
  80. {
  81. return SUCCESS;
  82. }
  83. PHP_MINFO_FUNCTION(fcgi)
  84. {
  85. php_info_print_table_start();
  86. php_info_print_table_header(2, "fcgi support", "enabled");
  87. php_info_print_table_end();
  88. }
  89. PHP_FUNCTION(confirm_fcgi_compiled)
  90. {
  91. char *arg = NULL;
  92. int arg_len, len;
  93. char *strg;
  94. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
  95. return;
  96. }
  97. len = sprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "fcgi", arg);
  98. RETURN_STRINGL(strg, len, 0);
  99. }
  100. PHP_FUNCTION(fcgi_init)
  101. {
  102. }
  103. PHP_FUNCTION(fcgi_fini)
  104. {
  105. }
  106. static FCGX_Stream* stInstream;
  107. static FCGX_Stream* stOutstream;
  108. static FCGX_Stream* stErrstream;
  109. static FCGX_ParamArray stParams;
  110. PHP_FUNCTION(fcgi_accept)
  111. {
  112. int ret = FCGX_Accept(&stInstream,&stOutstream,&stErrstream,&stParams);
  113. if(stInstream == NULL) {
  114. RETVAL_LONG(-1);
  115. }
  116. if(stOutstream == NULL) {
  117. RETVAL_LONG(-1);
  118. }
  119. if(stErrstream == NULL) {
  120. RETVAL_LONG(-1);
  121. }
  122. RETVAL_LONG(ret);
  123. }
  124. PHP_FUNCTION(fcgi_getparam)
  125. {
  126. char *arg = NULL;
  127. int arg_len;
  128. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
  129. return;
  130. }
  131. const char* ret = FCGX_GetParam(arg, stParams);
  132. ret = (ret == NULL) ? "" : ret;
  133. char* result = estrdup(ret);
  134. RETURN_STRINGL(result,strlen(result),0);
  135. }
  136. static int content_length()
  137. {
  138. const char* pbuf = FCGX_GetParam("CONTENT_LENGTH", stParams);
  139. if(pbuf)
  140. {
  141. if(strlen(pbuf) == 0)
  142. return 0;
  143. else
  144. return atoi(pbuf);
  145. } else {
  146. return 0;
  147. }
  148. }
  149. ////////////////////////////////////////////////////////////////////////////////////
  150. // rfc1867 callback
  151. static char* g_pcontent = NULL;
  152. int read_header_name(multipart_parser* p, const char *at, size_t length)
  153. {
  154. // printf("%.*s: ", length, at);
  155. return 0;
  156. }
  157. // rfc1867 callback
  158. int read_header_value(multipart_parser* p, const char *at, size_t length)
  159. {
  160. // printf("%.*s\n", length, at);
  161. if (NULL == g_pcontent) return 0;
  162. char* pbegin = strstr(at, "\"");
  163. if (!pbegin) return 0;
  164. ++pbegin;
  165. const char* pend = strstr(pbegin, "\"");
  166. if (!pend) return 0;
  167. const int len = pend - pbegin;
  168. strncat(g_pcontent, pbegin, len);
  169. strcat(g_pcontent, "=");
  170. return 0;
  171. }
  172. // rfc1867 callback
  173. int read_part_data(multipart_parser* p, const char *at, size_t length)
  174. {
  175. // printf("%.*s\n", length, at);
  176. if (NULL == g_pcontent) return 0;
  177. strncat(g_pcontent, at, length);
  178. strcat(g_pcontent, "&");
  179. return 0;
  180. }
  181. PHP_FUNCTION(fcgi_getcontent)
  182. {
  183. int con_len = content_length();
  184. if(con_len) {
  185. char* pCon = (char*)malloc(con_len + 1);
  186. con_len = FCGX_GetStr(pCon, con_len, stInstream);
  187. pCon[con_len] = '\0';
  188. // post webform boundary proccess
  189. const char* pbuf = FCGX_GetParam("CONTENT_TYPE", stParams);
  190. if(pbuf)
  191. {
  192. const char* boundary_begin = strstr(pbuf, "boundary");
  193. if (boundary_begin)
  194. {
  195. boundary_begin = boundary_begin + strlen("boundary");
  196. boundary_begin = strstr(boundary_begin, "=");
  197. if (boundary_begin)
  198. {
  199. ++boundary_begin;
  200. const int boundary_len = strlen(boundary_begin) + 3;
  201. char* boundary_value = (char*)malloc(boundary_len);
  202. if (boundary_value)
  203. {
  204. memset(boundary_value, 0, boundary_len);
  205. strcat(boundary_value, "--");
  206. strcat(boundary_value, boundary_begin);
  207. g_pcontent = (char*)malloc(con_len + 1);
  208. memset(g_pcontent, 0, con_len + 1);
  209. multipart_parser_settings callbacks;
  210. memset(&callbacks, 0, sizeof(multipart_parser_settings));
  211. callbacks.on_header_field = read_header_name;
  212. callbacks.on_header_value = read_header_value;
  213. callbacks.on_part_data = read_part_data;
  214. multipart_parser* parser = multipart_parser_init(boundary_value, &callbacks);
  215. if (parser) {
  216. multipart_parser_execute(parser, pCon, con_len);
  217. multipart_parser_free(parser);
  218. }
  219. if(g_pcontent != NULL)
  220. {
  221. int len = strlen(g_pcontent);
  222. if(len > 0 && g_pcontent[len - 1] == '&') {
  223. g_pcontent[len - 1] = '\0';
  224. }
  225. }
  226. char* result = estrdup(g_pcontent);
  227. free(g_pcontent);
  228. g_pcontent = NULL;
  229. RETURN_STRINGL(result, strlen(result),0);
  230. }
  231. }
  232. }
  233. }
  234. // normal proccess
  235. char* result = estrdup(pCon);
  236. RETURN_STRINGL(result,strlen(result),0);
  237. } else {
  238. char* result = estrdup("");
  239. RETURN_STRINGL(result,strlen(result),0);
  240. }
  241. }
  242. PHP_FUNCTION(fcgi_echo)
  243. {
  244. char *arg = NULL;
  245. int arg_len;
  246. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
  247. return;
  248. }
  249. if(arg != NULL) {
  250. FCGX_PutS(arg,stOutstream);
  251. }
  252. }
  253. PHP_FUNCTION(fcgi_finish)
  254. {
  255. FCGX_Finish();
  256. }