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
);
39 PHP_HTTP_API
long _http_support(long feature
)
41 long support
= HTTP_SUPPORT
;
44 support
|= HTTP_SUPPORT_REQUESTS
;
46 support
|= HTTP_SUPPORT_SSLREQUESTS
;
49 #ifdef HTTP_HAVE_MAGIC
50 support
|= HTTP_SUPPORT_MAGICMIME
;
53 support
|= HTTP_SUPPORT_ENCODINGS
;
57 return (feature
== (support
& feature
));
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
)
68 if ((wasalpha
= isalpha((int) key
[0]))) {
69 key
[0] = (char) (uctitle
? toupper((int) key
[0]) : tolower((int) key
[0]));
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
]));
76 if (xhyphen
&& (key
[i
] == '_')) {
87 /* {{{ STATUS http_parse_cookie(char *, HashTable *) */
88 PHP_HTTP_API STATUS
_http_parse_cookie(const char *list
, HashTable
*items TSRMLS_DC
)
97 int first
= 1, st
= ST_KEY
, keylen
= 0, vallen
= 0;
98 char *s
, *c
, *key
= NULL
, *val
= NULL
;
100 INIT_ZARR(array
, items
);
102 c
= s
= estrdup(list
);
105 char *tk
= NULL
, *tv
= NULL
;
109 tk
= estrndup(key
, keylen
);
113 tk
[3]='.'; tk
[4]='.'; tk
[5]='.';
118 tv
= estrndup(val
, vallen
);
122 tv
[3]='.'; tv
[4]='.'; tv
[5]='.';
125 fprintf(stderr
, "[%6s] %c \"%s=%s\"\n",
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":
135 STR_FREE(tk
); STR_FREE(tv
);
141 if (*(c
-1) != '\\') {
144 memmove(c
-1, c
, strlen(c
)+1);
229 } else if (*c
== ';') {
231 } else if (*c
!= ' ') {
239 vallen
= c
- val
- (*c
?1:0);
240 while (val
[vallen
-1] == ' ') --vallen
;
247 add_assoc_stringl(&array
, "name", key
, keylen
, 1);
248 add_assoc_stringl(&array
, "value", val
, vallen
, 1);
250 key
= estrndup(key
, keylen
);
251 add_assoc_stringl_ex(&array
, key
, keylen
+1, val
, vallen
, 1);
262 } else if (st
== ST_ADD
) {
273 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Unexpected character (%c) at pos %tu of %zu", *c
, c
-s
, strlen(s
));
279 /* {{{ void http_error(long, long, char*) */
280 void _http_error_ex(long type TSRMLS_DC
, long code
, const char *format
, ...)
284 va_start(args
, format
);
286 if ((type
== E_THROW
) || (PG(error_handling
) == EH_THROW
)) {
289 vspprintf(&message
, 0, format
, args
);
290 zend_throw_exception(http_exception_get_for_code(code
), message
, code TSRMLS_CC
);
294 php_verror(NULL
, "", type
, format
, args TSRMLS_CC
);
299 /* {{{ void http_log(char *, char *, char *) */
300 void _http_log_ex(char *file
, const char *ident
, const char *message TSRMLS_DC
)
304 char datetime
[20] = {0};
306 now
= HTTP_GET_REQUEST_TIME();
307 strftime(datetime
, sizeof(datetime
), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now
, &nowtm
));
309 #define HTTP_LOG_WRITE(file, type, msg) \
310 if (file && *file) { \
311 php_stream *log = php_stream_open_wrapper(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); \
314 php_stream_printf(log TSRMLS_CC, "%s\t[%s]\t%s\t<%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
315 php_stream_close(log); \
320 HTTP_LOG_WRITE(file
, ident
, message
);
321 HTTP_LOG_WRITE(HTTP_G
->log
.composite
, ident
, message
);
325 static void http_ob_blackhole(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
327 *handled_output
= ecalloc(1,1);
328 *handled_output_len
= 0;
331 /* {{{ STATUS http_exit(int, char*, char*) */
332 STATUS
_http_exit_ex(int status
, char *header
, char *body
, zend_bool send_header TSRMLS_DC
)
334 if ( (send_header
&& (SUCCESS
!= http_send_status_header(status
, header
))) ||
335 (!send_header
&& status
&& (SUCCESS
!= http_send_status(status
)))) {
336 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Failed to exit with status/header: %d - %s", status
, header
? header
: "");
342 php_end_ob_buffers(0 TSRMLS_CC
);
343 if ((SUCCESS
== sapi_send_headers(TSRMLS_C
)) && body
) {
344 PHPWRITE(body
, strlen(body
));
349 case 301: http_log(HTTP_G
->log
.redirect
, "301-REDIRECT", header
); break;
350 case 302: http_log(HTTP_G
->log
.redirect
, "302-REDIRECT", header
); break;
351 case 303: http_log(HTTP_G
->log
.redirect
, "303-REDIRECT", header
); break;
352 case 305: http_log(HTTP_G
->log
.redirect
, "305-REDIRECT", header
); break;
353 case 307: http_log(HTTP_G
->log
.redirect
, "307-REDIRECT", header
); break;
354 case 304: http_log(HTTP_G
->log
.cache
, "304-CACHE", header
); break;
355 case 405: http_log(HTTP_G
->log
.allowed_methods
, "405-ALLOWED", header
); break;
356 default: http_log(NULL
, header
, body
); break;
362 if (HTTP_G
->force_exit
) {
365 php_ob_set_internal_handler(http_ob_blackhole
, 4096, "blackhole", 0 TSRMLS_CC
);
372 /* {{{ STATUS http_check_method(char *) */
373 STATUS
_http_check_method_ex(const char *method
, const char *methods
)
377 if ( (found
= strstr(methods
, method
)) &&
378 (found
== method
|| !isalpha(found
[-1])) &&
379 (strlen(found
) >= strlen(method
) && !isalpha(found
[strlen(method
)]))) {
386 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
387 PHP_HTTP_API zval
*_http_get_server_var_ex(const char *key
, size_t key_size
, zend_bool check TSRMLS_DC
)
392 if ((SUCCESS
!= zend_hash_find(&EG(symbol_table
), "_SERVER", sizeof("_SERVER"), (void **) &hsv
)) || (Z_TYPE_PP(hsv
) != IS_ARRAY
)) {
395 if ((SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(hsv
), (char *) key
, key_size
, (void **) &var
)) || (Z_TYPE_PP(var
) != IS_STRING
)) {
398 if (check
&& !(Z_STRVAL_PP(var
) && Z_STRLEN_PP(var
))) {
405 /* {{{ STATUS http_get_request_body(char **, size_t *) */
406 PHP_HTTP_API STATUS
_http_get_request_body_ex(char **body
, size_t *length
, zend_bool dup TSRMLS_DC
)
411 if (SG(request_info
).raw_post_data
) {
412 *length
= SG(request_info
).raw_post_data_length
;
413 *body
= SG(request_info
).raw_post_data
;
416 *body
= estrndup(*body
, *length
);
419 } else if (sapi_module
.read_post
&& !HTTP_G
->read_post_data
) {
423 HTTP_G
->read_post_data
= 1;
425 while (0 < (len
= sapi_module
.read_post(buf
, sizeof(buf
) TSRMLS_CC
))) {
426 *body
= erealloc(*body
, *length
+ len
+ 1);
427 memcpy(*body
+ *length
, buf
, len
);
429 (*body
)[*length
] = '\0';
432 /* check for error */
439 SG(request_info
).raw_post_data
= *body
;
440 SG(request_info
).raw_post_data_length
= *length
;
443 *body
= estrndup(*body
, *length
);
452 /* {{{ php_stream *_http_get_request_body_stream(void) */
453 PHP_HTTP_API php_stream
*_http_get_request_body_stream(TSRMLS_D
)
455 php_stream
*s
= NULL
;
457 if (SG(request_info
).raw_post_data
) {
458 s
= php_stream_open_wrapper("php://input", "rb", 0, NULL
);
459 } else if (sapi_module
.read_post
&& !HTTP_G
->read_post_data
) {
460 HTTP_G
->read_post_data
= 1;
462 if ((s
= php_stream_temp_new())) {
466 while (0 < (len
= sapi_module
.read_post(buf
, sizeof(buf
) TSRMLS_CC
))) {
467 php_stream_write(s
, buf
, len
);
474 php_stream_rewind(s
);
488 * vim600: noet sw=4 ts=4 fdm=marker
489 * vim<600: noet sw=4 ts=4