- add http_parse_params()
[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 {
159 case 301: http_log(HTTP_G->log.redirect, "301-REDIRECT", header); break;
160 case 302: http_log(HTTP_G->log.redirect, "302-REDIRECT", header); break;
161 case 303: http_log(HTTP_G->log.redirect, "303-REDIRECT", header); break;
162 case 305: http_log(HTTP_G->log.redirect, "305-REDIRECT", header); break;
163 case 307: http_log(HTTP_G->log.redirect, "307-REDIRECT", header); break;
164 case 304: http_log(HTTP_G->log.cache, "304-CACHE", header); break;
165 case 405: http_log(HTTP_G->log.allowed_methods, "405-ALLOWED", header); break;
166 default: http_log(NULL, header, body); break;
167 }
168
169 STR_FREE(header);
170 STR_FREE(body);
171
172 if (HTTP_G->force_exit) {
173 zend_bailout();
174 } else {
175 php_ob_set_internal_handler(http_ob_blackhole, 4096, "blackhole", 0 TSRMLS_CC);
176 }
177
178 return SUCCESS;
179 }
180 /* }}} */
181
182 /* {{{ STATUS http_check_method(char *) */
183 STATUS _http_check_method_ex(const char *method, const char *methods)
184 {
185 const char *found;
186
187 if ( (found = strstr(methods, method)) &&
188 (found == method || !isalpha(found[-1])) &&
189 (strlen(found) >= strlen(method) && !isalpha(found[strlen(method)]))) {
190 return SUCCESS;
191 }
192 return FAILURE;
193 }
194 /* }}} */
195
196 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
197 PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC)
198 {
199 zval **hsv;
200 zval **var;
201 #ifdef ZEND_ENGINE_2
202 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
203 #endif
204 if ((SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &hsv)) || (Z_TYPE_PP(hsv) != IS_ARRAY)) {
205 return NULL;
206 }
207 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), (char *) key, key_size, (void *) &var)) || (Z_TYPE_PP(var) != IS_STRING)) {
208 return NULL;
209 }
210 if (check && !(Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
211 return NULL;
212 }
213 return *var;
214 }
215 /* }}} */
216
217 /* {{{ STATUS http_get_request_body(char **, size_t *) */
218 PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_bool dup TSRMLS_DC)
219 {
220 *length = 0;
221 *body = NULL;
222
223 if (SG(request_info).raw_post_data) {
224 *length = SG(request_info).raw_post_data_length;
225 *body = SG(request_info).raw_post_data;
226
227 if (dup) {
228 *body = estrndup(*body, *length);
229 }
230 return SUCCESS;
231 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
232 char buf[4096];
233 int len;
234
235 HTTP_G->read_post_data = 1;
236
237 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
238 *body = erealloc(*body, *length + len + 1);
239 memcpy(*body + *length, buf, len);
240 *length += len;
241 (*body)[*length] = '\0';
242 }
243
244 /* check for error */
245 if (len < 0) {
246 STR_FREE(*body);
247 *length = 0;
248 return FAILURE;
249 }
250
251 SG(request_info).raw_post_data = *body;
252 SG(request_info).raw_post_data_length = *length;
253
254 if (dup) {
255 *body = estrndup(*body, *length);
256 }
257 return SUCCESS;
258 }
259
260 return FAILURE;
261 }
262 /* }}} */
263
264 /* {{{ php_stream *http_get_request_body_stream(void) */
265 PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D)
266 {
267 php_stream *s = NULL;
268
269 if (SG(request_info).raw_post_data) {
270 s = php_stream_open_wrapper("php://input", "rb", 0, NULL);
271 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
272 HTTP_G->read_post_data = 1;
273
274 if ((s = php_stream_temp_new())) {
275 char buf[4096];
276 int len;
277
278 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
279 php_stream_write(s, buf, len);
280 }
281
282 if (len < 0) {
283 php_stream_close(s);
284 s = NULL;
285 } else {
286 php_stream_rewind(s);
287 }
288 }
289 }
290
291 return s;
292 }
293 /* }}} */
294
295 /* {{{ void http_parse_params_default_callback(...) */
296 PHP_HTTP_API void _http_parse_params_default_callback(void *arg, const char *key, int keylen, const char *val, int vallen TSRMLS_DC)
297 {
298 char *kdup;
299 zval tmp, *entry;
300 HashTable *ht = (HashTable *) arg;
301
302 if (ht) {
303 INIT_ZARR(tmp, ht);
304
305 if (vallen) {
306 MAKE_STD_ZVAL(entry);
307 array_init(entry);
308 kdup = estrndup(key, keylen);
309 add_assoc_stringl_ex(entry, kdup, keylen + 1, (char *) val, vallen, 1);
310 efree(kdup);
311 add_next_index_zval(&tmp, entry);
312 } else {
313 add_next_index_stringl(&tmp, (char *) key, keylen, 1);
314 }
315 }
316 }
317 /* }}} */
318
319 /* {{{ STATUS http_parse_params(const char *, HashTable *) */
320 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)
321 {
322 #define ST_QUOTE 1
323 #define ST_VALUE 2
324 #define ST_KEY 3
325 #define ST_ASSIGN 4
326 #define ST_ADD 5
327
328 int st = ST_KEY, keylen = 0, vallen = 0;
329 char *s, *c, *key = NULL, *val = NULL;
330
331 for(c = s = estrdup(param);;) {
332 #if 0
333 char *tk = NULL, *tv = NULL;
334
335 if (key) {
336 if (keylen) {
337 tk= estrndup(key, keylen);
338 } else {
339 tk = ecalloc(1, 7);
340 memcpy(tk, key, 3);
341 tk[3]='.'; tk[4]='.'; tk[5]='.';
342 }
343 }
344 if (val) {
345 if (vallen) {
346 tv = estrndup(val, vallen);
347 } else {
348 tv = ecalloc(1, 7);
349 memcpy(tv, val, 3);
350 tv[3]='.'; tv[4]='.'; tv[5]='.';
351 }
352 }
353 fprintf(stderr, "[%6s] %c \"%s=%s\"\n",
354 (
355 st == ST_QUOTE ? "QUOTE" :
356 st == ST_VALUE ? "VALUE" :
357 st == ST_KEY ? "KEY" :
358 st == ST_ASSIGN ? "ASSIGN" :
359 st == ST_ADD ? "ADD":
360 "HUH?"
361 ), *c, tk, tv
362 );
363 STR_FREE(tk); STR_FREE(tv);
364 #endif
365 switch (st)
366 {
367 case ST_QUOTE:
368 quote:
369 if (*c == '"') {
370 if (*(c-1) == '\\') {
371 memmove(c-1, c, strlen(c)+1);
372 goto quote;
373 } else {
374 goto add;
375 }
376 } else {
377 if (!val) {
378 val = c;
379 }
380 if (!*c) {
381 --val;
382 st = ST_ADD;
383 }
384 }
385 break;
386
387 case ST_VALUE:
388 switch (*c)
389 {
390 case '"':
391 if (!val) {
392 st = ST_QUOTE;
393 }
394 break;
395
396 case ' ':
397 break;
398
399 case ';':
400 case '\0':
401 goto add;
402 break;
403
404 default:
405 if (!val) {
406 val = c;
407 }
408 break;
409 }
410 break;
411
412 case ST_KEY:
413 switch (*c)
414 {
415 case ',':
416 if (allow_comma_sep) {
417 goto allow_comma;
418 }
419 case '\r':
420 case '\n':
421 case '\t':
422 case '\013':
423 case '\014':
424 goto failure;
425 break;
426
427 case '=':
428 if (key) {
429 keylen = c - key;
430 st = ST_VALUE;
431 } else {
432 goto failure;
433 }
434 break;
435
436 case ' ':
437 if (key) {
438 keylen = c - key;
439 st = ST_ASSIGN;
440 }
441 break;
442
443 case ';':
444 case '\0':
445 allow_comma:
446 if (key) {
447 keylen = c - key;
448 st = ST_ADD;
449 }
450 break;
451
452 default:
453 if (!key) {
454 key = c;
455 }
456 break;
457 }
458 break;
459
460 case ST_ASSIGN:
461 if (*c == '=') {
462 st = ST_VALUE;
463 } else if (!*c || *c == ';') {
464 st = ST_ADD;
465 } else if (*c != ' ') {
466 goto failure;
467 }
468 break;
469
470 case ST_ADD:
471 add:
472 if (val) {
473 vallen = c - val;
474 if (st != ST_QUOTE) {
475 while (val[vallen-1] == ' ') --vallen;
476 }
477 } else {
478 val = "";
479 vallen = 0;
480 }
481
482 cb(cb_arg, key, keylen, val, vallen TSRMLS_CC);
483
484 st = ST_KEY;
485 key = val = NULL;
486 keylen = vallen = 0;
487 break;
488 }
489
490 if (*c) {
491 ++c;
492 } else if (st == ST_ADD) {
493 goto add;
494 } else {
495 break;
496 }
497 }
498
499 efree(s);
500 return SUCCESS;
501
502 failure:
503 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Unexpected character (%c) at pos %tu of %zu", *c, c-s, strlen(s));
504 efree(s);
505 return FAILURE;
506 }
507 /* }}} */
508
509 /*
510 * Local variables:
511 * tab-width: 4
512 * c-basic-offset: 4
513 * End:
514 * vim600: noet sw=4 ts=4 fdm=marker
515 * vim<600: noet sw=4 ts=4
516 */
517