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