fcgi.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 nthing 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. zend_string *arg;
  92. char* ret = NULL;
  93. // int len = 0;
  94. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &arg) == FAILURE) {
  95. ret = "as this return char* data";
  96. // len = strlen(ret);
  97. // RETURN_STRING(ret,len);
  98. RETURN_STRING(ret);
  99. } else {
  100. RETURN_STR(arg);
  101. }
  102. }
  103. PHP_FUNCTION(fcgi_init)
  104. {
  105. }
  106. PHP_FUNCTION(fcgi_fini)
  107. {
  108. }
  109. static FCGX_Stream* stInstream;
  110. static FCGX_Stream* stOutstream;
  111. static FCGX_Stream* stErrstream;
  112. static FCGX_ParamArray stParams;
  113. PHP_FUNCTION(fcgi_accept)
  114. {
  115. int ret = FCGX_Accept(&stInstream,&stOutstream,&stErrstream,&stParams);
  116. if(stInstream == NULL) {
  117. RETVAL_LONG(-1);
  118. }
  119. if(stOutstream == NULL) {
  120. RETVAL_LONG(-1);
  121. }
  122. if(stErrstream == NULL) {
  123. RETVAL_LONG(-1);
  124. }
  125. RETVAL_LONG(ret);
  126. }
  127. PHP_FUNCTION(fcgi_getparam)
  128. {
  129. zend_string *arg = NULL;
  130. int arg_len;
  131. char* type;
  132. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &arg) == FAILURE) {
  133. RETURN_FALSE;
  134. }
  135. type = ZSTR_VAL(arg);
  136. const char* ret = FCGX_GetParam(type, stParams);
  137. ret = (ret == NULL) ? "" : ret;
  138. char* result = estrdup(ret);
  139. RETURN_STRING(result);
  140. }
  141. static int content_length()
  142. {
  143. const char* pbuf = FCGX_GetParam("CONTENT_LENGTH", stParams);
  144. if(pbuf)
  145. {
  146. if(strlen(pbuf) == 0)
  147. return 0;
  148. else
  149. return atoi(pbuf);
  150. } else {
  151. return 0;
  152. }
  153. }
  154. ////////////////////////////////////////////////////////////////////////////////////
  155. // rfc1867 callback
  156. static char* g_pcontent = NULL;
  157. int read_header_name(multipart_parser* p, const char *at, size_t length)
  158. {
  159. // printf("%.*s: ", length, at);
  160. return 0;
  161. }
  162. // rfc1867 callback
  163. int read_header_value(multipart_parser* p, const char *at, size_t length)
  164. {
  165. // printf("%.*s\n", length, at);
  166. if (NULL == g_pcontent) return 0;
  167. char* pbegin = strstr(at, "\"");
  168. if (!pbegin) return 0;
  169. ++pbegin;
  170. const char* pend = strstr(pbegin, "\"");
  171. if (!pend) return 0;
  172. const int len = pend - pbegin;
  173. strncat(g_pcontent, pbegin, len);
  174. strcat(g_pcontent, "=");
  175. return 0;
  176. }
  177. // rfc1867 callback
  178. int read_part_data(multipart_parser* p, const char *at, size_t length)
  179. {
  180. // printf("%.*s\n", length, at);
  181. if (NULL == g_pcontent) return 0;
  182. strncat(g_pcontent, at, length);
  183. strcat(g_pcontent, "&");
  184. return 0;
  185. }
  186. PHP_FUNCTION(fcgi_getcontent)
  187. {
  188. int con_len = content_length();
  189. if(con_len) {
  190. char* pCon = (char*)malloc(con_len + 1);
  191. con_len = FCGX_GetStr(pCon, con_len, stInstream);
  192. pCon[con_len] = '\0';
  193. // post webform boundary proccess
  194. const char* pbuf = FCGX_GetParam("CONTENT_TYPE", stParams);
  195. if(pbuf)
  196. {
  197. const char* boundary_begin = strstr(pbuf, "boundary");
  198. if (boundary_begin)
  199. {
  200. boundary_begin = boundary_begin + strlen("boundary");
  201. boundary_begin = strstr(boundary_begin, "=");
  202. if (boundary_begin)
  203. {
  204. ++boundary_begin;
  205. const int boundary_len = strlen(boundary_begin) + 3;
  206. char* boundary_value = (char*)malloc(boundary_len);
  207. if (boundary_value)
  208. {
  209. memset(boundary_value, 0, boundary_len);
  210. strcat(boundary_value, "--");
  211. strcat(boundary_value, boundary_begin);
  212. g_pcontent = (char*)malloc(con_len + 1);
  213. memset(g_pcontent, 0, con_len + 1);
  214. multipart_parser_settings callbacks;
  215. memset(&callbacks, 0, sizeof(multipart_parser_settings));
  216. callbacks.on_header_field = read_header_name;
  217. callbacks.on_header_value = read_header_value;
  218. callbacks.on_part_data = read_part_data;
  219. multipart_parser* parser = multipart_parser_init(boundary_value, &callbacks);
  220. if (parser) {
  221. multipart_parser_execute(parser, pCon, con_len);
  222. multipart_parser_free(parser);
  223. }
  224. if(g_pcontent != NULL)
  225. {
  226. int len = strlen(g_pcontent);
  227. if(len > 0 && g_pcontent[len - 1] == '&') {
  228. g_pcontent[len - 1] = '\0';
  229. }
  230. }
  231. char* result = estrdup(g_pcontent);
  232. free(g_pcontent);
  233. g_pcontent = NULL;
  234. RETURN_STRING(result);
  235. }
  236. }
  237. }
  238. }
  239. // normal proccess
  240. char* result = estrdup(pCon);
  241. RETURN_STRING(result);
  242. }
  243. else {
  244. char* result = estrdup("");
  245. RETURN_STRING(result);
  246. }
  247. }
  248. PHP_FUNCTION(fcgi_echo)
  249. {
  250. zend_string *arg = NULL;
  251. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &arg) == FAILURE) {
  252. return;
  253. }
  254. if(arg != NULL)
  255. {
  256. char* data = ZSTR_VAL(arg);
  257. FCGX_PutS(data,stOutstream);
  258. }
  259. }
  260. static void print_all_header()
  261. {
  262. for( ; *stParams != NULL; stParams++) {
  263. FCGX_PutS(*stParams,stOutstream);
  264. FCGX_PutS("</br>",stOutstream);
  265. }
  266. }
  267. PHP_FUNCTION(fcgi_finish)
  268. {
  269. FCGX_Finish();
  270. }