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