2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
15 #define HTTP_WANT_SAPI
18 #include "php_output.h"
19 #include "ext/standard/url.h"
21 #include "php_http_api.h"
22 #include "php_http_send_api.h"
25 # include "php_http_exception_object.h"
28 PHP_MINIT_FUNCTION(http_support
)
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
);
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
);
44 PHP_HTTP_API
long _http_support(long feature
)
46 long support
= HTTP_SUPPORT
;
49 support
|= HTTP_SUPPORT_REQUESTS
;
51 support
|= HTTP_SUPPORT_SSLREQUESTS
;
54 #ifdef HTTP_HAVE_MAGIC
55 support
|= HTTP_SUPPORT_MAGICMIME
;
58 support
|= HTTP_SUPPORT_ENCODINGS
;
62 return (feature
== (support
& feature
));
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
)
74 if ((wasalpha
= HTTP_IS_CTYPE(alpha
, key
[0]))) {
75 key
[0] = (char) (uctitle
? HTTP_TO_CTYPE(upper
, key
[0]) : HTTP_TO_CTYPE(lower
, key
[0]));
77 for (i
= 1; i
< key_len
; i
++) {
78 if (HTTP_IS_CTYPE(alpha
, key
[i
])) {
79 key
[i
] = (char) (((!wasalpha
) && uctitle
) ? HTTP_TO_CTYPE(upper
, key
[i
]) : HTTP_TO_CTYPE(lower
, key
[i
]));
82 if (xhyphen
&& (key
[i
] == '_')) {
93 /* {{{ void http_error(long, long, char*) */
94 void _http_error_ex(long type TSRMLS_DC
, long code
, const char *format
, ...)
98 va_start(args
, format
);
100 if ((type
== E_THROW
) || (PG(error_handling
) == EH_THROW
)) {
102 zend_class_entry
*ce
= http_exception_get_for_code(code
);
105 vspprintf(&message
, 0, format
, args
);
106 zend_throw_exception(ce
, message
, code TSRMLS_CC
);
108 } http_catch(PG(exception_class
) ? PG(exception_class
) : HTTP_EX_DEF_CE
);
111 php_verror(NULL
, "", type
, format
, args TSRMLS_CC
);
117 static inline void copy_bt_args(zval
*from
, zval
*to TSRMLS_DC
)
119 zval
**args
, **trace_0
, *old_trace_0
, *trace
= NULL
;
121 if ((trace
= zend_read_property(ZEND_EXCEPTION_GET_DEFAULT(), from
, "trace", lenof("trace"), 0 TSRMLS_CC
))) {
122 if (Z_TYPE_P(trace
) == IS_ARRAY
&& SUCCESS
== zend_hash_index_find(Z_ARRVAL_P(trace
), 0, (void *) &trace_0
)) {
123 old_trace_0
= *trace_0
;
124 if (Z_TYPE_PP(trace_0
) == IS_ARRAY
&& SUCCESS
== zend_hash_find(Z_ARRVAL_PP(trace_0
), "args", sizeof("args"), (void *) &args
)) {
125 if ((trace
= zend_read_property(ZEND_EXCEPTION_GET_DEFAULT(), to
, "trace", lenof("trace"), 0 TSRMLS_CC
))) {
126 if (Z_TYPE_P(trace
) == IS_ARRAY
&& SUCCESS
== zend_hash_index_find(Z_ARRVAL_P(trace
), 0, (void *) &trace_0
)) {
128 add_assoc_zval(*trace_0
, "args", *args
);
136 /* {{{ zval *http_exception_wrap(zval *, zval *, zend_class_entry *) */
137 zval
*_http_exception_wrap(zval
*old_exception
, zval
*new_exception
, zend_class_entry
*ce TSRMLS_DC
)
141 zval
*sub_exception
, *tmp_exception
;
143 if (!new_exception
) {
144 MAKE_STD_ZVAL(new_exception
);
145 object_init_ex(new_exception
, ce
);
147 zend_update_property(ce
, new_exception
, "innerException", lenof("innerException"), old_exception TSRMLS_CC
);
148 copy_bt_args(old_exception
, new_exception TSRMLS_CC
);
150 sub_exception
= old_exception
;
152 while ((sub_exception
= zend_read_property(Z_OBJCE_P(sub_exception
), sub_exception
, "innerException", lenof("innerException"), 0 TSRMLS_CC
)) && Z_TYPE_P(sub_exception
) == IS_OBJECT
) {
156 spprintf(&message
, 0, "Exception caused by %d inner exception(s)", inner
);
157 zend_update_property_string(ZEND_EXCEPTION_GET_DEFAULT(), new_exception
, "message", lenof("message"), message TSRMLS_CC
);
160 sub_exception
= new_exception
;
161 tmp_exception
= new_exception
;
163 while ((tmp_exception
= zend_read_property(Z_OBJCE_P(tmp_exception
), tmp_exception
, "innerException", lenof("innerException"), 0 TSRMLS_CC
)) && Z_TYPE_P(tmp_exception
) == IS_OBJECT
) {
164 sub_exception
= tmp_exception
;
167 zend_update_property(Z_OBJCE_P(sub_exception
), sub_exception
, "innerException", lenof("innerException"), old_exception TSRMLS_CC
);
168 copy_bt_args(old_exception
, new_exception TSRMLS_CC
);
169 copy_bt_args(old_exception
, sub_exception TSRMLS_CC
);
172 zval_ptr_dtor(&old_exception
);
173 return new_exception
;
176 #endif /* ZEND_ENGINE_2 */
178 /* {{{ void http_log(char *, char *, char *) */
179 void _http_log_ex(char *file
, const char *ident
, const char *message TSRMLS_DC
)
183 char datetime
[20] = {0};
185 now
= HTTP_G
->request
.time
;
186 strftime(datetime
, sizeof(datetime
), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now
, &nowtm
));
188 #define HTTP_LOG_WRITE(file, type, msg) \
189 if (file && *file) { \
190 php_stream *log = php_stream_open_wrapper_ex(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT); \
193 php_stream_printf(log TSRMLS_CC, "%s\t[%s]\t%s\t<%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
194 php_stream_close(log); \
199 HTTP_LOG_WRITE(file
, ident
, message
);
200 HTTP_LOG_WRITE(HTTP_G
->log
.composite
, ident
, message
);
204 static void http_ob_blackhole(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
206 *handled_output
= ecalloc(1,1);
207 *handled_output_len
= 0;
210 /* {{{ STATUS http_exit(int, char*, char*) */
211 STATUS
_http_exit_ex(int status
, char *header
, char *body
, zend_bool send_header TSRMLS_DC
)
213 if ( (send_header
&& (SUCCESS
!= http_send_status_header(status
, header
))) ||
214 (status
&& (SUCCESS
!= http_send_status(status
)))) {
215 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Failed to exit with status/header: %d - %s", status
, header
? header
: "");
222 php_end_ob_buffers(0 TSRMLS_CC
);
224 if ((SUCCESS
== sapi_send_headers(TSRMLS_C
)) && body
) {
225 PHPWRITE(body
, strlen(body
));
229 case 301: http_log(HTTP_G
->log
.redirect
, "301-REDIRECT", header
); break;
230 case 302: http_log(HTTP_G
->log
.redirect
, "302-REDIRECT", header
); break;
231 case 303: http_log(HTTP_G
->log
.redirect
, "303-REDIRECT", header
); break;
232 case 305: http_log(HTTP_G
->log
.redirect
, "305-REDIRECT", header
); break;
233 case 307: http_log(HTTP_G
->log
.redirect
, "307-REDIRECT", header
); break;
234 case 304: http_log(HTTP_G
->log
.cache
, "304-CACHE", header
); break;
235 case 404: http_log(HTTP_G
->log
.not_found
, "404-NOTFOUND", NULL
); break;
236 case 405: http_log(HTTP_G
->log
.allowed_methods
, "405-ALLOWED", header
); break;
237 default: http_log(NULL
, header
, body
); break;
243 if (HTTP_G
->force_exit
) {
246 php_ob_set_internal_handler(http_ob_blackhole
, 4096, "blackhole", 0 TSRMLS_CC
);
253 /* {{{ STATUS http_check_method(char *) */
254 STATUS
_http_check_method_ex(const char *method
, const char *methods
)
258 if ( (found
= strstr(methods
, method
)) &&
259 (found
== method
|| !HTTP_IS_CTYPE(alpha
, found
[-1])) &&
260 (strlen(found
) >= strlen(method
) && !HTTP_IS_CTYPE(alpha
, found
[strlen(method
)]))) {
267 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
268 PHP_HTTP_API zval
*_http_get_server_var_ex(const char *key
, size_t key_size
, zend_bool check TSRMLS_DC
)
273 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC
);
275 if ((SUCCESS
!= zend_hash_find(&EG(symbol_table
), "_SERVER", sizeof("_SERVER"), (void *) &hsv
)) || (Z_TYPE_PP(hsv
) != IS_ARRAY
)) {
278 if ((SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(hsv
), (char *) key
, key_size
, (void *) &var
))) {
281 if (check
&& !((Z_TYPE_PP(var
) == IS_STRING
) && Z_STRVAL_PP(var
) && Z_STRLEN_PP(var
))) {
288 /* {{{ STATUS http_get_request_body(char **, size_t *) */
289 PHP_HTTP_API STATUS
_http_get_request_body_ex(char **body
, size_t *length
, zend_bool dup TSRMLS_DC
)
294 if (SG(request_info
).raw_post_data
) {
295 *length
= SG(request_info
).raw_post_data_length
;
296 *body
= SG(request_info
).raw_post_data
;
299 *body
= estrndup(*body
, *length
);
302 } else if (sapi_module
.read_post
&& !HTTP_G
->read_post_data
) {
306 HTTP_G
->read_post_data
= 1;
308 while (0 < (len
= sapi_module
.read_post(buf
, sizeof(buf
) TSRMLS_CC
))) {
309 *body
= erealloc(*body
, *length
+ len
+ 1);
310 memcpy(*body
+ *length
, buf
, len
);
312 (*body
)[*length
] = '\0';
315 /* check for error */
322 SG(request_info
).raw_post_data
= *body
;
323 SG(request_info
).raw_post_data_length
= *length
;
326 *body
= estrndup(*body
, *length
);
335 /* {{{ php_stream *http_get_request_body_stream(void) */
336 PHP_HTTP_API php_stream
*_http_get_request_body_stream(TSRMLS_D
)
338 php_stream
*s
= NULL
;
340 if (SG(request_info
).raw_post_data
) {
341 s
= php_stream_open_wrapper("php://input", "rb", 0, NULL
);
342 } else if (sapi_module
.read_post
&& !HTTP_G
->read_post_data
) {
343 HTTP_G
->read_post_data
= 1;
345 if ((s
= php_stream_temp_new())) {
349 while (0 < (len
= sapi_module
.read_post(buf
, sizeof(buf
) TSRMLS_CC
))) {
350 php_stream_write(s
, buf
, len
);
357 php_stream_rewind(s
);
366 /* {{{ void http_parse_params_default_callback(...) */
367 PHP_HTTP_API
void _http_parse_params_default_callback(void *arg
, const char *key
, int keylen
, const char *val
, int vallen TSRMLS_DC
)
371 HashTable
*ht
= (HashTable
*) arg
;
377 MAKE_STD_ZVAL(entry
);
379 kdup
= estrndup(key
, keylen
);
380 add_assoc_stringl_ex(entry
, kdup
, keylen
+ 1, (char *) val
, vallen
, 1);
382 add_next_index_zval(&tmp
, entry
);
384 add_next_index_stringl(&tmp
, (char *) key
, keylen
, 1);
390 /* {{{ STATUS http_parse_params(const char *, HashTable *) */
391 PHP_HTTP_API STATUS
_http_parse_params_ex(const char *param
, int flags
, http_parse_params_callback cb
, void *cb_arg TSRMLS_DC
)
399 int st
= ST_KEY
, keylen
= 0, vallen
= 0;
400 char *s
, *c
, *key
= NULL
, *val
= NULL
;
402 for(c
= s
= estrdup(param
);;) {
404 char *tk
= NULL
, *tv
= NULL
;
408 tk
= estrndup(key
, keylen
);
412 tk
[3]='.'; tk
[4]='.'; tk
[5]='.';
417 tv
= estrndup(val
, vallen
);
421 tv
[3]='.'; tv
[4]='.'; tv
[5]='.';
424 fprintf(stderr
, "[%6s] %c \"%s=%s\"\n",
426 st
== ST_QUOTE
? "QUOTE" :
427 st
== ST_VALUE
? "VALUE" :
428 st
== ST_KEY
? "KEY" :
429 st
== ST_ASSIGN
? "ASSIGN" :
430 st
== ST_ADD
? "ADD":
434 STR_FREE(tk
); STR_FREE(tv
);
441 if (*(c
-1) == '\\') {
442 memmove(c
-1, c
, strlen(c
)+1);
474 if (flags
& HTTP_PARAMS_ALLOW_COMMA
) {
488 if (flags
& HTTP_PARAMS_ALLOW_COMMA
) {
535 } else if (!*c
|| *c
== ';' || ((flags
& HTTP_PARAMS_ALLOW_COMMA
) && *c
== ',')) {
537 } else if (*c
!= ' ') {
546 if (st
!= ST_QUOTE
) {
547 while (val
[vallen
-1] == ' ') --vallen
;
554 cb(cb_arg
, key
, keylen
, val
, vallen TSRMLS_CC
);
563 } else if (st
== ST_ADD
) {
574 if (flags
& HTTP_PARAMS_RAISE_ERROR
) {
575 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Unexpected character (%c) at pos %tu of %zu", *c
, c
-s
, strlen(s
));
577 if (flags
& HTTP_PARAMS_ALLOW_FAILURE
) {
592 * vim600: noet sw=4 ts=4 fdm=marker
593 * vim<600: noet sw=4 ts=4