2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
27 #include "php_http_std_defs.h"
28 #include "php_http_api.h"
29 #include "php_http_headers_api.h"
32 # include "php_http_exception_object.h"
35 ZEND_EXTERN_MODULE_GLOBALS(http
);
37 /* char *pretty_key(char *, size_t, zend_bool, zebd_bool) */
38 char *_http_pretty_key(char *key
, size_t key_len
, zend_bool uctitle
, zend_bool xhyphen
)
42 if (wasalpha
= isalpha(key
[0])) {
43 key
[0] = uctitle
? toupper(key
[0]) : tolower(key
[0]);
45 for (i
= 1; i
< key_len
; i
++) {
46 if (isalpha(key
[i
])) {
47 key
[i
] = ((!wasalpha
) && uctitle
) ? toupper(key
[i
]) : tolower(key
[i
]);
50 if (xhyphen
&& (key
[i
] == '_')) {
61 /* {{{ void http_error(long, long, char*) */
62 void _http_error_ex(long type
, long code
, const char *format
, ...)
67 va_start(args
, format
);
68 if (type
== E_THROW
) {
71 vspprintf(&message
, 0, format
, args
);
72 zend_throw_exception(http_exception_get_default(), message
, code TSRMLS_CC
);
77 if (type
!= E_THROW
) {
78 php_verror(NULL
, "", type
, format
, args TSRMLS_CC
);
84 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
85 PHP_HTTP_API zval
*_http_get_server_var_ex(const char *key
, size_t key_size
, zend_bool check TSRMLS_DC
)
88 if (SUCCESS
== zend_hash_find(HTTP_SERVER_VARS
, (char *) key
, key_size
, (void **) &var
)) {
90 return Z_STRVAL_PP(var
) && Z_STRLEN_PP(var
) ? *var
: NULL
;
100 /* {{{ STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
101 PHP_HTTP_API STATUS
_http_chunked_decode(const char *encoded
, size_t encoded_len
,
102 char **decoded
, size_t *decoded_len TSRMLS_DC
)
108 *decoded
= ecalloc(1, encoded_len
);
112 while (((e_ptr
- encoded
) - encoded_len
) > 0) {
113 char hex_len
[9] = {0};
114 size_t chunk_len
= 0;
117 /* read in chunk size */
118 while (isxdigit(*e_ptr
)) {
120 http_error_ex(E_WARNING
, HTTP_E_PARSE
, "Chunk size is too long: 0x%s...", hex_len
);
124 hex_len
[i
++] = *e_ptr
++;
127 /* reached the end */
128 if (!strcmp(hex_len
, "0")) {
133 if (strncmp(e_ptr
, HTTP_CRLF
, 2)) {
134 http_error_ex(E_WARNING
, HTTP_E_PARSE
, "Invalid character (expected 0x0D 0x0A; got: %x %x)", *e_ptr
, *(e_ptr
+ 1));
142 chunk_len
= strtol(hex_len
, &error
, 16);
143 if (error
== hex_len
) {
144 http_error_ex(E_WARNING
, HTTP_E_PARSE
, "Invalid chunk size string: '%s'", hex_len
);
150 memcpy(d_ptr
, e_ptr
+= 2, chunk_len
);
152 e_ptr
+= chunk_len
+ 2;
153 *decoded_len
+= chunk_len
;
160 /* {{{ STATUS http_split_response(zval *, zval *, zval *) */
161 PHP_HTTP_API STATUS
_http_split_response(zval
*response
, zval
*headers
, zval
*body TSRMLS_DC
)
165 STATUS status
= http_split_response_ex(Z_STRVAL_P(response
), Z_STRLEN_P(response
), Z_ARRVAL_P(headers
), &b
, &l
);
166 ZVAL_STRINGL(body
, b
, l
, 0);
171 /* {{{ STATUS http_split_response(char *, size_t, HashTable *, char **, size_t *) */
172 PHP_HTTP_API STATUS
_http_split_response_ex(char *response
, size_t response_len
,
173 HashTable
*headers
, char **body
, size_t *body_len TSRMLS_DC
)
175 char *header
= response
, *real_body
= NULL
;
177 while (0 < (response_len
- (response
- header
+ 4))) {
178 if ( (*response
++ == '\r') &&
179 (*response
++ == '\n') &&
180 (*response
++ == '\r') &&
181 (*response
++ == '\n')) {
182 real_body
= response
;
187 if (real_body
&& (*body_len
= (response_len
- (real_body
- header
)))) {
188 *body
= ecalloc(1, *body_len
+ 1);
189 memcpy(*body
, real_body
, *body_len
);
192 return http_parse_headers_ex(header
, real_body
? response_len
- *body_len
: response_len
, headers
, 1);
201 * vim600: noet sw=4 ts=4 fdm=marker
202 * vim<600: noet sw=4 ts=4