* simplify HTTPi_Response::send() a lot
[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 #ifdef HTTP_HAVE_CURL
50
51 # ifdef PHP_WIN32
52 # include <winsock2.h>
53 # include <sys/types.h>
54 # endif
55
56 # include <curl/curl.h>
57 # include <curl/easy.h>
58
59 #endif
60
61
62 ZEND_DECLARE_MODULE_GLOBALS(http)
63
64 /* {{{ day/month names */
65 static const char *days[] = {
66 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
67 };
68 static const char *wkdays[] = {
69 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
70 };
71 static const char *weekdays[] = {
72 "Monday", "Tuesday", "Wednesday",
73 "Thursday", "Friday", "Saturday", "Sunday"
74 };
75 static const char *months[] = {
76 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
77 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
78 };
79 enum assume_next {
80 DATE_MDAY,
81 DATE_YEAR,
82 DATE_TIME
83 };
84 static const struct time_zone {
85 const char *name;
86 const int offset;
87 } time_zones[] = {
88 {"GMT", 0}, /* Greenwich Mean */
89 {"UTC", 0}, /* Universal (Coordinated) */
90 {"WET", 0}, /* Western European */
91 {"BST", 0}, /* British Summer */
92 {"WAT", 60}, /* West Africa */
93 {"AST", 240}, /* Atlantic Standard */
94 {"ADT", 240}, /* Atlantic Daylight */
95 {"EST", 300}, /* Eastern Standard */
96 {"EDT", 300}, /* Eastern Daylight */
97 {"CST", 360}, /* Central Standard */
98 {"CDT", 360}, /* Central Daylight */
99 {"MST", 420}, /* Mountain Standard */
100 {"MDT", 420}, /* Mountain Daylight */
101 {"PST", 480}, /* Pacific Standard */
102 {"PDT", 480}, /* Pacific Daylight */
103 {"YST", 540}, /* Yukon Standard */
104 {"YDT", 540}, /* Yukon Daylight */
105 {"HST", 600}, /* Hawaii Standard */
106 {"HDT", 600}, /* Hawaii Daylight */
107 {"CAT", 600}, /* Central Alaska */
108 {"AHST", 600}, /* Alaska-Hawaii Standard */
109 {"NT", 660}, /* Nome */
110 {"IDLW", 720}, /* International Date Line West */
111 {"CET", -60}, /* Central European */
112 {"MET", -60}, /* Middle European */
113 {"MEWT", -60}, /* Middle European Winter */
114 {"MEST", -120}, /* Middle European Summer */
115 {"CEST", -120}, /* Central European Summer */
116 {"MESZ", -60}, /* Middle European Summer */
117 {"FWT", -60}, /* French Winter */
118 {"FST", -60}, /* French Summer */
119 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
120 {"WAST", -420}, /* West Australian Standard */
121 {"WADT", -420}, /* West Australian Daylight */
122 {"CCT", -480}, /* China Coast, USSR Zone 7 */
123 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
124 {"EAST", -600}, /* Eastern Australian Standard */
125 {"EADT", -600}, /* Eastern Australian Daylight */
126 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
127 {"NZT", -720}, /* New Zealand */
128 {"NZST", -720}, /* New Zealand Standard */
129 {"NZDT", -720}, /* New Zealand Daylight */
130 {"IDLE", -720}, /* International Date Line East */
131 };
132 /* }}} */
133
134 /* {{{ internals */
135
136 static int http_sort_q(const void *a, const void *b TSRMLS_DC);
137 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
138 static STATUS _http_send_chunk(const void *data, const size_t begin, const size_t end, const http_send_mode mode TSRMLS_DC);
139
140 static int check_day(char *day, size_t len);
141 static int check_month(char *month);
142 static int check_tzone(char *tzone);
143
144 static char *pretty_key(char *key, int key_len, int uctitle, int xhyphen);
145
146 static int http_ob_stack_get(php_ob_buffer *, php_ob_buffer **);
147
148 /* {{{ HAVE_CURL */
149 #ifdef HTTP_HAVE_CURL
150 #define http_curl_initbuf(m) _http_curl_initbuf((m) TSRMLS_CC)
151 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC);
152 #define http_curl_freebuf(m) _http_curl_freebuf((m) TSRMLS_CC)
153 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC);
154 #define http_curl_sizebuf(m, l) _http_curl_sizebuf((m), (l) TSRMLS_CC)
155 static inline void _http_curl_sizebuf(http_curlbuf_member member, size_t len TSRMLS_DC);
156 #define http_curl_movebuf(m, d, l) _http_curl_movebuf((m), (d), (l) TSRMLS_CC)
157 static inline void _http_curl_movebuf(http_curlbuf_member member, char **data, size_t *data_len TSRMLS_DC);
158 #define http_curl_copybuf(m, d, l) _http_curl_copybuf((m), (d), (l) TSRMLS_CC)
159 static inline void _http_curl_copybuf(http_curlbuf_member member, char **data, size_t *data_len TSRMLS_DC);
160 #define http_curl_setopts(c, u, o) _http_curl_setopts((c), (u), (o) TSRMLS_CC)
161 static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *options TSRMLS_DC);
162
163 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
164 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
165 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
166 static inline zval *_http_curl_getopt(HashTable *options, char *key TSRMLS_DC, int checks, ...);
167
168 static size_t http_curl_body_callback(char *, size_t, size_t, void *);
169 static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *);
170
171 #define http_curl_getinfo(c, h) _http_curl_getinfo((c), (h) TSRMLS_CC)
172 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC);
173 #define http_curl_getinfo_ex(c, i, a) _http_curl_getinfo_ex((c), (i), (a) TSRMLS_CC)
174 static inline void _http_curl_getinfo_ex(CURL *ch, CURLINFO i, zval *array TSRMLS_DC);
175 #define http_curl_getinfoname(i) _http_curl_getinfoname((i) TSRMLS_CC)
176 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC);
177
178 #endif
179 /* }}} HAVE_CURL */
180
181 /* {{{ inline char *http_etag(void *, size_t, http_send_mode) */
182 inline char *_http_etag(const void *data_ptr, const size_t data_len,
183 const http_send_mode data_mode TSRMLS_DC)
184 {
185 char ssb_buf[128] = {0};
186 unsigned char digest[16];
187 PHP_MD5_CTX ctx;
188 char *new_etag = ecalloc(33, 1);
189
190 PHP_MD5Init(&ctx);
191
192 switch (data_mode)
193 {
194 case SEND_DATA:
195 PHP_MD5Update(&ctx, data_ptr, data_len);
196 break;
197
198 case SEND_RSRC:
199 if (!HTTP_G(ssb).sb.st_ino) {
200 if (php_stream_stat((php_stream *) data_ptr, &HTTP_G(ssb))) {
201 return NULL;
202 }
203 }
204 snprintf(ssb_buf, 127, "%ld=%ld=%ld",
205 HTTP_G(ssb).sb.st_mtime,
206 HTTP_G(ssb).sb.st_ino,
207 HTTP_G(ssb).sb.st_size
208 );
209 PHP_MD5Update(&ctx, ssb_buf, strlen(ssb_buf));
210 break;
211
212 default:
213 efree(new_etag);
214 return NULL;
215 break;
216 }
217
218 PHP_MD5Final(digest, &ctx);
219 make_digest(new_etag, digest);
220
221 return new_etag;
222 }
223 /* }}} */
224
225 /* {{{ inline http_lmod(void *, http_send_mode) */
226 inline time_t _http_lmod(const void *data_ptr, const http_send_mode data_mode TSRMLS_DC)
227 {
228 switch (data_mode)
229 {
230 case SEND_DATA:
231 {
232 return time(NULL);
233 }
234
235 case SEND_RSRC:
236 {
237 if (!HTTP_G(ssb).sb.st_mtime) {
238 if (php_stream_stat((php_stream *) data_ptr, &HTTP_G(ssb))) {
239 return 0;
240 }
241 }
242 return HTTP_G(ssb).sb.st_mtime;
243 }
244
245 default:
246 {
247 zval mtime;
248 php_stat(Z_STRVAL_P((zval *) data_ptr), Z_STRLEN_P((zval *) data_ptr), FS_MTIME, &mtime TSRMLS_CC);
249 return Z_LVAL(mtime);
250 }
251 }
252 }
253 /* }}} */
254
255 /* {{{inline int http_is_range_request(void) */
256 inline int _http_is_range_request(TSRMLS_D)
257 {
258 return zend_hash_exists(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]),
259 "HTTP_RANGE", strlen("HTTP_RANGE") + 1);
260 }
261 /* }}} */
262
263 /* {{{ static int http_sort_q(const void *, const void *) */
264 static int http_sort_q(const void *a, const void *b TSRMLS_DC)
265 {
266 Bucket *f, *s;
267 zval result, *first, *second;
268
269 f = *((Bucket **) a);
270 s = *((Bucket **) b);
271
272 first = *((zval **) f->pData);
273 second= *((zval **) s->pData);
274
275 if (numeric_compare_function(&result, first, second TSRMLS_CC) != SUCCESS) {
276 return 0;
277 }
278 return (Z_LVAL(result) > 0 ? -1 : (Z_LVAL(result) < 0 ? 1 : 0));
279 }
280 /* }}} */
281
282 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
283 http_send_mode) */
284 static STATUS _http_send_chunk(const void *data, const size_t begin,
285 const size_t end, const http_send_mode mode TSRMLS_DC)
286 {
287 char *buf;
288 size_t read = 0;
289 long len = end - begin;
290 php_stream *s;
291
292 switch (mode)
293 {
294 case SEND_RSRC:
295 s = (php_stream *) data;
296 if (php_stream_seek(s, begin, SEEK_SET)) {
297 return FAILURE;
298 }
299 buf = (char *) ecalloc(1, HTTP_BUF_SIZE);
300 /* read into buf and write out */
301 while ((len -= HTTP_BUF_SIZE) >= 0) {
302 if (!(read = php_stream_read(s, buf, HTTP_BUF_SIZE))) {
303 efree(buf);
304 return FAILURE;
305 }
306 if (read - php_body_write(buf, read TSRMLS_CC)) {
307 efree(buf);
308 return FAILURE;
309 }
310 }
311
312 /* read & write left over */
313 if (len) {
314 if (read = php_stream_read(s, buf, HTTP_BUF_SIZE + len)) {
315 if (read - php_body_write(buf, read TSRMLS_CC)) {
316 efree(buf);
317 return FAILURE;
318 }
319 } else {
320 efree(buf);
321 return FAILURE;
322 }
323 }
324 efree(buf);
325 return SUCCESS;
326 break;
327
328 case SEND_DATA:
329 return len == php_body_write(((char *)data) + begin, len TSRMLS_CC)
330 ? SUCCESS : FAILURE;
331 break;
332
333 default:
334 return FAILURE;
335 break;
336 }
337 }
338 /* }}} */
339
340 /* {{{ HAVE_CURL */
341 #ifdef HTTP_HAVE_CURL
342
343 /* {{{ static inline void http_curl_initbuf(http_curlbuf_member) */
344 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC)
345 {
346 http_curl_freebuf(member);
347
348 if (member & CURLBUF_HDRS) {
349 HTTP_G(curlbuf).hdrs.data = emalloc(HTTP_CURLBUF_HDRSSIZE);
350 HTTP_G(curlbuf).hdrs.free = HTTP_CURLBUF_HDRSSIZE;
351 }
352 if (member & CURLBUF_BODY) {
353 HTTP_G(curlbuf).body.data = emalloc(HTTP_CURLBUF_BODYSIZE);
354 HTTP_G(curlbuf).body.free = HTTP_CURLBUF_BODYSIZE;
355 }
356 }
357 /* }}} */
358
359 /* {{{ static inline void http_curl_freebuf(http_curlbuf_member) */
360 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC)
361 {
362 if (member & CURLBUF_HDRS) {
363 if (HTTP_G(curlbuf).hdrs.data) {
364 efree(HTTP_G(curlbuf).hdrs.data);
365 HTTP_G(curlbuf).hdrs.data = NULL;
366 }
367 HTTP_G(curlbuf).hdrs.used = 0;
368 HTTP_G(curlbuf).hdrs.free = 0;
369 }
370 if (member & CURLBUF_BODY) {
371 if (HTTP_G(curlbuf).body.data) {
372 efree(HTTP_G(curlbuf).body.data);
373 HTTP_G(curlbuf).body.data = NULL;
374 }
375 HTTP_G(curlbuf).body.used = 0;
376 HTTP_G(curlbuf).body.free = 0;
377 }
378 }
379 /* }}} */
380
381 /* {{{ static inline void http_curl_copybuf(http_curlbuf_member, char **,
382 size_t *) */
383 static inline void _http_curl_copybuf(http_curlbuf_member member, char **data,
384 size_t *data_len TSRMLS_DC)
385 {
386 *data = NULL;
387 *data_len = 0;
388
389 if ((member & CURLBUF_HDRS) && HTTP_G(curlbuf).hdrs.used) {
390 if ((member & CURLBUF_BODY) && HTTP_G(curlbuf).body.used) {
391 *data = emalloc(HTTP_G(curlbuf).hdrs.used + HTTP_G(curlbuf).body.used + 1);
392 } else {
393 *data = emalloc(HTTP_G(curlbuf).hdrs.used + 1);
394 }
395 memcpy(*data, HTTP_G(curlbuf).hdrs.data, HTTP_G(curlbuf).hdrs.used);
396 *data_len = HTTP_G(curlbuf).hdrs.used;
397 }
398
399 if ((member & CURLBUF_BODY) && HTTP_G(curlbuf).body.used) {
400 if (*data) {
401 memcpy((*data) + HTTP_G(curlbuf).hdrs.used,
402 HTTP_G(curlbuf).body.data, HTTP_G(curlbuf).body.used);
403 *data_len = HTTP_G(curlbuf).hdrs.used + HTTP_G(curlbuf).body.used;
404 } else {
405 emalloc(HTTP_G(curlbuf).body.used + 1);
406 memcpy(*data, HTTP_G(curlbuf).body.data, HTTP_G(curlbuf).body.used);
407 *data_len = HTTP_G(curlbuf).body.used;
408 }
409 }
410 if (*data) {
411 (*data)[*data_len] = 0;
412 } else {
413 *data = "";
414 }
415 }
416 /* }}} */
417
418 /* {{{ static inline void http_curl_movebuf(http_curlbuf_member, char **,
419 size_t *) */
420 static inline void _http_curl_movebuf(http_curlbuf_member member, char **data,
421 size_t *data_len TSRMLS_DC)
422 {
423 http_curl_copybuf(member, data, data_len);
424 http_curl_freebuf(member);
425 }
426 /* }}} */
427
428 /* {{{ static size_t http_curl_body_callback(char *, size_t, size_t, void *) */
429 static size_t http_curl_body_callback(char *buf, size_t len, size_t n, void *s)
430 {
431 TSRMLS_FETCH();
432
433 if ((len *= n) > HTTP_G(curlbuf).body.free) {
434 size_t bsize = HTTP_CURLBUF_BODYSIZE;
435 while (bsize < len) {
436 bsize *= 2;
437 }
438 HTTP_G(curlbuf).body.data = erealloc(HTTP_G(curlbuf).body.data,
439 HTTP_G(curlbuf).body.used + bsize);
440 HTTP_G(curlbuf).body.free += bsize;
441 }
442
443 memcpy(HTTP_G(curlbuf).body.data + HTTP_G(curlbuf).body.used, buf, len);
444 HTTP_G(curlbuf).body.free -= len;
445 HTTP_G(curlbuf).body.used += len;
446
447 return len;
448 }
449 /* }}} */
450
451 /* {{{ static size_t http_curl_hdrs_callback(char*, size_t, size_t, void *) */
452 static size_t http_curl_hdrs_callback(char *buf, size_t len, size_t n, void *s)
453 {
454 TSRMLS_FETCH();
455
456 /* discard previous headers */
457 if ((HTTP_G(curlbuf).hdrs.used) && (!strncmp(buf, "HTTP/1.", strlen("HTTP/1.")))) {
458 http_curl_initbuf(CURLBUF_HDRS);
459 }
460
461 if ((len *= n) > HTTP_G(curlbuf).hdrs.free) {
462 size_t bsize = HTTP_CURLBUF_HDRSSIZE;
463 while (bsize < len) {
464 bsize *= 2;
465 }
466 HTTP_G(curlbuf).hdrs.data = erealloc(HTTP_G(curlbuf).hdrs.data,
467 HTTP_G(curlbuf).hdrs.used + bsize);
468 HTTP_G(curlbuf).hdrs.free += bsize;
469 }
470
471 memcpy(HTTP_G(curlbuf).hdrs.data + HTTP_G(curlbuf).hdrs.used, buf, len);
472 HTTP_G(curlbuf).hdrs.free -= len;
473 HTTP_G(curlbuf).hdrs.used += len;
474
475 return len;
476 }
477 /* }}} */
478
479 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
480 static inline zval *_http_curl_getopt(HashTable *options, char *key TSRMLS_DC, int checks, ...)
481 {
482 zval **zoption;
483 va_list types;
484 int i;
485
486 if (SUCCESS != zend_hash_find(options, key, strlen(key) + 1, (void **) &zoption)) {
487 return NULL;
488 }
489 if (checks < 1) {
490 return *zoption;
491 }
492
493 va_start(types, checks);
494 for (i = 0; i < checks; ++i) {
495 if ((va_arg(types, int)) == (Z_TYPE_PP(zoption))) {
496 va_end(types);
497 return *zoption;
498 }
499 }
500 va_end(types);
501 return NULL;
502 }
503 /* }}} */
504
505 /* {{{ static inline void http_curl_setopts(CURL *, char *, HashTable *) */
506 static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *options TSRMLS_DC)
507 {
508 zval *zoption;
509
510 /* standard options */
511 curl_easy_setopt(ch, CURLOPT_URL, url);
512 curl_easy_setopt(ch, CURLOPT_HEADER, 0);
513 curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1);
514 curl_easy_setopt(ch, CURLOPT_AUTOREFERER, 1);
515 curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, http_curl_body_callback);
516 curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, http_curl_hdrs_callback);
517 #ifdef ZTS
518 curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1);
519 #endif
520
521 if ((!options) || (1 > zend_hash_num_elements(options))) {
522 return;
523 }
524
525 /* redirects, defaults to 0 */
526 if (zoption = http_curl_getopt1(options, "redirect", IS_LONG)) {
527 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
528 curl_easy_setopt(ch, CURLOPT_MAXREDIRS, Z_LVAL_P(zoption));
529 if (zoption = http_curl_getopt2(options, "unrestrictedauth", IS_LONG, IS_BOOL)) {
530 curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
531 }
532 } else {
533 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0);
534 }
535
536 /* proxy */
537 if (zoption = http_curl_getopt1(options, "proxyhost", IS_STRING)) {
538 curl_easy_setopt(ch, CURLOPT_PROXY, Z_STRVAL_P(zoption));
539 /* port */
540 if (zoption = http_curl_getopt1(options, "proxyport", IS_LONG)) {
541 curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
542 }
543 /* user:pass */
544 if (zoption = http_curl_getopt1(options, "proxyauth", IS_STRING)) {
545 curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption));
546 }
547 /* auth method */
548 if (zoption = http_curl_getopt1(options, "proxyauthtype", IS_LONG)) {
549 curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
550 }
551 }
552
553 /* auth */
554 if (zoption = http_curl_getopt1(options, "httpauth", IS_STRING)) {
555 curl_easy_setopt(ch, CURLOPT_USERPWD, Z_STRVAL_P(zoption));
556 }
557 if (zoption = http_curl_getopt1(options, "httpauthtype", IS_LONG)) {
558 curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
559 }
560
561 /* compress, enabled by default (empty string enables deflate and gzip) */
562 if (zoption = http_curl_getopt2(options, "compress", IS_LONG, IS_BOOL)) {
563 if (Z_LVAL_P(zoption)) {
564 curl_easy_setopt(ch, CURLOPT_ENCODING, "");
565 }
566 } else {
567 curl_easy_setopt(ch, CURLOPT_ENCODING, "");
568 }
569
570 /* another port */
571 if (zoption = http_curl_getopt1(options, "port", IS_LONG)) {
572 curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption));
573 }
574
575 /* referer */
576 if (zoption = http_curl_getopt1(options, "referer", IS_STRING)) {
577 curl_easy_setopt(ch, CURLOPT_REFERER, Z_STRVAL_P(zoption));
578 }
579
580 /* useragent, default "PECL::HTTP/version (PHP/version)" */
581 if (zoption = http_curl_getopt1(options, "useragent", IS_STRING)) {
582 curl_easy_setopt(ch, CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
583 } else {
584 curl_easy_setopt(ch, CURLOPT_USERAGENT,
585 "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
586 }
587
588 /* cookies, array('name' => 'value') */
589 if (zoption = http_curl_getopt1(options, "cookies", IS_ARRAY)) {
590 char *cookie_key;
591 zval **cookie_val;
592 int key_type;
593 smart_str qstr = {0};
594
595 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
596 while (HASH_KEY_NON_EXISTANT != (key_type = zend_hash_get_current_key_type(Z_ARRVAL_P(zoption)))) {
597 if (key_type == HASH_KEY_IS_STRING) {
598 zend_hash_get_current_key(Z_ARRVAL_P(zoption), &cookie_key, NULL, 0);
599 zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val);
600 smart_str_appends(&qstr, cookie_key);
601 smart_str_appendl(&qstr, "=", 1);
602 smart_str_appendl(&qstr, Z_STRVAL_PP(cookie_val), Z_STRLEN_PP(cookie_val));
603 smart_str_appendl(&qstr, "; ", 2);
604 zend_hash_move_forward(Z_ARRVAL_P(zoption));
605 }
606 }
607 smart_str_0(&qstr);
608
609 if (qstr.c) {
610 curl_easy_setopt(ch, CURLOPT_COOKIE, qstr.c);
611 }
612 }
613
614 /* cookiestore */
615 if (zoption = http_curl_getopt1(options, "cookiestore", IS_STRING)) {
616 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, Z_STRVAL_P(zoption));
617 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, Z_STRVAL_P(zoption));
618 }
619
620 /* additional headers, array('name' => 'value') */
621 if (zoption = http_curl_getopt1(options, "headers", IS_ARRAY)) {
622 int key_type;
623 char *header_key, header[1024] = {0};
624 zval **header_val;
625 struct curl_slist *headers = NULL;
626
627 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
628 while (HASH_KEY_NON_EXISTANT != (key_type = zend_hash_get_current_key_type(Z_ARRVAL_P(zoption)))) {
629 if (key_type == HASH_KEY_IS_STRING) {
630 zend_hash_get_current_key(Z_ARRVAL_P(zoption), &header_key, NULL, 0);
631 zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val);
632 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
633 headers = curl_slist_append(headers, header);
634 zend_hash_move_forward(Z_ARRVAL_P(zoption));
635 }
636 }
637 if (headers) {
638 curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
639 }
640 }
641 }
642 /* }}} */
643
644 /* {{{ static inline char *http_curl_getinfoname(CURLINFO) */
645 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC)
646 {
647 #define CASE(I) case CURLINFO_ ##I : return pretty_key(estrdup( #I ), strlen(#I), 0, 0)
648 switch (i)
649 {
650 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
651 CASE(EFFECTIVE_URL);
652 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
653 CASE(RESPONSE_CODE);
654 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
655 CASE(TOTAL_TIME);
656 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
657 CASE(NAMELOOKUP_TIME);
658 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
659 CASE(CONNECT_TIME);
660 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
661 CASE(PRETRANSFER_TIME);
662 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
663 CASE(SIZE_UPLOAD);
664 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
665 CASE(SIZE_DOWNLOAD);
666 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
667 CASE(SPEED_DOWNLOAD);
668 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
669 CASE(SPEED_UPLOAD);
670 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
671 CASE(HEADER_SIZE);
672 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
673 CASE(REQUEST_SIZE);
674 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
675 CASE(SSL_VERIFYRESULT);
676 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
677 CASE(FILETIME);
678 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
679 CASE(CONTENT_LENGTH_DOWNLOAD);
680 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
681 CASE(CONTENT_LENGTH_UPLOAD);
682 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
683 CASE(STARTTRANSFER_TIME);
684 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
685 CASE(CONTENT_TYPE);
686 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
687 CASE(REDIRECT_TIME);
688 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
689 CASE(REDIRECT_COUNT);
690 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
691 CASE(PRIVATE);
692 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
693 CASE(HTTP_CONNECTCODE);
694 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
695 CASE(HTTPAUTH_AVAIL);
696 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
697 CASE(PROXYAUTH_AVAIL);
698 }
699 #undef CASE
700 return NULL;
701 }
702 /* }}} */
703
704 /* {{{ static inline void http_curl_getinfo_ex(CURL, CURLINFO, zval *) */
705 static inline void _http_curl_getinfo_ex(CURL *ch, CURLINFO i, zval *array TSRMLS_DC)
706 {
707 char *key;
708 if (key = http_curl_getinfoname(i)) {
709 switch (i & ~CURLINFO_MASK)
710 {
711 case CURLINFO_STRING:
712 {
713 char *c;
714 if (CURLE_OK == curl_easy_getinfo(ch, i, &c)) {
715 add_assoc_string(array, key, c ? c : "", 1);
716 }
717 }
718 break;
719
720 case CURLINFO_DOUBLE:
721 {
722 double d;
723 if (CURLE_OK == curl_easy_getinfo(ch, i, &d)) {
724 add_assoc_double(array, key, d);
725 }
726 }
727 break;
728
729 case CURLINFO_LONG:
730 {
731 long l;
732 if (CURLE_OK == curl_easy_getinfo(ch, i, &l)) {
733 add_assoc_long(array, key, l);
734 }
735 }
736 break;
737 }
738 }
739 }
740 /* }}} */
741
742 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
743 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC)
744 {
745 zval array;
746 Z_ARRVAL(array) = info;
747
748 #define INFO(I) http_curl_getinfo_ex(ch, CURLINFO_ ##I , &array)
749 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
750 INFO(EFFECTIVE_URL);
751 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
752 INFO(RESPONSE_CODE);
753 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
754 INFO(TOTAL_TIME);
755 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
756 INFO(NAMELOOKUP_TIME);
757 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
758 INFO(CONNECT_TIME);
759 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
760 INFO(PRETRANSFER_TIME);
761 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
762 INFO(SIZE_UPLOAD);
763 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
764 INFO(SIZE_DOWNLOAD);
765 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
766 INFO(SPEED_DOWNLOAD);
767 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
768 INFO(SPEED_UPLOAD);
769 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
770 INFO(HEADER_SIZE);
771 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
772 INFO(REQUEST_SIZE);
773 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
774 INFO(SSL_VERIFYRESULT);
775 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
776 INFO(FILETIME);
777 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
778 INFO(CONTENT_LENGTH_DOWNLOAD);
779 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
780 INFO(CONTENT_LENGTH_UPLOAD);
781 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
782 INFO(STARTTRANSFER_TIME);
783 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
784 INFO(CONTENT_TYPE);
785 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
786 INFO(REDIRECT_TIME);
787 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
788 INFO(REDIRECT_COUNT);
789 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
790 INFO(PRIVATE);
791 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
792 INFO(HTTP_CONNECTCODE);
793 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
794 INFO(HTTPAUTH_AVAIL);
795 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
796 INFO(PROXYAUTH_AVAIL);
797 #undef INFO
798 }
799 /* }}} */
800
801 #endif
802 /* }}} HAVE_CURL */
803
804 /* {{{ Day/Month/TZ checks for http_parse_date()
805 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
806 static int check_day(char *day, size_t len)
807 {
808 int i;
809 const char * const *check = (len > 3) ? &weekdays[0] : &wkdays[0];
810 for (i = 0; i < 7; i++) {
811 if (!strcmp(day, check[0])) {
812 return i;
813 }
814 check++;
815 }
816 return -1;
817 }
818
819 static int check_month(char *month)
820 {
821 int i;
822 const char * const *check = &months[0];
823 for (i = 0; i < 12; i++) {
824 if (!strcmp(month, check[0])) {
825 return i;
826 }
827 check++;
828 }
829 return -1;
830 }
831
832 /* return the time zone offset between GMT and the input one, in number
833 of seconds or -1 if the timezone wasn't found/legal */
834
835 static int check_tzone(char *tzone)
836 {
837 int i;
838 const struct time_zone *check = time_zones;
839 for (i = 0; i < sizeof(time_zones) / sizeof(time_zones[0]); i++) {
840 if (!strcmp(tzone, check->name)) {
841 return check->offset * 60;
842 }
843 check++;
844 }
845 return -1;
846 }
847 /* }}} */
848
849 /* static char *pretty_key(char *, int, int, int) */
850 static char *pretty_key(char *key, int key_len, int uctitle, int xhyphen)
851 {
852 if (key && key_len) {
853 int i, wasalpha;
854 if (wasalpha = isalpha(key[0])) {
855 key[0] = uctitle ? toupper(key[0]) : tolower(key[0]);
856 }
857 for (i = 1; i < key_len; i++) {
858 if (isalpha(key[i])) {
859 key[i] = ((!wasalpha) && uctitle) ? toupper(key[i]) : tolower(key[i]);
860 wasalpha = 1;
861 } else {
862 if (xhyphen && (key[i] == '_')) {
863 key[i] = '-';
864 }
865 wasalpha = 0;
866 }
867 }
868 }
869 return key;
870 }
871 /* }}} */
872
873 /* {{{ static STATUS http_ob_stack_get(php_ob_buffer *, php_ob_buffer **) */
874 static STATUS http_ob_stack_get(php_ob_buffer *o, php_ob_buffer **s)
875 {
876 static int i = 0;
877 php_ob_buffer *b = emalloc(sizeof(php_ob_buffer));
878 b->handler_name = estrdup(o->handler_name);
879 b->buffer = estrndup(o->buffer, o->text_length);
880 b->text_length = o->text_length;
881 b->chunk_size = o->chunk_size;
882 b->erase = o->erase;
883 s[i++] = b;
884 return SUCCESS;
885 }
886 /* }}} */
887
888 /* }}} internals */
889
890 /* {{{ public API */
891
892 /* {{{ char *http_date(time_t) */
893 PHP_HTTP_API char *_http_date(time_t t TSRMLS_DC)
894 {
895 struct tm *gmtime, tmbuf;
896 char *date = ecalloc(31, 1);
897
898 gmtime = php_gmtime_r(&t, &tmbuf);
899 snprintf(date, 30,
900 "%s, %02d %s %04d %02d:%02d:%02d GMT",
901 days[gmtime->tm_wday], gmtime->tm_mday,
902 months[gmtime->tm_mon], gmtime->tm_year + 1900,
903 gmtime->tm_hour, gmtime->tm_min, gmtime->tm_sec
904 );
905 return date;
906 }
907 /* }}} */
908
909 /* {{{ time_t http_parse_date(char *)
910 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
911 PHP_HTTP_API time_t _http_parse_date(const char *date)
912 {
913 time_t t = 0;
914 int tz_offset = -1, year = -1, month = -1, monthday = -1, weekday = -1,
915 hours = -1, minutes = -1, seconds = -1;
916 struct tm tm;
917 enum assume_next dignext = DATE_MDAY;
918 const char *indate = date;
919
920 int found = 0, part = 0; /* max 6 parts */
921
922 while (*date && (part < 6)) {
923 int found = 0;
924
925 while (*date && !isalnum(*date)) {
926 date++;
927 }
928
929 if (isalpha(*date)) {
930 /* a name coming up */
931 char buf[32] = "";
932 size_t len;
933 sscanf(date, "%31[A-Za-z]", buf);
934 len = strlen(buf);
935
936 if (weekday == -1) {
937 weekday = check_day(buf, len);
938 if (weekday != -1) {
939 found = 1;
940 }
941 }
942
943 if (!found && (month == -1)) {
944 month = check_month(buf);
945 if (month != -1) {
946 found = 1;
947 }
948 }
949
950 if (!found && (tz_offset == -1)) {
951 /* this just must be a time zone string */
952 tz_offset = check_tzone(buf);
953 if (tz_offset != -1) {
954 found = 1;
955 }
956 }
957
958 if (!found) {
959 return -1; /* bad string */
960 }
961 date += len;
962 }
963 else if (isdigit(*date)) {
964 /* a digit */
965 int val;
966 char *end;
967 if((seconds == -1) && (3 == sscanf(date, "%02d:%02d:%02d", &hours, &minutes, &seconds))) {
968 /* time stamp! */
969 date += 8;
970 found = 1;
971 }
972 else {
973 val = (int) strtol(date, &end, 10);
974
975 if ((tz_offset == -1) && ((end - date) == 4) && (val < 1300) && (indate < date) && ((date[-1] == '+' || date[-1] == '-'))) {
976 /* four digits and a value less than 1300 and it is preceeded with
977 a plus or minus. This is a time zone indication. */
978 found = 1;
979 tz_offset = (val / 100 * 60 + val % 100) * 60;
980
981 /* the + and - prefix indicates the local time compared to GMT,
982 this we need ther reversed math to get what we want */
983 tz_offset = date[-1] == '+' ? -tz_offset : tz_offset;
984 }
985
986 if (((end - date) == 8) && (year == -1) && (month == -1) && (monthday == -1)) {
987 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
988 found = 1;
989 year = val / 10000;
990 month = (val % 10000) / 100 - 1; /* month is 0 - 11 */
991 monthday = val % 100;
992 }
993
994 if (!found && (dignext == DATE_MDAY) && (monthday == -1)) {
995 if ((val > 0) && (val < 32)) {
996 monthday = val;
997 found = 1;
998 }
999 dignext = DATE_YEAR;
1000 }
1001
1002 if (!found && (dignext == DATE_YEAR) && (year == -1)) {
1003 year = val;
1004 found = 1;
1005 if (year < 1900) {
1006 year += year > 70 ? 1900 : 2000;
1007 }
1008 if(monthday == -1) {
1009 dignext = DATE_MDAY;
1010 }
1011 }
1012
1013 if (!found) {
1014 return -1;
1015 }
1016
1017 date = end;
1018 }
1019 }
1020
1021 part++;
1022 }
1023
1024 if (-1 == seconds) {
1025 seconds = minutes = hours = 0; /* no time, make it zero */
1026 }
1027
1028 if ((-1 == monthday) || (-1 == month) || (-1 == year)) {
1029 /* lacks vital info, fail */
1030 return -1;
1031 }
1032
1033 if (sizeof(time_t) < 5) {
1034 /* 32 bit time_t can only hold dates to the beginning of 2038 */
1035 if (year > 2037) {
1036 return 0x7fffffff;
1037 }
1038 }
1039
1040 tm.tm_sec = seconds;
1041 tm.tm_min = minutes;
1042 tm.tm_hour = hours;
1043 tm.tm_mday = monthday;
1044 tm.tm_mon = month;
1045 tm.tm_year = year - 1900;
1046 tm.tm_wday = 0;
1047 tm.tm_yday = 0;
1048 tm.tm_isdst = 0;
1049
1050 t = mktime(&tm);
1051
1052 /* time zone adjust */
1053 {
1054 struct tm *gmt, keeptime2;
1055 long delta;
1056 time_t t2;
1057
1058 if(!(gmt = php_gmtime_r(&t, &keeptime2))) {
1059 return -1; /* illegal date/time */
1060 }
1061
1062 t2 = mktime(gmt);
1063
1064 /* Add the time zone diff (between the given timezone and GMT) and the
1065 diff between the local time zone and GMT. */
1066 delta = (tz_offset != -1 ? tz_offset : 0) + (t - t2);
1067
1068 if((delta > 0) && (t + delta < t)) {
1069 return -1; /* time_t overflow */
1070 }
1071
1072 t += delta;
1073 }
1074
1075 return t;
1076 }
1077 /* }}} */
1078
1079 /* {{{ inline STATUS http_send_status(int) */
1080 PHP_HTTP_API inline STATUS _http_send_status(const int status TSRMLS_DC)
1081 {
1082 int s = status;
1083 return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) s TSRMLS_CC);
1084 }
1085 /* }}} */
1086
1087 /* {{{ inline STATUS http_send_header(char *) */
1088 PHP_HTTP_API inline STATUS _http_send_header(const char *header TSRMLS_DC)
1089 {
1090 return http_send_status_header(0, header);
1091 }
1092 /* }}} */
1093
1094 /* {{{ inline STATUS http_send_status_header(int, char *) */
1095 PHP_HTTP_API inline STATUS _http_send_status_header(const int status, const char *header TSRMLS_DC)
1096 {
1097 sapi_header_line h = {(char *) header, strlen(header), status};
1098 return sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC);
1099 }
1100 /* }}} */
1101
1102 /* {{{ inline zval *http_get_server_var(char *) */
1103 PHP_HTTP_API inline zval *_http_get_server_var(const char *key TSRMLS_DC)
1104 {
1105 zval **var;
1106 if (SUCCESS == zend_hash_find(
1107 HTTP_SERVER_VARS,
1108 (char *) key, strlen(key) + 1, (void **) &var)) {
1109 return *var;
1110 }
1111 return NULL;
1112 }
1113 /* }}} */
1114
1115 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
1116 PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len,
1117 char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
1118 {
1119 char etag[33] = { 0 };
1120 unsigned char digest[16];
1121
1122 if (mode & PHP_OUTPUT_HANDLER_START) {
1123 PHP_MD5Init(&HTTP_G(etag_md5));
1124 }
1125
1126 PHP_MD5Update(&HTTP_G(etag_md5), output, output_len);
1127
1128 if (mode & PHP_OUTPUT_HANDLER_END) {
1129 PHP_MD5Final(digest, &HTTP_G(etag_md5));
1130
1131 /* just do that if desired */
1132 if (HTTP_G(etag_started)) {
1133 make_digest(etag, digest);
1134
1135 if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
1136 http_send_status(304);
1137 } else {
1138 http_send_etag(etag, 32);
1139 }
1140 }
1141 }
1142
1143 *handled_output_len = output_len;
1144 *handled_output = estrndup(output, output_len);
1145 }
1146 /* }}} */
1147
1148 /* {{{ STATUS http_start_ob_handler(php_output_handler_func_t, char *, uint, zend_bool) */
1149 PHP_HTTP_API STATUS _http_start_ob_handler(php_output_handler_func_t handler_func,
1150 char *handler_name, uint chunk_size, zend_bool erase TSRMLS_DC)
1151 {
1152 php_ob_buffer **stack;
1153 int count, i;
1154
1155 if (count = OG(ob_nesting_level)) {
1156 stack = ecalloc(sizeof(php_ob_buffer *), count);
1157
1158 if (count > 1) {
1159 zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP,
1160 (int (*)(void *elem, void *)) http_ob_stack_get, stack);
1161 }
1162
1163 if (count > 0) {
1164 http_ob_stack_get(&OG(active_ob_buffer), stack);
1165 }
1166
1167 while (OG(ob_nesting_level)) {
1168 php_end_ob_buffer(0, 0 TSRMLS_CC);
1169 }
1170 }
1171
1172 php_ob_set_internal_handler(handler_func, chunk_size, handler_name, erase TSRMLS_CC);
1173
1174 for (i = 0; i < count; i++) {
1175 php_ob_buffer *s = stack[i];
1176 if (strcmp(s->handler_name, "default output handler")) {
1177 php_start_ob_buffer_named(s->handler_name, s->chunk_size, s->erase TSRMLS_CC);
1178 }
1179 php_body_write(s->buffer, s->text_length TSRMLS_CC);
1180 efree(s->handler_name);
1181 efree(s->buffer);
1182 efree(s);
1183 }
1184 if (count) {
1185 efree(stack);
1186 }
1187
1188 return SUCCESS;
1189 }
1190 /* }}} */
1191
1192 /* {{{ int http_modified_match(char *, int) */
1193 PHP_HTTP_API int _http_modified_match(const char *entry, const time_t t TSRMLS_DC)
1194 {
1195 int retval;
1196 zval *zmodified;
1197 char *modified, *chr_ptr;
1198
1199 HTTP_GSC(zmodified, entry, 0);
1200
1201 modified = estrndup(Z_STRVAL_P(zmodified), Z_STRLEN_P(zmodified));
1202 if (chr_ptr = strrchr(modified, ';')) {
1203 chr_ptr = 0;
1204 }
1205 retval = (t <= http_parse_date(modified));
1206 efree(modified);
1207 return retval;
1208 }
1209 /* }}} */
1210
1211 /* {{{ int http_etag_match(char *, char *) */
1212 PHP_HTTP_API int _http_etag_match(const char *entry, const char *etag TSRMLS_DC)
1213 {
1214 zval *zetag;
1215 char *quoted_etag;
1216 STATUS result;
1217
1218 HTTP_GSC(zetag, entry, 0);
1219
1220 if (NULL != strchr(Z_STRVAL_P(zetag), '*')) {
1221 return 1;
1222 }
1223
1224 quoted_etag = (char *) emalloc(strlen(etag) + 3);
1225 sprintf(quoted_etag, "\"%s\"", etag);
1226
1227 if (!strchr(Z_STRVAL_P(zetag), ',')) {
1228 result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
1229 } else {
1230 result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
1231 }
1232 efree(quoted_etag);
1233 return result;
1234 }
1235 /* }}} */
1236
1237 /* {{{ STATUS http_send_last_modified(int) */
1238 PHP_HTTP_API STATUS _http_send_last_modified(const time_t t TSRMLS_DC)
1239 {
1240 char modified[96] = "Last-Modified: ", *date;
1241 date = http_date(t);
1242 strcat(modified, date);
1243 efree(date);
1244
1245 /* remember */
1246 HTTP_G(lmod) = t;
1247
1248 return http_send_header(modified);
1249 }
1250 /* }}} */
1251
1252 /* {{{ static STATUS http_send_etag(char *, int) */
1253 PHP_HTTP_API STATUS _http_send_etag(const char *etag,
1254 const int etag_len TSRMLS_DC)
1255 {
1256 STATUS status;
1257 char *etag_header;
1258
1259 if (!etag_len){
1260 php_error_docref(NULL TSRMLS_CC,E_ERROR,
1261 "Attempt to send empty ETag (previous: %s)\n", HTTP_G(etag));
1262 return FAILURE;
1263 }
1264
1265 /* remember */
1266 if (HTTP_G(etag)) {
1267 efree(HTTP_G(etag));
1268 }
1269 HTTP_G(etag) = estrdup(etag);
1270
1271 etag_header = ecalloc(sizeof("ETag: \"\"") + etag_len, 1);
1272 sprintf(etag_header, "ETag: \"%s\"", etag);
1273 if (SUCCESS != (status = http_send_header(etag_header))) {
1274 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't send '%s' header", etag_header);
1275 }
1276 efree(etag_header);
1277 return status;
1278 }
1279 /* }}} */
1280
1281 /* {{{ STATUS http_send_cache_control(char *, size_t) */
1282 PHP_HTTP_API STATUS _http_send_cache_control(const char *cache_control,
1283 const size_t cc_len TSRMLS_DC)
1284 {
1285 STATUS status;
1286 char *cc_header = ecalloc(sizeof("Cache-Control: ") + cc_len, 1);
1287
1288 sprintf(cc_header, "Cache-Control: %s", cache_control);
1289 if (SUCCESS != (status = http_send_header(cc_header))) {
1290 php_error_docref(NULL TSRMLS_CC, E_NOTICE,
1291 "Could not send '%s' header", cc_header);
1292 }
1293 efree(cc_header);
1294 return status;
1295 }
1296 /* }}} */
1297
1298 /* {{{ STATUS http_send_content_type(char *, size_t) */
1299 PHP_HTTP_API STATUS _http_send_content_type(const char *content_type,
1300 const size_t ct_len TSRMLS_DC)
1301 {
1302 STATUS status;
1303 char *ct_header;
1304
1305 if (!strchr(content_type, '/')) {
1306 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1307 "Content-Type '%s' doesn't seem to consist of a primary and a secondary part",
1308 content_type);
1309 return FAILURE;
1310 }
1311
1312 /* remember for multiple ranges */
1313 if (HTTP_G(ctype)) {
1314 efree(HTTP_G(ctype));
1315 }
1316 HTTP_G(ctype) = estrndup(content_type, ct_len);
1317
1318 ct_header = ecalloc(sizeof("Content-Type: ") + ct_len, 1);
1319 sprintf(ct_header, "Content-Type: %s", content_type);
1320
1321 if (SUCCESS != (status = http_send_header(ct_header))) {
1322 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1323 "Couldn't send '%s' header", ct_header);
1324 }
1325 efree(ct_header);
1326 return status;
1327 }
1328 /* }}} */
1329
1330 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
1331 PHP_HTTP_API STATUS _http_send_content_disposition(const char *filename,
1332 const size_t f_len, const zend_bool send_inline TSRMLS_DC)
1333 {
1334 STATUS status;
1335 char *cd_header;
1336
1337 if (send_inline) {
1338 cd_header = ecalloc(sizeof("Content-Disposition: inline; filename=\"\"") + f_len, 1);
1339 sprintf(cd_header, "Content-Disposition: inline; filename=\"%s\"", filename);
1340 } else {
1341 cd_header = ecalloc(sizeof("Content-Disposition: attachment; filename=\"\"") + f_len, 1);
1342 sprintf(cd_header, "Content-Disposition: attachment; filename=\"%s\"", filename);
1343 }
1344
1345 if (SUCCESS != (status = http_send_header(cd_header))) {
1346 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't send '%s' header", cd_header);
1347 }
1348 efree(cd_header);
1349 return status;
1350 }
1351 /* }}} */
1352
1353 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
1354 PHP_HTTP_API STATUS _http_cache_last_modified(const time_t last_modified,
1355 const time_t send_modified, const char *cache_control, const size_t cc_len TSRMLS_DC)
1356 {
1357 if (cc_len) {
1358 http_send_cache_control(cache_control, cc_len);
1359 }
1360
1361 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified)) {
1362 if (SUCCESS == http_send_status(304)) {
1363 zend_bailout();
1364 } else {
1365 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
1366 return FAILURE;
1367 }
1368 }
1369 return http_send_last_modified(send_modified);
1370 }
1371 /* }}} */
1372
1373 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
1374 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, const size_t etag_len,
1375 const char *cache_control, const size_t cc_len TSRMLS_DC)
1376 {
1377 if (cc_len) {
1378 http_send_cache_control(cache_control, cc_len);
1379 }
1380
1381 if (etag_len) {
1382 http_send_etag(etag, etag_len);
1383 if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
1384 if (SUCCESS == http_send_status(304)) {
1385 zend_bailout();
1386 } else {
1387 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
1388 return FAILURE;
1389 }
1390 }
1391 }
1392
1393 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
1394 if (!HTTP_G(etag_started)) {
1395 if (SUCCESS == http_start_ob_handler(_http_ob_etaghandler, "ob_etaghandler", 4096, 1)) {
1396 HTTP_G(etag_started) = 1;
1397 return SUCCESS;
1398 } else {
1399 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not start ob_etaghandler");
1400 return FAILURE;
1401 }
1402 }
1403 return SUCCESS;
1404 }
1405 /* }}} */
1406
1407 /* {{{ char *http_absolute_uri(char *, char *) */
1408 PHP_HTTP_API char *_http_absolute_uri(const char *url,
1409 const char *proto TSRMLS_DC)
1410 {
1411 char URI[HTTP_URI_MAXLEN + 1], *PTR, *proto_ptr, *host, *path;
1412 zval *zhost;
1413
1414 if (!url || !strlen(url)) {
1415 if (!SG(request_info).request_uri) {
1416 return NULL;
1417 }
1418 url = SG(request_info).request_uri;
1419 }
1420 /* Mess around with already absolute URIs */
1421 else if (proto_ptr = strstr(url, "://")) {
1422 if (!proto || !strncmp(url, proto, strlen(proto))) {
1423 return estrdup(url);
1424 } else {
1425 snprintf(URI, HTTP_URI_MAXLEN, "%s%s", proto, proto_ptr + 3);
1426 return estrdup(URI);
1427 }
1428 }
1429
1430 /* protocol defaults to http */
1431 if (!proto || !strlen(proto)) {
1432 proto = "http";
1433 }
1434
1435 /* get host name */
1436 if ( (zhost = http_get_server_var("HTTP_HOST")) ||
1437 (zhost = http_get_server_var("SERVER_NAME"))) {
1438 host = Z_STRVAL_P(zhost);
1439 } else {
1440 host = "localhost";
1441 }
1442
1443
1444 /* glue together */
1445 if (url[0] == '/') {
1446 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s", proto, host, url);
1447 } else if (SG(request_info).request_uri) {
1448 path = estrdup(SG(request_info).request_uri);
1449 php_dirname(path, strlen(path));
1450 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s/%s", proto, host, path, url);
1451 efree(path);
1452 } else {
1453 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s/%s", proto, host, url);
1454 }
1455
1456 /* strip everything after a new line */
1457 PTR = URI;
1458 while (*PTR != 0) {
1459 if (*PTR == '\n' || *PTR == '\r') {
1460 *PTR = 0;
1461 break;
1462 }
1463 PTR++;
1464 }
1465
1466 return estrdup(URI);
1467 }
1468 /* }}} */
1469
1470 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
1471 PHP_HTTP_API char *_http_negotiate_q(const char *entry, const zval *supported,
1472 const char *def TSRMLS_DC)
1473 {
1474 zval *zaccept, *zarray, *zdelim, **zentry, *zentries, **zsupp;
1475 char *q_ptr, *result;
1476 int i, c;
1477 double qual;
1478
1479 HTTP_GSC(zaccept, entry, estrdup(def));
1480
1481 MAKE_STD_ZVAL(zarray);
1482 array_init(zarray);
1483
1484 MAKE_STD_ZVAL(zdelim);
1485 ZVAL_STRING(zdelim, ",", 0);
1486 php_explode(zdelim, zaccept, zarray, -1);
1487 efree(zdelim);
1488
1489 MAKE_STD_ZVAL(zentries);
1490 array_init(zentries);
1491
1492 c = zend_hash_num_elements(Z_ARRVAL_P(zarray));
1493 for (i = 0; i < c; i++, zend_hash_move_forward(Z_ARRVAL_P(zarray))) {
1494
1495 if (SUCCESS != zend_hash_get_current_data(
1496 Z_ARRVAL_P(zarray), (void **) &zentry)) {
1497 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1498 "Cannot parse %s header: %s", entry, Z_STRVAL_P(zaccept));
1499 break;
1500 }
1501
1502 /* check for qualifier */
1503 if (NULL != (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';'))) {
1504 qual = strtod(q_ptr + 3, NULL);
1505 } else {
1506 qual = 1000.0 - i;
1507 }
1508
1509 /* walk through the supported array */
1510 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported));
1511 SUCCESS == zend_hash_get_current_data(
1512 Z_ARRVAL_P(supported), (void **) &zsupp);
1513 zend_hash_move_forward(Z_ARRVAL_P(supported))) {
1514 if (!strcasecmp(Z_STRVAL_PP(zsupp), Z_STRVAL_PP(zentry))) {
1515 add_assoc_double(zentries, Z_STRVAL_PP(zsupp), qual);
1516 break;
1517 }
1518 }
1519 }
1520
1521 zval_dtor(zarray);
1522 efree(zarray);
1523
1524 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries));
1525
1526 if ( (SUCCESS != zend_hash_sort(Z_ARRVAL_P(zentries), zend_qsort,
1527 http_sort_q, 0 TSRMLS_CC)) ||
1528 (HASH_KEY_NON_EXISTANT == zend_hash_get_current_key(
1529 Z_ARRVAL_P(zentries), &result, 0, 1))) {
1530 result = estrdup(def);
1531 }
1532
1533 zval_dtor(zentries);
1534 efree(zentries);
1535
1536 return result;
1537 }
1538 /* }}} */
1539
1540 /* {{{ http_range_status http_get_request_ranges(zval *zranges, size_t) */
1541 PHP_HTTP_API http_range_status _http_get_request_ranges(zval *zranges,
1542 const size_t length TSRMLS_DC)
1543 {
1544 zval *zrange;
1545 char *range, c;
1546 long begin = -1, end = -1, *ptr;
1547
1548 HTTP_GSC(zrange, "HTTP_RANGE", RANGE_NO);
1549 range = Z_STRVAL_P(zrange);
1550
1551 if (strncmp(range, "bytes=", strlen("bytes="))) {
1552 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Range header misses bytes=");
1553 return RANGE_NO;
1554 }
1555
1556 ptr = &begin;
1557 range += strlen("bytes=");
1558
1559 do {
1560 switch (c = *(range++))
1561 {
1562 case '0':
1563 *ptr *= 10;
1564 break;
1565
1566 case '1': case '2': case '3':
1567 case '4': case '5': case '6':
1568 case '7': case '8': case '9':
1569 /*
1570 * If the value of the pointer is already set (non-negative)
1571 * then multiply its value by ten and add the current value,
1572 * else initialise the pointers value with the current value
1573 * --
1574 * This let us recognize empty fields when validating the
1575 * ranges, i.e. a "-10" for begin and "12345" for the end
1576 * was the following range request: "Range: bytes=0-12345";
1577 * While a "-1" for begin and "12345" for the end would
1578 * have been: "Range: bytes=-12345".
1579 */
1580 if (*ptr > 0) {
1581 *ptr *= 10;
1582 *ptr += c - '0';
1583 } else {
1584 *ptr = c - '0';
1585 }
1586 break;
1587
1588 case '-':
1589 ptr = &end;
1590 break;
1591
1592 case ' ':
1593 /* IE - ignore for now */
1594 break;
1595
1596 case 0:
1597 case ',':
1598
1599 if (length) {
1600 /* validate ranges */
1601 switch (begin)
1602 {
1603 /* "0-12345" */
1604 case -10:
1605 if ((length - end) < 1) {
1606 return RANGE_ERR;
1607 }
1608 begin = 0;
1609 break;
1610
1611 /* "-12345" */
1612 case -1:
1613 if ((length - end) < 1) {
1614 return RANGE_ERR;
1615 }
1616 begin = length - end;
1617 end = length;
1618 break;
1619
1620 /* "12345-(xxx)" */
1621 default:
1622 switch (end)
1623 {
1624 /* "12345-" */
1625 case -1:
1626 if ((length - begin) < 1) {
1627 return RANGE_ERR;
1628 }
1629 end = length - 1;
1630 break;
1631
1632 /* "12345-67890" */
1633 default:
1634 if ( ((length - begin) < 1) ||
1635 ((length - end) < 1) ||
1636 ((begin - end) >= 0)) {
1637 return RANGE_ERR;
1638 }
1639 break;
1640 }
1641 break;
1642 }
1643 }
1644 {
1645 zval *zentry;
1646 MAKE_STD_ZVAL(zentry);
1647 array_init(zentry);
1648 add_index_long(zentry, 0, begin);
1649 add_index_long(zentry, 1, end);
1650 add_next_index_zval(zranges, zentry);
1651
1652 begin = -1;
1653 end = -1;
1654 ptr = &begin;
1655 }
1656 break;
1657
1658 default:
1659 return RANGE_NO;
1660 break;
1661 }
1662 } while (c != 0);
1663
1664 return RANGE_OK;
1665 }
1666 /* }}} */
1667
1668 /* {{{ STATUS http_send_ranges(zval *, void *, size_t, http_send_mode) */
1669 PHP_HTTP_API STATUS _http_send_ranges(zval *zranges, const void *data, const size_t size, const http_send_mode mode TSRMLS_DC)
1670 {
1671 int c;
1672 long **begin, **end;
1673 zval **zrange;
1674
1675 /* Send HTTP 206 Partial Content */
1676 http_send_status(206);
1677
1678 /* single range */
1679 if ((c = zend_hash_num_elements(Z_ARRVAL_P(zranges))) == 1) {
1680 char range_header[256] = {0};
1681
1682 zend_hash_index_find(Z_ARRVAL_P(zranges), 0, (void **) &zrange);
1683 zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &begin);
1684 zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &end);
1685
1686 /* send content range header */
1687 snprintf(range_header, 255, "Content-Range: bytes %d-%d/%d", **begin, **end, size);
1688 http_send_header(range_header);
1689
1690 /* send requested chunk */
1691 return http_send_chunk(data, **begin, **end + 1, mode);
1692 }
1693
1694 /* multi range */
1695 else {
1696 int i;
1697 char bound[23] = {0}, preface[1024] = {0},
1698 multi_header[68] = "Content-Type: multipart/byteranges; boundary=";
1699
1700 snprintf(bound, 22, "--%d%0.9f", time(NULL), php_combined_lcg(TSRMLS_C));
1701 strncat(multi_header, bound + 2, 21);
1702 http_send_header(multi_header);
1703
1704 /* send each requested chunk */
1705 for ( i = 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(zranges));
1706 i < c;
1707 i++, zend_hash_move_forward(Z_ARRVAL_P(zranges))) {
1708 if ( HASH_KEY_NON_EXISTANT == zend_hash_get_current_data(
1709 Z_ARRVAL_P(zranges), (void **) &zrange) ||
1710 SUCCESS != zend_hash_index_find(
1711 Z_ARRVAL_PP(zrange), 0, (void **) &begin) ||
1712 SUCCESS != zend_hash_index_find(
1713 Z_ARRVAL_PP(zrange), 1, (void **) &end)) {
1714 break;
1715 }
1716
1717 snprintf(preface, 1023,
1718 HTTP_CRLF "%s"
1719 HTTP_CRLF "Content-Type: %s"
1720 HTTP_CRLF "Content-Range: bytes %ld-%ld/%ld"
1721 HTTP_CRLF
1722 HTTP_CRLF,
1723
1724 bound,
1725 HTTP_G(ctype) ? HTTP_G(ctype) : "application/x-octetstream",
1726 **begin,
1727 **end,
1728 size
1729 );
1730
1731 php_body_write(preface, strlen(preface) TSRMLS_CC);
1732 http_send_chunk(data, **begin, **end + 1, mode);
1733 }
1734
1735 /* write boundary once more */
1736 php_body_write(HTTP_CRLF, 2 TSRMLS_CC);
1737 php_body_write(bound, strlen(bound) TSRMLS_CC);
1738
1739 return SUCCESS;
1740 }
1741 }
1742 /* }}} */
1743
1744 /* {{{ STATUS http_send(void *, sizezo_t, http_send_mode) */
1745 PHP_HTTP_API STATUS _http_send(const void *data_ptr, const size_t data_size,
1746 const http_send_mode data_mode TSRMLS_DC)
1747 {
1748 int is_range_request = http_is_range_request();
1749
1750 if (!data_ptr) {
1751 return FAILURE;
1752 }
1753
1754 /* etag handling */
1755 if (HTTP_G(etag_started)) {
1756 char *etag;
1757 /* interrupt */
1758 HTTP_G(etag_started) = 0;
1759 /* never ever use the output to compute the ETag if http_send() is used */
1760 php_end_ob_buffer(0, 0 TSRMLS_CC);
1761 if (!(etag = http_etag(data_ptr, data_size, data_mode))) {
1762 return FAILURE;
1763 }
1764
1765 /* send 304 Not Modified if etag matches */
1766 if ((!is_range_request) && http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
1767 efree(etag);
1768 return http_send_status(304);
1769 }
1770
1771 http_send_etag(etag, 32);
1772 efree(etag);
1773 }
1774
1775 /* send 304 Not Modified if last-modified matches*/
1776 if ((!is_range_request) && http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod))) {
1777 return http_send_status(304);
1778 }
1779
1780 if (is_range_request) {
1781
1782 /* only send ranges if entity hasn't changed */
1783 if (
1784 ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_MATCH", 13)) ||
1785 http_etag_match("HTTP_IF_MATCH", HTTP_G(etag)))
1786 &&
1787 ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
1788 http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod)))
1789 ) {
1790
1791 STATUS result = FAILURE;
1792 zval *zranges = NULL;
1793 MAKE_STD_ZVAL(zranges);
1794 array_init(zranges);
1795
1796 switch (http_get_request_ranges(zranges, data_size))
1797 {
1798 case RANGE_NO:
1799 zval_dtor(zranges);
1800 efree(zranges);
1801 /* go ahead and send all */
1802 break;
1803
1804 case RANGE_OK:
1805 result = http_send_ranges(zranges, data_ptr, data_size, data_mode);
1806 zval_dtor(zranges);
1807 efree(zranges);
1808 return result;
1809 break;
1810
1811 case RANGE_ERR:
1812 zval_dtor(zranges);
1813 efree(zranges);
1814 http_send_status(416);
1815 return FAILURE;
1816 break;
1817
1818 default:
1819 return FAILURE;
1820 break;
1821 }
1822 }
1823 }
1824 /* send all */
1825 return http_send_chunk(data_ptr, 0, data_size, data_mode);
1826 }
1827 /* }}} */
1828
1829 /* {{{ STATUS http_send_data(zval *) */
1830 PHP_HTTP_API STATUS _http_send_data(const zval *zdata TSRMLS_DC)
1831 {
1832 if (!Z_STRLEN_P(zdata)) {
1833 return SUCCESS;
1834 }
1835 if (!Z_STRVAL_P(zdata)) {
1836 return FAILURE;
1837 }
1838
1839 return http_send(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata), SEND_DATA);
1840 }
1841 /* }}} */
1842
1843 /* {{{ STATUS http_send_stream(php_stream *) */
1844 PHP_HTTP_API STATUS _http_send_stream(const php_stream *file TSRMLS_DC)
1845 {
1846 if (php_stream_stat((php_stream *) file, &HTTP_G(ssb))) {
1847 return FAILURE;
1848 }
1849
1850 return http_send(file, HTTP_G(ssb).sb.st_size, SEND_RSRC);
1851 }
1852 /* }}} */
1853
1854 /* {{{ STATUS http_send_file(zval *) */
1855 PHP_HTTP_API STATUS _http_send_file(const zval *zfile TSRMLS_DC)
1856 {
1857 php_stream *file;
1858 STATUS ret;
1859
1860 if (!Z_STRLEN_P(zfile)) {
1861 return FAILURE;
1862 }
1863
1864 if (!(file = php_stream_open_wrapper(Z_STRVAL_P(zfile), "rb",
1865 REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
1866 return FAILURE;
1867 }
1868
1869 ret = http_send_stream(file);
1870 php_stream_close(file);
1871 return ret;
1872 }
1873 /* }}} */
1874
1875 /* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1876 PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded,
1877 const size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
1878 {
1879 const char *e_ptr;
1880 char *d_ptr;
1881
1882 *decoded_len = 0;
1883 *decoded = (char *) ecalloc(encoded_len, 1);
1884 d_ptr = *decoded;
1885 e_ptr = encoded;
1886
1887 while (((e_ptr - encoded) - encoded_len) > 0) {
1888 char hex_len[9] = {0};
1889 size_t chunk_len = 0;
1890 int i = 0;
1891
1892 /* read in chunk size */
1893 while (isxdigit(*e_ptr)) {
1894 if (i == 9) {
1895 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1896 "Chunk size is too long: 0x%s...", hex_len);
1897 efree(*decoded);
1898 return FAILURE;
1899 }
1900 hex_len[i++] = *e_ptr++;
1901 }
1902
1903 /* reached the end */
1904 if (!strcmp(hex_len, "0")) {
1905 break;
1906 }
1907
1908 /* new line */
1909 if (strncmp(e_ptr, HTTP_CRLF, 2)) {
1910 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1911 "Invalid character (expected 0x0D 0x0A; got: %x %x)",
1912 *e_ptr, *(e_ptr + 1));
1913 efree(*decoded);
1914 return FAILURE;
1915 }
1916
1917 /* hex to long */
1918 {
1919 char *error = NULL;
1920 chunk_len = strtol(hex_len, &error, 16);
1921 if (error == hex_len) {
1922 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1923 "Invalid chunk size string: '%s'", hex_len);
1924 efree(*decoded);
1925 return FAILURE;
1926 }
1927 }
1928
1929 memcpy(d_ptr, e_ptr += 2, chunk_len);
1930 d_ptr += chunk_len;
1931 e_ptr += chunk_len + 2;
1932 *decoded_len += chunk_len;
1933 }
1934
1935 return SUCCESS;
1936 }
1937 /* }}} */
1938
1939 /* {{{ proto STATUS http_split_response(zval *, zval *, zval *) */
1940 PHP_HTTP_API STATUS _http_split_response(const zval *zresponse, zval *zheaders,
1941 zval *zbody TSRMLS_DC)
1942 {
1943 char *header, *response, *body = NULL;
1944 long response_len = Z_STRLEN_P(zresponse);
1945 header = response = Z_STRVAL_P(zresponse);
1946
1947 while ((response - Z_STRVAL_P(zresponse) + 3) < response_len) {
1948 if ( (*response++ == '\r') &&
1949 (*response++ == '\n') &&
1950 (*response++ == '\r') &&
1951 (*response++ == '\n')) {
1952 body = response;
1953 break;
1954 }
1955 }
1956
1957 if (body && (response_len - (body - header))) {
1958 ZVAL_STRINGL(zbody, body, response_len - (body - header) - 1, 1);
1959 } else {
1960 Z_TYPE_P(zbody) = IS_NULL;
1961 }
1962
1963 return http_parse_headers(header, body - Z_STRVAL_P(zresponse), zheaders);
1964 }
1965 /* }}} */
1966
1967 /* {{{ STATUS http_parse_headers(char *, long, zval *) */
1968 PHP_HTTP_API STATUS _http_parse_headers(char *header, int header_len, zval *array TSRMLS_DC)
1969 {
1970 char *colon = NULL, *line = NULL, *begin = header;
1971
1972 if (header_len < 2) {
1973 return FAILURE;
1974 }
1975
1976 /* status code */
1977 if (!strncmp(header, "HTTP/1.", 7)) {
1978 char *end = strstr(header, HTTP_CRLF);
1979 add_assoc_stringl(array, "Status",
1980 header + strlen("HTTP/1.x "),
1981 end - (header + strlen("HTTP/1.x ")), 1);
1982 header = end + 2;
1983 }
1984
1985 line = header;
1986
1987 while (header_len >= (line - begin)) {
1988 int value_len = 0;
1989
1990 switch (*line++)
1991 {
1992 case 0:
1993 --value_len; /* we don't have CR so value length is one char less */
1994 case '\n':
1995 if (colon && ((!(*line - 1)) || ((*line != ' ') && (*line != '\t')))) {
1996
1997 /* skip empty key */
1998 if (header != colon) {
1999 char *key = estrndup(header, colon - header);
2000 value_len += line - colon - 1;
2001
2002 /* skip leading ws */
2003 while (isspace(*(++colon))) --value_len;
2004 /* skip trailing ws */
2005 while (isspace(colon[value_len - 1])) --value_len;
2006
2007 if (value_len < 1) {
2008 /* hm, empty header? */
2009 add_assoc_stringl(array, key, "", 0, 1);
2010 } else {
2011 add_assoc_stringl(array, key, colon, value_len, 1);
2012 }
2013 efree(key);
2014 }
2015
2016 colon = NULL;
2017 value_len = 0;
2018 header += line - header;
2019 }
2020 break;
2021
2022 case ':':
2023 if (!colon) {
2024 colon = line - 1;
2025 }
2026 break;
2027 }
2028 }
2029 return SUCCESS;
2030 }
2031 /* }}} */
2032
2033 /* {{{ void http_get_request_headers(zval *) */
2034 PHP_HTTP_API void _http_get_request_headers(zval *array TSRMLS_DC)
2035 {
2036 char *key;
2037
2038 for ( zend_hash_internal_pointer_reset(HTTP_SERVER_VARS);
2039 zend_hash_get_current_key(HTTP_SERVER_VARS, &key, NULL, 0) != HASH_KEY_NON_EXISTANT;
2040 zend_hash_move_forward(HTTP_SERVER_VARS)) {
2041 if (!strncmp(key, "HTTP_", 5)) {
2042 zval **header;
2043 zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header);
2044 add_assoc_stringl(array, pretty_key(key + 5, strlen(key) - 5, 1, 1), Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
2045 }
2046 }
2047 }
2048 /* }}} */
2049
2050 /* {{{ HAVE_CURL */
2051 #ifdef HTTP_HAVE_CURL
2052
2053 /* {{{ STATUS http_get(char *, HashTable *, HashTable *, char **, size_t *) */
2054 PHP_HTTP_API STATUS _http_get(const char *URL, HashTable *options,
2055 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2056 {
2057 STATUS rs;
2058 CURL *ch = curl_easy_init();
2059
2060 if (!ch) {
2061 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
2062 return FAILURE;
2063 }
2064
2065 rs = http_get_ex(ch, URL, options, info, data, data_len);
2066 curl_easy_cleanup(ch);
2067 return rs;
2068 }
2069 /* }}} */
2070
2071 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, char **, size_t *) */
2072 PHP_HTTP_API STATUS _http_get_ex(CURL *ch, const char *URL, HashTable *options,
2073 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2074 {
2075 http_curl_initbuf(CURLBUF_EVRY);
2076 http_curl_setopts(ch, URL, options);
2077 curl_easy_setopt(ch, CURLOPT_NOBODY, 0);
2078 curl_easy_setopt(ch, CURLOPT_POST, 0);
2079
2080 if (CURLE_OK != curl_easy_perform(ch)) {
2081 http_curl_freebuf(CURLBUF_EVRY);
2082 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
2083 return FAILURE;
2084 }
2085 if (info) {
2086 http_curl_getinfo(ch, info);
2087 }
2088 http_curl_movebuf(CURLBUF_EVRY, data, data_len);
2089 return SUCCESS;
2090 }
2091
2092 /* {{{ STATUS http_head(char *, HashTable *, HashTable *, char **data, size_t *) */
2093 PHP_HTTP_API STATUS _http_head(const char *URL, HashTable *options,
2094 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2095 {
2096 STATUS rs;
2097 CURL *ch = curl_easy_init();
2098
2099 if (!ch) {
2100 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
2101 return FAILURE;
2102 }
2103
2104 rs = http_head_ex(ch, URL, options, info, data, data_len);
2105 curl_easy_cleanup(ch);
2106 return rs;
2107 }
2108 /* }}} */
2109
2110 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, char **data, size_t *) */
2111 PHP_HTTP_API STATUS _http_head_ex(CURL *ch, const char *URL, HashTable *options,
2112 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2113 {
2114 http_curl_initbuf(CURLBUF_HDRS);
2115 http_curl_setopts(ch, URL, options);
2116 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
2117 curl_easy_setopt(ch, CURLOPT_POST, 0);
2118
2119 if (CURLE_OK != curl_easy_perform(ch)) {
2120 http_curl_freebuf(CURLBUF_HDRS);
2121 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
2122 return FAILURE;
2123 }
2124 if (info) {
2125 http_curl_getinfo(ch, info);
2126 }
2127 http_curl_movebuf(CURLBUF_HDRS, data, data_len);
2128 return SUCCESS;
2129 }
2130
2131 /* {{{ STATUS http_post_data(char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
2132 PHP_HTTP_API STATUS _http_post_data(const char *URL, char *postdata,
2133 size_t postdata_len, HashTable *options, HashTable *info, char **data,
2134 size_t *data_len TSRMLS_DC)
2135 {
2136 STATUS rs;
2137 CURL *ch = curl_easy_init();
2138
2139 if (!ch) {
2140 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
2141 return FAILURE;
2142 }
2143 rs = http_post_data_ex(ch, URL, postdata, postdata_len, options, info, data, data_len);
2144 curl_easy_cleanup(ch);
2145 return rs;
2146 }
2147 /* }}} */
2148
2149 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
2150 PHP_HTTP_API STATUS _http_post_data_ex(CURL *ch, const char *URL, char *postdata,
2151 size_t postdata_len, HashTable *options, HashTable *info, char **data,
2152 size_t *data_len TSRMLS_DC)
2153 {
2154 http_curl_initbuf(CURLBUF_EVRY);
2155 http_curl_setopts(ch, URL, options);
2156 curl_easy_setopt(ch, CURLOPT_POST, 1);
2157 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, postdata);
2158 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, postdata_len);
2159
2160 if (CURLE_OK != curl_easy_perform(ch)) {
2161 http_curl_freebuf(CURLBUF_EVRY);
2162 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
2163 return FAILURE;
2164 }
2165 if (info) {
2166 http_curl_getinfo(ch, info);
2167 }
2168 http_curl_movebuf(CURLBUF_EVRY, data, data_len);
2169 return SUCCESS;
2170 }
2171 /* }}} */
2172
2173 /* {{{ STATUS http_post_array(char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
2174 PHP_HTTP_API STATUS _http_post_array(const char *URL, HashTable *postarray,
2175 HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC)
2176 {
2177 smart_str qstr = {0};
2178 STATUS status;
2179
2180 if (php_url_encode_hash_ex(postarray, &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
2181 if (qstr.c) {
2182 efree(qstr.c);
2183 }
2184 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode post data");
2185 return FAILURE;
2186 }
2187 smart_str_0(&qstr);
2188
2189 status = http_post_data(URL, qstr.c, qstr.len, options, info, data, data_len);
2190 if (qstr.c) {
2191 efree(qstr.c);
2192 }
2193 return status;
2194 }
2195 /* }}} */
2196
2197 #endif
2198 /* }}} HAVE_CURL */
2199
2200 /* {{{ STATUS http_auth_header(char *, char*) */
2201 PHP_HTTP_API STATUS _http_auth_header(const char *type, const char *realm TSRMLS_DC)
2202 {
2203 char realm_header[1024] = {0};
2204 snprintf(realm_header, 1023, "WWW-Authenticate: %s realm=\"%s\"", type, realm);
2205 return http_send_status_header(401, realm_header);
2206 }
2207 /* }}} */
2208
2209 /* {{{ STATUS http_auth_credentials(char **, char **) */
2210 PHP_HTTP_API STATUS _http_auth_credentials(char **user, char **pass TSRMLS_DC)
2211 {
2212 if (strncmp(sapi_module.name, "isapi", 5)) {
2213 zval *zuser, *zpass;
2214
2215 HTTP_GSC(zuser, "PHP_AUTH_USER", FAILURE);
2216 HTTP_GSC(zpass, "PHP_AUTH_PW", FAILURE);
2217
2218 *user = estrndup(Z_STRVAL_P(zuser), Z_STRLEN_P(zuser));
2219 *pass = estrndup(Z_STRVAL_P(zpass), Z_STRLEN_P(zpass));
2220
2221 return SUCCESS;
2222 } else {
2223 zval *zauth = NULL;
2224 HTTP_GSC(zauth, "HTTP_AUTHORIZATION", FAILURE);
2225 {
2226 char *decoded, *colon;
2227 int decoded_len;
2228 decoded = php_base64_decode(Z_STRVAL_P(zauth), Z_STRLEN_P(zauth),
2229 &decoded_len);
2230
2231 if (colon = strchr(decoded + 6, ':')) {
2232 *user = estrndup(decoded + 6, colon - decoded - 6);
2233 *pass = estrndup(colon + 1, decoded + decoded_len - colon - 6 - 1);
2234
2235 return SUCCESS;
2236 } else {
2237 return FAILURE;
2238 }
2239 }
2240 }
2241 }
2242 /* }}} */
2243
2244 /* }}} public API */
2245
2246 /*
2247 * Local variables:
2248 * tab-width: 4
2249 * c-basic-offset: 4
2250 * End:
2251 * vim600: noet sw=4 ts=4 fdm=marker
2252 * vim<600: noet sw=4 ts=4
2253 */