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 +--------------------------------------------------------------------+
22 #include "php_output.h"
23 #include "ext/standard/url.h"
25 #include "php_http_api.h"
26 #include "php_http_send_api.h"
29 # include "php_http_exception_object.h"
32 PHP_MINIT_FUNCTION(http_support
)
34 HTTP_LONG_CONSTANT("HTTP_SUPPORT", HTTP_SUPPORT
);
35 HTTP_LONG_CONSTANT("HTTP_SUPPORT_REQUESTS", HTTP_SUPPORT_REQUESTS
);
36 HTTP_LONG_CONSTANT("HTTP_SUPPORT_MAGICMIME", HTTP_SUPPORT_MAGICMIME
);
37 HTTP_LONG_CONSTANT("HTTP_SUPPORT_ENCODINGS", HTTP_SUPPORT_ENCODINGS
);
38 HTTP_LONG_CONSTANT("HTTP_SUPPORT_SSLREQUESTS", HTTP_SUPPORT_SSLREQUESTS
);
43 PHP_HTTP_API
long _http_support(long feature
)
45 long support
= HTTP_SUPPORT
;
48 support
|= HTTP_SUPPORT_REQUESTS
;
50 support
|= HTTP_SUPPORT_SSLREQUESTS
;
53 #ifdef HTTP_HAVE_MAGIC
54 support
|= HTTP_SUPPORT_MAGICMIME
;
57 support
|= HTTP_SUPPORT_ENCODINGS
;
61 return (feature
== (support
& feature
));
66 /* char *pretty_key(char *, size_t, zend_bool, zend_bool) */
67 char *_http_pretty_key(char *key
, size_t key_len
, zend_bool uctitle
, zend_bool xhyphen
)
72 if ((wasalpha
= isalpha((int) key
[0]))) {
73 key
[0] = (char) (uctitle
? toupper((int) key
[0]) : tolower((int) key
[0]));
75 for (i
= 1; i
< key_len
; i
++) {
76 if (isalpha((int) key
[i
])) {
77 key
[i
] = (char) (((!wasalpha
) && uctitle
) ? toupper((int) key
[i
]) : tolower((int) key
[i
]));
80 if (xhyphen
&& (key
[i
] == '_')) {
92 void _http_key_list_default_decoder(const char *encoded
, size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
94 *decoded
= estrndup(encoded
, encoded_len
);
95 *decoded_len
= (size_t) php_url_decode(*decoded
, encoded_len
);
100 STATUS
_http_parse_key_list(const char *list
, HashTable
*items
, char separator
, http_key_list_decode_t decode
, zend_bool first_entry_is_name_value_pair TSRMLS_DC
)
102 const char *key
= list
, *val
= NULL
;
103 int vallen
= 0, keylen
= 0, done
= 0;
106 INIT_ZARR(array
, items
);
108 if (!(val
= strchr(list
, '='))) {
112 #define HTTP_KEYLIST_VAL(array, k, str, len) \
115 size_t decoded_len; \
117 decode(str, len, &decoded, &decoded_len TSRMLS_CC); \
120 decoded = estrndup(str, decoded_len); \
122 add_assoc_stringl(array, k, decoded, decoded_len, 0); \
124 #define HTTP_KEYLIST_FIXKEY() \
126 while (isspace(*key)) ++key; \
127 keylen = val - key; \
128 while (isspace(key[keylen - 1])) --keylen; \
130 #define HTTP_KEYLIST_FIXVAL() \
133 while (isspace(*val)) ++val; \
134 vallen = key - val; \
135 while (isspace(val[vallen - 1])) --vallen; \
138 HTTP_KEYLIST_FIXKEY();
140 if (first_entry_is_name_value_pair
) {
141 HTTP_KEYLIST_VAL(&array
, "name", key
, keylen
);
143 /* just one name=value */
144 if (!(key
= strchr(val
, separator
))) {
145 key
= val
+ strlen(val
);
146 HTTP_KEYLIST_FIXVAL();
147 HTTP_KEYLIST_VAL(&array
, "value", val
, vallen
);
150 /* additional info appended */
152 HTTP_KEYLIST_FIXVAL();
153 HTTP_KEYLIST_VAL(&array
, "value", val
, vallen
);
160 if (!(val
= strchr(key
, '='))) {
164 /* start at 0 if first_entry_is_name_value_pair==0 */
165 if (zend_hash_num_elements(items
)) {
169 HTTP_KEYLIST_FIXKEY();
170 keydup
= estrndup(key
, keylen
);
171 if (!(key
= strchr(val
, separator
))) {
173 key
= val
+ strlen(val
);
175 HTTP_KEYLIST_FIXVAL();
176 HTTP_KEYLIST_VAL(&array
, keydup
, val
, vallen
);
184 /* {{{ void http_error(long, long, char*) */
185 void _http_error_ex(long type TSRMLS_DC
, long code
, const char *format
, ...)
189 va_start(args
, format
);
191 if ((type
== E_THROW
) || (PG(error_handling
) == EH_THROW
)) {
194 vspprintf(&message
, 0, format
, args
);
195 zend_throw_exception(http_exception_get_for_code(code
), message
, code TSRMLS_CC
);
199 php_verror(NULL
, "", type
, format
, args TSRMLS_CC
);
204 /* {{{ void http_log(char *, char *, char *) */
205 void _http_log_ex(char *file
, const char *ident
, const char *message TSRMLS_DC
)
209 char datetime
[20] = {0};
212 strftime(datetime
, sizeof(datetime
), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now
, &nowtm
));
214 #define HTTP_LOG_WRITE(file, type, msg) \
215 if (file && *file) { \
216 php_stream *log = php_stream_open_wrapper(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); \
219 php_stream_printf(log TSRMLS_CC, "%s\t[%s]\t%s\t<%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
220 php_stream_close(log); \
225 HTTP_LOG_WRITE(file
, ident
, message
);
226 HTTP_LOG_WRITE(HTTP_G(log
).composite
, ident
, message
);
230 static void http_ob_blackhole(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
232 *handled_output
= ecalloc(1,1);
233 *handled_output_len
= 0;
236 /* {{{ STATUS http_exit(int, char*, char*) */
237 STATUS
_http_exit_ex(int status
, char *header
, char *body
, zend_bool send_header TSRMLS_DC
)
239 if ( (send_header
&& (SUCCESS
!= http_send_status_header(status
, header
))) ||
240 (!send_header
&& status
&& (SUCCESS
!= http_send_status(status
)))) {
241 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Failed to exit with status/header: %d - %s", status
, header
? header
: "");
247 php_end_ob_buffers(0 TSRMLS_CC
);
248 if ((SUCCESS
== sapi_send_headers(TSRMLS_C
)) && body
) {
249 PHPWRITE(body
, strlen(body
));
254 case 301: http_log(HTTP_G(log
).redirect
, "301-REDIRECT", header
); break;
255 case 302: http_log(HTTP_G(log
).redirect
, "302-REDIRECT", header
); break;
256 case 303: http_log(HTTP_G(log
).redirect
, "303-REDIRECT", header
); break;
257 case 305: http_log(HTTP_G(log
).redirect
, "305-REDIRECT", header
); break;
258 case 307: http_log(HTTP_G(log
).redirect
, "307-REDIRECT", header
); break;
259 case 304: http_log(HTTP_G(log
).cache
, "304-CACHE", header
); break;
260 case 405: http_log(HTTP_G(log
).allowed_methods
, "405-ALLOWED", header
); break;
261 default: http_log(NULL
, header
, body
); break;
267 if (HTTP_G(force_exit
)) {
270 php_ob_set_internal_handler(http_ob_blackhole
, 4096, "blackhole", 0 TSRMLS_CC
);
277 /* {{{ STATUS http_check_method(char *) */
278 STATUS
_http_check_method_ex(const char *method
, const char *methods
)
282 if ( (found
= strstr(methods
, method
)) &&
283 (found
== method
|| !isalpha(found
[-1])) &&
284 (strlen(found
) >= strlen(method
) && !isalpha(found
[strlen(method
)]))) {
291 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
292 PHP_HTTP_API zval
*_http_get_server_var_ex(const char *key
, size_t key_size
, zend_bool check TSRMLS_DC
)
297 if ((SUCCESS
!= zend_hash_find(&EG(symbol_table
), "_SERVER", sizeof("_SERVER"), (void **) &hsv
)) || (Z_TYPE_PP(hsv
) != IS_ARRAY
)) {
300 if ((SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(hsv
), (char *) key
, key_size
, (void **) &var
)) || (Z_TYPE_PP(var
) != IS_STRING
)) {
303 if (check
&& !(Z_STRVAL_PP(var
) && Z_STRLEN_PP(var
))) {
310 /* {{{ STATUS http_get_request_body(char **, size_t *) */
311 PHP_HTTP_API STATUS
_http_get_request_body_ex(char **body
, size_t *length
, zend_bool dup TSRMLS_DC
)
316 if (SG(request_info
).raw_post_data
) {
317 *length
= SG(request_info
).raw_post_data_length
;
318 *body
= (char *) (dup
? estrndup(SG(request_info
).raw_post_data
, *length
) : SG(request_info
).raw_post_data
);
322 /* PHP only reads POST */
323 if (sapi_module
.read_post
) {
327 while (0 < (len
= sapi_module
.read_post(buf
, sizeof(buf
) TSRMLS_CC
))) {
328 *body
= erealloc(*body
, *length
+ len
+ 1);
329 memcpy(*body
+ *length
, buf
, len
);
331 (*body
)[*length
] = '\0';
334 /* check for error */
336 /* connect to sapi module so it'll be freed */
338 SG(request_info
).raw_post_data
= *body
;
339 SG(request_info
).raw_post_data_length
= *length
;
358 * vim600: noet sw=4 ts=4 fdm=marker
359 * vim<600: noet sw=4 ts=4