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