2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
28 #include "php_version.h"
29 #include "php_streams.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"
41 # include "ext/standard/php_http.h"
43 #include "http_build_query.c"
47 #include "php_http_api.h"
52 # include <winsock2.h>
53 # include <sys/types.h>
56 # include <curl/curl.h>
57 # include <curl/easy.h>
62 ZEND_DECLARE_MODULE_GLOBALS(http
)
64 /* {{{ day/month names */
65 static const char *days
[] = {
66 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
68 static const char *wkdays
[] = {
69 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
71 static const char *weekdays
[] = {
72 "Monday", "Tuesday", "Wednesday",
73 "Thursday", "Friday", "Saturday", "Sunday"
75 static const char *months
[] = {
76 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
77 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
84 static const struct time_zone
{
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 */
136 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
);
137 #define http_etag(e, p, l, m) _http_etag((e), (p), (l), (m) TSRMLS_CC)
138 static inline char *_http_etag(char **new_etag
, const void *data_ptr
, const size_t data_len
, const http_send_mode data_mode TSRMLS_DC
);
139 #define http_is_range_request() _http_is_range_request(TSRMLS_C)
140 static inline int _http_is_range_request(TSRMLS_D
);
141 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
142 static STATUS
_http_send_chunk(const void *data
, const size_t begin
, const size_t end
, const http_send_mode mode TSRMLS_DC
);
144 static int check_day(char *day
, size_t len
);
145 static int check_month(char *month
);
146 static int check_tzone(char *tzone
);
148 static char *pretty_key(char *key
, int key_len
, int uctitle
, int xhyphen
);
150 static int http_ob_stack_get(php_ob_buffer
*, php_ob_buffer
**);
153 #ifdef HTTP_HAVE_CURL
154 #define http_curl_initbuf(m) _http_curl_initbuf((m) TSRMLS_CC)
155 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC
);
156 #define http_curl_freebuf(m) _http_curl_freebuf((m) TSRMLS_CC)
157 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC
);
158 #define http_curl_sizebuf(m, l) _http_curl_sizebuf((m), (l) TSRMLS_CC)
159 static inline void _http_curl_sizebuf(http_curlbuf_member member
, size_t len TSRMLS_DC
);
160 #define http_curl_movebuf(m, d, l) _http_curl_movebuf((m), (d), (l) TSRMLS_CC)
161 static inline void _http_curl_movebuf(http_curlbuf_member member
, char **data
, size_t *data_len TSRMLS_DC
);
162 #define http_curl_copybuf(m, d, l) _http_curl_copybuf((m), (d), (l) TSRMLS_CC)
163 static inline void _http_curl_copybuf(http_curlbuf_member member
, char **data
, size_t *data_len TSRMLS_DC
);
164 #define http_curl_setopts(c, u, o) _http_curl_setopts((c), (u), (o) TSRMLS_CC)
165 static inline void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
);
167 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
168 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
169 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
170 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...);
172 static size_t http_curl_body_callback(char *, size_t, size_t, void *);
173 static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *);
175 #define http_curl_getinfo(c, h) _http_curl_getinfo((c), (h) TSRMLS_CC)
176 static inline void _http_curl_getinfo(CURL
*ch
, HashTable
*info TSRMLS_DC
);
177 #define http_curl_getinfo_ex(c, i, a) _http_curl_getinfo_ex((c), (i), (a) TSRMLS_CC)
178 static inline void _http_curl_getinfo_ex(CURL
*ch
, CURLINFO i
, zval
*array TSRMLS_DC
);
179 #define http_curl_getinfoname(i) _http_curl_getinfoname((i) TSRMLS_CC)
180 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC
);
185 /* {{{ static int http_sort_q(const void *, const void *) */
186 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
189 zval result
, *first
, *second
;
191 f
= *((Bucket
**) a
);
192 s
= *((Bucket
**) b
);
194 first
= *((zval
**) f
->pData
);
195 second
= *((zval
**) s
->pData
);
197 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
200 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
204 /* {{{ static inline char *http_etag(char **, void *, size_t, http_send_mode) */
205 static inline char *_http_etag(char **new_etag
, const void *data_ptr
,
206 const size_t data_len
, const http_send_mode data_mode TSRMLS_DC
)
209 unsigned char digest
[16];
217 PHP_MD5Update(&ctx
, data_ptr
, data_len
);
221 snprintf(ssb_buf
, 127, "%l=%l=%l",
222 HTTP_G(ssb
).sb
.st_mtime
,
223 HTTP_G(ssb
).sb
.st_ino
,
224 HTTP_G(ssb
).sb
.st_size
226 PHP_MD5Update(&ctx
, ssb_buf
, strlen(ssb_buf
));
234 PHP_MD5Final(digest
, &ctx
);
235 make_digest(*new_etag
, digest
);
241 /* {{{ static inline int http_is_range_request(void) */
242 static inline int _http_is_range_request(TSRMLS_D
)
244 return zend_hash_exists(Z_ARRVAL_P(PG(http_globals
)[TRACK_VARS_SERVER
]),
245 "HTTP_RANGE", strlen("HTTP_RANGE") + 1);
249 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
251 static STATUS
_http_send_chunk(const void *data
, const size_t begin
,
252 const size_t end
, const http_send_mode mode TSRMLS_DC
)
256 long len
= end
- begin
;
262 s
= (php_stream
*) data
;
263 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
266 buf
= (char *) ecalloc(1, HTTP_BUF_SIZE
);
267 /* read into buf and write out */
268 while ((len
-= HTTP_BUF_SIZE
) >= 0) {
269 if (!(read
= php_stream_read(s
, buf
, HTTP_BUF_SIZE
))) {
273 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
279 /* read & write left over */
281 if (read
= php_stream_read(s
, buf
, HTTP_BUF_SIZE
+ len
)) {
282 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
296 return len
== php_body_write(((char *)data
) + begin
, len TSRMLS_CC
)
308 #ifdef HTTP_HAVE_CURL
310 /* {{{ static inline void http_curl_initbuf(http_curlbuf_member) */
311 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC
)
313 http_curl_freebuf(member
);
315 if (member
& CURLBUF_HDRS
) {
316 HTTP_G(curlbuf
).hdrs
.data
= emalloc(HTTP_CURLBUF_HDRSSIZE
);
317 HTTP_G(curlbuf
).hdrs
.free
= HTTP_CURLBUF_HDRSSIZE
;
319 if (member
& CURLBUF_BODY
) {
320 HTTP_G(curlbuf
).body
.data
= emalloc(HTTP_CURLBUF_BODYSIZE
);
321 HTTP_G(curlbuf
).body
.free
= HTTP_CURLBUF_BODYSIZE
;
326 /* {{{ static inline void http_curl_freebuf(http_curlbuf_member) */
327 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC
)
329 if (member
& CURLBUF_HDRS
) {
330 if (HTTP_G(curlbuf
).hdrs
.data
) {
331 efree(HTTP_G(curlbuf
).hdrs
.data
);
332 HTTP_G(curlbuf
).hdrs
.data
= NULL
;
334 HTTP_G(curlbuf
).hdrs
.used
= 0;
335 HTTP_G(curlbuf
).hdrs
.free
= 0;
337 if (member
& CURLBUF_BODY
) {
338 if (HTTP_G(curlbuf
).body
.data
) {
339 efree(HTTP_G(curlbuf
).body
.data
);
340 HTTP_G(curlbuf
).body
.data
= NULL
;
342 HTTP_G(curlbuf
).body
.used
= 0;
343 HTTP_G(curlbuf
).body
.free
= 0;
348 /* {{{ static inline void http_curl_copybuf(http_curlbuf_member, char **,
350 static inline void _http_curl_copybuf(http_curlbuf_member member
, char **data
,
351 size_t *data_len TSRMLS_DC
)
356 if ((member
& CURLBUF_HDRS
) && HTTP_G(curlbuf
).hdrs
.used
) {
357 if ((member
& CURLBUF_BODY
) && HTTP_G(curlbuf
).body
.used
) {
358 *data
= emalloc(HTTP_G(curlbuf
).hdrs
.used
+ HTTP_G(curlbuf
).body
.used
+ 1);
360 *data
= emalloc(HTTP_G(curlbuf
).hdrs
.used
+ 1);
362 memcpy(*data
, HTTP_G(curlbuf
).hdrs
.data
, HTTP_G(curlbuf
).hdrs
.used
);
363 *data_len
= HTTP_G(curlbuf
).hdrs
.used
;
366 if ((member
& CURLBUF_BODY
) && HTTP_G(curlbuf
).body
.used
) {
368 memcpy((*data
) + HTTP_G(curlbuf
).hdrs
.used
,
369 HTTP_G(curlbuf
).body
.data
, HTTP_G(curlbuf
).body
.used
);
370 *data_len
= HTTP_G(curlbuf
).hdrs
.used
+ HTTP_G(curlbuf
).body
.used
;
372 emalloc(HTTP_G(curlbuf
).body
.used
+ 1);
373 memcpy(*data
, HTTP_G(curlbuf
).body
.data
, HTTP_G(curlbuf
).body
.used
);
374 *data_len
= HTTP_G(curlbuf
).body
.used
;
378 (*data
)[*data_len
] = 0;
385 /* {{{ static inline void http_curl_movebuf(http_curlbuf_member, char **,
387 static inline void _http_curl_movebuf(http_curlbuf_member member
, char **data
,
388 size_t *data_len TSRMLS_DC
)
390 http_curl_copybuf(member
, data
, data_len
);
391 http_curl_freebuf(member
);
395 /* {{{ static size_t http_curl_body_callback(char *, size_t, size_t, void *) */
396 static size_t http_curl_body_callback(char *buf
, size_t len
, size_t n
, void *s
)
400 if ((len
*= n
) > HTTP_G(curlbuf
).body
.free
) {
401 size_t bsize
= HTTP_CURLBUF_BODYSIZE
;
402 while (bsize
< len
) {
405 HTTP_G(curlbuf
).body
.data
= erealloc(HTTP_G(curlbuf
).body
.data
,
406 HTTP_G(curlbuf
).body
.used
+ bsize
);
407 HTTP_G(curlbuf
).body
.free
+= bsize
;
410 memcpy(HTTP_G(curlbuf
).body
.data
+ HTTP_G(curlbuf
).body
.used
, buf
, len
);
411 HTTP_G(curlbuf
).body
.free
-= len
;
412 HTTP_G(curlbuf
).body
.used
+= len
;
418 /* {{{ static size_t http_curl_hdrs_callback(char*, size_t, size_t, void *) */
419 static size_t http_curl_hdrs_callback(char *buf
, size_t len
, size_t n
, void *s
)
423 /* discard previous headers */
424 if ((HTTP_G(curlbuf
).hdrs
.used
) && (!strncmp(buf
, "HTTP/1.", strlen("HTTP/1.")))) {
425 http_curl_initbuf(CURLBUF_HDRS
);
428 if ((len
*= n
) > HTTP_G(curlbuf
).hdrs
.free
) {
429 size_t bsize
= HTTP_CURLBUF_HDRSSIZE
;
430 while (bsize
< len
) {
433 HTTP_G(curlbuf
).hdrs
.data
= erealloc(HTTP_G(curlbuf
).hdrs
.data
,
434 HTTP_G(curlbuf
).hdrs
.used
+ bsize
);
435 HTTP_G(curlbuf
).hdrs
.free
+= bsize
;
438 memcpy(HTTP_G(curlbuf
).hdrs
.data
+ HTTP_G(curlbuf
).hdrs
.used
, buf
, len
);
439 HTTP_G(curlbuf
).hdrs
.free
-= len
;
440 HTTP_G(curlbuf
).hdrs
.used
+= len
;
446 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
447 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...)
453 if (SUCCESS
!= zend_hash_find(options
, key
, strlen(key
) + 1, (void **) &zoption
)) {
460 va_start(types
, checks
);
461 for (i
= 0; i
< checks
; ++i
) {
462 if ((va_arg(types
, int)) == (Z_TYPE_PP(zoption
))) {
472 /* {{{ static inline void http_curl_setopts(CURL *, char *, HashTable *) */
473 static inline void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
)
477 /* standard options */
478 curl_easy_setopt(ch
, CURLOPT_URL
, url
);
479 curl_easy_setopt(ch
, CURLOPT_HEADER
, 0);
480 curl_easy_setopt(ch
, CURLOPT_NOPROGRESS
, 1);
481 curl_easy_setopt(ch
, CURLOPT_AUTOREFERER
, 1);
482 curl_easy_setopt(ch
, CURLOPT_WRITEFUNCTION
, http_curl_body_callback
);
483 curl_easy_setopt(ch
, CURLOPT_HEADERFUNCTION
, http_curl_hdrs_callback
);
485 curl_easy_setopt(ch
, CURLOPT_NOSIGNAL
, 1);
488 if ((!options
) || (1 > zend_hash_num_elements(options
))) {
492 /* redirects, defaults to 0 */
493 if (zoption
= http_curl_getopt1(options
, "redirect", IS_LONG
)) {
494 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
495 curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, Z_LVAL_P(zoption
));
496 if (zoption
= http_curl_getopt2(options
, "unrestrictedauth", IS_LONG
, IS_BOOL
)) {
497 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
500 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, 0);
504 if (zoption
= http_curl_getopt1(options
, "proxyhost", IS_STRING
)) {
505 curl_easy_setopt(ch
, CURLOPT_PROXY
, Z_STRVAL_P(zoption
));
507 if (zoption
= http_curl_getopt1(options
, "proxyport", IS_LONG
)) {
508 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, Z_LVAL_P(zoption
));
511 if (zoption
= http_curl_getopt1(options
, "proxyauth", IS_STRING
)) {
512 curl_easy_setopt(ch
, CURLOPT_PROXYUSERPWD
, Z_STRVAL_P(zoption
));
515 if (zoption
= http_curl_getopt1(options
, "proxyauthtype", IS_LONG
)) {
516 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, Z_LVAL_P(zoption
));
521 if (zoption
= http_curl_getopt1(options
, "httpauth", IS_STRING
)) {
522 curl_easy_setopt(ch
, CURLOPT_USERPWD
, Z_STRVAL_P(zoption
));
524 if (zoption
= http_curl_getopt1(options
, "httpauthtype", IS_LONG
)) {
525 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, Z_LVAL_P(zoption
));
528 /* compress, enabled by default (empty string enables deflate and gzip) */
529 if (zoption
= http_curl_getopt2(options
, "compress", IS_LONG
, IS_BOOL
)) {
530 if (Z_LVAL_P(zoption
)) {
531 curl_easy_setopt(ch
, CURLOPT_ENCODING
, "");
534 curl_easy_setopt(ch
, CURLOPT_ENCODING
, "");
538 if (zoption
= http_curl_getopt1(options
, "port", IS_LONG
)) {
539 curl_easy_setopt(ch
, CURLOPT_PORT
, Z_LVAL_P(zoption
));
543 if (zoption
= http_curl_getopt1(options
, "referer", IS_STRING
)) {
544 curl_easy_setopt(ch
, CURLOPT_REFERER
, Z_STRVAL_P(zoption
));
547 /* useragent, default "PECL::HTTP/version (PHP/version)" */
548 if (zoption
= http_curl_getopt1(options
, "useragent", IS_STRING
)) {
549 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, Z_STRVAL_P(zoption
));
551 curl_easy_setopt(ch
, CURLOPT_USERAGENT
,
552 "PECL::HTTP/" PHP_EXT_HTTP_VERSION
" (PHP/" PHP_VERSION
")");
555 /* cookies, array('name' => 'value') */
556 if (zoption
= http_curl_getopt1(options
, "cookies", IS_ARRAY
)) {
560 smart_str qstr
= {0};
562 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption
));
563 while (HASH_KEY_NON_EXISTANT
!= (key_type
= zend_hash_get_current_key_type(Z_ARRVAL_P(zoption
)))) {
564 if (key_type
== HASH_KEY_IS_STRING
) {
565 zend_hash_get_current_key(Z_ARRVAL_P(zoption
), &cookie_key
, NULL
, 0);
566 zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
);
567 smart_str_appends(&qstr
, cookie_key
);
568 smart_str_appendl(&qstr
, "=", 1);
569 smart_str_appendl(&qstr
, Z_STRVAL_PP(cookie_val
), Z_STRLEN_PP(cookie_val
));
570 smart_str_appendl(&qstr
, "; ", 2);
571 zend_hash_move_forward(Z_ARRVAL_P(zoption
));
577 curl_easy_setopt(ch
, CURLOPT_COOKIE
, qstr
.c
);
582 if (zoption
= http_curl_getopt1(options
, "cookiestore", IS_STRING
)) {
583 curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, Z_STRVAL_P(zoption
));
584 curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, Z_STRVAL_P(zoption
));
587 /* additional headers, array('name' => 'value') */
588 if (zoption
= http_curl_getopt1(options
, "headers", IS_ARRAY
)) {
590 char *header_key
, header
[1024] = {0};
592 struct curl_slist
*headers
= NULL
;
594 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption
));
595 while (HASH_KEY_NON_EXISTANT
!= (key_type
= zend_hash_get_current_key_type(Z_ARRVAL_P(zoption
)))) {
596 if (key_type
== HASH_KEY_IS_STRING
) {
597 zend_hash_get_current_key(Z_ARRVAL_P(zoption
), &header_key
, NULL
, 0);
598 zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
);
599 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
600 headers
= curl_slist_append(headers
, header
);
601 zend_hash_move_forward(Z_ARRVAL_P(zoption
));
605 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, headers
);
611 /* {{{ static inline char *http_curl_getinfoname(CURLINFO) */
612 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC
)
614 #define CASE(I) case CURLINFO_ ##I : return pretty_key(estrdup( #I ), strlen(#I), 0, 0)
617 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
619 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
621 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
623 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
624 CASE(NAMELOOKUP_TIME
);
625 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
627 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
628 CASE(PRETRANSFER_TIME
);
629 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
631 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
633 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
634 CASE(SPEED_DOWNLOAD
);
635 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
637 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
639 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
641 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
642 CASE(SSL_VERIFYRESULT
);
643 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
645 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
646 CASE(CONTENT_LENGTH_DOWNLOAD
);
647 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
648 CASE(CONTENT_LENGTH_UPLOAD
);
649 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
650 CASE(STARTTRANSFER_TIME
);
651 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
653 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
655 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
656 CASE(REDIRECT_COUNT
);
657 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
659 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
660 CASE(HTTP_CONNECTCODE
);
661 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
662 CASE(HTTPAUTH_AVAIL
);
663 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
664 CASE(PROXYAUTH_AVAIL
);
671 /* {{{ static inline void http_curl_getinfo_ex(CURL, CURLINFO, zval *) */
672 static inline void _http_curl_getinfo_ex(CURL
*ch
, CURLINFO i
, zval
*array TSRMLS_DC
)
675 if (key
= http_curl_getinfoname(i
)) {
676 switch (i
& ~CURLINFO_MASK
)
678 case CURLINFO_STRING
:
681 if (CURLE_OK
== curl_easy_getinfo(ch
, i
, &c
)) {
682 add_assoc_string(array
, key
, c
? c
: "", 1);
687 case CURLINFO_DOUBLE
:
690 if (CURLE_OK
== curl_easy_getinfo(ch
, i
, &d
)) {
691 add_assoc_double(array
, key
, d
);
699 if (CURLE_OK
== curl_easy_getinfo(ch
, i
, &l
)) {
700 add_assoc_long(array
, key
, l
);
709 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
710 static inline void _http_curl_getinfo(CURL
*ch
, HashTable
*info TSRMLS_DC
)
713 Z_ARRVAL(array
) = info
;
715 #define INFO(I) http_curl_getinfo_ex(ch, CURLINFO_ ##I , &array)
716 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
718 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
720 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
722 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
723 INFO(NAMELOOKUP_TIME
);
724 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
726 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
727 INFO(PRETRANSFER_TIME
);
728 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
730 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
732 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
733 INFO(SPEED_DOWNLOAD
);
734 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
736 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
738 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
740 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
741 INFO(SSL_VERIFYRESULT
);
742 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
744 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
745 INFO(CONTENT_LENGTH_DOWNLOAD
);
746 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
747 INFO(CONTENT_LENGTH_UPLOAD
);
748 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
749 INFO(STARTTRANSFER_TIME
);
750 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
752 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
754 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
755 INFO(REDIRECT_COUNT
);
756 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
758 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
759 INFO(HTTP_CONNECTCODE
);
760 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
761 INFO(HTTPAUTH_AVAIL
);
762 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
763 INFO(PROXYAUTH_AVAIL
);
771 /* {{{ Day/Month/TZ checks for http_parse_date()
772 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
773 static int check_day(char *day
, size_t len
)
776 const char * const *check
= (len
> 3) ? &weekdays
[0] : &wkdays
[0];
777 for (i
= 0; i
< 7; i
++) {
778 if (!strcmp(day
, check
[0])) {
786 static int check_month(char *month
)
789 const char * const *check
= &months
[0];
790 for (i
= 0; i
< 12; i
++) {
791 if (!strcmp(month
, check
[0])) {
799 /* return the time zone offset between GMT and the input one, in number
800 of seconds or -1 if the timezone wasn't found/legal */
802 static int check_tzone(char *tzone
)
805 const struct time_zone
*check
= time_zones
;
806 for (i
= 0; i
< sizeof(time_zones
) / sizeof(time_zones
[0]); i
++) {
807 if (!strcmp(tzone
, check
->name
)) {
808 return check
->offset
* 60;
816 /* static char *pretty_key(char *, int, int, int) */
817 static char *pretty_key(char *key
, int key_len
, int uctitle
, int xhyphen
)
819 if (key
&& key_len
) {
821 if (wasalpha
= isalpha(key
[0])) {
822 key
[0] = uctitle
? toupper(key
[0]) : tolower(key
[0]);
824 for (i
= 1; i
< key_len
; i
++) {
825 if (isalpha(key
[i
])) {
826 key
[i
] = ((!wasalpha
) && uctitle
) ? toupper(key
[i
]) : tolower(key
[i
]);
829 if (xhyphen
&& (key
[i
] == '_')) {
840 /* {{{ static STATUS http_ob_stack_get(php_ob_buffer *, php_ob_buffer **) */
841 static STATUS
http_ob_stack_get(php_ob_buffer
*o
, php_ob_buffer
**s
)
844 php_ob_buffer
*b
= emalloc(sizeof(php_ob_buffer
));
845 b
->handler_name
= estrdup(o
->handler_name
);
846 b
->buffer
= estrndup(o
->buffer
, o
->text_length
);
847 b
->chunk_size
= o
->chunk_size
;
858 /* {{{ char *http_date(time_t) */
859 PHP_HTTP_API
char *_http_date(time_t t TSRMLS_DC
)
861 struct tm
*gmtime
, tmbuf
;
862 char *date
= ecalloc(31, 1);
864 gmtime
= php_gmtime_r(&t
, &tmbuf
);
866 "%s, %02d %s %04d %02d:%02d:%02d GMT",
867 days
[gmtime
->tm_wday
], gmtime
->tm_mday
,
868 months
[gmtime
->tm_mon
], gmtime
->tm_year
+ 1900,
869 gmtime
->tm_hour
, gmtime
->tm_min
, gmtime
->tm_sec
875 /* {{{ time_t http_parse_date(char *)
876 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
877 PHP_HTTP_API
time_t _http_parse_date(const char *date
)
880 int tz_offset
= -1, year
= -1, month
= -1, monthday
= -1, weekday
= -1,
881 hours
= -1, minutes
= -1, seconds
= -1;
883 enum assume_next dignext
= DATE_MDAY
;
884 const char *indate
= date
;
886 int found
= 0, part
= 0; /* max 6 parts */
888 while (*date
&& (part
< 6)) {
891 while (*date
&& !isalnum(*date
)) {
895 if (isalpha(*date
)) {
896 /* a name coming up */
899 sscanf(date
, "%31[A-Za-z]", buf
);
903 weekday
= check_day(buf
, len
);
909 if (!found
&& (month
== -1)) {
910 month
= check_month(buf
);
916 if (!found
&& (tz_offset
== -1)) {
917 /* this just must be a time zone string */
918 tz_offset
= check_tzone(buf
);
919 if (tz_offset
!= -1) {
925 return -1; /* bad string */
929 else if (isdigit(*date
)) {
933 if((seconds
== -1) && (3 == sscanf(date
, "%02d:%02d:%02d", &hours
, &minutes
, &seconds
))) {
939 val
= (int) strtol(date
, &end
, 10);
941 if ((tz_offset
== -1) && ((end
- date
) == 4) && (val
< 1300) && (indate
< date
) && ((date
[-1] == '+' || date
[-1] == '-'))) {
942 /* four digits and a value less than 1300 and it is preceeded with
943 a plus or minus. This is a time zone indication. */
945 tz_offset
= (val
/ 100 * 60 + val
% 100) * 60;
947 /* the + and - prefix indicates the local time compared to GMT,
948 this we need ther reversed math to get what we want */
949 tz_offset
= date
[-1] == '+' ? -tz_offset
: tz_offset
;
952 if (((end
- date
) == 8) && (year
== -1) && (month
== -1) && (monthday
== -1)) {
953 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
956 month
= (val
% 10000) / 100 - 1; /* month is 0 - 11 */
957 monthday
= val
% 100;
960 if (!found
&& (dignext
== DATE_MDAY
) && (monthday
== -1)) {
961 if ((val
> 0) && (val
< 32)) {
968 if (!found
&& (dignext
== DATE_YEAR
) && (year
== -1)) {
972 year
+= year
> 70 ? 1900 : 2000;
991 seconds
= minutes
= hours
= 0; /* no time, make it zero */
994 if ((-1 == monthday
) || (-1 == month
) || (-1 == year
)) {
995 /* lacks vital info, fail */
999 if (sizeof(time_t) < 5) {
1000 /* 32 bit time_t can only hold dates to the beginning of 2038 */
1006 tm
.tm_sec
= seconds
;
1007 tm
.tm_min
= minutes
;
1009 tm
.tm_mday
= monthday
;
1011 tm
.tm_year
= year
- 1900;
1018 /* time zone adjust */
1020 struct tm
*gmt
, keeptime2
;
1024 if(!(gmt
= php_gmtime_r(&t
, &keeptime2
))) {
1025 return -1; /* illegal date/time */
1030 /* Add the time zone diff (between the given timezone and GMT) and the
1031 diff between the local time zone and GMT. */
1032 delta
= (tz_offset
!= -1 ? tz_offset
: 0) + (t
- t2
);
1034 if((delta
> 0) && (t
+ delta
< t
)) {
1035 return -1; /* time_t overflow */
1045 /* {{{ inline STATUS http_send_status(int) */
1046 PHP_HTTP_API
inline STATUS
_http_send_status(const int status TSRMLS_DC
)
1049 return sapi_header_op(SAPI_HEADER_SET_STATUS
, (void *) s TSRMLS_CC
);
1053 /* {{{ inline STATUS http_send_header(char *) */
1054 PHP_HTTP_API
inline STATUS
_http_send_header(const char *header TSRMLS_DC
)
1056 return http_send_status_header(0, header
);
1060 /* {{{ inline STATUS http_send_status_header(int, char *) */
1061 PHP_HTTP_API
inline STATUS
_http_send_status_header(const int status
, const char *header TSRMLS_DC
)
1063 sapi_header_line h
= {(char *) header
, strlen(header
), status
};
1064 return sapi_header_op(SAPI_HEADER_REPLACE
, &h TSRMLS_CC
);
1068 /* {{{ inline zval *http_get_server_var(char *) */
1069 PHP_HTTP_API
inline zval
*_http_get_server_var(const char *key TSRMLS_DC
)
1072 if (SUCCESS
== zend_hash_find(
1074 (char *) key
, strlen(key
) + 1, (void **) &var
)) {
1081 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
1082 PHP_HTTP_API
void _http_ob_etaghandler(char *output
, uint output_len
,
1083 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
1085 char etag
[33] = { 0 };
1086 unsigned char digest
[16];
1088 if (mode
& PHP_OUTPUT_HANDLER_START
) {
1089 PHP_MD5Init(&HTTP_G(etag_md5
));
1092 PHP_MD5Update(&HTTP_G(etag_md5
), output
, output_len
);
1094 if (mode
& PHP_OUTPUT_HANDLER_END
) {
1095 PHP_MD5Final(digest
, &HTTP_G(etag_md5
));
1097 /* just do that if desired */
1098 if (HTTP_G(etag_started
)) {
1099 make_digest(etag
, digest
);
1101 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
1102 http_send_status(304);
1104 http_send_etag(etag
, 32);
1109 *handled_output_len
= output_len
;
1110 *handled_output
= estrndup(output
, output_len
);
1114 /* {{{ STATUS http_start_ob_handler(php_output_handler_func_t, char *, uint, zend_bool) */
1115 PHP_HTTP_API STATUS
_http_start_ob_handler(php_output_handler_func_t handler_func
,
1116 char *handler_name
, uint chunk_size
, zend_bool erase TSRMLS_DC
)
1118 php_ob_buffer
**stack
;
1121 if (count
= OG(ob_nesting_level
)) {
1122 stack
= ecalloc(sizeof(php_ob_buffer
), count
);
1125 zend_stack_apply_with_argument(&OG(ob_buffers
), ZEND_STACK_APPLY_BOTTOMUP
,
1126 (int (*)(void *elem
, void *)) http_ob_stack_get
, stack
);
1130 http_ob_stack_get(&OG(active_ob_buffer
), stack
);
1133 while (OG(ob_nesting_level
)) {
1134 php_end_ob_buffer(0, 0 TSRMLS_CC
);
1138 php_ob_set_internal_handler(handler_func
, chunk_size
, handler_name
, erase TSRMLS_CC
);
1140 for (i
= 0; i
< count
; i
++) {
1141 php_ob_buffer
*s
= stack
[i
];
1142 php_start_ob_buffer_named(s
->handler_name
, s
->chunk_size
, s
->erase TSRMLS_CC
);
1143 php_body_write(s
->buffer
, s
->text_length TSRMLS_CC
);
1150 /* {{{ int http_modified_match(char *, int) */
1151 PHP_HTTP_API
int _http_modified_match(const char *entry
, const time_t t TSRMLS_DC
)
1155 char *modified
, *chr_ptr
;
1157 HTTP_GSC(zmodified
, entry
, 0);
1159 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
1160 if (chr_ptr
= strrchr(modified
, ';')) {
1163 retval
= (t
<= http_parse_date(modified
));
1169 /* {{{ int http_etag_match(char *, char *) */
1170 PHP_HTTP_API
int _http_etag_match(const char *entry
, const char *etag TSRMLS_DC
)
1176 HTTP_GSC(zetag
, entry
, 0);
1178 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
1182 quoted_etag
= (char *) emalloc(strlen(etag
) + 3);
1183 sprintf(quoted_etag
, "\"%s\"", etag
);
1185 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
1186 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
1188 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
1195 /* {{{ STATUS http_send_last_modified(int) */
1196 PHP_HTTP_API STATUS
_http_send_last_modified(const time_t t TSRMLS_DC
)
1198 char modified
[96] = "Last-Modified: ", *date
;
1199 date
= http_date(t
);
1200 strcat(modified
, date
);
1206 return http_send_header(modified
);
1210 /* {{{ static STATUS http_send_etag(char *, int) */
1211 PHP_HTTP_API STATUS
_http_send_etag(const char *etag
,
1212 const int etag_len TSRMLS_DC
)
1218 header_len
= sizeof("ETag: \"\"") + etag_len
+ 1;
1219 etag_header
= ecalloc(header_len
, 1);
1220 sprintf(etag_header
, "ETag: \"%s\"", etag
);
1221 ret
= http_send_header(etag_header
);
1225 php_error_docref(NULL TSRMLS_CC
,E_ERROR
,
1226 "Sending empty Etag (previous: %s)\n", HTTP_G(etag
));
1231 efree(HTTP_G(etag
));
1233 HTTP_G(etag
) = estrdup(etag
);
1239 /* {{{ char *http_absolute_uri(char *, char *) */
1240 PHP_HTTP_API
char *_http_absolute_uri(const char *url
,
1241 const char *proto TSRMLS_DC
)
1243 char URI
[HTTP_URI_MAXLEN
+ 1], *PTR
, *proto_ptr
, *host
, *path
;
1246 if (!url
|| !strlen(url
)) {
1247 if (!SG(request_info
).request_uri
) {
1250 url
= SG(request_info
).request_uri
;
1252 /* Mess around with already absolute URIs */
1253 else if (proto_ptr
= strstr(url
, "://")) {
1254 if (!proto
|| !strncmp(url
, proto
, strlen(proto
))) {
1255 return estrdup(url
);
1257 snprintf(URI
, HTTP_URI_MAXLEN
, "%s%s", proto
, proto_ptr
+ 3);
1258 return estrdup(URI
);
1262 /* protocol defaults to http */
1263 if (!proto
|| !strlen(proto
)) {
1268 if ( (zhost
= http_get_server_var("HTTP_HOST")) ||
1269 (zhost
= http_get_server_var("SERVER_NAME"))) {
1270 host
= Z_STRVAL_P(zhost
);
1277 if (url
[0] == '/') {
1278 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s%s", proto
, host
, url
);
1279 } else if (SG(request_info
).request_uri
) {
1280 path
= estrdup(SG(request_info
).request_uri
);
1281 php_dirname(path
, strlen(path
));
1282 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s%s/%s", proto
, host
, path
, url
);
1285 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s/%s", proto
, host
, url
);
1288 /* strip everything after a new line */
1291 if (*PTR
== '\n' || *PTR
== '\r') {
1298 return estrdup(URI
);
1302 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
1303 PHP_HTTP_API
char *_http_negotiate_q(const char *entry
, const zval
*supported
,
1304 const char *def TSRMLS_DC
)
1306 zval
*zaccept
, *zarray
, *zdelim
, **zentry
, *zentries
, **zsupp
;
1307 char *q_ptr
, *result
;
1311 HTTP_GSC(zaccept
, entry
, estrdup(def
));
1313 MAKE_STD_ZVAL(zarray
);
1316 MAKE_STD_ZVAL(zdelim
);
1317 ZVAL_STRING(zdelim
, ",", 0);
1318 php_explode(zdelim
, zaccept
, zarray
, -1);
1321 MAKE_STD_ZVAL(zentries
);
1322 array_init(zentries
);
1324 c
= zend_hash_num_elements(Z_ARRVAL_P(zarray
));
1325 for (i
= 0; i
< c
; i
++, zend_hash_move_forward(Z_ARRVAL_P(zarray
))) {
1327 if (SUCCESS
!= zend_hash_get_current_data(
1328 Z_ARRVAL_P(zarray
), (void **) &zentry
)) {
1329 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1330 "Cannot parse %s header: %s", entry
, Z_STRVAL_P(zaccept
));
1334 /* check for qualifier */
1335 if (NULL
!= (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';'))) {
1336 qual
= strtod(q_ptr
+ 3, NULL
);
1341 /* walk through the supported array */
1342 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported
));
1343 SUCCESS
== zend_hash_get_current_data(
1344 Z_ARRVAL_P(supported
), (void **) &zsupp
);
1345 zend_hash_move_forward(Z_ARRVAL_P(supported
))) {
1346 if (!strcasecmp(Z_STRVAL_PP(zsupp
), Z_STRVAL_PP(zentry
))) {
1347 add_assoc_double(zentries
, Z_STRVAL_PP(zsupp
), qual
);
1356 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries
));
1358 if ( (SUCCESS
!= zend_hash_sort(Z_ARRVAL_P(zentries
), zend_qsort
,
1359 http_sort_q
, 0 TSRMLS_CC
)) ||
1360 (HASH_KEY_NON_EXISTANT
== zend_hash_get_current_key(
1361 Z_ARRVAL_P(zentries
), &result
, 0, 1))) {
1362 result
= estrdup(def
);
1365 zval_dtor(zentries
);
1372 /* {{{ http_range_status http_get_request_ranges(zval *zranges, size_t) */
1373 PHP_HTTP_API http_range_status
_http_get_request_ranges(zval
*zranges
,
1374 const size_t length TSRMLS_DC
)
1378 long begin
= -1, end
= -1, *ptr
;
1380 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
1381 range
= Z_STRVAL_P(zrange
);
1383 if (strncmp(range
, "bytes=", strlen("bytes="))) {
1384 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Range header misses bytes=");
1389 range
+= strlen("bytes=");
1392 switch (c
= *(range
++))
1398 case '1': case '2': case '3':
1399 case '4': case '5': case '6':
1400 case '7': case '8': case '9':
1402 * If the value of the pointer is already set (non-negative)
1403 * then multiply its value by ten and add the current value,
1404 * else initialise the pointers value with the current value
1406 * This let us recognize empty fields when validating the
1407 * ranges, i.e. a "-10" for begin and "12345" for the end
1408 * was the following range request: "Range: bytes=0-12345";
1409 * While a "-1" for begin and "12345" for the end would
1410 * have been: "Range: bytes=-12345".
1425 /* IE - ignore for now */
1432 /* validate ranges */
1437 if ((length
- end
) < 1) {
1445 if ((length
- end
) < 1) {
1448 begin
= length
- end
;
1458 if ((length
- begin
) < 1) {
1466 if ( ((length
- begin
) < 1) ||
1467 ((length
- end
) < 1) ||
1468 ((begin
- end
) >= 0)) {
1478 MAKE_STD_ZVAL(zentry
);
1480 add_index_long(zentry
, 0, begin
);
1481 add_index_long(zentry
, 1, end
);
1482 add_next_index_zval(zranges
, zentry
);
1500 /* {{{ STATUS http_send_ranges(zval *, void *, size_t, http_send_mode) */
1501 PHP_HTTP_API STATUS
_http_send_ranges(zval
*zranges
, const void *data
, const size_t size
, const http_send_mode mode TSRMLS_DC
)
1504 long **begin
, **end
;
1507 /* Send HTTP 206 Partial Content */
1508 http_send_status(206);
1511 if ((c
= zend_hash_num_elements(Z_ARRVAL_P(zranges
))) == 1) {
1512 char range_header
[256] = {0};
1514 zend_hash_index_find(Z_ARRVAL_P(zranges
), 0, (void **) &zrange
);
1515 zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
);
1516 zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
);
1518 /* send content range header */
1519 snprintf(range_header
, 255, "Content-Range: bytes %d-%d/%d", **begin
, **end
, size
);
1520 http_send_header(range_header
);
1522 /* send requested chunk */
1523 return http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1529 char bound
[23] = {0}, preface
[1024] = {0},
1530 multi_header
[68] = "Content-Type: multipart/byteranges; boundary=";
1532 snprintf(bound
, 22, "--%d%0.9f", time(NULL
), php_combined_lcg(TSRMLS_C
));
1533 strncat(multi_header
, bound
+ 2, 21);
1534 http_send_header(multi_header
);
1536 /* send each requested chunk */
1537 for ( i
= 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(zranges
));
1539 i
++, zend_hash_move_forward(Z_ARRVAL_P(zranges
))) {
1540 if ( HASH_KEY_NON_EXISTANT
== zend_hash_get_current_data(
1541 Z_ARRVAL_P(zranges
), (void **) &zrange
) ||
1542 SUCCESS
!= zend_hash_index_find(
1543 Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1544 SUCCESS
!= zend_hash_index_find(
1545 Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1549 snprintf(preface
, 1023,
1551 HTTP_CRLF
"Content-Type: %s"
1552 HTTP_CRLF
"Content-Range: bytes %ld-%ld/%ld"
1557 HTTP_G(ctype
) ? HTTP_G(ctype
) : "application/x-octetstream",
1563 php_body_write(preface
, strlen(preface
) TSRMLS_CC
);
1564 http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1567 /* write boundary once more */
1568 php_body_write(HTTP_CRLF
, 2 TSRMLS_CC
);
1569 php_body_write(bound
, strlen(bound
) TSRMLS_CC
);
1576 /* {{{ STATUS http_send(void *, sizezo_t, http_send_mode) */
1577 PHP_HTTP_API STATUS
_http_send(const void *data_ptr
, const size_t data_size
,
1578 const http_send_mode data_mode TSRMLS_DC
)
1580 int is_range_request
= http_is_range_request();
1587 if (HTTP_G(etag_started
)) {
1588 char *etag
= ecalloc(33, 1);
1590 HTTP_G(etag_started
) = 0;
1591 /* never ever use the output to compute the ETag if http_send() is used */
1592 php_end_ob_buffer(0, 0 TSRMLS_CC
);
1593 if (NULL
== http_etag(&etag
, data_ptr
, data_size
, data_mode
)) {
1598 /* send 304 Not Modified if etag matches */
1599 if ((!is_range_request
) && http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
1601 return http_send_status(304);
1604 http_send_etag(etag
, 32);
1608 /* send 304 Not Modified if last-modified matches*/
1609 if ((!is_range_request
) && http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod
))) {
1610 return http_send_status(304);
1613 if (is_range_request
) {
1615 /* only send ranges if entity hasn't changed */
1617 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_MATCH", 13)) ||
1618 http_etag_match("HTTP_IF_MATCH", HTTP_G(etag
)))
1620 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
1621 http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod
)))
1624 STATUS result
= FAILURE
;
1625 zval
*zranges
= NULL
;
1626 MAKE_STD_ZVAL(zranges
);
1627 array_init(zranges
);
1629 switch (http_get_request_ranges(zranges
, data_size
))
1634 /* go ahead and send all */
1638 result
= http_send_ranges(zranges
, data_ptr
, data_size
, data_mode
);
1647 http_send_status(416);
1658 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
1662 /* {{{ STATUS http_send_data(zval *) */
1663 PHP_HTTP_API STATUS
_http_send_data(const zval
*zdata TSRMLS_DC
)
1665 if (!Z_STRLEN_P(zdata
)) {
1668 if (!Z_STRVAL_P(zdata
)) {
1672 return http_send(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
), SEND_DATA
);
1676 /* {{{ STATUS http_send_stream(php_stream *) */
1677 PHP_HTTP_API STATUS
_http_send_stream(const php_stream
*file TSRMLS_DC
)
1679 if (php_stream_stat((php_stream
*) file
, &HTTP_G(ssb
))) {
1683 return http_send(file
, HTTP_G(ssb
).sb
.st_size
, SEND_RSRC
);
1687 /* {{{ STATUS http_send_file(zval *) */
1688 PHP_HTTP_API STATUS
_http_send_file(const zval
*zfile TSRMLS_DC
)
1693 if (!Z_STRLEN_P(zfile
)) {
1697 if (!(file
= php_stream_open_wrapper(Z_STRVAL_P(zfile
), "rb",
1698 REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
))) {
1702 ret
= http_send_stream(file
);
1703 php_stream_close(file
);
1708 /* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1709 PHP_HTTP_API STATUS
_http_chunked_decode(const char *encoded
,
1710 const size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
1716 *decoded
= (char *) ecalloc(encoded_len
, 1);
1720 while (((e_ptr
- encoded
) - encoded_len
) > 0) {
1721 char hex_len
[9] = {0};
1722 size_t chunk_len
= 0;
1725 /* read in chunk size */
1726 while (isxdigit(*e_ptr
)) {
1728 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1729 "Chunk size is too long: 0x%s...", hex_len
);
1733 hex_len
[i
++] = *e_ptr
++;
1736 /* reached the end */
1737 if (!strcmp(hex_len
, "0")) {
1742 if (strncmp(e_ptr
, HTTP_CRLF
, 2)) {
1743 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1744 "Invalid character (expected 0x0D 0x0A; got: %x %x)",
1745 *e_ptr
, *(e_ptr
+ 1));
1753 chunk_len
= strtol(hex_len
, &error
, 16);
1754 if (error
== hex_len
) {
1755 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1756 "Invalid chunk size string: '%s'", hex_len
);
1762 memcpy(d_ptr
, e_ptr
+= 2, chunk_len
);
1764 e_ptr
+= chunk_len
+ 2;
1765 *decoded_len
+= chunk_len
;
1772 /* {{{ proto STATUS http_split_response(zval *, zval *, zval *) */
1773 PHP_HTTP_API STATUS
_http_split_response(const zval
*zresponse
, zval
*zheaders
,
1774 zval
*zbody TSRMLS_DC
)
1776 char *header
, *response
, *body
= NULL
;
1777 long response_len
= Z_STRLEN_P(zresponse
);
1778 header
= response
= Z_STRVAL_P(zresponse
);
1780 while ((response
- Z_STRVAL_P(zresponse
) + 3) < response_len
) {
1781 if ( (*response
++ == '\r') &&
1782 (*response
++ == '\n') &&
1783 (*response
++ == '\r') &&
1784 (*response
++ == '\n')) {
1790 if (body
&& (response_len
- (body
- header
))) {
1791 ZVAL_STRINGL(zbody
, body
, response_len
- (body
- header
) - 1, 1);
1793 Z_TYPE_P(zbody
) = IS_NULL
;
1796 return http_parse_headers(header
, body
- Z_STRVAL_P(zresponse
), zheaders
);
1800 /* {{{ STATUS http_parse_headers(char *, long, zval *) */
1801 PHP_HTTP_API STATUS
_http_parse_headers(char *header
, int header_len
, zval
*array TSRMLS_DC
)
1803 char *colon
= NULL
, *line
= NULL
, *begin
= header
;
1805 if (header_len
< 2) {
1810 if (!strncmp(header
, "HTTP/1.", 7)) {
1811 char *end
= strstr(header
, HTTP_CRLF
);
1812 add_assoc_stringl(array
, "Status",
1813 header
+ strlen("HTTP/1.x "),
1814 end
- (header
+ strlen("HTTP/1.x ")), 1);
1820 while (header_len
>= (line
- begin
)) {
1826 --value_len
; /* we don't have CR so value length is one char less */
1828 if (colon
&& ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t')))) {
1830 /* skip empty key */
1831 if (header
!= colon
) {
1832 char *key
= estrndup(header
, colon
- header
);
1833 value_len
+= line
- colon
- 1;
1835 /* skip leading ws */
1836 while (isspace(*(++colon
))) --value_len
;
1837 /* skip trailing ws */
1838 while (isspace(colon
[value_len
- 1])) --value_len
;
1840 if (value_len
< 1) {
1841 /* hm, empty header? */
1842 add_assoc_stringl(array
, key
, "", 0, 1);
1844 add_assoc_stringl(array
, key
, colon
, value_len
, 1);
1851 header
+= line
- header
;
1866 /* {{{ void http_get_request_headers(zval *) */
1867 PHP_HTTP_API
void _http_get_request_headers(zval
*array TSRMLS_DC
)
1871 for ( zend_hash_internal_pointer_reset(HTTP_SERVER_VARS
);
1872 zend_hash_get_current_key(HTTP_SERVER_VARS
, &key
, NULL
, 0) != HASH_KEY_NON_EXISTANT
;
1873 zend_hash_move_forward(HTTP_SERVER_VARS
)) {
1874 if (!strncmp(key
, "HTTP_", 5)) {
1876 zend_hash_get_current_data(HTTP_SERVER_VARS
, (void **) &header
);
1877 add_assoc_stringl(array
, pretty_key(key
+ 5, strlen(key
) - 5, 1, 1), Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1884 #ifdef HTTP_HAVE_CURL
1886 /* {{{ STATUS http_get(char *, HashTable *, HashTable *, char **, size_t *) */
1887 PHP_HTTP_API STATUS
_http_get(const char *URL
, HashTable
*options
,
1888 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
1890 CURL
*ch
= curl_easy_init();
1893 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
1897 http_curl_initbuf(CURLBUF_EVRY
);
1898 http_curl_setopts(ch
, URL
, options
);
1900 if (CURLE_OK
!= curl_easy_perform(ch
)) {
1901 curl_easy_cleanup(ch
);
1902 http_curl_freebuf(CURLBUF_EVRY
);
1903 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
1907 http_curl_getinfo(ch
, info
);
1909 curl_easy_cleanup(ch
);
1911 http_curl_movebuf(CURLBUF_EVRY
, data
, data_len
);
1917 /* {{{ STATUS http_head(char *, HashTable *, HashTable *, char **data, size_t *) */
1918 PHP_HTTP_API STATUS
_http_head(const char *URL
, HashTable
*options
,
1919 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
1921 CURL
*ch
= curl_easy_init();
1924 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
1928 http_curl_initbuf(CURLBUF_HDRS
);
1929 http_curl_setopts(ch
, URL
, options
);
1930 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
1932 if (CURLE_OK
!= curl_easy_perform(ch
)) {
1933 curl_easy_cleanup(ch
);
1934 http_curl_freebuf(CURLBUF_HDRS
);
1935 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
1939 http_curl_getinfo(ch
, info
);
1941 curl_easy_cleanup(ch
);
1943 http_curl_movebuf(CURLBUF_HDRS
, data
, data_len
);
1949 /* {{{ STATUS http_post_data(char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
1950 PHP_HTTP_API STATUS
_http_post_data(const char *URL
, char *postdata
,
1951 size_t postdata_len
, HashTable
*options
, HashTable
*info
, char **data
,
1952 size_t *data_len TSRMLS_DC
)
1954 CURL
*ch
= curl_easy_init();
1957 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
1961 http_curl_initbuf(CURLBUF_EVRY
);
1962 http_curl_setopts(ch
, URL
, options
);
1963 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
1964 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, postdata
);
1965 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, postdata_len
);
1967 if (CURLE_OK
!= curl_easy_perform(ch
)) {
1968 curl_easy_cleanup(ch
);
1969 http_curl_freebuf(CURLBUF_EVRY
);
1970 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
1974 http_curl_getinfo(ch
, info
);
1976 curl_easy_cleanup(ch
);
1978 http_curl_movebuf(CURLBUF_EVRY
, data
, data_len
);
1984 /* {{{ STATUS http_post_array(char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
1985 PHP_HTTP_API STATUS
_http_post_array(const char *URL
, HashTable
*postarray
,
1986 HashTable
*options
, HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
1988 smart_str qstr
= {0};
1991 if (php_url_encode_hash_ex(postarray
, &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
1995 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode post data");
2000 status
= http_post_data(URL
, qstr
.c
, qstr
.len
, options
, info
, data
, data_len
);
2011 /* {{{ STATUS http_auth_header(char *, char*) */
2012 PHP_HTTP_API STATUS
_http_auth_header(const char *type
, const char *realm TSRMLS_DC
)
2014 char realm_header
[1024] = {0};
2015 snprintf(realm_header
, 1023, "WWW-Authenticate: %s realm=\"%s\"", type
, realm
);
2016 return http_send_status_header(401, realm_header
);
2020 /* {{{ STATUS http_auth_credentials(char **, char **) */
2021 PHP_HTTP_API STATUS
_http_auth_credentials(char **user
, char **pass TSRMLS_DC
)
2023 if (strncmp(sapi_module
.name
, "isapi", 5)) {
2024 zval
*zuser
, *zpass
;
2026 HTTP_GSC(zuser
, "PHP_AUTH_USER", FAILURE
);
2027 HTTP_GSC(zpass
, "PHP_AUTH_PW", FAILURE
);
2029 *user
= estrndup(Z_STRVAL_P(zuser
), Z_STRLEN_P(zuser
));
2030 *pass
= estrndup(Z_STRVAL_P(zpass
), Z_STRLEN_P(zpass
));
2035 HTTP_GSC(zauth
, "HTTP_AUTHORIZATION", FAILURE
);
2037 char *decoded
, *colon
;
2039 decoded
= php_base64_decode(Z_STRVAL_P(zauth
), Z_STRLEN_P(zauth
),
2042 if (colon
= strchr(decoded
+ 6, ':')) {
2043 *user
= estrndup(decoded
+ 6, colon
- decoded
- 6);
2044 *pass
= estrndup(colon
+ 1, decoded
+ decoded_len
- colon
- 6 - 1);
2055 /* }}} public API */
2062 * vim600: noet sw=4 ts=4 fdm=marker
2063 * vim<600: noet sw=4 ts=4