fcgi.c 7.5 KB

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