* fixed a LOAD of mem-leaks
[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 #define _WINSOCKAPI_
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <ctype.h>
26
27 #include "php.h"
28 #include "php_version.h"
29 #include "php_streams.h"
30 #include "snprintf.h"
31 #include "ext/standard/md5.h"
32 #include "ext/standard/url.h"
33 #include "ext/standard/base64.h"
34 #include "ext/standard/php_string.h"
35 #include "ext/standard/php_smart_str.h"
36 #include "ext/standard/php_lcg.h"
37 #include "ext/standard/php_filestat.h"
38
39 #include "SAPI.h"
40
41 #ifdef ZEND_ENGINE_2
42 # include "ext/standard/php_http.h"
43 #else
44 #include "http_build_query.c"
45 #endif
46
47 #include "php_http.h"
48 #include "php_http_api.h"
49
50 #ifdef HTTP_HAVE_CURL
51
52 # ifdef PHP_WIN32
53 # include <winsock2.h>
54 # include <sys/types.h>
55 # endif
56
57 # include <curl/curl.h>
58 # include <curl/easy.h>
59
60 #endif
61
62
63 ZEND_DECLARE_MODULE_GLOBALS(http)
64
65 /* {{{ day/month names */
66 static const char *days[] = {
67 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
68 };
69 static const char *wkdays[] = {
70 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
71 };
72 static const char *weekdays[] = {
73 "Monday", "Tuesday", "Wednesday",
74 "Thursday", "Friday", "Saturday", "Sunday"
75 };
76 static const char *months[] = {
77 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
78 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
79 };
80 enum assume_next {
81 DATE_MDAY,
82 DATE_YEAR,
83 DATE_TIME
84 };
85 static const struct time_zone {
86 const char *name;
87 const int offset;
88 } time_zones[] = {
89 {"GMT", 0}, /* Greenwich Mean */
90 {"UTC", 0}, /* Universal (Coordinated) */
91 {"WET", 0}, /* Western European */
92 {"BST", 0}, /* British Summer */
93 {"WAT", 60}, /* West Africa */
94 {"AST", 240}, /* Atlantic Standard */
95 {"ADT", 240}, /* Atlantic Daylight */
96 {"EST", 300}, /* Eastern Standard */
97 {"EDT", 300}, /* Eastern Daylight */
98 {"CST", 360}, /* Central Standard */
99 {"CDT", 360}, /* Central Daylight */
100 {"MST", 420}, /* Mountain Standard */
101 {"MDT", 420}, /* Mountain Daylight */
102 {"PST", 480}, /* Pacific Standard */
103 {"PDT", 480}, /* Pacific Daylight */
104 {"YST", 540}, /* Yukon Standard */
105 {"YDT", 540}, /* Yukon Daylight */
106 {"HST", 600}, /* Hawaii Standard */
107 {"HDT", 600}, /* Hawaii Daylight */
108 {"CAT", 600}, /* Central Alaska */
109 {"AHST", 600}, /* Alaska-Hawaii Standard */
110 {"NT", 660}, /* Nome */
111 {"IDLW", 720}, /* International Date Line West */
112 {"CET", -60}, /* Central European */
113 {"MET", -60}, /* Middle European */
114 {"MEWT", -60}, /* Middle European Winter */
115 {"MEST", -120}, /* Middle European Summer */
116 {"CEST", -120}, /* Central European Summer */
117 {"MESZ", -60}, /* Middle European Summer */
118 {"FWT", -60}, /* French Winter */
119 {"FST", -60}, /* French Summer */
120 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
121 {"WAST", -420}, /* West Australian Standard */
122 {"WADT", -420}, /* West Australian Daylight */
123 {"CCT", -480}, /* China Coast, USSR Zone 7 */
124 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
125 {"EAST", -600}, /* Eastern Australian Standard */
126 {"EADT", -600}, /* Eastern Australian Daylight */
127 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
128 {"NZT", -720}, /* New Zealand */
129 {"NZST", -720}, /* New Zealand Standard */
130 {"NZDT", -720}, /* New Zealand Daylight */
131 {"IDLE", -720}, /* International Date Line East */
132 };
133 /* }}} */
134
135 /* {{{ internals */
136
137 static int http_sort_q(const void *a, const void *b TSRMLS_DC);
138 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
139 static STATUS _http_send_chunk(const void *data, const size_t begin, const size_t end, const http_send_mode mode TSRMLS_DC);
140
141 static int check_day(char *day, size_t len);
142 static int check_month(char *month);
143 static int check_tzone(char *tzone);
144
145 static char *pretty_key(char *key, int key_len, int uctitle, int xhyphen);
146
147 static int http_ob_stack_get(php_ob_buffer *, php_ob_buffer **);
148
149 /* {{{ HAVE_CURL */
150 #ifdef HTTP_HAVE_CURL
151 #define http_curl_initbuf(m) _http_curl_initbuf((m) TSRMLS_CC)
152 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC);
153 #define http_curl_freebuf(m) _http_curl_freebuf((m) TSRMLS_CC)
154 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC);
155 #define http_curl_sizebuf(m, l) _http_curl_sizebuf((m), (l) TSRMLS_CC)
156 static inline void _http_curl_sizebuf(http_curlbuf_member member, size_t len TSRMLS_DC);
157 #define http_curl_movebuf(m, d, l) _http_curl_movebuf((m), (d), (l) TSRMLS_CC)
158 static inline void _http_curl_movebuf(http_curlbuf_member member, char **data, size_t *data_len TSRMLS_DC);
159 #define http_curl_copybuf(m, d, l) _http_curl_copybuf((m), (d), (l) TSRMLS_CC)
160 static inline void _http_curl_copybuf(http_curlbuf_member member, char **data, size_t *data_len TSRMLS_DC);
161 #define http_curl_setopts(c, u, o) _http_curl_setopts((c), (u), (o) TSRMLS_CC)
162 static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *options TSRMLS_DC);
163
164 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
165 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
166 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
167 static inline zval *_http_curl_getopt(HashTable *options, char *key TSRMLS_DC, int checks, ...);
168
169 static size_t http_curl_body_callback(char *, size_t, size_t, void *);
170 static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *);
171
172 #define http_curl_getinfo(c, h) _http_curl_getinfo((c), (h) TSRMLS_CC)
173 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC);
174 #define http_curl_getinfo_ex(c, i, a) _http_curl_getinfo_ex((c), (i), (a) TSRMLS_CC)
175 static inline void _http_curl_getinfo_ex(CURL *ch, CURLINFO i, zval *array TSRMLS_DC);
176 #define http_curl_getinfoname(i) _http_curl_getinfoname((i) TSRMLS_CC)
177 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC);
178
179 #endif
180 /* }}} HAVE_CURL */
181
182 /* {{{ static int http_sort_q(const void *, const void *) */
183 static int http_sort_q(const void *a, const void *b TSRMLS_DC)
184 {
185 Bucket *f, *s;
186 zval result, *first, *second;
187
188 f = *((Bucket **) a);
189 s = *((Bucket **) b);
190
191 first = *((zval **) f->pData);
192 second= *((zval **) s->pData);
193
194 if (numeric_compare_function(&result, first, second TSRMLS_CC) != SUCCESS) {
195 return 0;
196 }
197 return (Z_LVAL(result) > 0 ? -1 : (Z_LVAL(result) < 0 ? 1 : 0));
198 }
199 /* }}} */
200
201 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
202 http_send_mode) */
203 static STATUS _http_send_chunk(const void *data, const size_t begin,
204 const size_t end, const http_send_mode mode TSRMLS_DC)
205 {
206 char *buf;
207 size_t read = 0;
208 long len = end - begin;
209 php_stream *s;
210
211 switch (mode)
212 {
213 case SEND_RSRC:
214 s = (php_stream *) data;
215 if (php_stream_seek(s, begin, SEEK_SET)) {
216 return FAILURE;
217 }
218 buf = (char *) ecalloc(1, HTTP_BUF_SIZE);
219 /* read into buf and write out */
220 while ((len -= HTTP_BUF_SIZE) >= 0) {
221 if (!(read = php_stream_read(s, buf, HTTP_BUF_SIZE))) {
222 efree(buf);
223 return FAILURE;
224 }
225 if (read - php_body_write(buf, read TSRMLS_CC)) {
226 efree(buf);
227 return FAILURE;
228 }
229 }
230
231 /* read & write left over */
232 if (len) {
233 if (read = php_stream_read(s, buf, HTTP_BUF_SIZE + len)) {
234 if (read - php_body_write(buf, read TSRMLS_CC)) {
235 efree(buf);
236 return FAILURE;
237 }
238 } else {
239 efree(buf);
240 return FAILURE;
241 }
242 }
243 efree(buf);
244 return SUCCESS;
245 break;
246
247 case SEND_DATA:
248 return len == php_body_write(((char *)data) + begin, len TSRMLS_CC)
249 ? SUCCESS : FAILURE;
250 break;
251
252 default:
253 return FAILURE;
254 break;
255 }
256 }
257 /* }}} */
258
259 /* {{{ HAVE_CURL */
260 #ifdef HTTP_HAVE_CURL
261
262 /* {{{ static inline void http_curl_initbuf(http_curlbuf_member) */
263 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC)
264 {
265 http_curl_freebuf(member);
266
267 if (member & CURLBUF_HDRS) {
268 HTTP_G(curlbuf).hdrs.data = emalloc(HTTP_CURLBUF_HDRSSIZE);
269 HTTP_G(curlbuf).hdrs.free = HTTP_CURLBUF_HDRSSIZE;
270 }
271 if (member & CURLBUF_BODY) {
272 HTTP_G(curlbuf).body.data = emalloc(HTTP_CURLBUF_BODYSIZE);
273 HTTP_G(curlbuf).body.free = HTTP_CURLBUF_BODYSIZE;
274 }
275 }
276 /* }}} */
277
278 /* {{{ static inline void http_curl_freebuf(http_curlbuf_member) */
279 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC)
280 {
281 if (member & CURLBUF_HDRS) {
282 if (HTTP_G(curlbuf).hdrs.data) {
283 efree(HTTP_G(curlbuf).hdrs.data);
284 HTTP_G(curlbuf).hdrs.data = NULL;
285 }
286 HTTP_G(curlbuf).hdrs.used = 0;
287 HTTP_G(curlbuf).hdrs.free = 0;
288 }
289 if (member & CURLBUF_BODY) {
290 if (HTTP_G(curlbuf).body.data) {
291 efree(HTTP_G(curlbuf).body.data);
292 HTTP_G(curlbuf).body.data = NULL;
293 }
294 HTTP_G(curlbuf).body.used = 0;
295 HTTP_G(curlbuf).body.free = 0;
296 }
297 }
298 /* }}} */
299
300 /* {{{ static inline void http_curl_copybuf(http_curlbuf_member, char **,
301 size_t *) */
302 static inline void _http_curl_copybuf(http_curlbuf_member member, char **data,
303 size_t *data_len TSRMLS_DC)
304 {
305 *data = NULL;
306 *data_len = 0;
307
308 if ((member & CURLBUF_HDRS) && HTTP_G(curlbuf).hdrs.used) {
309 if ((member & CURLBUF_BODY) && HTTP_G(curlbuf).body.used) {
310 *data = emalloc(HTTP_G(curlbuf).hdrs.used + HTTP_G(curlbuf).body.used + 1);
311 } else {
312 *data = emalloc(HTTP_G(curlbuf).hdrs.used + 1);
313 }
314 memcpy(*data, HTTP_G(curlbuf).hdrs.data, HTTP_G(curlbuf).hdrs.used);
315 *data_len = HTTP_G(curlbuf).hdrs.used;
316 }
317
318 if ((member & CURLBUF_BODY) && HTTP_G(curlbuf).body.used) {
319 if (*data) {
320 memcpy((*data) + HTTP_G(curlbuf).hdrs.used,
321 HTTP_G(curlbuf).body.data, HTTP_G(curlbuf).body.used);
322 *data_len = HTTP_G(curlbuf).hdrs.used + HTTP_G(curlbuf).body.used;
323 } else {
324 emalloc(HTTP_G(curlbuf).body.used + 1);
325 memcpy(*data, HTTP_G(curlbuf).body.data, HTTP_G(curlbuf).body.used);
326 *data_len = HTTP_G(curlbuf).body.used;
327 }
328 }
329 if (*data) {
330 (*data)[*data_len] = 0;
331 } else {
332 *data = "";
333 }
334 }
335 /* }}} */
336
337 /* {{{ static inline void http_curl_movebuf(http_curlbuf_member, char **,
338 size_t *) */
339 static inline void _http_curl_movebuf(http_curlbuf_member member, char **data,
340 size_t *data_len TSRMLS_DC)
341 {
342 http_curl_copybuf(member, data, data_len);
343 http_curl_freebuf(member);
344 }
345 /* }}} */
346
347 /* {{{ static size_t http_curl_body_callback(char *, size_t, size_t, void *) */
348 static size_t http_curl_body_callback(char *buf, size_t len, size_t n, void *s)
349 {
350 TSRMLS_FETCH();
351
352 if ((len *= n) > HTTP_G(curlbuf).body.free) {
353 size_t bsize = HTTP_CURLBUF_BODYSIZE;
354 while (bsize < len) {
355 bsize *= 2;
356 }
357 HTTP_G(curlbuf).body.data = erealloc(HTTP_G(curlbuf).body.data,
358 HTTP_G(curlbuf).body.used + bsize);
359 HTTP_G(curlbuf).body.free += bsize;
360 }
361
362 memcpy(HTTP_G(curlbuf).body.data + HTTP_G(curlbuf).body.used, buf, len);
363 HTTP_G(curlbuf).body.free -= len;
364 HTTP_G(curlbuf).body.used += len;
365
366 return len;
367 }
368 /* }}} */
369
370 /* {{{ static size_t http_curl_hdrs_callback(char*, size_t, size_t, void *) */
371 static size_t http_curl_hdrs_callback(char *buf, size_t len, size_t n, void *s)
372 {
373 TSRMLS_FETCH();
374
375 /* discard previous headers */
376 if ((HTTP_G(curlbuf).hdrs.used) && (!strncmp(buf, "HTTP/1.", strlen("HTTP/1.")))) {
377 http_curl_initbuf(CURLBUF_HDRS);
378 }
379
380 if ((len *= n) > HTTP_G(curlbuf).hdrs.free) {
381 size_t bsize = HTTP_CURLBUF_HDRSSIZE;
382 while (bsize < len) {
383 bsize *= 2;
384 }
385 HTTP_G(curlbuf).hdrs.data = erealloc(HTTP_G(curlbuf).hdrs.data,
386 HTTP_G(curlbuf).hdrs.used + bsize);
387 HTTP_G(curlbuf).hdrs.free += bsize;
388 }
389
390 memcpy(HTTP_G(curlbuf).hdrs.data + HTTP_G(curlbuf).hdrs.used, buf, len);
391 HTTP_G(curlbuf).hdrs.free -= len;
392 HTTP_G(curlbuf).hdrs.used += len;
393
394 return len;
395 }
396 /* }}} */
397
398 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
399 static inline zval *_http_curl_getopt(HashTable *options, char *key TSRMLS_DC, int checks, ...)
400 {
401 zval **zoption;
402 va_list types;
403 int i;
404
405 if (SUCCESS != zend_hash_find(options, key, strlen(key) + 1, (void **) &zoption)) {
406 return NULL;
407 }
408 if (checks < 1) {
409 return *zoption;
410 }
411
412 va_start(types, checks);
413 for (i = 0; i < checks; ++i) {
414 if ((va_arg(types, int)) == (Z_TYPE_PP(zoption))) {
415 va_end(types);
416 return *zoption;
417 }
418 }
419 va_end(types);
420 return NULL;
421 }
422 /* }}} */
423
424 /* {{{ static inline void http_curl_setopts(CURL *, char *, HashTable *) */
425 static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *options TSRMLS_DC)
426 {
427 zval *zoption;
428
429 /* standard options */
430 curl_easy_setopt(ch, CURLOPT_URL, url);
431 curl_easy_setopt(ch, CURLOPT_HEADER, 0);
432 curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1);
433 curl_easy_setopt(ch, CURLOPT_AUTOREFERER, 1);
434 curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, http_curl_body_callback);
435 curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, http_curl_hdrs_callback);
436 #ifdef ZTS
437 curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1);
438 #endif
439
440 if ((!options) || (1 > zend_hash_num_elements(options))) {
441 return;
442 }
443
444 /* redirects, defaults to 0 */
445 if (zoption = http_curl_getopt1(options, "redirect", IS_LONG)) {
446 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
447 curl_easy_setopt(ch, CURLOPT_MAXREDIRS, Z_LVAL_P(zoption));
448 if (zoption = http_curl_getopt2(options, "unrestrictedauth", IS_LONG, IS_BOOL)) {
449 curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
450 }
451 } else {
452 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0);
453 }
454
455 /* proxy */
456 if (zoption = http_curl_getopt1(options, "proxyhost", IS_STRING)) {
457 curl_easy_setopt(ch, CURLOPT_PROXY, Z_STRVAL_P(zoption));
458 /* port */
459 if (zoption = http_curl_getopt1(options, "proxyport", IS_LONG)) {
460 curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
461 }
462 /* user:pass */
463 if (zoption = http_curl_getopt1(options, "proxyauth", IS_STRING)) {
464 curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption));
465 }
466 /* auth method */
467 if (zoption = http_curl_getopt1(options, "proxyauthtype", IS_LONG)) {
468 curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
469 }
470 }
471
472 /* auth */
473 if (zoption = http_curl_getopt1(options, "httpauth", IS_STRING)) {
474 curl_easy_setopt(ch, CURLOPT_USERPWD, Z_STRVAL_P(zoption));
475 }
476 if (zoption = http_curl_getopt1(options, "httpauthtype", IS_LONG)) {
477 curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
478 }
479
480 /* compress, enabled by default (empty string enables deflate and gzip) */
481 if (zoption = http_curl_getopt2(options, "compress", IS_LONG, IS_BOOL)) {
482 if (Z_LVAL_P(zoption)) {
483 curl_easy_setopt(ch, CURLOPT_ENCODING, "");
484 }
485 } else {
486 curl_easy_setopt(ch, CURLOPT_ENCODING, "");
487 }
488
489 /* another port */
490 if (zoption = http_curl_getopt1(options, "port", IS_LONG)) {
491 curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption));
492 }
493
494 /* referer */
495 if (zoption = http_curl_getopt1(options, "referer", IS_STRING)) {
496 curl_easy_setopt(ch, CURLOPT_REFERER, Z_STRVAL_P(zoption));
497 }
498
499 /* useragent, default "PECL::HTTP/version (PHP/version)" */
500 if (zoption = http_curl_getopt1(options, "useragent", IS_STRING)) {
501 curl_easy_setopt(ch, CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
502 } else {
503 curl_easy_setopt(ch, CURLOPT_USERAGENT,
504 "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
505 }
506
507 /* cookies, array('name' => 'value') */
508 if (zoption = http_curl_getopt1(options, "cookies", IS_ARRAY)) {
509 char *cookie_key;
510 zval **cookie_val;
511 int key_type;
512 smart_str qstr = {0};
513
514 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
515 while (HASH_KEY_NON_EXISTANT != (key_type = zend_hash_get_current_key_type(Z_ARRVAL_P(zoption)))) {
516 if (key_type == HASH_KEY_IS_STRING) {
517 zend_hash_get_current_key(Z_ARRVAL_P(zoption), &cookie_key, NULL, 0);
518 zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val);
519 smart_str_appends(&qstr, cookie_key);
520 smart_str_appendl(&qstr, "=", 1);
521 smart_str_appendl(&qstr, Z_STRVAL_PP(cookie_val), Z_STRLEN_PP(cookie_val));
522 smart_str_appendl(&qstr, "; ", 2);
523 zend_hash_move_forward(Z_ARRVAL_P(zoption));
524 }
525 }
526 smart_str_0(&qstr);
527
528 if (qstr.c) {
529 curl_easy_setopt(ch, CURLOPT_COOKIE, qstr.c);
530 }
531 }
532
533 /* cookiestore */
534 if (zoption = http_curl_getopt1(options, "cookiestore", IS_STRING)) {
535 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, Z_STRVAL_P(zoption));
536 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, Z_STRVAL_P(zoption));
537 }
538
539 /* additional headers, array('name' => 'value') */
540 if (zoption = http_curl_getopt1(options, "headers", IS_ARRAY)) {
541 int key_type;
542 char *header_key, header[1024] = {0};
543 zval **header_val;
544 struct curl_slist *headers = NULL;
545
546 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
547 while (HASH_KEY_NON_EXISTANT != (key_type = zend_hash_get_current_key_type(Z_ARRVAL_P(zoption)))) {
548 if (key_type == HASH_KEY_IS_STRING) {
549 zend_hash_get_current_key(Z_ARRVAL_P(zoption), &header_key, NULL, 0);
550 zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val);
551 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
552 headers = curl_slist_append(headers, header);
553 zend_hash_move_forward(Z_ARRVAL_P(zoption));
554 }
555 }
556 if (headers) {
557 curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
558 }
559 }
560 }
561 /* }}} */
562
563 /* {{{ static inline char *http_curl_getinfoname(CURLINFO) */
564 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC)
565 {
566 #define CASE(I) case CURLINFO_ ##I : { static char I[] = #I; return pretty_key(I, sizeof(#I)-1, 0, 0); }
567 switch (i)
568 {
569 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
570 CASE(EFFECTIVE_URL);
571 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
572 CASE(RESPONSE_CODE);
573 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
574 CASE(TOTAL_TIME);
575 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
576 CASE(NAMELOOKUP_TIME);
577 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
578 CASE(CONNECT_TIME);
579 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
580 CASE(PRETRANSFER_TIME);
581 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
582 CASE(SIZE_UPLOAD);
583 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
584 CASE(SIZE_DOWNLOAD);
585 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
586 CASE(SPEED_DOWNLOAD);
587 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
588 CASE(SPEED_UPLOAD);
589 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
590 CASE(HEADER_SIZE);
591 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
592 CASE(REQUEST_SIZE);
593 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
594 CASE(SSL_VERIFYRESULT);
595 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
596 CASE(FILETIME);
597 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
598 CASE(CONTENT_LENGTH_DOWNLOAD);
599 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
600 CASE(CONTENT_LENGTH_UPLOAD);
601 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
602 CASE(STARTTRANSFER_TIME);
603 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
604 CASE(CONTENT_TYPE);
605 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
606 CASE(REDIRECT_TIME);
607 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
608 CASE(REDIRECT_COUNT);
609 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
610 CASE(PRIVATE);
611 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
612 CASE(HTTP_CONNECTCODE);
613 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
614 CASE(HTTPAUTH_AVAIL);
615 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
616 CASE(PROXYAUTH_AVAIL);
617 }
618 #undef CASE
619 return NULL;
620 }
621 /* }}} */
622
623 /* {{{ static inline void http_curl_getinfo_ex(CURL, CURLINFO, zval *) */
624 static inline void _http_curl_getinfo_ex(CURL *ch, CURLINFO i, zval *array TSRMLS_DC)
625 {
626 char *key;
627 if (key = http_curl_getinfoname(i)) {
628 switch (i & ~CURLINFO_MASK)
629 {
630 case CURLINFO_STRING:
631 {
632 char *c;
633 if (CURLE_OK == curl_easy_getinfo(ch, i, &c)) {
634 add_assoc_string(array, key, c ? c : "", 1);
635 }
636 }
637 break;
638
639 case CURLINFO_DOUBLE:
640 {
641 double d;
642 if (CURLE_OK == curl_easy_getinfo(ch, i, &d)) {
643 add_assoc_double(array, key, d);
644 }
645 }
646 break;
647
648 case CURLINFO_LONG:
649 {
650 long l;
651 if (CURLE_OK == curl_easy_getinfo(ch, i, &l)) {
652 add_assoc_long(array, key, l);
653 }
654 }
655 break;
656 }
657 }
658 }
659 /* }}} */
660
661 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
662 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC)
663 {
664 zval array;
665 Z_ARRVAL(array) = info;
666
667 #define INFO(I) http_curl_getinfo_ex(ch, CURLINFO_ ##I , &array)
668 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
669 INFO(EFFECTIVE_URL);
670 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
671 INFO(RESPONSE_CODE);
672 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
673 INFO(TOTAL_TIME);
674 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
675 INFO(NAMELOOKUP_TIME);
676 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
677 INFO(CONNECT_TIME);
678 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
679 INFO(PRETRANSFER_TIME);
680 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
681 INFO(SIZE_UPLOAD);
682 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
683 INFO(SIZE_DOWNLOAD);
684 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
685 INFO(SPEED_DOWNLOAD);
686 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
687 INFO(SPEED_UPLOAD);
688 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
689 INFO(HEADER_SIZE);
690 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
691 INFO(REQUEST_SIZE);
692 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
693 INFO(SSL_VERIFYRESULT);
694 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
695 INFO(FILETIME);
696 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
697 INFO(CONTENT_LENGTH_DOWNLOAD);
698 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
699 INFO(CONTENT_LENGTH_UPLOAD);
700 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
701 INFO(STARTTRANSFER_TIME);
702 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
703 INFO(CONTENT_TYPE);
704 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
705 INFO(REDIRECT_TIME);
706 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
707 INFO(REDIRECT_COUNT);
708 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
709 INFO(PRIVATE);
710 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
711 INFO(HTTP_CONNECTCODE);
712 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
713 INFO(HTTPAUTH_AVAIL);
714 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
715 INFO(PROXYAUTH_AVAIL);
716 #undef INFO
717 }
718 /* }}} */
719
720 #endif
721 /* }}} HAVE_CURL */
722
723 /* {{{ Day/Month/TZ checks for http_parse_date()
724 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
725 static int check_day(char *day, size_t len)
726 {
727 int i;
728 const char * const *check = (len > 3) ? &weekdays[0] : &wkdays[0];
729 for (i = 0; i < 7; i++) {
730 if (!strcmp(day, check[0])) {
731 return i;
732 }
733 check++;
734 }
735 return -1;
736 }
737
738 static int check_month(char *month)
739 {
740 int i;
741 const char * const *check = &months[0];
742 for (i = 0; i < 12; i++) {
743 if (!strcmp(month, check[0])) {
744 return i;
745 }
746 check++;
747 }
748 return -1;
749 }
750
751 /* return the time zone offset between GMT and the input one, in number
752 of seconds or -1 if the timezone wasn't found/legal */
753
754 static int check_tzone(char *tzone)
755 {
756 int i;
757 const struct time_zone *check = time_zones;
758 for (i = 0; i < sizeof(time_zones) / sizeof(time_zones[0]); i++) {
759 if (!strcmp(tzone, check->name)) {
760 return check->offset * 60;
761 }
762 check++;
763 }
764 return -1;
765 }
766 /* }}} */
767
768 /* static char *pretty_key(char *, int, int, int) */
769 static char *pretty_key(char *key, int key_len, int uctitle, int xhyphen)
770 {
771 if (key && key_len) {
772 int i, wasalpha;
773 if (wasalpha = isalpha(key[0])) {
774 key[0] = uctitle ? toupper(key[0]) : tolower(key[0]);
775 }
776 for (i = 1; i < key_len; i++) {
777 if (isalpha(key[i])) {
778 key[i] = ((!wasalpha) && uctitle) ? toupper(key[i]) : tolower(key[i]);
779 wasalpha = 1;
780 } else {
781 if (xhyphen && (key[i] == '_')) {
782 key[i] = '-';
783 }
784 wasalpha = 0;
785 }
786 }
787 }
788 return key;
789 }
790 /* }}} */
791
792 /* {{{ static STATUS http_ob_stack_get(php_ob_buffer *, php_ob_buffer **) */
793 static STATUS http_ob_stack_get(php_ob_buffer *o, php_ob_buffer **s)
794 {
795 static int i = 0;
796 php_ob_buffer *b = emalloc(sizeof(php_ob_buffer));
797 b->handler_name = estrdup(o->handler_name);
798 b->buffer = estrndup(o->buffer, o->text_length);
799 b->text_length = o->text_length;
800 b->chunk_size = o->chunk_size;
801 b->erase = o->erase;
802 s[i++] = b;
803 return SUCCESS;
804 }
805 /* }}} */
806
807 /* }}} internals */
808
809 /* {{{ public API */
810
811 /* {{{ char *http_date(time_t) */
812 PHP_HTTP_API char *_http_date(time_t t TSRMLS_DC)
813 {
814 struct tm *gmtime, tmbuf;
815 char *date = ecalloc(31, 1);
816
817 gmtime = php_gmtime_r(&t, &tmbuf);
818 snprintf(date, 30,
819 "%s, %02d %s %04d %02d:%02d:%02d GMT",
820 days[gmtime->tm_wday], gmtime->tm_mday,
821 months[gmtime->tm_mon], gmtime->tm_year + 1900,
822 gmtime->tm_hour, gmtime->tm_min, gmtime->tm_sec
823 );
824 return date;
825 }
826 /* }}} */
827
828 /* {{{ time_t http_parse_date(char *)
829 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
830 PHP_HTTP_API time_t _http_parse_date(const char *date)
831 {
832 time_t t = 0;
833 int tz_offset = -1, year = -1, month = -1, monthday = -1, weekday = -1,
834 hours = -1, minutes = -1, seconds = -1;
835 struct tm tm;
836 enum assume_next dignext = DATE_MDAY;
837 const char *indate = date;
838
839 int found = 0, part = 0; /* max 6 parts */
840
841 while (*date && (part < 6)) {
842 int found = 0;
843
844 while (*date && !isalnum(*date)) {
845 date++;
846 }
847
848 if (isalpha(*date)) {
849 /* a name coming up */
850 char buf[32] = "";
851 size_t len;
852 sscanf(date, "%31[A-Za-z]", buf);
853 len = strlen(buf);
854
855 if (weekday == -1) {
856 weekday = check_day(buf, len);
857 if (weekday != -1) {
858 found = 1;
859 }
860 }
861
862 if (!found && (month == -1)) {
863 month = check_month(buf);
864 if (month != -1) {
865 found = 1;
866 }
867 }
868
869 if (!found && (tz_offset == -1)) {
870 /* this just must be a time zone string */
871 tz_offset = check_tzone(buf);
872 if (tz_offset != -1) {
873 found = 1;
874 }
875 }
876
877 if (!found) {
878 return -1; /* bad string */
879 }
880 date += len;
881 }
882 else if (isdigit(*date)) {
883 /* a digit */
884 int val;
885 char *end;
886 if((seconds == -1) && (3 == sscanf(date, "%02d:%02d:%02d", &hours, &minutes, &seconds))) {
887 /* time stamp! */
888 date += 8;
889 found = 1;
890 }
891 else {
892 val = (int) strtol(date, &end, 10);
893
894 if ((tz_offset == -1) && ((end - date) == 4) && (val < 1300) && (indate < date) && ((date[-1] == '+' || date[-1] == '-'))) {
895 /* four digits and a value less than 1300 and it is preceeded with
896 a plus or minus. This is a time zone indication. */
897 found = 1;
898 tz_offset = (val / 100 * 60 + val % 100) * 60;
899
900 /* the + and - prefix indicates the local time compared to GMT,
901 this we need ther reversed math to get what we want */
902 tz_offset = date[-1] == '+' ? -tz_offset : tz_offset;
903 }
904
905 if (((end - date) == 8) && (year == -1) && (month == -1) && (monthday == -1)) {
906 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
907 found = 1;
908 year = val / 10000;
909 month = (val % 10000) / 100 - 1; /* month is 0 - 11 */
910 monthday = val % 100;
911 }
912
913 if (!found && (dignext == DATE_MDAY) && (monthday == -1)) {
914 if ((val > 0) && (val < 32)) {
915 monthday = val;
916 found = 1;
917 }
918 dignext = DATE_YEAR;
919 }
920
921 if (!found && (dignext == DATE_YEAR) && (year == -1)) {
922 year = val;
923 found = 1;
924 if (year < 1900) {
925 year += year > 70 ? 1900 : 2000;
926 }
927 if(monthday == -1) {
928 dignext = DATE_MDAY;
929 }
930 }
931
932 if (!found) {
933 return -1;
934 }
935
936 date = end;
937 }
938 }
939
940 part++;
941 }
942
943 if (-1 == seconds) {
944 seconds = minutes = hours = 0; /* no time, make it zero */
945 }
946
947 if ((-1 == monthday) || (-1 == month) || (-1 == year)) {
948 /* lacks vital info, fail */
949 return -1;
950 }
951
952 if (sizeof(time_t) < 5) {
953 /* 32 bit time_t can only hold dates to the beginning of 2038 */
954 if (year > 2037) {
955 return 0x7fffffff;
956 }
957 }
958
959 tm.tm_sec = seconds;
960 tm.tm_min = minutes;
961 tm.tm_hour = hours;
962 tm.tm_mday = monthday;
963 tm.tm_mon = month;
964 tm.tm_year = year - 1900;
965 tm.tm_wday = 0;
966 tm.tm_yday = 0;
967 tm.tm_isdst = 0;
968
969 t = mktime(&tm);
970
971 /* time zone adjust */
972 {
973 struct tm *gmt, keeptime2;
974 long delta;
975 time_t t2;
976
977 if(!(gmt = php_gmtime_r(&t, &keeptime2))) {
978 return -1; /* illegal date/time */
979 }
980
981 t2 = mktime(gmt);
982
983 /* Add the time zone diff (between the given timezone and GMT) and the
984 diff between the local time zone and GMT. */
985 delta = (tz_offset != -1 ? tz_offset : 0) + (t - t2);
986
987 if((delta > 0) && (t + delta < t)) {
988 return -1; /* time_t overflow */
989 }
990
991 t += delta;
992 }
993
994 return t;
995 }
996 /* }}} */
997
998 /* {{{ inline char *http_etag(void *, size_t, http_send_mode) */
999 PHP_HTTP_API inline char *_http_etag(const void *data_ptr, const size_t data_len,
1000 const http_send_mode data_mode TSRMLS_DC)
1001 {
1002 char ssb_buf[128] = {0};
1003 unsigned char digest[16];
1004 PHP_MD5_CTX ctx;
1005 char *new_etag = ecalloc(33, 1);
1006
1007 PHP_MD5Init(&ctx);
1008
1009 switch (data_mode)
1010 {
1011 case SEND_DATA:
1012 PHP_MD5Update(&ctx, data_ptr, data_len);
1013 break;
1014
1015 case SEND_RSRC:
1016 if (!HTTP_G(ssb).sb.st_ino) {
1017 if (php_stream_stat((php_stream *) data_ptr, &HTTP_G(ssb))) {
1018 return NULL;
1019 }
1020 }
1021 snprintf(ssb_buf, 127, "%ld=%ld=%ld",
1022 HTTP_G(ssb).sb.st_mtime,
1023 HTTP_G(ssb).sb.st_ino,
1024 HTTP_G(ssb).sb.st_size
1025 );
1026 PHP_MD5Update(&ctx, ssb_buf, strlen(ssb_buf));
1027 break;
1028
1029 default:
1030 efree(new_etag);
1031 return NULL;
1032 break;
1033 }
1034
1035 PHP_MD5Final(digest, &ctx);
1036 make_digest(new_etag, digest);
1037
1038 return new_etag;
1039 }
1040 /* }}} */
1041
1042 /* {{{ inline http_lmod(void *, http_send_mode) */
1043 PHP_HTTP_API inline time_t _http_lmod(const void *data_ptr, const http_send_mode data_mode TSRMLS_DC)
1044 {
1045 switch (data_mode)
1046 {
1047 case SEND_DATA:
1048 {
1049 return time(NULL);
1050 }
1051
1052 case SEND_RSRC:
1053 {
1054 if (!HTTP_G(ssb).sb.st_mtime) {
1055 if (php_stream_stat((php_stream *) data_ptr, &HTTP_G(ssb))) {
1056 return 0;
1057 }
1058 }
1059 return HTTP_G(ssb).sb.st_mtime;
1060 }
1061
1062 default:
1063 {
1064 zval mtime;
1065 php_stat(Z_STRVAL_P((zval *) data_ptr), Z_STRLEN_P((zval *) data_ptr), 6, &mtime TSRMLS_CC);
1066 return Z_LVAL(mtime);
1067 }
1068 }
1069 }
1070 /* }}} */
1071
1072 /* {{{inline int http_is_range_request(void) */
1073 PHP_HTTP_API inline int _http_is_range_request(TSRMLS_D)
1074 {
1075 return zend_hash_exists(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]),
1076 "HTTP_RANGE", strlen("HTTP_RANGE") + 1);
1077 }
1078 /* }}} */
1079
1080 /* {{{ inline STATUS http_send_status(int) */
1081 PHP_HTTP_API inline STATUS _http_send_status(const int status TSRMLS_DC)
1082 {
1083 int s = status;
1084 return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) s TSRMLS_CC);
1085 }
1086 /* }}} */
1087
1088 /* {{{ inline STATUS http_send_header(char *) */
1089 PHP_HTTP_API inline STATUS _http_send_header(const char *header TSRMLS_DC)
1090 {
1091 return http_send_status_header(0, header);
1092 }
1093 /* }}} */
1094
1095 /* {{{ inline STATUS http_send_status_header(int, char *) */
1096 PHP_HTTP_API inline STATUS _http_send_status_header(const int status, const char *header TSRMLS_DC)
1097 {
1098 sapi_header_line h = {(char *) header, strlen(header), status};
1099 return sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC);
1100 }
1101 /* }}} */
1102
1103 /* {{{ inline zval *http_get_server_var(char *) */
1104 PHP_HTTP_API inline zval *_http_get_server_var(const char *key TSRMLS_DC)
1105 {
1106 zval **var;
1107 if (SUCCESS == zend_hash_find(
1108 HTTP_SERVER_VARS,
1109 (char *) key, strlen(key) + 1, (void **) &var)) {
1110 return *var;
1111 }
1112 return NULL;
1113 }
1114 /* }}} */
1115
1116 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
1117 PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len,
1118 char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
1119 {
1120 char etag[33] = { 0 };
1121 unsigned char digest[16];
1122
1123 if (mode & PHP_OUTPUT_HANDLER_START) {
1124 PHP_MD5Init(&HTTP_G(etag_md5));
1125 }
1126
1127 PHP_MD5Update(&HTTP_G(etag_md5), output, output_len);
1128
1129 if (mode & PHP_OUTPUT_HANDLER_END) {
1130 PHP_MD5Final(digest, &HTTP_G(etag_md5));
1131
1132 /* just do that if desired */
1133 if (HTTP_G(etag_started)) {
1134 make_digest(etag, digest);
1135
1136 if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
1137 http_send_status(304);
1138 } else {
1139 http_send_etag(etag, 32);
1140 }
1141 }
1142 }
1143
1144 *handled_output_len = output_len;
1145 *handled_output = estrndup(output, output_len);
1146 }
1147 /* }}} */
1148
1149 /* {{{ STATUS http_start_ob_handler(php_output_handler_func_t, char *, uint, zend_bool) */
1150 PHP_HTTP_API STATUS _http_start_ob_handler(php_output_handler_func_t handler_func,
1151 char *handler_name, uint chunk_size, zend_bool erase TSRMLS_DC)
1152 {
1153 php_ob_buffer **stack;
1154 int count, i;
1155
1156 if (count = OG(ob_nesting_level)) {
1157 stack = ecalloc(sizeof(php_ob_buffer *), count);
1158
1159 if (count > 1) {
1160 zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP,
1161 (int (*)(void *elem, void *)) http_ob_stack_get, stack);
1162 }
1163
1164 if (count > 0) {
1165 http_ob_stack_get(&OG(active_ob_buffer), stack);
1166 }
1167
1168 while (OG(ob_nesting_level)) {
1169 php_end_ob_buffer(0, 0 TSRMLS_CC);
1170 }
1171 }
1172
1173 php_ob_set_internal_handler(handler_func, chunk_size, handler_name, erase TSRMLS_CC);
1174
1175 for (i = 0; i < count; i++) {
1176 php_ob_buffer *s = stack[i];
1177 if (strcmp(s->handler_name, "default output handler")) {
1178 php_start_ob_buffer_named(s->handler_name, s->chunk_size, s->erase TSRMLS_CC);
1179 }
1180 php_body_write(s->buffer, s->text_length TSRMLS_CC);
1181 efree(s->handler_name);
1182 efree(s->buffer);
1183 efree(s);
1184 }
1185 if (count) {
1186 efree(stack);
1187 }
1188
1189 return SUCCESS;
1190 }
1191 /* }}} */
1192
1193 /* {{{ int http_modified_match(char *, int) */
1194 PHP_HTTP_API int _http_modified_match(const char *entry, const time_t t TSRMLS_DC)
1195 {
1196 int retval;
1197 zval *zmodified;
1198 char *modified, *chr_ptr;
1199
1200 HTTP_GSC(zmodified, entry, 0);
1201
1202 modified = estrndup(Z_STRVAL_P(zmodified), Z_STRLEN_P(zmodified));
1203 if (chr_ptr = strrchr(modified, ';')) {
1204 chr_ptr = 0;
1205 }
1206 retval = (t <= http_parse_date(modified));
1207 efree(modified);
1208 return retval;
1209 }
1210 /* }}} */
1211
1212 /* {{{ int http_etag_match(char *, char *) */
1213 PHP_HTTP_API int _http_etag_match(const char *entry, const char *etag TSRMLS_DC)
1214 {
1215 zval *zetag;
1216 char *quoted_etag;
1217 STATUS result;
1218
1219 HTTP_GSC(zetag, entry, 0);
1220
1221 if (NULL != strchr(Z_STRVAL_P(zetag), '*')) {
1222 return 1;
1223 }
1224
1225 quoted_etag = (char *) emalloc(strlen(etag) + 3);
1226 sprintf(quoted_etag, "\"%s\"", etag);
1227
1228 if (!strchr(Z_STRVAL_P(zetag), ',')) {
1229 result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
1230 } else {
1231 result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
1232 }
1233 efree(quoted_etag);
1234 return result;
1235 }
1236 /* }}} */
1237
1238 /* {{{ STATUS http_send_last_modified(int) */
1239 PHP_HTTP_API STATUS _http_send_last_modified(const time_t t TSRMLS_DC)
1240 {
1241 char modified[96] = "Last-Modified: ", *date;
1242 date = http_date(t);
1243 strcat(modified, date);
1244 efree(date);
1245
1246 /* remember */
1247 HTTP_G(lmod) = t;
1248
1249 return http_send_header(modified);
1250 }
1251 /* }}} */
1252
1253 /* {{{ static STATUS http_send_etag(char *, int) */
1254 PHP_HTTP_API STATUS _http_send_etag(const char *etag,
1255 const int etag_len TSRMLS_DC)
1256 {
1257 STATUS status;
1258 char *etag_header;
1259
1260 if (!etag_len){
1261 php_error_docref(NULL TSRMLS_CC,E_ERROR,
1262 "Attempt to send empty ETag (previous: %s)\n", HTTP_G(etag));
1263 return FAILURE;
1264 }
1265
1266 /* remember */
1267 if (HTTP_G(etag)) {
1268 efree(HTTP_G(etag));
1269 }
1270 HTTP_G(etag) = estrdup(etag);
1271
1272 etag_header = ecalloc(sizeof("ETag: \"\"") + etag_len, 1);
1273 sprintf(etag_header, "ETag: \"%s\"", etag);
1274 if (SUCCESS != (status = http_send_header(etag_header))) {
1275 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't send '%s' header", etag_header);
1276 }
1277 efree(etag_header);
1278 return status;
1279 }
1280 /* }}} */
1281
1282 /* {{{ STATUS http_send_cache_control(char *, size_t) */
1283 PHP_HTTP_API STATUS _http_send_cache_control(const char *cache_control,
1284 const size_t cc_len TSRMLS_DC)
1285 {
1286 STATUS status;
1287 char *cc_header = ecalloc(sizeof("Cache-Control: ") + cc_len, 1);
1288
1289 sprintf(cc_header, "Cache-Control: %s", cache_control);
1290 if (SUCCESS != (status = http_send_header(cc_header))) {
1291 php_error_docref(NULL TSRMLS_CC, E_NOTICE,
1292 "Could not send '%s' header", cc_header);
1293 }
1294 efree(cc_header);
1295 return status;
1296 }
1297 /* }}} */
1298
1299 /* {{{ STATUS http_send_content_type(char *, size_t) */
1300 PHP_HTTP_API STATUS _http_send_content_type(const char *content_type,
1301 const size_t ct_len TSRMLS_DC)
1302 {
1303 STATUS status;
1304 char *ct_header;
1305
1306 if (!strchr(content_type, '/')) {
1307 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1308 "Content-Type '%s' doesn't seem to consist of a primary and a secondary part",
1309 content_type);
1310 return FAILURE;
1311 }
1312
1313 /* remember for multiple ranges */
1314 if (HTTP_G(ctype)) {
1315 efree(HTTP_G(ctype));
1316 }
1317 HTTP_G(ctype) = estrndup(content_type, ct_len);
1318
1319 ct_header = ecalloc(sizeof("Content-Type: ") + ct_len, 1);
1320 sprintf(ct_header, "Content-Type: %s", content_type);
1321
1322 if (SUCCESS != (status = http_send_header(ct_header))) {
1323 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1324 "Couldn't send '%s' header", ct_header);
1325 }
1326 efree(ct_header);
1327 return status;
1328 }
1329 /* }}} */
1330
1331 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
1332 PHP_HTTP_API STATUS _http_send_content_disposition(const char *filename,
1333 const size_t f_len, const int send_inline TSRMLS_DC)
1334 {
1335 STATUS status;
1336 char *cd_header;
1337
1338 if (send_inline) {
1339 cd_header = ecalloc(sizeof("Content-Disposition: inline; filename=\"\"") + f_len, 1);
1340 sprintf(cd_header, "Content-Disposition: inline; filename=\"%s\"", filename);
1341 } else {
1342 cd_header = ecalloc(sizeof("Content-Disposition: attachment; filename=\"\"") + f_len, 1);
1343 sprintf(cd_header, "Content-Disposition: attachment; filename=\"%s\"", filename);
1344 }
1345
1346 if (SUCCESS != (status = http_send_header(cd_header))) {
1347 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't send '%s' header", cd_header);
1348 }
1349 efree(cd_header);
1350 return status;
1351 }
1352 /* }}} */
1353
1354 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
1355 PHP_HTTP_API STATUS _http_cache_last_modified(const time_t last_modified,
1356 const time_t send_modified, const char *cache_control, const size_t cc_len TSRMLS_DC)
1357 {
1358 if (cc_len) {
1359 http_send_cache_control(cache_control, cc_len);
1360 }
1361
1362 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified)) {
1363 if (SUCCESS == http_send_status(304)) {
1364 zend_bailout();
1365 } else {
1366 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
1367 return FAILURE;
1368 }
1369 }
1370 return http_send_last_modified(send_modified);
1371 }
1372 /* }}} */
1373
1374 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
1375 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, const size_t etag_len,
1376 const char *cache_control, const size_t cc_len TSRMLS_DC)
1377 {
1378 if (cc_len) {
1379 http_send_cache_control(cache_control, cc_len);
1380 }
1381
1382 if (etag_len) {
1383 http_send_etag(etag, etag_len);
1384 if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
1385 if (SUCCESS == http_send_status(304)) {
1386 zend_bailout();
1387 } else {
1388 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
1389 return FAILURE;
1390 }
1391 }
1392 }
1393
1394 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
1395 if (!HTTP_G(etag_started)) {
1396 if (SUCCESS == http_start_ob_handler(_http_ob_etaghandler, "ob_etaghandler", 4096, 1)) {
1397 HTTP_G(etag_started) = 1;
1398 return SUCCESS;
1399 } else {
1400 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not start ob_etaghandler");
1401 return FAILURE;
1402 }
1403 }
1404 return SUCCESS;
1405 }
1406 /* }}} */
1407
1408 /* {{{ char *http_absolute_uri(char *, char *) */
1409 PHP_HTTP_API char *_http_absolute_uri(const char *url,
1410 const char *proto TSRMLS_DC)
1411 {
1412 char URI[HTTP_URI_MAXLEN + 1], *PTR, *proto_ptr, *host, *path;
1413 zval *zhost;
1414
1415 if (!url || !strlen(url)) {
1416 if (!SG(request_info).request_uri) {
1417 return NULL;
1418 }
1419 url = SG(request_info).request_uri;
1420 }
1421 /* Mess around with already absolute URIs */
1422 else if (proto_ptr = strstr(url, "://")) {
1423 if (!proto || !strncmp(url, proto, strlen(proto))) {
1424 return estrdup(url);
1425 } else {
1426 snprintf(URI, HTTP_URI_MAXLEN, "%s%s", proto, proto_ptr + 3);
1427 return estrdup(URI);
1428 }
1429 }
1430
1431 /* protocol defaults to http */
1432 if (!proto || !strlen(proto)) {
1433 proto = "http";
1434 }
1435
1436 /* get host name */
1437 if ( (zhost = http_get_server_var("HTTP_HOST")) ||
1438 (zhost = http_get_server_var("SERVER_NAME"))) {
1439 host = Z_STRVAL_P(zhost);
1440 } else {
1441 host = "localhost";
1442 }
1443
1444
1445 /* glue together */
1446 if (url[0] == '/') {
1447 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s", proto, host, url);
1448 } else if (SG(request_info).request_uri) {
1449 path = estrdup(SG(request_info).request_uri);
1450 php_dirname(path, strlen(path));
1451 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s/%s", proto, host, path, url);
1452 efree(path);
1453 } else {
1454 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s/%s", proto, host, url);
1455 }
1456
1457 /* strip everything after a new line */
1458 PTR = URI;
1459 while (*PTR != 0) {
1460 if (*PTR == '\n' || *PTR == '\r') {
1461 *PTR = 0;
1462 break;
1463 }
1464 PTR++;
1465 }
1466
1467 return estrdup(URI);
1468 }
1469 /* }}} */
1470
1471 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
1472 PHP_HTTP_API char *_http_negotiate_q(const char *entry, const zval *supported,
1473 const char *def TSRMLS_DC)
1474 {
1475 zval *zaccept, *zarray, *zdelim, **zentry, *zentries, **zsupp;
1476 char *q_ptr, *result;
1477 int i, c;
1478 double qual;
1479
1480 HTTP_GSC(zaccept, entry, estrdup(def));
1481
1482 MAKE_STD_ZVAL(zarray);
1483 array_init(zarray);
1484
1485 MAKE_STD_ZVAL(zdelim);
1486 ZVAL_STRING(zdelim, ",", 0);
1487 php_explode(zdelim, zaccept, zarray, -1);
1488 efree(zdelim);
1489
1490 MAKE_STD_ZVAL(zentries);
1491 array_init(zentries);
1492
1493 c = zend_hash_num_elements(Z_ARRVAL_P(zarray));
1494 for (i = 0; i < c; i++, zend_hash_move_forward(Z_ARRVAL_P(zarray))) {
1495
1496 if (SUCCESS != zend_hash_get_current_data(
1497 Z_ARRVAL_P(zarray), (void **) &zentry)) {
1498 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1499 "Cannot parse %s header: %s", entry, Z_STRVAL_P(zaccept));
1500 break;
1501 }
1502
1503 /* check for qualifier */
1504 if (NULL != (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';'))) {
1505 qual = strtod(q_ptr + 3, NULL);
1506 } else {
1507 qual = 1000.0 - i;
1508 }
1509
1510 /* walk through the supported array */
1511 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported));
1512 SUCCESS == zend_hash_get_current_data(
1513 Z_ARRVAL_P(supported), (void **) &zsupp);
1514 zend_hash_move_forward(Z_ARRVAL_P(supported))) {
1515 if (!strcasecmp(Z_STRVAL_PP(zsupp), Z_STRVAL_PP(zentry))) {
1516 add_assoc_double(zentries, Z_STRVAL_PP(zsupp), qual);
1517 break;
1518 }
1519 }
1520 }
1521
1522 zval_dtor(zarray);
1523 efree(zarray);
1524
1525 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries));
1526
1527 if ( (SUCCESS != zend_hash_sort(Z_ARRVAL_P(zentries), zend_qsort,
1528 http_sort_q, 0 TSRMLS_CC)) ||
1529 (HASH_KEY_NON_EXISTANT == zend_hash_get_current_key(
1530 Z_ARRVAL_P(zentries), &result, 0, 1))) {
1531 result = estrdup(def);
1532 }
1533
1534 zval_dtor(zentries);
1535 efree(zentries);
1536
1537 return result;
1538 }
1539 /* }}} */
1540
1541 /* {{{ http_range_status http_get_request_ranges(zval *zranges, size_t) */
1542 PHP_HTTP_API http_range_status _http_get_request_ranges(zval *zranges,
1543 const size_t length TSRMLS_DC)
1544 {
1545 zval *zrange;
1546 char *range, c;
1547 long begin = -1, end = -1, *ptr;
1548
1549 HTTP_GSC(zrange, "HTTP_RANGE", RANGE_NO);
1550 range = Z_STRVAL_P(zrange);
1551
1552 if (strncmp(range, "bytes=", strlen("bytes="))) {
1553 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Range header misses bytes=");
1554 return RANGE_NO;
1555 }
1556
1557 ptr = &begin;
1558 range += strlen("bytes=");
1559
1560 do {
1561 switch (c = *(range++))
1562 {
1563 case '0':
1564 *ptr *= 10;
1565 break;
1566
1567 case '1': case '2': case '3':
1568 case '4': case '5': case '6':
1569 case '7': case '8': case '9':
1570 /*
1571 * If the value of the pointer is already set (non-negative)
1572 * then multiply its value by ten and add the current value,
1573 * else initialise the pointers value with the current value
1574 * --
1575 * This let us recognize empty fields when validating the
1576 * ranges, i.e. a "-10" for begin and "12345" for the end
1577 * was the following range request: "Range: bytes=0-12345";
1578 * While a "-1" for begin and "12345" for the end would
1579 * have been: "Range: bytes=-12345".
1580 */
1581 if (*ptr > 0) {
1582 *ptr *= 10;
1583 *ptr += c - '0';
1584 } else {
1585 *ptr = c - '0';
1586 }
1587 break;
1588
1589 case '-':
1590 ptr = &end;
1591 break;
1592
1593 case ' ':
1594 /* IE - ignore for now */
1595 break;
1596
1597 case 0:
1598 case ',':
1599
1600 if (length) {
1601 /* validate ranges */
1602 switch (begin)
1603 {
1604 /* "0-12345" */
1605 case -10:
1606 if ((length - end) < 1) {
1607 return RANGE_ERR;
1608 }
1609 begin = 0;
1610 break;
1611
1612 /* "-12345" */
1613 case -1:
1614 if ((length - end) < 1) {
1615 return RANGE_ERR;
1616 }
1617 begin = length - end;
1618 end = length;
1619 break;
1620
1621 /* "12345-(xxx)" */
1622 default:
1623 switch (end)
1624 {
1625 /* "12345-" */
1626 case -1:
1627 if ((length - begin) < 1) {
1628 return RANGE_ERR;
1629 }
1630 end = length - 1;
1631 break;
1632
1633 /* "12345-67890" */
1634 default:
1635 if ( ((length - begin) < 1) ||
1636 ((length - end) < 1) ||
1637 ((begin - end) >= 0)) {
1638 return RANGE_ERR;
1639 }
1640 break;
1641 }
1642 break;
1643 }
1644 }
1645 {
1646 zval *zentry;
1647 MAKE_STD_ZVAL(zentry);
1648 array_init(zentry);
1649 add_index_long(zentry, 0, begin);
1650 add_index_long(zentry, 1, end);
1651 add_next_index_zval(zranges, zentry);
1652
1653 begin = -1;
1654 end = -1;
1655 ptr = &begin;
1656 }
1657 break;
1658
1659 default:
1660 return RANGE_NO;
1661 break;
1662 }
1663 } while (c != 0);
1664
1665 return RANGE_OK;
1666 }
1667 /* }}} */
1668
1669 /* {{{ STATUS http_send_ranges(zval *, void *, size_t, http_send_mode) */
1670 PHP_HTTP_API STATUS _http_send_ranges(zval *zranges, const void *data, const size_t size, const http_send_mode mode TSRMLS_DC)
1671 {
1672 int c;
1673 long **begin, **end;
1674 zval **zrange;
1675
1676 /* Send HTTP 206 Partial Content */
1677 http_send_status(206);
1678
1679 /* single range */
1680 if ((c = zend_hash_num_elements(Z_ARRVAL_P(zranges))) == 1) {
1681 char range_header[256] = {0};
1682
1683 zend_hash_index_find(Z_ARRVAL_P(zranges), 0, (void **) &zrange);
1684 zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &begin);
1685 zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &end);
1686
1687 /* send content range header */
1688 snprintf(range_header, 255, "Content-Range: bytes %d-%d/%d", **begin, **end, size);
1689 http_send_header(range_header);
1690
1691 /* send requested chunk */
1692 return http_send_chunk(data, **begin, **end + 1, mode);
1693 }
1694
1695 /* multi range */
1696 else {
1697 int i;
1698 char bound[23] = {0}, preface[1024] = {0},
1699 multi_header[68] = "Content-Type: multipart/byteranges; boundary=";
1700
1701 snprintf(bound, 22, "--%d%0.9f", time(NULL), php_combined_lcg(TSRMLS_C));
1702 strncat(multi_header, bound + 2, 21);
1703 http_send_header(multi_header);
1704
1705 /* send each requested chunk */
1706 for ( i = 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(zranges));
1707 i < c;
1708 i++, zend_hash_move_forward(Z_ARRVAL_P(zranges))) {
1709 if ( HASH_KEY_NON_EXISTANT == zend_hash_get_current_data(
1710 Z_ARRVAL_P(zranges), (void **) &zrange) ||
1711 SUCCESS != zend_hash_index_find(
1712 Z_ARRVAL_PP(zrange), 0, (void **) &begin) ||
1713 SUCCESS != zend_hash_index_find(
1714 Z_ARRVAL_PP(zrange), 1, (void **) &end)) {
1715 break;
1716 }
1717
1718 snprintf(preface, 1023,
1719 HTTP_CRLF "%s"
1720 HTTP_CRLF "Content-Type: %s"
1721 HTTP_CRLF "Content-Range: bytes %ld-%ld/%ld"
1722 HTTP_CRLF
1723 HTTP_CRLF,
1724
1725 bound,
1726 HTTP_G(ctype) ? HTTP_G(ctype) : "application/x-octetstream",
1727 **begin,
1728 **end,
1729 size
1730 );
1731
1732 php_body_write(preface, strlen(preface) TSRMLS_CC);
1733 http_send_chunk(data, **begin, **end + 1, mode);
1734 }
1735
1736 /* write boundary once more */
1737 php_body_write(HTTP_CRLF, 2 TSRMLS_CC);
1738 php_body_write(bound, strlen(bound) TSRMLS_CC);
1739
1740 return SUCCESS;
1741 }
1742 }
1743 /* }}} */
1744
1745 /* {{{ STATUS http_send(void *, sizezo_t, http_send_mode) */
1746 PHP_HTTP_API STATUS _http_send(const void *data_ptr, const size_t data_size,
1747 const http_send_mode data_mode TSRMLS_DC)
1748 {
1749 int is_range_request = http_is_range_request();
1750
1751 if (!data_ptr) {
1752 return FAILURE;
1753 }
1754
1755 /* etag handling */
1756 if (HTTP_G(etag_started)) {
1757 char *etag;
1758 /* interrupt */
1759 HTTP_G(etag_started) = 0;
1760 /* never ever use the output to compute the ETag if http_send() is used */
1761 php_end_ob_buffer(0, 0 TSRMLS_CC);
1762 if (!(etag = http_etag(data_ptr, data_size, data_mode))) {
1763 return FAILURE;
1764 }
1765
1766 /* send 304 Not Modified if etag matches */
1767 if ((!is_range_request) && http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
1768 efree(etag);
1769 return http_send_status(304);
1770 }
1771
1772 http_send_etag(etag, 32);
1773 efree(etag);
1774 }
1775
1776 /* send 304 Not Modified if last-modified matches*/
1777 if ((!is_range_request) && http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod))) {
1778 return http_send_status(304);
1779 }
1780
1781 if (is_range_request) {
1782
1783 /* only send ranges if entity hasn't changed */
1784 if (
1785 ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_MATCH", 13)) ||
1786 http_etag_match("HTTP_IF_MATCH", HTTP_G(etag)))
1787 &&
1788 ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
1789 http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod)))
1790 ) {
1791
1792 STATUS result = FAILURE;
1793 zval *zranges = NULL;
1794 MAKE_STD_ZVAL(zranges);
1795 array_init(zranges);
1796
1797 switch (http_get_request_ranges(zranges, data_size))
1798 {
1799 case RANGE_NO:
1800 zval_dtor(zranges);
1801 efree(zranges);
1802 /* go ahead and send all */
1803 break;
1804
1805 case RANGE_OK:
1806 result = http_send_ranges(zranges, data_ptr, data_size, data_mode);
1807 zval_dtor(zranges);
1808 efree(zranges);
1809 return result;
1810 break;
1811
1812 case RANGE_ERR:
1813 zval_dtor(zranges);
1814 efree(zranges);
1815 http_send_status(416);
1816 return FAILURE;
1817 break;
1818
1819 default:
1820 return FAILURE;
1821 break;
1822 }
1823 }
1824 }
1825 /* send all */
1826 return http_send_chunk(data_ptr, 0, data_size, data_mode);
1827 }
1828 /* }}} */
1829
1830 /* {{{ STATUS http_send_data(zval *) */
1831 PHP_HTTP_API STATUS _http_send_data(const zval *zdata TSRMLS_DC)
1832 {
1833 if (!Z_STRLEN_P(zdata)) {
1834 return SUCCESS;
1835 }
1836 if (!Z_STRVAL_P(zdata)) {
1837 return FAILURE;
1838 }
1839
1840 return http_send(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata), SEND_DATA);
1841 }
1842 /* }}} */
1843
1844 /* {{{ STATUS http_send_stream(php_stream *) */
1845 PHP_HTTP_API STATUS _http_send_stream(const php_stream *file TSRMLS_DC)
1846 {
1847 if (php_stream_stat((php_stream *) file, &HTTP_G(ssb))) {
1848 return FAILURE;
1849 }
1850
1851 return http_send(file, HTTP_G(ssb).sb.st_size, SEND_RSRC);
1852 }
1853 /* }}} */
1854
1855 /* {{{ STATUS http_send_file(zval *) */
1856 PHP_HTTP_API STATUS _http_send_file(const zval *zfile TSRMLS_DC)
1857 {
1858 php_stream *file;
1859 STATUS ret;
1860
1861 if (!Z_STRLEN_P(zfile)) {
1862 return FAILURE;
1863 }
1864
1865 if (!(file = php_stream_open_wrapper(Z_STRVAL_P(zfile), "rb",
1866 REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
1867 return FAILURE;
1868 }
1869
1870 ret = http_send_stream(file);
1871 php_stream_close(file);
1872 return ret;
1873 }
1874 /* }}} */
1875
1876 /* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1877 PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded,
1878 const size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
1879 {
1880 const char *e_ptr;
1881 char *d_ptr;
1882
1883 *decoded_len = 0;
1884 *decoded = (char *) ecalloc(encoded_len, 1);
1885 d_ptr = *decoded;
1886 e_ptr = encoded;
1887
1888 while (((e_ptr - encoded) - encoded_len) > 0) {
1889 char hex_len[9] = {0};
1890 size_t chunk_len = 0;
1891 int i = 0;
1892
1893 /* read in chunk size */
1894 while (isxdigit(*e_ptr)) {
1895 if (i == 9) {
1896 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1897 "Chunk size is too long: 0x%s...", hex_len);
1898 efree(*decoded);
1899 return FAILURE;
1900 }
1901 hex_len[i++] = *e_ptr++;
1902 }
1903
1904 /* reached the end */
1905 if (!strcmp(hex_len, "0")) {
1906 break;
1907 }
1908
1909 /* new line */
1910 if (strncmp(e_ptr, HTTP_CRLF, 2)) {
1911 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1912 "Invalid character (expected 0x0D 0x0A; got: %x %x)",
1913 *e_ptr, *(e_ptr + 1));
1914 efree(*decoded);
1915 return FAILURE;
1916 }
1917
1918 /* hex to long */
1919 {
1920 char *error = NULL;
1921 chunk_len = strtol(hex_len, &error, 16);
1922 if (error == hex_len) {
1923 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1924 "Invalid chunk size string: '%s'", hex_len);
1925 efree(*decoded);
1926 return FAILURE;
1927 }
1928 }
1929
1930 memcpy(d_ptr, e_ptr += 2, chunk_len);
1931 d_ptr += chunk_len;
1932 e_ptr += chunk_len + 2;
1933 *decoded_len += chunk_len;
1934 }
1935
1936 return SUCCESS;
1937 }
1938 /* }}} */
1939
1940 /* {{{ proto STATUS http_split_response_ex(char *, size_t, zval *, zval *) */
1941 PHP_HTTP_API STATUS _http_split_response_ex( char *response,
1942 size_t response_len, zval *zheaders, zval *zbody TSRMLS_DC)
1943 {
1944 char *body = NULL;
1945 char *header = response;
1946
1947 while ((response - header + 4) < response_len) {
1948 if ( (*response++ == '\r') &&
1949 (*response++ == '\n') &&
1950 (*response++ == '\r') &&
1951 (*response++ == '\n')) {
1952 body = response;
1953 break;
1954 }
1955 }
1956
1957 if (body && (response_len - (body - header))) {
1958 ZVAL_STRINGL(zbody, body, response_len - (body - header) - 1, 1);
1959 } else {
1960 Z_TYPE_P(zbody) = IS_NULL;
1961 }
1962
1963 return http_parse_headers(header, body ? body - header : response_len, zheaders);
1964 }
1965 /* }}} */
1966
1967 /* {{{ STATUS http_parse_headers(char *, long, zval *) */
1968 PHP_HTTP_API STATUS _http_parse_headers(char *header, int header_len, zval *array TSRMLS_DC)
1969 {
1970 char *colon = NULL, *line = NULL, *begin = header;
1971
1972 if (header_len < 2) {
1973 return FAILURE;
1974 }
1975
1976 /* status code */
1977 if (!strncmp(header, "HTTP/1.", 7)) {
1978 char *end = strstr(header, HTTP_CRLF);
1979 add_assoc_stringl(array, "Status",
1980 header + strlen("HTTP/1.x "),
1981 end - (header + strlen("HTTP/1.x ")), 1);
1982 header = end + 2;
1983 }
1984
1985 line = header;
1986
1987 while (header_len >= (line - begin)) {
1988 int value_len = 0;
1989
1990 switch (*line++)
1991 {
1992 case 0:
1993 --value_len; /* we don't have CR so value length is one char less */
1994 case '\n':
1995 if (colon && ((!(*line - 1)) || ((*line != ' ') && (*line != '\t')))) {
1996
1997 /* skip empty key */
1998 if (header != colon) {
1999 char *key = estrndup(header, colon - header);
2000 value_len += line - colon - 1;
2001
2002 /* skip leading ws */
2003 while (isspace(*(++colon))) --value_len;
2004 /* skip trailing ws */
2005 while (isspace(colon[value_len - 1])) --value_len;
2006
2007 if (value_len < 1) {
2008 /* hm, empty header? */
2009 add_assoc_stringl(array, key, "", 0, 1);
2010 } else {
2011 add_assoc_stringl(array, key, colon, value_len, 1);
2012 }
2013 efree(key);
2014 }
2015
2016 colon = NULL;
2017 value_len = 0;
2018 header += line - header;
2019 }
2020 break;
2021
2022 case ':':
2023 if (!colon) {
2024 colon = line - 1;
2025 }
2026 break;
2027 }
2028 }
2029 return SUCCESS;
2030 }
2031 /* }}} */
2032
2033 /* {{{ void http_get_request_headers(zval *) */
2034 PHP_HTTP_API void _http_get_request_headers(zval *array TSRMLS_DC)
2035 {
2036 char *key;
2037
2038 for ( zend_hash_internal_pointer_reset(HTTP_SERVER_VARS);
2039 zend_hash_get_current_key(HTTP_SERVER_VARS, &key, NULL, 0) != HASH_KEY_NON_EXISTANT;
2040 zend_hash_move_forward(HTTP_SERVER_VARS)) {
2041 if (!strncmp(key, "HTTP_", 5)) {
2042 zval **header;
2043 zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header);
2044 add_assoc_stringl(array, pretty_key(key + 5, strlen(key) - 5, 1, 1), Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
2045 }
2046 }
2047 }
2048 /* }}} */
2049
2050 /* {{{ HAVE_CURL */
2051 #ifdef HTTP_HAVE_CURL
2052
2053 /* {{{ STATUS http_get(char *, HashTable *, HashTable *, char **, size_t *) */
2054 PHP_HTTP_API STATUS _http_get(const char *URL, HashTable *options,
2055 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2056 {
2057 STATUS rs;
2058 CURL *ch = curl_easy_init();
2059
2060 if (!ch) {
2061 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
2062 return FAILURE;
2063 }
2064
2065 rs = http_get_ex(ch, URL, options, info, data, data_len);
2066 curl_easy_cleanup(ch);
2067 return rs;
2068 }
2069 /* }}} */
2070
2071 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, char **, size_t *) */
2072 PHP_HTTP_API STATUS _http_get_ex(CURL *ch, const char *URL, HashTable *options,
2073 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2074 {
2075 http_curl_initbuf(CURLBUF_EVRY);
2076 http_curl_setopts(ch, URL, options);
2077 curl_easy_setopt(ch, CURLOPT_NOBODY, 0);
2078 curl_easy_setopt(ch, CURLOPT_POST, 0);
2079
2080 if (CURLE_OK != curl_easy_perform(ch)) {
2081 http_curl_freebuf(CURLBUF_EVRY);
2082 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
2083 return FAILURE;
2084 }
2085 if (info) {
2086 http_curl_getinfo(ch, info);
2087 }
2088 http_curl_movebuf(CURLBUF_EVRY, data, data_len);
2089 return SUCCESS;
2090 }
2091
2092 /* {{{ STATUS http_head(char *, HashTable *, HashTable *, char **data, size_t *) */
2093 PHP_HTTP_API STATUS _http_head(const char *URL, HashTable *options,
2094 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2095 {
2096 STATUS rs;
2097 CURL *ch = curl_easy_init();
2098
2099 if (!ch) {
2100 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
2101 return FAILURE;
2102 }
2103
2104 rs = http_head_ex(ch, URL, options, info, data, data_len);
2105 curl_easy_cleanup(ch);
2106 return rs;
2107 }
2108 /* }}} */
2109
2110 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, char **data, size_t *) */
2111 PHP_HTTP_API STATUS _http_head_ex(CURL *ch, const char *URL, HashTable *options,
2112 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2113 {
2114 http_curl_initbuf(CURLBUF_HDRS);
2115 http_curl_setopts(ch, URL, options);
2116 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
2117 curl_easy_setopt(ch, CURLOPT_POST, 0);
2118
2119 if (CURLE_OK != curl_easy_perform(ch)) {
2120 http_curl_freebuf(CURLBUF_HDRS);
2121 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
2122 return FAILURE;
2123 }
2124 if (info) {
2125 http_curl_getinfo(ch, info);
2126 }
2127 http_curl_movebuf(CURLBUF_HDRS, data, data_len);
2128 return SUCCESS;
2129 }
2130
2131 /* {{{ STATUS http_post_data(char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
2132 PHP_HTTP_API STATUS _http_post_data(const char *URL, char *postdata,
2133 size_t postdata_len, HashTable *options, HashTable *info, char **data,
2134 size_t *data_len TSRMLS_DC)
2135 {
2136 STATUS rs;
2137 CURL *ch = curl_easy_init();
2138
2139 if (!ch) {
2140 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
2141 return FAILURE;
2142 }
2143 rs = http_post_data_ex(ch, URL, postdata, postdata_len, options, info, data, data_len);
2144 curl_easy_cleanup(ch);
2145 return rs;
2146 }
2147 /* }}} */
2148
2149 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
2150 PHP_HTTP_API STATUS _http_post_data_ex(CURL *ch, const char *URL, char *postdata,
2151 size_t postdata_len, HashTable *options, HashTable *info, char **data,
2152 size_t *data_len TSRMLS_DC)
2153 {
2154 http_curl_initbuf(CURLBUF_EVRY);
2155 http_curl_setopts(ch, URL, options);
2156 curl_easy_setopt(ch, CURLOPT_POST, 1);
2157 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, postdata);
2158 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, postdata_len);
2159
2160 if (CURLE_OK != curl_easy_perform(ch)) {
2161 http_curl_freebuf(CURLBUF_EVRY);
2162 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
2163 return FAILURE;
2164 }
2165 if (info) {
2166 http_curl_getinfo(ch, info);
2167 }
2168 http_curl_movebuf(CURLBUF_EVRY, data, data_len);
2169 return SUCCESS;
2170 }
2171 /* }}} */
2172
2173 /* {{{ STATUS http_post_array(char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
2174 PHP_HTTP_API STATUS _http_post_array(const char *URL, HashTable *postarray,
2175 HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2176 {
2177 smart_str qstr = {0};
2178 STATUS status;
2179
2180 if (php_url_encode_hash_ex(postarray, &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
2181 if (qstr.c) {
2182 efree(qstr.c);
2183 }
2184 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode post data");
2185 return FAILURE;
2186 }
2187 smart_str_0(&qstr);
2188
2189 status = http_post_data(URL, qstr.c, qstr.len, options, info, data, data_len);
2190 if (qstr.c) {
2191 efree(qstr.c);
2192 }
2193 return status;
2194 }
2195 /* }}} */
2196
2197 #endif
2198 /* }}} HAVE_CURL */
2199
2200 /* {{{ STATUS http_auth_header(char *, char*) */
2201 PHP_HTTP_API STATUS _http_auth_header(const char *type, const char *realm TSRMLS_DC)
2202 {
2203 char realm_header[1024] = {0};
2204 snprintf(realm_header, 1023, "WWW-Authenticate: %s realm=\"%s\"", type, realm);
2205 return http_send_status_header(401, realm_header);
2206 }
2207 /* }}} */
2208
2209 /* {{{ STATUS http_auth_credentials(char **, char **) */
2210 PHP_HTTP_API STATUS _http_auth_credentials(char **user, char **pass TSRMLS_DC)
2211 {
2212 if (strncmp(sapi_module.name, "isapi", 5)) {
2213 zval *zuser, *zpass;
2214
2215 HTTP_GSC(zuser, "PHP_AUTH_USER", FAILURE);
2216 HTTP_GSC(zpass, "PHP_AUTH_PW", FAILURE);
2217
2218 *user = estrndup(Z_STRVAL_P(zuser), Z_STRLEN_P(zuser));
2219 *pass = estrndup(Z_STRVAL_P(zpass), Z_STRLEN_P(zpass));
2220
2221 return SUCCESS;
2222 } else {
2223 zval *zauth = NULL;
2224 HTTP_GSC(zauth, "HTTP_AUTHORIZATION", FAILURE);
2225 {
2226 char *decoded, *colon;
2227 int decoded_len;
2228 decoded = php_base64_decode(Z_STRVAL_P(zauth), Z_STRLEN_P(zauth),
2229 &decoded_len);
2230
2231 if (colon = strchr(decoded + 6, ':')) {
2232 *user = estrndup(decoded + 6, colon - decoded - 6);
2233 *pass = estrndup(colon + 1, decoded + decoded_len - colon - 6 - 1);
2234
2235 return SUCCESS;
2236 } else {
2237 return FAILURE;
2238 }
2239 }
2240 }
2241 }
2242 /* }}} */
2243
2244 /* }}} public API */
2245
2246 /*
2247 * Local variables:
2248 * tab-width: 4
2249 * c-basic-offset: 4
2250 * End:
2251 * vim600: noet sw=4 ts=4 fdm=marker
2252 * vim<600: noet sw=4 ts=4
2253 */