- missing include
[m6w6/ext-http] / http_request_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 #include "php.h"
22
23 #include "php_http.h"
24 #include "php_http_std_defs.h"
25 #include "php_http_api.h"
26 #include "php_http_request_api.h"
27 #include "php_http_url_api.h"
28 #ifdef ZEND_ENGINE_2
29 # include "php_http_request_object.h"
30 #endif
31
32 #include "phpstr/phpstr.h"
33
34 #ifdef PHP_WIN32
35 # include <winsock2.h>
36 #endif
37
38 #include <curl/curl.h>
39
40 ZEND_EXTERN_MODULE_GLOBALS(http);
41
42 #if LIBCURL_VERSION_NUM < 0x070c00
43 # define curl_easy_strerror(code) HTTP_G(request).error
44 #endif
45
46 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
47 #define HTTP_CURL_INFO_EX(I, X) \
48 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
49 { \
50 case CURLINFO_STRING: \
51 { \
52 char *c; \
53 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
54 add_assoc_string(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
55 } \
56 } \
57 break; \
58 \
59 case CURLINFO_DOUBLE: \
60 { \
61 double d; \
62 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
63 add_assoc_double(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), d); \
64 } \
65 } \
66 break; \
67 \
68 case CURLINFO_LONG: \
69 { \
70 long l; \
71 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
72 add_assoc_long(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), l); \
73 } \
74 } \
75 break; \
76 }
77
78 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
79 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
80 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
81 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
82 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
83 if (!strcasecmp(key, #keyname)) { \
84 convert_to_string_ex(param); \
85 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
86 key = NULL; \
87 continue; \
88 }
89 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
90 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
91 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
92 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
93 if (!strcasecmp(key, #keyname)) { \
94 convert_to_long_ex(param); \
95 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
96 key = NULL; \
97 continue; \
98 }
99
100 static const char *const http_request_methods[HTTP_MAX_REQUEST_METHOD + 1];
101 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
102 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
103 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
104 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
105 static int http_curl_progress_callback(void *, double, double, double, double);
106 static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *);
107 static int http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) { return n*l; }
108
109 #define HTTP_CURL_CALLBACK_DATA(from, type, var) \
110 http_curl_callback_ctx *__CTX = (http_curl_callback_ctx *) (from); \
111 TSRMLS_FETCH_FROM_CTX(__CTX->tsrm_ctx); \
112 type (var) = (type) (__CTX->data)
113
114 #define http_curl_callback_data(data) _http_curl_callback_data((data) TSRMLS_CC)
115 static http_curl_callback_ctx *_http_curl_callback_data(void *data TSRMLS_DC);
116
117
118 /* {{{ void *http_request_data_copy(int, void *) */
119 void *_http_request_data_copy(int type, void *data TSRMLS_DC)
120 {
121 switch (type)
122 {
123 case COPY_STRING:
124 {
125 char *new_str = estrdup(data);
126 zend_llist_add_element(&HTTP_G(request).copies.strings, &new_str);
127 return new_str;
128 }
129
130 case COPY_SLIST:
131 {
132 zend_llist_add_element(&HTTP_G(request).copies.slists, &data);
133 return data;
134 }
135
136 case COPY_CONTEXT:
137 {
138 zend_llist_add_element(&HTTP_G(request).copies.contexts, &data);
139 return data;
140 }
141
142 case COPY_CONV:
143 {
144 zend_llist_add_element(&HTTP_G(request).copies.convs, &data);
145 return data;
146 }
147
148 default:
149 {
150 return data;
151 }
152 }
153 }
154 /* }}} */
155
156 /* {{{ void http_request_data_free_string(char **) */
157 void _http_request_data_free_string(void *string)
158 {
159 efree(*((char **)string));
160 }
161 /* }}} */
162
163 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
164 void _http_request_data_free_slist(void *list)
165 {
166 curl_slist_free_all(*((struct curl_slist **) list));
167 }
168 /* }}} */
169
170 /* {{{ _http_request_data_free_context(http_curl_callback_ctx **) */
171 void _http_request_data_free_context(void *context)
172 {
173 efree(*((http_curl_callback_ctx **) context));
174 }
175 /* }}} */
176
177 /* {{{ _http_request_data_free_conv(http_curl_conv **) */
178 void _http_request_data_free_conv(void *conv)
179 {
180 efree(*((http_curl_conv **) conv));
181 }
182 /* }}} */
183
184 /* {{{ http_request_body *http_request_body_new() */
185 PHP_HTTP_API http_request_body *_http_request_body_new(TSRMLS_D)
186 {
187 http_request_body *body = ecalloc(1, sizeof(http_request_body));
188 return body;
189 }
190 /* }}} */
191
192 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
193 PHP_HTTP_API STATUS _http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files TSRMLS_DC)
194 {
195 if (files && (zend_hash_num_elements(files) > 0)) {
196 char *key = NULL;
197 ulong idx;
198 zval **data;
199 struct curl_httppost *http_post_data[2] = {NULL, NULL};
200
201 /* normal data */
202 FOREACH_HASH_KEYVAL(fields, key, idx, data) {
203 CURLcode err;
204 if (key) {
205 convert_to_string_ex(data);
206 err = curl_formadd(&http_post_data[0], &http_post_data[1],
207 CURLFORM_COPYNAME, key,
208 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
209 CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data),
210 CURLFORM_END
211 );
212 if (CURLE_OK != err) {
213 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post fields: %s", curl_easy_strerror(err));
214 curl_formfree(http_post_data[0]);
215 return FAILURE;
216 }
217
218 /* reset */
219 key = NULL;
220 }
221 }
222
223 /* file data */
224 FOREACH_HASH_VAL(files, data) {
225 CURLcode err;
226 zval **file, **type, **name;
227 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) &&
228 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) &&
229 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
230 err = curl_formadd(&http_post_data[0], &http_post_data[1],
231 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
232 CURLFORM_FILE, Z_STRVAL_PP(file),
233 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
234 CURLFORM_END
235 );
236 if (CURLE_OK != err) {
237 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post files: %s", curl_easy_strerror(err));
238 curl_formfree(http_post_data[0]);
239 return FAILURE;
240 }
241 } else {
242 http_error(HE_NOTICE, HTTP_E_INVALID_PARAM, "Post file array entry misses either 'name', 'type' or 'file' entry");
243 }
244 }
245
246 body->type = HTTP_REQUEST_BODY_CURLPOST;
247 body->data = http_post_data[0];
248 body->size = 0;
249
250 } else {
251 char *encoded;
252 size_t encoded_len;
253
254 if (SUCCESS != http_urlencode_hash_ex(fields, 1, NULL, 0, &encoded, &encoded_len)) {
255 http_error(HE_WARNING, HTTP_E_ENCODING, "Could not encode post data");
256 return FAILURE;
257 }
258
259 body->type = HTTP_REQUEST_BODY_CSTRING;
260 body->data = encoded;
261 body->size = encoded_len;
262 }
263
264 return SUCCESS;
265 }
266 /* }}} */
267
268 /* {{{ void http_request_body_dtor(http_request_body *) */
269 PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC)
270 {
271 if (body) {
272 switch (body->type)
273 {
274 case HTTP_REQUEST_BODY_CSTRING:
275 if (body->data) {
276 efree(body->data);
277 }
278 break;
279
280 case HTTP_REQUEST_BODY_CURLPOST:
281 curl_formfree(body->data);
282 break;
283
284 case HTTP_REQUEST_BODY_UPLOADFILE:
285 php_stream_close(body->data);
286 break;
287 }
288 }
289 }
290 /* }}} */
291
292 /* {{{ void http_request_body_free(http_request_body *) */
293 PHP_HTTP_API void _http_request_body_free(http_request_body *body TSRMLS_DC)
294 {
295 if (body) {
296 http_request_body_dtor(body);
297 efree(body);
298 }
299 }
300 /* }}} */
301
302 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *) */
303 PHP_HTTP_API STATUS _http_request_init(CURL *ch, http_request_method meth, char *url, http_request_body *body, HashTable *options TSRMLS_DC)
304 {
305 zval *zoption;
306 zend_bool range_req = 0;
307
308 /* reset CURL handle */
309 #if LIBCURL_VERSION_NUM >= 0x070c01
310 curl_easy_reset(ch);
311 #endif
312
313 /* set options */
314 if (url) {
315 HTTP_CURL_OPT(URL, http_request_data_copy(COPY_STRING, url));
316 }
317
318 HTTP_CURL_OPT(HEADER, 0);
319 HTTP_CURL_OPT(FILETIME, 1);
320 HTTP_CURL_OPT(AUTOREFERER, 1);
321 HTTP_CURL_OPT(READFUNCTION, http_curl_read_callback);
322 /* we'll get all data through the debug function */
323 HTTP_CURL_OPT(WRITEFUNCTION, http_curl_dummy_callback);
324 HTTP_CURL_OPT(HEADERFUNCTION, NULL);
325
326 HTTP_CURL_OPT(VERBOSE, 1);
327 HTTP_CURL_OPT(DEBUGFUNCTION, http_curl_raw_callback);
328
329 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
330 HTTP_CURL_OPT(NOSIGNAL, 1);
331 #endif
332 #if LIBCURL_VERSION_NUM < 0x070c00
333 HTTP_CURL_OPT(ERRORBUFFER, HTTP_G(request).error);
334 #endif
335
336 /* progress callback */
337 if (zoption = http_curl_getopt(options, "onprogress", 0)) {
338 HTTP_CURL_OPT(NOPROGRESS, 0);
339 HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
340 HTTP_CURL_OPT(PROGRESSDATA, http_curl_callback_data(zoption));
341 } else {
342 HTTP_CURL_OPT(NOPROGRESS, 1);
343 }
344
345 /* proxy */
346 if (zoption = http_curl_getopt(options, "proxyhost", IS_STRING)) {
347 HTTP_CURL_OPT(PROXY, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
348 /* port */
349 if (zoption = http_curl_getopt(options, "proxyport", IS_LONG)) {
350 HTTP_CURL_OPT(PROXYPORT, Z_LVAL_P(zoption));
351 }
352 /* user:pass */
353 if (zoption = http_curl_getopt(options, "proxyauth", IS_STRING)) {
354 HTTP_CURL_OPT(PROXYUSERPWD, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
355 }
356 #if LIBCURL_VERSION_NUM >= 0x070a07
357 /* auth method */
358 if (zoption = http_curl_getopt(options, "proxyauthtype", IS_LONG)) {
359 HTTP_CURL_OPT(PROXYAUTH, Z_LVAL_P(zoption));
360 }
361 #endif
362 }
363
364 /* outgoing interface */
365 if (zoption = http_curl_getopt(options, "interface", IS_STRING)) {
366 HTTP_CURL_OPT(INTERFACE, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
367 }
368
369 /* another port */
370 if (zoption = http_curl_getopt(options, "port", IS_LONG)) {
371 HTTP_CURL_OPT(PORT, Z_LVAL_P(zoption));
372 }
373
374 /* auth */
375 if (zoption = http_curl_getopt(options, "httpauth", IS_STRING)) {
376 HTTP_CURL_OPT(USERPWD, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
377 }
378 #if LIBCURL_VERSION_NUM >= 0x070a06
379 if (zoption = http_curl_getopt(options, "httpauthtype", IS_LONG)) {
380 HTTP_CURL_OPT(HTTPAUTH, Z_LVAL_P(zoption));
381 }
382 #endif
383
384 /* compress, empty string enables deflate and gzip */
385 if ((zoption = http_curl_getopt(options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) {
386 HTTP_CURL_OPT(ENCODING, "");
387 } else {
388 HTTP_CURL_OPT(ENCODING, 0);
389 }
390
391 /* redirects, defaults to 0 */
392 if (zoption = http_curl_getopt(options, "redirect", IS_LONG)) {
393 HTTP_CURL_OPT(FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
394 HTTP_CURL_OPT(MAXREDIRS, Z_LVAL_P(zoption));
395 if (zoption = http_curl_getopt(options, "unrestrictedauth", IS_BOOL)) {
396 HTTP_CURL_OPT(UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
397 }
398 } else {
399 HTTP_CURL_OPT(FOLLOWLOCATION, 0);
400 }
401
402 /* referer */
403 if (zoption = http_curl_getopt(options, "referer", IS_STRING)) {
404 HTTP_CURL_OPT(REFERER, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
405 } else {
406 HTTP_CURL_OPT(REFERER, NULL);
407 }
408
409 /* useragent, default "PECL::HTTP/version (PHP/version)" */
410 if (zoption = http_curl_getopt(options, "useragent", IS_STRING)) {
411 HTTP_CURL_OPT(USERAGENT, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
412 } else {
413 HTTP_CURL_OPT(USERAGENT, "PECL::HTTP/" HTTP_PEXT_VERSION " (PHP/" PHP_VERSION ")");
414 }
415
416 /* additional headers, array('name' => 'value') */
417 if (zoption = http_curl_getopt(options, "headers", IS_ARRAY)) {
418 char *header_key;
419 ulong header_idx;
420 struct curl_slist *headers = NULL;
421
422 FOREACH_KEY(zoption, header_key, header_idx) {
423 if (header_key) {
424 zval **header_val;
425 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val)) {
426 char header[1024] = {0};
427 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
428 headers = curl_slist_append(headers, http_request_data_copy(COPY_STRING, header));
429 }
430
431 /* reset */
432 header_key = NULL;
433 }
434 }
435
436 if (headers) {
437 HTTP_CURL_OPT(HTTPHEADER, http_request_data_copy(COPY_SLIST, headers));
438 }
439 } else {
440 HTTP_CURL_OPT(HTTPHEADER, NULL);
441 }
442
443 /* cookies, array('name' => 'value') */
444 if (zoption = http_curl_getopt(options, "cookies", IS_ARRAY)) {
445 char *cookie_key = NULL;
446 ulong cookie_idx = 0;
447 phpstr *qstr = phpstr_new();
448
449 FOREACH_KEY(zoption, cookie_key, cookie_idx) {
450 if (cookie_key) {
451 zval **cookie_val;
452 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val)) {
453 phpstr_appendf(qstr, "%s=%s; ", cookie_key, Z_STRVAL_PP(cookie_val));
454 }
455
456 /* reset */
457 cookie_key = NULL;
458 }
459 }
460
461 if (qstr->used) {
462 phpstr_fix(qstr);
463 HTTP_CURL_OPT(COOKIE, http_request_data_copy(COPY_STRING, qstr->data));
464 }
465 phpstr_free(qstr);
466 } else {
467 HTTP_CURL_OPT(COOKIE, NULL);
468 }
469
470 /* session cookies */
471 if (zoption = http_curl_getopt(options, "cookiesession", IS_BOOL)) {
472 if (Z_LVAL_P(zoption)) {
473 /* accept cookies for this session */
474 HTTP_CURL_OPT(COOKIEFILE, "");
475 } else {
476 /* reset session cookies */
477 HTTP_CURL_OPT(COOKIESESSION, 1);
478 }
479 } else {
480 HTTP_CURL_OPT(COOKIEFILE, NULL);
481 }
482
483 /* cookiestore, read initial cookies from that file and store cookies back into that file */
484 if ((zoption = http_curl_getopt(options, "cookiestore", IS_STRING)) && Z_STRLEN_P(zoption)) {
485 HTTP_CURL_OPT(COOKIEFILE, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
486 HTTP_CURL_OPT(COOKIEJAR, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
487 } else {
488 HTTP_CURL_OPT(COOKIEFILE, NULL);
489 HTTP_CURL_OPT(COOKIEJAR, NULL);
490 }
491
492 /* resume */
493 if (zoption = http_curl_getopt(options, "resume", IS_LONG)) {
494 range_req = 1;
495 HTTP_CURL_OPT(RESUME_FROM, Z_LVAL_P(zoption));
496 } else {
497 HTTP_CURL_OPT(RESUME_FROM, 0);
498 }
499
500 /* maxfilesize */
501 if (zoption = http_curl_getopt(options, "maxfilesize", IS_LONG)) {
502 HTTP_CURL_OPT(MAXFILESIZE, Z_LVAL_P(zoption));
503 } else {
504 HTTP_CURL_OPT(MAXFILESIZE, 0);
505 }
506
507 /* lastmodified */
508 if (zoption = http_curl_getopt(options, "lastmodified", IS_LONG)) {
509 HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
510 HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption));
511 } else {
512 HTTP_CURL_OPT(TIMEVALUE, 0);
513 }
514
515 /* timeout, defaults to 3 */
516 if (zoption = http_curl_getopt(options, "timeout", IS_LONG)) {
517 HTTP_CURL_OPT(TIMEOUT, Z_LVAL_P(zoption));
518 } else {
519 HTTP_CURL_OPT(TIMEOUT, 3);
520 }
521
522 /* connecttimeout, defaults to 3 */
523 if (zoption = http_curl_getopt(options, "connecttimeout", IS_LONG)) {
524 HTTP_CURL_OPT(CONNECTTIMEOUT, Z_LVAL_P(zoption));
525 } else {
526 HTTP_CURL_OPT(CONNECTTIMEOUT, 3);
527 }
528
529 /* ssl */
530 if (zoption = http_curl_getopt(options, "ssl", IS_ARRAY)) {
531 ulong idx;
532 char *key = NULL;
533 zval **param;
534
535 FOREACH_KEYVAL(zoption, key, idx, param) {
536 if (key) {
537 HTTP_CURL_OPT_SSL_STRING(CERT);
538 #if LIBCURL_VERSION_NUM >= 0x070903
539 HTTP_CURL_OPT_SSL_STRING(CERTTYPE);
540 #endif
541 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD);
542
543 HTTP_CURL_OPT_SSL_STRING(KEY);
544 HTTP_CURL_OPT_SSL_STRING(KEYTYPE);
545 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD);
546
547 HTTP_CURL_OPT_SSL_STRING(ENGINE);
548 HTTP_CURL_OPT_SSL_LONG(VERSION);
549
550 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER);
551 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST);
552 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST);
553
554
555 HTTP_CURL_OPT_STRING(CAINFO);
556 #if LIBCURL_VERSION_NUM >= 0x070908
557 HTTP_CURL_OPT_STRING(CAPATH);
558 #endif
559 HTTP_CURL_OPT_STRING(RANDOM_FILE);
560 HTTP_CURL_OPT_STRING(EGDSOCKET);
561
562 /* reset key */
563 key = NULL;
564 }
565 }
566 } else {
567 /* disable SSL verification by default */
568 HTTP_CURL_OPT(SSL_VERIFYPEER, 0);
569 HTTP_CURL_OPT(SSL_VERIFYHOST, 0);
570 }
571
572 /* request method */
573 switch (meth)
574 {
575 case HTTP_GET:
576 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1);
577 break;
578
579 case HTTP_HEAD:
580 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
581 break;
582
583 case HTTP_POST:
584 curl_easy_setopt(ch, CURLOPT_POST, 1);
585 break;
586
587 case HTTP_PUT:
588 curl_easy_setopt(ch, CURLOPT_UPLOAD, 1);
589 break;
590
591 default:
592 if (http_request_method_exists(0, meth, NULL)) {
593 curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, http_request_method_name(meth));
594 } else {
595 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unsupported request method: %d", meth);
596 return FAILURE;
597 }
598 break;
599 }
600
601 /* attach request body */
602 if (body && (meth != HTTP_GET) && (meth != HTTP_HEAD)) {
603 switch (body->type)
604 {
605 case HTTP_REQUEST_BODY_CSTRING:
606 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, body->data);
607 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, body->size);
608 break;
609
610 case HTTP_REQUEST_BODY_CURLPOST:
611 curl_easy_setopt(ch, CURLOPT_HTTPPOST, (struct curl_httppost *) body->data);
612 break;
613
614 case HTTP_REQUEST_BODY_UPLOADFILE:
615 curl_easy_setopt(ch, CURLOPT_READDATA, http_curl_callback_data(body));
616 curl_easy_setopt(ch, CURLOPT_INFILESIZE, body->size);
617 break;
618
619 default:
620 /* shouldn't ever happen */
621 http_error_ex(HE_ERROR, 0, "Unknown request body type: %d", body->type);
622 return FAILURE;
623 break;
624 }
625 }
626
627 return SUCCESS;
628 }
629 /* }}} */
630
631 /* {{{ void http_request_conv(CURL *, phpstr *, phpstr *) */
632 void _http_request_conv(CURL *ch, phpstr* response, phpstr *request TSRMLS_DC)
633 {
634 http_curl_conv *conv = emalloc(sizeof(http_curl_conv));
635 conv->response = response;
636 conv->request = request;
637 conv->last_info = -1;
638 HTTP_CURL_OPT(DEBUGDATA, http_curl_callback_data(http_request_data_copy(COPY_CONV, conv)));
639 }
640 /* }}} */
641
642 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
643 PHP_HTTP_API STATUS _http_request_exec(CURL *ch, HashTable *info, phpstr *response, phpstr *request TSRMLS_DC)
644 {
645 CURLcode result;
646
647 http_request_conv(ch, response, request);
648
649 /* perform request */
650 if (CURLE_OK != (result = curl_easy_perform(ch))) {
651 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "Could not perform request: %s", curl_easy_strerror(result));
652 return FAILURE;
653 } else {
654 /* get curl info */
655 if (info) {
656 http_request_info(ch, info);
657 }
658 return SUCCESS;
659 }
660 }
661 /* }}} */
662
663 /* {{{ void http_request_info(CURL *, HashTable *) */
664 PHP_HTTP_API void _http_request_info(CURL *ch, HashTable *info TSRMLS_DC)
665 {
666 zval array;
667 Z_ARRVAL(array) = info;
668
669 HTTP_CURL_INFO(EFFECTIVE_URL);
670 #if LIBCURL_VERSION_NUM >= 0x070a07
671 HTTP_CURL_INFO(RESPONSE_CODE);
672 #else
673 HTTP_CURL_INFO_EX(HTTP_CODE, RESPONSE_CODE);
674 #endif
675 HTTP_CURL_INFO(HTTP_CONNECTCODE);
676 #if LIBCURL_VERSION_NUM >= 0x070500
677 HTTP_CURL_INFO(FILETIME);
678 #endif
679 HTTP_CURL_INFO(TOTAL_TIME);
680 HTTP_CURL_INFO(NAMELOOKUP_TIME);
681 HTTP_CURL_INFO(CONNECT_TIME);
682 HTTP_CURL_INFO(PRETRANSFER_TIME);
683 HTTP_CURL_INFO(STARTTRANSFER_TIME);
684 #if LIBCURL_VERSION_NUM >= 0x070907
685 HTTP_CURL_INFO(REDIRECT_TIME);
686 HTTP_CURL_INFO(REDIRECT_COUNT);
687 #endif
688 HTTP_CURL_INFO(SIZE_UPLOAD);
689 HTTP_CURL_INFO(SIZE_DOWNLOAD);
690 HTTP_CURL_INFO(SPEED_DOWNLOAD);
691 HTTP_CURL_INFO(SPEED_UPLOAD);
692 HTTP_CURL_INFO(HEADER_SIZE);
693 HTTP_CURL_INFO(REQUEST_SIZE);
694 HTTP_CURL_INFO(SSL_VERIFYRESULT);
695 #if LIBCURL_VERSION_NUM >= 0x070c03
696 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
697 #endif
698 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD);
699 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD);
700 HTTP_CURL_INFO(CONTENT_TYPE);
701 #if LIBCURL_VERSION_NUM >= 0x070a03
702 /*HTTP_CURL_INFO(PRIVATE);*/
703 #endif
704 #if LIBCURL_VERSION_NUM >= 0x070a08
705 HTTP_CURL_INFO(HTTPAUTH_AVAIL);
706 HTTP_CURL_INFO(PROXYAUTH_AVAIL);
707 #endif
708 #if LIBCURL_VERSION_NUM >= 0x070c02
709 /*HTTP_CURL_INFO(OS_ERRNO);*/
710 #endif
711 #if LIBCURL_VERSION_NUM >= 0x070c03
712 HTTP_CURL_INFO(NUM_CONNECTS);
713 #endif
714 }
715 /* }}} */
716
717 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
718 PHP_HTTP_API STATUS _http_request_ex(CURL *ch, http_request_method meth, char *url, http_request_body *body, HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
719 {
720 STATUS status;
721 zend_bool clean_curl;
722
723 if ((clean_curl = (!ch))) {
724 if (!(ch = curl_easy_init())) {
725 http_error(HE_WARNING, HTTP_E_REQUEST, "Could not initialize curl.");
726 return FAILURE;
727 }
728 }
729
730 status = ((SUCCESS == http_request_init(ch, meth, url, body, options)) &&
731 (SUCCESS == http_request_exec(ch, info, response, NULL))) ? SUCCESS : FAILURE;
732
733 if (clean_curl) {
734 curl_easy_cleanup(ch);
735 }
736 return status;
737 }
738 /* }}} */
739
740 /* {{{ char *http_request_method_name(http_request_method) */
741 PHP_HTTP_API const char *_http_request_method_name(http_request_method m TSRMLS_DC)
742 {
743 zval **meth;
744
745 if (HTTP_STD_REQUEST_METHOD(m)) {
746 return http_request_methods[m];
747 }
748
749 if (SUCCESS == zend_hash_index_find(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(m), (void **) &meth)) {
750 return Z_STRVAL_PP(meth);
751 }
752
753 return http_request_methods[0];
754 }
755 /* }}} */
756
757 /* {{{ unsigned long http_request_method_exists(zend_bool, unsigned long, char *) */
758 PHP_HTTP_API unsigned long _http_request_method_exists(zend_bool by_name, unsigned long id, const char *name TSRMLS_DC)
759 {
760 if (by_name) {
761 unsigned i;
762
763 for (i = HTTP_NO_REQUEST_METHOD + 1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
764 if (!strcmp(name, http_request_methods[i])) {
765 return i;
766 }
767 }
768 {
769 zval **data;
770 char *key;
771 ulong idx;
772
773 FOREACH_HASH_KEYVAL(&HTTP_G(request).methods.custom, key, idx, data) {
774 if (!strcmp(name, Z_STRVAL_PP(data))) {
775 return idx + HTTP_MAX_REQUEST_METHOD;
776 }
777 }
778 }
779 return 0;
780 } else {
781 return HTTP_STD_REQUEST_METHOD(id) || zend_hash_index_exists(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(id)) ? id : 0;
782 }
783 }
784 /* }}} */
785
786 /* {{{ unsigned long http_request_method_register(char *) */
787 PHP_HTTP_API unsigned long _http_request_method_register(const char *method TSRMLS_DC)
788 {
789 zval array;
790 char *http_method;
791 unsigned long meth_num = HTTP_G(request).methods.custom.nNextFreeElement + HTTP_MAX_REQUEST_METHOD;
792
793 Z_ARRVAL(array) = &HTTP_G(request).methods.custom;
794 add_next_index_string(&array, estrdup(method), 0);
795
796 spprintf(&http_method, 0, "HTTP_%s", method);
797 zend_register_long_constant(http_method, strlen(http_method) + 1, meth_num, CONST_CS, http_module_number TSRMLS_CC);
798 efree(http_method);
799
800 return meth_num;
801 }
802 /* }}} */
803
804 /* {{{ STATUS http_request_method_unregister(usngigned long) */
805 PHP_HTTP_API STATUS _http_request_method_unregister(unsigned long method TSRMLS_DC)
806 {
807 zval **zmethod;
808 char *http_method;
809
810 if (SUCCESS != zend_hash_index_find(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(method), (void **) &zmethod)) {
811 http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Request method with id %lu does not exist", method);
812 return FAILURE;
813 }
814
815 spprintf(&http_method, 0, "HTTP_%s", Z_STRVAL_PP(zmethod));
816
817 if ( (SUCCESS != zend_hash_index_del(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(method)))
818 || (SUCCESS != zend_hash_del(EG(zend_constants), http_method, strlen(http_method) + 1))) {
819 http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Could not unregister request method: %s", http_method);
820 efree(http_method);
821 return FAILURE;
822 }
823
824 efree(http_method);
825 return SUCCESS;
826 }
827 /* }}} */
828
829
830 /* {{{ char *http_request_methods[] */
831 static const char *const http_request_methods[] = {
832 "UNKOWN",
833 /* HTTP/1.1 */
834 "GET",
835 "HEAD",
836 "POST",
837 "PUT",
838 "DELETE",
839 "OPTIONS",
840 "TRACE",
841 "CONNECT",
842 /* WebDAV - RFC 2518 */
843 "PROPFIND",
844 "PROPPATCH",
845 "MKCOL",
846 "COPY",
847 "MOVE",
848 "LOCK",
849 "UNLOCK",
850 /* WebDAV Versioning - RFC 3253 */
851 "VERSION-CONTROL",
852 "REPORT",
853 "CHECKOUT",
854 "CHECKIN",
855 "UNCHECKOUT",
856 "MKWORKSPACE",
857 "UPDATE",
858 "LABEL",
859 "MERGE",
860 "BASELINE-CONTROL",
861 "MKACTIVITY",
862 /* WebDAV Access Control - RFC 3744 */
863 "ACL",
864 NULL
865 };
866 /* }}} */
867
868 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
869 static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *s)
870 {
871 HTTP_CURL_CALLBACK_DATA(s, http_request_body *, body);
872
873 if (body->type != HTTP_REQUEST_BODY_UPLOADFILE) {
874 return 0;
875 }
876 return php_stream_read((php_stream *) body->data, data, len * n);
877 }
878 /* }}} */
879
880 /* {{{ http_curl_callback_ctx http_curl_callback_data(void *) */
881 static http_curl_callback_ctx *_http_curl_callback_data(void *data TSRMLS_DC)
882 {
883 http_curl_callback_ctx *ctx = emalloc(sizeof(http_curl_callback_ctx));
884 TSRMLS_SET_CTX(ctx->tsrm_ctx);
885 ctx->data = data;
886 return http_request_data_copy(COPY_CONTEXT, ctx);
887 }
888 /* }}} */
889
890 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
891 static int http_curl_progress_callback(void *data, double dltotal, double dlnow, double ultotal, double ulnow)
892 {
893 zval *params_pass[4], params_local[4], retval;
894 HTTP_CURL_CALLBACK_DATA(data, zval *, func);
895
896 params_pass[0] = &params_local[0];
897 params_pass[1] = &params_local[1];
898 params_pass[2] = &params_local[2];
899 params_pass[3] = &params_local[3];
900
901 INIT_PZVAL(params_pass[0]);
902 INIT_PZVAL(params_pass[1]);
903 INIT_PZVAL(params_pass[2]);
904 INIT_PZVAL(params_pass[3]);
905 ZVAL_DOUBLE(params_pass[0], dltotal);
906 ZVAL_DOUBLE(params_pass[1], dlnow);
907 ZVAL_DOUBLE(params_pass[2], ultotal);
908 ZVAL_DOUBLE(params_pass[3], ulnow);
909
910 return call_user_function(EG(function_table), NULL, func, &retval, 4, params_pass TSRMLS_CC);
911 }
912 /* }}} */
913
914 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
915 static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
916 {
917 HTTP_CURL_CALLBACK_DATA(ctx, http_curl_conv *, conv);
918
919 switch (type)
920 {
921 case CURLINFO_DATA_IN:
922 if (conv->response && conv->last_info == CURLINFO_HEADER_IN) {
923 phpstr_appends(conv->response, HTTP_CRLF);
924 }
925 case CURLINFO_HEADER_IN:
926 if (conv->response) {
927 phpstr_append(conv->response, data, length);
928 }
929 break;
930 case CURLINFO_DATA_OUT:
931 if (conv->request && conv->last_info == CURLINFO_HEADER_OUT) {
932 phpstr_appends(conv->request, HTTP_CRLF);
933 }
934 case CURLINFO_HEADER_OUT:
935 if (conv->request) {
936 phpstr_append(conv->request, data, length);
937 }
938 break;
939 }
940
941 if (type) {
942 conv->last_info = type;
943 }
944 return 0;
945 }
946 /* }}} */
947
948 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
949 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
950 {
951 zval **zoption;
952
953 if (!options || (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))) {
954 return NULL;
955 }
956
957 if (Z_TYPE_PP(zoption) != type) {
958 switch (type)
959 {
960 case IS_BOOL: convert_to_boolean_ex(zoption); break;
961 case IS_LONG: convert_to_long_ex(zoption); break;
962 case IS_DOUBLE: convert_to_double_ex(zoption); break;
963 case IS_STRING: convert_to_string_ex(zoption); break;
964 case IS_ARRAY: convert_to_array_ex(zoption); break;
965 case IS_OBJECT: convert_to_object_ex(zoption); break;
966 default:
967 break;
968 }
969 }
970
971 return *zoption;
972 }
973 /* }}} */
974
975 /*
976 * Local variables:
977 * tab-width: 4
978 * c-basic-offset: 4
979 * End:
980 * vim600: noet sw=4 ts=4 fdm=marker
981 * vim<600: noet sw=4 ts=4
982 */
983