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