PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/requests/yaf_request_http.c

https://gitlab.com/oytunistrator/yaf
C | 312 lines | 201 code | 43 blank | 68 comment | 58 complexity | 51e2b515f68eb7aa7e80906002da377f MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Yet Another Framework |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Xinchen Hui <laruence@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "php.h"
  20. #include "main/SAPI.h"
  21. #include "ext/standard/url.h" /* for php_url */
  22. #include "php_yaf.h"
  23. #include "yaf_namespace.h"
  24. #include "yaf_request.h"
  25. #include "yaf_exception.h"
  26. #include "requests/yaf_request_http.h"
  27. static zend_class_entry * yaf_request_http_ce;
  28. /** {{{ yaf_request_t * yaf_request_http_instance(yaf_request_t *this_ptr, char *request_uri, char *base_uri TSRMLS_DC)
  29. */
  30. yaf_request_t * yaf_request_http_instance(yaf_request_t *this_ptr, char *request_uri, char *base_uri TSRMLS_DC) {
  31. yaf_request_t *instance;
  32. zval *method, *params, *settled_uri = NULL;
  33. if (this_ptr) {
  34. instance = this_ptr;
  35. } else {
  36. MAKE_STD_ZVAL(instance);
  37. object_init_ex(instance, yaf_request_http_ce);
  38. }
  39. MAKE_STD_ZVAL(method);
  40. if (SG(request_info).request_method) {
  41. ZVAL_STRING(method, (char *)SG(request_info).request_method, 1);
  42. } else if (strncasecmp(sapi_module.name, "cli", 3)) {
  43. ZVAL_STRING(method, "Unknow", 1);
  44. } else {
  45. ZVAL_STRING(method, "Cli", 1);
  46. }
  47. zend_update_property(yaf_request_http_ce, instance, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_METHOD), method TSRMLS_CC);
  48. zval_ptr_dtor(&method);
  49. if (request_uri) {
  50. MAKE_STD_ZVAL(settled_uri);
  51. ZVAL_STRING(settled_uri, request_uri, 1);
  52. } else {
  53. zval *uri;
  54. do {
  55. #ifdef PHP_WIN32
  56. /* check this first so IIS will catch */
  57. uri = yaf_request_query(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("HTTP_X_REWRITE_URL") TSRMLS_CC);
  58. if (Z_TYPE_P(uri) != IS_NULL) {
  59. settled_uri = uri;
  60. break;
  61. }
  62. zval_ptr_dtor(&uri);
  63. /* IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem) */
  64. uri = yaf_request_query(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("IIS_WasUrlRewritten") TSRMLS_CC);
  65. if (Z_TYPE_P(uri) != IS_NULL) {
  66. zval *rewrited = yaf_request_query(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("IIS_WasUrlRewritten") TSRMLS_CC);
  67. zval *unencode = yaf_request_query(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("UNENCODED_URL") TSRMLS_CC);
  68. if (Z_TYPE_P(rewrited) == IS_LONG
  69. && Z_LVAL_P(rewrited) == 1
  70. && Z_TYPE_P(unencode) == IS_STRING
  71. && Z_STRLEN_P(unencode) > 0) {
  72. settled_uri = uri;
  73. }
  74. break;
  75. }
  76. zval_ptr_dtor(&uri);
  77. #endif
  78. uri = yaf_request_query(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("PATH_INFO") TSRMLS_CC);
  79. if (Z_TYPE_P(uri) != IS_NULL) {
  80. settled_uri = uri;
  81. break;
  82. }
  83. zval_ptr_dtor(&uri);
  84. uri = yaf_request_query(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("REQUEST_URI") TSRMLS_CC);
  85. if (Z_TYPE_P(uri) != IS_NULL) {
  86. /* Http proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path */
  87. if (strstr(Z_STRVAL_P(uri), "http") == Z_STRVAL_P(uri)) {
  88. php_url *url_info = php_url_parse(Z_STRVAL_P(uri));
  89. zval_ptr_dtor(&uri);
  90. if (url_info && url_info->path) {
  91. MAKE_STD_ZVAL(settled_uri);
  92. ZVAL_STRING(settled_uri, url_info->path, 1);
  93. }
  94. php_url_free(url_info);
  95. } else {
  96. char *pos = NULL;
  97. if ((pos = strstr(Z_STRVAL_P(uri), "?"))) {
  98. MAKE_STD_ZVAL(settled_uri);
  99. ZVAL_STRINGL(settled_uri, Z_STRVAL_P(uri), pos - Z_STRVAL_P(uri), 1);
  100. zval_ptr_dtor(&uri);
  101. } else {
  102. settled_uri = uri;
  103. }
  104. }
  105. break;
  106. }
  107. zval_ptr_dtor(&uri);
  108. uri = yaf_request_query(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("ORIG_PATH_INFO") TSRMLS_CC);
  109. if (Z_TYPE_P(uri) != IS_NULL) {
  110. /* intended do nothing */
  111. /* zval *query = yaf_request_query(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("QUERY_STRING") TSRMLS_CC);
  112. if (Z_TYPE_P(query) != IS_NULL) {
  113. }
  114. */
  115. settled_uri = uri;
  116. break;
  117. }
  118. zval_ptr_dtor(&uri);
  119. } while (0);
  120. }
  121. if (settled_uri) {
  122. char *p = Z_STRVAL_P(settled_uri);
  123. while (*p == '/' && *(p + 1) == '/') {
  124. p++;
  125. }
  126. if (p != Z_STRVAL_P(settled_uri)) {
  127. char *garbage = Z_STRVAL_P(settled_uri);
  128. ZVAL_STRING(settled_uri, p, 1);
  129. efree(garbage);
  130. }
  131. zend_update_property(yaf_request_http_ce, instance, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_URI), settled_uri TSRMLS_CC);
  132. yaf_request_set_base_uri(instance, base_uri, Z_STRVAL_P(settled_uri) TSRMLS_CC);
  133. zval_ptr_dtor(&settled_uri);
  134. }
  135. MAKE_STD_ZVAL(params);
  136. array_init(params);
  137. zend_update_property(yaf_request_http_ce, instance, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_PARAMS), params TSRMLS_CC);
  138. zval_ptr_dtor(&params);
  139. return instance;
  140. }
  141. /* }}} */
  142. /** {{{ proto public Yaf_Request_Http::getQuery(mixed $name, mixed $default = NULL)
  143. */
  144. YAF_REQUEST_METHOD(yaf_request_http, Query, YAF_GLOBAL_VARS_GET);
  145. /* }}} */
  146. /** {{{ proto public Yaf_Request_Http::getPost(mixed $name, mixed $default = NULL)
  147. */
  148. YAF_REQUEST_METHOD(yaf_request_http, Post, YAF_GLOBAL_VARS_POST);
  149. /* }}} */
  150. /** {{{ proto public Yaf_Request_Http::getRequet(mixed $name, mixed $default = NULL)
  151. */
  152. YAF_REQUEST_METHOD(yaf_request_http, Request, YAF_GLOBAL_VARS_REQUEST);
  153. /* }}} */
  154. /** {{{ proto public Yaf_Request_Http::getFiles(mixed $name, mixed $default = NULL)
  155. */
  156. YAF_REQUEST_METHOD(yaf_request_http, Files, YAF_GLOBAL_VARS_FILES);
  157. /* }}} */
  158. /** {{{ proto public Yaf_Request_Http::getCookie(mixed $name, mixed $default = NULL)
  159. */
  160. YAF_REQUEST_METHOD(yaf_request_http, Cookie, YAF_GLOBAL_VARS_COOKIE);
  161. /* }}} */
  162. /** {{{ proto public Yaf_Request_Http::isXmlHttpRequest()
  163. */
  164. PHP_METHOD(yaf_request_http, isXmlHttpRequest) {
  165. zval * header = yaf_request_query(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("HTTP_X_REQUESTED_WITH") TSRMLS_CC);
  166. if (Z_TYPE_P(header) == IS_STRING
  167. && strncasecmp("XMLHttpRequest", Z_STRVAL_P(header), Z_STRLEN_P(header)) == 0) {
  168. zval_ptr_dtor(&header);
  169. RETURN_TRUE;
  170. }
  171. zval_ptr_dtor(&header);
  172. RETURN_FALSE;
  173. }
  174. /* }}} */
  175. /** {{{ proto public Yaf_Request_Http::get(mixed $name, mixed $default)
  176. * params -> post -> get -> cookie -> server
  177. */
  178. PHP_METHOD(yaf_request_http, get) {
  179. char *name = NULL;
  180. int len = 0;
  181. zval *def = NULL;
  182. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &name, &len, &def) == FAILURE) {
  183. WRONG_PARAM_COUNT;
  184. } else {
  185. zval *value = yaf_request_get_param(getThis(), name, len TSRMLS_CC);
  186. if (value) {
  187. RETURN_ZVAL(value, 1, 0);
  188. } else {
  189. zval *params = NULL;
  190. zval **ppzval = NULL;
  191. YAF_GLOBAL_VARS_TYPE methods[4] = {
  192. YAF_GLOBAL_VARS_POST,
  193. YAF_GLOBAL_VARS_GET,
  194. YAF_GLOBAL_VARS_COOKIE,
  195. YAF_GLOBAL_VARS_SERVER
  196. };
  197. {
  198. int i = 0;
  199. for (;i<4; i++) {
  200. params = PG(http_globals)[methods[i]];
  201. if (params && Z_TYPE_P(params) == IS_ARRAY) {
  202. if (zend_hash_find(Z_ARRVAL_P(params), name, len + 1, (void **)&ppzval) == SUCCESS ){
  203. RETURN_ZVAL(*ppzval, 1, 0);
  204. }
  205. }
  206. }
  207. }
  208. if (def) {
  209. RETURN_ZVAL(def, 1, 0);
  210. }
  211. }
  212. }
  213. RETURN_NULL();
  214. }
  215. /* }}} */
  216. /** {{{ proto public Yaf_Request_Http::__construct(string $request_uri, string $base_uri)
  217. */
  218. PHP_METHOD(yaf_request_http, __construct) {
  219. char *request_uri = NULL;
  220. char *base_uri = NULL;
  221. int rlen = 0;
  222. int blen = 0;
  223. yaf_request_t *self = getThis();
  224. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &request_uri, &rlen, &base_uri, &blen) == FAILURE) {
  225. YAF_UNINITIALIZED_OBJECT(getThis());
  226. return;
  227. }
  228. (void)yaf_request_http_instance(self, request_uri, base_uri TSRMLS_CC);
  229. }
  230. /* }}} */
  231. /** {{{ proto private Yaf_Request_Http::__clone
  232. */
  233. PHP_METHOD(yaf_request_http, __clone) {
  234. }
  235. /* }}} */
  236. /** {{{ yaf_request_http_methods
  237. */
  238. zend_function_entry yaf_request_http_methods[] = {
  239. PHP_ME(yaf_request_http, getQuery, NULL, ZEND_ACC_PUBLIC)
  240. PHP_ME(yaf_request_http, getRequest, NULL, ZEND_ACC_PUBLIC)
  241. PHP_ME(yaf_request_http, getPost, NULL, ZEND_ACC_PUBLIC)
  242. PHP_ME(yaf_request_http, getCookie, NULL, ZEND_ACC_PUBLIC)
  243. PHP_ME(yaf_request_http, getFiles, NULL, ZEND_ACC_PUBLIC)
  244. PHP_ME(yaf_request_http, get, NULL, ZEND_ACC_PUBLIC)
  245. PHP_ME(yaf_request_http, isXmlHttpRequest, NULL, ZEND_ACC_PUBLIC)
  246. PHP_ME(yaf_request_http, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
  247. PHP_ME(yaf_request_http, __clone, NULL, ZEND_ACC_PRIVATE | ZEND_ACC_CLONE)
  248. {NULL, NULL, NULL}
  249. };
  250. /* }}} */
  251. /** {{{ YAF_STARTUP_FUNCTION
  252. */
  253. YAF_STARTUP_FUNCTION(request_http){
  254. zend_class_entry ce;
  255. YAF_INIT_CLASS_ENTRY(ce, "Yaf_Request_Http", "Yaf\\Request\\Http", yaf_request_http_methods);
  256. yaf_request_http_ce = zend_register_internal_class_ex(&ce, yaf_request_ce, NULL TSRMLS_CC);
  257. zend_declare_class_constant_string(yaf_request_ce, ZEND_STRL("SCHEME_HTTP"), "http" TSRMLS_CC);
  258. zend_declare_class_constant_string(yaf_request_ce, ZEND_STRL("SCHEME_HTTPS"), "https" TSRMLS_CC);
  259. return SUCCESS;
  260. }
  261. /* }}} */
  262. /*
  263. * Local variables:
  264. * tab-width: 4
  265. * c-basic-offset: 4
  266. * End:
  267. * vim600: noet sw=4 ts=4 fdm=marker
  268. * vim<600: noet sw=4 ts=4
  269. */