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 +----------------------------------------------------------------------+
25 # include <winsock2.h>
26 #elif defined(HAVE_NETDB_H)
31 #include "php_version.h"
32 #include "php_streams.h"
33 #include "php_output.h"
35 #include "ext/standard/md5.h"
36 #include "ext/standard/url.h"
37 #include "ext/standard/base64.h"
38 #include "ext/standard/php_string.h"
39 #include "ext/standard/php_smart_str.h"
40 #include "ext/standard/php_lcg.h"
45 # include "ext/standard/php_http.h"
49 #include "php_http_api.h"
50 #include "php_http_std_defs.h"
52 ZEND_DECLARE_MODULE_GLOBALS(http
)
54 /* {{{ day/month names */
55 static const char *days
[] = {
56 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
58 static const char *wkdays
[] = {
59 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
61 static const char *weekdays
[] = {
62 "Monday", "Tuesday", "Wednesday",
63 "Thursday", "Friday", "Saturday", "Sunday"
65 static const char *months
[] = {
66 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
67 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
74 static const struct time_zone
{
78 {"GMT", 0}, /* Greenwich Mean */
79 {"UTC", 0}, /* Universal (Coordinated) */
80 {"WET", 0}, /* Western European */
81 {"BST", 0}, /* British Summer */
82 {"WAT", 60}, /* West Africa */
83 {"AST", 240}, /* Atlantic Standard */
84 {"ADT", 240}, /* Atlantic Daylight */
85 {"EST", 300}, /* Eastern Standard */
86 {"EDT", 300}, /* Eastern Daylight */
87 {"CST", 360}, /* Central Standard */
88 {"CDT", 360}, /* Central Daylight */
89 {"MST", 420}, /* Mountain Standard */
90 {"MDT", 420}, /* Mountain Daylight */
91 {"PST", 480}, /* Pacific Standard */
92 {"PDT", 480}, /* Pacific Daylight */
93 {"YST", 540}, /* Yukon Standard */
94 {"YDT", 540}, /* Yukon Daylight */
95 {"HST", 600}, /* Hawaii Standard */
96 {"HDT", 600}, /* Hawaii Daylight */
97 {"CAT", 600}, /* Central Alaska */
98 {"AHST", 600}, /* Alaska-Hawaii Standard */
99 {"NT", 660}, /* Nome */
100 {"IDLW", 720}, /* International Date Line West */
101 {"CET", -60}, /* Central European */
102 {"MET", -60}, /* Middle European */
103 {"MEWT", -60}, /* Middle European Winter */
104 {"MEST", -120}, /* Middle European Summer */
105 {"CEST", -120}, /* Central European Summer */
106 {"MESZ", -60}, /* Middle European Summer */
107 {"FWT", -60}, /* French Winter */
108 {"FST", -60}, /* French Summer */
109 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
110 {"WAST", -420}, /* West Australian Standard */
111 {"WADT", -420}, /* West Australian Daylight */
112 {"CCT", -480}, /* China Coast, USSR Zone 7 */
113 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
114 {"EAST", -600}, /* Eastern Australian Standard */
115 {"EADT", -600}, /* Eastern Australian Daylight */
116 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
117 {"NZT", -720}, /* New Zealand */
118 {"NZST", -720}, /* New Zealand Standard */
119 {"NZDT", -720}, /* New Zealand Daylight */
120 {"IDLE", -720}, /* International Date Line East */
126 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
);
127 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
128 static STATUS
_http_send_chunk(const void *data
, const size_t begin
, const size_t end
, const http_send_mode mode TSRMLS_DC
);
130 static int check_day(char *day
, size_t len
);
131 static int check_month(char *month
);
132 static int check_tzone(char *tzone
);
134 static int http_ob_stack_get(php_ob_buffer
*, php_ob_buffer
**);
136 /* {{{ static int http_sort_q(const void *, const void *) */
137 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
140 zval result
, *first
, *second
;
142 f
= *((Bucket
**) a
);
143 s
= *((Bucket
**) b
);
145 first
= *((zval
**) f
->pData
);
146 second
= *((zval
**) s
->pData
);
148 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
151 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
155 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
157 static STATUS
_http_send_chunk(const void *data
, const size_t begin
,
158 const size_t end
, const http_send_mode mode TSRMLS_DC
)
160 long len
= end
- begin
;
168 php_stream
*s
= (php_stream
*) data
;
170 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
174 buf
= (char *) ecalloc(1, HTTP_SENDBUF_SIZE
);
175 /* read into buf and write out */
176 while ((len
-= HTTP_SENDBUF_SIZE
) >= 0) {
177 if (!(read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
))) {
181 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
185 /* ob_flush() && flush() */
186 php_end_ob_buffer(1, 1 TSRMLS_CC
);
187 sapi_flush(TSRMLS_C
);
190 /* read & write left over */
192 if (read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
+ len
)) {
193 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
201 /* ob_flush() & flush() */
202 php_end_ob_buffer(1, 1 TSRMLS_CC
);
203 sapi_flush(TSRMLS_C
);
211 char *s
= (char *) data
+ begin
;
213 while ((len
-= HTTP_SENDBUF_SIZE
) >= 0) {
214 if (HTTP_SENDBUF_SIZE
- php_body_write(s
, HTTP_SENDBUF_SIZE TSRMLS_CC
)) {
217 s
+= HTTP_SENDBUF_SIZE
;
218 /* ob_flush() & flush() */
219 php_end_ob_buffer(1, 1 TSRMLS_CC
);
220 sapi_flush(TSRMLS_C
);
223 /* write left over */
225 if (HTTP_SENDBUF_SIZE
+ len
- php_body_write(s
, HTTP_SENDBUF_SIZE
+ len TSRMLS_CC
)) {
228 /* ob_flush() & flush() */
229 php_end_ob_buffer(1, 1 TSRMLS_CC
);
230 sapi_flush(TSRMLS_C
);
242 /* {{{ Day/Month/TZ checks for http_parse_date()
243 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
244 static int check_day(char *day
, size_t len
)
247 const char * const *check
= (len
> 3) ? &weekdays
[0] : &wkdays
[0];
248 for (i
= 0; i
< 7; i
++) {
249 if (!strcmp(day
, check
[0])) {
257 static int check_month(char *month
)
260 const char * const *check
= &months
[0];
261 for (i
= 0; i
< 12; i
++) {
262 if (!strcmp(month
, check
[0])) {
270 /* return the time zone offset between GMT and the input one, in number
271 of seconds or -1 if the timezone wasn't found/legal */
273 static int check_tzone(char *tzone
)
276 const struct time_zone
*check
= time_zones
;
277 for (i
= 0; i
< sizeof(time_zones
) / sizeof(time_zones
[0]); i
++) {
278 if (!strcmp(tzone
, check
->name
)) {
279 return check
->offset
* 60;
287 /* char *pretty_key(char *, int, int, int) */
288 char *pretty_key(char *key
, int key_len
, int uctitle
, int xhyphen
)
290 if (key
&& key_len
) {
292 if (wasalpha
= isalpha(key
[0])) {
293 key
[0] = uctitle
? toupper(key
[0]) : tolower(key
[0]);
295 for (i
= 1; i
< key_len
; i
++) {
296 if (isalpha(key
[i
])) {
297 key
[i
] = ((!wasalpha
) && uctitle
) ? toupper(key
[i
]) : tolower(key
[i
]);
300 if (xhyphen
&& (key
[i
] == '_')) {
311 /* {{{ static STATUS http_ob_stack_get(php_ob_buffer *, php_ob_buffer **) */
312 static STATUS
http_ob_stack_get(php_ob_buffer
*o
, php_ob_buffer
**s
)
315 php_ob_buffer
*b
= emalloc(sizeof(php_ob_buffer
));
316 b
->handler_name
= estrdup(o
->handler_name
);
317 b
->buffer
= estrndup(o
->buffer
, o
->text_length
);
318 b
->text_length
= o
->text_length
;
319 b
->chunk_size
= o
->chunk_size
;
330 /* {{{ char *http_date(time_t) */
331 PHP_HTTP_API
char *_http_date(time_t t TSRMLS_DC
)
333 struct tm
*gmtime
, tmbuf
;
335 if (gmtime
= php_gmtime_r(&t
, &tmbuf
)) {
336 char *date
= ecalloc(1, 31);
338 "%s, %02d %s %04d %02d:%02d:%02d GMT",
339 days
[gmtime
->tm_wday
], gmtime
->tm_mday
,
340 months
[gmtime
->tm_mon
], gmtime
->tm_year
+ 1900,
341 gmtime
->tm_hour
, gmtime
->tm_min
, gmtime
->tm_sec
350 /* {{{ time_t http_parse_date(char *)
351 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
352 PHP_HTTP_API
time_t _http_parse_date(const char *date
)
355 int tz_offset
= -1, year
= -1, month
= -1, monthday
= -1, weekday
= -1,
356 hours
= -1, minutes
= -1, seconds
= -1;
358 enum assume_next dignext
= DATE_MDAY
;
359 const char *indate
= date
;
361 int found
= 0, part
= 0; /* max 6 parts */
363 while (*date
&& (part
< 6)) {
366 while (*date
&& !isalnum(*date
)) {
370 if (isalpha(*date
)) {
371 /* a name coming up */
374 sscanf(date
, "%31[A-Za-z]", buf
);
378 weekday
= check_day(buf
, len
);
384 if (!found
&& (month
== -1)) {
385 month
= check_month(buf
);
391 if (!found
&& (tz_offset
== -1)) {
392 /* this just must be a time zone string */
393 tz_offset
= check_tzone(buf
);
394 if (tz_offset
!= -1) {
400 return -1; /* bad string */
404 else if (isdigit(*date
)) {
408 if ((seconds
== -1) &&
409 (3 == sscanf(date
, "%02d:%02d:%02d", &hours
, &minutes
, &seconds
))) {
415 val
= (int) strtol(date
, &end
, 10);
417 if ((tz_offset
== -1) && ((end
- date
) == 4) && (val
< 1300) &&
418 (indate
< date
) && ((date
[-1] == '+' || date
[-1] == '-'))) {
419 /* four digits and a value less than 1300 and it is preceeded with
420 a plus or minus. This is a time zone indication. */
422 tz_offset
= (val
/ 100 * 60 + val
% 100) * 60;
424 /* the + and - prefix indicates the local time compared to GMT,
425 this we need ther reversed math to get what we want */
426 tz_offset
= date
[-1] == '+' ? -tz_offset
: tz_offset
;
429 if (((end
- date
) == 8) && (year
== -1) && (month
== -1) && (monthday
== -1)) {
430 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
433 month
= (val
% 10000) / 100 - 1; /* month is 0 - 11 */
434 monthday
= val
% 100;
437 if (!found
&& (dignext
== DATE_MDAY
) && (monthday
== -1)) {
438 if ((val
> 0) && (val
< 32)) {
445 if (!found
&& (dignext
== DATE_YEAR
) && (year
== -1)) {
449 year
+= year
> 70 ? 1900 : 2000;
468 seconds
= minutes
= hours
= 0; /* no time, make it zero */
471 if ((-1 == monthday
) || (-1 == month
) || (-1 == year
)) {
472 /* lacks vital info, fail */
476 if (sizeof(time_t) < 5) {
477 /* 32 bit time_t can only hold dates to the beginning of 2038 */
486 tm
.tm_mday
= monthday
;
488 tm
.tm_year
= year
- 1900;
495 /* time zone adjust */
497 struct tm
*gmt
, keeptime2
;
501 if(!(gmt
= php_gmtime_r(&t
, &keeptime2
))) {
502 return -1; /* illegal date/time */
507 /* Add the time zone diff (between the given timezone and GMT) and the
508 diff between the local time zone and GMT. */
509 delta
= (tz_offset
!= -1 ? tz_offset
: 0) + (t
- t2
);
511 if((delta
> 0) && (t
+ delta
< t
)) {
512 return -1; /* time_t overflow */
522 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
523 PHP_HTTP_API
char *_http_etag(const void *data_ptr
, const size_t data_len
,
524 const http_send_mode data_mode TSRMLS_DC
)
526 char ssb_buf
[128] = {0};
527 unsigned char digest
[16];
529 char *new_etag
= ecalloc(1, 33);
536 PHP_MD5Update(&ctx
, data_ptr
, data_len
);
540 if (!HTTP_G(ssb
).sb
.st_ino
) {
541 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
545 snprintf(ssb_buf
, 127, "%ld=%ld=%ld",
546 HTTP_G(ssb
).sb
.st_mtime
,
547 HTTP_G(ssb
).sb
.st_ino
,
548 HTTP_G(ssb
).sb
.st_size
550 PHP_MD5Update(&ctx
, ssb_buf
, strlen(ssb_buf
));
559 PHP_MD5Final(digest
, &ctx
);
560 make_digest(new_etag
, digest
);
566 /* {{{ time_t http_lmod(void *, http_send_mode) */
567 PHP_HTTP_API
time_t _http_lmod(const void *data_ptr
, const http_send_mode data_mode TSRMLS_DC
)
578 if (!HTTP_G(ssb
).sb
.st_mtime
) {
579 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
583 return HTTP_G(ssb
).sb
.st_mtime
;
588 if (!HTTP_G(ssb
).sb
.st_mtime
) {
589 if(php_stream_stat_path(Z_STRVAL_P((zval
*) data_ptr
), &HTTP_G(ssb
))) {
593 return HTTP_G(ssb
).sb
.st_mtime
;
599 /* {{{ STATUS http_send_status_header(int, char *) */
600 PHP_HTTP_API STATUS
_http_send_status_header(const int status
, const char *header TSRMLS_DC
)
602 sapi_header_line h
= {(char *) header
, strlen(header
), status
};
603 return sapi_header_op(SAPI_HEADER_REPLACE
, &h TSRMLS_CC
);
607 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
608 PHP_HTTP_API zval
*_http_get_server_var_ex(const char *key
, size_t key_size
, zend_bool check TSRMLS_DC
)
611 if (SUCCESS
== zend_hash_find(HTTP_SERVER_VARS
, (char *) key
, key_size
, (void **) &var
)) {
613 return Z_STRVAL_PP(var
) && Z_STRLEN_PP(var
) ? *var
: NULL
;
622 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
623 PHP_HTTP_API
void _http_ob_etaghandler(char *output
, uint output_len
,
624 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
626 char etag
[33] = { 0 };
627 unsigned char digest
[16];
629 if (mode
& PHP_OUTPUT_HANDLER_START
) {
630 PHP_MD5Init(&HTTP_G(etag_md5
));
633 PHP_MD5Update(&HTTP_G(etag_md5
), output
, output_len
);
635 if (mode
& PHP_OUTPUT_HANDLER_END
) {
636 PHP_MD5Final(digest
, &HTTP_G(etag_md5
));
638 /* just do that if desired */
639 if (HTTP_G(etag_started
)) {
640 make_digest(etag
, digest
);
642 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
643 http_send_status(304);
646 http_send_etag(etag
, 32);
651 *handled_output_len
= output_len
;
652 *handled_output
= estrndup(output
, output_len
);
656 /* {{{ STATUS http_start_ob_handler(php_output_handler_func_t, char *, uint, zend_bool) */
657 PHP_HTTP_API STATUS
_http_start_ob_handler(php_output_handler_func_t handler_func
,
658 char *handler_name
, uint chunk_size
, zend_bool erase TSRMLS_DC
)
660 php_ob_buffer
**stack
;
663 if (count
= OG(ob_nesting_level
)) {
664 stack
= ecalloc(count
, sizeof(php_ob_buffer
*));
667 zend_stack_apply_with_argument(&OG(ob_buffers
), ZEND_STACK_APPLY_BOTTOMUP
,
668 (int (*)(void *elem
, void *)) http_ob_stack_get
, stack
);
672 http_ob_stack_get(&OG(active_ob_buffer
), stack
);
675 while (OG(ob_nesting_level
)) {
676 php_end_ob_buffer(0, 0 TSRMLS_CC
);
680 php_ob_set_internal_handler(handler_func
, chunk_size
, handler_name
, erase TSRMLS_CC
);
682 for (i
= 0; i
< count
; i
++) {
683 php_ob_buffer
*s
= stack
[i
];
684 if (strcmp(s
->handler_name
, "default output handler")) {
685 php_start_ob_buffer_named(s
->handler_name
, s
->chunk_size
, s
->erase TSRMLS_CC
);
687 php_body_write(s
->buffer
, s
->text_length TSRMLS_CC
);
688 efree(s
->handler_name
);
700 /* {{{ int http_modified_match(char *, time_t) */
701 PHP_HTTP_API
int _http_modified_match_ex(const char *entry
, const time_t t
,
702 const int enforce_presence TSRMLS_DC
)
706 char *modified
, *chr_ptr
;
708 HTTP_GSC(zmodified
, entry
, !enforce_presence
);
710 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
711 if (chr_ptr
= strrchr(modified
, ';')) {
714 retval
= (t
<= http_parse_date(modified
));
720 /* {{{ int http_etag_match(char *, char *) */
721 PHP_HTTP_API
int _http_etag_match_ex(const char *entry
, const char *etag
,
722 const int enforce_presence TSRMLS_DC
)
728 HTTP_GSC(zetag
, entry
, !enforce_presence
);
730 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
734 quoted_etag
= (char *) emalloc(strlen(etag
) + 3);
735 sprintf(quoted_etag
, "\"%s\"", etag
);
737 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
738 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
740 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
747 /* {{{ STATUS http_send_last_modified(int) */
748 PHP_HTTP_API STATUS
_http_send_last_modified(const time_t t TSRMLS_DC
)
751 if (date
= http_date(t
)) {
752 char modified
[96] = "Last-Modified: ";
753 strcat(modified
, date
);
759 return http_send_header(modified
);
765 /* {{{ static STATUS http_send_etag(char *, int) */
766 PHP_HTTP_API STATUS
_http_send_etag(const char *etag
,
767 const int etag_len TSRMLS_DC
)
773 php_error_docref(NULL TSRMLS_CC
,E_ERROR
,
774 "Attempt to send empty ETag (previous: %s)\n", HTTP_G(etag
));
782 HTTP_G(etag
) = estrdup(etag
);
784 etag_header
= ecalloc(1, sizeof("ETag: \"\"") + etag_len
);
785 sprintf(etag_header
, "ETag: \"%s\"", etag
);
786 if (SUCCESS
!= (status
= http_send_header(etag_header
))) {
787 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", etag_header
);
794 /* {{{ STATUS http_send_cache_control(char *, size_t) */
795 PHP_HTTP_API STATUS
_http_send_cache_control(const char *cache_control
,
796 const size_t cc_len TSRMLS_DC
)
799 char *cc_header
= ecalloc(1, sizeof("Cache-Control: ") + cc_len
);
801 sprintf(cc_header
, "Cache-Control: %s", cache_control
);
802 if (SUCCESS
!= (status
= http_send_header(cc_header
))) {
803 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
,
804 "Could not send '%s' header", cc_header
);
811 /* {{{ STATUS http_send_content_type(char *, size_t) */
812 PHP_HTTP_API STATUS
_http_send_content_type(const char *content_type
,
813 const size_t ct_len TSRMLS_DC
)
818 if (!strchr(content_type
, '/')) {
819 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
820 "Content-Type '%s' doesn't seem to consist of a primary and a secondary part",
825 /* remember for multiple ranges */
827 efree(HTTP_G(ctype
));
829 HTTP_G(ctype
) = estrndup(content_type
, ct_len
);
831 ct_header
= ecalloc(1, sizeof("Content-Type: ") + ct_len
);
832 sprintf(ct_header
, "Content-Type: %s", content_type
);
834 if (SUCCESS
!= (status
= http_send_header(ct_header
))) {
835 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
836 "Couldn't send '%s' header", ct_header
);
843 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
844 PHP_HTTP_API STATUS
_http_send_content_disposition(const char *filename
,
845 const size_t f_len
, const int send_inline TSRMLS_DC
)
851 cd_header
= ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len
);
852 sprintf(cd_header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
854 cd_header
= ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len
);
855 sprintf(cd_header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
858 if (SUCCESS
!= (status
= http_send_header(cd_header
))) {
859 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", cd_header
);
866 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
867 PHP_HTTP_API STATUS
_http_cache_last_modified(const time_t last_modified
,
868 const time_t send_modified
, const char *cache_control
, const size_t cc_len TSRMLS_DC
)
871 http_send_cache_control(cache_control
, cc_len
);
874 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
875 if (SUCCESS
== http_send_status(304)) {
878 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
882 return http_send_last_modified(send_modified
);
886 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
887 PHP_HTTP_API STATUS
_http_cache_etag(const char *etag
, const size_t etag_len
,
888 const char *cache_control
, const size_t cc_len TSRMLS_DC
)
891 http_send_cache_control(cache_control
, cc_len
);
895 http_send_etag(etag
, etag_len
);
896 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
897 if (SUCCESS
== http_send_status(304)) {
900 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
906 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
907 if (!HTTP_G(etag_started
)) {
908 if (SUCCESS
== http_start_ob_handler(_http_ob_etaghandler
, "ob_etaghandler", 4096, 1)) {
909 HTTP_G(etag_started
) = 1;
912 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not start ob_etaghandler");
920 /* {{{ char *http_absolute_uri(char *) */
921 PHP_HTTP_API
char *_http_absolute_uri_ex(
922 const char *url
, size_t url_len
,
923 const char *proto
, size_t proto_len
,
924 const char *host
, size_t host_len
,
925 unsigned port TSRMLS_DC
)
927 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
930 php_url
*purl
, furl
= {NULL
};
933 char *scheme
= NULL
, *URL
= ecalloc(1, HTTP_URI_MAXLEN
+ 1);
935 if ((!url
|| !url_len
) && (
936 (!(url
= SG(request_info
).request_uri
)) ||
937 (!(url_len
= strlen(SG(request_info
).request_uri
))))) {
938 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
939 "Cannot build an absolute URI if supplied URL and REQUEST_URI is empty");
943 if (!(purl
= php_url_parse((char *) url
))) {
944 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse supplied URL");
948 furl
.user
= purl
->user
;
949 furl
.pass
= purl
->pass
;
950 furl
.path
= purl
->path
;
951 furl
.query
= purl
->query
;
952 furl
.fragment
= purl
->fragment
;
954 if (proto
&& proto_len
) {
955 furl
.scheme
= scheme
= estrdup(proto
);
956 } else if (purl
->scheme
) {
957 furl
.scheme
= purl
->scheme
;
958 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
959 } else if (port
&& (se
= getservbyport(port
, "tcp"))) {
960 furl
.scheme
= (scheme
= estrdup(se
->s_name
));
963 furl
.scheme
= "http";
968 } else if (purl
->port
) {
969 furl
.port
= purl
->port
;
970 } else if (strncmp(furl
.scheme
, "http", 4)) {
971 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
972 if (se
= getservbyname(furl
.scheme
, "tcp")) {
973 furl
.port
= se
->s_port
;
977 furl
.port
= (furl
.scheme
[4] == 's') ? 443 : 80;
981 furl
.host
= (char *) host
;
982 } else if (purl
->host
) {
983 furl
.host
= purl
->host
;
984 } else if ( (zhost
= http_get_server_var("HTTP_HOST")) ||
985 (zhost
= http_get_server_var("SERVER_NAME"))) {
986 furl
.host
= Z_STRVAL_P(zhost
);
988 furl
.host
= "localhost";
991 #define HTTP_URI_STRLCATS(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, sizeof(add_string)-1)
992 #define HTTP_URI_STRLCATL(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, strlen(add_string))
993 #define HTTP_URI_STRLCAT(URL, full_len, add_string, add_len) \
994 if ((full_len += add_len) > HTTP_URI_MAXLEN) { \
995 php_error_docref(NULL TSRMLS_CC, E_NOTICE, \
996 "Absolute URI would have exceeded max URI length (%d bytes) - " \
997 "tried to add %d bytes ('%s')", \
998 HTTP_URI_MAXLEN, add_len, add_string); \
1002 php_url_free(purl); \
1005 strcat(URL, add_string); \
1008 HTTP_URI_STRLCATL(URL
, full_len
, furl
.scheme
);
1009 HTTP_URI_STRLCATS(URL
, full_len
, "://");
1012 HTTP_URI_STRLCATL(URL
, full_len
, furl
.user
);
1014 HTTP_URI_STRLCATS(URL
, full_len
, ":");
1015 HTTP_URI_STRLCATL(URL
, full_len
, furl
.pass
);
1017 HTTP_URI_STRLCATS(URL
, full_len
, "@");
1020 HTTP_URI_STRLCATL(URL
, full_len
, furl
.host
);
1022 if ( (!strcmp(furl
.scheme
, "http") && (furl
.port
!= 80)) ||
1023 (!strcmp(furl
.scheme
, "https") && (furl
.port
!= 443))) {
1024 char port_string
[8] = {0};
1025 snprintf(port_string
, 7, ":%u", furl
.port
);
1026 HTTP_URI_STRLCATL(URL
, full_len
, port_string
);
1030 if (furl
.path
[0] != '/') {
1031 HTTP_URI_STRLCATS(URL
, full_len
, "/");
1033 HTTP_URI_STRLCATL(URL
, full_len
, furl
.path
);
1035 HTTP_URI_STRLCATS(URL
, full_len
, "/");
1039 HTTP_URI_STRLCATS(URL
, full_len
, "?");
1040 HTTP_URI_STRLCATL(URL
, full_len
, furl
.query
);
1043 if (furl
.fragment
) {
1044 HTTP_URI_STRLCATS(URL
, full_len
, "#");
1045 HTTP_URI_STRLCATL(URL
, full_len
, furl
.fragment
);
1057 /* {{{ char *http_negotiate_q(char *, HashTable *, char *) */
1058 PHP_HTTP_API
char *_http_negotiate_q(const char *entry
, const HashTable
*supported
, const char *def TSRMLS_DC
)
1060 zval
*zaccept
, zdelim
, zarray
, zentries
, **zentry
, **zsupp
;
1061 char *q_ptr
= NULL
, *key
= NULL
;
1065 HTTP_GSC(zaccept
, entry
, estrdup(def
));
1067 array_init(&zarray
);
1068 array_init(&zentries
);
1070 Z_STRVAL(zdelim
) = ",";
1071 Z_STRLEN(zdelim
) = 1;
1073 php_explode(&zdelim
, zaccept
, &zarray
, -1);
1075 FOREACH_HASH_VAL(Z_ARRVAL(zarray
), zentry
) {
1076 if (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';')) {
1077 qual
= strtod(q_ptr
+ 3, NULL
);
1081 qual
= 1000.0 - i
++;
1083 FOREACH_HASH_VAL((HashTable
*)supported
, zsupp
) {
1084 if (!strcasecmp(Z_STRVAL_PP(zsupp
), Z_STRVAL_PP(zentry
))) {
1085 add_assoc_double(&zentries
, Z_STRVAL_PP(zsupp
), qual
);
1092 zend_hash_sort(Z_ARRVAL(zentries
), zend_qsort
, http_sort_q
, 0 TSRMLS_CC
);
1094 FOREACH_HASH_KEY(Z_ARRVAL(zentries
), key
, idx
) {
1097 zval_dtor(&zentries
);
1101 zval_dtor(&zentries
);
1103 return estrdup(def
);
1107 /* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
1108 PHP_HTTP_API http_range_status
_http_get_request_ranges(HashTable
*ranges
,
1109 const size_t length TSRMLS_DC
)
1113 long begin
= -1, end
= -1, *ptr
;
1115 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
1116 range
= Z_STRVAL_P(zrange
);
1118 if (strncmp(range
, "bytes=", sizeof("bytes=") - 1)) {
1119 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Range header misses bytes=");
1124 range
+= sizeof("bytes=") - 1;
1127 switch (c
= *(range
++))
1133 case '1': case '2': case '3':
1134 case '4': case '5': case '6':
1135 case '7': case '8': case '9':
1137 * If the value of the pointer is already set (non-negative)
1138 * then multiply its value by ten and add the current value,
1139 * else initialise the pointers value with the current value
1141 * This let us recognize empty fields when validating the
1142 * ranges, i.e. a "-10" for begin and "12345" for the end
1143 * was the following range request: "Range: bytes=0-12345";
1144 * While a "-1" for begin and "12345" for the end would
1145 * have been: "Range: bytes=-12345".
1160 /* IE - ignore for now */
1167 /* validate ranges */
1172 if (length
<= end
) {
1180 if (length
<= end
) {
1183 begin
= length
- end
;
1193 if (length
<= begin
) {
1201 if ( (length
<= begin
) ||
1213 MAKE_STD_ZVAL(zentry
);
1215 add_index_long(zentry
, 0, begin
);
1216 add_index_long(zentry
, 1, end
);
1217 zend_hash_next_index_insert(ranges
, &zentry
, sizeof(zval
*), NULL
);
1235 /* {{{ STATUS http_send_ranges(HashTable *, void *, size_t, http_send_mode) */
1236 PHP_HTTP_API STATUS
_http_send_ranges(HashTable
*ranges
, const void *data
, const size_t size
, const http_send_mode mode TSRMLS_DC
)
1238 long **begin
, **end
;
1242 if (zend_hash_num_elements(ranges
) == 1) {
1243 char range_header
[256] = {0};
1245 if (SUCCESS
!= zend_hash_index_find(ranges
, 0, (void **) &zrange
) ||
1246 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1247 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1251 /* Send HTTP 206 Partial Content */
1252 http_send_status(206);
1254 /* send content range header */
1255 snprintf(range_header
, 255, "Content-Range: bytes %d-%d/%d", **begin
, **end
, size
);
1256 http_send_header(range_header
);
1258 /* send requested chunk */
1259 return http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1264 char bound
[23] = {0}, preface
[1024] = {0},
1265 multi_header
[68] = "Content-Type: multipart/byteranges; boundary=";
1267 /* Send HTTP 206 Partial Content */
1268 http_send_status(206);
1270 /* send multipart/byteranges header */
1271 snprintf(bound
, 22, "--%d%0.9f", time(NULL
), php_combined_lcg(TSRMLS_C
));
1272 strncat(multi_header
, bound
+ 2, 21);
1273 http_send_header(multi_header
);
1275 /* send each requested chunk */
1276 FOREACH_HASH_VAL(ranges
, zrange
) {
1277 if (SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1278 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1282 snprintf(preface
, 1023,
1284 HTTP_CRLF
"Content-Type: %s"
1285 HTTP_CRLF
"Content-Range: bytes %ld-%ld/%ld"
1290 HTTP_G(ctype
) ? HTTP_G(ctype
) : "application/x-octetstream",
1296 php_body_write(preface
, strlen(preface
) TSRMLS_CC
);
1297 http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1300 /* write boundary once more */
1301 php_body_write(HTTP_CRLF
, sizeof(HTTP_CRLF
) - 1 TSRMLS_CC
);
1302 php_body_write(bound
, strlen(bound
) TSRMLS_CC
);
1303 php_body_write("--", 2 TSRMLS_CC
);
1310 /* {{{ STATUS http_send(void *, size_t, http_send_mode) */
1311 PHP_HTTP_API STATUS
_http_send(const void *data_ptr
, const size_t data_size
,
1312 const http_send_mode data_mode TSRMLS_DC
)
1315 http_range_status range_status
;
1325 /* stop on-the-fly etag generation */
1326 if (cache_etag
= HTTP_G(etag_started
)) {
1328 HTTP_G(etag_started
) = 0;
1329 /* never ever use the output to compute the ETag if http_send() is used */
1330 php_end_ob_buffers(0 TSRMLS_CC
);
1333 zend_hash_init(&ranges
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
1334 range_status
= http_get_request_ranges(&ranges
, data_size
);
1336 if (range_status
== RANGE_ERR
) {
1337 zend_hash_destroy(&ranges
);
1338 http_send_status(416);
1342 /* Range Request - only send ranges if entity hasn't changed */
1343 if ( range_status
== RANGE_OK
&&
1344 http_etag_match_ex("HTTP_IF_MATCH", HTTP_G(etag
), 0) &&
1345 http_modified_match_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod
), 0)) {
1346 STATUS result
= http_send_ranges(&ranges
, data_ptr
, data_size
, data_mode
);
1347 zend_hash_destroy(&ranges
);
1351 zend_hash_destroy(&ranges
);
1353 /* send 304 Not Modified if etag matches */
1358 if (!(etag
= http_etag(data_ptr
, data_size
, data_mode
))) {
1362 http_send_etag(etag
, 32);
1363 etag_match
= http_etag_match("HTTP_IF_NONE_MATCH", etag
);
1367 return http_send_status(304);
1371 /* send 304 Not Modified if last modified matches */
1372 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod
))) {
1373 return http_send_status(304);
1376 /* send full entity */
1377 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
1381 /* {{{ STATUS http_send_stream(php_stream *) */
1382 PHP_HTTP_API STATUS
_http_send_stream_ex(php_stream
*file
,
1383 zend_bool close_stream TSRMLS_DC
)
1387 if ((!file
) || php_stream_stat(file
, &HTTP_G(ssb
))) {
1391 status
= http_send(file
, HTTP_G(ssb
).sb
.st_size
, SEND_RSRC
);
1394 php_stream_close(file
);
1401 /* {{{ STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1402 PHP_HTTP_API STATUS
_http_chunked_decode(const char *encoded
,
1403 const size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
1409 *decoded
= ecalloc(1, encoded_len
);
1413 while (((e_ptr
- encoded
) - encoded_len
) > 0) {
1414 char hex_len
[9] = {0};
1415 size_t chunk_len
= 0;
1418 /* read in chunk size */
1419 while (isxdigit(*e_ptr
)) {
1421 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1422 "Chunk size is too long: 0x%s...", hex_len
);
1426 hex_len
[i
++] = *e_ptr
++;
1429 /* reached the end */
1430 if (!strcmp(hex_len
, "0")) {
1435 if (strncmp(e_ptr
, HTTP_CRLF
, 2)) {
1436 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1437 "Invalid character (expected 0x0D 0x0A; got: %x %x)",
1438 *e_ptr
, *(e_ptr
+ 1));
1446 chunk_len
= strtol(hex_len
, &error
, 16);
1447 if (error
== hex_len
) {
1448 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1449 "Invalid chunk size string: '%s'", hex_len
);
1455 memcpy(d_ptr
, e_ptr
+= 2, chunk_len
);
1457 e_ptr
+= chunk_len
+ 2;
1458 *decoded_len
+= chunk_len
;
1465 /* {{{ STATUS http_split_response(zval *, zval *, zval *) */
1466 PHP_HTTP_API STATUS
_http_split_response(zval
*response
, zval
*headers
, zval
*body TSRMLS_DC
)
1470 STATUS status
= http_split_response_ex(Z_STRVAL_P(response
), Z_STRLEN_P(response
), Z_ARRVAL_P(headers
), &b
, &l
);
1471 ZVAL_STRINGL(body
, b
, l
, 0);
1476 /* {{{ STATUS http_split_response(char *, size_t, HashTable *, char **, size_t *) */
1477 PHP_HTTP_API STATUS
_http_split_response_ex(char *response
,
1478 size_t response_len
, HashTable
*headers
, char **body
, size_t *body_len TSRMLS_DC
)
1480 char *header
= response
, *real_body
= NULL
;
1482 while (0 < (response_len
- (response
- header
+ 4))) {
1483 if ( (*response
++ == '\r') &&
1484 (*response
++ == '\n') &&
1485 (*response
++ == '\r') &&
1486 (*response
++ == '\n')) {
1487 real_body
= response
;
1492 if (real_body
&& (*body_len
= (response_len
- (real_body
- header
)))) {
1493 *body
= ecalloc(1, *body_len
+ 1);
1494 memcpy(*body
, real_body
, *body_len
);
1497 return http_parse_headers_ex(header
, real_body
? response_len
- *body_len
: response_len
, headers
, 1);
1501 /* {{{ STATUS http_parse_headers(char *, long, zval *) */
1502 PHP_HTTP_API STATUS
_http_parse_headers_ex(char *header
, int header_len
,
1503 HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
1505 char *colon
= NULL
, *line
= NULL
, *begin
= header
;
1508 Z_ARRVAL(array
) = headers
;
1510 if (header_len
< 2) {
1515 if (!strncmp(header
, "HTTP/1.", 7)) {
1516 char *end
= strstr(header
, HTTP_CRLF
);
1517 add_assoc_stringl(&array
, "Status",
1518 header
+ sizeof("HTTP/1.x ") - 1,
1519 end
- (header
+ sizeof("HTTP/1.x ") - 1), 1);
1525 while (header_len
>= (line
- begin
)) {
1531 --value_len
; /* we don't have CR so value length is one char less */
1533 if (colon
&& ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t')))) {
1535 /* skip empty key */
1536 if (header
!= colon
) {
1537 char *key
= estrndup(header
, colon
- header
);
1540 key
= pretty_key(key
, colon
- header
, 1, 1);
1543 value_len
+= line
- colon
- 1;
1545 /* skip leading ws */
1546 while (isspace(*(++colon
))) --value_len
;
1547 /* skip trailing ws */
1548 while (isspace(colon
[value_len
- 1])) --value_len
;
1550 if (value_len
< 1) {
1551 /* hm, empty header? */
1552 add_assoc_stringl(&array
, key
, "", 0, 1);
1554 add_assoc_stringl(&array
, key
, colon
, value_len
, 1);
1561 header
+= line
- header
;
1576 /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
1577 PHP_HTTP_API
void _http_get_request_headers_ex(HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
1583 Z_ARRVAL(array
) = headers
;
1585 FOREACH_HASH_KEY(HTTP_SERVER_VARS
, key
, idx
) {
1586 if (key
&& !strncmp(key
, "HTTP_", 5)) {
1591 key
= pretty_key(key
, strlen(key
), 1, 1);
1594 zend_hash_get_current_data(HTTP_SERVER_VARS
, (void **) &header
);
1595 add_assoc_stringl(&array
, key
, Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1602 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, int, char **, size_t *) */
1603 PHP_HTTP_API STATUS
_http_urlencode_hash_ex(HashTable
*hash
, int override_argsep
,
1604 char *pre_encoded_data
, size_t pre_encoded_len
,
1605 char **encoded_data
, size_t *encoded_len TSRMLS_DC
)
1607 smart_str qstr
= {0};
1609 if (override_argsep
) {
1610 HTTP_URL_ARGSEP_OVERRIDE
;
1613 if (pre_encoded_len
&& pre_encoded_data
) {
1614 smart_str_appendl(&qstr
, pre_encoded_data
, pre_encoded_len
);
1617 if (SUCCESS
!= php_url_encode_hash_ex(hash
, &qstr
, NULL
, 0, NULL
, 0, NULL
, 0, NULL TSRMLS_CC
)) {
1618 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't encode query data");
1622 if (override_argsep
) {
1623 HTTP_URL_ARGSEP_RESTORE
;
1628 if (override_argsep
) {
1629 HTTP_URL_ARGSEP_RESTORE
;
1634 *encoded_data
= qstr
.c
;
1636 *encoded_len
= qstr
.len
;
1643 /* {{{ STATUS http_auth_header(char *, char*) */
1644 PHP_HTTP_API STATUS
_http_auth_header(const char *type
, const char *realm TSRMLS_DC
)
1646 char realm_header
[1024] = {0};
1647 snprintf(realm_header
, 1023, "WWW-Authenticate: %s realm=\"%s\"", type
, realm
);
1648 return http_send_status_header(401, realm_header
);
1652 /* {{{ STATUS http_auth_credentials(char **, char **) */
1653 PHP_HTTP_API STATUS
_http_auth_credentials(char **user
, char **pass TSRMLS_DC
)
1655 if (strncmp(sapi_module
.name
, "isapi", 5)) {
1656 zval
*zuser
, *zpass
;
1658 HTTP_GSC(zuser
, "PHP_AUTH_USER", FAILURE
);
1659 HTTP_GSC(zpass
, "PHP_AUTH_PW", FAILURE
);
1661 *user
= estrndup(Z_STRVAL_P(zuser
), Z_STRLEN_P(zuser
));
1662 *pass
= estrndup(Z_STRVAL_P(zpass
), Z_STRLEN_P(zpass
));
1667 HTTP_GSC(zauth
, "HTTP_AUTHORIZATION", FAILURE
);
1669 char *decoded
, *colon
;
1671 decoded
= php_base64_decode(Z_STRVAL_P(zauth
), Z_STRLEN_P(zauth
),
1674 if (colon
= strchr(decoded
+ 6, ':')) {
1675 *user
= estrndup(decoded
+ 6, colon
- decoded
- 6);
1676 *pass
= estrndup(colon
+ 1, decoded
+ decoded_len
- colon
- 6 - 1);
1687 #ifndef ZEND_ENGINE_2
1688 /* {{{ php_url_encode_hash
1689 Author: Sara Golemon <pollita@php.net> */
1690 PHP_HTTP_API STATUS
php_url_encode_hash_ex(HashTable
*ht
, smart_str
*formstr
,
1691 const char *num_prefix
, int num_prefix_len
,
1692 const char *key_prefix
, int key_prefix_len
,
1693 const char *key_suffix
, int key_suffix_len
,
1694 zval
*type TSRMLS_DC
)
1696 char *arg_sep
= NULL
, *key
= NULL
, *ekey
, *newprefix
, *p
;
1697 int arg_sep_len
, key_len
, ekey_len
, key_type
, newprefix_len
;
1699 zval
**zdata
= NULL
, *copyzval
;
1705 if (ht
->nApplyCount
> 0) {
1706 /* Prevent recursion */
1710 arg_sep
= INI_STR("arg_separator.output");
1711 if (!arg_sep
|| !strlen(arg_sep
)) {
1712 arg_sep
= HTTP_URL_ARGSEP_DEFAULT
;
1714 arg_sep_len
= strlen(arg_sep
);
1716 for (zend_hash_internal_pointer_reset(ht
);
1717 (key_type
= zend_hash_get_current_key_ex(ht
, &key
, &key_len
, &idx
, 0, NULL
)) != HASH_KEY_NON_EXISTANT
;
1718 zend_hash_move_forward(ht
)
1720 if (key_type
== HASH_KEY_IS_STRING
&& key_len
&& key
[key_len
-1] == '\0') {
1721 /* We don't want that trailing NULL */
1725 #ifdef ZEND_ENGINE_2
1726 /* handling for private & protected object properties */
1727 if (key
&& *key
== '\0' && type
!= NULL
) {
1730 zend_object
*zobj
= zend_objects_get_address(type TSRMLS_CC
);
1731 if (zend_check_property_access(zobj
, key TSRMLS_CC
) != SUCCESS
) {
1732 /* private or protected property access outside of the class */
1735 zend_unmangle_property_name(key
, &tmp
, &key
);
1736 key_len
= strlen(key
);
1740 if (zend_hash_get_current_data_ex(ht
, (void **)&zdata
, NULL
) == FAILURE
|| !zdata
|| !(*zdata
)) {
1741 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Error traversing form data array.");
1744 if (Z_TYPE_PP(zdata
) == IS_ARRAY
|| Z_TYPE_PP(zdata
) == IS_OBJECT
) {
1745 if (key_type
== HASH_KEY_IS_STRING
) {
1746 ekey
= php_url_encode(key
, key_len
, &ekey_len
);
1747 newprefix_len
= key_suffix_len
+ ekey_len
+ key_prefix_len
+ 1;
1748 newprefix
= emalloc(newprefix_len
+ 1);
1752 memcpy(p
, key_prefix
, key_prefix_len
);
1753 p
+= key_prefix_len
;
1756 memcpy(p
, ekey
, ekey_len
);
1761 memcpy(p
, key_suffix
, key_suffix_len
);
1762 p
+= key_suffix_len
;
1768 /* Is an integer key */
1769 ekey_len
= spprintf(&ekey
, 12, "%ld", idx
);
1770 newprefix_len
= key_prefix_len
+ num_prefix_len
+ ekey_len
+ key_suffix_len
+ 1;
1771 newprefix
= emalloc(newprefix_len
+ 1);
1775 memcpy(p
, key_prefix
, key_prefix_len
);
1776 p
+= key_prefix_len
;
1779 memcpy(p
, num_prefix
, num_prefix_len
);
1780 p
+= num_prefix_len
;
1782 memcpy(p
, ekey
, ekey_len
);
1787 memcpy(p
, key_suffix
, key_suffix_len
);
1788 p
+= key_suffix_len
;
1794 php_url_encode_hash_ex(HASH_OF(*zdata
), formstr
, NULL
, 0, newprefix
, newprefix_len
, "]", 1, (Z_TYPE_PP(zdata
) == IS_OBJECT
? *zdata
: NULL
) TSRMLS_CC
);
1797 } else if (Z_TYPE_PP(zdata
) == IS_NULL
|| Z_TYPE_PP(zdata
) == IS_RESOURCE
) {
1798 /* Skip these types */
1802 smart_str_appendl(formstr
, arg_sep
, arg_sep_len
);
1804 /* Simple key=value */
1805 smart_str_appendl(formstr
, key_prefix
, key_prefix_len
);
1806 if (key_type
== HASH_KEY_IS_STRING
) {
1807 ekey
= php_url_encode(key
, key_len
, &ekey_len
);
1808 smart_str_appendl(formstr
, ekey
, ekey_len
);
1813 smart_str_appendl(formstr
, num_prefix
, num_prefix_len
);
1815 ekey_len
= spprintf(&ekey
, 12, "%ld", idx
);
1816 smart_str_appendl(formstr
, ekey
, ekey_len
);
1819 smart_str_appendl(formstr
, key_suffix
, key_suffix_len
);
1820 smart_str_appendl(formstr
, "=", 1);
1821 switch (Z_TYPE_PP(zdata
)) {
1823 ekey
= php_url_encode(Z_STRVAL_PP(zdata
), Z_STRLEN_PP(zdata
), &ekey_len
);
1827 ekey_len
= spprintf(&ekey
, 12, "%ld", Z_LVAL_PP(zdata
));
1830 ekey_len
= spprintf(&ekey
, 48, "%.*G", (int) EG(precision
), Z_DVAL_PP(zdata
));
1833 /* fall back on convert to string */
1834 MAKE_STD_ZVAL(copyzval
);
1835 *copyzval
= **zdata
;
1836 zval_copy_ctor(copyzval
);
1837 convert_to_string_ex(©zval
);
1838 ekey
= php_url_encode(Z_STRVAL_P(copyzval
), Z_STRLEN_P(copyzval
), &ekey_len
);
1839 zval_ptr_dtor(©zval
);
1841 smart_str_appendl(formstr
, ekey
, ekey_len
);
1849 #endif /* !ZEND_ENDGINE_2 */
1851 /* }}} public API */
1858 * vim600: noet sw=4 ts=4 fdm=marker
1859 * vim<600: noet sw=4 ts=4