eec41daa0d7068d964cc26ab7377e15dc6b78fba
[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 #ifdef HTTP_HAVE_MAGIC
40 # if defined(PHP_WIN32) && !defined(USE_MAGIC_DLL) && !defined(USE_MAGIC_STATIC)
41 # define USE_MAGIC_STATIC
42 # endif
43 # include <magic.h>
44 #endif
45
46 ZEND_EXTERN_MODULE_GLOBALS(http);
47
48 /* char *pretty_key(char *, size_t, zend_bool, zend_bool) */
49 char *_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
50 {
51 if (key && key_len) {
52 size_t i;
53 int wasalpha;
54 if (wasalpha = isalpha((int) key[0])) {
55 key[0] = (char) (uctitle ? toupper((int) key[0]) : tolower((int) key[0]));
56 }
57 for (i = 1; i < key_len; i++) {
58 if (isalpha((int) key[i])) {
59 key[i] = (char) (((!wasalpha) && uctitle) ? toupper((int) key[i]) : tolower((int) key[i]));
60 wasalpha = 1;
61 } else {
62 if (xhyphen && (key[i] == '_')) {
63 key[i] = '-';
64 }
65 wasalpha = 0;
66 }
67 }
68 }
69 return key;
70 }
71 /* }}} */
72
73 /* {{{ */
74 void _http_key_list_default_decoder(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
75 {
76 *decoded = estrndup(encoded, encoded_len);
77 *decoded_len = (size_t) php_url_decode(*decoded, encoded_len);
78 }
79 /* }}} */
80
81 /* {{{ */
82 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)
83 {
84 const char *key = list, *val = NULL;
85 int vallen = 0, keylen = 0, done = 0;
86 zval array;
87
88 INIT_ZARR(array, items);
89
90 if (!(val = strchr(list, '='))) {
91 return FAILURE;
92 }
93
94 #define HTTP_KEYLIST_VAL(array, k, str, len) \
95 { \
96 char *decoded; \
97 size_t decoded_len; \
98 if (decode) { \
99 decode(str, len, &decoded, &decoded_len TSRMLS_CC); \
100 } else { \
101 decoded_len = len; \
102 decoded = estrndup(str, decoded_len); \
103 } \
104 add_assoc_stringl(array, k, decoded, decoded_len, 0); \
105 }
106 #define HTTP_KEYLIST_FIXKEY() \
107 { \
108 while (isspace(*key)) ++key; \
109 keylen = val - key; \
110 while (isspace(key[keylen - 1])) --keylen; \
111 }
112 #define HTTP_KEYLIST_FIXVAL() \
113 { \
114 ++val; \
115 while (isspace(*val)) ++val; \
116 vallen = key - val; \
117 while (isspace(val[vallen - 1])) --vallen; \
118 }
119
120 HTTP_KEYLIST_FIXKEY();
121
122 if (first_entry_is_name_value_pair) {
123 HTTP_KEYLIST_VAL(&array, "name", key, keylen);
124
125 /* just one name=value */
126 if (!(key = strchr(val, separator))) {
127 key = val + strlen(val);
128 HTTP_KEYLIST_FIXVAL();
129 HTTP_KEYLIST_VAL(&array, "value", val, vallen);
130 return SUCCESS;
131 }
132 /* additional info appended */
133 else {
134 HTTP_KEYLIST_FIXVAL();
135 HTTP_KEYLIST_VAL(&array, "value", val, vallen);
136 }
137 }
138
139 do {
140 char *keydup = NULL;
141
142 if (!(val = strchr(key, '='))) {
143 break;
144 }
145
146 /* start at 0 if first_entry_is_name_value_pair==0 */
147 if (zend_hash_num_elements(items)) {
148 ++key;
149 }
150
151 HTTP_KEYLIST_FIXKEY();
152 keydup = estrndup(key, keylen);
153 if (!(key = strchr(val, separator))) {
154 done = 1;
155 key = val + strlen(val);
156 }
157 HTTP_KEYLIST_FIXVAL();
158 HTTP_KEYLIST_VAL(&array, keydup, val, vallen);
159 efree(keydup);
160 } while (!done);
161
162 return SUCCESS;
163 }
164 /* }}} */
165
166 /* {{{ void http_error(long, long, char*) */
167 void _http_error_ex(long type TSRMLS_DC, long code, const char *format, ...)
168 {
169 va_list args;
170
171 va_start(args, format);
172 #ifdef ZEND_ENGINE_2
173 if ((type == E_THROW) || (PG(error_handling) == EH_THROW)) {
174 char *message;
175
176 vspprintf(&message, 0, format, args);
177 zend_throw_exception(http_exception_get_for_code(code), message, code TSRMLS_CC);
178 } else
179 #endif
180 php_verror(NULL, "", type, format, args TSRMLS_CC);
181 va_end(args);
182 }
183 /* }}} */
184
185 /* {{{ void http_log(char *, char *, char *) */
186 void _http_log_ex(char *file, const char *ident, const char *message TSRMLS_DC)
187 {
188 time_t now;
189 struct tm nowtm;
190 char datetime[128];
191
192 time(&now);
193 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
194
195 #define HTTP_LOG_WRITE(file, type, msg) \
196 if (file && strlen(file)) { \
197 php_stream *log = php_stream_open_wrapper(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); \
198 \
199 if (log) { \
200 php_stream_printf(log TSRMLS_CC, "%s\t[%s]\t%s\t<%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
201 php_stream_close(log); \
202 } \
203 \
204 }
205
206 HTTP_LOG_WRITE(file, ident, message);
207 HTTP_LOG_WRITE(HTTP_G(log).composite, ident, message);
208 }
209 /* }}} */
210
211 /* {{{ STATUS http_exit(int, char*, char*) */
212 STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header TSRMLS_DC)
213 {
214 if (status || send_header) {
215 if (SUCCESS != http_send_status_header(status, send_header ? header : NULL)) {
216 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Failed to exit with status/header: %d - %s", status, header ? header : "");
217 STR_FREE(header);
218 STR_FREE(body);
219 return FAILURE;
220 }
221 }
222
223 if (body) {
224 PHPWRITE(body, strlen(body));
225 }
226
227 switch (status)
228 {
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 304: http_log(HTTP_G(log).cache, "304-CACHE", header); break;
232 case 405: http_log(HTTP_G(log).allowed_methods, "405-ALLOWED", header); break;
233 default: http_log(NULL, header, body); break;
234 }
235
236 STR_FREE(header);
237 STR_FREE(body);
238
239 zend_bailout();
240 /* fake */
241 return SUCCESS;
242 }
243 /* }}} */
244
245 /* {{{ STATUS http_check_method(char *) */
246 STATUS _http_check_method_ex(const char *method, const char *methods)
247 {
248 const char *found;
249
250 if ( (found = strstr(methods, method)) &&
251 (found == method || !isalpha(found[-1])) &&
252 (!isalpha(found[strlen(method) + 1]))) {
253 return SUCCESS;
254 }
255 return FAILURE;
256 }
257 /* }}} */
258
259 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
260 PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC)
261 {
262 zval **hsv;
263 zval **var;
264
265 if (SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &hsv)) {
266 return NULL;
267 }
268 if (SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), (char *) key, key_size, (void **) &var)) {
269 return NULL;
270 }
271 if (check && !(Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
272 return NULL;
273 }
274 return *var;
275 }
276 /* }}} */
277
278 /* {{{ STATUS http_get_request_body(char **, size_t *) */
279 PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_bool dup TSRMLS_DC)
280 {
281 *length = 0;
282 *body = NULL;
283
284 if (SG(request_info).raw_post_data) {
285 *length = SG(request_info).raw_post_data_length;
286 *body = (char *) (dup ? estrndup(SG(request_info).raw_post_data, *length) : SG(request_info).raw_post_data);
287 return SUCCESS;
288 }
289 return FAILURE;
290 }
291 /* }}} */
292
293 /* {{{ char *http_chunked_decode(char *, size_t, char **, size_t *) */
294 PHP_HTTP_API const char *_http_chunked_decode(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
295 {
296 const char *e_ptr;
297 char *d_ptr;
298
299 *decoded_len = 0;
300 *decoded = ecalloc(1, encoded_len);
301 d_ptr = *decoded;
302 e_ptr = encoded;
303
304 while (((e_ptr - encoded) - encoded_len) > 0) {
305 size_t chunk_len = 0, EOL_len = 0;
306 int eol_mismatch = 0;
307 char *n_ptr;
308
309 chunk_len = strtol(e_ptr, &n_ptr, 16);
310
311 /* check if:
312 * - we could not read in chunk size
313 * - chunk size is not followed by (CR)LF|NUL
314 */
315 if ((n_ptr == e_ptr) || (*n_ptr && (eol_mismatch = n_ptr != http_locate_eol(e_ptr, &EOL_len)))) {
316 /* don't fail on apperently not encoded data */
317 if (e_ptr == encoded) {
318 memcpy(*decoded, encoded, encoded_len);
319 *decoded_len = encoded_len;
320 return encoded + encoded_len;
321 } else {
322 efree(*decoded);
323 if (eol_mismatch) {
324 if (EOL_len == 2) {
325 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0D 0x0A; got: 0x%X 0x%X)", *n_ptr, *(n_ptr + 1));
326 } else {
327 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0A; got: 0x%X)", *n_ptr);
328 }
329 } else {
330 char *error = estrndup(n_ptr, strcspn(n_ptr, "\r\n "));
331 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid chunk size: '%s' at pos %d", error, n_ptr - encoded);
332 efree(error);
333 }
334 return NULL;
335 }
336 } else {
337 e_ptr = n_ptr;
338 }
339
340 /* reached the end */
341 if (!chunk_len) {
342 break;
343 }
344
345 memcpy(d_ptr, e_ptr += EOL_len, chunk_len);
346 d_ptr += chunk_len;
347 e_ptr += chunk_len + EOL_len;
348 *decoded_len += chunk_len;
349 }
350
351 return e_ptr;
352 }
353 /* }}} */
354
355 /* {{{ char *http_guess_content_type(char *magic_file, long magic_mode, void *data, size_t size, http_send_mode mode) */
356 PHP_HTTP_API char *_http_guess_content_type(const char *magicfile, long magicmode, void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC)
357 {
358 char *ct = NULL;
359
360 #ifdef HTTP_HAVE_MAGIC
361 /* magic_load() fails if MAGIC_MIME is set because it
362 cowardly adds .mime to the file name */
363 struct magic_set *magic = magic_open(magicmode &~ MAGIC_MIME);
364
365 if (!magic) {
366 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid magic mode: %ld", magicmode);
367 } else if (-1 == magic_load(magic, magicfile)) {
368 http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Failed to load magic database '%s' (%s)", magicfile, magic_error(magic));
369 } else {
370 const char *ctype = NULL;
371
372 magic_setflags(magic, magicmode);
373
374 switch (data_mode)
375 {
376 case SEND_RSRC:
377 {
378 char *buffer;
379 size_t b_len;
380
381 b_len = php_stream_copy_to_mem(data_ptr, &buffer, 65536, 0);
382 ctype = magic_buffer(magic, buffer, b_len);
383 efree(buffer);
384 }
385 break;
386
387 case SEND_DATA:
388 ctype = magic_buffer(magic, data_ptr, data_len);
389 break;
390
391 default:
392 ctype = magic_file(magic, data_ptr);
393 break;
394 }
395
396 if (ctype) {
397 ct = estrdup(ctype);
398 } else {
399 http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Failed to guess Content-Type: %s", magic_error(magic));
400 }
401 }
402 if (magic) {
403 magic_close(magic);
404 }
405 #else
406 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot guess Content-Type; libmagic not available");
407 #endif
408
409 return ct;
410 }
411 /* }}} */
412 /*
413 * Local variables:
414 * tab-width: 4
415 * c-basic-offset: 4
416 * End:
417 * vim600: noet sw=4 ts=4 fdm=marker
418 * vim<600: noet sw=4 ts=4
419 */
420