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 +----------------------------------------------------------------------+
18 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
27 #include "php_version.h"
28 #include "php_streams.h"
30 #include "ext/standard/md5.h"
31 #include "ext/standard/url.h"
32 #include "ext/standard/base64.h"
33 #include "ext/standard/php_string.h"
34 #include "ext/standard/php_smart_str.h"
35 #include "ext/standard/php_lcg.h"
39 #if (PHP_MAJOR_VERSION >= 5)
40 #include "ext/standard/php_http.h"
42 #include "php_http_build_query.h"
43 #include "http_build_query.c"
47 #include "php_http_api.h"
53 #include <sys/types.h>
56 #include <curl/curl.h>
57 #include <curl/easy.h>
61 ZEND_DECLARE_MODULE_GLOBALS(http
)
63 /* {{{ day/month names */
64 static const char *days
[] = {
65 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
67 static const char *wkdays
[] = {
68 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
70 static const char *weekdays
[] = {
71 "Monday", "Tuesday", "Wednesday",
72 "Thursday", "Friday", "Saturday", "Sunday"
74 static const char *months
[] = {
75 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
76 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
83 static const struct time_zone
{
87 {"GMT", 0}, /* Greenwich Mean */
88 {"UTC", 0}, /* Universal (Coordinated) */
89 {"WET", 0}, /* Western European */
90 {"BST", 0}, /* British Summer */
91 {"WAT", 60}, /* West Africa */
92 {"AST", 240}, /* Atlantic Standard */
93 {"ADT", 240}, /* Atlantic Daylight */
94 {"EST", 300}, /* Eastern Standard */
95 {"EDT", 300}, /* Eastern Daylight */
96 {"CST", 360}, /* Central Standard */
97 {"CDT", 360}, /* Central Daylight */
98 {"MST", 420}, /* Mountain Standard */
99 {"MDT", 420}, /* Mountain Daylight */
100 {"PST", 480}, /* Pacific Standard */
101 {"PDT", 480}, /* Pacific Daylight */
102 {"YST", 540}, /* Yukon Standard */
103 {"YDT", 540}, /* Yukon Daylight */
104 {"HST", 600}, /* Hawaii Standard */
105 {"HDT", 600}, /* Hawaii Daylight */
106 {"CAT", 600}, /* Central Alaska */
107 {"AHST", 600}, /* Alaska-Hawaii Standard */
108 {"NT", 660}, /* Nome */
109 {"IDLW", 720}, /* International Date Line West */
110 {"CET", -60}, /* Central European */
111 {"MET", -60}, /* Middle European */
112 {"MEWT", -60}, /* Middle European Winter */
113 {"MEST", -120}, /* Middle European Summer */
114 {"CEST", -120}, /* Central European Summer */
115 {"MESZ", -60}, /* Middle European Summer */
116 {"FWT", -60}, /* French Winter */
117 {"FST", -60}, /* French Summer */
118 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
119 {"WAST", -420}, /* West Australian Standard */
120 {"WADT", -420}, /* West Australian Daylight */
121 {"CCT", -480}, /* China Coast, USSR Zone 7 */
122 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
123 {"EAST", -600}, /* Eastern Australian Standard */
124 {"EADT", -600}, /* Eastern Australian Daylight */
125 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
126 {"NZT", -720}, /* New Zealand */
127 {"NZST", -720}, /* New Zealand Standard */
128 {"NZDT", -720}, /* New Zealand Daylight */
129 {"IDLE", -720}, /* International Date Line East */
135 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
);
136 #define http_etag(e, p, l, m) _http_etag((e), (p), (l), (m) TSRMLS_CC)
137 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
);
138 #define http_is_range_request() _http_is_range_request(TSRMLS_C)
139 static inline int _http_is_range_request(TSRMLS_D
);
140 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
141 static STATUS
_http_send_chunk(const void *data
, const size_t begin
, const size_t end
, const http_send_mode mode TSRMLS_DC
);
143 static int check_day(char *day
, size_t len
);
144 static int check_month(char *month
);
145 static int check_tzone(char *tzone
);
148 #ifdef HTTP_HAVE_CURL
149 #define http_curl_initbuf(m) _http_curl_initbuf((m) TSRMLS_CC)
150 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC
);
151 #define http_curl_freebuf(m) _http_curl_freebuf((m) TSRMLS_CC)
152 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC
);
153 #define http_curl_sizebuf(m, l) _http_curl_sizebuf((m), (l) TSRMLS_CC)
154 static inline void _http_curl_sizebuf(http_curlbuf_member member
, size_t len TSRMLS_DC
);
155 #define http_curl_movebuf(m, d, l) _http_curl_movebuf((m), (d), (l) TSRMLS_CC)
156 static inline void _http_curl_movebuf(http_curlbuf_member member
, char **data
, size_t *data_len TSRMLS_DC
);
157 #define http_curl_copybuf(m, d, l) _http_curl_copybuf((m), (d), (l) TSRMLS_CC)
158 static inline void _http_curl_copybuf(http_curlbuf_member member
, char **data
, size_t *data_len TSRMLS_DC
);
159 #define http_curl_setopts(c, u, o) _http_curl_setopts((c), (u), (o) TSRMLS_CC)
160 static inline void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
);
162 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
163 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
164 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
165 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...);
167 static size_t http_curl_body_callback(char *, size_t, size_t, void *);
168 static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *);
170 #define http_curl_getinfo(c, h) _http_curl_getinfo((c), (h) TSRMLS_CC)
171 static inline void _http_curl_getinfo(CURL
*ch
, HashTable
*info TSRMLS_DC
);
172 #define http_curl_getinfo_ex(c, i, a) _http_curl_getinfo_ex((c), (i), (a) TSRMLS_CC)
173 static inline void _http_curl_getinfo_ex(CURL
*ch
, CURLINFO i
, zval
*array TSRMLS_DC
);
174 #define http_curl_getinfoname(i) _http_curl_getinfoname((i) TSRMLS_CC)
175 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC
);
180 /* {{{ static int http_sort_q(const void *, const void *) */
181 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
184 zval result
, *first
, *second
;
186 f
= *((Bucket
**) a
);
187 s
= *((Bucket
**) b
);
189 first
= *((zval
**) f
->pData
);
190 second
= *((zval
**) s
->pData
);
192 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
195 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
199 /* {{{ static inline char *http_etag(char **, void *, size_t, http_send_mode) */
200 static inline char *_http_etag(char **new_etag
, const void *data_ptr
,
201 const size_t data_len
, const http_send_mode data_mode TSRMLS_DC
)
203 char ssb_buf
[127], digest
[16];
211 PHP_MD5Update(&ctx
, data_ptr
, data_len
);
215 snprintf(ssb_buf
, 127, "%l=%l=%l",
216 HTTP_G(ssb
).sb
.st_mtime
,
217 HTTP_G(ssb
).sb
.st_ino
,
218 HTTP_G(ssb
).sb
.st_size
220 PHP_MD5Update(&ctx
, ssb_buf
, strlen(ssb_buf
));
228 PHP_MD5Final(digest
, &ctx
);
229 make_digest(*new_etag
, digest
);
235 /* {{{ static inline int http_is_range_request(void) */
236 static inline int _http_is_range_request(TSRMLS_D
)
238 return zend_hash_exists(Z_ARRVAL_P(PG(http_globals
)[TRACK_VARS_SERVER
]),
239 "HTTP_RANGE", strlen("HTTP_RANGE") + 1);
243 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
245 static STATUS
_http_send_chunk(const void *data
, const size_t begin
,
246 const size_t end
, const http_send_mode mode TSRMLS_DC
)
250 long len
= end
- begin
;
256 s
= (php_stream
*) data
;
257 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
260 buf
= (char *) ecalloc(1, HTTP_BUF_SIZE
);
261 /* read into buf and write out */
262 while ((len
-= HTTP_BUF_SIZE
) >= 0) {
263 if (!(read
= php_stream_read(s
, buf
, HTTP_BUF_SIZE
))) {
267 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
273 /* read & write left over */
275 if (read
= php_stream_read(s
, buf
, HTTP_BUF_SIZE
+ len
)) {
276 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
290 return len
== php_body_write(
291 Z_STRVAL_P((zval
*) data
) + begin
, len TSRMLS_CC
)
303 #ifdef HTTP_HAVE_CURL
305 /* {{{ static inline void http_curl_initbuf(http_curlbuf_member) */
306 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC
)
308 http_curl_freebuf(member
);
310 if (member
& CURLBUF_HDRS
) {
311 HTTP_G(curlbuf
).hdrs
.data
= emalloc(HTTP_CURLBUF_HDRSSIZE
);
312 HTTP_G(curlbuf
).hdrs
.free
= HTTP_CURLBUF_HDRSSIZE
;
314 if (member
& CURLBUF_BODY
) {
315 HTTP_G(curlbuf
).body
.data
= emalloc(HTTP_CURLBUF_BODYSIZE
);
316 HTTP_G(curlbuf
).body
.free
= HTTP_CURLBUF_BODYSIZE
;
321 /* {{{ static inline void http_curl_freebuf(http_curlbuf_member) */
322 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC
)
324 if (member
& CURLBUF_HDRS
) {
325 if (HTTP_G(curlbuf
).hdrs
.data
) {
326 efree(HTTP_G(curlbuf
).hdrs
.data
);
327 HTTP_G(curlbuf
).hdrs
.data
= NULL
;
329 HTTP_G(curlbuf
).hdrs
.used
= 0;
330 HTTP_G(curlbuf
).hdrs
.free
= 0;
332 if (member
& CURLBUF_BODY
) {
333 if (HTTP_G(curlbuf
).body
.data
) {
334 efree(HTTP_G(curlbuf
).body
.data
);
335 HTTP_G(curlbuf
).body
.data
= NULL
;
337 HTTP_G(curlbuf
).body
.used
= 0;
338 HTTP_G(curlbuf
).body
.free
= 0;
343 /* {{{ static inline void http_curl_copybuf(http_curlbuf_member, char **,
345 static inline void _http_curl_copybuf(http_curlbuf_member member
, char **data
,
346 size_t *data_len TSRMLS_DC
)
351 if ((member
& CURLBUF_HDRS
) && HTTP_G(curlbuf
).hdrs
.used
) {
352 if ((member
& CURLBUF_BODY
) && HTTP_G(curlbuf
).body
.used
) {
353 *data
= emalloc(HTTP_G(curlbuf
).hdrs
.used
+ HTTP_G(curlbuf
).body
.used
+ 1);
355 *data
= emalloc(HTTP_G(curlbuf
).hdrs
.used
+ 1);
357 memcpy(*data
, HTTP_G(curlbuf
).hdrs
.data
, HTTP_G(curlbuf
).hdrs
.used
);
358 *data_len
= HTTP_G(curlbuf
).hdrs
.used
;
361 if ((member
& CURLBUF_BODY
) && HTTP_G(curlbuf
).body
.used
) {
363 memcpy((*data
) + HTTP_G(curlbuf
).hdrs
.used
,
364 HTTP_G(curlbuf
).body
.data
, HTTP_G(curlbuf
).body
.used
);
365 *data_len
= HTTP_G(curlbuf
).hdrs
.used
+ HTTP_G(curlbuf
).body
.used
;
367 emalloc(HTTP_G(curlbuf
).body
.used
+ 1);
368 memcpy(*data
, HTTP_G(curlbuf
).body
.data
, HTTP_G(curlbuf
).body
.used
);
369 *data_len
= HTTP_G(curlbuf
).body
.used
;
373 (*data
)[*data_len
] = 0;
380 /* {{{ static inline void http_curl_movebuf(http_curlbuf_member, char **,
382 static inline void _http_curl_movebuf(http_curlbuf_member member
, char **data
,
383 size_t *data_len TSRMLS_DC
)
385 http_curl_copybuf(member
, data
, data_len
);
386 http_curl_freebuf(member
);
390 /* {{{ static size_t http_curl_body_callback(char *, size_t, size_t, void *) */
391 static size_t http_curl_body_callback(char *buf
, size_t len
, size_t n
, void *s
)
395 if ((len
*= n
) > HTTP_G(curlbuf
).body
.free
) {
396 size_t bsize
= HTTP_CURLBUF_BODYSIZE
;
397 while (bsize
< len
) {
400 HTTP_G(curlbuf
).body
.data
= erealloc(HTTP_G(curlbuf
).body
.data
,
401 HTTP_G(curlbuf
).body
.used
+ bsize
);
402 HTTP_G(curlbuf
).body
.free
+= bsize
;
405 memcpy(HTTP_G(curlbuf
).body
.data
+ HTTP_G(curlbuf
).body
.used
, buf
, len
);
406 HTTP_G(curlbuf
).body
.free
-= len
;
407 HTTP_G(curlbuf
).body
.used
+= len
;
413 /* {{{ static size_t http_curl_hdrs_callback(char*, size_t, size_t, void *) */
414 static size_t http_curl_hdrs_callback(char *buf
, size_t len
, size_t n
, void *s
)
418 /* discard previous headers */
419 if ((HTTP_G(curlbuf
).hdrs
.used
) && (!strncmp(buf
, "HTTP/1.", strlen("HTTP/1.")))) {
420 http_curl_initbuf(CURLBUF_HDRS
);
423 if ((len
*= n
) > HTTP_G(curlbuf
).hdrs
.free
) {
424 size_t bsize
= HTTP_CURLBUF_HDRSSIZE
;
425 while (bsize
< len
) {
428 HTTP_G(curlbuf
).hdrs
.data
= erealloc(HTTP_G(curlbuf
).hdrs
.data
,
429 HTTP_G(curlbuf
).hdrs
.used
+ bsize
);
430 HTTP_G(curlbuf
).hdrs
.free
+= bsize
;
433 memcpy(HTTP_G(curlbuf
).hdrs
.data
+ HTTP_G(curlbuf
).hdrs
.used
, buf
, len
);
434 HTTP_G(curlbuf
).hdrs
.free
-= len
;
435 HTTP_G(curlbuf
).hdrs
.used
+= len
;
441 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
442 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...)
448 if (SUCCESS
!= zend_hash_find(options
, key
, strlen(key
) + 1, (void **) &zoption
)) {
455 va_start(types
, checks
);
456 for (i
= 0; i
< checks
; ++i
) {
457 if ((va_arg(types
, int)) == (Z_TYPE_PP(zoption
))) {
467 /* {{{ static inline void http_curl_setopts(CURL *, char *, HashTable *) */
468 static inline void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
)
472 /* standard options */
473 curl_easy_setopt(ch
, CURLOPT_URL
, url
);
474 curl_easy_setopt(ch
, CURLOPT_HEADER
, 0);
475 curl_easy_setopt(ch
, CURLOPT_NOPROGRESS
, 1);
476 curl_easy_setopt(ch
, CURLOPT_AUTOREFERER
, 1);
477 curl_easy_setopt(ch
, CURLOPT_WRITEFUNCTION
, http_curl_body_callback
);
478 curl_easy_setopt(ch
, CURLOPT_HEADERFUNCTION
, http_curl_hdrs_callback
);
480 curl_easy_setopt(ch
, CURLOPT_NOSIGNAL
, 1);
483 if ((!options
) || (1 > zend_hash_num_elements(options
))) {
487 /* redirects, defaults to 0 */
488 if (zoption
= http_curl_getopt1(options
, "redirect", IS_LONG
)) {
489 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
490 curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, Z_LVAL_P(zoption
));
491 if (zoption
= http_curl_getopt2(options
, "unrestrictedauth", IS_LONG
, IS_BOOL
)) {
492 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
495 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, 0);
499 if (zoption
= http_curl_getopt1(options
, "proxyhost", IS_STRING
)) {
500 curl_easy_setopt(ch
, CURLOPT_PROXY
, Z_STRVAL_P(zoption
));
502 if (zoption
= http_curl_getopt1(options
, "proxyport", IS_LONG
)) {
503 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, Z_LVAL_P(zoption
));
506 if (zoption
= http_curl_getopt1(options
, "proxyauth", IS_STRING
)) {
507 curl_easy_setopt(ch
, CURLOPT_PROXYUSERPWD
, Z_STRVAL_P(zoption
));
510 if (zoption
= http_curl_getopt1(options
, "proxyauthtype", IS_LONG
)) {
511 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, Z_LVAL_P(zoption
));
516 if (zoption
= http_curl_getopt1(options
, "httpauth", IS_STRING
)) {
517 curl_easy_setopt(ch
, CURLOPT_USERPWD
, Z_STRVAL_P(zoption
));
519 if (zoption
= http_curl_getopt1(options
, "httpauthtype", IS_LONG
)) {
520 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, Z_LVAL_P(zoption
));
523 /* compress, enabled by default (empty string enables deflate and gzip) */
524 if (zoption
= http_curl_getopt2(options
, "compress", IS_LONG
, IS_BOOL
)) {
525 if (Z_LVAL_P(zoption
)) {
526 curl_easy_setopt(ch
, CURLOPT_ENCODING
, "");
529 curl_easy_setopt(ch
, CURLOPT_ENCODING
, "");
533 if (zoption
= http_curl_getopt1(options
, "port", IS_LONG
)) {
534 curl_easy_setopt(ch
, CURLOPT_PORT
, Z_LVAL_P(zoption
));
538 if (zoption
= http_curl_getopt1(options
, "referer", IS_STRING
)) {
539 curl_easy_setopt(ch
, CURLOPT_REFERER
, Z_STRVAL_P(zoption
));
542 /* useragent, default "PECL::HTTP/version (PHP/version)" */
543 if (zoption
= http_curl_getopt1(options
, "useragent", IS_STRING
)) {
544 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, Z_STRVAL_P(zoption
));
546 curl_easy_setopt(ch
, CURLOPT_USERAGENT
,
547 "PECL::HTTP/" PHP_EXT_HTTP_VERSION
" (PHP/" PHP_VERSION
")");
550 /* cookies, array('name' => 'value') */
551 if (zoption
= http_curl_getopt1(options
, "cookies", IS_ARRAY
)) {
555 smart_str qstr
= {0};
557 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption
));
558 while (HASH_KEY_NON_EXISTANT
!= (key_type
= zend_hash_get_current_key_type(Z_ARRVAL_P(zoption
)))) {
559 if (key_type
== HASH_KEY_IS_STRING
) {
560 zend_hash_get_current_key(Z_ARRVAL_P(zoption
), &cookie_key
, NULL
, 0);
561 zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
);
562 smart_str_appends(&qstr
, cookie_key
);
563 smart_str_appendl(&qstr
, "=", 1);
564 smart_str_appendl(&qstr
, Z_STRVAL_PP(cookie_val
), Z_STRLEN_PP(cookie_val
));
565 smart_str_appendl(&qstr
, "; ", 2);
566 zend_hash_move_forward(Z_ARRVAL_P(zoption
));
572 curl_easy_setopt(ch
, CURLOPT_COOKIE
, qstr
.c
);
577 if (zoption
= http_curl_getopt1(options
, "cookiestore", IS_STRING
)) {
578 curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, Z_STRVAL_P(zoption
));
579 curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, Z_STRVAL_P(zoption
));
582 /* additional headers, array('name' => 'value') */
583 if (zoption
= http_curl_getopt1(options
, "headers", IS_ARRAY
)) {
585 char *header_key
, header
[1024] = {0};
587 struct curl_slist
*headers
= NULL
;
589 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption
));
590 while (HASH_KEY_NON_EXISTANT
!= (key_type
= zend_hash_get_current_key_type(Z_ARRVAL_P(zoption
)))) {
591 if (key_type
== HASH_KEY_IS_STRING
) {
592 zend_hash_get_current_key(Z_ARRVAL_P(zoption
), &header_key
, NULL
, 0);
593 zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
);
594 snprintf(header
, 1024, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
595 headers
= curl_slist_append(headers
, header
);
596 zend_hash_move_forward(Z_ARRVAL_P(zoption
));
600 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, headers
);
606 /* {{{ static inline char *http_curl_getinfoname(CURLINFO) */
607 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC
)
609 #define CASE(I) case CURLINFO_ ##I : return #I
612 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
614 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
616 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
618 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
619 CASE(NAMELOOKUP_TIME
);
620 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
622 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
623 CASE(PRETRANSFER_TIME
);
624 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
626 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
628 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
629 CASE(SPEED_DOWNLOAD
);
630 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
632 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
634 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
636 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
637 CASE(SSL_VERIFYRESULT
);
638 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
640 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
641 CASE(CONTENT_LENGTH_DOWNLOAD
);
642 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
643 CASE(CONTENT_LENGTH_UPLOAD
);
644 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
645 CASE(STARTTRANSFER_TIME
);
646 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
648 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
650 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
651 CASE(REDIRECT_COUNT
);
652 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
654 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
655 CASE(HTTP_CONNECTCODE
);
656 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
657 CASE(HTTPAUTH_AVAIL
);
658 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
659 CASE(PROXYAUTH_AVAIL
);
666 /* {{{ static inline void http_curl_getinfo_ex(CURL, CURLINFO, zval *) */
667 static inline void _http_curl_getinfo_ex(CURL
*ch
, CURLINFO i
, zval
*array TSRMLS_DC
)
670 if (key
= http_curl_getinfoname(i
)) {
671 switch (i
& ~CURLINFO_MASK
)
673 case CURLINFO_STRING
:
676 if (CURLE_OK
== curl_easy_getinfo(ch
, i
, &c
)) {
677 add_assoc_string(array
, key
, c
? c
: "", 1);
682 case CURLINFO_DOUBLE
:
685 if (CURLE_OK
== curl_easy_getinfo(ch
, i
, &d
)) {
686 add_assoc_double(array
, key
, (double) d
);
694 if (CURLE_OK
== curl_easy_getinfo(ch
, i
, &l
)) {
695 add_assoc_long(array
, key
, (long) l
);
704 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
705 static inline void _http_curl_getinfo(CURL
*ch
, HashTable
*info TSRMLS_DC
)
709 MAKE_STD_ZVAL(array
);
710 Z_ARRVAL_P(array
) = info
;
712 #define INFO(I) http_curl_getinfo_ex(ch, CURLINFO_ ##I , array)
713 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
715 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
717 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
719 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
720 INFO(NAMELOOKUP_TIME
);
721 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
723 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
724 INFO(PRETRANSFER_TIME
);
725 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
727 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
729 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
730 INFO(SPEED_DOWNLOAD
);
731 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
733 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
735 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
737 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
738 INFO(SSL_VERIFYRESULT
);
739 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
741 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
742 INFO(CONTENT_LENGTH_DOWNLOAD
);
743 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
744 INFO(CONTENT_LENGTH_UPLOAD
);
745 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
746 INFO(STARTTRANSFER_TIME
);
747 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
749 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
751 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
752 INFO(REDIRECT_COUNT
);
753 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
755 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
756 INFO(HTTP_CONNECTCODE
);
757 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
758 INFO(HTTPAUTH_AVAIL
);
759 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
760 INFO(PROXYAUTH_AVAIL
);
766 /* {{{ Day/Month/TZ checks for http_parse_date()
767 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
768 static int check_day(char *day
, size_t len
)
771 const char * const *check
= (len
> 3) ? &weekdays
[0] : &wkdays
[0];
772 for (i
= 0; i
< 7; i
++) {
773 if (!strcmp(day
, check
[0])) {
781 static int check_month(char *month
)
784 const char * const *check
= &months
[0];
785 for (i
= 0; i
< 12; i
++) {
786 if (!strcmp(month
, check
[0])) {
794 /* return the time zone offset between GMT and the input one, in number
795 of seconds or -1 if the timezone wasn't found/legal */
797 static int check_tzone(char *tzone
)
800 const struct time_zone
*check
= time_zones
;
801 for (i
= 0; i
< sizeof(time_zones
) / sizeof(time_zones
[0]); i
++) {
802 if (!strcmp(tzone
, check
->name
)) {
803 return check
->offset
* 60;
818 /* {{{ char *http_date(time_t) */
819 PHP_HTTP_API
char *_http_date(time_t t TSRMLS_DC
)
821 struct tm
*gmtime
, tmbuf
;
822 char *date
= ecalloc(1, 30);
824 gmtime
= php_gmtime_r(&t
, &tmbuf
);
826 "%s, %02d %s %04d %02d:%02d:%02d GMT",
827 days
[gmtime
->tm_wday
], gmtime
->tm_mday
,
828 months
[gmtime
->tm_mon
], gmtime
->tm_year
+ 1900,
829 gmtime
->tm_hour
, gmtime
->tm_min
, gmtime
->tm_sec
835 /* {{{ time_t http_parse_date(char *)
836 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
837 PHP_HTTP_API
time_t _http_parse_date(const char *date
)
840 int tz_offset
= -1, year
= -1, month
= -1, monthday
= -1, weekday
= -1,
841 hours
= -1, minutes
= -1, seconds
= -1;
843 enum assume_next dignext
= DATE_MDAY
;
844 const char *indate
= date
;
846 int found
= 0, part
= 0; /* max 6 parts */
848 while (*date
&& (part
< 6)) {
851 while (*date
&& !isalnum(*date
)) {
855 if (isalpha(*date
)) {
856 /* a name coming up */
859 sscanf(date
, "%31[A-Za-z]", buf
);
863 weekday
= check_day(buf
, len
);
869 if (!found
&& (month
== -1)) {
870 month
= check_month(buf
);
876 if (!found
&& (tz_offset
== -1)) {
877 /* this just must be a time zone string */
878 tz_offset
= check_tzone(buf
);
879 if (tz_offset
!= -1) {
885 return -1; /* bad string */
889 else if (isdigit(*date
)) {
893 if((seconds
== -1) && (3 == sscanf(date
, "%02d:%02d:%02d", &hours
, &minutes
, &seconds
))) {
899 val
= (int) strtol(date
, &end
, 10);
901 if ((tz_offset
== -1) && ((end
- date
) == 4) && (val
< 1300) && (indate
< date
) && ((date
[-1] == '+' || date
[-1] == '-'))) {
902 /* four digits and a value less than 1300 and it is preceeded with
903 a plus or minus. This is a time zone indication. */
905 tz_offset
= (val
/ 100 * 60 + val
% 100) * 60;
907 /* the + and - prefix indicates the local time compared to GMT,
908 this we need ther reversed math to get what we want */
909 tz_offset
= date
[-1] == '+' ? -tz_offset
: tz_offset
;
912 if (((end
- date
) == 8) && (year
== -1) && (month
== -1) && (monthday
== -1)) {
913 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
916 month
= (val
% 10000) / 100 - 1; /* month is 0 - 11 */
917 monthday
= val
% 100;
920 if (!found
&& (dignext
== DATE_MDAY
) && (monthday
== -1)) {
921 if ((val
> 0) && (val
< 32)) {
928 if (!found
&& (dignext
== DATE_YEAR
) && (year
== -1)) {
932 year
+= year
> 70 ? 1900 : 2000;
951 seconds
= minutes
= hours
= 0; /* no time, make it zero */
954 if ((-1 == monthday
) || (-1 == month
) || (-1 == year
)) {
955 /* lacks vital info, fail */
959 if (sizeof(time_t) < 5) {
960 /* 32 bit time_t can only hold dates to the beginning of 2038 */
969 tm
.tm_mday
= monthday
;
971 tm
.tm_year
= year
- 1900;
978 /* time zone adjust */
980 struct tm
*gmt
, keeptime2
;
984 if(!(gmt
= php_gmtime_r(&t
, &keeptime2
))) {
985 return -1; /* illegal date/time */
990 /* Add the time zone diff (between the given timezone and GMT) and the
991 diff between the local time zone and GMT. */
992 delta
= (tz_offset
!= -1 ? tz_offset
: 0) + (t
- t2
);
994 if((delta
> 0) && (t
+ delta
< t
)) {
995 return -1; /* time_t overflow */
1005 /* {{{ inline STATUS http_send_status(int) */
1006 PHP_HTTP_API
inline STATUS
_http_send_status(const int status TSRMLS_DC
)
1009 return sapi_header_op(SAPI_HEADER_SET_STATUS
, (void *) s TSRMLS_CC
);
1013 /* {{{ inline STATUS http_send_header(char *) */
1014 PHP_HTTP_API
inline STATUS
_http_send_header(const char *header TSRMLS_DC
)
1016 return http_send_status_header(0, header
);
1020 /* {{{ inline STATUS http_send_status_header(int, char *) */
1021 PHP_HTTP_API
inline STATUS
_http_send_status_header(const int status
, const char *header TSRMLS_DC
)
1023 sapi_header_line h
= {(char *) header
, strlen(header
), status
};
1024 return sapi_header_op(SAPI_HEADER_REPLACE
, &h TSRMLS_CC
);
1028 /* {{{ inline zval *http_get_server_var(char *) */
1029 PHP_HTTP_API
inline zval
*_http_get_server_var(const char *key TSRMLS_DC
)
1032 if (SUCCESS
== zend_hash_find(
1034 (char *) key
, strlen(key
) + 1, (void **) &var
)) {
1041 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
1042 PHP_HTTP_API
void _http_ob_etaghandler(char *output
, uint output_len
,
1043 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
1045 char etag
[33] = { 0 };
1046 unsigned char digest
[16];
1048 if (mode
& PHP_OUTPUT_HANDLER_START
) {
1049 PHP_MD5Init(&HTTP_G(etag_md5
));
1052 PHP_MD5Update(&HTTP_G(etag_md5
), output
, output_len
);
1054 if (mode
& PHP_OUTPUT_HANDLER_END
) {
1055 PHP_MD5Final(digest
, &HTTP_G(etag_md5
));
1057 /* just do that if desired */
1058 if (HTTP_G(etag_started
)) {
1059 make_digest(etag
, digest
);
1061 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
1062 http_send_status(304);
1064 http_send_etag(etag
, 32);
1069 *handled_output_len
= output_len
;
1070 *handled_output
= estrndup(output
, output_len
);
1074 /* {{{ int http_modified_match(char *, int) */
1075 PHP_HTTP_API
int _http_modified_match(const char *entry
, const time_t t TSRMLS_DC
)
1079 char *modified
, *chr_ptr
;
1081 HTTP_GSC(zmodified
, entry
, 0);
1083 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
1084 if (chr_ptr
= strrchr(modified
, ';')) {
1087 retval
= (t
<= http_parse_date(modified
));
1093 /* {{{ int http_etag_match(char *, char *) */
1094 PHP_HTTP_API
int _http_etag_match(const char *entry
, const char *etag TSRMLS_DC
)
1100 HTTP_GSC(zetag
, entry
, 0);
1102 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
1106 quoted_etag
= (char *) emalloc(strlen(etag
) + 3);
1107 sprintf(quoted_etag
, "\"%s\"", etag
);
1109 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
1110 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
1112 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
1119 /* {{{ STATUS http_send_last_modified(int) */
1120 PHP_HTTP_API STATUS
_http_send_last_modified(const time_t t TSRMLS_DC
)
1122 char modified
[96] = "Last-Modified: ", *date
;
1123 date
= http_date(t
);
1124 strcat(modified
, date
);
1130 return http_send_header(modified
);
1134 /* {{{ static STATUS http_send_etag(char *, int) */
1135 PHP_HTTP_API STATUS
_http_send_etag(const char *etag
,
1136 const int etag_len TSRMLS_DC
)
1142 header_len
= strlen("ETag: \"\"") + etag_len
+ 1;
1143 etag_header
= (char *) emalloc(header_len
);
1144 snprintf(etag_header
, header_len
, "ETag: \"%s\"", etag
);
1145 ret
= http_send_header(etag_header
);
1150 efree(HTTP_G(etag
));
1152 HTTP_G(etag
) = estrdup(etag
);
1158 /* {{{ char *http_absolute_uri(char *, char *) */
1159 PHP_HTTP_API
char *_http_absolute_uri(const char *url
,
1160 const char *proto TSRMLS_DC
)
1162 char URI
[HTTP_URI_MAXLEN
+ 1], *PTR
, *proto_ptr
, *host
, *path
;
1165 if (!url
|| !strlen(url
)) {
1166 if (!SG(request_info
).request_uri
) {
1169 url
= SG(request_info
).request_uri
;
1171 /* Mess around with already absolute URIs */
1172 else if (proto_ptr
= strstr(url
, "://")) {
1173 if (!proto
|| !strncmp(url
, proto
, strlen(proto
))) {
1174 return estrdup(url
);
1176 snprintf(URI
, HTTP_URI_MAXLEN
, "%s%s", proto
, proto_ptr
+ 3);
1177 return estrdup(URI
);
1181 /* protocol defaults to http */
1182 if (!proto
|| !strlen(proto
)) {
1187 if ( (zhost
= http_get_server_var("HTTP_HOST")) ||
1188 (zhost
= http_get_server_var("SERVER_NAME"))) {
1189 host
= Z_STRVAL_P(zhost
);
1196 if (url
[0] == '/') {
1197 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s%s", proto
, host
, url
);
1198 } else if (SG(request_info
).request_uri
) {
1199 path
= estrdup(SG(request_info
).request_uri
);
1200 php_dirname(path
, strlen(path
));
1201 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s%s/%s", proto
, host
, path
, url
);
1204 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s/%s", proto
, host
, url
);
1207 /* strip everything after a new line */
1210 if (*PTR
== '\n' || *PTR
== '\r') {
1217 return estrdup(URI
);
1221 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
1222 PHP_HTTP_API
char *_http_negotiate_q(const char *entry
, const zval
*supported
,
1223 const char *def TSRMLS_DC
)
1225 zval
*zaccept
, *zarray
, *zdelim
, **zentry
, *zentries
, **zsupp
;
1226 char *q_ptr
, *result
;
1230 HTTP_GSC(zaccept
, entry
, estrdup(def
));
1232 MAKE_STD_ZVAL(zarray
);
1235 MAKE_STD_ZVAL(zdelim
);
1236 ZVAL_STRING(zdelim
, ",", 0);
1237 php_explode(zdelim
, zaccept
, zarray
, -1);
1240 MAKE_STD_ZVAL(zentries
);
1241 array_init(zentries
);
1243 c
= zend_hash_num_elements(Z_ARRVAL_P(zarray
));
1244 for (i
= 0; i
< c
; i
++, zend_hash_move_forward(Z_ARRVAL_P(zarray
))) {
1246 if (SUCCESS
!= zend_hash_get_current_data(
1247 Z_ARRVAL_P(zarray
), (void **) &zentry
)) {
1248 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1249 "Cannot parse %s header: %s", entry
, Z_STRVAL_P(zaccept
));
1253 /* check for qualifier */
1254 if (NULL
!= (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';'))) {
1255 qual
= strtod(q_ptr
+ 3, NULL
);
1260 /* walk through the supported array */
1261 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported
));
1262 SUCCESS
== zend_hash_get_current_data(
1263 Z_ARRVAL_P(supported
), (void **) &zsupp
);
1264 zend_hash_move_forward(Z_ARRVAL_P(supported
))) {
1265 if (!strcasecmp(Z_STRVAL_PP(zsupp
), Z_STRVAL_PP(zentry
))) {
1266 add_assoc_double(zentries
, Z_STRVAL_PP(zsupp
), qual
);
1275 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries
));
1277 if ( (SUCCESS
!= zend_hash_sort(Z_ARRVAL_P(zentries
), zend_qsort
,
1278 http_sort_q
, 0 TSRMLS_CC
)) ||
1279 (HASH_KEY_NON_EXISTANT
== zend_hash_get_current_key(
1280 Z_ARRVAL_P(zentries
), &result
, 0, 1))) {
1281 result
= estrdup(def
);
1284 zval_dtor(zentries
);
1291 /* {{{ http_range_status http_get_request_ranges(zval *zranges, size_t) */
1292 PHP_HTTP_API http_range_status
_http_get_request_ranges(zval
*zranges
,
1293 const size_t length TSRMLS_DC
)
1297 long begin
= -1, end
= -1, *ptr
;
1299 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
1300 range
= Z_STRVAL_P(zrange
);
1302 if (strncmp(range
, "bytes=", strlen("bytes="))) {
1303 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Range header misses bytes=");
1308 range
+= strlen("bytes=");
1311 switch (c
= *(range
++))
1317 case '1': case '2': case '3':
1318 case '4': case '5': case '6':
1319 case '7': case '8': case '9':
1321 * If the value of the pointer is already set (non-negative)
1322 * then multiply its value by ten and add the current value,
1323 * else initialise the pointers value with the current value
1325 * This let us recognize empty fields when validating the
1326 * ranges, i.e. a "-10" for begin and "12345" for the end
1327 * was the following range request: "Range: bytes=0-12345";
1328 * While a "-1" for begin and "12345" for the end would
1329 * have been: "Range: bytes=-12345".
1344 /* IE - ignore for now */
1351 /* validate ranges */
1356 if ((length
- end
) < 1) {
1364 if ((length
- end
) < 1) {
1367 begin
= length
- end
;
1377 if ((length
- begin
) < 1) {
1385 if ( ((length
- begin
) < 1) ||
1386 ((length
- end
) < 1) ||
1387 ((begin
- end
) >= 0)) {
1397 MAKE_STD_ZVAL(zentry
);
1399 add_index_long(zentry
, 0, begin
);
1400 add_index_long(zentry
, 1, end
);
1401 add_next_index_zval(zranges
, zentry
);
1419 /* {{{ STATUS http_send_ranges(zval *, void *, size_t, http_send_mode) */
1420 PHP_HTTP_API STATUS
_http_send_ranges(zval
*zranges
, const void *data
, const size_t size
, const http_send_mode mode TSRMLS_DC
)
1423 long **begin
, **end
;
1424 char range_header
[255], multi_header
[68] = "Content-Type: multipart/byteranges; boundary=", bound
[23], preface
[1024];
1427 /* Send HTTP 206 Partial Content */
1428 http_send_status(206);
1431 if ((c
= zend_hash_num_elements(Z_ARRVAL_P(zranges
))) == 1) {
1432 zend_hash_index_find(Z_ARRVAL_P(zranges
), 0, (void **) &zrange
);
1433 zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
);
1434 zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
);
1436 /* send content range header */
1437 snprintf(range_header
, 255, "Content-Range: bytes %d-%d/%d", **begin
, **end
, size
);
1438 http_send_header(range_header
);
1440 /* send requested chunk */
1441 return http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1446 snprintf(bound
, 23, "--%d%0.9f", time(NULL
), php_combined_lcg(TSRMLS_C
));
1447 strncat(multi_header
, bound
+ 2, 21);
1448 http_send_header(multi_header
);
1450 /* send each requested chunk */
1451 for ( i
= 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(zranges
));
1453 i
++, zend_hash_move_forward(Z_ARRVAL_P(zranges
))) {
1454 if ( HASH_KEY_NON_EXISTANT
== zend_hash_get_current_data(
1455 Z_ARRVAL_P(zranges
), (void **) &zrange
) ||
1456 SUCCESS
!= zend_hash_index_find(
1457 Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1458 SUCCESS
!= zend_hash_index_find(
1459 Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1463 snprintf(preface
, 1024,
1464 "\r\n%s\r\nContent-Type: %s\r\nContent-Range: bytes %d-%d/%d\r\n\r\n", bound
,
1465 HTTP_G(ctype
) ? HTTP_G(ctype
) : "application/x-octetstream",
1466 **begin
, **end
, size
);
1467 php_body_write(preface
, strlen(preface
) TSRMLS_CC
);
1468 http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1471 /* write boundary once more */
1472 php_body_write("\r\n", 1 TSRMLS_CC
);
1473 php_body_write(bound
, strlen(bound
) TSRMLS_CC
);
1479 /* {{{ STATUS http_send(void *, sizezo_t, http_send_mode) */
1480 PHP_HTTP_API STATUS
_http_send(const void *data_ptr
, const size_t data_size
,
1481 const http_send_mode data_mode TSRMLS_DC
)
1483 char *new_etag
= NULL
;
1484 int is_range_request
= http_is_range_request();
1491 if (HTTP_G(etag_started
)) {
1492 new_etag
= (char *) emalloc(33);
1494 /* never ever use the output to compute the ETag if http_send() is used */
1495 HTTP_G(etag_started
) = 0;
1496 php_end_ob_buffer(0, 0 TSRMLS_CC
);
1497 if (NULL
== http_etag(&new_etag
, data_ptr
, data_size
, data_mode
)) {
1502 /* send 304 Not Modified if etag matches */
1503 if ((!is_range_request
) && http_etag_match("HTTP_IF_NONE_MATCH", new_etag
)) {
1505 return http_send_status(304);
1508 http_send_etag(new_etag
, 32);
1512 /* send 304 Not Modified if last-modified matches*/
1513 if ((!is_range_request
) && http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod
))) {
1514 return http_send_status(304);
1517 if (is_range_request
) {
1519 /* only send ranges if entity hasn't changed */
1521 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_MATCH", 13)) ||
1522 http_etag_match("HTTP_IF_MATCH", HTTP_G(etag
)))
1524 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
1525 http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod
)))
1528 STATUS result
= FAILURE
;
1529 zval
*zranges
= NULL
;
1530 MAKE_STD_ZVAL(zranges
);
1531 array_init(zranges
);
1533 switch (http_get_request_ranges(zranges
, data_size
))
1538 /* go ahead and send all */
1542 result
= http_send_ranges(zranges
, data_ptr
, data_size
, data_mode
);
1551 http_send_status(416);
1562 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
1566 /* {{{ STATUS http_send_data(zval *) */
1567 PHP_HTTP_API STATUS
_http_send_data(const zval
*zdata TSRMLS_DC
)
1569 if (!Z_STRLEN_P(zdata
)) {
1572 if (!Z_STRVAL_P(zdata
)) {
1576 return http_send(zdata
, Z_STRLEN_P(zdata
), SEND_DATA
);
1580 /* {{{ STATUS http_send_stream(php_stream *) */
1581 PHP_HTTP_API STATUS
_http_send_stream(const php_stream
*file TSRMLS_DC
)
1583 if (php_stream_stat((php_stream
*) file
, &HTTP_G(ssb
))) {
1587 return http_send(file
, HTTP_G(ssb
).sb
.st_size
, SEND_RSRC
);
1591 /* {{{ STATUS http_send_file(zval *) */
1592 PHP_HTTP_API STATUS
_http_send_file(const zval
*zfile TSRMLS_DC
)
1597 if (!Z_STRLEN_P(zfile
)) {
1601 if (!(file
= php_stream_open_wrapper(Z_STRVAL_P(zfile
), "rb",
1602 REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
))) {
1606 ret
= http_send_stream(file
);
1607 php_stream_close(file
);
1612 /* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1613 PHP_HTTP_API STATUS
_http_chunked_decode(const char *encoded
,
1614 const size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
1620 *decoded
= (char *) ecalloc(1, encoded_len
);
1624 while (((e_ptr
- encoded
) - encoded_len
) > 0) {
1625 char hex_len
[9] = {0};
1626 size_t chunk_len
= 0;
1629 /* read in chunk size */
1630 while (isxdigit(*e_ptr
)) {
1632 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1633 "Chunk size is too long: 0x%s...", hex_len
);
1637 hex_len
[i
++] = *e_ptr
++;
1641 if (strcmp(hex_len
, "0")) {
1643 chunk_len
= strtol(hex_len
, &error
, 16);
1644 if (error
== hex_len
) {
1645 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1646 "Invalid chunk size string: '%s'", hex_len
);
1655 if (*e_ptr
++ != '\r' || *e_ptr
++ != '\n') {
1656 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1657 "Invalid character (expected 0x0A, 0x0D; got: %x)",
1663 memcpy(d_ptr
, e_ptr
, chunk_len
);
1665 e_ptr
+= chunk_len
+ 2;
1666 *decoded_len
+= chunk_len
;
1673 /* {{{ proto void http_split_response(zval *, zval *, zval *) */
1674 PHP_HTTP_API
void _http_split_response(const zval
*zresponse
, zval
*zheaders
,
1675 zval
*zbody TSRMLS_DC
)
1677 char *header
, *response
, *body
= NULL
;
1678 long response_len
= Z_STRLEN_P(zresponse
);
1679 header
= response
= Z_STRVAL_P(zresponse
);
1681 while ((response
- Z_STRVAL_P(zresponse
) + 3) < response_len
) {
1682 if ( (*response
++ == '\r') &&
1683 (*response
++ == '\n') &&
1684 (*response
++ == '\r') &&
1685 (*response
++ == '\n')) {
1691 if (body
&& (response_len
- (body
- header
))) {
1692 ZVAL_STRINGL(zbody
, body
, response_len
- (body
- header
) - 1, 1);
1694 Z_TYPE_P(zbody
) = IS_NULL
;
1697 /* check for HTTP status - FIXXME: strchr() */
1698 if (!strncmp(header
, "HTTP/1.", 7)) {
1699 char *end
= strchr(header
, '\r');
1700 add_assoc_stringl(zheaders
, "Status",
1701 header
+ strlen("HTTP/1.x "),
1702 end
- (header
+ strlen("HTTP/1.x ")), 1);
1707 char *colon
= NULL
, *line
= header
;
1709 while ( (line
- Z_STRVAL_P(zresponse
) + 3) <
1710 (body
- Z_STRVAL_P(zresponse
))) {
1714 if (colon
&& (*line
== '\n')) {
1715 char *key
= estrndup(header
, colon
- header
);
1716 add_assoc_stringl(zheaders
, key
,
1717 colon
+ 2, line
- colon
- 3, 1);
1721 header
+= line
- header
+ 1;
1737 #ifdef HTTP_HAVE_CURL
1739 /* {{{ STATUS http_get(char *, HashTable *, HashTable *, char **, size_t *) */
1740 PHP_HTTP_API STATUS
_http_get(const char *URL
, HashTable
*options
,
1741 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
1743 CURL
*ch
= curl_easy_init();
1746 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
1750 http_curl_initbuf(CURLBUF_EVRY
);
1751 http_curl_setopts(ch
, URL
, options
);
1753 if (CURLE_OK
!= curl_easy_perform(ch
)) {
1754 curl_easy_cleanup(ch
);
1755 http_curl_freebuf(CURLBUF_EVRY
);
1756 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
1760 http_curl_getinfo(ch
, info
);
1762 curl_easy_cleanup(ch
);
1764 http_curl_movebuf(CURLBUF_EVRY
, data
, data_len
);
1770 /* {{{ STATUS http_head(char *, HashTable *, HashTable *, char **data, size_t *) */
1771 PHP_HTTP_API STATUS
_http_head(const char *URL
, HashTable
*options
,
1772 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
1774 CURL
*ch
= curl_easy_init();
1777 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
1781 http_curl_initbuf(CURLBUF_HDRS
);
1782 http_curl_setopts(ch
, URL
, options
);
1783 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
1785 if (CURLE_OK
!= curl_easy_perform(ch
)) {
1786 curl_easy_cleanup(ch
);
1787 http_curl_freebuf(CURLBUF_HDRS
);
1788 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
1792 http_curl_getinfo(ch
, info
);
1794 curl_easy_cleanup(ch
);
1796 http_curl_movebuf(CURLBUF_HDRS
, data
, data_len
);
1802 /* {{{ STATUS http_post_data(char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
1803 PHP_HTTP_API STATUS
_http_post_data(const char *URL
, char *postdata
,
1804 size_t postdata_len
, HashTable
*options
, HashTable
*info
, char **data
,
1805 size_t *data_len TSRMLS_DC
)
1807 CURL
*ch
= curl_easy_init();
1810 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
1814 http_curl_initbuf(CURLBUF_EVRY
);
1815 http_curl_setopts(ch
, URL
, options
);
1816 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
1817 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, postdata
);
1818 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, postdata_len
);
1820 if (CURLE_OK
!= curl_easy_perform(ch
)) {
1821 curl_easy_cleanup(ch
);
1822 http_curl_freebuf(CURLBUF_EVRY
);
1823 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
1827 http_curl_getinfo(ch
, info
);
1829 curl_easy_cleanup(ch
);
1831 http_curl_movebuf(CURLBUF_EVRY
, data
, data_len
);
1837 /* {{{ STATUS http_post_array(char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
1838 PHP_HTTP_API STATUS
_http_post_array(const char *URL
, HashTable
*postarray
,
1839 HashTable
*options
, HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
1841 smart_str qstr
= {0};
1844 if (php_url_encode_hash_ex(postarray
, &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
1848 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode post data");
1853 status
= http_post_data(URL
, qstr
.c
, qstr
.len
, options
, info
, data
, data_len
);
1864 /* {{{ void http_auth_header(char *, char*) */
1865 PHP_HTTP_API
void _http_auth_header(const char *type
, const char *realm TSRMLS_DC
)
1867 char realm_header
[1024];
1868 snprintf(realm_header
, 1024, "WWW-Authenticate: %s realm=\"%s\"", type
, realm
);
1869 http_send_status_header(401, realm_header
);
1873 /* {{{ STATUS http_auth_credentials(char **, char **) */
1874 PHP_HTTP_API STATUS
_http_auth_credentials(char **user
, char **pass TSRMLS_DC
)
1876 if (strncmp(sapi_module
.name
, "isapi", 5)) {
1877 zval
*zuser
, *zpass
;
1879 HTTP_GSC(zuser
, "PHP_AUTH_USER", FAILURE
);
1880 HTTP_GSC(zpass
, "PHP_AUTH_PW", FAILURE
);
1882 *user
= estrndup(Z_STRVAL_P(zuser
), Z_STRLEN_P(zuser
));
1883 *pass
= estrndup(Z_STRVAL_P(zpass
), Z_STRLEN_P(zpass
));
1888 HTTP_GSC(zauth
, "HTTP_AUTHORIZATION", FAILURE
);
1890 char *decoded
, *colon
;
1892 decoded
= php_base64_decode(Z_STRVAL_P(zauth
), Z_STRLEN_P(zauth
),
1895 if (colon
= strchr(decoded
+ 6, ':')) {
1896 *user
= estrndup(decoded
+ 6, colon
- decoded
- 6);
1897 *pass
= estrndup(colon
+ 1, decoded
+ decoded_len
- colon
- 6 - 1);
1908 /* }}} public API */
1915 * vim600: noet sw=4 ts=4 fdm=marker
1916 * vim<600: noet sw=4 ts=4