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"
37 #include "ext/standard/php_filestat.h"
42 # include "ext/standard/php_http.h"
44 #include "http_build_query.c"
48 #include "php_http_api.h"
53 # include <winsock2.h>
54 # include <sys/types.h>
57 # include <curl/curl.h>
58 # include <curl/easy.h>
63 ZEND_DECLARE_MODULE_GLOBALS(http
)
65 /* {{{ day/month names */
66 static const char *days
[] = {
67 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
69 static const char *wkdays
[] = {
70 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
72 static const char *weekdays
[] = {
73 "Monday", "Tuesday", "Wednesday",
74 "Thursday", "Friday", "Saturday", "Sunday"
76 static const char *months
[] = {
77 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
78 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
85 static const struct time_zone
{
89 {"GMT", 0}, /* Greenwich Mean */
90 {"UTC", 0}, /* Universal (Coordinated) */
91 {"WET", 0}, /* Western European */
92 {"BST", 0}, /* British Summer */
93 {"WAT", 60}, /* West Africa */
94 {"AST", 240}, /* Atlantic Standard */
95 {"ADT", 240}, /* Atlantic Daylight */
96 {"EST", 300}, /* Eastern Standard */
97 {"EDT", 300}, /* Eastern Daylight */
98 {"CST", 360}, /* Central Standard */
99 {"CDT", 360}, /* Central Daylight */
100 {"MST", 420}, /* Mountain Standard */
101 {"MDT", 420}, /* Mountain Daylight */
102 {"PST", 480}, /* Pacific Standard */
103 {"PDT", 480}, /* Pacific Daylight */
104 {"YST", 540}, /* Yukon Standard */
105 {"YDT", 540}, /* Yukon Daylight */
106 {"HST", 600}, /* Hawaii Standard */
107 {"HDT", 600}, /* Hawaii Daylight */
108 {"CAT", 600}, /* Central Alaska */
109 {"AHST", 600}, /* Alaska-Hawaii Standard */
110 {"NT", 660}, /* Nome */
111 {"IDLW", 720}, /* International Date Line West */
112 {"CET", -60}, /* Central European */
113 {"MET", -60}, /* Middle European */
114 {"MEWT", -60}, /* Middle European Winter */
115 {"MEST", -120}, /* Middle European Summer */
116 {"CEST", -120}, /* Central European Summer */
117 {"MESZ", -60}, /* Middle European Summer */
118 {"FWT", -60}, /* French Winter */
119 {"FST", -60}, /* French Summer */
120 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
121 {"WAST", -420}, /* West Australian Standard */
122 {"WADT", -420}, /* West Australian Daylight */
123 {"CCT", -480}, /* China Coast, USSR Zone 7 */
124 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
125 {"EAST", -600}, /* Eastern Australian Standard */
126 {"EADT", -600}, /* Eastern Australian Daylight */
127 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
128 {"NZT", -720}, /* New Zealand */
129 {"NZST", -720}, /* New Zealand Standard */
130 {"NZDT", -720}, /* New Zealand Daylight */
131 {"IDLE", -720}, /* International Date Line East */
137 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
);
138 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
139 static STATUS
_http_send_chunk(const void *data
, const size_t begin
, const size_t end
, const http_send_mode mode TSRMLS_DC
);
141 static int check_day(char *day
, size_t len
);
142 static int check_month(char *month
);
143 static int check_tzone(char *tzone
);
145 static char *pretty_key(char *key
, int key_len
, int uctitle
, int xhyphen
);
147 static int http_ob_stack_get(php_ob_buffer
*, php_ob_buffer
**);
150 #ifdef HTTP_HAVE_CURL
151 #define http_curl_initbuf(m) _http_curl_initbuf((m) TSRMLS_CC)
152 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC
);
153 #define http_curl_freebuf(m) _http_curl_freebuf((m) TSRMLS_CC)
154 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC
);
155 #define http_curl_sizebuf(m, l) _http_curl_sizebuf((m), (l) TSRMLS_CC)
156 static inline void _http_curl_sizebuf(http_curlbuf_member member
, size_t len TSRMLS_DC
);
157 #define http_curl_movebuf(m, d, l) _http_curl_movebuf((m), (d), (l) TSRMLS_CC)
158 static inline void _http_curl_movebuf(http_curlbuf_member member
, char **data
, size_t *data_len TSRMLS_DC
);
159 #define http_curl_copybuf(m, d, l) _http_curl_copybuf((m), (d), (l) TSRMLS_CC)
160 static inline void _http_curl_copybuf(http_curlbuf_member member
, char **data
, size_t *data_len TSRMLS_DC
);
161 #define http_curl_setopts(c, u, o) _http_curl_setopts((c), (u), (o) TSRMLS_CC)
162 static inline void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
);
164 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
165 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
166 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
167 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...);
169 static size_t http_curl_body_callback(char *, size_t, size_t, void *);
170 static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *);
172 #define http_curl_getinfo(c, h) _http_curl_getinfo((c), (h) TSRMLS_CC)
173 static inline void _http_curl_getinfo(CURL
*ch
, HashTable
*info TSRMLS_DC
);
174 #define http_curl_getinfo_ex(c, i, a) _http_curl_getinfo_ex((c), (i), (a) TSRMLS_CC)
175 static inline void _http_curl_getinfo_ex(CURL
*ch
, CURLINFO i
, zval
*array TSRMLS_DC
);
176 #define http_curl_getinfoname(i) _http_curl_getinfoname((i) TSRMLS_CC)
177 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC
);
182 /* {{{ static int http_sort_q(const void *, const void *) */
183 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
186 zval result
, *first
, *second
;
188 f
= *((Bucket
**) a
);
189 s
= *((Bucket
**) b
);
191 first
= *((zval
**) f
->pData
);
192 second
= *((zval
**) s
->pData
);
194 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
197 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
201 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
203 static STATUS
_http_send_chunk(const void *data
, const size_t begin
,
204 const size_t end
, const http_send_mode mode TSRMLS_DC
)
208 long len
= end
- begin
;
214 s
= (php_stream
*) data
;
215 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
218 buf
= (char *) ecalloc(1, HTTP_BUF_SIZE
);
219 /* read into buf and write out */
220 while ((len
-= HTTP_BUF_SIZE
) >= 0) {
221 if (!(read
= php_stream_read(s
, buf
, HTTP_BUF_SIZE
))) {
225 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
231 /* read & write left over */
233 if (read
= php_stream_read(s
, buf
, HTTP_BUF_SIZE
+ len
)) {
234 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
248 return len
== php_body_write(((char *)data
) + begin
, len TSRMLS_CC
)
260 #ifdef HTTP_HAVE_CURL
262 /* {{{ static inline void http_curl_initbuf(http_curlbuf_member) */
263 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC
)
265 http_curl_freebuf(member
);
267 if (member
& CURLBUF_HDRS
) {
268 HTTP_G(curlbuf
).hdrs
.data
= emalloc(HTTP_CURLBUF_HDRSSIZE
);
269 HTTP_G(curlbuf
).hdrs
.free
= HTTP_CURLBUF_HDRSSIZE
;
271 if (member
& CURLBUF_BODY
) {
272 HTTP_G(curlbuf
).body
.data
= emalloc(HTTP_CURLBUF_BODYSIZE
);
273 HTTP_G(curlbuf
).body
.free
= HTTP_CURLBUF_BODYSIZE
;
278 /* {{{ static inline void http_curl_freebuf(http_curlbuf_member) */
279 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC
)
281 if (member
& CURLBUF_HDRS
) {
282 if (HTTP_G(curlbuf
).hdrs
.data
) {
283 efree(HTTP_G(curlbuf
).hdrs
.data
);
284 HTTP_G(curlbuf
).hdrs
.data
= NULL
;
286 HTTP_G(curlbuf
).hdrs
.used
= 0;
287 HTTP_G(curlbuf
).hdrs
.free
= 0;
289 if (member
& CURLBUF_BODY
) {
290 if (HTTP_G(curlbuf
).body
.data
) {
291 efree(HTTP_G(curlbuf
).body
.data
);
292 HTTP_G(curlbuf
).body
.data
= NULL
;
294 HTTP_G(curlbuf
).body
.used
= 0;
295 HTTP_G(curlbuf
).body
.free
= 0;
300 /* {{{ static inline void http_curl_copybuf(http_curlbuf_member, char **,
302 static inline void _http_curl_copybuf(http_curlbuf_member member
, char **data
,
303 size_t *data_len TSRMLS_DC
)
308 if ((member
& CURLBUF_HDRS
) && HTTP_G(curlbuf
).hdrs
.used
) {
309 if ((member
& CURLBUF_BODY
) && HTTP_G(curlbuf
).body
.used
) {
310 *data
= emalloc(HTTP_G(curlbuf
).hdrs
.used
+ HTTP_G(curlbuf
).body
.used
+ 1);
312 *data
= emalloc(HTTP_G(curlbuf
).hdrs
.used
+ 1);
314 memcpy(*data
, HTTP_G(curlbuf
).hdrs
.data
, HTTP_G(curlbuf
).hdrs
.used
);
315 *data_len
= HTTP_G(curlbuf
).hdrs
.used
;
318 if ((member
& CURLBUF_BODY
) && HTTP_G(curlbuf
).body
.used
) {
320 memcpy((*data
) + HTTP_G(curlbuf
).hdrs
.used
,
321 HTTP_G(curlbuf
).body
.data
, HTTP_G(curlbuf
).body
.used
);
322 *data_len
= HTTP_G(curlbuf
).hdrs
.used
+ HTTP_G(curlbuf
).body
.used
;
324 emalloc(HTTP_G(curlbuf
).body
.used
+ 1);
325 memcpy(*data
, HTTP_G(curlbuf
).body
.data
, HTTP_G(curlbuf
).body
.used
);
326 *data_len
= HTTP_G(curlbuf
).body
.used
;
330 (*data
)[*data_len
] = 0;
337 /* {{{ static inline void http_curl_movebuf(http_curlbuf_member, char **,
339 static inline void _http_curl_movebuf(http_curlbuf_member member
, char **data
,
340 size_t *data_len TSRMLS_DC
)
342 http_curl_copybuf(member
, data
, data_len
);
343 http_curl_freebuf(member
);
347 /* {{{ static size_t http_curl_body_callback(char *, size_t, size_t, void *) */
348 static size_t http_curl_body_callback(char *buf
, size_t len
, size_t n
, void *s
)
352 if ((len
*= n
) > HTTP_G(curlbuf
).body
.free
) {
353 size_t bsize
= HTTP_CURLBUF_BODYSIZE
;
354 while (bsize
< len
) {
357 HTTP_G(curlbuf
).body
.data
= erealloc(HTTP_G(curlbuf
).body
.data
,
358 HTTP_G(curlbuf
).body
.used
+ bsize
);
359 HTTP_G(curlbuf
).body
.free
+= bsize
;
362 memcpy(HTTP_G(curlbuf
).body
.data
+ HTTP_G(curlbuf
).body
.used
, buf
, len
);
363 HTTP_G(curlbuf
).body
.free
-= len
;
364 HTTP_G(curlbuf
).body
.used
+= len
;
370 /* {{{ static size_t http_curl_hdrs_callback(char*, size_t, size_t, void *) */
371 static size_t http_curl_hdrs_callback(char *buf
, size_t len
, size_t n
, void *s
)
375 /* discard previous headers */
376 if ((HTTP_G(curlbuf
).hdrs
.used
) && (!strncmp(buf
, "HTTP/1.", strlen("HTTP/1.")))) {
377 http_curl_initbuf(CURLBUF_HDRS
);
380 if ((len
*= n
) > HTTP_G(curlbuf
).hdrs
.free
) {
381 size_t bsize
= HTTP_CURLBUF_HDRSSIZE
;
382 while (bsize
< len
) {
385 HTTP_G(curlbuf
).hdrs
.data
= erealloc(HTTP_G(curlbuf
).hdrs
.data
,
386 HTTP_G(curlbuf
).hdrs
.used
+ bsize
);
387 HTTP_G(curlbuf
).hdrs
.free
+= bsize
;
390 memcpy(HTTP_G(curlbuf
).hdrs
.data
+ HTTP_G(curlbuf
).hdrs
.used
, buf
, len
);
391 HTTP_G(curlbuf
).hdrs
.free
-= len
;
392 HTTP_G(curlbuf
).hdrs
.used
+= len
;
398 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
399 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...)
405 if (SUCCESS
!= zend_hash_find(options
, key
, strlen(key
) + 1, (void **) &zoption
)) {
412 va_start(types
, checks
);
413 for (i
= 0; i
< checks
; ++i
) {
414 if ((va_arg(types
, int)) == (Z_TYPE_PP(zoption
))) {
424 /* {{{ static inline void http_curl_setopts(CURL *, char *, HashTable *) */
425 static inline void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
)
429 /* standard options */
430 curl_easy_setopt(ch
, CURLOPT_URL
, url
);
431 curl_easy_setopt(ch
, CURLOPT_HEADER
, 0);
432 curl_easy_setopt(ch
, CURLOPT_NOPROGRESS
, 1);
433 curl_easy_setopt(ch
, CURLOPT_AUTOREFERER
, 1);
434 curl_easy_setopt(ch
, CURLOPT_WRITEFUNCTION
, http_curl_body_callback
);
435 curl_easy_setopt(ch
, CURLOPT_HEADERFUNCTION
, http_curl_hdrs_callback
);
437 curl_easy_setopt(ch
, CURLOPT_NOSIGNAL
, 1);
440 if ((!options
) || (1 > zend_hash_num_elements(options
))) {
444 /* redirects, defaults to 0 */
445 if (zoption
= http_curl_getopt1(options
, "redirect", IS_LONG
)) {
446 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
447 curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, Z_LVAL_P(zoption
));
448 if (zoption
= http_curl_getopt2(options
, "unrestrictedauth", IS_LONG
, IS_BOOL
)) {
449 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
452 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, 0);
456 if (zoption
= http_curl_getopt1(options
, "proxyhost", IS_STRING
)) {
457 curl_easy_setopt(ch
, CURLOPT_PROXY
, Z_STRVAL_P(zoption
));
459 if (zoption
= http_curl_getopt1(options
, "proxyport", IS_LONG
)) {
460 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, Z_LVAL_P(zoption
));
463 if (zoption
= http_curl_getopt1(options
, "proxyauth", IS_STRING
)) {
464 curl_easy_setopt(ch
, CURLOPT_PROXYUSERPWD
, Z_STRVAL_P(zoption
));
467 if (zoption
= http_curl_getopt1(options
, "proxyauthtype", IS_LONG
)) {
468 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, Z_LVAL_P(zoption
));
473 if (zoption
= http_curl_getopt1(options
, "httpauth", IS_STRING
)) {
474 curl_easy_setopt(ch
, CURLOPT_USERPWD
, Z_STRVAL_P(zoption
));
476 if (zoption
= http_curl_getopt1(options
, "httpauthtype", IS_LONG
)) {
477 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, Z_LVAL_P(zoption
));
480 /* compress, enabled by default (empty string enables deflate and gzip) */
481 if (zoption
= http_curl_getopt2(options
, "compress", IS_LONG
, IS_BOOL
)) {
482 if (Z_LVAL_P(zoption
)) {
483 curl_easy_setopt(ch
, CURLOPT_ENCODING
, "");
486 curl_easy_setopt(ch
, CURLOPT_ENCODING
, "");
490 if (zoption
= http_curl_getopt1(options
, "port", IS_LONG
)) {
491 curl_easy_setopt(ch
, CURLOPT_PORT
, Z_LVAL_P(zoption
));
495 if (zoption
= http_curl_getopt1(options
, "referer", IS_STRING
)) {
496 curl_easy_setopt(ch
, CURLOPT_REFERER
, Z_STRVAL_P(zoption
));
499 /* useragent, default "PECL::HTTP/version (PHP/version)" */
500 if (zoption
= http_curl_getopt1(options
, "useragent", IS_STRING
)) {
501 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, Z_STRVAL_P(zoption
));
503 curl_easy_setopt(ch
, CURLOPT_USERAGENT
,
504 "PECL::HTTP/" PHP_EXT_HTTP_VERSION
" (PHP/" PHP_VERSION
")");
507 /* cookies, array('name' => 'value') */
508 if (zoption
= http_curl_getopt1(options
, "cookies", IS_ARRAY
)) {
512 smart_str qstr
= {0};
514 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption
));
515 while (HASH_KEY_NON_EXISTANT
!= (key_type
= zend_hash_get_current_key_type(Z_ARRVAL_P(zoption
)))) {
516 if (key_type
== HASH_KEY_IS_STRING
) {
517 zend_hash_get_current_key(Z_ARRVAL_P(zoption
), &cookie_key
, NULL
, 0);
518 zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
);
519 smart_str_appends(&qstr
, cookie_key
);
520 smart_str_appendl(&qstr
, "=", 1);
521 smart_str_appendl(&qstr
, Z_STRVAL_PP(cookie_val
), Z_STRLEN_PP(cookie_val
));
522 smart_str_appendl(&qstr
, "; ", 2);
523 zend_hash_move_forward(Z_ARRVAL_P(zoption
));
529 curl_easy_setopt(ch
, CURLOPT_COOKIE
, qstr
.c
);
534 if (zoption
= http_curl_getopt1(options
, "cookiestore", IS_STRING
)) {
535 curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, Z_STRVAL_P(zoption
));
536 curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, Z_STRVAL_P(zoption
));
539 /* additional headers, array('name' => 'value') */
540 if (zoption
= http_curl_getopt1(options
, "headers", IS_ARRAY
)) {
542 char *header_key
, header
[1024] = {0};
544 struct curl_slist
*headers
= NULL
;
546 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption
));
547 while (HASH_KEY_NON_EXISTANT
!= (key_type
= zend_hash_get_current_key_type(Z_ARRVAL_P(zoption
)))) {
548 if (key_type
== HASH_KEY_IS_STRING
) {
549 zend_hash_get_current_key(Z_ARRVAL_P(zoption
), &header_key
, NULL
, 0);
550 zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
);
551 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
552 headers
= curl_slist_append(headers
, header
);
553 zend_hash_move_forward(Z_ARRVAL_P(zoption
));
557 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, headers
);
563 /* {{{ static inline char *http_curl_getinfoname(CURLINFO) */
564 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC
)
566 #define CASE(I) case CURLINFO_ ##I : { static char I[] = #I; return pretty_key(I, sizeof(#I)-1, 0, 0); }
569 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
571 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
573 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
575 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
576 CASE(NAMELOOKUP_TIME
);
577 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
579 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
580 CASE(PRETRANSFER_TIME
);
581 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
583 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
585 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
586 CASE(SPEED_DOWNLOAD
);
587 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
589 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
591 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
593 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
594 CASE(SSL_VERIFYRESULT
);
595 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
597 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
598 CASE(CONTENT_LENGTH_DOWNLOAD
);
599 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
600 CASE(CONTENT_LENGTH_UPLOAD
);
601 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
602 CASE(STARTTRANSFER_TIME
);
603 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
605 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
607 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
608 CASE(REDIRECT_COUNT
);
609 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
611 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
612 CASE(HTTP_CONNECTCODE
);
613 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
614 CASE(HTTPAUTH_AVAIL
);
615 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
616 CASE(PROXYAUTH_AVAIL
);
623 /* {{{ static inline void http_curl_getinfo_ex(CURL, CURLINFO, zval *) */
624 static inline void _http_curl_getinfo_ex(CURL
*ch
, CURLINFO i
, zval
*array TSRMLS_DC
)
627 if (key
= http_curl_getinfoname(i
)) {
628 switch (i
& ~CURLINFO_MASK
)
630 case CURLINFO_STRING
:
633 if (CURLE_OK
== curl_easy_getinfo(ch
, i
, &c
)) {
634 add_assoc_string(array
, key
, c
? c
: "", 1);
639 case CURLINFO_DOUBLE
:
642 if (CURLE_OK
== curl_easy_getinfo(ch
, i
, &d
)) {
643 add_assoc_double(array
, key
, d
);
651 if (CURLE_OK
== curl_easy_getinfo(ch
, i
, &l
)) {
652 add_assoc_long(array
, key
, l
);
661 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
662 static inline void _http_curl_getinfo(CURL
*ch
, HashTable
*info TSRMLS_DC
)
665 Z_ARRVAL(array
) = info
;
667 #define INFO(I) http_curl_getinfo_ex(ch, CURLINFO_ ##I , &array)
668 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
670 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
672 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
674 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
675 INFO(NAMELOOKUP_TIME
);
676 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
678 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
679 INFO(PRETRANSFER_TIME
);
680 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
682 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
684 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
685 INFO(SPEED_DOWNLOAD
);
686 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
688 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
690 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
692 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
693 INFO(SSL_VERIFYRESULT
);
694 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
696 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
697 INFO(CONTENT_LENGTH_DOWNLOAD
);
698 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
699 INFO(CONTENT_LENGTH_UPLOAD
);
700 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
701 INFO(STARTTRANSFER_TIME
);
702 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
704 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
706 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
707 INFO(REDIRECT_COUNT
);
708 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
710 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
711 INFO(HTTP_CONNECTCODE
);
712 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
713 INFO(HTTPAUTH_AVAIL
);
714 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
715 INFO(PROXYAUTH_AVAIL
);
723 /* {{{ Day/Month/TZ checks for http_parse_date()
724 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
725 static int check_day(char *day
, size_t len
)
728 const char * const *check
= (len
> 3) ? &weekdays
[0] : &wkdays
[0];
729 for (i
= 0; i
< 7; i
++) {
730 if (!strcmp(day
, check
[0])) {
738 static int check_month(char *month
)
741 const char * const *check
= &months
[0];
742 for (i
= 0; i
< 12; i
++) {
743 if (!strcmp(month
, check
[0])) {
751 /* return the time zone offset between GMT and the input one, in number
752 of seconds or -1 if the timezone wasn't found/legal */
754 static int check_tzone(char *tzone
)
757 const struct time_zone
*check
= time_zones
;
758 for (i
= 0; i
< sizeof(time_zones
) / sizeof(time_zones
[0]); i
++) {
759 if (!strcmp(tzone
, check
->name
)) {
760 return check
->offset
* 60;
768 /* static char *pretty_key(char *, int, int, int) */
769 static char *pretty_key(char *key
, int key_len
, int uctitle
, int xhyphen
)
771 if (key
&& key_len
) {
773 if (wasalpha
= isalpha(key
[0])) {
774 key
[0] = uctitle
? toupper(key
[0]) : tolower(key
[0]);
776 for (i
= 1; i
< key_len
; i
++) {
777 if (isalpha(key
[i
])) {
778 key
[i
] = ((!wasalpha
) && uctitle
) ? toupper(key
[i
]) : tolower(key
[i
]);
781 if (xhyphen
&& (key
[i
] == '_')) {
792 /* {{{ static STATUS http_ob_stack_get(php_ob_buffer *, php_ob_buffer **) */
793 static STATUS
http_ob_stack_get(php_ob_buffer
*o
, php_ob_buffer
**s
)
796 php_ob_buffer
*b
= emalloc(sizeof(php_ob_buffer
));
797 b
->handler_name
= estrdup(o
->handler_name
);
798 b
->buffer
= estrndup(o
->buffer
, o
->text_length
);
799 b
->text_length
= o
->text_length
;
800 b
->chunk_size
= o
->chunk_size
;
811 /* {{{ char *http_date(time_t) */
812 PHP_HTTP_API
char *_http_date(time_t t TSRMLS_DC
)
814 struct tm
*gmtime
, tmbuf
;
815 char *date
= ecalloc(31, 1);
817 gmtime
= php_gmtime_r(&t
, &tmbuf
);
819 "%s, %02d %s %04d %02d:%02d:%02d GMT",
820 days
[gmtime
->tm_wday
], gmtime
->tm_mday
,
821 months
[gmtime
->tm_mon
], gmtime
->tm_year
+ 1900,
822 gmtime
->tm_hour
, gmtime
->tm_min
, gmtime
->tm_sec
828 /* {{{ time_t http_parse_date(char *)
829 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
830 PHP_HTTP_API
time_t _http_parse_date(const char *date
)
833 int tz_offset
= -1, year
= -1, month
= -1, monthday
= -1, weekday
= -1,
834 hours
= -1, minutes
= -1, seconds
= -1;
836 enum assume_next dignext
= DATE_MDAY
;
837 const char *indate
= date
;
839 int found
= 0, part
= 0; /* max 6 parts */
841 while (*date
&& (part
< 6)) {
844 while (*date
&& !isalnum(*date
)) {
848 if (isalpha(*date
)) {
849 /* a name coming up */
852 sscanf(date
, "%31[A-Za-z]", buf
);
856 weekday
= check_day(buf
, len
);
862 if (!found
&& (month
== -1)) {
863 month
= check_month(buf
);
869 if (!found
&& (tz_offset
== -1)) {
870 /* this just must be a time zone string */
871 tz_offset
= check_tzone(buf
);
872 if (tz_offset
!= -1) {
878 return -1; /* bad string */
882 else if (isdigit(*date
)) {
886 if((seconds
== -1) && (3 == sscanf(date
, "%02d:%02d:%02d", &hours
, &minutes
, &seconds
))) {
892 val
= (int) strtol(date
, &end
, 10);
894 if ((tz_offset
== -1) && ((end
- date
) == 4) && (val
< 1300) && (indate
< date
) && ((date
[-1] == '+' || date
[-1] == '-'))) {
895 /* four digits and a value less than 1300 and it is preceeded with
896 a plus or minus. This is a time zone indication. */
898 tz_offset
= (val
/ 100 * 60 + val
% 100) * 60;
900 /* the + and - prefix indicates the local time compared to GMT,
901 this we need ther reversed math to get what we want */
902 tz_offset
= date
[-1] == '+' ? -tz_offset
: tz_offset
;
905 if (((end
- date
) == 8) && (year
== -1) && (month
== -1) && (monthday
== -1)) {
906 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
909 month
= (val
% 10000) / 100 - 1; /* month is 0 - 11 */
910 monthday
= val
% 100;
913 if (!found
&& (dignext
== DATE_MDAY
) && (monthday
== -1)) {
914 if ((val
> 0) && (val
< 32)) {
921 if (!found
&& (dignext
== DATE_YEAR
) && (year
== -1)) {
925 year
+= year
> 70 ? 1900 : 2000;
944 seconds
= minutes
= hours
= 0; /* no time, make it zero */
947 if ((-1 == monthday
) || (-1 == month
) || (-1 == year
)) {
948 /* lacks vital info, fail */
952 if (sizeof(time_t) < 5) {
953 /* 32 bit time_t can only hold dates to the beginning of 2038 */
962 tm
.tm_mday
= monthday
;
964 tm
.tm_year
= year
- 1900;
971 /* time zone adjust */
973 struct tm
*gmt
, keeptime2
;
977 if(!(gmt
= php_gmtime_r(&t
, &keeptime2
))) {
978 return -1; /* illegal date/time */
983 /* Add the time zone diff (between the given timezone and GMT) and the
984 diff between the local time zone and GMT. */
985 delta
= (tz_offset
!= -1 ? tz_offset
: 0) + (t
- t2
);
987 if((delta
> 0) && (t
+ delta
< t
)) {
988 return -1; /* time_t overflow */
998 /* {{{ inline char *http_etag(void *, size_t, http_send_mode) */
999 PHP_HTTP_API
inline char *_http_etag(const void *data_ptr
, const size_t data_len
,
1000 const http_send_mode data_mode TSRMLS_DC
)
1002 char ssb_buf
[128] = {0};
1003 unsigned char digest
[16];
1005 char *new_etag
= ecalloc(33, 1);
1012 PHP_MD5Update(&ctx
, data_ptr
, data_len
);
1016 if (!HTTP_G(ssb
).sb
.st_ino
) {
1017 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
1021 snprintf(ssb_buf
, 127, "%ld=%ld=%ld",
1022 HTTP_G(ssb
).sb
.st_mtime
,
1023 HTTP_G(ssb
).sb
.st_ino
,
1024 HTTP_G(ssb
).sb
.st_size
1026 PHP_MD5Update(&ctx
, ssb_buf
, strlen(ssb_buf
));
1035 PHP_MD5Final(digest
, &ctx
);
1036 make_digest(new_etag
, digest
);
1042 /* {{{ inline http_lmod(void *, http_send_mode) */
1043 PHP_HTTP_API
inline time_t _http_lmod(const void *data_ptr
, const http_send_mode data_mode TSRMLS_DC
)
1054 if (!HTTP_G(ssb
).sb
.st_mtime
) {
1055 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
1059 return HTTP_G(ssb
).sb
.st_mtime
;
1064 if (!HTTP_G(ssb
).sb
.st_mtime
) {
1065 if(php_stream_stat_path(Z_STRVAL_P((zval
*) data_ptr
), &HTTP_G(ssb
))) {
1069 return HTTP_G(ssb
).sb
.st_mtime
;
1075 /* {{{inline int http_is_range_request(void) */
1076 PHP_HTTP_API
inline int _http_is_range_request(TSRMLS_D
)
1078 return zend_hash_exists(Z_ARRVAL_P(PG(http_globals
)[TRACK_VARS_SERVER
]),
1079 "HTTP_RANGE", strlen("HTTP_RANGE") + 1);
1083 /* {{{ inline STATUS http_send_status(int) */
1084 PHP_HTTP_API
inline STATUS
_http_send_status(const int status TSRMLS_DC
)
1087 return sapi_header_op(SAPI_HEADER_SET_STATUS
, (void *) s TSRMLS_CC
);
1091 /* {{{ inline STATUS http_send_header(char *) */
1092 PHP_HTTP_API
inline STATUS
_http_send_header(const char *header TSRMLS_DC
)
1094 return http_send_status_header(0, header
);
1098 /* {{{ inline STATUS http_send_status_header(int, char *) */
1099 PHP_HTTP_API
inline STATUS
_http_send_status_header(const int status
, const char *header TSRMLS_DC
)
1101 sapi_header_line h
= {(char *) header
, strlen(header
), status
};
1102 return sapi_header_op(SAPI_HEADER_REPLACE
, &h TSRMLS_CC
);
1106 /* {{{ inline zval *http_get_server_var(char *) */
1107 PHP_HTTP_API
inline zval
*_http_get_server_var(const char *key TSRMLS_DC
)
1110 if (SUCCESS
== zend_hash_find(
1112 (char *) key
, strlen(key
) + 1, (void **) &var
)) {
1119 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
1120 PHP_HTTP_API
void _http_ob_etaghandler(char *output
, uint output_len
,
1121 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
1123 char etag
[33] = { 0 };
1124 unsigned char digest
[16];
1126 if (mode
& PHP_OUTPUT_HANDLER_START
) {
1127 PHP_MD5Init(&HTTP_G(etag_md5
));
1130 PHP_MD5Update(&HTTP_G(etag_md5
), output
, output_len
);
1132 if (mode
& PHP_OUTPUT_HANDLER_END
) {
1133 PHP_MD5Final(digest
, &HTTP_G(etag_md5
));
1135 /* just do that if desired */
1136 if (HTTP_G(etag_started
)) {
1137 make_digest(etag
, digest
);
1139 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
1140 http_send_status(304);
1142 http_send_etag(etag
, 32);
1147 *handled_output_len
= output_len
;
1148 *handled_output
= estrndup(output
, output_len
);
1152 /* {{{ STATUS http_start_ob_handler(php_output_handler_func_t, char *, uint, zend_bool) */
1153 PHP_HTTP_API STATUS
_http_start_ob_handler(php_output_handler_func_t handler_func
,
1154 char *handler_name
, uint chunk_size
, zend_bool erase TSRMLS_DC
)
1156 php_ob_buffer
**stack
;
1159 if (count
= OG(ob_nesting_level
)) {
1160 stack
= ecalloc(sizeof(php_ob_buffer
*), count
);
1163 zend_stack_apply_with_argument(&OG(ob_buffers
), ZEND_STACK_APPLY_BOTTOMUP
,
1164 (int (*)(void *elem
, void *)) http_ob_stack_get
, stack
);
1168 http_ob_stack_get(&OG(active_ob_buffer
), stack
);
1171 while (OG(ob_nesting_level
)) {
1172 php_end_ob_buffer(0, 0 TSRMLS_CC
);
1176 php_ob_set_internal_handler(handler_func
, chunk_size
, handler_name
, erase TSRMLS_CC
);
1178 for (i
= 0; i
< count
; i
++) {
1179 php_ob_buffer
*s
= stack
[i
];
1180 if (strcmp(s
->handler_name
, "default output handler")) {
1181 php_start_ob_buffer_named(s
->handler_name
, s
->chunk_size
, s
->erase TSRMLS_CC
);
1183 php_body_write(s
->buffer
, s
->text_length TSRMLS_CC
);
1184 efree(s
->handler_name
);
1196 /* {{{ int http_modified_match(char *, int) */
1197 PHP_HTTP_API
int _http_modified_match(const char *entry
, const time_t t TSRMLS_DC
)
1201 char *modified
, *chr_ptr
;
1203 HTTP_GSC(zmodified
, entry
, 0);
1205 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
1206 if (chr_ptr
= strrchr(modified
, ';')) {
1209 retval
= (t
<= http_parse_date(modified
));
1215 /* {{{ int http_etag_match(char *, char *) */
1216 PHP_HTTP_API
int _http_etag_match(const char *entry
, const char *etag TSRMLS_DC
)
1222 HTTP_GSC(zetag
, entry
, 0);
1224 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
1228 quoted_etag
= (char *) emalloc(strlen(etag
) + 3);
1229 sprintf(quoted_etag
, "\"%s\"", etag
);
1231 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
1232 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
1234 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
1241 /* {{{ STATUS http_send_last_modified(int) */
1242 PHP_HTTP_API STATUS
_http_send_last_modified(const time_t t TSRMLS_DC
)
1244 char modified
[96] = "Last-Modified: ", *date
;
1245 date
= http_date(t
);
1246 strcat(modified
, date
);
1252 return http_send_header(modified
);
1256 /* {{{ static STATUS http_send_etag(char *, int) */
1257 PHP_HTTP_API STATUS
_http_send_etag(const char *etag
,
1258 const int etag_len TSRMLS_DC
)
1264 php_error_docref(NULL TSRMLS_CC
,E_ERROR
,
1265 "Attempt to send empty ETag (previous: %s)\n", HTTP_G(etag
));
1271 efree(HTTP_G(etag
));
1273 HTTP_G(etag
) = estrdup(etag
);
1275 etag_header
= ecalloc(sizeof("ETag: \"\"") + etag_len
, 1);
1276 sprintf(etag_header
, "ETag: \"%s\"", etag
);
1277 if (SUCCESS
!= (status
= http_send_header(etag_header
))) {
1278 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", etag_header
);
1285 /* {{{ STATUS http_send_cache_control(char *, size_t) */
1286 PHP_HTTP_API STATUS
_http_send_cache_control(const char *cache_control
,
1287 const size_t cc_len TSRMLS_DC
)
1290 char *cc_header
= ecalloc(sizeof("Cache-Control: ") + cc_len
, 1);
1292 sprintf(cc_header
, "Cache-Control: %s", cache_control
);
1293 if (SUCCESS
!= (status
= http_send_header(cc_header
))) {
1294 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
,
1295 "Could not send '%s' header", cc_header
);
1302 /* {{{ STATUS http_send_content_type(char *, size_t) */
1303 PHP_HTTP_API STATUS
_http_send_content_type(const char *content_type
,
1304 const size_t ct_len TSRMLS_DC
)
1309 if (!strchr(content_type
, '/')) {
1310 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1311 "Content-Type '%s' doesn't seem to consist of a primary and a secondary part",
1316 /* remember for multiple ranges */
1317 if (HTTP_G(ctype
)) {
1318 efree(HTTP_G(ctype
));
1320 HTTP_G(ctype
) = estrndup(content_type
, ct_len
);
1322 ct_header
= ecalloc(sizeof("Content-Type: ") + ct_len
, 1);
1323 sprintf(ct_header
, "Content-Type: %s", content_type
);
1325 if (SUCCESS
!= (status
= http_send_header(ct_header
))) {
1326 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1327 "Couldn't send '%s' header", ct_header
);
1334 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
1335 PHP_HTTP_API STATUS
_http_send_content_disposition(const char *filename
,
1336 const size_t f_len
, const int send_inline TSRMLS_DC
)
1342 cd_header
= ecalloc(sizeof("Content-Disposition: inline; filename=\"\"") + f_len
, 1);
1343 sprintf(cd_header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
1345 cd_header
= ecalloc(sizeof("Content-Disposition: attachment; filename=\"\"") + f_len
, 1);
1346 sprintf(cd_header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
1349 if (SUCCESS
!= (status
= http_send_header(cd_header
))) {
1350 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", cd_header
);
1357 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
1358 PHP_HTTP_API STATUS
_http_cache_last_modified(const time_t last_modified
,
1359 const time_t send_modified
, const char *cache_control
, const size_t cc_len TSRMLS_DC
)
1362 http_send_cache_control(cache_control
, cc_len
);
1365 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
1366 if (SUCCESS
== http_send_status(304)) {
1369 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
1373 return http_send_last_modified(send_modified
);
1377 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
1378 PHP_HTTP_API STATUS
_http_cache_etag(const char *etag
, const size_t etag_len
,
1379 const char *cache_control
, const size_t cc_len TSRMLS_DC
)
1382 http_send_cache_control(cache_control
, cc_len
);
1386 http_send_etag(etag
, etag_len
);
1387 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
1388 if (SUCCESS
== http_send_status(304)) {
1391 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
1397 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
1398 if (!HTTP_G(etag_started
)) {
1399 if (SUCCESS
== http_start_ob_handler(_http_ob_etaghandler
, "ob_etaghandler", 4096, 1)) {
1400 HTTP_G(etag_started
) = 1;
1403 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not start ob_etaghandler");
1411 /* {{{ char *http_absolute_uri(char *, char *) */
1412 PHP_HTTP_API
char *_http_absolute_uri(const char *url
,
1413 const char *proto TSRMLS_DC
)
1415 char URI
[HTTP_URI_MAXLEN
+ 1], *PTR
, *proto_ptr
, *host
, *path
;
1418 if (!url
|| !strlen(url
)) {
1419 if (!SG(request_info
).request_uri
) {
1422 url
= SG(request_info
).request_uri
;
1424 /* Mess around with already absolute URIs */
1425 else if (proto_ptr
= strstr(url
, "://")) {
1426 if (!proto
|| !strncmp(url
, proto
, strlen(proto
))) {
1427 return estrdup(url
);
1429 snprintf(URI
, HTTP_URI_MAXLEN
, "%s%s", proto
, proto_ptr
+ 3);
1430 return estrdup(URI
);
1434 /* protocol defaults to http */
1435 if (!proto
|| !strlen(proto
)) {
1440 if ( (zhost
= http_get_server_var("HTTP_HOST")) ||
1441 (zhost
= http_get_server_var("SERVER_NAME"))) {
1442 host
= Z_STRVAL_P(zhost
);
1449 if (url
[0] == '/') {
1450 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s%s", proto
, host
, url
);
1451 } else if (SG(request_info
).request_uri
) {
1452 path
= estrdup(SG(request_info
).request_uri
);
1453 php_dirname(path
, strlen(path
));
1454 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s%s/%s", proto
, host
, path
, url
);
1457 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s/%s", proto
, host
, url
);
1460 /* strip everything after a new line */
1463 if (*PTR
== '\n' || *PTR
== '\r') {
1470 return estrdup(URI
);
1474 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
1475 PHP_HTTP_API
char *_http_negotiate_q(const char *entry
, const zval
*supported
,
1476 const char *def TSRMLS_DC
)
1478 zval
*zaccept
, *zarray
, *zdelim
, **zentry
, *zentries
, **zsupp
;
1479 char *q_ptr
, *result
;
1483 HTTP_GSC(zaccept
, entry
, estrdup(def
));
1485 MAKE_STD_ZVAL(zarray
);
1488 MAKE_STD_ZVAL(zdelim
);
1489 ZVAL_STRING(zdelim
, ",", 0);
1490 php_explode(zdelim
, zaccept
, zarray
, -1);
1493 MAKE_STD_ZVAL(zentries
);
1494 array_init(zentries
);
1496 c
= zend_hash_num_elements(Z_ARRVAL_P(zarray
));
1497 for (i
= 0; i
< c
; i
++, zend_hash_move_forward(Z_ARRVAL_P(zarray
))) {
1499 if (SUCCESS
!= zend_hash_get_current_data(
1500 Z_ARRVAL_P(zarray
), (void **) &zentry
)) {
1501 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1502 "Cannot parse %s header: %s", entry
, Z_STRVAL_P(zaccept
));
1506 /* check for qualifier */
1507 if (NULL
!= (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';'))) {
1508 qual
= strtod(q_ptr
+ 3, NULL
);
1513 /* walk through the supported array */
1514 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported
));
1515 SUCCESS
== zend_hash_get_current_data(
1516 Z_ARRVAL_P(supported
), (void **) &zsupp
);
1517 zend_hash_move_forward(Z_ARRVAL_P(supported
))) {
1518 if (!strcasecmp(Z_STRVAL_PP(zsupp
), Z_STRVAL_PP(zentry
))) {
1519 add_assoc_double(zentries
, Z_STRVAL_PP(zsupp
), qual
);
1528 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries
));
1530 if ( (SUCCESS
!= zend_hash_sort(Z_ARRVAL_P(zentries
), zend_qsort
,
1531 http_sort_q
, 0 TSRMLS_CC
)) ||
1532 (HASH_KEY_NON_EXISTANT
== zend_hash_get_current_key(
1533 Z_ARRVAL_P(zentries
), &result
, 0, 1))) {
1534 result
= estrdup(def
);
1537 zval_dtor(zentries
);
1544 /* {{{ http_range_status http_get_request_ranges(zval *zranges, size_t) */
1545 PHP_HTTP_API http_range_status
_http_get_request_ranges(zval
*zranges
,
1546 const size_t length TSRMLS_DC
)
1550 long begin
= -1, end
= -1, *ptr
;
1552 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
1553 range
= Z_STRVAL_P(zrange
);
1555 if (strncmp(range
, "bytes=", strlen("bytes="))) {
1556 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Range header misses bytes=");
1561 range
+= strlen("bytes=");
1564 switch (c
= *(range
++))
1570 case '1': case '2': case '3':
1571 case '4': case '5': case '6':
1572 case '7': case '8': case '9':
1574 * If the value of the pointer is already set (non-negative)
1575 * then multiply its value by ten and add the current value,
1576 * else initialise the pointers value with the current value
1578 * This let us recognize empty fields when validating the
1579 * ranges, i.e. a "-10" for begin and "12345" for the end
1580 * was the following range request: "Range: bytes=0-12345";
1581 * While a "-1" for begin and "12345" for the end would
1582 * have been: "Range: bytes=-12345".
1597 /* IE - ignore for now */
1604 /* validate ranges */
1609 if ((length
- end
) < 1) {
1617 if ((length
- end
) < 1) {
1620 begin
= length
- end
;
1630 if ((length
- begin
) < 1) {
1638 if ( ((length
- begin
) < 1) ||
1639 ((length
- end
) < 1) ||
1640 ((begin
- end
) >= 0)) {
1650 MAKE_STD_ZVAL(zentry
);
1652 add_index_long(zentry
, 0, begin
);
1653 add_index_long(zentry
, 1, end
);
1654 add_next_index_zval(zranges
, zentry
);
1672 /* {{{ STATUS http_send_ranges(zval *, void *, size_t, http_send_mode) */
1673 PHP_HTTP_API STATUS
_http_send_ranges(zval
*zranges
, const void *data
, const size_t size
, const http_send_mode mode TSRMLS_DC
)
1676 long **begin
, **end
;
1679 /* Send HTTP 206 Partial Content */
1680 http_send_status(206);
1683 if ((c
= zend_hash_num_elements(Z_ARRVAL_P(zranges
))) == 1) {
1684 char range_header
[256] = {0};
1686 zend_hash_index_find(Z_ARRVAL_P(zranges
), 0, (void **) &zrange
);
1687 zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
);
1688 zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
);
1690 /* send content range header */
1691 snprintf(range_header
, 255, "Content-Range: bytes %d-%d/%d", **begin
, **end
, size
);
1692 http_send_header(range_header
);
1694 /* send requested chunk */
1695 return http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1701 char bound
[23] = {0}, preface
[1024] = {0},
1702 multi_header
[68] = "Content-Type: multipart/byteranges; boundary=";
1704 snprintf(bound
, 22, "--%d%0.9f", time(NULL
), php_combined_lcg(TSRMLS_C
));
1705 strncat(multi_header
, bound
+ 2, 21);
1706 http_send_header(multi_header
);
1708 /* send each requested chunk */
1709 for ( i
= 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(zranges
));
1711 i
++, zend_hash_move_forward(Z_ARRVAL_P(zranges
))) {
1712 if ( HASH_KEY_NON_EXISTANT
== zend_hash_get_current_data(
1713 Z_ARRVAL_P(zranges
), (void **) &zrange
) ||
1714 SUCCESS
!= zend_hash_index_find(
1715 Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1716 SUCCESS
!= zend_hash_index_find(
1717 Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1721 snprintf(preface
, 1023,
1723 HTTP_CRLF
"Content-Type: %s"
1724 HTTP_CRLF
"Content-Range: bytes %ld-%ld/%ld"
1729 HTTP_G(ctype
) ? HTTP_G(ctype
) : "application/x-octetstream",
1735 php_body_write(preface
, strlen(preface
) TSRMLS_CC
);
1736 http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1739 /* write boundary once more */
1740 php_body_write(HTTP_CRLF
, 2 TSRMLS_CC
);
1741 php_body_write(bound
, strlen(bound
) TSRMLS_CC
);
1748 /* {{{ STATUS http_send(void *, sizezo_t, http_send_mode) */
1749 PHP_HTTP_API STATUS
_http_send(const void *data_ptr
, const size_t data_size
,
1750 const http_send_mode data_mode TSRMLS_DC
)
1752 int is_range_request
= http_is_range_request();
1759 if (HTTP_G(etag_started
)) {
1762 HTTP_G(etag_started
) = 0;
1763 /* never ever use the output to compute the ETag if http_send() is used */
1764 php_end_ob_buffer(0, 0 TSRMLS_CC
);
1765 if (!(etag
= http_etag(data_ptr
, data_size
, data_mode
))) {
1769 /* send 304 Not Modified if etag matches */
1770 if ((!is_range_request
) && http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
1772 return http_send_status(304);
1775 http_send_etag(etag
, 32);
1779 /* send 304 Not Modified if last-modified matches*/
1780 if ((!is_range_request
) && http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod
))) {
1781 return http_send_status(304);
1784 if (is_range_request
) {
1786 /* only send ranges if entity hasn't changed */
1788 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_MATCH", 13)) ||
1789 http_etag_match("HTTP_IF_MATCH", HTTP_G(etag
)))
1791 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
1792 http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod
)))
1795 STATUS result
= FAILURE
;
1796 zval
*zranges
= NULL
;
1797 MAKE_STD_ZVAL(zranges
);
1798 array_init(zranges
);
1800 switch (http_get_request_ranges(zranges
, data_size
))
1805 /* go ahead and send all */
1809 result
= http_send_ranges(zranges
, data_ptr
, data_size
, data_mode
);
1818 http_send_status(416);
1829 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
1833 /* {{{ STATUS http_send_data(zval *) */
1834 PHP_HTTP_API STATUS
_http_send_data(const zval
*zdata TSRMLS_DC
)
1836 if (!Z_STRLEN_P(zdata
)) {
1839 if (!Z_STRVAL_P(zdata
)) {
1843 return http_send(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
), SEND_DATA
);
1847 /* {{{ STATUS http_send_stream(php_stream *) */
1848 PHP_HTTP_API STATUS
_http_send_stream(const php_stream
*file TSRMLS_DC
)
1850 if (php_stream_stat((php_stream
*) file
, &HTTP_G(ssb
))) {
1854 return http_send(file
, HTTP_G(ssb
).sb
.st_size
, SEND_RSRC
);
1858 /* {{{ STATUS http_send_file(zval *) */
1859 PHP_HTTP_API STATUS
_http_send_file(const zval
*zfile TSRMLS_DC
)
1864 if (!Z_STRLEN_P(zfile
)) {
1868 if (!(file
= php_stream_open_wrapper(Z_STRVAL_P(zfile
), "rb",
1869 REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
))) {
1873 ret
= http_send_stream(file
);
1874 php_stream_close(file
);
1879 /* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1880 PHP_HTTP_API STATUS
_http_chunked_decode(const char *encoded
,
1881 const size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
1887 *decoded
= (char *) ecalloc(encoded_len
, 1);
1891 while (((e_ptr
- encoded
) - encoded_len
) > 0) {
1892 char hex_len
[9] = {0};
1893 size_t chunk_len
= 0;
1896 /* read in chunk size */
1897 while (isxdigit(*e_ptr
)) {
1899 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1900 "Chunk size is too long: 0x%s...", hex_len
);
1904 hex_len
[i
++] = *e_ptr
++;
1907 /* reached the end */
1908 if (!strcmp(hex_len
, "0")) {
1913 if (strncmp(e_ptr
, HTTP_CRLF
, 2)) {
1914 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1915 "Invalid character (expected 0x0D 0x0A; got: %x %x)",
1916 *e_ptr
, *(e_ptr
+ 1));
1924 chunk_len
= strtol(hex_len
, &error
, 16);
1925 if (error
== hex_len
) {
1926 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1927 "Invalid chunk size string: '%s'", hex_len
);
1933 memcpy(d_ptr
, e_ptr
+= 2, chunk_len
);
1935 e_ptr
+= chunk_len
+ 2;
1936 *decoded_len
+= chunk_len
;
1943 /* {{{ proto STATUS http_split_response_ex(char *, size_t, zval *, zval *) */
1944 PHP_HTTP_API STATUS
_http_split_response_ex( char *response
,
1945 size_t response_len
, zval
*zheaders
, zval
*zbody TSRMLS_DC
)
1948 char *header
= response
;
1950 while ((response
- header
+ 4) < response_len
) {
1951 if ( (*response
++ == '\r') &&
1952 (*response
++ == '\n') &&
1953 (*response
++ == '\r') &&
1954 (*response
++ == '\n')) {
1960 if (body
&& (response_len
- (body
- header
))) {
1961 ZVAL_STRINGL(zbody
, body
, response_len
- (body
- header
) - 1, 1);
1963 Z_TYPE_P(zbody
) = IS_NULL
;
1966 return http_parse_headers(header
, body
? body
- header
: response_len
, zheaders
);
1970 /* {{{ STATUS http_parse_headers(char *, long, zval *) */
1971 PHP_HTTP_API STATUS
_http_parse_headers(char *header
, int header_len
, zval
*array TSRMLS_DC
)
1973 char *colon
= NULL
, *line
= NULL
, *begin
= header
;
1975 if (header_len
< 2) {
1980 if (!strncmp(header
, "HTTP/1.", 7)) {
1981 char *end
= strstr(header
, HTTP_CRLF
);
1982 add_assoc_stringl(array
, "Status",
1983 header
+ strlen("HTTP/1.x "),
1984 end
- (header
+ strlen("HTTP/1.x ")), 1);
1990 while (header_len
>= (line
- begin
)) {
1996 --value_len
; /* we don't have CR so value length is one char less */
1998 if (colon
&& ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t')))) {
2000 /* skip empty key */
2001 if (header
!= colon
) {
2002 char *key
= estrndup(header
, colon
- header
);
2003 value_len
+= line
- colon
- 1;
2005 /* skip leading ws */
2006 while (isspace(*(++colon
))) --value_len
;
2007 /* skip trailing ws */
2008 while (isspace(colon
[value_len
- 1])) --value_len
;
2010 if (value_len
< 1) {
2011 /* hm, empty header? */
2012 add_assoc_stringl(array
, key
, "", 0, 1);
2014 add_assoc_stringl(array
, key
, colon
, value_len
, 1);
2021 header
+= line
- header
;
2036 /* {{{ void http_get_request_headers(zval *) */
2037 PHP_HTTP_API
void _http_get_request_headers(zval
*array TSRMLS_DC
)
2041 for ( zend_hash_internal_pointer_reset(HTTP_SERVER_VARS
);
2042 zend_hash_get_current_key(HTTP_SERVER_VARS
, &key
, NULL
, 0) != HASH_KEY_NON_EXISTANT
;
2043 zend_hash_move_forward(HTTP_SERVER_VARS
)) {
2044 if (!strncmp(key
, "HTTP_", 5)) {
2046 zend_hash_get_current_data(HTTP_SERVER_VARS
, (void **) &header
);
2047 add_assoc_stringl(array
, pretty_key(key
+ 5, strlen(key
) - 5, 1, 1), Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
2054 #ifdef HTTP_HAVE_CURL
2056 /* {{{ STATUS http_get(char *, HashTable *, HashTable *, char **, size_t *) */
2057 PHP_HTTP_API STATUS
_http_get(const char *URL
, HashTable
*options
,
2058 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
2061 CURL
*ch
= curl_easy_init();
2064 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
2068 rs
= http_get_ex(ch
, URL
, options
, info
, data
, data_len
);
2069 curl_easy_cleanup(ch
);
2074 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, char **, size_t *) */
2075 PHP_HTTP_API STATUS
_http_get_ex(CURL
*ch
, const char *URL
, HashTable
*options
,
2076 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
2078 http_curl_initbuf(CURLBUF_EVRY
);
2079 http_curl_setopts(ch
, URL
, options
);
2080 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 0);
2081 curl_easy_setopt(ch
, CURLOPT_POST
, 0);
2083 if (CURLE_OK
!= curl_easy_perform(ch
)) {
2084 http_curl_freebuf(CURLBUF_EVRY
);
2085 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
2089 http_curl_getinfo(ch
, info
);
2091 http_curl_movebuf(CURLBUF_EVRY
, data
, data_len
);
2095 /* {{{ STATUS http_head(char *, HashTable *, HashTable *, char **data, size_t *) */
2096 PHP_HTTP_API STATUS
_http_head(const char *URL
, HashTable
*options
,
2097 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
2100 CURL
*ch
= curl_easy_init();
2103 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
2107 rs
= http_head_ex(ch
, URL
, options
, info
, data
, data_len
);
2108 curl_easy_cleanup(ch
);
2113 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, char **data, size_t *) */
2114 PHP_HTTP_API STATUS
_http_head_ex(CURL
*ch
, const char *URL
, HashTable
*options
,
2115 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
2117 http_curl_initbuf(CURLBUF_HDRS
);
2118 http_curl_setopts(ch
, URL
, options
);
2119 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
2120 curl_easy_setopt(ch
, CURLOPT_POST
, 0);
2122 if (CURLE_OK
!= curl_easy_perform(ch
)) {
2123 http_curl_freebuf(CURLBUF_HDRS
);
2124 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
2128 http_curl_getinfo(ch
, info
);
2130 http_curl_movebuf(CURLBUF_HDRS
, data
, data_len
);
2134 /* {{{ STATUS http_post_data(char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
2135 PHP_HTTP_API STATUS
_http_post_data(const char *URL
, char *postdata
,
2136 size_t postdata_len
, HashTable
*options
, HashTable
*info
, char **data
,
2137 size_t *data_len TSRMLS_DC
)
2140 CURL
*ch
= curl_easy_init();
2143 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
2146 rs
= http_post_data_ex(ch
, URL
, postdata
, postdata_len
, options
, info
, data
, data_len
);
2147 curl_easy_cleanup(ch
);
2152 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
2153 PHP_HTTP_API STATUS
_http_post_data_ex(CURL
*ch
, const char *URL
, char *postdata
,
2154 size_t postdata_len
, HashTable
*options
, HashTable
*info
, char **data
,
2155 size_t *data_len TSRMLS_DC
)
2157 http_curl_initbuf(CURLBUF_EVRY
);
2158 http_curl_setopts(ch
, URL
, options
);
2159 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
2160 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, postdata
);
2161 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, postdata_len
);
2163 if (CURLE_OK
!= curl_easy_perform(ch
)) {
2164 http_curl_freebuf(CURLBUF_EVRY
);
2165 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
2169 http_curl_getinfo(ch
, info
);
2171 http_curl_movebuf(CURLBUF_EVRY
, data
, data_len
);
2176 /* {{{ STATUS http_post_array(char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
2177 PHP_HTTP_API STATUS
_http_post_array(const char *URL
, HashTable
*postarray
,
2178 HashTable
*options
, HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
2180 smart_str qstr
= {0};
2183 if (php_url_encode_hash_ex(postarray
, &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
2187 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode post data");
2192 status
= http_post_data(URL
, qstr
.c
, qstr
.len
, options
, info
, data
, data_len
);
2203 /* {{{ STATUS http_auth_header(char *, char*) */
2204 PHP_HTTP_API STATUS
_http_auth_header(const char *type
, const char *realm TSRMLS_DC
)
2206 char realm_header
[1024] = {0};
2207 snprintf(realm_header
, 1023, "WWW-Authenticate: %s realm=\"%s\"", type
, realm
);
2208 return http_send_status_header(401, realm_header
);
2212 /* {{{ STATUS http_auth_credentials(char **, char **) */
2213 PHP_HTTP_API STATUS
_http_auth_credentials(char **user
, char **pass TSRMLS_DC
)
2215 if (strncmp(sapi_module
.name
, "isapi", 5)) {
2216 zval
*zuser
, *zpass
;
2218 HTTP_GSC(zuser
, "PHP_AUTH_USER", FAILURE
);
2219 HTTP_GSC(zpass
, "PHP_AUTH_PW", FAILURE
);
2221 *user
= estrndup(Z_STRVAL_P(zuser
), Z_STRLEN_P(zuser
));
2222 *pass
= estrndup(Z_STRVAL_P(zpass
), Z_STRLEN_P(zpass
));
2227 HTTP_GSC(zauth
, "HTTP_AUTHORIZATION", FAILURE
);
2229 char *decoded
, *colon
;
2231 decoded
= php_base64_decode(Z_STRVAL_P(zauth
), Z_STRLEN_P(zauth
),
2234 if (colon
= strchr(decoded
+ 6, ':')) {
2235 *user
= estrndup(decoded
+ 6, colon
- decoded
- 6);
2236 *pass
= estrndup(colon
+ 1, decoded
+ decoded_len
- colon
- 6 - 1);
2247 /* }}} public API */
2254 * vim600: noet sw=4 ts=4 fdm=marker
2255 * vim<600: noet sw=4 ts=4