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