- enable dns data sharing for the global request datashare by default
[m6w6/ext-http] / http_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #include "php_http.h"
17
18 #include "php_output.h"
19 #include "ext/standard/url.h"
20
21 #include "php_http_api.h"
22 #include "php_http_send_api.h"
23
24 #ifdef ZEND_ENGINE_2
25 # include "php_http_exception_object.h"
26 #endif
27
28 PHP_MINIT_FUNCTION(http_support)
29 {
30 HTTP_LONG_CONSTANT("HTTP_SUPPORT", HTTP_SUPPORT);
31 HTTP_LONG_CONSTANT("HTTP_SUPPORT_REQUESTS", HTTP_SUPPORT_REQUESTS);
32 HTTP_LONG_CONSTANT("HTTP_SUPPORT_MAGICMIME", HTTP_SUPPORT_MAGICMIME);
33 HTTP_LONG_CONSTANT("HTTP_SUPPORT_ENCODINGS", HTTP_SUPPORT_ENCODINGS);
34 HTTP_LONG_CONSTANT("HTTP_SUPPORT_SSLREQUESTS", HTTP_SUPPORT_SSLREQUESTS);
35
36 HTTP_LONG_CONSTANT("HTTP_PARAMS_ALLOW_COMMA", HTTP_PARAMS_ALLOW_COMMA);
37 HTTP_LONG_CONSTANT("HTTP_PARAMS_ALLOW_FAILURE", HTTP_PARAMS_ALLOW_FAILURE);
38 HTTP_LONG_CONSTANT("HTTP_PARAMS_RAISE_ERROR", HTTP_PARAMS_RAISE_ERROR);
39 HTTP_LONG_CONSTANT("HTTP_PARAMS_DEFAULT", HTTP_PARAMS_DEFAULT);
40
41 return SUCCESS;
42 }
43
44 PHP_HTTP_API long _http_support(long feature)
45 {
46 long support = HTTP_SUPPORT;
47
48 #ifdef HTTP_HAVE_CURL
49 support |= HTTP_SUPPORT_REQUESTS;
50 # ifdef HTTP_HAVE_SSL
51 support |= HTTP_SUPPORT_SSLREQUESTS;
52 # endif
53 #endif
54 #ifdef HTTP_HAVE_MAGIC
55 support |= HTTP_SUPPORT_MAGICMIME;
56 #endif
57 #ifdef HTTP_HAVE_ZLIB
58 support |= HTTP_SUPPORT_ENCODINGS;
59 #endif
60
61 if (feature) {
62 return (feature == (support & feature));
63 }
64 return support;
65 }
66
67 /* char *pretty_key(char *, size_t, zend_bool, zend_bool) */
68 char *_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
69 {
70 if (key && key_len) {
71 size_t i;
72 int wasalpha;
73 if ((wasalpha = isalpha((int) key[0]))) {
74 key[0] = (char) (uctitle ? toupper((int) key[0]) : tolower((int) key[0]));
75 }
76 for (i = 1; i < key_len; i++) {
77 if (isalpha((int) key[i])) {
78 key[i] = (char) (((!wasalpha) && uctitle) ? toupper((int) key[i]) : tolower((int) key[i]));
79 wasalpha = 1;
80 } else {
81 if (xhyphen && (key[i] == '_')) {
82 key[i] = '-';
83 }
84 wasalpha = 0;
85 }
86 }
87 }
88 return key;
89 }
90 /* }}} */
91
92 /* {{{ void http_error(long, long, char*) */
93 void _http_error_ex(long type TSRMLS_DC, long code, const char *format, ...)
94 {
95 va_list args;
96
97 va_start(args, format);
98 #ifdef ZEND_ENGINE_2
99 if ((type == E_THROW) || (PG(error_handling) == EH_THROW)) {
100 char *message;
101 zend_class_entry *ce = http_exception_get_for_code(code);
102
103 http_try {
104 vspprintf(&message, 0, format, args);
105 zend_throw_exception(ce, message, code TSRMLS_CC);
106 efree(message);
107 } http_catch(PG(exception_class) ? PG(exception_class) : HTTP_EX_DEF_CE);
108 } else
109 #endif
110 php_verror(NULL, "", type, format, args TSRMLS_CC);
111 va_end(args);
112 }
113 /* }}} */
114
115 #ifdef ZEND_ENGINE_2
116 static inline void copy_bt_args(zval *from, zval *to TSRMLS_DC)
117 {
118 zval **args, **trace_0, *old_trace_0, *trace = NULL;
119
120 if ((trace = zend_read_property(ZEND_EXCEPTION_GET_DEFAULT(), from, "trace", lenof("trace"), 0 TSRMLS_CC))) {
121 if (Z_TYPE_P(trace) == IS_ARRAY && SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void *) &trace_0)) {
122 old_trace_0 = *trace_0;
123 if (Z_TYPE_PP(trace_0) == IS_ARRAY && SUCCESS == zend_hash_find(Z_ARRVAL_PP(trace_0), "args", sizeof("args"), (void *) &args)) {
124 if ((trace = zend_read_property(ZEND_EXCEPTION_GET_DEFAULT(), to, "trace", lenof("trace"), 0 TSRMLS_CC))) {
125 if (Z_TYPE_P(trace) == IS_ARRAY && SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void *) &trace_0)) {
126 ZVAL_ADDREF(*args);
127 add_assoc_zval(*trace_0, "args", *args);
128 }
129 }
130 }
131 }
132 }
133 }
134
135 /* {{{ zval *http_exception_wrap(zval *, zval *, zend_class_entry *) */
136 zval *_http_exception_wrap(zval *old_exception, zval *new_exception, zend_class_entry *ce TSRMLS_DC)
137 {
138 int inner = 1;
139 char *message;
140 zval *sub_exception, *tmp_exception;
141
142 if (!new_exception) {
143 MAKE_STD_ZVAL(new_exception);
144 object_init_ex(new_exception, ce);
145
146 zend_update_property(ce, new_exception, "innerException", lenof("innerException"), old_exception TSRMLS_CC);
147 copy_bt_args(old_exception, new_exception TSRMLS_CC);
148
149 sub_exception = old_exception;
150
151 while ((sub_exception = zend_read_property(Z_OBJCE_P(sub_exception), sub_exception, "innerException", lenof("innerException"), 0 TSRMLS_CC)) && Z_TYPE_P(sub_exception) == IS_OBJECT) {
152 ++inner;
153 }
154
155 spprintf(&message, 0, "Exception caused by %d inner exception(s)", inner);
156 zend_update_property_string(ZEND_EXCEPTION_GET_DEFAULT(), new_exception, "message", lenof("message"), message TSRMLS_CC);
157 efree(message);
158 } else {
159 sub_exception = new_exception;
160 tmp_exception = new_exception;
161
162 while ((tmp_exception = zend_read_property(Z_OBJCE_P(tmp_exception), tmp_exception, "innerException", lenof("innerException"), 0 TSRMLS_CC)) && Z_TYPE_P(tmp_exception) == IS_OBJECT) {
163 sub_exception = tmp_exception;
164 }
165
166 zend_update_property(Z_OBJCE_P(sub_exception), sub_exception, "innerException", lenof("innerException"), old_exception TSRMLS_CC);
167 copy_bt_args(old_exception, new_exception TSRMLS_CC);
168 copy_bt_args(old_exception, sub_exception TSRMLS_CC);
169 }
170
171 zval_ptr_dtor(&old_exception);
172 return new_exception;
173 }
174 /* }}} */
175 #endif /* ZEND_ENGINE_2 */
176
177 /* {{{ void http_log(char *, char *, char *) */
178 void _http_log_ex(char *file, const char *ident, const char *message TSRMLS_DC)
179 {
180 time_t now;
181 struct tm nowtm;
182 char datetime[20] = {0};
183
184 now = HTTP_G->request.time;
185 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
186
187 #define HTTP_LOG_WRITE(file, type, msg) \
188 if (file && *file) { \
189 php_stream *log = php_stream_open_wrapper_ex(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT); \
190 \
191 if (log) { \
192 php_stream_printf(log TSRMLS_CC, "%s\t[%s]\t%s\t<%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
193 php_stream_close(log); \
194 } \
195 \
196 }
197
198 HTTP_LOG_WRITE(file, ident, message);
199 HTTP_LOG_WRITE(HTTP_G->log.composite, ident, message);
200 }
201 /* }}} */
202
203 static void http_ob_blackhole(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
204 {
205 *handled_output = ecalloc(1,1);
206 *handled_output_len = 0;
207 }
208
209 /* {{{ STATUS http_exit(int, char*, char*) */
210 STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header TSRMLS_DC)
211 {
212 if ( (send_header && (SUCCESS != http_send_status_header(status, header))) ||
213 (status && (SUCCESS != http_send_status(status)))) {
214 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Failed to exit with status/header: %d - %s", status, header ? header : "");
215 STR_FREE(header);
216 STR_FREE(body);
217 return FAILURE;
218 }
219
220 if (!OG(ob_lock)) {
221 php_end_ob_buffers(0 TSRMLS_CC);
222 }
223 if ((SUCCESS == sapi_send_headers(TSRMLS_C)) && body) {
224 PHPWRITE(body, strlen(body));
225 }
226
227 switch (status) {
228 case 301: http_log(HTTP_G->log.redirect, "301-REDIRECT", header); break;
229 case 302: http_log(HTTP_G->log.redirect, "302-REDIRECT", header); break;
230 case 303: http_log(HTTP_G->log.redirect, "303-REDIRECT", header); break;
231 case 305: http_log(HTTP_G->log.redirect, "305-REDIRECT", header); break;
232 case 307: http_log(HTTP_G->log.redirect, "307-REDIRECT", header); break;
233 case 304: http_log(HTTP_G->log.cache, "304-CACHE", header); break;
234 case 404: http_log(HTTP_G->log.not_found, "404-NOTFOUND", NULL); break;
235 case 405: http_log(HTTP_G->log.allowed_methods, "405-ALLOWED", header); break;
236 default: http_log(NULL, header, body); break;
237 }
238
239 STR_FREE(header);
240 STR_FREE(body);
241
242 if (HTTP_G->force_exit) {
243 zend_bailout();
244 } else {
245 php_ob_set_internal_handler(http_ob_blackhole, 4096, "blackhole", 0 TSRMLS_CC);
246 }
247
248 return SUCCESS;
249 }
250 /* }}} */
251
252 /* {{{ STATUS http_check_method(char *) */
253 STATUS _http_check_method_ex(const char *method, const char *methods)
254 {
255 const char *found;
256
257 if ( (found = strstr(methods, method)) &&
258 (found == method || !isalpha(found[-1])) &&
259 (strlen(found) >= strlen(method) && !isalpha(found[strlen(method)]))) {
260 return SUCCESS;
261 }
262 return FAILURE;
263 }
264 /* }}} */
265
266 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
267 PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC)
268 {
269 zval **hsv;
270 zval **var;
271 #ifdef ZEND_ENGINE_2
272 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
273 #endif
274 if ((SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &hsv)) || (Z_TYPE_PP(hsv) != IS_ARRAY)) {
275 return NULL;
276 }
277 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), (char *) key, key_size, (void *) &var))) {
278 return NULL;
279 }
280 if (check && !((Z_TYPE_PP(var) == IS_STRING) && Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
281 return NULL;
282 }
283 return *var;
284 }
285 /* }}} */
286
287 /* {{{ STATUS http_get_request_body(char **, size_t *) */
288 PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_bool dup TSRMLS_DC)
289 {
290 *length = 0;
291 *body = NULL;
292
293 if (SG(request_info).raw_post_data) {
294 *length = SG(request_info).raw_post_data_length;
295 *body = SG(request_info).raw_post_data;
296
297 if (dup) {
298 *body = estrndup(*body, *length);
299 }
300 return SUCCESS;
301 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
302 char buf[4096];
303 int len;
304
305 HTTP_G->read_post_data = 1;
306
307 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
308 *body = erealloc(*body, *length + len + 1);
309 memcpy(*body + *length, buf, len);
310 *length += len;
311 (*body)[*length] = '\0';
312 }
313
314 /* check for error */
315 if (len < 0) {
316 STR_FREE(*body);
317 *length = 0;
318 return FAILURE;
319 }
320
321 SG(request_info).raw_post_data = *body;
322 SG(request_info).raw_post_data_length = *length;
323
324 if (dup) {
325 *body = estrndup(*body, *length);
326 }
327 return SUCCESS;
328 }
329
330 return FAILURE;
331 }
332 /* }}} */
333
334 /* {{{ php_stream *http_get_request_body_stream(void) */
335 PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D)
336 {
337 php_stream *s = NULL;
338
339 if (SG(request_info).raw_post_data) {
340 s = php_stream_open_wrapper("php://input", "rb", 0, NULL);
341 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
342 HTTP_G->read_post_data = 1;
343
344 if ((s = php_stream_temp_new())) {
345 char buf[4096];
346 int len;
347
348 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
349 php_stream_write(s, buf, len);
350 }
351
352 if (len < 0) {
353 php_stream_close(s);
354 s = NULL;
355 } else {
356 php_stream_rewind(s);
357 }
358 }
359 }
360
361 return s;
362 }
363 /* }}} */
364
365 /* {{{ void http_parse_params_default_callback(...) */
366 PHP_HTTP_API void _http_parse_params_default_callback(void *arg, const char *key, int keylen, const char *val, int vallen TSRMLS_DC)
367 {
368 char *kdup;
369 zval tmp, *entry;
370 HashTable *ht = (HashTable *) arg;
371
372 if (ht) {
373 INIT_ZARR(tmp, ht);
374
375 if (vallen) {
376 MAKE_STD_ZVAL(entry);
377 array_init(entry);
378 kdup = estrndup(key, keylen);
379 add_assoc_stringl_ex(entry, kdup, keylen + 1, (char *) val, vallen, 1);
380 efree(kdup);
381 add_next_index_zval(&tmp, entry);
382 } else {
383 add_next_index_stringl(&tmp, (char *) key, keylen, 1);
384 }
385 }
386 }
387 /* }}} */
388
389 /* {{{ STATUS http_parse_params(const char *, HashTable *) */
390 PHP_HTTP_API STATUS _http_parse_params_ex(const char *param, int flags, http_parse_params_callback cb, void *cb_arg TSRMLS_DC)
391 {
392 #define ST_QUOTE 1
393 #define ST_VALUE 2
394 #define ST_KEY 3
395 #define ST_ASSIGN 4
396 #define ST_ADD 5
397
398 int st = ST_KEY, keylen = 0, vallen = 0;
399 char *s, *c, *key = NULL, *val = NULL;
400
401 for(c = s = estrdup(param);;) {
402 #if 0
403 char *tk = NULL, *tv = NULL;
404
405 if (key) {
406 if (keylen) {
407 tk= estrndup(key, keylen);
408 } else {
409 tk = ecalloc(1, 7);
410 memcpy(tk, key, 3);
411 tk[3]='.'; tk[4]='.'; tk[5]='.';
412 }
413 }
414 if (val) {
415 if (vallen) {
416 tv = estrndup(val, vallen);
417 } else {
418 tv = ecalloc(1, 7);
419 memcpy(tv, val, 3);
420 tv[3]='.'; tv[4]='.'; tv[5]='.';
421 }
422 }
423 fprintf(stderr, "[%6s] %c \"%s=%s\"\n",
424 (
425 st == ST_QUOTE ? "QUOTE" :
426 st == ST_VALUE ? "VALUE" :
427 st == ST_KEY ? "KEY" :
428 st == ST_ASSIGN ? "ASSIGN" :
429 st == ST_ADD ? "ADD":
430 "HUH?"
431 ), *c?*c:'0', tk, tv
432 );
433 STR_FREE(tk); STR_FREE(tv);
434 #endif
435 continued:
436 switch (st) {
437 case ST_QUOTE:
438 quote:
439 if (*c == '"') {
440 if (*(c-1) == '\\') {
441 memmove(c-1, c, strlen(c)+1);
442 goto quote;
443 } else {
444 goto add;
445 }
446 } else {
447 if (!val) {
448 val = c;
449 }
450 if (!*c) {
451 --val;
452 st = ST_ADD;
453 }
454 }
455 break;
456
457 case ST_VALUE:
458 switch (*c) {
459 case '"':
460 if (!val) {
461 st = ST_QUOTE;
462 }
463 break;
464
465 case ' ':
466 break;
467
468 case ';':
469 case '\0':
470 goto add;
471 break;
472 case ',':
473 if (flags & HTTP_PARAMS_ALLOW_COMMA) {
474 goto add;
475 }
476 default:
477 if (!val) {
478 val = c;
479 }
480 break;
481 }
482 break;
483
484 case ST_KEY:
485 switch (*c) {
486 case ',':
487 if (flags & HTTP_PARAMS_ALLOW_COMMA) {
488 goto allow_comma;
489 }
490 case '\r':
491 case '\n':
492 case '\t':
493 case '\013':
494 case '\014':
495 goto failure;
496 break;
497
498 case '=':
499 if (key) {
500 keylen = c - key;
501 st = ST_VALUE;
502 } else {
503 goto failure;
504 }
505 break;
506
507 case ' ':
508 if (key) {
509 keylen = c - key;
510 st = ST_ASSIGN;
511 }
512 break;
513
514 case ';':
515 case '\0':
516 allow_comma:
517 if (key) {
518 keylen = c-- - key;
519 st = ST_ADD;
520 }
521 break;
522
523 default:
524 if (!key) {
525 key = c;
526 }
527 break;
528 }
529 break;
530
531 case ST_ASSIGN:
532 if (*c == '=') {
533 st = ST_VALUE;
534 } else if (!*c || *c == ';' || ((flags & HTTP_PARAMS_ALLOW_COMMA) && *c == ',')) {
535 st = ST_ADD;
536 } else if (*c != ' ') {
537 goto failure;
538 }
539 break;
540
541 case ST_ADD:
542 add:
543 if (val) {
544 vallen = c - val;
545 if (st != ST_QUOTE) {
546 while (val[vallen-1] == ' ') --vallen;
547 }
548 } else {
549 val = "";
550 vallen = 0;
551 }
552
553 cb(cb_arg, key, keylen, val, vallen TSRMLS_CC);
554
555 st = ST_KEY;
556 key = val = NULL;
557 keylen = vallen = 0;
558 break;
559 }
560 if (*c) {
561 ++c;
562 } else if (st == ST_ADD) {
563 goto add;
564 } else {
565 break;
566 }
567 }
568
569 efree(s);
570 return SUCCESS;
571
572 failure:
573 if (flags & HTTP_PARAMS_RAISE_ERROR) {
574 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Unexpected character (%c) at pos %tu of %zu", *c, c-s, strlen(s));
575 }
576 if (flags & HTTP_PARAMS_ALLOW_FAILURE) {
577 --c;
578 st = ST_ADD;
579 goto continued;
580 }
581 efree(s);
582 return FAILURE;
583 }
584 /* }}} */
585
586 /*
587 * Local variables:
588 * tab-width: 4
589 * c-basic-offset: 4
590 * End:
591 * vim600: noet sw=4 ts=4 fdm=marker
592 * vim<600: noet sw=4 ts=4
593 */
594