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