- PHP only reads the request body if the request method is POST
[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-2005, 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[128];
210
211 HTTP_CHECK_OPEN_BASEDIR(file, return);
212
213 time(&now);
214 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
215
216 #define HTTP_LOG_WRITE(file, type, msg) \
217 if (file && *file) { \
218 php_stream *log = php_stream_open_wrapper(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); \
219 \
220 if (log) { \
221 php_stream_printf(log TSRMLS_CC, "%s\t[%s]\t%s\t<%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
222 php_stream_close(log); \
223 } \
224 \
225 }
226
227 HTTP_LOG_WRITE(file, ident, message);
228 HTTP_LOG_WRITE(HTTP_G(log).composite, ident, message);
229 }
230 /* }}} */
231
232 static void http_ob_blackhole(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
233 {
234 *handled_output = ecalloc(1,1);
235 *handled_output_len = 0;
236 }
237
238 /* {{{ STATUS http_exit(int, char*, char*) */
239 STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header TSRMLS_DC)
240 {
241 if ( (send_header && (SUCCESS != http_send_status_header(status, header))) ||
242 (!send_header && status && (SUCCESS != http_send_status(status)))) {
243 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Failed to exit with status/header: %d - %s", status, header ? header : "");
244 STR_FREE(header);
245 STR_FREE(body);
246 return FAILURE;
247 }
248
249 php_end_ob_buffers(0 TSRMLS_CC);
250 if ((SUCCESS == sapi_send_headers(TSRMLS_C)) && body) {
251 PHPWRITE(body, strlen(body));
252 }
253
254 switch (status)
255 {
256 case 301: http_log(HTTP_G(log).redirect, "301-REDIRECT", header); break;
257 case 302: http_log(HTTP_G(log).redirect, "302-REDIRECT", header); break;
258 case 303: http_log(HTTP_G(log).redirect, "303-REDIRECT", header); break;
259 case 307: http_log(HTTP_G(log).redirect, "307-REDIRECT", header); break;
260 case 304: http_log(HTTP_G(log).cache, "304-CACHE", header); break;
261 case 405: http_log(HTTP_G(log).allowed_methods, "405-ALLOWED", header); break;
262 default: http_log(NULL, header, body); break;
263 }
264
265 STR_FREE(header);
266 STR_FREE(body);
267
268 if (HTTP_G(force_exit)) {
269 zend_bailout();
270 } else {
271 php_ob_set_internal_handler(http_ob_blackhole, 4096, "blackhole", 0 TSRMLS_CC);
272 }
273
274 return SUCCESS;
275 }
276 /* }}} */
277
278 /* {{{ STATUS http_check_method(char *) */
279 STATUS _http_check_method_ex(const char *method, const char *methods)
280 {
281 const char *found;
282
283 if ( (found = strstr(methods, method)) &&
284 (found == method || !isalpha(found[-1])) &&
285 (strlen(found) >= strlen(method) && !isalpha(found[strlen(method)]))) {
286 return SUCCESS;
287 }
288 return FAILURE;
289 }
290 /* }}} */
291
292 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
293 PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC)
294 {
295 zval **hsv;
296 zval **var;
297
298 if ((SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &hsv)) || (Z_TYPE_PP(hsv) != IS_ARRAY)) {
299 return NULL;
300 }
301 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), (char *) key, key_size, (void **) &var)) || (Z_TYPE_PP(var) != IS_STRING)) {
302 return NULL;
303 }
304 if (check && !(Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
305 return NULL;
306 }
307 return *var;
308 }
309 /* }}} */
310
311 /* {{{ STATUS http_get_request_body(char **, size_t *) */
312 PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_bool dup TSRMLS_DC)
313 {
314 *length = 0;
315 *body = NULL;
316
317 if (SG(request_info).raw_post_data) {
318 *length = SG(request_info).raw_post_data_length;
319 *body = (char *) (dup ? estrndup(SG(request_info).raw_post_data, *length) : SG(request_info).raw_post_data);
320 return SUCCESS;
321 }
322
323 /* PHP only reads POST */
324 if (sapi_module.read_post) {
325 char buf[4096];
326 int len;
327
328 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
329 *body = erealloc(*body, *length + len + 1);
330 memcpy(*body + *length, buf, len);
331 *length += len;
332 (*body)[*length] = '\0';
333 }
334
335 /* check for error */
336 if (len >= 0) {
337 /* connect to sapi module so it'll be freed */
338 if (!dup) {
339 SG(request_info).raw_post_data = *body;
340 SG(request_info).raw_post_data_length = *length;
341 }
342 return SUCCESS;
343 } else {
344 STR_FREE(*body);
345 *length = 0;
346 }
347 }
348
349 return FAILURE;
350 }
351 /* }}} */
352
353
354 /*
355 * Local variables:
356 * tab-width: 4
357 * c-basic-offset: 4
358 * End:
359 * vim600: noet sw=4 ts=4 fdm=marker
360 * vim<600: noet sw=4 ts=4
361 */
362