4744a827f32fffd80e88d3824bfada3f081621f4
[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 return SUCCESS;
37 }
38
39 PHP_HTTP_API long _http_support(long feature)
40 {
41 long support = HTTP_SUPPORT;
42
43 #ifdef HTTP_HAVE_CURL
44 support |= HTTP_SUPPORT_REQUESTS;
45 # ifdef HTTP_HAVE_SSL
46 support |= HTTP_SUPPORT_SSLREQUESTS;
47 # endif
48 #endif
49 #ifdef HTTP_HAVE_MAGIC
50 support |= HTTP_SUPPORT_MAGICMIME;
51 #endif
52 #ifdef HTTP_HAVE_ZLIB
53 support |= HTTP_SUPPORT_ENCODINGS;
54 #endif
55
56 if (feature) {
57 return (feature == (support & feature));
58 }
59 return support;
60 }
61
62 /* char *pretty_key(char *, size_t, zend_bool, zend_bool) */
63 char *_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
64 {
65 if (key && key_len) {
66 size_t i;
67 int wasalpha;
68 if ((wasalpha = isalpha((int) key[0]))) {
69 key[0] = (char) (uctitle ? toupper((int) key[0]) : tolower((int) key[0]));
70 }
71 for (i = 1; i < key_len; i++) {
72 if (isalpha((int) key[i])) {
73 key[i] = (char) (((!wasalpha) && uctitle) ? toupper((int) key[i]) : tolower((int) key[i]));
74 wasalpha = 1;
75 } else {
76 if (xhyphen && (key[i] == '_')) {
77 key[i] = '-';
78 }
79 wasalpha = 0;
80 }
81 }
82 }
83 return key;
84 }
85 /* }}} */
86
87 /* {{{ void http_error(long, long, char*) */
88 void _http_error_ex(long type TSRMLS_DC, long code, const char *format, ...)
89 {
90 va_list args;
91
92 va_start(args, format);
93 #ifdef ZEND_ENGINE_2
94 if ((type == E_THROW) || (PG(error_handling) == EH_THROW)) {
95 char *message;
96
97 vspprintf(&message, 0, format, args);
98 zend_throw_exception(http_exception_get_for_code(code), message, code TSRMLS_CC);
99 efree(message);
100 } else
101 #endif
102 php_verror(NULL, "", type, format, args TSRMLS_CC);
103 va_end(args);
104 }
105 /* }}} */
106
107 /* {{{ void http_log(char *, char *, char *) */
108 void _http_log_ex(char *file, const char *ident, const char *message TSRMLS_DC)
109 {
110 time_t now;
111 struct tm nowtm;
112 char datetime[20] = {0};
113
114 now = HTTP_GET_REQUEST_TIME();
115 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
116
117 #define HTTP_LOG_WRITE(file, type, msg) \
118 if (file && *file) { \
119 php_stream *log = php_stream_open_wrapper_ex(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT); \
120 \
121 if (log) { \
122 php_stream_printf(log TSRMLS_CC, "%s\t[%s]\t%s\t<%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
123 php_stream_close(log); \
124 } \
125 \
126 }
127
128 HTTP_LOG_WRITE(file, ident, message);
129 HTTP_LOG_WRITE(HTTP_G->log.composite, ident, message);
130 }
131 /* }}} */
132
133 static void http_ob_blackhole(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
134 {
135 *handled_output = ecalloc(1,1);
136 *handled_output_len = 0;
137 }
138
139 /* {{{ STATUS http_exit(int, char*, char*) */
140 STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header TSRMLS_DC)
141 {
142 if ( (send_header && (SUCCESS != http_send_status_header(status, header))) ||
143 (!send_header && status && (SUCCESS != http_send_status(status)))) {
144 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Failed to exit with status/header: %d - %s", status, header ? header : "");
145 STR_FREE(header);
146 STR_FREE(body);
147 return FAILURE;
148 }
149
150 if (!OG(ob_lock)) {
151 php_end_ob_buffers(0 TSRMLS_CC);
152 }
153 if ((SUCCESS == sapi_send_headers(TSRMLS_C)) && body) {
154 PHPWRITE(body, strlen(body));
155 }
156
157 switch (status) {
158 case 301: http_log(HTTP_G->log.redirect, "301-REDIRECT", header); break;
159 case 302: http_log(HTTP_G->log.redirect, "302-REDIRECT", header); break;
160 case 303: http_log(HTTP_G->log.redirect, "303-REDIRECT", header); break;
161 case 305: http_log(HTTP_G->log.redirect, "305-REDIRECT", header); break;
162 case 307: http_log(HTTP_G->log.redirect, "307-REDIRECT", header); break;
163 case 304: http_log(HTTP_G->log.cache, "304-CACHE", header); break;
164 case 405: http_log(HTTP_G->log.allowed_methods, "405-ALLOWED", header); break;
165 default: http_log(NULL, header, body); break;
166 }
167
168 STR_FREE(header);
169 STR_FREE(body);
170
171 if (HTTP_G->force_exit) {
172 zend_bailout();
173 } else {
174 php_ob_set_internal_handler(http_ob_blackhole, 4096, "blackhole", 0 TSRMLS_CC);
175 }
176
177 return SUCCESS;
178 }
179 /* }}} */
180
181 /* {{{ STATUS http_check_method(char *) */
182 STATUS _http_check_method_ex(const char *method, const char *methods)
183 {
184 const char *found;
185
186 if ( (found = strstr(methods, method)) &&
187 (found == method || !isalpha(found[-1])) &&
188 (strlen(found) >= strlen(method) && !isalpha(found[strlen(method)]))) {
189 return SUCCESS;
190 }
191 return FAILURE;
192 }
193 /* }}} */
194
195 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
196 PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC)
197 {
198 zval **hsv;
199 zval **var;
200 #ifdef ZEND_ENGINE_2
201 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
202 #endif
203 if ((SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &hsv)) || (Z_TYPE_PP(hsv) != IS_ARRAY)) {
204 return NULL;
205 }
206 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), (char *) key, key_size, (void *) &var)) || (Z_TYPE_PP(var) != IS_STRING)) {
207 return NULL;
208 }
209 if (check && !(Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
210 return NULL;
211 }
212 return *var;
213 }
214 /* }}} */
215
216 /* {{{ STATUS http_get_request_body(char **, size_t *) */
217 PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_bool dup TSRMLS_DC)
218 {
219 *length = 0;
220 *body = NULL;
221
222 if (SG(request_info).raw_post_data) {
223 *length = SG(request_info).raw_post_data_length;
224 *body = SG(request_info).raw_post_data;
225
226 if (dup) {
227 *body = estrndup(*body, *length);
228 }
229 return SUCCESS;
230 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
231 char buf[4096];
232 int len;
233
234 HTTP_G->read_post_data = 1;
235
236 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
237 *body = erealloc(*body, *length + len + 1);
238 memcpy(*body + *length, buf, len);
239 *length += len;
240 (*body)[*length] = '\0';
241 }
242
243 /* check for error */
244 if (len < 0) {
245 STR_FREE(*body);
246 *length = 0;
247 return FAILURE;
248 }
249
250 SG(request_info).raw_post_data = *body;
251 SG(request_info).raw_post_data_length = *length;
252
253 if (dup) {
254 *body = estrndup(*body, *length);
255 }
256 return SUCCESS;
257 }
258
259 return FAILURE;
260 }
261 /* }}} */
262
263 /* {{{ php_stream *http_get_request_body_stream(void) */
264 PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D)
265 {
266 php_stream *s = NULL;
267
268 if (SG(request_info).raw_post_data) {
269 s = php_stream_open_wrapper("php://input", "rb", 0, NULL);
270 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
271 HTTP_G->read_post_data = 1;
272
273 if ((s = php_stream_temp_new())) {
274 char buf[4096];
275 int len;
276
277 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
278 php_stream_write(s, buf, len);
279 }
280
281 if (len < 0) {
282 php_stream_close(s);
283 s = NULL;
284 } else {
285 php_stream_rewind(s);
286 }
287 }
288 }
289
290 return s;
291 }
292 /* }}} */
293
294 /* {{{ void http_parse_params_default_callback(...) */
295 PHP_HTTP_API void _http_parse_params_default_callback(void *arg, const char *key, int keylen, const char *val, int vallen TSRMLS_DC)
296 {
297 char *kdup;
298 zval tmp, *entry;
299 HashTable *ht = (HashTable *) arg;
300
301 if (ht) {
302 INIT_ZARR(tmp, ht);
303
304 if (vallen) {
305 MAKE_STD_ZVAL(entry);
306 array_init(entry);
307 kdup = estrndup(key, keylen);
308 add_assoc_stringl_ex(entry, kdup, keylen + 1, (char *) val, vallen, 1);
309 efree(kdup);
310 add_next_index_zval(&tmp, entry);
311 } else {
312 add_next_index_stringl(&tmp, (char *) key, keylen, 1);
313 }
314 }
315 }
316 /* }}} */
317
318 /* {{{ STATUS http_parse_params(const char *, HashTable *) */
319 PHP_HTTP_API STATUS _http_parse_params_ex(const char *param, int allow_comma_sep, http_parse_params_callback cb, void *cb_arg TSRMLS_DC)
320 {
321 #define ST_QUOTE 1
322 #define ST_VALUE 2
323 #define ST_KEY 3
324 #define ST_ASSIGN 4
325 #define ST_ADD 5
326
327 int st = ST_KEY, keylen = 0, vallen = 0;
328 char *s, *c, *key = NULL, *val = NULL;
329
330 for(c = s = estrdup(param);;) {
331 #if 0
332 char *tk = NULL, *tv = NULL;
333
334 if (key) {
335 if (keylen) {
336 tk= estrndup(key, keylen);
337 } else {
338 tk = ecalloc(1, 7);
339 memcpy(tk, key, 3);
340 tk[3]='.'; tk[4]='.'; tk[5]='.';
341 }
342 }
343 if (val) {
344 if (vallen) {
345 tv = estrndup(val, vallen);
346 } else {
347 tv = ecalloc(1, 7);
348 memcpy(tv, val, 3);
349 tv[3]='.'; tv[4]='.'; tv[5]='.';
350 }
351 }
352 fprintf(stderr, "[%6s] %c \"%s=%s\"\n",
353 (
354 st == ST_QUOTE ? "QUOTE" :
355 st == ST_VALUE ? "VALUE" :
356 st == ST_KEY ? "KEY" :
357 st == ST_ASSIGN ? "ASSIGN" :
358 st == ST_ADD ? "ADD":
359 "HUH?"
360 ), *c, tk, tv
361 );
362 STR_FREE(tk); STR_FREE(tv);
363 #endif
364 switch (st) {
365 case ST_QUOTE:
366 quote:
367 if (*c == '"') {
368 if (*(c-1) == '\\') {
369 memmove(c-1, c, strlen(c)+1);
370 goto quote;
371 } else {
372 goto add;
373 }
374 } else {
375 if (!val) {
376 val = c;
377 }
378 if (!*c) {
379 --val;
380 st = ST_ADD;
381 }
382 }
383 break;
384
385 case ST_VALUE:
386 switch (*c) {
387 case '"':
388 if (!val) {
389 st = ST_QUOTE;
390 }
391 break;
392
393 case ' ':
394 break;
395
396 case ';':
397 case '\0':
398 goto add;
399 break;
400
401 default:
402 if (!val) {
403 val = c;
404 }
405 break;
406 }
407 break;
408
409 case ST_KEY:
410 switch (*c) {
411 case ',':
412 if (allow_comma_sep) {
413 goto allow_comma;
414 }
415 case '\r':
416 case '\n':
417 case '\t':
418 case '\013':
419 case '\014':
420 goto failure;
421 break;
422
423 case '=':
424 if (key) {
425 keylen = c - key;
426 st = ST_VALUE;
427 } else {
428 goto failure;
429 }
430 break;
431
432 case ' ':
433 if (key) {
434 keylen = c - key;
435 st = ST_ASSIGN;
436 }
437 break;
438
439 case ';':
440 case '\0':
441 allow_comma:
442 if (key) {
443 keylen = c - key;
444 st = ST_ADD;
445 }
446 break;
447
448 default:
449 if (!key) {
450 key = c;
451 }
452 break;
453 }
454 break;
455
456 case ST_ASSIGN:
457 if (*c == '=') {
458 st = ST_VALUE;
459 } else if (!*c || *c == ';') {
460 st = ST_ADD;
461 } else if (*c != ' ') {
462 goto failure;
463 }
464 break;
465
466 case ST_ADD:
467 add:
468 if (val) {
469 vallen = c - val;
470 if (st != ST_QUOTE) {
471 while (val[vallen-1] == ' ') --vallen;
472 }
473 } else {
474 val = "";
475 vallen = 0;
476 }
477
478 cb(cb_arg, key, keylen, val, vallen TSRMLS_CC);
479
480 st = ST_KEY;
481 key = val = NULL;
482 keylen = vallen = 0;
483 break;
484 }
485
486 if (*c) {
487 ++c;
488 } else if (st == ST_ADD) {
489 goto add;
490 } else {
491 break;
492 }
493 }
494
495 efree(s);
496 return SUCCESS;
497
498 failure:
499 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Unexpected character (%c) at pos %tu of %zu", *c, c-s, strlen(s));
500 efree(s);
501 return FAILURE;
502 }
503 /* }}} */
504
505 /*
506 * Local variables:
507 * tab-width: 4
508 * c-basic-offset: 4
509 * End:
510 * vim600: noet sw=4 ts=4 fdm=marker
511 * vim<600: noet sw=4 ts=4
512 */
513