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