bee02cc01ff592551d911c0ee11bd5b6d006b1a2
[m6w6/ext-http] / http_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
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 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #define _WINSOCKAPI_
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <ctype.h>
26
27 #include "php.h"
28 #include "php_version.h"
29 #include "php_streams.h"
30 #include "snprintf.h"
31 #include "ext/standard/md5.h"
32 #include "ext/standard/url.h"
33 #include "ext/standard/base64.h"
34 #include "ext/standard/php_string.h"
35 #include "ext/standard/php_smart_str.h"
36 #include "ext/standard/php_lcg.h"
37
38 #include "SAPI.h"
39
40 #ifdef ZEND_ENGINE_2
41 # include "ext/standard/php_http.h"
42 #else
43 #include "http_build_query.c"
44 #endif
45
46 #include "php_http.h"
47 #include "php_http_api.h"
48
49 ZEND_DECLARE_MODULE_GLOBALS(http)
50
51 /* {{{ day/month names */
52 static const char *days[] = {
53 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
54 };
55 static const char *wkdays[] = {
56 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
57 };
58 static const char *weekdays[] = {
59 "Monday", "Tuesday", "Wednesday",
60 "Thursday", "Friday", "Saturday", "Sunday"
61 };
62 static const char *months[] = {
63 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
64 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
65 };
66 enum assume_next {
67 DATE_MDAY,
68 DATE_YEAR,
69 DATE_TIME
70 };
71 static const struct time_zone {
72 const char *name;
73 const int offset;
74 } time_zones[] = {
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 */
118 };
119 /* }}} */
120
121 /* {{{ internals */
122
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);
126
127 static int check_day(char *day, size_t len);
128 static int check_month(char *month);
129 static int check_tzone(char *tzone);
130
131
132 static int http_ob_stack_get(php_ob_buffer *, php_ob_buffer **);
133
134 /* {{{ static int http_sort_q(const void *, const void *) */
135 static int http_sort_q(const void *a, const void *b TSRMLS_DC)
136 {
137 Bucket *f, *s;
138 zval result, *first, *second;
139
140 f = *((Bucket **) a);
141 s = *((Bucket **) b);
142
143 first = *((zval **) f->pData);
144 second= *((zval **) s->pData);
145
146 if (numeric_compare_function(&result, first, second TSRMLS_CC) != SUCCESS) {
147 return 0;
148 }
149 return (Z_LVAL(result) > 0 ? -1 : (Z_LVAL(result) < 0 ? 1 : 0));
150 }
151 /* }}} */
152
153 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
154 http_send_mode) */
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)
157 {
158 char *buf;
159 size_t read = 0;
160 long len = end - begin;
161 php_stream *s;
162
163 switch (mode)
164 {
165 case SEND_RSRC:
166 s = (php_stream *) data;
167 if (php_stream_seek(s, begin, SEEK_SET)) {
168 return FAILURE;
169 }
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))) {
174 efree(buf);
175 return FAILURE;
176 }
177 if (read - php_body_write(buf, read TSRMLS_CC)) {
178 efree(buf);
179 return FAILURE;
180 }
181 }
182
183 /* read & write left over */
184 if (len) {
185 if (read = php_stream_read(s, buf, HTTP_BUF_SIZE + len)) {
186 if (read - php_body_write(buf, read TSRMLS_CC)) {
187 efree(buf);
188 return FAILURE;
189 }
190 } else {
191 efree(buf);
192 return FAILURE;
193 }
194 }
195 efree(buf);
196 return SUCCESS;
197 break;
198
199 case SEND_DATA:
200 return len == php_body_write(((char *)data) + begin, len TSRMLS_CC)
201 ? SUCCESS : FAILURE;
202 break;
203
204 default:
205 return FAILURE;
206 break;
207 }
208 }
209 /* }}} */
210
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)
214 {
215 int i;
216 const char * const *check = (len > 3) ? &weekdays[0] : &wkdays[0];
217 for (i = 0; i < 7; i++) {
218 if (!strcmp(day, check[0])) {
219 return i;
220 }
221 check++;
222 }
223 return -1;
224 }
225
226 static int check_month(char *month)
227 {
228 int i;
229 const char * const *check = &months[0];
230 for (i = 0; i < 12; i++) {
231 if (!strcmp(month, check[0])) {
232 return i;
233 }
234 check++;
235 }
236 return -1;
237 }
238
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 */
241
242 static int check_tzone(char *tzone)
243 {
244 int i;
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;
249 }
250 check++;
251 }
252 return -1;
253 }
254 /* }}} */
255
256 /* char *pretty_key(char *, int, int, int) */
257 char *pretty_key(char *key, int key_len, int uctitle, int xhyphen)
258 {
259 if (key && key_len) {
260 int i, wasalpha;
261 if (wasalpha = isalpha(key[0])) {
262 key[0] = uctitle ? toupper(key[0]) : tolower(key[0]);
263 }
264 for (i = 1; i < key_len; i++) {
265 if (isalpha(key[i])) {
266 key[i] = ((!wasalpha) && uctitle) ? toupper(key[i]) : tolower(key[i]);
267 wasalpha = 1;
268 } else {
269 if (xhyphen && (key[i] == '_')) {
270 key[i] = '-';
271 }
272 wasalpha = 0;
273 }
274 }
275 }
276 return key;
277 }
278 /* }}} */
279
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)
282 {
283 static int i = 0;
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;
289 b->erase = o->erase;
290 s[i++] = b;
291 return SUCCESS;
292 }
293 /* }}} */
294
295 /* }}} internals */
296
297 /* {{{ public API */
298
299 /* {{{ char *http_date(time_t) */
300 PHP_HTTP_API char *_http_date(time_t t TSRMLS_DC)
301 {
302 struct tm *gmtime, tmbuf;
303 char *date = ecalloc(1, 31);
304
305 gmtime = php_gmtime_r(&t, &tmbuf);
306 snprintf(date, 30,
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
311 );
312 return date;
313 }
314 /* }}} */
315
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)
319 {
320 time_t t = 0;
321 int tz_offset = -1, year = -1, month = -1, monthday = -1, weekday = -1,
322 hours = -1, minutes = -1, seconds = -1;
323 struct tm tm;
324 enum assume_next dignext = DATE_MDAY;
325 const char *indate = date;
326
327 int found = 0, part = 0; /* max 6 parts */
328
329 while (*date && (part < 6)) {
330 int found = 0;
331
332 while (*date && !isalnum(*date)) {
333 date++;
334 }
335
336 if (isalpha(*date)) {
337 /* a name coming up */
338 char buf[32] = "";
339 size_t len;
340 sscanf(date, "%31[A-Za-z]", buf);
341 len = strlen(buf);
342
343 if (weekday == -1) {
344 weekday = check_day(buf, len);
345 if (weekday != -1) {
346 found = 1;
347 }
348 }
349
350 if (!found && (month == -1)) {
351 month = check_month(buf);
352 if (month != -1) {
353 found = 1;
354 }
355 }
356
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) {
361 found = 1;
362 }
363 }
364
365 if (!found) {
366 return -1; /* bad string */
367 }
368 date += len;
369 }
370 else if (isdigit(*date)) {
371 /* a digit */
372 int val;
373 char *end;
374 if((seconds == -1) && (3 == sscanf(date, "%02d:%02d:%02d", &hours, &minutes, &seconds))) {
375 /* time stamp! */
376 date += 8;
377 found = 1;
378 }
379 else {
380 val = (int) strtol(date, &end, 10);
381
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. */
385 found = 1;
386 tz_offset = (val / 100 * 60 + val % 100) * 60;
387
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;
391 }
392
393 if (((end - date) == 8) && (year == -1) && (month == -1) && (monthday == -1)) {
394 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
395 found = 1;
396 year = val / 10000;
397 month = (val % 10000) / 100 - 1; /* month is 0 - 11 */
398 monthday = val % 100;
399 }
400
401 if (!found && (dignext == DATE_MDAY) && (monthday == -1)) {
402 if ((val > 0) && (val < 32)) {
403 monthday = val;
404 found = 1;
405 }
406 dignext = DATE_YEAR;
407 }
408
409 if (!found && (dignext == DATE_YEAR) && (year == -1)) {
410 year = val;
411 found = 1;
412 if (year < 1900) {
413 year += year > 70 ? 1900 : 2000;
414 }
415 if(monthday == -1) {
416 dignext = DATE_MDAY;
417 }
418 }
419
420 if (!found) {
421 return -1;
422 }
423
424 date = end;
425 }
426 }
427
428 part++;
429 }
430
431 if (-1 == seconds) {
432 seconds = minutes = hours = 0; /* no time, make it zero */
433 }
434
435 if ((-1 == monthday) || (-1 == month) || (-1 == year)) {
436 /* lacks vital info, fail */
437 return -1;
438 }
439
440 if (sizeof(time_t) < 5) {
441 /* 32 bit time_t can only hold dates to the beginning of 2038 */
442 if (year > 2037) {
443 return 0x7fffffff;
444 }
445 }
446
447 tm.tm_sec = seconds;
448 tm.tm_min = minutes;
449 tm.tm_hour = hours;
450 tm.tm_mday = monthday;
451 tm.tm_mon = month;
452 tm.tm_year = year - 1900;
453 tm.tm_wday = 0;
454 tm.tm_yday = 0;
455 tm.tm_isdst = 0;
456
457 t = mktime(&tm);
458
459 /* time zone adjust */
460 {
461 struct tm *gmt, keeptime2;
462 long delta;
463 time_t t2;
464
465 if(!(gmt = php_gmtime_r(&t, &keeptime2))) {
466 return -1; /* illegal date/time */
467 }
468
469 t2 = mktime(gmt);
470
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);
474
475 if((delta > 0) && (t + delta < t)) {
476 return -1; /* time_t overflow */
477 }
478
479 t += delta;
480 }
481
482 return t;
483 }
484 /* }}} */
485
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)
489 {
490 char ssb_buf[128] = {0};
491 unsigned char digest[16];
492 PHP_MD5_CTX ctx;
493 char *new_etag = ecalloc(1, 33);
494
495 PHP_MD5Init(&ctx);
496
497 switch (data_mode)
498 {
499 case SEND_DATA:
500 PHP_MD5Update(&ctx, data_ptr, data_len);
501 break;
502
503 case SEND_RSRC:
504 if (!HTTP_G(ssb).sb.st_ino) {
505 if (php_stream_stat((php_stream *) data_ptr, &HTTP_G(ssb))) {
506 return NULL;
507 }
508 }
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
513 );
514 PHP_MD5Update(&ctx, ssb_buf, strlen(ssb_buf));
515 break;
516
517 default:
518 efree(new_etag);
519 return NULL;
520 break;
521 }
522
523 PHP_MD5Final(digest, &ctx);
524 make_digest(new_etag, digest);
525
526 return new_etag;
527 }
528 /* }}} */
529
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)
532 {
533 switch (data_mode)
534 {
535 case SEND_DATA:
536 {
537 return time(NULL);
538 }
539
540 case SEND_RSRC:
541 {
542 if (!HTTP_G(ssb).sb.st_mtime) {
543 if (php_stream_stat((php_stream *) data_ptr, &HTTP_G(ssb))) {
544 return 0;
545 }
546 }
547 return HTTP_G(ssb).sb.st_mtime;
548 }
549
550 default:
551 {
552 if (!HTTP_G(ssb).sb.st_mtime) {
553 if(php_stream_stat_path(Z_STRVAL_P((zval *) data_ptr), &HTTP_G(ssb))) {
554 return 0;
555 }
556 }
557 return HTTP_G(ssb).sb.st_mtime;
558 }
559 }
560 }
561 /* }}} */
562
563 /* {{{inline int http_is_range_request(void) */
564 PHP_HTTP_API inline int _http_is_range_request(TSRMLS_D)
565 {
566 return zend_hash_exists(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]),
567 "HTTP_RANGE", strlen("HTTP_RANGE") + 1);
568 }
569 /* }}} */
570
571 /* {{{ inline STATUS http_send_status(int) */
572 PHP_HTTP_API inline STATUS _http_send_status(const int status TSRMLS_DC)
573 {
574 int s = status;
575 return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) s TSRMLS_CC);
576 }
577 /* }}} */
578
579 /* {{{ inline STATUS http_send_header(char *) */
580 PHP_HTTP_API inline STATUS _http_send_header(const char *header TSRMLS_DC)
581 {
582 return http_send_status_header(0, header);
583 }
584 /* }}} */
585
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)
588 {
589 sapi_header_line h = {(char *) header, strlen(header), status};
590 return sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC);
591 }
592 /* }}} */
593
594 /* {{{ inline zval *http_get_server_var(char *) */
595 PHP_HTTP_API inline zval *_http_get_server_var(const char *key TSRMLS_DC)
596 {
597 zval **var;
598 if (SUCCESS == zend_hash_find(
599 HTTP_SERVER_VARS,
600 (char *) key, strlen(key) + 1, (void **) &var)) {
601 return *var;
602 }
603 return NULL;
604 }
605 /* }}} */
606
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)
610 {
611 char etag[33] = { 0 };
612 unsigned char digest[16];
613
614 if (mode & PHP_OUTPUT_HANDLER_START) {
615 PHP_MD5Init(&HTTP_G(etag_md5));
616 }
617
618 PHP_MD5Update(&HTTP_G(etag_md5), output, output_len);
619
620 if (mode & PHP_OUTPUT_HANDLER_END) {
621 PHP_MD5Final(digest, &HTTP_G(etag_md5));
622
623 /* just do that if desired */
624 if (HTTP_G(etag_started)) {
625 make_digest(etag, digest);
626
627 if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
628 http_send_status(304);
629 } else {
630 http_send_etag(etag, 32);
631 }
632 }
633 }
634
635 *handled_output_len = output_len;
636 *handled_output = estrndup(output, output_len);
637 }
638 /* }}} */
639
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)
643 {
644 php_ob_buffer **stack;
645 int count, i;
646
647 if (count = OG(ob_nesting_level)) {
648 stack = ecalloc(count, sizeof(php_ob_buffer *));
649
650 if (count > 1) {
651 zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP,
652 (int (*)(void *elem, void *)) http_ob_stack_get, stack);
653 }
654
655 if (count > 0) {
656 http_ob_stack_get(&OG(active_ob_buffer), stack);
657 }
658
659 while (OG(ob_nesting_level)) {
660 php_end_ob_buffer(0, 0 TSRMLS_CC);
661 }
662 }
663
664 php_ob_set_internal_handler(handler_func, chunk_size, handler_name, erase TSRMLS_CC);
665
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);
670 }
671 php_body_write(s->buffer, s->text_length TSRMLS_CC);
672 efree(s->handler_name);
673 efree(s->buffer);
674 efree(s);
675 }
676 if (count) {
677 efree(stack);
678 }
679
680 return SUCCESS;
681 }
682 /* }}} */
683
684 /* {{{ int http_modified_match(char *, int) */
685 PHP_HTTP_API int _http_modified_match(const char *entry, const time_t t TSRMLS_DC)
686 {
687 int retval;
688 zval *zmodified;
689 char *modified, *chr_ptr;
690
691 HTTP_GSC(zmodified, entry, 0);
692
693 modified = estrndup(Z_STRVAL_P(zmodified), Z_STRLEN_P(zmodified));
694 if (chr_ptr = strrchr(modified, ';')) {
695 chr_ptr = 0;
696 }
697 retval = (t <= http_parse_date(modified));
698 efree(modified);
699 return retval;
700 }
701 /* }}} */
702
703 /* {{{ int http_etag_match(char *, char *) */
704 PHP_HTTP_API int _http_etag_match(const char *entry, const char *etag TSRMLS_DC)
705 {
706 zval *zetag;
707 char *quoted_etag;
708 STATUS result;
709
710 HTTP_GSC(zetag, entry, 0);
711
712 if (NULL != strchr(Z_STRVAL_P(zetag), '*')) {
713 return 1;
714 }
715
716 quoted_etag = (char *) emalloc(strlen(etag) + 3);
717 sprintf(quoted_etag, "\"%s\"", etag);
718
719 if (!strchr(Z_STRVAL_P(zetag), ',')) {
720 result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
721 } else {
722 result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
723 }
724 efree(quoted_etag);
725 return result;
726 }
727 /* }}} */
728
729 /* {{{ STATUS http_send_last_modified(int) */
730 PHP_HTTP_API STATUS _http_send_last_modified(const time_t t TSRMLS_DC)
731 {
732 char modified[96] = "Last-Modified: ", *date;
733 date = http_date(t);
734 strcat(modified, date);
735 efree(date);
736
737 /* remember */
738 HTTP_G(lmod) = t;
739
740 return http_send_header(modified);
741 }
742 /* }}} */
743
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)
747 {
748 STATUS status;
749 char *etag_header;
750
751 if (!etag_len){
752 php_error_docref(NULL TSRMLS_CC,E_ERROR,
753 "Attempt to send empty ETag (previous: %s)\n", HTTP_G(etag));
754 return FAILURE;
755 }
756
757 /* remember */
758 if (HTTP_G(etag)) {
759 efree(HTTP_G(etag));
760 }
761 HTTP_G(etag) = estrdup(etag);
762
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);
767 }
768 efree(etag_header);
769 return status;
770 }
771 /* }}} */
772
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)
776 {
777 STATUS status;
778 char *cc_header = ecalloc(1, sizeof("Cache-Control: ") + cc_len);
779
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);
784 }
785 efree(cc_header);
786 return status;
787 }
788 /* }}} */
789
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)
793 {
794 STATUS status;
795 char *ct_header;
796
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",
800 content_type);
801 return FAILURE;
802 }
803
804 /* remember for multiple ranges */
805 if (HTTP_G(ctype)) {
806 efree(HTTP_G(ctype));
807 }
808 HTTP_G(ctype) = estrndup(content_type, ct_len);
809
810 ct_header = ecalloc(1, sizeof("Content-Type: ") + ct_len);
811 sprintf(ct_header, "Content-Type: %s", content_type);
812
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);
816 }
817 efree(ct_header);
818 return status;
819 }
820 /* }}} */
821
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)
825 {
826 STATUS status;
827 char *cd_header;
828
829 if (send_inline) {
830 cd_header = ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len);
831 sprintf(cd_header, "Content-Disposition: inline; filename=\"%s\"", filename);
832 } else {
833 cd_header = ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len);
834 sprintf(cd_header, "Content-Disposition: attachment; filename=\"%s\"", filename);
835 }
836
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);
839 }
840 efree(cd_header);
841 return status;
842 }
843 /* }}} */
844
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)
848 {
849 if (cc_len) {
850 http_send_cache_control(cache_control, cc_len);
851 }
852
853 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified)) {
854 if (SUCCESS == http_send_status(304)) {
855 zend_bailout();
856 } else {
857 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
858 return FAILURE;
859 }
860 }
861 return http_send_last_modified(send_modified);
862 }
863 /* }}} */
864
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)
868 {
869 if (cc_len) {
870 http_send_cache_control(cache_control, cc_len);
871 }
872
873 if (etag_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)) {
877 zend_bailout();
878 } else {
879 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
880 return FAILURE;
881 }
882 }
883 }
884
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;
889 return SUCCESS;
890 } else {
891 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not start ob_etaghandler");
892 return FAILURE;
893 }
894 }
895 return SUCCESS;
896 }
897 /* }}} */
898
899 /* {{{ char *http_absolute_uri(char *, char *) */
900 PHP_HTTP_API char *_http_absolute_uri(const char *url,
901 const char *proto TSRMLS_DC)
902 {
903 char *proto_ptr, *host, *path, *PTR, *URI = ecalloc(1, HTTP_URI_MAXLEN + 1);
904 zval *zhost;
905
906 if (!url || !strlen(url)) {
907 if (!SG(request_info).request_uri) {
908 return NULL;
909 }
910 url = SG(request_info).request_uri;
911 }
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);
916 return URI;
917 } else {
918 snprintf(URI, HTTP_URI_MAXLEN, "%s%s", proto, proto_ptr + 3);
919 return URI;
920 }
921 }
922
923 /* protocol defaults to http */
924 if (!proto || !strlen(proto)) {
925 proto = "http";
926 }
927
928 /* get host name */
929 if ( (zhost = http_get_server_var("HTTP_HOST")) ||
930 (zhost = http_get_server_var("SERVER_NAME"))) {
931 host = Z_STRVAL_P(zhost);
932 } else {
933 host = "localhost";
934 }
935
936
937 /* glue together */
938 if (url[0] == '/') {
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);
944 efree(path);
945 } else {
946 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s/%s", proto, host, url);
947 }
948
949 /* strip everything after a new line */
950 if ((PTR = strchr(URI, '\r')) || (PTR = strchr(URI, '\n'))) {
951 PTR = 0;
952 }
953
954 return URI;
955 }
956 /* }}} */
957
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)
961 {
962 zval *zaccept, *zarray, *zdelim, **zentry, *zentries, **zsupp;
963 char *q_ptr, *result;
964 int i, c;
965 double qual;
966
967 HTTP_GSC(zaccept, entry, estrdup(def));
968
969 MAKE_STD_ZVAL(zarray);
970 array_init(zarray);
971
972 MAKE_STD_ZVAL(zdelim);
973 ZVAL_STRING(zdelim, ",", 0);
974 php_explode(zdelim, zaccept, zarray, -1);
975 efree(zdelim);
976
977 MAKE_STD_ZVAL(zentries);
978 array_init(zentries);
979
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))) {
982
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));
987 break;
988 }
989
990 /* check for qualifier */
991 if (NULL != (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';'))) {
992 qual = strtod(q_ptr + 3, NULL);
993 } else {
994 qual = 1000.0 - i;
995 }
996
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);
1004 break;
1005 }
1006 }
1007 }
1008
1009 zval_dtor(zarray);
1010 efree(zarray);
1011
1012 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries));
1013
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);
1019 }
1020
1021 zval_dtor(zentries);
1022 efree(zentries);
1023
1024 return result;
1025 }
1026 /* }}} */
1027
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)
1031 {
1032 zval *zrange;
1033 char *range, c;
1034 long begin = -1, end = -1, *ptr;
1035
1036 HTTP_GSC(zrange, "HTTP_RANGE", RANGE_NO);
1037 range = Z_STRVAL_P(zrange);
1038
1039 if (strncmp(range, "bytes=", strlen("bytes="))) {
1040 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Range header misses bytes=");
1041 return RANGE_NO;
1042 }
1043
1044 ptr = &begin;
1045 range += strlen("bytes=");
1046
1047 do {
1048 switch (c = *(range++))
1049 {
1050 case '0':
1051 *ptr *= 10;
1052 break;
1053
1054 case '1': case '2': case '3':
1055 case '4': case '5': case '6':
1056 case '7': case '8': case '9':
1057 /*
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
1061 * --
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".
1067 */
1068 if (*ptr > 0) {
1069 *ptr *= 10;
1070 *ptr += c - '0';
1071 } else {
1072 *ptr = c - '0';
1073 }
1074 break;
1075
1076 case '-':
1077 ptr = &end;
1078 break;
1079
1080 case ' ':
1081 /* IE - ignore for now */
1082 break;
1083
1084 case 0:
1085 case ',':
1086
1087 if (length) {
1088 /* validate ranges */
1089 switch (begin)
1090 {
1091 /* "0-12345" */
1092 case -10:
1093 if ((length - end) < 1) {
1094 return RANGE_ERR;
1095 }
1096 begin = 0;
1097 break;
1098
1099 /* "-12345" */
1100 case -1:
1101 if ((length - end) < 1) {
1102 return RANGE_ERR;
1103 }
1104 begin = length - end;
1105 end = length;
1106 break;
1107
1108 /* "12345-(xxx)" */
1109 default:
1110 switch (end)
1111 {
1112 /* "12345-" */
1113 case -1:
1114 if ((length - begin) < 1) {
1115 return RANGE_ERR;
1116 }
1117 end = length - 1;
1118 break;
1119
1120 /* "12345-67890" */
1121 default:
1122 if ( ((length - begin) < 1) ||
1123 ((length - end) < 1) ||
1124 ((begin - end) >= 0)) {
1125 return RANGE_ERR;
1126 }
1127 break;
1128 }
1129 break;
1130 }
1131 }
1132 {
1133 zval *zentry;
1134 MAKE_STD_ZVAL(zentry);
1135 array_init(zentry);
1136 add_index_long(zentry, 0, begin);
1137 add_index_long(zentry, 1, end);
1138 add_next_index_zval(zranges, zentry);
1139
1140 begin = -1;
1141 end = -1;
1142 ptr = &begin;
1143 }
1144 break;
1145
1146 default:
1147 return RANGE_NO;
1148 break;
1149 }
1150 } while (c != 0);
1151
1152 return RANGE_OK;
1153 }
1154 /* }}} */
1155
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)
1158 {
1159 int c;
1160 long **begin, **end;
1161 zval **zrange;
1162
1163 /* Send HTTP 206 Partial Content */
1164 http_send_status(206);
1165
1166 /* single range */
1167 if ((c = zend_hash_num_elements(Z_ARRVAL_P(zranges))) == 1) {
1168 char range_header[256] = {0};
1169
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);
1173
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);
1177
1178 /* send requested chunk */
1179 return http_send_chunk(data, **begin, **end + 1, mode);
1180 }
1181
1182 /* multi range */
1183 else {
1184 int i;
1185 char bound[23] = {0}, preface[1024] = {0},
1186 multi_header[68] = "Content-Type: multipart/byteranges; boundary=";
1187
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);
1191
1192 /* send each requested chunk */
1193 for ( i = 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(zranges));
1194 i < c;
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)) {
1202 break;
1203 }
1204
1205 snprintf(preface, 1023,
1206 HTTP_CRLF "%s"
1207 HTTP_CRLF "Content-Type: %s"
1208 HTTP_CRLF "Content-Range: bytes %ld-%ld/%ld"
1209 HTTP_CRLF
1210 HTTP_CRLF,
1211
1212 bound,
1213 HTTP_G(ctype) ? HTTP_G(ctype) : "application/x-octetstream",
1214 **begin,
1215 **end,
1216 size
1217 );
1218
1219 php_body_write(preface, strlen(preface) TSRMLS_CC);
1220 http_send_chunk(data, **begin, **end + 1, mode);
1221 }
1222
1223 /* write boundary once more */
1224 php_body_write(HTTP_CRLF, 2 TSRMLS_CC);
1225 php_body_write(bound, strlen(bound) TSRMLS_CC);
1226
1227 return SUCCESS;
1228 }
1229 }
1230 /* }}} */
1231
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)
1235 {
1236 int is_range_request = http_is_range_request();
1237
1238 if (!data_ptr) {
1239 return FAILURE;
1240 }
1241
1242 /* etag handling */
1243 if (HTTP_G(etag_started)) {
1244 char *etag;
1245 /* interrupt */
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))) {
1250 return FAILURE;
1251 }
1252
1253 /* send 304 Not Modified if etag matches */
1254 if ((!is_range_request) && http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
1255 efree(etag);
1256 return http_send_status(304);
1257 }
1258
1259 http_send_etag(etag, 32);
1260 efree(etag);
1261 }
1262
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);
1266 }
1267
1268 if (is_range_request) {
1269
1270 /* only send ranges if entity hasn't changed */
1271 if (
1272 ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_MATCH", 13)) ||
1273 http_etag_match("HTTP_IF_MATCH", HTTP_G(etag)))
1274 &&
1275 ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
1276 http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod)))
1277 ) {
1278
1279 STATUS result = FAILURE;
1280 zval *zranges = NULL;
1281 MAKE_STD_ZVAL(zranges);
1282 array_init(zranges);
1283
1284 switch (http_get_request_ranges(zranges, data_size))
1285 {
1286 case RANGE_NO:
1287 zval_dtor(zranges);
1288 efree(zranges);
1289 /* go ahead and send all */
1290 break;
1291
1292 case RANGE_OK:
1293 result = http_send_ranges(zranges, data_ptr, data_size, data_mode);
1294 zval_dtor(zranges);
1295 efree(zranges);
1296 return result;
1297 break;
1298
1299 case RANGE_ERR:
1300 zval_dtor(zranges);
1301 efree(zranges);
1302 http_send_status(416);
1303 return FAILURE;
1304 break;
1305
1306 default:
1307 return FAILURE;
1308 break;
1309 }
1310 }
1311 }
1312 /* send all */
1313 return http_send_chunk(data_ptr, 0, data_size, data_mode);
1314 }
1315 /* }}} */
1316
1317 /* {{{ STATUS http_send_data(zval *) */
1318 PHP_HTTP_API STATUS _http_send_data(const zval *zdata TSRMLS_DC)
1319 {
1320 if (!Z_STRLEN_P(zdata)) {
1321 return SUCCESS;
1322 }
1323 if (!Z_STRVAL_P(zdata)) {
1324 return FAILURE;
1325 }
1326
1327 return http_send(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata), SEND_DATA);
1328 }
1329 /* }}} */
1330
1331 /* {{{ STATUS http_send_stream(php_stream *) */
1332 PHP_HTTP_API STATUS _http_send_stream(const php_stream *file TSRMLS_DC)
1333 {
1334 if (php_stream_stat((php_stream *) file, &HTTP_G(ssb))) {
1335 return FAILURE;
1336 }
1337
1338 return http_send(file, HTTP_G(ssb).sb.st_size, SEND_RSRC);
1339 }
1340 /* }}} */
1341
1342 /* {{{ STATUS http_send_file(zval *) */
1343 PHP_HTTP_API STATUS _http_send_file(const zval *zfile TSRMLS_DC)
1344 {
1345 php_stream *file;
1346 STATUS ret;
1347
1348 if (!Z_STRLEN_P(zfile)) {
1349 return FAILURE;
1350 }
1351
1352 if (!(file = php_stream_open_wrapper(Z_STRVAL_P(zfile), "rb",
1353 REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
1354 return FAILURE;
1355 }
1356
1357 ret = http_send_stream(file);
1358 php_stream_close(file);
1359 return ret;
1360 }
1361 /* }}} */
1362
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)
1366 {
1367 const char *e_ptr;
1368 char *d_ptr;
1369
1370 *decoded_len = 0;
1371 *decoded = ecalloc(1, encoded_len);
1372 d_ptr = *decoded;
1373 e_ptr = encoded;
1374
1375 while (((e_ptr - encoded) - encoded_len) > 0) {
1376 char hex_len[9] = {0};
1377 size_t chunk_len = 0;
1378 int i = 0;
1379
1380 /* read in chunk size */
1381 while (isxdigit(*e_ptr)) {
1382 if (i == 9) {
1383 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1384 "Chunk size is too long: 0x%s...", hex_len);
1385 efree(*decoded);
1386 return FAILURE;
1387 }
1388 hex_len[i++] = *e_ptr++;
1389 }
1390
1391 /* reached the end */
1392 if (!strcmp(hex_len, "0")) {
1393 break;
1394 }
1395
1396 /* new line */
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));
1401 efree(*decoded);
1402 return FAILURE;
1403 }
1404
1405 /* hex to long */
1406 {
1407 char *error = NULL;
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);
1412 efree(*decoded);
1413 return FAILURE;
1414 }
1415 }
1416
1417 memcpy(d_ptr, e_ptr += 2, chunk_len);
1418 d_ptr += chunk_len;
1419 e_ptr += chunk_len + 2;
1420 *decoded_len += chunk_len;
1421 }
1422
1423 return SUCCESS;
1424 }
1425 /* }}} */
1426
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)
1430 {
1431 char *body = NULL;
1432 char *header = response;
1433
1434 while ((response - header + 4) < response_len) {
1435 if ( (*response++ == '\r') &&
1436 (*response++ == '\n') &&
1437 (*response++ == '\r') &&
1438 (*response++ == '\n')) {
1439 body = response;
1440 break;
1441 }
1442 }
1443
1444 if (body && (response_len - (body - header))) {
1445 ZVAL_STRINGL(zbody, body, response_len - (body - header) - 1, 1);
1446 } else {
1447 Z_TYPE_P(zbody) = IS_NULL;
1448 }
1449
1450 return http_parse_headers(header, body ? body - header : response_len, zheaders);
1451 }
1452 /* }}} */
1453
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)
1456 {
1457 char *colon = NULL, *line = NULL, *begin = header;
1458
1459 if (header_len < 2) {
1460 return FAILURE;
1461 }
1462
1463 /* status code */
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);
1469 header = end + 2;
1470 }
1471
1472 line = header;
1473
1474 while (header_len >= (line - begin)) {
1475 int value_len = 0;
1476
1477 switch (*line++)
1478 {
1479 case 0:
1480 --value_len; /* we don't have CR so value length is one char less */
1481 case '\n':
1482 if (colon && ((!(*line - 1)) || ((*line != ' ') && (*line != '\t')))) {
1483
1484 /* skip empty key */
1485 if (header != colon) {
1486 char *key = estrndup(header, colon - header);
1487 value_len += line - colon - 1;
1488
1489 /* skip leading ws */
1490 while (isspace(*(++colon))) --value_len;
1491 /* skip trailing ws */
1492 while (isspace(colon[value_len - 1])) --value_len;
1493
1494 if (value_len < 1) {
1495 /* hm, empty header? */
1496 add_assoc_stringl(array, key, "", 0, 1);
1497 } else {
1498 add_assoc_stringl(array, key, colon, value_len, 1);
1499 }
1500 efree(key);
1501 }
1502
1503 colon = NULL;
1504 value_len = 0;
1505 header += line - header;
1506 }
1507 break;
1508
1509 case ':':
1510 if (!colon) {
1511 colon = line - 1;
1512 }
1513 break;
1514 }
1515 }
1516 return SUCCESS;
1517 }
1518 /* }}} */
1519
1520 /* {{{ void http_get_request_headers(zval *) */
1521 PHP_HTTP_API void _http_get_request_headers(zval *array TSRMLS_DC)
1522 {
1523 char *key;
1524
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)) {
1529 zval **header;
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);
1532 }
1533 }
1534 }
1535 /* }}} */
1536
1537 /* {{{ STATUS http_auth_header(char *, char*) */
1538 PHP_HTTP_API STATUS _http_auth_header(const char *type, const char *realm TSRMLS_DC)
1539 {
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);
1543 }
1544 /* }}} */
1545
1546 /* {{{ STATUS http_auth_credentials(char **, char **) */
1547 PHP_HTTP_API STATUS _http_auth_credentials(char **user, char **pass TSRMLS_DC)
1548 {
1549 if (strncmp(sapi_module.name, "isapi", 5)) {
1550 zval *zuser, *zpass;
1551
1552 HTTP_GSC(zuser, "PHP_AUTH_USER", FAILURE);
1553 HTTP_GSC(zpass, "PHP_AUTH_PW", FAILURE);
1554
1555 *user = estrndup(Z_STRVAL_P(zuser), Z_STRLEN_P(zuser));
1556 *pass = estrndup(Z_STRVAL_P(zpass), Z_STRLEN_P(zpass));
1557
1558 return SUCCESS;
1559 } else {
1560 zval *zauth = NULL;
1561 HTTP_GSC(zauth, "HTTP_AUTHORIZATION", FAILURE);
1562 {
1563 char *decoded, *colon;
1564 int decoded_len;
1565 decoded = php_base64_decode(Z_STRVAL_P(zauth), Z_STRLEN_P(zauth),
1566 &decoded_len);
1567
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);
1571
1572 return SUCCESS;
1573 } else {
1574 return FAILURE;
1575 }
1576 }
1577 }
1578 }
1579 /* }}} */
1580
1581 /* }}} public API */
1582
1583 /*
1584 * Local variables:
1585 * tab-width: 4
1586 * c-basic-offset: 4
1587 * End:
1588 * vim600: noet sw=4 ts=4 fdm=marker
1589 * vim<600: noet sw=4 ts=4
1590 */
1591