2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
28 #include "php_version.h"
29 #include "php_streams.h"
31 #include "ext/standard/md5.h"
32 #include "ext/standard/url.h"
33 #include "ext/standard/base64.h"
34 #include "ext/standard/php_string.h"
35 #include "ext/standard/php_smart_str.h"
36 #include "ext/standard/php_lcg.h"
41 # include "ext/standard/php_http.h"
43 #include "http_build_query.c"
47 #include "php_http_api.h"
49 ZEND_DECLARE_MODULE_GLOBALS(http
)
51 /* {{{ day/month names */
52 static const char *days
[] = {
53 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
55 static const char *wkdays
[] = {
56 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
58 static const char *weekdays
[] = {
59 "Monday", "Tuesday", "Wednesday",
60 "Thursday", "Friday", "Saturday", "Sunday"
62 static const char *months
[] = {
63 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
64 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
71 static const struct time_zone
{
75 {"GMT", 0}, /* Greenwich Mean */
76 {"UTC", 0}, /* Universal (Coordinated) */
77 {"WET", 0}, /* Western European */
78 {"BST", 0}, /* British Summer */
79 {"WAT", 60}, /* West Africa */
80 {"AST", 240}, /* Atlantic Standard */
81 {"ADT", 240}, /* Atlantic Daylight */
82 {"EST", 300}, /* Eastern Standard */
83 {"EDT", 300}, /* Eastern Daylight */
84 {"CST", 360}, /* Central Standard */
85 {"CDT", 360}, /* Central Daylight */
86 {"MST", 420}, /* Mountain Standard */
87 {"MDT", 420}, /* Mountain Daylight */
88 {"PST", 480}, /* Pacific Standard */
89 {"PDT", 480}, /* Pacific Daylight */
90 {"YST", 540}, /* Yukon Standard */
91 {"YDT", 540}, /* Yukon Daylight */
92 {"HST", 600}, /* Hawaii Standard */
93 {"HDT", 600}, /* Hawaii Daylight */
94 {"CAT", 600}, /* Central Alaska */
95 {"AHST", 600}, /* Alaska-Hawaii Standard */
96 {"NT", 660}, /* Nome */
97 {"IDLW", 720}, /* International Date Line West */
98 {"CET", -60}, /* Central European */
99 {"MET", -60}, /* Middle European */
100 {"MEWT", -60}, /* Middle European Winter */
101 {"MEST", -120}, /* Middle European Summer */
102 {"CEST", -120}, /* Central European Summer */
103 {"MESZ", -60}, /* Middle European Summer */
104 {"FWT", -60}, /* French Winter */
105 {"FST", -60}, /* French Summer */
106 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
107 {"WAST", -420}, /* West Australian Standard */
108 {"WADT", -420}, /* West Australian Daylight */
109 {"CCT", -480}, /* China Coast, USSR Zone 7 */
110 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
111 {"EAST", -600}, /* Eastern Australian Standard */
112 {"EADT", -600}, /* Eastern Australian Daylight */
113 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
114 {"NZT", -720}, /* New Zealand */
115 {"NZST", -720}, /* New Zealand Standard */
116 {"NZDT", -720}, /* New Zealand Daylight */
117 {"IDLE", -720}, /* International Date Line East */
123 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
);
124 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
125 static STATUS
_http_send_chunk(const void *data
, const size_t begin
, const size_t end
, const http_send_mode mode TSRMLS_DC
);
127 static int check_day(char *day
, size_t len
);
128 static int check_month(char *month
);
129 static int check_tzone(char *tzone
);
132 static int http_ob_stack_get(php_ob_buffer
*, php_ob_buffer
**);
134 /* {{{ static int http_sort_q(const void *, const void *) */
135 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
138 zval result
, *first
, *second
;
140 f
= *((Bucket
**) a
);
141 s
= *((Bucket
**) b
);
143 first
= *((zval
**) f
->pData
);
144 second
= *((zval
**) s
->pData
);
146 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
149 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
153 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
155 static STATUS
_http_send_chunk(const void *data
, const size_t begin
,
156 const size_t end
, const http_send_mode mode TSRMLS_DC
)
160 long len
= end
- begin
;
166 s
= (php_stream
*) data
;
167 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
170 buf
= (char *) ecalloc(1, HTTP_BUF_SIZE
);
171 /* read into buf and write out */
172 while ((len
-= HTTP_BUF_SIZE
) >= 0) {
173 if (!(read
= php_stream_read(s
, buf
, HTTP_BUF_SIZE
))) {
177 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
183 /* read & write left over */
185 if (read
= php_stream_read(s
, buf
, HTTP_BUF_SIZE
+ len
)) {
186 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
200 return len
== php_body_write(((char *)data
) + begin
, len TSRMLS_CC
)
211 /* {{{ Day/Month/TZ checks for http_parse_date()
212 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
213 static int check_day(char *day
, size_t len
)
216 const char * const *check
= (len
> 3) ? &weekdays
[0] : &wkdays
[0];
217 for (i
= 0; i
< 7; i
++) {
218 if (!strcmp(day
, check
[0])) {
226 static int check_month(char *month
)
229 const char * const *check
= &months
[0];
230 for (i
= 0; i
< 12; i
++) {
231 if (!strcmp(month
, check
[0])) {
239 /* return the time zone offset between GMT and the input one, in number
240 of seconds or -1 if the timezone wasn't found/legal */
242 static int check_tzone(char *tzone
)
245 const struct time_zone
*check
= time_zones
;
246 for (i
= 0; i
< sizeof(time_zones
) / sizeof(time_zones
[0]); i
++) {
247 if (!strcmp(tzone
, check
->name
)) {
248 return check
->offset
* 60;
256 /* char *pretty_key(char *, int, int, int) */
257 char *pretty_key(char *key
, int key_len
, int uctitle
, int xhyphen
)
259 if (key
&& key_len
) {
261 if (wasalpha
= isalpha(key
[0])) {
262 key
[0] = uctitle
? toupper(key
[0]) : tolower(key
[0]);
264 for (i
= 1; i
< key_len
; i
++) {
265 if (isalpha(key
[i
])) {
266 key
[i
] = ((!wasalpha
) && uctitle
) ? toupper(key
[i
]) : tolower(key
[i
]);
269 if (xhyphen
&& (key
[i
] == '_')) {
280 /* {{{ static STATUS http_ob_stack_get(php_ob_buffer *, php_ob_buffer **) */
281 static STATUS
http_ob_stack_get(php_ob_buffer
*o
, php_ob_buffer
**s
)
284 php_ob_buffer
*b
= emalloc(sizeof(php_ob_buffer
));
285 b
->handler_name
= estrdup(o
->handler_name
);
286 b
->buffer
= estrndup(o
->buffer
, o
->text_length
);
287 b
->text_length
= o
->text_length
;
288 b
->chunk_size
= o
->chunk_size
;
299 /* {{{ char *http_date(time_t) */
300 PHP_HTTP_API
char *_http_date(time_t t TSRMLS_DC
)
302 struct tm
*gmtime
, tmbuf
;
303 char *date
= ecalloc(1, 31);
305 gmtime
= php_gmtime_r(&t
, &tmbuf
);
307 "%s, %02d %s %04d %02d:%02d:%02d GMT",
308 days
[gmtime
->tm_wday
], gmtime
->tm_mday
,
309 months
[gmtime
->tm_mon
], gmtime
->tm_year
+ 1900,
310 gmtime
->tm_hour
, gmtime
->tm_min
, gmtime
->tm_sec
316 /* {{{ time_t http_parse_date(char *)
317 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
318 PHP_HTTP_API
time_t _http_parse_date(const char *date
)
321 int tz_offset
= -1, year
= -1, month
= -1, monthday
= -1, weekday
= -1,
322 hours
= -1, minutes
= -1, seconds
= -1;
324 enum assume_next dignext
= DATE_MDAY
;
325 const char *indate
= date
;
327 int found
= 0, part
= 0; /* max 6 parts */
329 while (*date
&& (part
< 6)) {
332 while (*date
&& !isalnum(*date
)) {
336 if (isalpha(*date
)) {
337 /* a name coming up */
340 sscanf(date
, "%31[A-Za-z]", buf
);
344 weekday
= check_day(buf
, len
);
350 if (!found
&& (month
== -1)) {
351 month
= check_month(buf
);
357 if (!found
&& (tz_offset
== -1)) {
358 /* this just must be a time zone string */
359 tz_offset
= check_tzone(buf
);
360 if (tz_offset
!= -1) {
366 return -1; /* bad string */
370 else if (isdigit(*date
)) {
374 if((seconds
== -1) && (3 == sscanf(date
, "%02d:%02d:%02d", &hours
, &minutes
, &seconds
))) {
380 val
= (int) strtol(date
, &end
, 10);
382 if ((tz_offset
== -1) && ((end
- date
) == 4) && (val
< 1300) && (indate
< date
) && ((date
[-1] == '+' || date
[-1] == '-'))) {
383 /* four digits and a value less than 1300 and it is preceeded with
384 a plus or minus. This is a time zone indication. */
386 tz_offset
= (val
/ 100 * 60 + val
% 100) * 60;
388 /* the + and - prefix indicates the local time compared to GMT,
389 this we need ther reversed math to get what we want */
390 tz_offset
= date
[-1] == '+' ? -tz_offset
: tz_offset
;
393 if (((end
- date
) == 8) && (year
== -1) && (month
== -1) && (monthday
== -1)) {
394 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
397 month
= (val
% 10000) / 100 - 1; /* month is 0 - 11 */
398 monthday
= val
% 100;
401 if (!found
&& (dignext
== DATE_MDAY
) && (monthday
== -1)) {
402 if ((val
> 0) && (val
< 32)) {
409 if (!found
&& (dignext
== DATE_YEAR
) && (year
== -1)) {
413 year
+= year
> 70 ? 1900 : 2000;
432 seconds
= minutes
= hours
= 0; /* no time, make it zero */
435 if ((-1 == monthday
) || (-1 == month
) || (-1 == year
)) {
436 /* lacks vital info, fail */
440 if (sizeof(time_t) < 5) {
441 /* 32 bit time_t can only hold dates to the beginning of 2038 */
450 tm
.tm_mday
= monthday
;
452 tm
.tm_year
= year
- 1900;
459 /* time zone adjust */
461 struct tm
*gmt
, keeptime2
;
465 if(!(gmt
= php_gmtime_r(&t
, &keeptime2
))) {
466 return -1; /* illegal date/time */
471 /* Add the time zone diff (between the given timezone and GMT) and the
472 diff between the local time zone and GMT. */
473 delta
= (tz_offset
!= -1 ? tz_offset
: 0) + (t
- t2
);
475 if((delta
> 0) && (t
+ delta
< t
)) {
476 return -1; /* time_t overflow */
486 /* {{{ inline char *http_etag(void *, size_t, http_send_mode) */
487 PHP_HTTP_API
inline char *_http_etag(const void *data_ptr
, const size_t data_len
,
488 const http_send_mode data_mode TSRMLS_DC
)
490 char ssb_buf
[128] = {0};
491 unsigned char digest
[16];
493 char *new_etag
= ecalloc(1, 33);
500 PHP_MD5Update(&ctx
, data_ptr
, data_len
);
504 if (!HTTP_G(ssb
).sb
.st_ino
) {
505 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
509 snprintf(ssb_buf
, 127, "%ld=%ld=%ld",
510 HTTP_G(ssb
).sb
.st_mtime
,
511 HTTP_G(ssb
).sb
.st_ino
,
512 HTTP_G(ssb
).sb
.st_size
514 PHP_MD5Update(&ctx
, ssb_buf
, strlen(ssb_buf
));
523 PHP_MD5Final(digest
, &ctx
);
524 make_digest(new_etag
, digest
);
530 /* {{{ inline http_lmod(void *, http_send_mode) */
531 PHP_HTTP_API
inline time_t _http_lmod(const void *data_ptr
, const http_send_mode data_mode TSRMLS_DC
)
542 if (!HTTP_G(ssb
).sb
.st_mtime
) {
543 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
547 return HTTP_G(ssb
).sb
.st_mtime
;
552 if (!HTTP_G(ssb
).sb
.st_mtime
) {
553 if(php_stream_stat_path(Z_STRVAL_P((zval
*) data_ptr
), &HTTP_G(ssb
))) {
557 return HTTP_G(ssb
).sb
.st_mtime
;
563 /* {{{inline int http_is_range_request(void) */
564 PHP_HTTP_API
inline int _http_is_range_request(TSRMLS_D
)
566 return zend_hash_exists(Z_ARRVAL_P(PG(http_globals
)[TRACK_VARS_SERVER
]),
567 "HTTP_RANGE", strlen("HTTP_RANGE") + 1);
571 /* {{{ inline STATUS http_send_status(int) */
572 PHP_HTTP_API
inline STATUS
_http_send_status(const int status TSRMLS_DC
)
575 return sapi_header_op(SAPI_HEADER_SET_STATUS
, (void *) s TSRMLS_CC
);
579 /* {{{ inline STATUS http_send_header(char *) */
580 PHP_HTTP_API
inline STATUS
_http_send_header(const char *header TSRMLS_DC
)
582 return http_send_status_header(0, header
);
586 /* {{{ inline STATUS http_send_status_header(int, char *) */
587 PHP_HTTP_API
inline STATUS
_http_send_status_header(const int status
, const char *header TSRMLS_DC
)
589 sapi_header_line h
= {(char *) header
, strlen(header
), status
};
590 return sapi_header_op(SAPI_HEADER_REPLACE
, &h TSRMLS_CC
);
594 /* {{{ inline zval *http_get_server_var(char *) */
595 PHP_HTTP_API
inline zval
*_http_get_server_var(const char *key TSRMLS_DC
)
598 if (SUCCESS
== zend_hash_find(
600 (char *) key
, strlen(key
) + 1, (void **) &var
)) {
607 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
608 PHP_HTTP_API
void _http_ob_etaghandler(char *output
, uint output_len
,
609 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
611 char etag
[33] = { 0 };
612 unsigned char digest
[16];
614 if (mode
& PHP_OUTPUT_HANDLER_START
) {
615 PHP_MD5Init(&HTTP_G(etag_md5
));
618 PHP_MD5Update(&HTTP_G(etag_md5
), output
, output_len
);
620 if (mode
& PHP_OUTPUT_HANDLER_END
) {
621 PHP_MD5Final(digest
, &HTTP_G(etag_md5
));
623 /* just do that if desired */
624 if (HTTP_G(etag_started
)) {
625 make_digest(etag
, digest
);
627 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
628 http_send_status(304);
630 http_send_etag(etag
, 32);
635 *handled_output_len
= output_len
;
636 *handled_output
= estrndup(output
, output_len
);
640 /* {{{ STATUS http_start_ob_handler(php_output_handler_func_t, char *, uint, zend_bool) */
641 PHP_HTTP_API STATUS
_http_start_ob_handler(php_output_handler_func_t handler_func
,
642 char *handler_name
, uint chunk_size
, zend_bool erase TSRMLS_DC
)
644 php_ob_buffer
**stack
;
647 if (count
= OG(ob_nesting_level
)) {
648 stack
= ecalloc(count
, sizeof(php_ob_buffer
*));
651 zend_stack_apply_with_argument(&OG(ob_buffers
), ZEND_STACK_APPLY_BOTTOMUP
,
652 (int (*)(void *elem
, void *)) http_ob_stack_get
, stack
);
656 http_ob_stack_get(&OG(active_ob_buffer
), stack
);
659 while (OG(ob_nesting_level
)) {
660 php_end_ob_buffer(0, 0 TSRMLS_CC
);
664 php_ob_set_internal_handler(handler_func
, chunk_size
, handler_name
, erase TSRMLS_CC
);
666 for (i
= 0; i
< count
; i
++) {
667 php_ob_buffer
*s
= stack
[i
];
668 if (strcmp(s
->handler_name
, "default output handler")) {
669 php_start_ob_buffer_named(s
->handler_name
, s
->chunk_size
, s
->erase TSRMLS_CC
);
671 php_body_write(s
->buffer
, s
->text_length TSRMLS_CC
);
672 efree(s
->handler_name
);
684 /* {{{ int http_modified_match(char *, int) */
685 PHP_HTTP_API
int _http_modified_match(const char *entry
, const time_t t TSRMLS_DC
)
689 char *modified
, *chr_ptr
;
691 HTTP_GSC(zmodified
, entry
, 0);
693 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
694 if (chr_ptr
= strrchr(modified
, ';')) {
697 retval
= (t
<= http_parse_date(modified
));
703 /* {{{ int http_etag_match(char *, char *) */
704 PHP_HTTP_API
int _http_etag_match(const char *entry
, const char *etag TSRMLS_DC
)
710 HTTP_GSC(zetag
, entry
, 0);
712 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
716 quoted_etag
= (char *) emalloc(strlen(etag
) + 3);
717 sprintf(quoted_etag
, "\"%s\"", etag
);
719 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
720 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
722 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
729 /* {{{ STATUS http_send_last_modified(int) */
730 PHP_HTTP_API STATUS
_http_send_last_modified(const time_t t TSRMLS_DC
)
732 char modified
[96] = "Last-Modified: ", *date
;
734 strcat(modified
, date
);
740 return http_send_header(modified
);
744 /* {{{ static STATUS http_send_etag(char *, int) */
745 PHP_HTTP_API STATUS
_http_send_etag(const char *etag
,
746 const int etag_len TSRMLS_DC
)
752 php_error_docref(NULL TSRMLS_CC
,E_ERROR
,
753 "Attempt to send empty ETag (previous: %s)\n", HTTP_G(etag
));
761 HTTP_G(etag
) = estrdup(etag
);
763 etag_header
= ecalloc(1, sizeof("ETag: \"\"") + etag_len
);
764 sprintf(etag_header
, "ETag: \"%s\"", etag
);
765 if (SUCCESS
!= (status
= http_send_header(etag_header
))) {
766 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", etag_header
);
773 /* {{{ STATUS http_send_cache_control(char *, size_t) */
774 PHP_HTTP_API STATUS
_http_send_cache_control(const char *cache_control
,
775 const size_t cc_len TSRMLS_DC
)
778 char *cc_header
= ecalloc(1, sizeof("Cache-Control: ") + cc_len
);
780 sprintf(cc_header
, "Cache-Control: %s", cache_control
);
781 if (SUCCESS
!= (status
= http_send_header(cc_header
))) {
782 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
,
783 "Could not send '%s' header", cc_header
);
790 /* {{{ STATUS http_send_content_type(char *, size_t) */
791 PHP_HTTP_API STATUS
_http_send_content_type(const char *content_type
,
792 const size_t ct_len TSRMLS_DC
)
797 if (!strchr(content_type
, '/')) {
798 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
799 "Content-Type '%s' doesn't seem to consist of a primary and a secondary part",
804 /* remember for multiple ranges */
806 efree(HTTP_G(ctype
));
808 HTTP_G(ctype
) = estrndup(content_type
, ct_len
);
810 ct_header
= ecalloc(1, sizeof("Content-Type: ") + ct_len
);
811 sprintf(ct_header
, "Content-Type: %s", content_type
);
813 if (SUCCESS
!= (status
= http_send_header(ct_header
))) {
814 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
815 "Couldn't send '%s' header", ct_header
);
822 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
823 PHP_HTTP_API STATUS
_http_send_content_disposition(const char *filename
,
824 const size_t f_len
, const int send_inline TSRMLS_DC
)
830 cd_header
= ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len
);
831 sprintf(cd_header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
833 cd_header
= ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len
);
834 sprintf(cd_header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
837 if (SUCCESS
!= (status
= http_send_header(cd_header
))) {
838 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", cd_header
);
845 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
846 PHP_HTTP_API STATUS
_http_cache_last_modified(const time_t last_modified
,
847 const time_t send_modified
, const char *cache_control
, const size_t cc_len TSRMLS_DC
)
850 http_send_cache_control(cache_control
, cc_len
);
853 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
854 if (SUCCESS
== http_send_status(304)) {
857 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
861 return http_send_last_modified(send_modified
);
865 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
866 PHP_HTTP_API STATUS
_http_cache_etag(const char *etag
, const size_t etag_len
,
867 const char *cache_control
, const size_t cc_len TSRMLS_DC
)
870 http_send_cache_control(cache_control
, cc_len
);
874 http_send_etag(etag
, etag_len
);
875 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
876 if (SUCCESS
== http_send_status(304)) {
879 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
885 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
886 if (!HTTP_G(etag_started
)) {
887 if (SUCCESS
== http_start_ob_handler(_http_ob_etaghandler
, "ob_etaghandler", 4096, 1)) {
888 HTTP_G(etag_started
) = 1;
891 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not start ob_etaghandler");
899 /* {{{ char *http_absolute_uri(char *, char *) */
900 PHP_HTTP_API
char *_http_absolute_uri(const char *url
,
901 const char *proto TSRMLS_DC
)
903 char *proto_ptr
, *host
, *path
, *PTR
, *URI
= ecalloc(1, HTTP_URI_MAXLEN
+ 1);
906 if (!url
|| !strlen(url
)) {
907 if (!SG(request_info
).request_uri
) {
910 url
= SG(request_info
).request_uri
;
912 /* Mess around with already absolute URIs */
913 else if (proto_ptr
= strstr(url
, "://")) {
914 if (!proto
|| !strncmp(url
, proto
, strlen(proto
))) {
915 strncpy(URI
, url
, HTTP_URI_MAXLEN
);
918 snprintf(URI
, HTTP_URI_MAXLEN
, "%s%s", proto
, proto_ptr
+ 3);
923 /* protocol defaults to http */
924 if (!proto
|| !strlen(proto
)) {
929 if ( (zhost
= http_get_server_var("HTTP_HOST")) ||
930 (zhost
= http_get_server_var("SERVER_NAME"))) {
931 host
= Z_STRVAL_P(zhost
);
939 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s%s", proto
, host
, url
);
940 } else if (SG(request_info
).request_uri
) {
941 path
= estrdup(SG(request_info
).request_uri
);
942 php_dirname(path
, strlen(path
));
943 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s%s/%s", proto
, host
, path
, url
);
946 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s/%s", proto
, host
, url
);
949 /* strip everything after a new line */
950 if ((PTR
= strchr(URI
, '\r')) || (PTR
= strchr(URI
, '\n'))) {
958 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
959 PHP_HTTP_API
char *_http_negotiate_q(const char *entry
, const zval
*supported
,
960 const char *def TSRMLS_DC
)
962 zval
*zaccept
, *zarray
, *zdelim
, **zentry
, *zentries
, **zsupp
;
963 char *q_ptr
, *result
;
967 HTTP_GSC(zaccept
, entry
, estrdup(def
));
969 MAKE_STD_ZVAL(zarray
);
972 MAKE_STD_ZVAL(zdelim
);
973 ZVAL_STRING(zdelim
, ",", 0);
974 php_explode(zdelim
, zaccept
, zarray
, -1);
977 MAKE_STD_ZVAL(zentries
);
978 array_init(zentries
);
980 c
= zend_hash_num_elements(Z_ARRVAL_P(zarray
));
981 for (i
= 0; i
< c
; i
++, zend_hash_move_forward(Z_ARRVAL_P(zarray
))) {
983 if (SUCCESS
!= zend_hash_get_current_data(
984 Z_ARRVAL_P(zarray
), (void **) &zentry
)) {
985 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
986 "Cannot parse %s header: %s", entry
, Z_STRVAL_P(zaccept
));
990 /* check for qualifier */
991 if (NULL
!= (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';'))) {
992 qual
= strtod(q_ptr
+ 3, NULL
);
997 /* walk through the supported array */
998 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported
));
999 SUCCESS
== zend_hash_get_current_data(
1000 Z_ARRVAL_P(supported
), (void **) &zsupp
);
1001 zend_hash_move_forward(Z_ARRVAL_P(supported
))) {
1002 if (!strcasecmp(Z_STRVAL_PP(zsupp
), Z_STRVAL_PP(zentry
))) {
1003 add_assoc_double(zentries
, Z_STRVAL_PP(zsupp
), qual
);
1012 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries
));
1014 if ( (SUCCESS
!= zend_hash_sort(Z_ARRVAL_P(zentries
), zend_qsort
,
1015 http_sort_q
, 0 TSRMLS_CC
)) ||
1016 (HASH_KEY_NON_EXISTANT
== zend_hash_get_current_key(
1017 Z_ARRVAL_P(zentries
), &result
, 0, 1))) {
1018 result
= estrdup(def
);
1021 zval_dtor(zentries
);
1028 /* {{{ http_range_status http_get_request_ranges(zval *zranges, size_t) */
1029 PHP_HTTP_API http_range_status
_http_get_request_ranges(zval
*zranges
,
1030 const size_t length TSRMLS_DC
)
1034 long begin
= -1, end
= -1, *ptr
;
1036 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
1037 range
= Z_STRVAL_P(zrange
);
1039 if (strncmp(range
, "bytes=", strlen("bytes="))) {
1040 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Range header misses bytes=");
1045 range
+= strlen("bytes=");
1048 switch (c
= *(range
++))
1054 case '1': case '2': case '3':
1055 case '4': case '5': case '6':
1056 case '7': case '8': case '9':
1058 * If the value of the pointer is already set (non-negative)
1059 * then multiply its value by ten and add the current value,
1060 * else initialise the pointers value with the current value
1062 * This let us recognize empty fields when validating the
1063 * ranges, i.e. a "-10" for begin and "12345" for the end
1064 * was the following range request: "Range: bytes=0-12345";
1065 * While a "-1" for begin and "12345" for the end would
1066 * have been: "Range: bytes=-12345".
1081 /* IE - ignore for now */
1088 /* validate ranges */
1093 if ((length
- end
) < 1) {
1101 if ((length
- end
) < 1) {
1104 begin
= length
- end
;
1114 if ((length
- begin
) < 1) {
1122 if ( ((length
- begin
) < 1) ||
1123 ((length
- end
) < 1) ||
1124 ((begin
- end
) >= 0)) {
1134 MAKE_STD_ZVAL(zentry
);
1136 add_index_long(zentry
, 0, begin
);
1137 add_index_long(zentry
, 1, end
);
1138 add_next_index_zval(zranges
, zentry
);
1156 /* {{{ STATUS http_send_ranges(zval *, void *, size_t, http_send_mode) */
1157 PHP_HTTP_API STATUS
_http_send_ranges(zval
*zranges
, const void *data
, const size_t size
, const http_send_mode mode TSRMLS_DC
)
1160 long **begin
, **end
;
1163 /* Send HTTP 206 Partial Content */
1164 http_send_status(206);
1167 if ((c
= zend_hash_num_elements(Z_ARRVAL_P(zranges
))) == 1) {
1168 char range_header
[256] = {0};
1170 zend_hash_index_find(Z_ARRVAL_P(zranges
), 0, (void **) &zrange
);
1171 zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
);
1172 zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
);
1174 /* send content range header */
1175 snprintf(range_header
, 255, "Content-Range: bytes %d-%d/%d", **begin
, **end
, size
);
1176 http_send_header(range_header
);
1178 /* send requested chunk */
1179 return http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1185 char bound
[23] = {0}, preface
[1024] = {0},
1186 multi_header
[68] = "Content-Type: multipart/byteranges; boundary=";
1188 snprintf(bound
, 22, "--%d%0.9f", time(NULL
), php_combined_lcg(TSRMLS_C
));
1189 strncat(multi_header
, bound
+ 2, 21);
1190 http_send_header(multi_header
);
1192 /* send each requested chunk */
1193 for ( i
= 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(zranges
));
1195 i
++, zend_hash_move_forward(Z_ARRVAL_P(zranges
))) {
1196 if ( HASH_KEY_NON_EXISTANT
== zend_hash_get_current_data(
1197 Z_ARRVAL_P(zranges
), (void **) &zrange
) ||
1198 SUCCESS
!= zend_hash_index_find(
1199 Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1200 SUCCESS
!= zend_hash_index_find(
1201 Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1205 snprintf(preface
, 1023,
1207 HTTP_CRLF
"Content-Type: %s"
1208 HTTP_CRLF
"Content-Range: bytes %ld-%ld/%ld"
1213 HTTP_G(ctype
) ? HTTP_G(ctype
) : "application/x-octetstream",
1219 php_body_write(preface
, strlen(preface
) TSRMLS_CC
);
1220 http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1223 /* write boundary once more */
1224 php_body_write(HTTP_CRLF
, 2 TSRMLS_CC
);
1225 php_body_write(bound
, strlen(bound
) TSRMLS_CC
);
1232 /* {{{ STATUS http_send(void *, sizezo_t, http_send_mode) */
1233 PHP_HTTP_API STATUS
_http_send(const void *data_ptr
, const size_t data_size
,
1234 const http_send_mode data_mode TSRMLS_DC
)
1236 int is_range_request
= http_is_range_request();
1243 if (HTTP_G(etag_started
)) {
1246 HTTP_G(etag_started
) = 0;
1247 /* never ever use the output to compute the ETag if http_send() is used */
1248 php_end_ob_buffer(0, 0 TSRMLS_CC
);
1249 if (!(etag
= http_etag(data_ptr
, data_size
, data_mode
))) {
1253 /* send 304 Not Modified if etag matches */
1254 if ((!is_range_request
) && http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
1256 return http_send_status(304);
1259 http_send_etag(etag
, 32);
1263 /* send 304 Not Modified if last-modified matches*/
1264 if ((!is_range_request
) && http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod
))) {
1265 return http_send_status(304);
1268 if (is_range_request
) {
1270 /* only send ranges if entity hasn't changed */
1272 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_MATCH", 13)) ||
1273 http_etag_match("HTTP_IF_MATCH", HTTP_G(etag
)))
1275 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
1276 http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod
)))
1279 STATUS result
= FAILURE
;
1280 zval
*zranges
= NULL
;
1281 MAKE_STD_ZVAL(zranges
);
1282 array_init(zranges
);
1284 switch (http_get_request_ranges(zranges
, data_size
))
1289 /* go ahead and send all */
1293 result
= http_send_ranges(zranges
, data_ptr
, data_size
, data_mode
);
1302 http_send_status(416);
1313 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
1317 /* {{{ STATUS http_send_data(zval *) */
1318 PHP_HTTP_API STATUS
_http_send_data(const zval
*zdata TSRMLS_DC
)
1320 if (!Z_STRLEN_P(zdata
)) {
1323 if (!Z_STRVAL_P(zdata
)) {
1327 return http_send(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
), SEND_DATA
);
1331 /* {{{ STATUS http_send_stream(php_stream *) */
1332 PHP_HTTP_API STATUS
_http_send_stream(const php_stream
*file TSRMLS_DC
)
1334 if (php_stream_stat((php_stream
*) file
, &HTTP_G(ssb
))) {
1338 return http_send(file
, HTTP_G(ssb
).sb
.st_size
, SEND_RSRC
);
1342 /* {{{ STATUS http_send_file(zval *) */
1343 PHP_HTTP_API STATUS
_http_send_file(const zval
*zfile TSRMLS_DC
)
1348 if (!Z_STRLEN_P(zfile
)) {
1352 if (!(file
= php_stream_open_wrapper(Z_STRVAL_P(zfile
), "rb",
1353 REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
))) {
1357 ret
= http_send_stream(file
);
1358 php_stream_close(file
);
1363 /* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1364 PHP_HTTP_API STATUS
_http_chunked_decode(const char *encoded
,
1365 const size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
1371 *decoded
= ecalloc(1, encoded_len
);
1375 while (((e_ptr
- encoded
) - encoded_len
) > 0) {
1376 char hex_len
[9] = {0};
1377 size_t chunk_len
= 0;
1380 /* read in chunk size */
1381 while (isxdigit(*e_ptr
)) {
1383 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1384 "Chunk size is too long: 0x%s...", hex_len
);
1388 hex_len
[i
++] = *e_ptr
++;
1391 /* reached the end */
1392 if (!strcmp(hex_len
, "0")) {
1397 if (strncmp(e_ptr
, HTTP_CRLF
, 2)) {
1398 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1399 "Invalid character (expected 0x0D 0x0A; got: %x %x)",
1400 *e_ptr
, *(e_ptr
+ 1));
1408 chunk_len
= strtol(hex_len
, &error
, 16);
1409 if (error
== hex_len
) {
1410 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1411 "Invalid chunk size string: '%s'", hex_len
);
1417 memcpy(d_ptr
, e_ptr
+= 2, chunk_len
);
1419 e_ptr
+= chunk_len
+ 2;
1420 *decoded_len
+= chunk_len
;
1427 /* {{{ proto STATUS http_split_response_ex(char *, size_t, zval *, zval *) */
1428 PHP_HTTP_API STATUS
_http_split_response_ex( char *response
,
1429 size_t response_len
, zval
*zheaders
, zval
*zbody TSRMLS_DC
)
1432 char *header
= response
;
1434 while ((response
- header
+ 4) < response_len
) {
1435 if ( (*response
++ == '\r') &&
1436 (*response
++ == '\n') &&
1437 (*response
++ == '\r') &&
1438 (*response
++ == '\n')) {
1444 if (body
&& (response_len
- (body
- header
))) {
1445 ZVAL_STRINGL(zbody
, body
, response_len
- (body
- header
) - 1, 1);
1447 Z_TYPE_P(zbody
) = IS_NULL
;
1450 return http_parse_headers(header
, body
? body
- header
: response_len
, zheaders
);
1454 /* {{{ STATUS http_parse_headers(char *, long, zval *) */
1455 PHP_HTTP_API STATUS
_http_parse_headers(char *header
, int header_len
, zval
*array TSRMLS_DC
)
1457 char *colon
= NULL
, *line
= NULL
, *begin
= header
;
1459 if (header_len
< 2) {
1464 if (!strncmp(header
, "HTTP/1.", 7)) {
1465 char *end
= strstr(header
, HTTP_CRLF
);
1466 add_assoc_stringl(array
, "Status",
1467 header
+ strlen("HTTP/1.x "),
1468 end
- (header
+ strlen("HTTP/1.x ")), 1);
1474 while (header_len
>= (line
- begin
)) {
1480 --value_len
; /* we don't have CR so value length is one char less */
1482 if (colon
&& ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t')))) {
1484 /* skip empty key */
1485 if (header
!= colon
) {
1486 char *key
= estrndup(header
, colon
- header
);
1487 value_len
+= line
- colon
- 1;
1489 /* skip leading ws */
1490 while (isspace(*(++colon
))) --value_len
;
1491 /* skip trailing ws */
1492 while (isspace(colon
[value_len
- 1])) --value_len
;
1494 if (value_len
< 1) {
1495 /* hm, empty header? */
1496 add_assoc_stringl(array
, key
, "", 0, 1);
1498 add_assoc_stringl(array
, key
, colon
, value_len
, 1);
1505 header
+= line
- header
;
1520 /* {{{ void http_get_request_headers(zval *) */
1521 PHP_HTTP_API
void _http_get_request_headers(zval
*array TSRMLS_DC
)
1525 for ( zend_hash_internal_pointer_reset(HTTP_SERVER_VARS
);
1526 zend_hash_get_current_key(HTTP_SERVER_VARS
, &key
, NULL
, 0) != HASH_KEY_NON_EXISTANT
;
1527 zend_hash_move_forward(HTTP_SERVER_VARS
)) {
1528 if (!strncmp(key
, "HTTP_", 5)) {
1530 zend_hash_get_current_data(HTTP_SERVER_VARS
, (void **) &header
);
1531 add_assoc_stringl(array
, pretty_key(key
+ 5, strlen(key
) - 5, 1, 1), Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1537 /* {{{ STATUS http_auth_header(char *, char*) */
1538 PHP_HTTP_API STATUS
_http_auth_header(const char *type
, const char *realm TSRMLS_DC
)
1540 char realm_header
[1024] = {0};
1541 snprintf(realm_header
, 1023, "WWW-Authenticate: %s realm=\"%s\"", type
, realm
);
1542 return http_send_status_header(401, realm_header
);
1546 /* {{{ STATUS http_auth_credentials(char **, char **) */
1547 PHP_HTTP_API STATUS
_http_auth_credentials(char **user
, char **pass TSRMLS_DC
)
1549 if (strncmp(sapi_module
.name
, "isapi", 5)) {
1550 zval
*zuser
, *zpass
;
1552 HTTP_GSC(zuser
, "PHP_AUTH_USER", FAILURE
);
1553 HTTP_GSC(zpass
, "PHP_AUTH_PW", FAILURE
);
1555 *user
= estrndup(Z_STRVAL_P(zuser
), Z_STRLEN_P(zuser
));
1556 *pass
= estrndup(Z_STRVAL_P(zpass
), Z_STRLEN_P(zpass
));
1561 HTTP_GSC(zauth
, "HTTP_AUTHORIZATION", FAILURE
);
1563 char *decoded
, *colon
;
1565 decoded
= php_base64_decode(Z_STRVAL_P(zauth
), Z_STRLEN_P(zauth
),
1568 if (colon
= strchr(decoded
+ 6, ':')) {
1569 *user
= estrndup(decoded
+ 6, colon
- decoded
- 6);
1570 *pass
= estrndup(colon
+ 1, decoded
+ decoded_len
- colon
- 6 - 1);
1581 /* }}} public API */
1588 * vim600: noet sw=4 ts=4 fdm=marker
1589 * vim<600: noet sw=4 ts=4