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_lcg.h"
44 # include "ext/standard/php_http.h"
48 #include "php_http_api.h"
49 #include "php_http_std_defs.h"
51 ZEND_DECLARE_MODULE_GLOBALS(http
)
53 /* {{{ day/month names */
54 static const char *days
[] = {
55 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
57 static const char *wkdays
[] = {
58 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
60 static const char *weekdays
[] = {
61 "Monday", "Tuesday", "Wednesday",
62 "Thursday", "Friday", "Saturday", "Sunday"
64 static const char *months
[] = {
65 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
66 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
73 static const struct time_zone
{
77 {"GMT", 0}, /* Greenwich Mean */
78 {"UTC", 0}, /* Universal (Coordinated) */
79 {"WET", 0}, /* Western European */
80 {"BST", 0}, /* British Summer */
81 {"WAT", 60}, /* West Africa */
82 {"AST", 240}, /* Atlantic Standard */
83 {"ADT", 240}, /* Atlantic Daylight */
84 {"EST", 300}, /* Eastern Standard */
85 {"EDT", 300}, /* Eastern Daylight */
86 {"CST", 360}, /* Central Standard */
87 {"CDT", 360}, /* Central Daylight */
88 {"MST", 420}, /* Mountain Standard */
89 {"MDT", 420}, /* Mountain Daylight */
90 {"PST", 480}, /* Pacific Standard */
91 {"PDT", 480}, /* Pacific Daylight */
92 {"YST", 540}, /* Yukon Standard */
93 {"YDT", 540}, /* Yukon Daylight */
94 {"HST", 600}, /* Hawaii Standard */
95 {"HDT", 600}, /* Hawaii Daylight */
96 {"CAT", 600}, /* Central Alaska */
97 {"AHST", 600}, /* Alaska-Hawaii Standard */
98 {"NT", 660}, /* Nome */
99 {"IDLW", 720}, /* International Date Line West */
100 {"CET", -60}, /* Central European */
101 {"MET", -60}, /* Middle European */
102 {"MEWT", -60}, /* Middle European Winter */
103 {"MEST", -120}, /* Middle European Summer */
104 {"CEST", -120}, /* Central European Summer */
105 {"MESZ", -60}, /* Middle European Summer */
106 {"FWT", -60}, /* French Winter */
107 {"FST", -60}, /* French Summer */
108 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
109 {"WAST", -420}, /* West Australian Standard */
110 {"WADT", -420}, /* West Australian Daylight */
111 {"CCT", -480}, /* China Coast, USSR Zone 7 */
112 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
113 {"EAST", -600}, /* Eastern Australian Standard */
114 {"EADT", -600}, /* Eastern Australian Daylight */
115 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
116 {"NZT", -720}, /* New Zealand */
117 {"NZST", -720}, /* New Zealand Standard */
118 {"NZDT", -720}, /* New Zealand Daylight */
119 {"IDLE", -720}, /* International Date Line East */
125 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
);
126 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
127 static STATUS
_http_send_chunk(const void *data
, size_t begin
, size_t end
, http_send_mode mode TSRMLS_DC
);
129 static int check_day(char *day
, size_t len
);
130 static int check_month(char *month
);
131 static int check_tzone(char *tzone
);
133 static int http_ob_stack_get(php_ob_buffer
*, php_ob_buffer
**);
135 /* {{{ static int http_sort_q(const void *, const void *) */
136 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
139 zval result
, *first
, *second
;
141 f
= *((Bucket
**) a
);
142 s
= *((Bucket
**) b
);
144 first
= *((zval
**) f
->pData
);
145 second
= *((zval
**) s
->pData
);
147 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
150 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
154 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
156 static STATUS
_http_send_chunk(const void *data
, size_t begin
, size_t end
, http_send_mode mode TSRMLS_DC
)
158 long len
= end
- begin
;
166 php_stream
*s
= (php_stream
*) data
;
168 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
172 buf
= (char *) ecalloc(1, HTTP_SENDBUF_SIZE
);
173 /* read into buf and write out */
174 while ((len
-= HTTP_SENDBUF_SIZE
) >= 0) {
175 if (!(read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
))) {
179 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
183 /* ob_flush() && flush() */
184 php_end_ob_buffer(1, 1 TSRMLS_CC
);
185 sapi_flush(TSRMLS_C
);
188 /* read & write left over */
190 if (read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
+ len
)) {
191 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
199 /* ob_flush() & flush() */
200 php_end_ob_buffer(1, 1 TSRMLS_CC
);
201 sapi_flush(TSRMLS_C
);
209 char *s
= (char *) data
+ begin
;
211 while ((len
-= HTTP_SENDBUF_SIZE
) >= 0) {
212 if (HTTP_SENDBUF_SIZE
- php_body_write(s
, HTTP_SENDBUF_SIZE TSRMLS_CC
)) {
215 s
+= HTTP_SENDBUF_SIZE
;
216 /* ob_flush() & flush() */
217 php_end_ob_buffer(1, 1 TSRMLS_CC
);
218 sapi_flush(TSRMLS_C
);
221 /* write left over */
223 if (HTTP_SENDBUF_SIZE
+ len
- php_body_write(s
, HTTP_SENDBUF_SIZE
+ len TSRMLS_CC
)) {
226 /* ob_flush() & flush() */
227 php_end_ob_buffer(1, 1 TSRMLS_CC
);
228 sapi_flush(TSRMLS_C
);
240 /* {{{ Day/Month/TZ checks for http_parse_date()
241 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
242 static int check_day(char *day
, size_t len
)
245 const char * const *check
= (len
> 3) ? &weekdays
[0] : &wkdays
[0];
246 for (i
= 0; i
< 7; i
++) {
247 if (!strcmp(day
, check
[0])) {
255 static int check_month(char *month
)
258 const char * const *check
= &months
[0];
259 for (i
= 0; i
< 12; i
++) {
260 if (!strcmp(month
, check
[0])) {
268 /* return the time zone offset between GMT and the input one, in number
269 of seconds or -1 if the timezone wasn't found/legal */
271 static int check_tzone(char *tzone
)
274 const struct time_zone
*check
= time_zones
;
275 for (i
= 0; i
< sizeof(time_zones
) / sizeof(time_zones
[0]); i
++) {
276 if (!strcmp(tzone
, check
->name
)) {
277 return check
->offset
* 60;
285 /* char *pretty_key(char *, size_t, zend_bool, zebd_bool) */
286 char *pretty_key(char *key
, size_t key_len
, zend_bool uctitle
, zend_bool xhyphen
)
288 if (key
&& key_len
) {
289 unsigned i
, wasalpha
;
290 if (wasalpha
= isalpha(key
[0])) {
291 key
[0] = uctitle
? toupper(key
[0]) : tolower(key
[0]);
293 for (i
= 1; i
< key_len
; i
++) {
294 if (isalpha(key
[i
])) {
295 key
[i
] = ((!wasalpha
) && uctitle
) ? toupper(key
[i
]) : tolower(key
[i
]);
298 if (xhyphen
&& (key
[i
] == '_')) {
309 /* {{{ static STATUS http_ob_stack_get(php_ob_buffer *, php_ob_buffer **) */
310 static STATUS
http_ob_stack_get(php_ob_buffer
*o
, php_ob_buffer
**s
)
313 php_ob_buffer
*b
= emalloc(sizeof(php_ob_buffer
));
314 b
->handler_name
= estrdup(o
->handler_name
);
315 b
->buffer
= estrndup(o
->buffer
, o
->text_length
);
316 b
->text_length
= o
->text_length
;
317 b
->chunk_size
= o
->chunk_size
;
328 /* {{{ char *http_date(time_t) */
329 PHP_HTTP_API
char *_http_date(time_t t TSRMLS_DC
)
331 struct tm
*gmtime
, tmbuf
;
333 if (gmtime
= php_gmtime_r(&t
, &tmbuf
)) {
334 char *date
= ecalloc(1, 31);
336 "%s, %02d %s %04d %02d:%02d:%02d GMT",
337 days
[gmtime
->tm_wday
], gmtime
->tm_mday
,
338 months
[gmtime
->tm_mon
], gmtime
->tm_year
+ 1900,
339 gmtime
->tm_hour
, gmtime
->tm_min
, gmtime
->tm_sec
348 /* {{{ time_t http_parse_date(char *)
349 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
350 PHP_HTTP_API
time_t _http_parse_date(const char *date
)
353 int tz_offset
= -1, year
= -1, month
= -1, monthday
= -1, weekday
= -1,
354 hours
= -1, minutes
= -1, seconds
= -1;
356 enum assume_next dignext
= DATE_MDAY
;
357 const char *indate
= date
;
359 int found
= 0, part
= 0; /* max 6 parts */
361 while (*date
&& (part
< 6)) {
364 while (*date
&& !isalnum(*date
)) {
368 if (isalpha(*date
)) {
369 /* a name coming up */
372 sscanf(date
, "%31[A-Za-z]", buf
);
376 weekday
= check_day(buf
, len
);
382 if (!found
&& (month
== -1)) {
383 month
= check_month(buf
);
389 if (!found
&& (tz_offset
== -1)) {
390 /* this just must be a time zone string */
391 tz_offset
= check_tzone(buf
);
392 if (tz_offset
!= -1) {
398 return -1; /* bad string */
402 else if (isdigit(*date
)) {
406 if ((seconds
== -1) &&
407 (3 == sscanf(date
, "%02d:%02d:%02d", &hours
, &minutes
, &seconds
))) {
413 val
= (int) strtol(date
, &end
, 10);
415 if ((tz_offset
== -1) && ((end
- date
) == 4) && (val
< 1300) &&
416 (indate
< date
) && ((date
[-1] == '+' || date
[-1] == '-'))) {
417 /* four digits and a value less than 1300 and it is preceeded with
418 a plus or minus. This is a time zone indication. */
420 tz_offset
= (val
/ 100 * 60 + val
% 100) * 60;
422 /* the + and - prefix indicates the local time compared to GMT,
423 this we need ther reversed math to get what we want */
424 tz_offset
= date
[-1] == '+' ? -tz_offset
: tz_offset
;
427 if (((end
- date
) == 8) && (year
== -1) && (month
== -1) && (monthday
== -1)) {
428 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
431 month
= (val
% 10000) / 100 - 1; /* month is 0 - 11 */
432 monthday
= val
% 100;
435 if (!found
&& (dignext
== DATE_MDAY
) && (monthday
== -1)) {
436 if ((val
> 0) && (val
< 32)) {
443 if (!found
&& (dignext
== DATE_YEAR
) && (year
== -1)) {
447 year
+= year
> 70 ? 1900 : 2000;
466 seconds
= minutes
= hours
= 0; /* no time, make it zero */
469 if ((-1 == monthday
) || (-1 == month
) || (-1 == year
)) {
470 /* lacks vital info, fail */
474 if (sizeof(time_t) < 5) {
475 /* 32 bit time_t can only hold dates to the beginning of 2038 */
484 tm
.tm_mday
= monthday
;
486 tm
.tm_year
= year
- 1900;
493 /* time zone adjust */
495 struct tm
*gmt
, keeptime2
;
499 if(!(gmt
= php_gmtime_r(&t
, &keeptime2
))) {
500 return -1; /* illegal date/time */
505 /* Add the time zone diff (between the given timezone and GMT) and the
506 diff between the local time zone and GMT. */
507 delta
= (tz_offset
!= -1 ? tz_offset
: 0) + (t
- t2
);
509 if((delta
> 0) && (t
+ delta
< t
)) {
510 return -1; /* time_t overflow */
520 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
521 PHP_HTTP_API
char *_http_etag(const void *data_ptr
, size_t data_len
, http_send_mode data_mode TSRMLS_DC
)
523 char ssb_buf
[128] = {0};
524 unsigned char digest
[16];
526 char *new_etag
= ecalloc(1, 33);
533 PHP_MD5Update(&ctx
, data_ptr
, data_len
);
537 if (!HTTP_G(ssb
).sb
.st_ino
) {
538 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
542 snprintf(ssb_buf
, 127, "%ld=%ld=%ld",
543 HTTP_G(ssb
).sb
.st_mtime
,
544 HTTP_G(ssb
).sb
.st_ino
,
545 HTTP_G(ssb
).sb
.st_size
547 PHP_MD5Update(&ctx
, ssb_buf
, strlen(ssb_buf
));
556 PHP_MD5Final(digest
, &ctx
);
557 make_digest(new_etag
, digest
);
563 /* {{{ time_t http_lmod(void *, http_send_mode) */
564 PHP_HTTP_API
time_t _http_lmod(const void *data_ptr
, http_send_mode data_mode TSRMLS_DC
)
575 if (!HTTP_G(ssb
).sb
.st_mtime
) {
576 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
580 return HTTP_G(ssb
).sb
.st_mtime
;
585 if (!HTTP_G(ssb
).sb
.st_mtime
) {
586 if(php_stream_stat_path(Z_STRVAL_P((zval
*) data_ptr
), &HTTP_G(ssb
))) {
590 return HTTP_G(ssb
).sb
.st_mtime
;
596 /* {{{ STATUS http_send_status_header(int, char *) */
597 PHP_HTTP_API STATUS
_http_send_status_header(int status
, const char *header TSRMLS_DC
)
599 sapi_header_line h
= {(char *) header
, strlen(header
), status
};
600 return sapi_header_op(SAPI_HEADER_REPLACE
, &h TSRMLS_CC
);
604 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
605 PHP_HTTP_API zval
*_http_get_server_var_ex(const char *key
, size_t key_size
, zend_bool check TSRMLS_DC
)
608 if (SUCCESS
== zend_hash_find(HTTP_SERVER_VARS
, (char *) key
, key_size
, (void **) &var
)) {
610 return Z_STRVAL_PP(var
) && Z_STRLEN_PP(var
) ? *var
: NULL
;
619 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
620 PHP_HTTP_API
void _http_ob_etaghandler(char *output
, uint output_len
,
621 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
623 char etag
[33] = { 0 };
624 unsigned char digest
[16];
626 if (mode
& PHP_OUTPUT_HANDLER_START
) {
627 PHP_MD5Init(&HTTP_G(etag_md5
));
630 PHP_MD5Update(&HTTP_G(etag_md5
), output
, output_len
);
632 if (mode
& PHP_OUTPUT_HANDLER_END
) {
633 PHP_MD5Final(digest
, &HTTP_G(etag_md5
));
635 /* just do that if desired */
636 if (HTTP_G(etag_started
)) {
637 make_digest(etag
, digest
);
639 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
640 http_send_status(304);
643 http_send_etag(etag
, 32);
648 *handled_output_len
= output_len
;
649 *handled_output
= estrndup(output
, output_len
);
653 /* {{{ STATUS http_start_ob_handler(php_output_handler_func_t, char *, uint, zend_bool) */
654 PHP_HTTP_API STATUS
_http_start_ob_handler(php_output_handler_func_t handler_func
,
655 char *handler_name
, uint chunk_size
, zend_bool erase TSRMLS_DC
)
657 php_ob_buffer
**stack
;
660 if (count
= OG(ob_nesting_level
)) {
661 stack
= ecalloc(count
, sizeof(php_ob_buffer
*));
664 zend_stack_apply_with_argument(&OG(ob_buffers
), ZEND_STACK_APPLY_BOTTOMUP
,
665 (int (*)(void *elem
, void *)) http_ob_stack_get
, stack
);
669 http_ob_stack_get(&OG(active_ob_buffer
), stack
);
672 while (OG(ob_nesting_level
)) {
673 php_end_ob_buffer(0, 0 TSRMLS_CC
);
677 php_ob_set_internal_handler(handler_func
, chunk_size
, handler_name
, erase TSRMLS_CC
);
679 for (i
= 0; i
< count
; i
++) {
680 php_ob_buffer
*s
= stack
[i
];
681 if (strcmp(s
->handler_name
, "default output handler")) {
682 php_start_ob_buffer_named(s
->handler_name
, s
->chunk_size
, s
->erase TSRMLS_CC
);
684 php_body_write(s
->buffer
, s
->text_length TSRMLS_CC
);
685 efree(s
->handler_name
);
697 /* {{{ zend_bool http_modified_match(char *, time_t) */
698 PHP_HTTP_API zend_bool
_http_modified_match_ex(const char *entry
, time_t t
, zend_bool enforce_presence TSRMLS_DC
)
702 char *modified
, *chr_ptr
;
704 HTTP_GSC(zmodified
, entry
, !enforce_presence
);
706 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
707 if (chr_ptr
= strrchr(modified
, ';')) {
710 retval
= (t
<= http_parse_date(modified
));
716 /* {{{ zend_bool http_etag_match(char *, char *) */
717 PHP_HTTP_API zend_bool
_http_etag_match_ex(const char *entry
, const char *etag
, zend_bool enforce_presence TSRMLS_DC
)
723 HTTP_GSC(zetag
, entry
, !enforce_presence
);
725 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
729 quoted_etag
= (char *) emalloc(strlen(etag
) + 3);
730 sprintf(quoted_etag
, "\"%s\"", etag
);
732 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
733 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
735 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
742 /* {{{ STATUS http_send_last_modified(int) */
743 PHP_HTTP_API STATUS
_http_send_last_modified(time_t t TSRMLS_DC
)
746 if (date
= http_date(t
)) {
747 char modified
[96] = "Last-Modified: ";
748 strcat(modified
, date
);
754 return http_send_header(modified
);
760 /* {{{ STATUS http_send_etag(char *, size_t) */
761 PHP_HTTP_API STATUS
_http_send_etag(const char *etag
, size_t etag_len TSRMLS_DC
)
767 php_error_docref(NULL TSRMLS_CC
,E_ERROR
,
768 "Attempt to send empty ETag (previous: %s)\n", HTTP_G(etag
));
776 HTTP_G(etag
) = estrdup(etag
);
778 etag_header
= ecalloc(1, sizeof("ETag: \"\"") + etag_len
);
779 sprintf(etag_header
, "ETag: \"%s\"", etag
);
780 if (SUCCESS
!= (status
= http_send_header(etag_header
))) {
781 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", etag_header
);
788 /* {{{ STATUS http_send_cache_control(char *, size_t) */
789 PHP_HTTP_API STATUS
_http_send_cache_control(const char *cache_control
, size_t cc_len TSRMLS_DC
)
792 char *cc_header
= ecalloc(1, sizeof("Cache-Control: ") + cc_len
);
794 sprintf(cc_header
, "Cache-Control: %s", cache_control
);
795 if (SUCCESS
!= (status
= http_send_header(cc_header
))) {
796 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
,
797 "Could not send '%s' header", cc_header
);
804 /* {{{ STATUS http_send_content_type(char *, size_t) */
805 PHP_HTTP_API STATUS
_http_send_content_type(const char *content_type
, size_t ct_len TSRMLS_DC
)
810 if (!strchr(content_type
, '/')) {
811 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
812 "Content-Type '%s' doesn't seem to consist of a primary and a secondary part",
817 /* remember for multiple ranges */
819 efree(HTTP_G(ctype
));
821 HTTP_G(ctype
) = estrndup(content_type
, ct_len
);
823 ct_header
= ecalloc(1, sizeof("Content-Type: ") + ct_len
);
824 sprintf(ct_header
, "Content-Type: %s", content_type
);
826 if (SUCCESS
!= (status
= http_send_header(ct_header
))) {
827 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
828 "Couldn't send '%s' header", ct_header
);
835 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
836 PHP_HTTP_API STATUS
_http_send_content_disposition(const char *filename
, size_t f_len
, zend_bool send_inline TSRMLS_DC
)
842 cd_header
= ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len
);
843 sprintf(cd_header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
845 cd_header
= ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len
);
846 sprintf(cd_header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
849 if (SUCCESS
!= (status
= http_send_header(cd_header
))) {
850 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", cd_header
);
857 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
858 PHP_HTTP_API STATUS
_http_cache_last_modified(time_t last_modified
,
859 time_t send_modified
, const char *cache_control
, size_t cc_len TSRMLS_DC
)
862 http_send_cache_control(cache_control
, cc_len
);
865 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
866 if (SUCCESS
== http_send_status(304)) {
869 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
873 return http_send_last_modified(send_modified
);
877 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
878 PHP_HTTP_API STATUS
_http_cache_etag(const char *etag
, size_t etag_len
,
879 const char *cache_control
, size_t cc_len TSRMLS_DC
)
882 http_send_cache_control(cache_control
, cc_len
);
886 http_send_etag(etag
, etag_len
);
887 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
888 if (SUCCESS
== http_send_status(304)) {
891 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
897 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
898 if (!HTTP_G(etag_started
)) {
899 if (SUCCESS
== http_start_ob_handler(_http_ob_etaghandler
, "ob_etaghandler", 4096, 1)) {
900 HTTP_G(etag_started
) = 1;
903 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not start ob_etaghandler");
911 /* {{{ char *http_absolute_uri(char *) */
912 PHP_HTTP_API
char *_http_absolute_uri_ex(
913 const char *url
, size_t url_len
,
914 const char *proto
, size_t proto_len
,
915 const char *host
, size_t host_len
,
916 unsigned port TSRMLS_DC
)
918 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
921 php_url
*purl
, furl
= {NULL
};
924 char *scheme
= NULL
, *URL
= ecalloc(1, HTTP_URI_MAXLEN
+ 1);
926 if ((!url
|| !url_len
) && (
927 (!(url
= SG(request_info
).request_uri
)) ||
928 (!(url_len
= strlen(SG(request_info
).request_uri
))))) {
929 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
930 "Cannot build an absolute URI if supplied URL and REQUEST_URI is empty");
934 if (!(purl
= php_url_parse((char *) url
))) {
935 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse supplied URL");
939 furl
.user
= purl
->user
;
940 furl
.pass
= purl
->pass
;
941 furl
.path
= purl
->path
;
942 furl
.query
= purl
->query
;
943 furl
.fragment
= purl
->fragment
;
945 if (proto
&& proto_len
) {
946 furl
.scheme
= scheme
= estrdup(proto
);
947 } else if (purl
->scheme
) {
948 furl
.scheme
= purl
->scheme
;
949 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
950 } else if (port
&& (se
= getservbyport(port
, "tcp"))) {
951 furl
.scheme
= (scheme
= estrdup(se
->s_name
));
954 furl
.scheme
= "http";
959 } else if (purl
->port
) {
960 furl
.port
= purl
->port
;
961 } else if (strncmp(furl
.scheme
, "http", 4)) {
962 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
963 if (se
= getservbyname(furl
.scheme
, "tcp")) {
964 furl
.port
= se
->s_port
;
968 furl
.port
= (furl
.scheme
[4] == 's') ? 443 : 80;
972 furl
.host
= (char *) host
;
973 } else if (purl
->host
) {
974 furl
.host
= purl
->host
;
975 } else if ( (zhost
= http_get_server_var("HTTP_HOST")) ||
976 (zhost
= http_get_server_var("SERVER_NAME"))) {
977 furl
.host
= Z_STRVAL_P(zhost
);
979 furl
.host
= "localhost";
982 #define HTTP_URI_STRLCATS(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, sizeof(add_string)-1)
983 #define HTTP_URI_STRLCATL(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, strlen(add_string))
984 #define HTTP_URI_STRLCAT(URL, full_len, add_string, add_len) \
985 if ((full_len += add_len) > HTTP_URI_MAXLEN) { \
986 php_error_docref(NULL TSRMLS_CC, E_NOTICE, \
987 "Absolute URI would have exceeded max URI length (%d bytes) - " \
988 "tried to add %d bytes ('%s')", \
989 HTTP_URI_MAXLEN, add_len, add_string); \
993 php_url_free(purl); \
996 strcat(URL, add_string); \
999 HTTP_URI_STRLCATL(URL
, full_len
, furl
.scheme
);
1000 HTTP_URI_STRLCATS(URL
, full_len
, "://");
1003 HTTP_URI_STRLCATL(URL
, full_len
, furl
.user
);
1005 HTTP_URI_STRLCATS(URL
, full_len
, ":");
1006 HTTP_URI_STRLCATL(URL
, full_len
, furl
.pass
);
1008 HTTP_URI_STRLCATS(URL
, full_len
, "@");
1011 HTTP_URI_STRLCATL(URL
, full_len
, furl
.host
);
1013 if ( (!strcmp(furl
.scheme
, "http") && (furl
.port
!= 80)) ||
1014 (!strcmp(furl
.scheme
, "https") && (furl
.port
!= 443))) {
1015 char port_string
[8] = {0};
1016 snprintf(port_string
, 7, ":%u", furl
.port
);
1017 HTTP_URI_STRLCATL(URL
, full_len
, port_string
);
1021 if (furl
.path
[0] != '/') {
1022 HTTP_URI_STRLCATS(URL
, full_len
, "/");
1024 HTTP_URI_STRLCATL(URL
, full_len
, furl
.path
);
1026 HTTP_URI_STRLCATS(URL
, full_len
, "/");
1030 HTTP_URI_STRLCATS(URL
, full_len
, "?");
1031 HTTP_URI_STRLCATL(URL
, full_len
, furl
.query
);
1034 if (furl
.fragment
) {
1035 HTTP_URI_STRLCATS(URL
, full_len
, "#");
1036 HTTP_URI_STRLCATL(URL
, full_len
, furl
.fragment
);
1048 /* {{{ char *http_negotiate_q(char *, HashTable *, char *) */
1049 PHP_HTTP_API
char *_http_negotiate_q(const char *entry
, const HashTable
*supported
, const char *def TSRMLS_DC
)
1051 zval
*zaccept
, zdelim
, zarray
, zentries
, **zentry
, **zsupp
;
1052 char *q_ptr
= NULL
, *key
= NULL
;
1056 HTTP_GSC(zaccept
, entry
, estrdup(def
));
1058 array_init(&zarray
);
1059 array_init(&zentries
);
1061 Z_STRVAL(zdelim
) = ",";
1062 Z_STRLEN(zdelim
) = 1;
1064 php_explode(&zdelim
, zaccept
, &zarray
, -1);
1066 FOREACH_HASH_VAL(Z_ARRVAL(zarray
), zentry
) {
1067 if (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';')) {
1068 qual
= strtod(q_ptr
+ 3, NULL
);
1072 qual
= 1000.0 - i
++;
1074 FOREACH_HASH_VAL((HashTable
*)supported
, zsupp
) {
1075 if (!strcasecmp(Z_STRVAL_PP(zsupp
), Z_STRVAL_PP(zentry
))) {
1076 add_assoc_double(&zentries
, Z_STRVAL_PP(zsupp
), qual
);
1083 zend_hash_sort(Z_ARRVAL(zentries
), zend_qsort
, http_sort_q
, 0 TSRMLS_CC
);
1085 FOREACH_HASH_KEY(Z_ARRVAL(zentries
), key
, idx
) {
1088 zval_dtor(&zentries
);
1092 zval_dtor(&zentries
);
1094 return estrdup(def
);
1098 /* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
1099 PHP_HTTP_API http_range_status
_http_get_request_ranges(HashTable
*ranges
, size_t length TSRMLS_DC
)
1103 long begin
= -1, end
= -1, *ptr
;
1105 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
1106 range
= Z_STRVAL_P(zrange
);
1108 if (strncmp(range
, "bytes=", sizeof("bytes=") - 1)) {
1109 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Range header misses bytes=");
1114 range
+= sizeof("bytes=") - 1;
1117 switch (c
= *(range
++))
1123 case '1': case '2': case '3':
1124 case '4': case '5': case '6':
1125 case '7': case '8': case '9':
1127 * If the value of the pointer is already set (non-negative)
1128 * then multiply its value by ten and add the current value,
1129 * else initialise the pointers value with the current value
1131 * This let us recognize empty fields when validating the
1132 * ranges, i.e. a "-10" for begin and "12345" for the end
1133 * was the following range request: "Range: bytes=0-12345";
1134 * While a "-1" for begin and "12345" for the end would
1135 * have been: "Range: bytes=-12345".
1150 /* IE - ignore for now */
1157 /* validate ranges */
1162 /* "0-", "0-0" or overflow */
1163 if (end
== -1 || end
== -10 || length
<= end
) {
1171 /* "-", "-0" or overflow */
1172 if (end
== -1 || end
== -10 || length
<= end
) {
1175 begin
= length
- end
;
1190 if (length
<= begin
) {
1198 if ( (length
<= begin
) ||
1210 MAKE_STD_ZVAL(zentry
);
1212 add_index_long(zentry
, 0, begin
);
1213 add_index_long(zentry
, 1, end
);
1214 zend_hash_next_index_insert(ranges
, &zentry
, sizeof(zval
*), NULL
);
1232 /* {{{ STATUS http_send_ranges(HashTable *, void *, size_t, http_send_mode) */
1233 PHP_HTTP_API STATUS
_http_send_ranges(HashTable
*ranges
, const void *data
, size_t size
, http_send_mode mode TSRMLS_DC
)
1235 long **begin
, **end
;
1239 if (zend_hash_num_elements(ranges
) == 1) {
1240 char range_header
[256] = {0};
1242 if (SUCCESS
!= zend_hash_index_find(ranges
, 0, (void **) &zrange
) ||
1243 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1244 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1248 /* Send HTTP 206 Partial Content */
1249 http_send_status(206);
1251 /* send content range header */
1252 snprintf(range_header
, 255, "Content-Range: bytes %d-%d/%d", **begin
, **end
, size
);
1253 http_send_header(range_header
);
1255 /* send requested chunk */
1256 return http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1261 char bound
[23] = {0}, preface
[1024] = {0},
1262 multi_header
[68] = "Content-Type: multipart/byteranges; boundary=";
1264 /* Send HTTP 206 Partial Content */
1265 http_send_status(206);
1267 /* send multipart/byteranges header */
1268 snprintf(bound
, 22, "--%d%0.9f", time(NULL
), php_combined_lcg(TSRMLS_C
));
1269 strncat(multi_header
, bound
+ 2, 21);
1270 http_send_header(multi_header
);
1272 /* send each requested chunk */
1273 FOREACH_HASH_VAL(ranges
, zrange
) {
1274 if (SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1275 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1279 snprintf(preface
, 1023,
1281 HTTP_CRLF
"Content-Type: %s"
1282 HTTP_CRLF
"Content-Range: bytes %ld-%ld/%ld"
1287 HTTP_G(ctype
) ? HTTP_G(ctype
) : "application/x-octetstream",
1293 php_body_write(preface
, strlen(preface
) TSRMLS_CC
);
1294 http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1297 /* write boundary once more */
1298 php_body_write(HTTP_CRLF
, sizeof(HTTP_CRLF
) - 1 TSRMLS_CC
);
1299 php_body_write(bound
, strlen(bound
) TSRMLS_CC
);
1300 php_body_write("--", 2 TSRMLS_CC
);
1307 /* {{{ STATUS http_send(void *, size_t, http_send_mode) */
1308 PHP_HTTP_API STATUS
_http_send(const void *data_ptr
, size_t data_size
, http_send_mode data_mode TSRMLS_DC
)
1311 http_range_status range_status
;
1321 /* stop on-the-fly etag generation */
1322 if (cache_etag
= HTTP_G(etag_started
)) {
1324 HTTP_G(etag_started
) = 0;
1325 /* never ever use the output to compute the ETag if http_send() is used */
1326 php_end_ob_buffers(0 TSRMLS_CC
);
1329 zend_hash_init(&ranges
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
1330 range_status
= http_get_request_ranges(&ranges
, data_size
);
1332 if (range_status
== RANGE_ERR
) {
1333 zend_hash_destroy(&ranges
);
1334 http_send_status(416);
1338 /* Range Request - only send ranges if entity hasn't changed */
1339 if ( range_status
== RANGE_OK
&&
1340 http_etag_match_ex("HTTP_IF_MATCH", HTTP_G(etag
), 0) &&
1341 http_modified_match_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod
), 0)) {
1342 STATUS result
= http_send_ranges(&ranges
, data_ptr
, data_size
, data_mode
);
1343 zend_hash_destroy(&ranges
);
1347 zend_hash_destroy(&ranges
);
1349 /* send 304 Not Modified if etag matches */
1354 if (!(etag
= http_etag(data_ptr
, data_size
, data_mode
))) {
1358 http_send_etag(etag
, 32);
1359 etag_match
= http_etag_match("HTTP_IF_NONE_MATCH", etag
);
1363 return http_send_status(304);
1367 /* send 304 Not Modified if last modified matches */
1368 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod
))) {
1369 return http_send_status(304);
1372 /* send full entity */
1373 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
1377 /* {{{ STATUS http_send_stream(php_stream *) */
1378 PHP_HTTP_API STATUS
_http_send_stream_ex(php_stream
*file
, zend_bool close_stream TSRMLS_DC
)
1382 if ((!file
) || php_stream_stat(file
, &HTTP_G(ssb
))) {
1386 status
= http_send(file
, HTTP_G(ssb
).sb
.st_size
, SEND_RSRC
);
1389 php_stream_close(file
);
1396 /* {{{ STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1397 PHP_HTTP_API STATUS
_http_chunked_decode(const char *encoded
, size_t encoded_len
,
1398 char **decoded
, size_t *decoded_len TSRMLS_DC
)
1404 *decoded
= ecalloc(1, encoded_len
);
1408 while (((e_ptr
- encoded
) - encoded_len
) > 0) {
1409 char hex_len
[9] = {0};
1410 size_t chunk_len
= 0;
1413 /* read in chunk size */
1414 while (isxdigit(*e_ptr
)) {
1416 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1417 "Chunk size is too long: 0x%s...", hex_len
);
1421 hex_len
[i
++] = *e_ptr
++;
1424 /* reached the end */
1425 if (!strcmp(hex_len
, "0")) {
1430 if (strncmp(e_ptr
, HTTP_CRLF
, 2)) {
1431 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1432 "Invalid character (expected 0x0D 0x0A; got: %x %x)",
1433 *e_ptr
, *(e_ptr
+ 1));
1441 chunk_len
= strtol(hex_len
, &error
, 16);
1442 if (error
== hex_len
) {
1443 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1444 "Invalid chunk size string: '%s'", hex_len
);
1450 memcpy(d_ptr
, e_ptr
+= 2, chunk_len
);
1452 e_ptr
+= chunk_len
+ 2;
1453 *decoded_len
+= chunk_len
;
1460 /* {{{ STATUS http_split_response(zval *, zval *, zval *) */
1461 PHP_HTTP_API STATUS
_http_split_response(zval
*response
, zval
*headers
, zval
*body TSRMLS_DC
)
1465 STATUS status
= http_split_response_ex(Z_STRVAL_P(response
), Z_STRLEN_P(response
), Z_ARRVAL_P(headers
), &b
, &l
);
1466 ZVAL_STRINGL(body
, b
, l
, 0);
1471 /* {{{ STATUS http_split_response(char *, size_t, HashTable *, char **, size_t *) */
1472 PHP_HTTP_API STATUS
_http_split_response_ex(char *response
, size_t response_len
,
1473 HashTable
*headers
, char **body
, size_t *body_len TSRMLS_DC
)
1475 char *header
= response
, *real_body
= NULL
;
1477 while (0 < (response_len
- (response
- header
+ 4))) {
1478 if ( (*response
++ == '\r') &&
1479 (*response
++ == '\n') &&
1480 (*response
++ == '\r') &&
1481 (*response
++ == '\n')) {
1482 real_body
= response
;
1487 if (real_body
&& (*body_len
= (response_len
- (real_body
- header
)))) {
1488 *body
= ecalloc(1, *body_len
+ 1);
1489 memcpy(*body
, real_body
, *body_len
);
1492 return http_parse_headers_ex(header
, real_body
? response_len
- *body_len
: response_len
, headers
, 1);
1496 /* {{{ STATUS http_parse_headers(char *, size_t, HashTable *, zend_bool) */
1497 PHP_HTTP_API STATUS
_http_parse_headers_ex(char *header
, size_t header_len
,
1498 HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
1500 char *colon
= NULL
, *line
= NULL
, *begin
= header
;
1503 Z_ARRVAL(array
) = headers
;
1505 if (header_len
< 2) {
1510 if (!strncmp(header
, "HTTP/1.", 7)) {
1511 char *end
= strstr(header
, HTTP_CRLF
);
1512 add_assoc_stringl(&array
, "Status",
1513 header
+ sizeof("HTTP/1.x ") - 1,
1514 end
- (header
+ sizeof("HTTP/1.x ") - 1), 1);
1520 while (header_len
>= (line
- begin
)) {
1526 --value_len
; /* we don't have CR so value length is one char less */
1528 if (colon
&& ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t')))) {
1530 /* skip empty key */
1531 if (header
!= colon
) {
1532 char *key
= estrndup(header
, colon
- header
);
1535 key
= pretty_key(key
, colon
- header
, 1, 1);
1538 value_len
+= line
- colon
- 1;
1540 /* skip leading ws */
1541 while (isspace(*(++colon
))) --value_len
;
1542 /* skip trailing ws */
1543 while (isspace(colon
[value_len
- 1])) --value_len
;
1545 if (value_len
< 1) {
1546 /* hm, empty header? */
1547 add_assoc_stringl(&array
, key
, "", 0, 1);
1549 add_assoc_stringl(&array
, key
, colon
, value_len
, 1);
1556 header
+= line
- header
;
1571 /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
1572 PHP_HTTP_API
void _http_get_request_headers_ex(HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
1578 Z_ARRVAL(array
) = headers
;
1580 FOREACH_HASH_KEY(HTTP_SERVER_VARS
, key
, idx
) {
1581 if (key
&& !strncmp(key
, "HTTP_", 5)) {
1586 key
= pretty_key(key
, strlen(key
), 1, 1);
1589 zend_hash_get_current_data(HTTP_SERVER_VARS
, (void **) &header
);
1590 add_assoc_stringl(&array
, key
, Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1597 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
1598 PHP_HTTP_API STATUS
_http_urlencode_hash_ex(HashTable
*hash
, zend_bool override_argsep
,
1599 char *pre_encoded_data
, size_t pre_encoded_len
,
1600 char **encoded_data
, size_t *encoded_len TSRMLS_DC
)
1603 phpstr
*qstr
= phpstr_new();
1605 if (override_argsep
|| !strlen(arg_sep
= INI_STR("arg_separator.output"))) {
1606 arg_sep
= HTTP_URL_ARGSEP_DEFAULT
;
1609 if (pre_encoded_len
&& pre_encoded_data
) {
1610 phpstr_append(qstr
, pre_encoded_data
, pre_encoded_len
);
1613 if (SUCCESS
!= http_urlencode_hash_implementation(hash
, qstr
, arg_sep
)) {
1614 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't encode query data");
1619 phpstr_data(qstr
, encoded_data
, encoded_len
);
1626 /* {{{ STATUS http_auth_header(char *, char*) */
1627 PHP_HTTP_API STATUS
_http_auth_header(const char *type
, const char *realm TSRMLS_DC
)
1629 char realm_header
[1024] = {0};
1630 snprintf(realm_header
, 1023, "WWW-Authenticate: %s realm=\"%s\"", type
, realm
);
1631 return http_send_status_header(401, realm_header
);
1635 /* {{{ STATUS http_auth_credentials(char **, char **) */
1636 PHP_HTTP_API STATUS
_http_auth_credentials(char **user
, char **pass TSRMLS_DC
)
1638 if (strncmp(sapi_module
.name
, "isapi", 5)) {
1639 zval
*zuser
, *zpass
;
1641 HTTP_GSC(zuser
, "PHP_AUTH_USER", FAILURE
);
1642 HTTP_GSC(zpass
, "PHP_AUTH_PW", FAILURE
);
1644 *user
= estrndup(Z_STRVAL_P(zuser
), Z_STRLEN_P(zuser
));
1645 *pass
= estrndup(Z_STRVAL_P(zpass
), Z_STRLEN_P(zpass
));
1650 HTTP_GSC(zauth
, "HTTP_AUTHORIZATION", FAILURE
);
1652 char *decoded
, *colon
;
1654 decoded
= php_base64_decode(Z_STRVAL_P(zauth
), Z_STRLEN_P(zauth
),
1657 if (colon
= strchr(decoded
+ 6, ':')) {
1658 *user
= estrndup(decoded
+ 6, colon
- decoded
- 6);
1659 *pass
= estrndup(colon
+ 1, decoded
+ decoded_len
- colon
- 6 - 1);
1670 /* {{{ http_urlencode_hash_implementation
1671 Original Author: Sara Golemon <pollita@php.net> */
1672 PHP_HTTP_API STATUS
_http_urlencode_hash_implementation_ex(
1673 HashTable
*ht
, phpstr
*formstr
, char *arg_sep
,
1674 const char *num_prefix
, int num_prefix_len
,
1675 const char *key_prefix
, int key_prefix_len
,
1676 const char *key_suffix
, int key_suffix_len
,
1677 zval
*type TSRMLS_DC
)
1679 char *key
= NULL
, *ekey
, *newprefix
, *p
;
1680 int arg_sep_len
, key_len
, ekey_len
, key_type
, newprefix_len
;
1682 zval
**zdata
= NULL
, *copyzval
;
1684 if (!ht
|| !formstr
) {
1688 if (ht
->nApplyCount
> 0) {
1689 /* Prevent recursion */
1693 if (!arg_sep
|| !strlen(arg_sep
)) {
1694 arg_sep
= HTTP_URL_ARGSEP_DEFAULT
;
1696 arg_sep_len
= strlen(arg_sep
);
1698 for (zend_hash_internal_pointer_reset(ht
);
1699 (key_type
= zend_hash_get_current_key_ex(ht
, &key
, &key_len
, &idx
, 0, NULL
)) != HASH_KEY_NON_EXISTANT
;
1700 zend_hash_move_forward(ht
)
1702 if (key_type
== HASH_KEY_IS_STRING
&& key_len
&& key
[key_len
-1] == '\0') {
1703 /* We don't want that trailing NULL */
1707 #ifdef ZEND_ENGINE_2
1708 /* handling for private & protected object properties */
1709 if (key
&& *key
== '\0' && type
!= NULL
) {
1712 zend_object
*zobj
= zend_objects_get_address(type TSRMLS_CC
);
1713 if (zend_check_property_access(zobj
, key TSRMLS_CC
) != SUCCESS
) {
1714 /* private or protected property access outside of the class */
1717 zend_unmangle_property_name(key
, &tmp
, &key
);
1718 key_len
= strlen(key
);
1722 if (zend_hash_get_current_data_ex(ht
, (void **)&zdata
, NULL
) == FAILURE
|| !zdata
|| !(*zdata
)) {
1723 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Error traversing form data array.");
1726 if (Z_TYPE_PP(zdata
) == IS_ARRAY
|| Z_TYPE_PP(zdata
) == IS_OBJECT
) {
1727 if (key_type
== HASH_KEY_IS_STRING
) {
1728 ekey
= php_url_encode(key
, key_len
, &ekey_len
);
1729 newprefix_len
= key_suffix_len
+ ekey_len
+ key_prefix_len
+ 1;
1730 newprefix
= emalloc(newprefix_len
+ 1);
1734 memcpy(p
, key_prefix
, key_prefix_len
);
1735 p
+= key_prefix_len
;
1738 memcpy(p
, ekey
, ekey_len
);
1743 memcpy(p
, key_suffix
, key_suffix_len
);
1744 p
+= key_suffix_len
;
1750 /* Is an integer key */
1751 ekey_len
= spprintf(&ekey
, 12, "%ld", idx
);
1752 newprefix_len
= key_prefix_len
+ num_prefix_len
+ ekey_len
+ key_suffix_len
+ 1;
1753 newprefix
= emalloc(newprefix_len
+ 1);
1757 memcpy(p
, key_prefix
, key_prefix_len
);
1758 p
+= key_prefix_len
;
1761 memcpy(p
, num_prefix
, num_prefix_len
);
1762 p
+= num_prefix_len
;
1764 memcpy(p
, ekey
, ekey_len
);
1769 memcpy(p
, key_suffix
, key_suffix_len
);
1770 p
+= key_suffix_len
;
1776 http_urlencode_hash_implementation_ex(HASH_OF(*zdata
), formstr
, arg_sep
,
1777 NULL
, 0, newprefix
, newprefix_len
, "]", 1, (Z_TYPE_PP(zdata
) == IS_OBJECT
? *zdata
: NULL
));
1780 } else if (Z_TYPE_PP(zdata
) == IS_NULL
|| Z_TYPE_PP(zdata
) == IS_RESOURCE
) {
1781 /* Skip these types */
1784 if (formstr
->used
) {
1785 phpstr_append(formstr
, arg_sep
, arg_sep_len
);
1787 /* Simple key=value */
1788 phpstr_append(formstr
, key_prefix
, key_prefix_len
);
1789 if (key_type
== HASH_KEY_IS_STRING
) {
1790 ekey
= php_url_encode(key
, key_len
, &ekey_len
);
1791 phpstr_append(formstr
, ekey
, ekey_len
);
1796 phpstr_append(formstr
, num_prefix
, num_prefix_len
);
1798 ekey_len
= spprintf(&ekey
, 12, "%ld", idx
);
1799 phpstr_append(formstr
, ekey
, ekey_len
);
1802 phpstr_append(formstr
, key_suffix
, key_suffix_len
);
1803 phpstr_appends(formstr
, "=");
1804 switch (Z_TYPE_PP(zdata
)) {
1806 ekey
= php_url_encode(Z_STRVAL_PP(zdata
), Z_STRLEN_PP(zdata
), &ekey_len
);
1810 ekey_len
= spprintf(&ekey
, 12, "%ld", Z_LVAL_PP(zdata
));
1813 ekey_len
= spprintf(&ekey
, 48, "%.*G", (int) EG(precision
), Z_DVAL_PP(zdata
));
1816 /* fall back on convert to string */
1817 MAKE_STD_ZVAL(copyzval
);
1818 *copyzval
= **zdata
;
1819 zval_copy_ctor(copyzval
);
1820 convert_to_string_ex(©zval
);
1821 ekey
= php_url_encode(Z_STRVAL_P(copyzval
), Z_STRLEN_P(copyzval
), &ekey_len
);
1822 zval_ptr_dtor(©zval
);
1824 phpstr_append(formstr
, ekey
, ekey_len
);
1833 /* }}} public API */
1840 * vim600: noet sw=4 ts=4 fdm=marker
1841 * vim<600: noet sw=4 ts=4