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