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