3f4b7a66072b9af63905daee897931fffcc86b49
[m6w6/ext-http] / http_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 #include "php_http.h"
20
21 #include "SAPI.h"
22 #include "php_output.h"
23 #include "ext/standard/url.h"
24
25 #include "php_http_api.h"
26 #include "php_http_send_api.h"
27
28 #ifdef ZEND_ENGINE_2
29 # include "php_http_exception_object.h"
30 #endif
31
32 PHP_MINIT_FUNCTION(http_support)
33 {
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);
39
40 return SUCCESS;
41 }
42
43 PHP_HTTP_API long _http_support(long feature)
44 {
45 long support = HTTP_SUPPORT;
46
47 #ifdef HTTP_HAVE_CURL
48 support |= HTTP_SUPPORT_REQUESTS;
49 # ifdef HTTP_HAVE_SSL
50 support |= HTTP_SUPPORT_SSLREQUESTS;
51 # endif
52 #endif
53 #ifdef HTTP_HAVE_MAGIC
54 support |= HTTP_SUPPORT_MAGICMIME;
55 #endif
56 #ifdef HTTP_HAVE_ZLIB
57 support |= HTTP_SUPPORT_ENCODINGS;
58 #endif
59
60 if (feature) {
61 return (feature == (support & feature));
62 }
63 return support;
64 }
65
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)
68 {
69 if (key && key_len) {
70 size_t i;
71 int wasalpha;
72 if ((wasalpha = isalpha((int) key[0]))) {
73 key[0] = (char) (uctitle ? toupper((int) key[0]) : tolower((int) key[0]));
74 }
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]));
78 wasalpha = 1;
79 } else {
80 if (xhyphen && (key[i] == '_')) {
81 key[i] = '-';
82 }
83 wasalpha = 0;
84 }
85 }
86 }
87 return key;
88 }
89 /* }}} */
90
91 /* {{{ */
92 void _http_key_list_default_decoder(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
93 {
94 *decoded = estrndup(encoded, encoded_len);
95 *decoded_len = (size_t) php_url_decode(*decoded, encoded_len);
96 }
97 /* }}} */
98
99 /* {{{ */
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)
101 {
102 const char *key = list, *val = NULL;
103 int vallen = 0, keylen = 0, done = 0;
104 zval array;
105
106 INIT_ZARR(array, items);
107
108 if (!(val = strchr(list, '='))) {
109 return FAILURE;
110 }
111
112 #define HTTP_KEYLIST_VAL(array, k, str, len) \
113 { \
114 char *decoded; \
115 size_t decoded_len; \
116 if (decode) { \
117 decode(str, len, &decoded, &decoded_len TSRMLS_CC); \
118 } else { \
119 decoded_len = len; \
120 decoded = estrndup(str, decoded_len); \
121 } \
122 add_assoc_stringl(array, k, decoded, decoded_len, 0); \
123 }
124 #define HTTP_KEYLIST_FIXKEY() \
125 { \
126 while (isspace(*key)) ++key; \
127 keylen = val - key; \
128 while (isspace(key[keylen - 1])) --keylen; \
129 }
130 #define HTTP_KEYLIST_FIXVAL() \
131 { \
132 ++val; \
133 while (isspace(*val)) ++val; \
134 vallen = key - val; \
135 while (isspace(val[vallen - 1])) --vallen; \
136 }
137
138 HTTP_KEYLIST_FIXKEY();
139
140 if (first_entry_is_name_value_pair) {
141 HTTP_KEYLIST_VAL(&array, "name", key, keylen);
142
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);
148 return SUCCESS;
149 }
150 /* additional info appended */
151 else {
152 HTTP_KEYLIST_FIXVAL();
153 HTTP_KEYLIST_VAL(&array, "value", val, vallen);
154 }
155 }
156
157 do {
158 char *keydup = NULL;
159
160 if (!(val = strchr(key, '='))) {
161 break;
162 }
163
164 /* start at 0 if first_entry_is_name_value_pair==0 */
165 if (zend_hash_num_elements(items)) {
166 ++key;
167 }
168
169 HTTP_KEYLIST_FIXKEY();
170 keydup = estrndup(key, keylen);
171 if (!(key = strchr(val, separator))) {
172 done = 1;
173 key = val + strlen(val);
174 }
175 HTTP_KEYLIST_FIXVAL();
176 HTTP_KEYLIST_VAL(&array, keydup, val, vallen);
177 efree(keydup);
178 } while (!done);
179
180 return SUCCESS;
181 }
182 /* }}} */
183
184 /* {{{ void http_error(long, long, char*) */
185 void _http_error_ex(long type TSRMLS_DC, long code, const char *format, ...)
186 {
187 va_list args;
188
189 va_start(args, format);
190 #ifdef ZEND_ENGINE_2
191 if ((type == E_THROW) || (PG(error_handling) == EH_THROW)) {
192 char *message;
193
194 vspprintf(&message, 0, format, args);
195 zend_throw_exception(http_exception_get_for_code(code), message, code TSRMLS_CC);
196 efree(message);
197 } else
198 #endif
199 php_verror(NULL, "", type, format, args TSRMLS_CC);
200 va_end(args);
201 }
202 /* }}} */
203
204 /* {{{ void http_log(char *, char *, char *) */
205 void _http_log_ex(char *file, const char *ident, const char *message TSRMLS_DC)
206 {
207 time_t now;
208 struct tm nowtm;
209 char datetime[20] = {0};
210
211 time(&now);
212 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
213
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); \
217 \
218 if (log) { \
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); \
221 } \
222 \
223 }
224
225 HTTP_LOG_WRITE(file, ident, message);
226 HTTP_LOG_WRITE(HTTP_G(log).composite, ident, message);
227 }
228 /* }}} */
229
230 static void http_ob_blackhole(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
231 {
232 *handled_output = ecalloc(1,1);
233 *handled_output_len = 0;
234 }
235
236 /* {{{ STATUS http_exit(int, char*, char*) */
237 STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header TSRMLS_DC)
238 {
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 : "");
242 STR_FREE(header);
243 STR_FREE(body);
244 return FAILURE;
245 }
246
247 php_end_ob_buffers(0 TSRMLS_CC);
248 if ((SUCCESS == sapi_send_headers(TSRMLS_C)) && body) {
249 PHPWRITE(body, strlen(body));
250 }
251
252 switch (status)
253 {
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;
262 }
263
264 STR_FREE(header);
265 STR_FREE(body);
266
267 if (HTTP_G(force_exit)) {
268 zend_bailout();
269 } else {
270 php_ob_set_internal_handler(http_ob_blackhole, 4096, "blackhole", 0 TSRMLS_CC);
271 }
272
273 return SUCCESS;
274 }
275 /* }}} */
276
277 /* {{{ STATUS http_check_method(char *) */
278 STATUS _http_check_method_ex(const char *method, const char *methods)
279 {
280 const char *found;
281
282 if ( (found = strstr(methods, method)) &&
283 (found == method || !isalpha(found[-1])) &&
284 (strlen(found) >= strlen(method) && !isalpha(found[strlen(method)]))) {
285 return SUCCESS;
286 }
287 return FAILURE;
288 }
289 /* }}} */
290
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)
293 {
294 zval **hsv;
295 zval **var;
296
297 if ((SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &hsv)) || (Z_TYPE_PP(hsv) != IS_ARRAY)) {
298 return NULL;
299 }
300 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), (char *) key, key_size, (void **) &var)) || (Z_TYPE_PP(var) != IS_STRING)) {
301 return NULL;
302 }
303 if (check && !(Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
304 return NULL;
305 }
306 return *var;
307 }
308 /* }}} */
309
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)
312 {
313 *length = 0;
314 *body = NULL;
315
316 if (SG(request_info).raw_post_data) {
317 *length = SG(request_info).raw_post_data_length;
318 *body = SG(request_info).raw_post_data;
319
320 if (dup) {
321 *body = estrndup(*body, *length);
322 }
323 return SUCCESS;
324 } else if (sapi_module.read_post && !HTTP_G(read_post_data)) {
325 char buf[4096];
326 int len;
327
328 HTTP_G(read_post_data) = 1;
329
330 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
331 *body = erealloc(*body, *length + len + 1);
332 memcpy(*body + *length, buf, len);
333 *length += len;
334 (*body)[*length] = '\0';
335 }
336
337 /* check for error */
338 if (len < 0) {
339 STR_FREE(*body);
340 *length = 0;
341 return FAILURE;
342 }
343
344 SG(request_info).raw_post_data = *body;
345 SG(request_info).raw_post_data_length = *length;
346
347 if (dup) {
348 *body = estrndup(*body, *length);
349 }
350 return SUCCESS;
351 }
352
353 return FAILURE;
354 }
355 /* }}} */
356
357 /* {{{ php_stream *_http_get_request_body_stream(void) */
358 PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D)
359 {
360 php_stream *s = NULL;
361
362 if (SG(request_info).raw_post_data) {
363 s = php_stream_open_wrapper("php://input", "rb", 0, NULL);
364 } else if (sapi_module.read_post && !HTTP_G(read_post_data)) {
365 HTTP_G(read_post_data) = 1;
366
367 if ((s = php_stream_temp_new())) {
368 char buf[4096];
369 int len;
370
371 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
372 php_stream_write(s, buf, len);
373 }
374
375 if (len < 0) {
376 php_stream_close(s);
377 s = NULL;
378 } else {
379 php_stream_rewind(s);
380 }
381 }
382 }
383
384 return s;
385 }
386 /* }}} */
387
388 /*
389 * Local variables:
390 * tab-width: 4
391 * c-basic-offset: 4
392 * End:
393 * vim600: noet sw=4 ts=4 fdm=marker
394 * vim<600: noet sw=4 ts=4
395 */
396