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