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