* fixed typo
[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 if (!HTTP_G(ssb).sb.st_mtime) {
1065 if(php_stream_stat_path(Z_STRVAL_P((zval *) data_ptr), &HTTP_G(ssb))) {
1066 return 0;
1067 }
1068 }
1069 return HTTP_G(ssb).sb.st_mtime;
1070 }
1071 }
1072 }
1073 /* }}} */
1074
1075 /* {{{inline int http_is_range_request(void) */
1076 PHP_HTTP_API inline int _http_is_range_request(TSRMLS_D)
1077 {
1078 return zend_hash_exists(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]),
1079 "HTTP_RANGE", strlen("HTTP_RANGE") + 1);
1080 }
1081 /* }}} */
1082
1083 /* {{{ inline STATUS http_send_status(int) */
1084 PHP_HTTP_API inline STATUS _http_send_status(const int status TSRMLS_DC)
1085 {
1086 int s = status;
1087 return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) s TSRMLS_CC);
1088 }
1089 /* }}} */
1090
1091 /* {{{ inline STATUS http_send_header(char *) */
1092 PHP_HTTP_API inline STATUS _http_send_header(const char *header TSRMLS_DC)
1093 {
1094 return http_send_status_header(0, header);
1095 }
1096 /* }}} */
1097
1098 /* {{{ inline STATUS http_send_status_header(int, char *) */
1099 PHP_HTTP_API inline STATUS _http_send_status_header(const int status, const char *header TSRMLS_DC)
1100 {
1101 sapi_header_line h = {(char *) header, strlen(header), status};
1102 return sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC);
1103 }
1104 /* }}} */
1105
1106 /* {{{ inline zval *http_get_server_var(char *) */
1107 PHP_HTTP_API inline zval *_http_get_server_var(const char *key TSRMLS_DC)
1108 {
1109 zval **var;
1110 if (SUCCESS == zend_hash_find(
1111 HTTP_SERVER_VARS,
1112 (char *) key, strlen(key) + 1, (void **) &var)) {
1113 return *var;
1114 }
1115 return NULL;
1116 }
1117 /* }}} */
1118
1119 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
1120 PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len,
1121 char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
1122 {
1123 char etag[33] = { 0 };
1124 unsigned char digest[16];
1125
1126 if (mode & PHP_OUTPUT_HANDLER_START) {
1127 PHP_MD5Init(&HTTP_G(etag_md5));
1128 }
1129
1130 PHP_MD5Update(&HTTP_G(etag_md5), output, output_len);
1131
1132 if (mode & PHP_OUTPUT_HANDLER_END) {
1133 PHP_MD5Final(digest, &HTTP_G(etag_md5));
1134
1135 /* just do that if desired */
1136 if (HTTP_G(etag_started)) {
1137 make_digest(etag, digest);
1138
1139 if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
1140 http_send_status(304);
1141 } else {
1142 http_send_etag(etag, 32);
1143 }
1144 }
1145 }
1146
1147 *handled_output_len = output_len;
1148 *handled_output = estrndup(output, output_len);
1149 }
1150 /* }}} */
1151
1152 /* {{{ STATUS http_start_ob_handler(php_output_handler_func_t, char *, uint, zend_bool) */
1153 PHP_HTTP_API STATUS _http_start_ob_handler(php_output_handler_func_t handler_func,
1154 char *handler_name, uint chunk_size, zend_bool erase TSRMLS_DC)
1155 {
1156 php_ob_buffer **stack;
1157 int count, i;
1158
1159 if (count = OG(ob_nesting_level)) {
1160 stack = ecalloc(sizeof(php_ob_buffer *), count);
1161
1162 if (count > 1) {
1163 zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP,
1164 (int (*)(void *elem, void *)) http_ob_stack_get, stack);
1165 }
1166
1167 if (count > 0) {
1168 http_ob_stack_get(&OG(active_ob_buffer), stack);
1169 }
1170
1171 while (OG(ob_nesting_level)) {
1172 php_end_ob_buffer(0, 0 TSRMLS_CC);
1173 }
1174 }
1175
1176 php_ob_set_internal_handler(handler_func, chunk_size, handler_name, erase TSRMLS_CC);
1177
1178 for (i = 0; i < count; i++) {
1179 php_ob_buffer *s = stack[i];
1180 if (strcmp(s->handler_name, "default output handler")) {
1181 php_start_ob_buffer_named(s->handler_name, s->chunk_size, s->erase TSRMLS_CC);
1182 }
1183 php_body_write(s->buffer, s->text_length TSRMLS_CC);
1184 efree(s->handler_name);
1185 efree(s->buffer);
1186 efree(s);
1187 }
1188 if (count) {
1189 efree(stack);
1190 }
1191
1192 return SUCCESS;
1193 }
1194 /* }}} */
1195
1196 /* {{{ int http_modified_match(char *, int) */
1197 PHP_HTTP_API int _http_modified_match(const char *entry, const time_t t TSRMLS_DC)
1198 {
1199 int retval;
1200 zval *zmodified;
1201 char *modified, *chr_ptr;
1202
1203 HTTP_GSC(zmodified, entry, 0);
1204
1205 modified = estrndup(Z_STRVAL_P(zmodified), Z_STRLEN_P(zmodified));
1206 if (chr_ptr = strrchr(modified, ';')) {
1207 chr_ptr = 0;
1208 }
1209 retval = (t <= http_parse_date(modified));
1210 efree(modified);
1211 return retval;
1212 }
1213 /* }}} */
1214
1215 /* {{{ int http_etag_match(char *, char *) */
1216 PHP_HTTP_API int _http_etag_match(const char *entry, const char *etag TSRMLS_DC)
1217 {
1218 zval *zetag;
1219 char *quoted_etag;
1220 STATUS result;
1221
1222 HTTP_GSC(zetag, entry, 0);
1223
1224 if (NULL != strchr(Z_STRVAL_P(zetag), '*')) {
1225 return 1;
1226 }
1227
1228 quoted_etag = (char *) emalloc(strlen(etag) + 3);
1229 sprintf(quoted_etag, "\"%s\"", etag);
1230
1231 if (!strchr(Z_STRVAL_P(zetag), ',')) {
1232 result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
1233 } else {
1234 result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
1235 }
1236 efree(quoted_etag);
1237 return result;
1238 }
1239 /* }}} */
1240
1241 /* {{{ STATUS http_send_last_modified(int) */
1242 PHP_HTTP_API STATUS _http_send_last_modified(const time_t t TSRMLS_DC)
1243 {
1244 char modified[96] = "Last-Modified: ", *date;
1245 date = http_date(t);
1246 strcat(modified, date);
1247 efree(date);
1248
1249 /* remember */
1250 HTTP_G(lmod) = t;
1251
1252 return http_send_header(modified);
1253 }
1254 /* }}} */
1255
1256 /* {{{ static STATUS http_send_etag(char *, int) */
1257 PHP_HTTP_API STATUS _http_send_etag(const char *etag,
1258 const int etag_len TSRMLS_DC)
1259 {
1260 STATUS status;
1261 char *etag_header;
1262
1263 if (!etag_len){
1264 php_error_docref(NULL TSRMLS_CC,E_ERROR,
1265 "Attempt to send empty ETag (previous: %s)\n", HTTP_G(etag));
1266 return FAILURE;
1267 }
1268
1269 /* remember */
1270 if (HTTP_G(etag)) {
1271 efree(HTTP_G(etag));
1272 }
1273 HTTP_G(etag) = estrdup(etag);
1274
1275 etag_header = ecalloc(sizeof("ETag: \"\"") + etag_len, 1);
1276 sprintf(etag_header, "ETag: \"%s\"", etag);
1277 if (SUCCESS != (status = http_send_header(etag_header))) {
1278 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't send '%s' header", etag_header);
1279 }
1280 efree(etag_header);
1281 return status;
1282 }
1283 /* }}} */
1284
1285 /* {{{ STATUS http_send_cache_control(char *, size_t) */
1286 PHP_HTTP_API STATUS _http_send_cache_control(const char *cache_control,
1287 const size_t cc_len TSRMLS_DC)
1288 {
1289 STATUS status;
1290 char *cc_header = ecalloc(sizeof("Cache-Control: ") + cc_len, 1);
1291
1292 sprintf(cc_header, "Cache-Control: %s", cache_control);
1293 if (SUCCESS != (status = http_send_header(cc_header))) {
1294 php_error_docref(NULL TSRMLS_CC, E_NOTICE,
1295 "Could not send '%s' header", cc_header);
1296 }
1297 efree(cc_header);
1298 return status;
1299 }
1300 /* }}} */
1301
1302 /* {{{ STATUS http_send_content_type(char *, size_t) */
1303 PHP_HTTP_API STATUS _http_send_content_type(const char *content_type,
1304 const size_t ct_len TSRMLS_DC)
1305 {
1306 STATUS status;
1307 char *ct_header;
1308
1309 if (!strchr(content_type, '/')) {
1310 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1311 "Content-Type '%s' doesn't seem to consist of a primary and a secondary part",
1312 content_type);
1313 return FAILURE;
1314 }
1315
1316 /* remember for multiple ranges */
1317 if (HTTP_G(ctype)) {
1318 efree(HTTP_G(ctype));
1319 }
1320 HTTP_G(ctype) = estrndup(content_type, ct_len);
1321
1322 ct_header = ecalloc(sizeof("Content-Type: ") + ct_len, 1);
1323 sprintf(ct_header, "Content-Type: %s", content_type);
1324
1325 if (SUCCESS != (status = http_send_header(ct_header))) {
1326 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1327 "Couldn't send '%s' header", ct_header);
1328 }
1329 efree(ct_header);
1330 return status;
1331 }
1332 /* }}} */
1333
1334 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
1335 PHP_HTTP_API STATUS _http_send_content_disposition(const char *filename,
1336 const size_t f_len, const int send_inline TSRMLS_DC)
1337 {
1338 STATUS status;
1339 char *cd_header;
1340
1341 if (send_inline) {
1342 cd_header = ecalloc(sizeof("Content-Disposition: inline; filename=\"\"") + f_len, 1);
1343 sprintf(cd_header, "Content-Disposition: inline; filename=\"%s\"", filename);
1344 } else {
1345 cd_header = ecalloc(sizeof("Content-Disposition: attachment; filename=\"\"") + f_len, 1);
1346 sprintf(cd_header, "Content-Disposition: attachment; filename=\"%s\"", filename);
1347 }
1348
1349 if (SUCCESS != (status = http_send_header(cd_header))) {
1350 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't send '%s' header", cd_header);
1351 }
1352 efree(cd_header);
1353 return status;
1354 }
1355 /* }}} */
1356
1357 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
1358 PHP_HTTP_API STATUS _http_cache_last_modified(const time_t last_modified,
1359 const time_t send_modified, const char *cache_control, const size_t cc_len TSRMLS_DC)
1360 {
1361 if (cc_len) {
1362 http_send_cache_control(cache_control, cc_len);
1363 }
1364
1365 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified)) {
1366 if (SUCCESS == http_send_status(304)) {
1367 zend_bailout();
1368 } else {
1369 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
1370 return FAILURE;
1371 }
1372 }
1373 return http_send_last_modified(send_modified);
1374 }
1375 /* }}} */
1376
1377 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
1378 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, const size_t etag_len,
1379 const char *cache_control, const size_t cc_len TSRMLS_DC)
1380 {
1381 if (cc_len) {
1382 http_send_cache_control(cache_control, cc_len);
1383 }
1384
1385 if (etag_len) {
1386 http_send_etag(etag, etag_len);
1387 if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
1388 if (SUCCESS == http_send_status(304)) {
1389 zend_bailout();
1390 } else {
1391 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
1392 return FAILURE;
1393 }
1394 }
1395 }
1396
1397 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
1398 if (!HTTP_G(etag_started)) {
1399 if (SUCCESS == http_start_ob_handler(_http_ob_etaghandler, "ob_etaghandler", 4096, 1)) {
1400 HTTP_G(etag_started) = 1;
1401 return SUCCESS;
1402 } else {
1403 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not start ob_etaghandler");
1404 return FAILURE;
1405 }
1406 }
1407 return SUCCESS;
1408 }
1409 /* }}} */
1410
1411 /* {{{ char *http_absolute_uri(char *, char *) */
1412 PHP_HTTP_API char *_http_absolute_uri(const char *url,
1413 const char *proto TSRMLS_DC)
1414 {
1415 char URI[HTTP_URI_MAXLEN + 1], *PTR, *proto_ptr, *host, *path;
1416 zval *zhost;
1417
1418 if (!url || !strlen(url)) {
1419 if (!SG(request_info).request_uri) {
1420 return NULL;
1421 }
1422 url = SG(request_info).request_uri;
1423 }
1424 /* Mess around with already absolute URIs */
1425 else if (proto_ptr = strstr(url, "://")) {
1426 if (!proto || !strncmp(url, proto, strlen(proto))) {
1427 return estrdup(url);
1428 } else {
1429 snprintf(URI, HTTP_URI_MAXLEN, "%s%s", proto, proto_ptr + 3);
1430 return estrdup(URI);
1431 }
1432 }
1433
1434 /* protocol defaults to http */
1435 if (!proto || !strlen(proto)) {
1436 proto = "http";
1437 }
1438
1439 /* get host name */
1440 if ( (zhost = http_get_server_var("HTTP_HOST")) ||
1441 (zhost = http_get_server_var("SERVER_NAME"))) {
1442 host = Z_STRVAL_P(zhost);
1443 } else {
1444 host = "localhost";
1445 }
1446
1447
1448 /* glue together */
1449 if (url[0] == '/') {
1450 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s", proto, host, url);
1451 } else if (SG(request_info).request_uri) {
1452 path = estrdup(SG(request_info).request_uri);
1453 php_dirname(path, strlen(path));
1454 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s/%s", proto, host, path, url);
1455 efree(path);
1456 } else {
1457 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s/%s", proto, host, url);
1458 }
1459
1460 /* strip everything after a new line */
1461 PTR = URI;
1462 while (*PTR != 0) {
1463 if (*PTR == '\n' || *PTR == '\r') {
1464 *PTR = 0;
1465 break;
1466 }
1467 PTR++;
1468 }
1469
1470 return estrdup(URI);
1471 }
1472 /* }}} */
1473
1474 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
1475 PHP_HTTP_API char *_http_negotiate_q(const char *entry, const zval *supported,
1476 const char *def TSRMLS_DC)
1477 {
1478 zval *zaccept, *zarray, *zdelim, **zentry, *zentries, **zsupp;
1479 char *q_ptr, *result;
1480 int i, c;
1481 double qual;
1482
1483 HTTP_GSC(zaccept, entry, estrdup(def));
1484
1485 MAKE_STD_ZVAL(zarray);
1486 array_init(zarray);
1487
1488 MAKE_STD_ZVAL(zdelim);
1489 ZVAL_STRING(zdelim, ",", 0);
1490 php_explode(zdelim, zaccept, zarray, -1);
1491 efree(zdelim);
1492
1493 MAKE_STD_ZVAL(zentries);
1494 array_init(zentries);
1495
1496 c = zend_hash_num_elements(Z_ARRVAL_P(zarray));
1497 for (i = 0; i < c; i++, zend_hash_move_forward(Z_ARRVAL_P(zarray))) {
1498
1499 if (SUCCESS != zend_hash_get_current_data(
1500 Z_ARRVAL_P(zarray), (void **) &zentry)) {
1501 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1502 "Cannot parse %s header: %s", entry, Z_STRVAL_P(zaccept));
1503 break;
1504 }
1505
1506 /* check for qualifier */
1507 if (NULL != (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';'))) {
1508 qual = strtod(q_ptr + 3, NULL);
1509 } else {
1510 qual = 1000.0 - i;
1511 }
1512
1513 /* walk through the supported array */
1514 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported));
1515 SUCCESS == zend_hash_get_current_data(
1516 Z_ARRVAL_P(supported), (void **) &zsupp);
1517 zend_hash_move_forward(Z_ARRVAL_P(supported))) {
1518 if (!strcasecmp(Z_STRVAL_PP(zsupp), Z_STRVAL_PP(zentry))) {
1519 add_assoc_double(zentries, Z_STRVAL_PP(zsupp), qual);
1520 break;
1521 }
1522 }
1523 }
1524
1525 zval_dtor(zarray);
1526 efree(zarray);
1527
1528 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries));
1529
1530 if ( (SUCCESS != zend_hash_sort(Z_ARRVAL_P(zentries), zend_qsort,
1531 http_sort_q, 0 TSRMLS_CC)) ||
1532 (HASH_KEY_NON_EXISTANT == zend_hash_get_current_key(
1533 Z_ARRVAL_P(zentries), &result, 0, 1))) {
1534 result = estrdup(def);
1535 }
1536
1537 zval_dtor(zentries);
1538 efree(zentries);
1539
1540 return result;
1541 }
1542 /* }}} */
1543
1544 /* {{{ http_range_status http_get_request_ranges(zval *zranges, size_t) */
1545 PHP_HTTP_API http_range_status _http_get_request_ranges(zval *zranges,
1546 const size_t length TSRMLS_DC)
1547 {
1548 zval *zrange;
1549 char *range, c;
1550 long begin = -1, end = -1, *ptr;
1551
1552 HTTP_GSC(zrange, "HTTP_RANGE", RANGE_NO);
1553 range = Z_STRVAL_P(zrange);
1554
1555 if (strncmp(range, "bytes=", strlen("bytes="))) {
1556 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Range header misses bytes=");
1557 return RANGE_NO;
1558 }
1559
1560 ptr = &begin;
1561 range += strlen("bytes=");
1562
1563 do {
1564 switch (c = *(range++))
1565 {
1566 case '0':
1567 *ptr *= 10;
1568 break;
1569
1570 case '1': case '2': case '3':
1571 case '4': case '5': case '6':
1572 case '7': case '8': case '9':
1573 /*
1574 * If the value of the pointer is already set (non-negative)
1575 * then multiply its value by ten and add the current value,
1576 * else initialise the pointers value with the current value
1577 * --
1578 * This let us recognize empty fields when validating the
1579 * ranges, i.e. a "-10" for begin and "12345" for the end
1580 * was the following range request: "Range: bytes=0-12345";
1581 * While a "-1" for begin and "12345" for the end would
1582 * have been: "Range: bytes=-12345".
1583 */
1584 if (*ptr > 0) {
1585 *ptr *= 10;
1586 *ptr += c - '0';
1587 } else {
1588 *ptr = c - '0';
1589 }
1590 break;
1591
1592 case '-':
1593 ptr = &end;
1594 break;
1595
1596 case ' ':
1597 /* IE - ignore for now */
1598 break;
1599
1600 case 0:
1601 case ',':
1602
1603 if (length) {
1604 /* validate ranges */
1605 switch (begin)
1606 {
1607 /* "0-12345" */
1608 case -10:
1609 if ((length - end) < 1) {
1610 return RANGE_ERR;
1611 }
1612 begin = 0;
1613 break;
1614
1615 /* "-12345" */
1616 case -1:
1617 if ((length - end) < 1) {
1618 return RANGE_ERR;
1619 }
1620 begin = length - end;
1621 end = length;
1622 break;
1623
1624 /* "12345-(xxx)" */
1625 default:
1626 switch (end)
1627 {
1628 /* "12345-" */
1629 case -1:
1630 if ((length - begin) < 1) {
1631 return RANGE_ERR;
1632 }
1633 end = length - 1;
1634 break;
1635
1636 /* "12345-67890" */
1637 default:
1638 if ( ((length - begin) < 1) ||
1639 ((length - end) < 1) ||
1640 ((begin - end) >= 0)) {
1641 return RANGE_ERR;
1642 }
1643 break;
1644 }
1645 break;
1646 }
1647 }
1648 {
1649 zval *zentry;
1650 MAKE_STD_ZVAL(zentry);
1651 array_init(zentry);
1652 add_index_long(zentry, 0, begin);
1653 add_index_long(zentry, 1, end);
1654 add_next_index_zval(zranges, zentry);
1655
1656 begin = -1;
1657 end = -1;
1658 ptr = &begin;
1659 }
1660 break;
1661
1662 default:
1663 return RANGE_NO;
1664 break;
1665 }
1666 } while (c != 0);
1667
1668 return RANGE_OK;
1669 }
1670 /* }}} */
1671
1672 /* {{{ STATUS http_send_ranges(zval *, void *, size_t, http_send_mode) */
1673 PHP_HTTP_API STATUS _http_send_ranges(zval *zranges, const void *data, const size_t size, const http_send_mode mode TSRMLS_DC)
1674 {
1675 int c;
1676 long **begin, **end;
1677 zval **zrange;
1678
1679 /* Send HTTP 206 Partial Content */
1680 http_send_status(206);
1681
1682 /* single range */
1683 if ((c = zend_hash_num_elements(Z_ARRVAL_P(zranges))) == 1) {
1684 char range_header[256] = {0};
1685
1686 zend_hash_index_find(Z_ARRVAL_P(zranges), 0, (void **) &zrange);
1687 zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &begin);
1688 zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &end);
1689
1690 /* send content range header */
1691 snprintf(range_header, 255, "Content-Range: bytes %d-%d/%d", **begin, **end, size);
1692 http_send_header(range_header);
1693
1694 /* send requested chunk */
1695 return http_send_chunk(data, **begin, **end + 1, mode);
1696 }
1697
1698 /* multi range */
1699 else {
1700 int i;
1701 char bound[23] = {0}, preface[1024] = {0},
1702 multi_header[68] = "Content-Type: multipart/byteranges; boundary=";
1703
1704 snprintf(bound, 22, "--%d%0.9f", time(NULL), php_combined_lcg(TSRMLS_C));
1705 strncat(multi_header, bound + 2, 21);
1706 http_send_header(multi_header);
1707
1708 /* send each requested chunk */
1709 for ( i = 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(zranges));
1710 i < c;
1711 i++, zend_hash_move_forward(Z_ARRVAL_P(zranges))) {
1712 if ( HASH_KEY_NON_EXISTANT == zend_hash_get_current_data(
1713 Z_ARRVAL_P(zranges), (void **) &zrange) ||
1714 SUCCESS != zend_hash_index_find(
1715 Z_ARRVAL_PP(zrange), 0, (void **) &begin) ||
1716 SUCCESS != zend_hash_index_find(
1717 Z_ARRVAL_PP(zrange), 1, (void **) &end)) {
1718 break;
1719 }
1720
1721 snprintf(preface, 1023,
1722 HTTP_CRLF "%s"
1723 HTTP_CRLF "Content-Type: %s"
1724 HTTP_CRLF "Content-Range: bytes %ld-%ld/%ld"
1725 HTTP_CRLF
1726 HTTP_CRLF,
1727
1728 bound,
1729 HTTP_G(ctype) ? HTTP_G(ctype) : "application/x-octetstream",
1730 **begin,
1731 **end,
1732 size
1733 );
1734
1735 php_body_write(preface, strlen(preface) TSRMLS_CC);
1736 http_send_chunk(data, **begin, **end + 1, mode);
1737 }
1738
1739 /* write boundary once more */
1740 php_body_write(HTTP_CRLF, 2 TSRMLS_CC);
1741 php_body_write(bound, strlen(bound) TSRMLS_CC);
1742
1743 return SUCCESS;
1744 }
1745 }
1746 /* }}} */
1747
1748 /* {{{ STATUS http_send(void *, sizezo_t, http_send_mode) */
1749 PHP_HTTP_API STATUS _http_send(const void *data_ptr, const size_t data_size,
1750 const http_send_mode data_mode TSRMLS_DC)
1751 {
1752 int is_range_request = http_is_range_request();
1753
1754 if (!data_ptr) {
1755 return FAILURE;
1756 }
1757
1758 /* etag handling */
1759 if (HTTP_G(etag_started)) {
1760 char *etag;
1761 /* interrupt */
1762 HTTP_G(etag_started) = 0;
1763 /* never ever use the output to compute the ETag if http_send() is used */
1764 php_end_ob_buffer(0, 0 TSRMLS_CC);
1765 if (!(etag = http_etag(data_ptr, data_size, data_mode))) {
1766 return FAILURE;
1767 }
1768
1769 /* send 304 Not Modified if etag matches */
1770 if ((!is_range_request) && http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
1771 efree(etag);
1772 return http_send_status(304);
1773 }
1774
1775 http_send_etag(etag, 32);
1776 efree(etag);
1777 }
1778
1779 /* send 304 Not Modified if last-modified matches*/
1780 if ((!is_range_request) && http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod))) {
1781 return http_send_status(304);
1782 }
1783
1784 if (is_range_request) {
1785
1786 /* only send ranges if entity hasn't changed */
1787 if (
1788 ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_MATCH", 13)) ||
1789 http_etag_match("HTTP_IF_MATCH", HTTP_G(etag)))
1790 &&
1791 ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
1792 http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod)))
1793 ) {
1794
1795 STATUS result = FAILURE;
1796 zval *zranges = NULL;
1797 MAKE_STD_ZVAL(zranges);
1798 array_init(zranges);
1799
1800 switch (http_get_request_ranges(zranges, data_size))
1801 {
1802 case RANGE_NO:
1803 zval_dtor(zranges);
1804 efree(zranges);
1805 /* go ahead and send all */
1806 break;
1807
1808 case RANGE_OK:
1809 result = http_send_ranges(zranges, data_ptr, data_size, data_mode);
1810 zval_dtor(zranges);
1811 efree(zranges);
1812 return result;
1813 break;
1814
1815 case RANGE_ERR:
1816 zval_dtor(zranges);
1817 efree(zranges);
1818 http_send_status(416);
1819 return FAILURE;
1820 break;
1821
1822 default:
1823 return FAILURE;
1824 break;
1825 }
1826 }
1827 }
1828 /* send all */
1829 return http_send_chunk(data_ptr, 0, data_size, data_mode);
1830 }
1831 /* }}} */
1832
1833 /* {{{ STATUS http_send_data(zval *) */
1834 PHP_HTTP_API STATUS _http_send_data(const zval *zdata TSRMLS_DC)
1835 {
1836 if (!Z_STRLEN_P(zdata)) {
1837 return SUCCESS;
1838 }
1839 if (!Z_STRVAL_P(zdata)) {
1840 return FAILURE;
1841 }
1842
1843 return http_send(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata), SEND_DATA);
1844 }
1845 /* }}} */
1846
1847 /* {{{ STATUS http_send_stream(php_stream *) */
1848 PHP_HTTP_API STATUS _http_send_stream(const php_stream *file TSRMLS_DC)
1849 {
1850 if (php_stream_stat((php_stream *) file, &HTTP_G(ssb))) {
1851 return FAILURE;
1852 }
1853
1854 return http_send(file, HTTP_G(ssb).sb.st_size, SEND_RSRC);
1855 }
1856 /* }}} */
1857
1858 /* {{{ STATUS http_send_file(zval *) */
1859 PHP_HTTP_API STATUS _http_send_file(const zval *zfile TSRMLS_DC)
1860 {
1861 php_stream *file;
1862 STATUS ret;
1863
1864 if (!Z_STRLEN_P(zfile)) {
1865 return FAILURE;
1866 }
1867
1868 if (!(file = php_stream_open_wrapper(Z_STRVAL_P(zfile), "rb",
1869 REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
1870 return FAILURE;
1871 }
1872
1873 ret = http_send_stream(file);
1874 php_stream_close(file);
1875 return ret;
1876 }
1877 /* }}} */
1878
1879 /* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1880 PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded,
1881 const size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
1882 {
1883 const char *e_ptr;
1884 char *d_ptr;
1885
1886 *decoded_len = 0;
1887 *decoded = (char *) ecalloc(encoded_len, 1);
1888 d_ptr = *decoded;
1889 e_ptr = encoded;
1890
1891 while (((e_ptr - encoded) - encoded_len) > 0) {
1892 char hex_len[9] = {0};
1893 size_t chunk_len = 0;
1894 int i = 0;
1895
1896 /* read in chunk size */
1897 while (isxdigit(*e_ptr)) {
1898 if (i == 9) {
1899 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1900 "Chunk size is too long: 0x%s...", hex_len);
1901 efree(*decoded);
1902 return FAILURE;
1903 }
1904 hex_len[i++] = *e_ptr++;
1905 }
1906
1907 /* reached the end */
1908 if (!strcmp(hex_len, "0")) {
1909 break;
1910 }
1911
1912 /* new line */
1913 if (strncmp(e_ptr, HTTP_CRLF, 2)) {
1914 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1915 "Invalid character (expected 0x0D 0x0A; got: %x %x)",
1916 *e_ptr, *(e_ptr + 1));
1917 efree(*decoded);
1918 return FAILURE;
1919 }
1920
1921 /* hex to long */
1922 {
1923 char *error = NULL;
1924 chunk_len = strtol(hex_len, &error, 16);
1925 if (error == hex_len) {
1926 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1927 "Invalid chunk size string: '%s'", hex_len);
1928 efree(*decoded);
1929 return FAILURE;
1930 }
1931 }
1932
1933 memcpy(d_ptr, e_ptr += 2, chunk_len);
1934 d_ptr += chunk_len;
1935 e_ptr += chunk_len + 2;
1936 *decoded_len += chunk_len;
1937 }
1938
1939 return SUCCESS;
1940 }
1941 /* }}} */
1942
1943 /* {{{ proto STATUS http_split_response_ex(char *, size_t, zval *, zval *) */
1944 PHP_HTTP_API STATUS _http_split_response_ex( char *response,
1945 size_t response_len, zval *zheaders, zval *zbody TSRMLS_DC)
1946 {
1947 char *body = NULL;
1948 char *header = response;
1949
1950 while ((response - header + 4) < response_len) {
1951 if ( (*response++ == '\r') &&
1952 (*response++ == '\n') &&
1953 (*response++ == '\r') &&
1954 (*response++ == '\n')) {
1955 body = response;
1956 break;
1957 }
1958 }
1959
1960 if (body && (response_len - (body - header))) {
1961 ZVAL_STRINGL(zbody, body, response_len - (body - header) - 1, 1);
1962 } else {
1963 Z_TYPE_P(zbody) = IS_NULL;
1964 }
1965
1966 return http_parse_headers(header, body ? body - header : response_len, zheaders);
1967 }
1968 /* }}} */
1969
1970 /* {{{ STATUS http_parse_headers(char *, long, zval *) */
1971 PHP_HTTP_API STATUS _http_parse_headers(char *header, int header_len, zval *array TSRMLS_DC)
1972 {
1973 char *colon = NULL, *line = NULL, *begin = header;
1974
1975 if (header_len < 2) {
1976 return FAILURE;
1977 }
1978
1979 /* status code */
1980 if (!strncmp(header, "HTTP/1.", 7)) {
1981 char *end = strstr(header, HTTP_CRLF);
1982 add_assoc_stringl(array, "Status",
1983 header + strlen("HTTP/1.x "),
1984 end - (header + strlen("HTTP/1.x ")), 1);
1985 header = end + 2;
1986 }
1987
1988 line = header;
1989
1990 while (header_len >= (line - begin)) {
1991 int value_len = 0;
1992
1993 switch (*line++)
1994 {
1995 case 0:
1996 --value_len; /* we don't have CR so value length is one char less */
1997 case '\n':
1998 if (colon && ((!(*line - 1)) || ((*line != ' ') && (*line != '\t')))) {
1999
2000 /* skip empty key */
2001 if (header != colon) {
2002 char *key = estrndup(header, colon - header);
2003 value_len += line - colon - 1;
2004
2005 /* skip leading ws */
2006 while (isspace(*(++colon))) --value_len;
2007 /* skip trailing ws */
2008 while (isspace(colon[value_len - 1])) --value_len;
2009
2010 if (value_len < 1) {
2011 /* hm, empty header? */
2012 add_assoc_stringl(array, key, "", 0, 1);
2013 } else {
2014 add_assoc_stringl(array, key, colon, value_len, 1);
2015 }
2016 efree(key);
2017 }
2018
2019 colon = NULL;
2020 value_len = 0;
2021 header += line - header;
2022 }
2023 break;
2024
2025 case ':':
2026 if (!colon) {
2027 colon = line - 1;
2028 }
2029 break;
2030 }
2031 }
2032 return SUCCESS;
2033 }
2034 /* }}} */
2035
2036 /* {{{ void http_get_request_headers(zval *) */
2037 PHP_HTTP_API void _http_get_request_headers(zval *array TSRMLS_DC)
2038 {
2039 char *key;
2040
2041 for ( zend_hash_internal_pointer_reset(HTTP_SERVER_VARS);
2042 zend_hash_get_current_key(HTTP_SERVER_VARS, &key, NULL, 0) != HASH_KEY_NON_EXISTANT;
2043 zend_hash_move_forward(HTTP_SERVER_VARS)) {
2044 if (!strncmp(key, "HTTP_", 5)) {
2045 zval **header;
2046 zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header);
2047 add_assoc_stringl(array, pretty_key(key + 5, strlen(key) - 5, 1, 1), Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
2048 }
2049 }
2050 }
2051 /* }}} */
2052
2053 /* {{{ HAVE_CURL */
2054 #ifdef HTTP_HAVE_CURL
2055
2056 /* {{{ STATUS http_get(char *, HashTable *, HashTable *, char **, size_t *) */
2057 PHP_HTTP_API STATUS _http_get(const char *URL, HashTable *options,
2058 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2059 {
2060 STATUS rs;
2061 CURL *ch = curl_easy_init();
2062
2063 if (!ch) {
2064 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
2065 return FAILURE;
2066 }
2067
2068 rs = http_get_ex(ch, URL, options, info, data, data_len);
2069 curl_easy_cleanup(ch);
2070 return rs;
2071 }
2072 /* }}} */
2073
2074 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, char **, size_t *) */
2075 PHP_HTTP_API STATUS _http_get_ex(CURL *ch, const char *URL, HashTable *options,
2076 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2077 {
2078 http_curl_initbuf(CURLBUF_EVRY);
2079 http_curl_setopts(ch, URL, options);
2080 curl_easy_setopt(ch, CURLOPT_NOBODY, 0);
2081 curl_easy_setopt(ch, CURLOPT_POST, 0);
2082
2083 if (CURLE_OK != curl_easy_perform(ch)) {
2084 http_curl_freebuf(CURLBUF_EVRY);
2085 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
2086 return FAILURE;
2087 }
2088 if (info) {
2089 http_curl_getinfo(ch, info);
2090 }
2091 http_curl_movebuf(CURLBUF_EVRY, data, data_len);
2092 return SUCCESS;
2093 }
2094
2095 /* {{{ STATUS http_head(char *, HashTable *, HashTable *, char **data, size_t *) */
2096 PHP_HTTP_API STATUS _http_head(const char *URL, HashTable *options,
2097 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2098 {
2099 STATUS rs;
2100 CURL *ch = curl_easy_init();
2101
2102 if (!ch) {
2103 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
2104 return FAILURE;
2105 }
2106
2107 rs = http_head_ex(ch, URL, options, info, data, data_len);
2108 curl_easy_cleanup(ch);
2109 return rs;
2110 }
2111 /* }}} */
2112
2113 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, char **data, size_t *) */
2114 PHP_HTTP_API STATUS _http_head_ex(CURL *ch, const char *URL, HashTable *options,
2115 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2116 {
2117 http_curl_initbuf(CURLBUF_HDRS);
2118 http_curl_setopts(ch, URL, options);
2119 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
2120 curl_easy_setopt(ch, CURLOPT_POST, 0);
2121
2122 if (CURLE_OK != curl_easy_perform(ch)) {
2123 http_curl_freebuf(CURLBUF_HDRS);
2124 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
2125 return FAILURE;
2126 }
2127 if (info) {
2128 http_curl_getinfo(ch, info);
2129 }
2130 http_curl_movebuf(CURLBUF_HDRS, data, data_len);
2131 return SUCCESS;
2132 }
2133
2134 /* {{{ STATUS http_post_data(char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
2135 PHP_HTTP_API STATUS _http_post_data(const char *URL, char *postdata,
2136 size_t postdata_len, HashTable *options, HashTable *info, char **data,
2137 size_t *data_len TSRMLS_DC)
2138 {
2139 STATUS rs;
2140 CURL *ch = curl_easy_init();
2141
2142 if (!ch) {
2143 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
2144 return FAILURE;
2145 }
2146 rs = http_post_data_ex(ch, URL, postdata, postdata_len, options, info, data, data_len);
2147 curl_easy_cleanup(ch);
2148 return rs;
2149 }
2150 /* }}} */
2151
2152 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
2153 PHP_HTTP_API STATUS _http_post_data_ex(CURL *ch, const char *URL, char *postdata,
2154 size_t postdata_len, HashTable *options, HashTable *info, char **data,
2155 size_t *data_len TSRMLS_DC)
2156 {
2157 http_curl_initbuf(CURLBUF_EVRY);
2158 http_curl_setopts(ch, URL, options);
2159 curl_easy_setopt(ch, CURLOPT_POST, 1);
2160 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, postdata);
2161 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, postdata_len);
2162
2163 if (CURLE_OK != curl_easy_perform(ch)) {
2164 http_curl_freebuf(CURLBUF_EVRY);
2165 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
2166 return FAILURE;
2167 }
2168 if (info) {
2169 http_curl_getinfo(ch, info);
2170 }
2171 http_curl_movebuf(CURLBUF_EVRY, data, data_len);
2172 return SUCCESS;
2173 }
2174 /* }}} */
2175
2176 /* {{{ STATUS http_post_array(char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
2177 PHP_HTTP_API STATUS _http_post_array(const char *URL, HashTable *postarray,
2178 HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2179 {
2180 smart_str qstr = {0};
2181 STATUS status;
2182
2183 if (php_url_encode_hash_ex(postarray, &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
2184 if (qstr.c) {
2185 efree(qstr.c);
2186 }
2187 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode post data");
2188 return FAILURE;
2189 }
2190 smart_str_0(&qstr);
2191
2192 status = http_post_data(URL, qstr.c, qstr.len, options, info, data, data_len);
2193 if (qstr.c) {
2194 efree(qstr.c);
2195 }
2196 return status;
2197 }
2198 /* }}} */
2199
2200 #endif
2201 /* }}} HAVE_CURL */
2202
2203 /* {{{ STATUS http_auth_header(char *, char*) */
2204 PHP_HTTP_API STATUS _http_auth_header(const char *type, const char *realm TSRMLS_DC)
2205 {
2206 char realm_header[1024] = {0};
2207 snprintf(realm_header, 1023, "WWW-Authenticate: %s realm=\"%s\"", type, realm);
2208 return http_send_status_header(401, realm_header);
2209 }
2210 /* }}} */
2211
2212 /* {{{ STATUS http_auth_credentials(char **, char **) */
2213 PHP_HTTP_API STATUS _http_auth_credentials(char **user, char **pass TSRMLS_DC)
2214 {
2215 if (strncmp(sapi_module.name, "isapi", 5)) {
2216 zval *zuser, *zpass;
2217
2218 HTTP_GSC(zuser, "PHP_AUTH_USER", FAILURE);
2219 HTTP_GSC(zpass, "PHP_AUTH_PW", FAILURE);
2220
2221 *user = estrndup(Z_STRVAL_P(zuser), Z_STRLEN_P(zuser));
2222 *pass = estrndup(Z_STRVAL_P(zpass), Z_STRLEN_P(zpass));
2223
2224 return SUCCESS;
2225 } else {
2226 zval *zauth = NULL;
2227 HTTP_GSC(zauth, "HTTP_AUTHORIZATION", FAILURE);
2228 {
2229 char *decoded, *colon;
2230 int decoded_len;
2231 decoded = php_base64_decode(Z_STRVAL_P(zauth), Z_STRLEN_P(zauth),
2232 &decoded_len);
2233
2234 if (colon = strchr(decoded + 6, ':')) {
2235 *user = estrndup(decoded + 6, colon - decoded - 6);
2236 *pass = estrndup(colon + 1, decoded + decoded_len - colon - 6 - 1);
2237
2238 return SUCCESS;
2239 } else {
2240 return FAILURE;
2241 }
2242 }
2243 }
2244 }
2245 /* }}} */
2246
2247 /* }}} public API */
2248
2249 /*
2250 * Local variables:
2251 * tab-width: 4
2252 * c-basic-offset: 4
2253 * End:
2254 * vim600: noet sw=4 ts=4 fdm=marker
2255 * vim<600: noet sw=4 ts=4
2256 */