- nested exception order was fine
[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(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 /* {{{ zval *http_exception_wrap(zval *, zval *, zend_class_entry *) */
117 zval *_http_exception_wrap(zval *old_exception, zval *new_exception, zend_class_entry *ce TSRMLS_DC)
118 {
119 zval **args, **trace_0, *old_trace_0, *trace = NULL, *sub_exception, *tmp_exception;
120
121 if (!new_exception) {
122 MAKE_STD_ZVAL(tmp_exception);
123 object_init_ex(tmp_exception, ce);
124 zend_update_property_string(ZEND_EXCEPTION_GET_DEFAULT(), tmp_exception, "message", lenof("message"), "Exception caused by inner exception(s)" TSRMLS_CC);
125 new_exception = tmp_exception;
126 } else {
127 sub_exception = new_exception;
128 tmp_exception = new_exception;
129 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) {
130 sub_exception = tmp_exception;
131 }
132 tmp_exception = new_exception;
133 new_exception = sub_exception;
134 }
135
136 zend_update_property(Z_OBJCE_P(new_exception), new_exception, "innerException", lenof("innerException"), old_exception TSRMLS_CC);
137
138 /* copy bt arguments */
139 if ((trace = zend_read_property(ZEND_EXCEPTION_GET_DEFAULT(), old_exception, "trace", lenof("trace"), 0 TSRMLS_CC))) {
140 if (Z_TYPE_P(trace) == IS_ARRAY && SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void *) &trace_0)) {
141 old_trace_0 = *trace_0;
142 if (Z_TYPE_PP(trace_0) == IS_ARRAY && SUCCESS == zend_hash_find(Z_ARRVAL_PP(trace_0), "args", sizeof("args"), (void *) &args)) {
143 if ((trace = zend_read_property(ZEND_EXCEPTION_GET_DEFAULT(), new_exception, "trace", lenof("trace"), 0 TSRMLS_CC))) {
144 if (Z_TYPE_P(trace) == IS_ARRAY && SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void *) &trace_0)) {
145 ZVAL_ADDREF(*args);
146 add_assoc_zval(*trace_0, "args", *args);
147 }
148 }
149 }
150 }
151 }
152
153 zval_ptr_dtor(&old_exception);
154 return tmp_exception;
155 }
156 /* }}} */
157 #endif /* ZEND_ENGINE_2 */
158
159 /* {{{ void http_log(char *, char *, char *) */
160 void _http_log_ex(char *file, const char *ident, const char *message TSRMLS_DC)
161 {
162 time_t now;
163 struct tm nowtm;
164 char datetime[20] = {0};
165
166 now = HTTP_G->request.time;
167 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
168
169 #define HTTP_LOG_WRITE(file, type, msg) \
170 if (file && *file) { \
171 php_stream *log = php_stream_open_wrapper_ex(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT); \
172 \
173 if (log) { \
174 php_stream_printf(log TSRMLS_CC, "%s\t[%s]\t%s\t<%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
175 php_stream_close(log); \
176 } \
177 \
178 }
179
180 HTTP_LOG_WRITE(file, ident, message);
181 HTTP_LOG_WRITE(HTTP_G->log.composite, ident, message);
182 }
183 /* }}} */
184
185 static void http_ob_blackhole(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
186 {
187 *handled_output = ecalloc(1,1);
188 *handled_output_len = 0;
189 }
190
191 /* {{{ STATUS http_exit(int, char*, char*) */
192 STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header TSRMLS_DC)
193 {
194 if ( (send_header && (SUCCESS != http_send_status_header(status, header))) ||
195 (status && (SUCCESS != http_send_status(status)))) {
196 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Failed to exit with status/header: %d - %s", status, header ? header : "");
197 STR_FREE(header);
198 STR_FREE(body);
199 return FAILURE;
200 }
201
202 if (!OG(ob_lock)) {
203 php_end_ob_buffers(0 TSRMLS_CC);
204 }
205 if ((SUCCESS == sapi_send_headers(TSRMLS_C)) && body) {
206 PHPWRITE(body, strlen(body));
207 }
208
209 switch (status) {
210 case 301: http_log(HTTP_G->log.redirect, "301-REDIRECT", header); break;
211 case 302: http_log(HTTP_G->log.redirect, "302-REDIRECT", header); break;
212 case 303: http_log(HTTP_G->log.redirect, "303-REDIRECT", header); break;
213 case 305: http_log(HTTP_G->log.redirect, "305-REDIRECT", header); break;
214 case 307: http_log(HTTP_G->log.redirect, "307-REDIRECT", header); break;
215 case 304: http_log(HTTP_G->log.cache, "304-CACHE", header); break;
216 case 404: http_log(HTTP_G->log.not_found, "404-NOTFOUND", NULL); break;
217 case 405: http_log(HTTP_G->log.allowed_methods, "405-ALLOWED", header); break;
218 default: http_log(NULL, header, body); break;
219 }
220
221 STR_FREE(header);
222 STR_FREE(body);
223
224 if (HTTP_G->force_exit) {
225 zend_bailout();
226 } else {
227 php_ob_set_internal_handler(http_ob_blackhole, 4096, "blackhole", 0 TSRMLS_CC);
228 }
229
230 return SUCCESS;
231 }
232 /* }}} */
233
234 /* {{{ STATUS http_check_method(char *) */
235 STATUS _http_check_method_ex(const char *method, const char *methods)
236 {
237 const char *found;
238
239 if ( (found = strstr(methods, method)) &&
240 (found == method || !isalpha(found[-1])) &&
241 (strlen(found) >= strlen(method) && !isalpha(found[strlen(method)]))) {
242 return SUCCESS;
243 }
244 return FAILURE;
245 }
246 /* }}} */
247
248 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
249 PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC)
250 {
251 zval **hsv;
252 zval **var;
253 #ifdef ZEND_ENGINE_2
254 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
255 #endif
256 if ((SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &hsv)) || (Z_TYPE_PP(hsv) != IS_ARRAY)) {
257 return NULL;
258 }
259 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), (char *) key, key_size, (void *) &var))) {
260 return NULL;
261 }
262 if (check && !((Z_TYPE_PP(var) == IS_STRING) && Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
263 return NULL;
264 }
265 return *var;
266 }
267 /* }}} */
268
269 /* {{{ STATUS http_get_request_body(char **, size_t *) */
270 PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_bool dup TSRMLS_DC)
271 {
272 *length = 0;
273 *body = NULL;
274
275 if (SG(request_info).raw_post_data) {
276 *length = SG(request_info).raw_post_data_length;
277 *body = SG(request_info).raw_post_data;
278
279 if (dup) {
280 *body = estrndup(*body, *length);
281 }
282 return SUCCESS;
283 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
284 char buf[4096];
285 int len;
286
287 HTTP_G->read_post_data = 1;
288
289 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
290 *body = erealloc(*body, *length + len + 1);
291 memcpy(*body + *length, buf, len);
292 *length += len;
293 (*body)[*length] = '\0';
294 }
295
296 /* check for error */
297 if (len < 0) {
298 STR_FREE(*body);
299 *length = 0;
300 return FAILURE;
301 }
302
303 SG(request_info).raw_post_data = *body;
304 SG(request_info).raw_post_data_length = *length;
305
306 if (dup) {
307 *body = estrndup(*body, *length);
308 }
309 return SUCCESS;
310 }
311
312 return FAILURE;
313 }
314 /* }}} */
315
316 /* {{{ php_stream *http_get_request_body_stream(void) */
317 PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D)
318 {
319 php_stream *s = NULL;
320
321 if (SG(request_info).raw_post_data) {
322 s = php_stream_open_wrapper("php://input", "rb", 0, NULL);
323 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
324 HTTP_G->read_post_data = 1;
325
326 if ((s = php_stream_temp_new())) {
327 char buf[4096];
328 int len;
329
330 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
331 php_stream_write(s, buf, len);
332 }
333
334 if (len < 0) {
335 php_stream_close(s);
336 s = NULL;
337 } else {
338 php_stream_rewind(s);
339 }
340 }
341 }
342
343 return s;
344 }
345 /* }}} */
346
347 /* {{{ void http_parse_params_default_callback(...) */
348 PHP_HTTP_API void _http_parse_params_default_callback(void *arg, const char *key, int keylen, const char *val, int vallen TSRMLS_DC)
349 {
350 char *kdup;
351 zval tmp, *entry;
352 HashTable *ht = (HashTable *) arg;
353
354 if (ht) {
355 INIT_ZARR(tmp, ht);
356
357 if (vallen) {
358 MAKE_STD_ZVAL(entry);
359 array_init(entry);
360 kdup = estrndup(key, keylen);
361 add_assoc_stringl_ex(entry, kdup, keylen + 1, (char *) val, vallen, 1);
362 efree(kdup);
363 add_next_index_zval(&tmp, entry);
364 } else {
365 add_next_index_stringl(&tmp, (char *) key, keylen, 1);
366 }
367 }
368 }
369 /* }}} */
370
371 /* {{{ STATUS http_parse_params(const char *, HashTable *) */
372 PHP_HTTP_API STATUS _http_parse_params_ex(const char *param, int flags, http_parse_params_callback cb, void *cb_arg TSRMLS_DC)
373 {
374 #define ST_QUOTE 1
375 #define ST_VALUE 2
376 #define ST_KEY 3
377 #define ST_ASSIGN 4
378 #define ST_ADD 5
379
380 int st = ST_KEY, keylen = 0, vallen = 0;
381 char *s, *c, *key = NULL, *val = NULL;
382
383 for(c = s = estrdup(param);;) {
384 #if 0
385 char *tk = NULL, *tv = NULL;
386
387 if (key) {
388 if (keylen) {
389 tk= estrndup(key, keylen);
390 } else {
391 tk = ecalloc(1, 7);
392 memcpy(tk, key, 3);
393 tk[3]='.'; tk[4]='.'; tk[5]='.';
394 }
395 }
396 if (val) {
397 if (vallen) {
398 tv = estrndup(val, vallen);
399 } else {
400 tv = ecalloc(1, 7);
401 memcpy(tv, val, 3);
402 tv[3]='.'; tv[4]='.'; tv[5]='.';
403 }
404 }
405 fprintf(stderr, "[%6s] %c \"%s=%s\"\n",
406 (
407 st == ST_QUOTE ? "QUOTE" :
408 st == ST_VALUE ? "VALUE" :
409 st == ST_KEY ? "KEY" :
410 st == ST_ASSIGN ? "ASSIGN" :
411 st == ST_ADD ? "ADD":
412 "HUH?"
413 ), *c?*c:'0', tk, tv
414 );
415 STR_FREE(tk); STR_FREE(tv);
416 #endif
417 continued:
418 switch (st) {
419 case ST_QUOTE:
420 quote:
421 if (*c == '"') {
422 if (*(c-1) == '\\') {
423 memmove(c-1, c, strlen(c)+1);
424 goto quote;
425 } else {
426 goto add;
427 }
428 } else {
429 if (!val) {
430 val = c;
431 }
432 if (!*c) {
433 --val;
434 st = ST_ADD;
435 }
436 }
437 break;
438
439 case ST_VALUE:
440 switch (*c) {
441 case '"':
442 if (!val) {
443 st = ST_QUOTE;
444 }
445 break;
446
447 case ' ':
448 break;
449
450 case ';':
451 case '\0':
452 goto add;
453 break;
454 case ',':
455 if (flags & HTTP_PARAMS_ALLOW_COMMA) {
456 goto add;
457 }
458 default:
459 if (!val) {
460 val = c;
461 }
462 break;
463 }
464 break;
465
466 case ST_KEY:
467 switch (*c) {
468 case ',':
469 if (flags & HTTP_PARAMS_ALLOW_COMMA) {
470 goto allow_comma;
471 }
472 case '\r':
473 case '\n':
474 case '\t':
475 case '\013':
476 case '\014':
477 goto failure;
478 break;
479
480 case '=':
481 if (key) {
482 keylen = c - key;
483 st = ST_VALUE;
484 } else {
485 goto failure;
486 }
487 break;
488
489 case ' ':
490 if (key) {
491 keylen = c - key;
492 st = ST_ASSIGN;
493 }
494 break;
495
496 case ';':
497 case '\0':
498 allow_comma:
499 if (key) {
500 keylen = c-- - key;
501 st = ST_ADD;
502 }
503 break;
504
505 default:
506 if (!key) {
507 key = c;
508 }
509 break;
510 }
511 break;
512
513 case ST_ASSIGN:
514 if (*c == '=') {
515 st = ST_VALUE;
516 } else if (!*c || *c == ';' || ((flags & HTTP_PARAMS_ALLOW_COMMA) && *c == ',')) {
517 st = ST_ADD;
518 } else if (*c != ' ') {
519 goto failure;
520 }
521 break;
522
523 case ST_ADD:
524 add:
525 if (val) {
526 vallen = c - val;
527 if (st != ST_QUOTE) {
528 while (val[vallen-1] == ' ') --vallen;
529 }
530 } else {
531 val = "";
532 vallen = 0;
533 }
534
535 cb(cb_arg, key, keylen, val, vallen TSRMLS_CC);
536
537 st = ST_KEY;
538 key = val = NULL;
539 keylen = vallen = 0;
540 break;
541 }
542 if (*c) {
543 ++c;
544 } else if (st == ST_ADD) {
545 goto add;
546 } else {
547 break;
548 }
549 }
550
551 efree(s);
552 return SUCCESS;
553
554 failure:
555 if (flags & HTTP_PARAMS_RAISE_ERROR) {
556 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Unexpected character (%c) at pos %tu of %zu", *c, c-s, strlen(s));
557 }
558 if (flags & HTTP_PARAMS_ALLOW_FAILURE) {
559 --c;
560 st = ST_ADD;
561 goto continued;
562 }
563 efree(s);
564 return FAILURE;
565 }
566 /* }}} */
567
568 /*
569 * Local variables:
570 * tab-width: 4
571 * c-basic-offset: 4
572 * End:
573 * vim600: noet sw=4 ts=4 fdm=marker
574 * vim<600: noet sw=4 ts=4
575 */
576