- fix config.m4 (CURL_LIBS define was missing)
[m6w6/ext-http] / http_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
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 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include "php.h"
22
23 #include "SAPI.h"
24 #include "ext/standard/url.h"
25
26 #include "php_http.h"
27 #include "php_http_std_defs.h"
28 #include "php_http_api.h"
29 #include "php_http_headers_api.h"
30 #include "php_http_send_api.h"
31
32 #ifdef ZEND_ENGINE_2
33 # include "zend_exceptions.h"
34 # include "php_http_exception_object.h"
35 #endif
36
37 #include <ctype.h>
38
39 ZEND_EXTERN_MODULE_GLOBALS(http);
40
41 /* char *pretty_key(char *, size_t, zend_bool, zend_bool) */
42 char *_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
43 {
44 if (key && key_len) {
45 size_t i;
46 int wasalpha;
47 if (wasalpha = isalpha((int) key[0])) {
48 key[0] = (char) (uctitle ? toupper((int) key[0]) : tolower((int) key[0]));
49 }
50 for (i = 1; i < key_len; i++) {
51 if (isalpha((int) key[i])) {
52 key[i] = (char) (((!wasalpha) && uctitle) ? toupper((int) key[i]) : tolower((int) key[i]));
53 wasalpha = 1;
54 } else {
55 if (xhyphen && (key[i] == '_')) {
56 key[i] = '-';
57 }
58 wasalpha = 0;
59 }
60 }
61 }
62 return key;
63 }
64 /* }}} */
65
66 /* {{{ */
67 void _http_key_list_default_decoder(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
68 {
69 *decoded = estrndup(encoded, encoded_len);
70 *decoded_len = (size_t) php_url_decode(*decoded, encoded_len);
71 }
72 /* }}} */
73
74 /* {{{ */
75 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)
76 {
77 const char *key = list, *val = NULL;
78 int vallen = 0, keylen = 0, done = 0;
79 zval array;
80
81 Z_ARRVAL(array) = items;
82
83 if (!(val = strchr(list, '='))) {
84 return FAILURE;
85 }
86
87 #define HTTP_KEYLIST_VAL(array, k, str, len) \
88 { \
89 char *decoded; \
90 size_t decoded_len; \
91 if (decode) { \
92 decode(str, len, &decoded, &decoded_len TSRMLS_CC); \
93 } else { \
94 decoded_len = len; \
95 decoded = estrndup(str, decoded_len); \
96 } \
97 add_assoc_stringl(array, k, decoded, decoded_len, 0); \
98 }
99 #define HTTP_KEYLIST_FIXKEY() \
100 { \
101 while (isspace(*key)) ++key; \
102 keylen = val - key; \
103 while (isspace(key[keylen - 1])) --keylen; \
104 }
105 #define HTTP_KEYLIST_FIXVAL() \
106 { \
107 ++val; \
108 while (isspace(*val)) ++val; \
109 vallen = key - val; \
110 while (isspace(val[vallen - 1])) --vallen; \
111 }
112
113 HTTP_KEYLIST_FIXKEY();
114
115 if (first_entry_is_name_value_pair) {
116 HTTP_KEYLIST_VAL(&array, "name", key, keylen);
117
118 /* just one name=value */
119 if (!(key = strchr(val, separator))) {
120 key = val + strlen(val);
121 HTTP_KEYLIST_FIXVAL();
122 HTTP_KEYLIST_VAL(&array, "value", val, vallen);
123 return SUCCESS;
124 }
125 /* additional info appended */
126 else {
127 HTTP_KEYLIST_FIXVAL();
128 HTTP_KEYLIST_VAL(&array, "value", val, vallen);
129 }
130 }
131
132 do {
133 char *keydup = NULL;
134
135 if (!(val = strchr(key, '='))) {
136 break;
137 }
138
139 /* start at 0 if first_entry_is_name_value_pair==0 */
140 if (zend_hash_num_elements(items)) {
141 ++key;
142 }
143
144 HTTP_KEYLIST_FIXKEY();
145 keydup = estrndup(key, keylen);
146 if (!(key = strchr(val, separator))) {
147 done = 1;
148 key = val + strlen(val);
149 }
150 HTTP_KEYLIST_FIXVAL();
151 HTTP_KEYLIST_VAL(&array, keydup, val, vallen);
152 efree(keydup);
153 } while (!done);
154
155 return SUCCESS;
156 }
157 /* }}} */
158
159 /* {{{ void http_error(long, long, char*) */
160 void _http_error_ex(long type, long code, const char *format, ...)
161 {
162 va_list args;
163 TSRMLS_FETCH();
164
165 va_start(args, format);
166 if (type == E_THROW) {
167 #ifdef ZEND_ENGINE_2
168 char *message;
169 vspprintf(&message, 0, format, args);
170 zend_throw_exception(http_exception_get_for_code(code), message, code TSRMLS_CC);
171 #else
172 type = E_WARNING;
173 #endif
174 }
175 if (type != E_THROW) {
176 php_verror(NULL, "", type, format, args TSRMLS_CC);
177 }
178 va_end(args);
179 }
180 /* }}} */
181
182 /* {{{ void http_log(char *, char *, char *) */
183 void _http_log_ex(char *file, const char *ident, const char *message TSRMLS_DC)
184 {
185 time_t now;
186 struct tm nowtm;
187 char datetime[128];
188
189 time(&now);
190 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
191
192 #define HTTP_LOG_WRITE(file, type, msg) \
193 if (file && strlen(file)) { \
194 php_stream *log = php_stream_open_wrapper(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); \
195 \
196 if (log) { \
197 php_stream_printf(log TSRMLS_CC, "%s [%12s] %32s <%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
198 php_stream_close(log); \
199 } \
200 \
201 }
202
203 HTTP_LOG_WRITE(file, ident, message);
204 HTTP_LOG_WRITE(HTTP_G(log).composite, ident, message);
205 }
206 /* }}} */
207
208 /* {{{ STATUS http_exit(int, char*, char*) */
209 STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header TSRMLS_DC)
210 {
211 if (status || send_header) {
212 if (SUCCESS != http_send_status_header(status, send_header ? header : NULL)) {
213 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Failed to exit with status/header: %d - %s", status, header ? header : "");
214 STR_FREE(header);
215 STR_FREE(body);
216 return FAILURE;
217 }
218 }
219
220 if (body) {
221 PHPWRITE(body, strlen(body));
222 }
223
224 switch (status)
225 {
226 case 301: http_log(HTTP_G(log).redirect, "301-REDIRECT", header); break;
227 case 302: http_log(HTTP_G(log).redirect, "302-REDIRECT", header); break;
228 case 304: http_log(HTTP_G(log).cache, "304-CACHE", header); break;
229 case 405: http_log(HTTP_G(log).allowed_methods, "405-ALLOWED", header); break;
230 default: http_log(NULL, header, body); break;
231 }
232
233 STR_FREE(header);
234 STR_FREE(body);
235
236 zend_bailout();
237 /* fake */
238 return SUCCESS;
239 }
240 /* }}} */
241
242 /* {{{ STATUS http_check_method(char *) */
243 STATUS _http_check_method_ex(const char *method, const char *methods)
244 {
245 const char *found;
246
247 if ( (found = strstr(methods, method)) &&
248 (found == method || !isalpha(found[-1])) &&
249 (!isalpha(found[strlen(method) + 1]))) {
250 return SUCCESS;
251 }
252 return FAILURE;
253 }
254 /* }}} */
255
256 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
257 PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC)
258 {
259 zval **hsv;
260 zval **var;
261
262 if (SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &hsv)) {
263 return NULL;
264 }
265 if (SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), (char *) key, key_size, (void **) &var)) {
266 return NULL;
267 }
268 if (check && !(Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
269 return NULL;
270 }
271 return *var;
272 }
273 /* }}} */
274
275 /* {{{ STATUS http_get_request_body(char **, size_t *) */
276 PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_bool dup TSRMLS_DC)
277 {
278 *length = 0;
279 *body = NULL;
280
281 if (SG(request_info).raw_post_data) {
282 *length = SG(request_info).raw_post_data_length;
283 *body = (char *) (dup ? estrndup(SG(request_info).raw_post_data, *length) : SG(request_info).raw_post_data);
284 return SUCCESS;
285 }
286 return FAILURE;
287 }
288 /* }}} */
289
290 /* {{{ char *http_chunked_decode(char *, size_t, char **, size_t *) */
291 PHP_HTTP_API const char *_http_chunked_decode(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
292 {
293 const char *e_ptr;
294 char *d_ptr;
295
296 *decoded_len = 0;
297 *decoded = ecalloc(1, encoded_len);
298 d_ptr = *decoded;
299 e_ptr = encoded;
300
301 while (((e_ptr - encoded) - encoded_len) > 0) {
302 size_t chunk_len = 0, EOL_len = 0;
303 int eol_mismatch = 0;
304 char *n_ptr;
305
306 chunk_len = strtol(e_ptr, &n_ptr, 16);
307
308 /* check if:
309 * - we could not read in chunk size
310 * - chunk size is not followed by (CR)LF|NUL
311 */
312 if ((n_ptr == e_ptr) || (*n_ptr && (eol_mismatch = n_ptr != http_locate_eol(e_ptr, &EOL_len)))) {
313 /* don't fail on apperently not encoded data */
314 if (e_ptr == encoded) {
315 memcpy(*decoded, encoded, encoded_len);
316 *decoded_len = encoded_len;
317 return encoded + encoded_len;
318 } else {
319 efree(*decoded);
320 if (eol_mismatch) {
321 if (EOL_len == 2) {
322 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0D 0x0A; got: 0x%X 0x%X)", *n_ptr, *(n_ptr + 1));
323 } else {
324 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0A; got: 0x%X)", *n_ptr);
325 }
326 } else {
327 char *error = estrndup(n_ptr, strcspn(n_ptr, "\r\n "));
328 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid chunk size: '%s' at pos %d", error, n_ptr - encoded);
329 efree(error);
330 }
331
332 return NULL;
333 }
334 } else {
335 e_ptr = n_ptr;
336 }
337
338 /* reached the end */
339 if (!chunk_len) {
340 break;
341 }
342
343 memcpy(d_ptr, e_ptr += EOL_len, chunk_len);
344 d_ptr += chunk_len;
345 e_ptr += chunk_len + EOL_len;
346 *decoded_len += chunk_len;
347 }
348
349 return e_ptr;
350 }
351 /* }}} */
352
353 /*
354 * Local variables:
355 * tab-width: 4
356 * c-basic-offset: 4
357 * End:
358 * vim600: noet sw=4 ts=4 fdm=marker
359 * vim<600: noet sw=4 ts=4
360 */
361