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