- no user debug callbacks any longer
[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 #ifndef HTTP_CURL_USE_ZEND_MM
43 # define HTTP_CURL_USE_ZEND_MM 0
44 #endif
45
46 #if LIBCURL_VERSION_NUM < 0x070c00
47 # define curl_easy_strerror(code) HTTP_G(request).error
48 #endif
49
50 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
51 #define HTTP_CURL_INFO_EX(I, X) \
52 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
53 { \
54 case CURLINFO_STRING: \
55 { \
56 char *c; \
57 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
58 add_assoc_string(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
59 } \
60 } \
61 break; \
62 \
63 case CURLINFO_DOUBLE: \
64 { \
65 double d; \
66 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
67 add_assoc_double(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), d); \
68 } \
69 } \
70 break; \
71 \
72 case CURLINFO_LONG: \
73 { \
74 long l; \
75 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
76 add_assoc_long(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), l); \
77 } \
78 } \
79 break; \
80 }
81
82 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
83 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
84 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
85 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
86 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
87 if (!strcasecmp(key, #keyname)) { \
88 convert_to_string_ex(param); \
89 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
90 key = NULL; \
91 continue; \
92 }
93 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
94 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
95 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
96 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
97 if (!strcasecmp(key, #keyname)) { \
98 convert_to_long_ex(param); \
99 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
100 key = NULL; \
101 continue; \
102 }
103
104 typedef struct _http_curl_conv {
105 phpstr *response;
106 phpstr *request;
107 curl_infotype last_info;
108 } http_curl_conv;
109
110 static const char *const http_request_methods[HTTP_MAX_REQUEST_METHOD + 1];
111 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
112 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
113 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
114 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
115 static int http_curl_progress_callback(void *, double, double, double, double);
116 static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *);
117 static int http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) { return n*l; }
118
119 #define HTTP_CURL_CALLBACK_DATA(from, type, var) \
120 http_curl_callback_ctx *__CTX = (http_curl_callback_ctx *) (from); \
121 TSRMLS_FETCH_FROM_CTX(__CTX->tsrm_ctx); \
122 type (var) = (type) (__CTX->data)
123
124 #define http_curl_callback_data(data) _http_curl_callback_data((data) TSRMLS_CC)
125 static http_curl_callback_ctx *_http_curl_callback_data(void *data TSRMLS_DC);
126
127
128 #if HTTP_CURL_USE_ZEND_MM
129 static void http_curl_free(void *p) { efree(p); }
130 static char *http_curl_strdup(const char *p) { return estrdup(p); }
131 static void *http_curl_malloc(size_t s) { return emalloc(s); }
132 static void *http_curl_realloc(void *p, size_t s) { return erealloc(p, s); }
133 static void *http_curl_calloc(size_t n, size_t s) { return ecalloc(n, s); }
134 #endif
135
136 /* {{{ STATUS http_request_global_init() */
137 STATUS _http_request_global_init(void)
138 {
139 #if HTTP_CURL_USE_ZEND_MM
140 if (CURLE_OK != curl_global_init_mem(CURL_GLOBAL_ALL,
141 http_curl_malloc,
142 http_curl_free,
143 http_curl_realloc,
144 http_curl_strdup,
145 http_curl_calloc)) {
146 return FAILURE;
147 }
148 #else
149 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
150 return FAILURE;
151 }
152 #endif
153 return SUCCESS;
154 }
155 /* }}} */
156
157 /* {{{ void *http_request_data_copy(int, void *) */
158 void *_http_request_data_copy(int type, void *data TSRMLS_DC)
159 {
160 switch (type)
161 {
162 case COPY_STRING:
163 {
164 char *new_str = estrdup(data);
165 zend_llist_add_element(&HTTP_G(request).copies.strings, &new_str);
166 return new_str;
167 }
168
169 case COPY_SLIST:
170 {
171 zend_llist_add_element(&HTTP_G(request).copies.slists, &data);
172 return data;
173 }
174
175 case COPY_CONTEXT:
176 {
177 zend_llist_add_element(&HTTP_G(request).copies.contexts, &data);
178 return data;
179 }
180
181 default:
182 {
183 return data;
184 }
185 }
186 }
187 /* }}} */
188
189 /* {{{ void http_request_data_free_string(char **) */
190 void _http_request_data_free_string(void *string)
191 {
192 efree(*((char **)string));
193 }
194 /* }}} */
195
196 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
197 void _http_request_data_free_slist(void *list)
198 {
199 curl_slist_free_all(*((struct curl_slist **) list));
200 }
201 /* }}} */
202
203 /* {{{ _http_request_data_free_context(http_curl_callback_ctx **) */
204 void _http_request_data_free_context(void *context)
205 {
206 efree(*((http_curl_callback_ctx **) context));
207 }
208 /* }}} */
209
210 /* {{{ http_request_body *http_request_body_new() */
211 PHP_HTTP_API http_request_body *_http_request_body_new(TSRMLS_D)
212 {
213 http_request_body *body = ecalloc(1, sizeof(http_request_body));
214 return body;
215 }
216 /* }}} */
217
218 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
219 PHP_HTTP_API STATUS _http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files TSRMLS_DC)
220 {
221 if (files && (zend_hash_num_elements(files) > 0)) {
222 char *key = NULL;
223 ulong idx;
224 zval **data;
225 struct curl_httppost *http_post_data[2] = {NULL, NULL};
226
227 /* normal data */
228 FOREACH_HASH_KEYVAL(fields, key, idx, data) {
229 CURLcode err;
230 if (key) {
231 convert_to_string_ex(data);
232 err = curl_formadd(&http_post_data[0], &http_post_data[1],
233 CURLFORM_COPYNAME, key,
234 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
235 CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data),
236 CURLFORM_END
237 );
238 if (CURLE_OK != err) {
239 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not encode post fields: %s", curl_easy_strerror(err));
240 curl_formfree(http_post_data[0]);
241 return FAILURE;
242 }
243
244 /* reset */
245 key = NULL;
246 }
247 }
248
249 /* file data */
250 FOREACH_HASH_VAL(files, data) {
251 CURLcode err;
252 zval **file, **type, **name;
253 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) &&
254 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) &&
255 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
256 err = curl_formadd(&http_post_data[0], &http_post_data[1],
257 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
258 CURLFORM_FILE, Z_STRVAL_PP(file),
259 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
260 CURLFORM_END
261 );
262 if (CURLE_OK != err) {
263 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not encode post files: %s", curl_easy_strerror(err));
264 curl_formfree(http_post_data[0]);
265 return FAILURE;
266 }
267 } else {
268 http_error(E_NOTICE, HTTP_E_PARAM, "Post file array entry misses either 'name', 'type' or 'file' entry");
269 }
270 }
271
272 body->type = HTTP_REQUEST_BODY_CURLPOST;
273 body->data = http_post_data[0];
274 body->size = 0;
275
276 } else {
277 char *encoded;
278 size_t encoded_len;
279
280 if (SUCCESS != http_urlencode_hash_ex(fields, 1, NULL, 0, &encoded, &encoded_len)) {
281 http_error(E_WARNING, HTTP_E_ENCODE, "Could not encode post data");
282 return FAILURE;
283 }
284
285 body->type = HTTP_REQUEST_BODY_CSTRING;
286 body->data = encoded;
287 body->size = encoded_len;
288 }
289
290 return SUCCESS;
291 }
292 /* }}} */
293
294 /* {{{ void http_request_body_dtor(http_request_body *) */
295 PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC)
296 {
297 if (body) {
298 switch (body->type)
299 {
300 case HTTP_REQUEST_BODY_CSTRING:
301 if (body->data) {
302 efree(body->data);
303 }
304 break;
305
306 case HTTP_REQUEST_BODY_CURLPOST:
307 curl_formfree(body->data);
308 break;
309
310 case HTTP_REQUEST_BODY_UPLOADFILE:
311 php_stream_close(body->data);
312 break;
313 }
314 }
315 }
316 /* }}} */
317
318 /* {{{ void http_request_body_free(http_request_body *) */
319 PHP_HTTP_API void _http_request_body_free(http_request_body *body TSRMLS_DC)
320 {
321 if (body) {
322 http_request_body_dtor(body);
323 efree(body);
324 }
325 }
326 /* }}} */
327
328 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *) */
329 PHP_HTTP_API STATUS _http_request_init(CURL *ch, http_request_method meth, char *url, http_request_body *body, HashTable *options TSRMLS_DC)
330 {
331 zval *zoption;
332 zend_bool range_req = 0;
333
334 /* reset CURL handle */
335 #if LIBCURL_VERSION_NUM >= 0x070c01
336 curl_easy_reset(ch);
337 #endif
338
339 /* set options */
340 if (url) {
341 HTTP_CURL_OPT(URL, http_request_data_copy(COPY_STRING, url));
342 }
343
344 HTTP_CURL_OPT(HEADER, 0);
345 HTTP_CURL_OPT(FILETIME, 1);
346 HTTP_CURL_OPT(AUTOREFERER, 1);
347 HTTP_CURL_OPT(READFUNCTION, http_curl_read_callback);
348 /* we'll get all data through the debug function */
349 HTTP_CURL_OPT(WRITEFUNCTION, http_curl_dummy_callback);
350 HTTP_CURL_OPT(HEADERFUNCTION, NULL);
351
352 HTTP_CURL_OPT(VERBOSE, 1);
353 HTTP_CURL_OPT(DEBUGFUNCTION, http_curl_raw_callback);
354
355 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
356 HTTP_CURL_OPT(NOSIGNAL, 1);
357 #endif
358 #if LIBCURL_VERSION_NUM < 0x070c00
359 HTTP_CURL_OPT(ERRORBUFFER, HTTP_G(request).error);
360 #endif
361
362 /* progress callback */
363 if (zoption = http_curl_getopt(options, "onprogress", 0)) {
364 HTTP_CURL_OPT(NOPROGRESS, 0);
365 HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
366 HTTP_CURL_OPT(PROGRESSDATA, http_curl_callback_data(zoption));
367 } else {
368 HTTP_CURL_OPT(NOPROGRESS, 1);
369 }
370
371 /* proxy */
372 if (zoption = http_curl_getopt(options, "proxyhost", IS_STRING)) {
373 HTTP_CURL_OPT(PROXY, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
374 /* port */
375 if (zoption = http_curl_getopt(options, "proxyport", IS_LONG)) {
376 HTTP_CURL_OPT(PROXYPORT, Z_LVAL_P(zoption));
377 }
378 /* user:pass */
379 if (zoption = http_curl_getopt(options, "proxyauth", IS_STRING)) {
380 HTTP_CURL_OPT(PROXYUSERPWD, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
381 }
382 #if LIBCURL_VERSION_NUM >= 0x070a07
383 /* auth method */
384 if (zoption = http_curl_getopt(options, "proxyauthtype", IS_LONG)) {
385 HTTP_CURL_OPT(PROXYAUTH, Z_LVAL_P(zoption));
386 }
387 #endif
388 }
389
390 /* outgoing interface */
391 if (zoption = http_curl_getopt(options, "interface", IS_STRING)) {
392 HTTP_CURL_OPT(INTERFACE, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
393 }
394
395 /* another port */
396 if (zoption = http_curl_getopt(options, "port", IS_LONG)) {
397 HTTP_CURL_OPT(PORT, Z_LVAL_P(zoption));
398 }
399
400 /* auth */
401 if (zoption = http_curl_getopt(options, "httpauth", IS_STRING)) {
402 HTTP_CURL_OPT(USERPWD, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
403 }
404 #if LIBCURL_VERSION_NUM >= 0x070a06
405 if (zoption = http_curl_getopt(options, "httpauthtype", IS_LONG)) {
406 HTTP_CURL_OPT(HTTPAUTH, Z_LVAL_P(zoption));
407 }
408 #endif
409
410 /* compress, empty string enables deflate and gzip */
411 if ((zoption = http_curl_getopt(options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) {
412 HTTP_CURL_OPT(ENCODING, "");
413 } else {
414 HTTP_CURL_OPT(ENCODING, 0);
415 }
416
417 /* redirects, defaults to 0 */
418 if (zoption = http_curl_getopt(options, "redirect", IS_LONG)) {
419 HTTP_CURL_OPT(FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
420 HTTP_CURL_OPT(MAXREDIRS, Z_LVAL_P(zoption));
421 if (zoption = http_curl_getopt(options, "unrestrictedauth", IS_BOOL)) {
422 HTTP_CURL_OPT(UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
423 }
424 } else {
425 HTTP_CURL_OPT(FOLLOWLOCATION, 0);
426 }
427
428 /* referer */
429 if (zoption = http_curl_getopt(options, "referer", IS_STRING)) {
430 HTTP_CURL_OPT(REFERER, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
431 } else {
432 HTTP_CURL_OPT(REFERER, NULL);
433 }
434
435 /* useragent, default "PECL::HTTP/version (PHP/version)" */
436 if (zoption = http_curl_getopt(options, "useragent", IS_STRING)) {
437 HTTP_CURL_OPT(USERAGENT, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
438 } else {
439 HTTP_CURL_OPT(USERAGENT, "PECL::HTTP/" HTTP_PEXT_VERSION " (PHP/" PHP_VERSION ")");
440 }
441
442 /* additional headers, array('name' => 'value') */
443 if (zoption = http_curl_getopt(options, "headers", IS_ARRAY)) {
444 char *header_key;
445 ulong header_idx;
446 struct curl_slist *headers = NULL;
447
448 FOREACH_KEY(zoption, header_key, header_idx) {
449 if (header_key) {
450 zval **header_val;
451 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val)) {
452 char header[1024] = {0};
453 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
454 headers = curl_slist_append(headers, http_request_data_copy(COPY_STRING, header));
455 }
456
457 /* reset */
458 header_key = NULL;
459 }
460 }
461
462 if (headers) {
463 HTTP_CURL_OPT(HTTPHEADER, http_request_data_copy(COPY_SLIST, headers));
464 }
465 } else {
466 HTTP_CURL_OPT(HTTPHEADER, NULL);
467 }
468
469 /* cookies, array('name' => 'value') */
470 if (zoption = http_curl_getopt(options, "cookies", IS_ARRAY)) {
471 char *cookie_key = NULL;
472 ulong cookie_idx = 0;
473 phpstr *qstr = phpstr_new();
474
475 FOREACH_KEY(zoption, cookie_key, cookie_idx) {
476 if (cookie_key) {
477 zval **cookie_val;
478 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val)) {
479 phpstr_appendf(qstr, "%s=%s; ", cookie_key, Z_STRVAL_PP(cookie_val));
480 }
481
482 /* reset */
483 cookie_key = NULL;
484 }
485 }
486
487 if (qstr->used) {
488 phpstr_fix(qstr);
489 HTTP_CURL_OPT(COOKIE, http_request_data_copy(COPY_STRING, qstr->data));
490 }
491 phpstr_free(qstr);
492 } else {
493 HTTP_CURL_OPT(COOKIE, NULL);
494 }
495
496 /* session cookies */
497 if (zoption = http_curl_getopt(options, "cookiesession", IS_BOOL)) {
498 if (Z_LVAL_P(zoption)) {
499 /* accept cookies for this session */
500 HTTP_CURL_OPT(COOKIEFILE, "");
501 } else {
502 /* reset session cookies */
503 HTTP_CURL_OPT(COOKIESESSION, 1);
504 }
505 } else {
506 HTTP_CURL_OPT(COOKIEFILE, NULL);
507 }
508
509 /* cookiestore, read initial cookies from that file and store cookies back into that file */
510 if ((zoption = http_curl_getopt(options, "cookiestore", IS_STRING)) && Z_STRLEN_P(zoption)) {
511 HTTP_CURL_OPT(COOKIEFILE, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
512 HTTP_CURL_OPT(COOKIEJAR, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
513 } else {
514 HTTP_CURL_OPT(COOKIEFILE, NULL);
515 HTTP_CURL_OPT(COOKIEJAR, NULL);
516 }
517
518 /* resume */
519 if (zoption = http_curl_getopt(options, "resume", IS_LONG)) {
520 range_req = 1;
521 HTTP_CURL_OPT(RESUME_FROM, Z_LVAL_P(zoption));
522 } else {
523 HTTP_CURL_OPT(RESUME_FROM, 0);
524 }
525
526 /* maxfilesize */
527 if (zoption = http_curl_getopt(options, "maxfilesize", IS_LONG)) {
528 HTTP_CURL_OPT(MAXFILESIZE, Z_LVAL_P(zoption));
529 } else {
530 HTTP_CURL_OPT(MAXFILESIZE, 0);
531 }
532
533 /* lastmodified */
534 if (zoption = http_curl_getopt(options, "lastmodified", IS_LONG)) {
535 HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
536 HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption));
537 } else {
538 HTTP_CURL_OPT(TIMEVALUE, 0);
539 }
540
541 /* timeout, defaults to 3 */
542 if (zoption = http_curl_getopt(options, "timeout", IS_LONG)) {
543 HTTP_CURL_OPT(TIMEOUT, Z_LVAL_P(zoption));
544 } else {
545 HTTP_CURL_OPT(TIMEOUT, 3);
546 }
547
548 /* connecttimeout, defaults to 3 */
549 if (zoption = http_curl_getopt(options, "connecttimeout", IS_LONG)) {
550 HTTP_CURL_OPT(CONNECTTIMEOUT, Z_LVAL_P(zoption));
551 } else {
552 HTTP_CURL_OPT(CONNECTTIMEOUT, 3);
553 }
554
555 /* ssl */
556 if (zoption = http_curl_getopt(options, "ssl", IS_ARRAY)) {
557 ulong idx;
558 char *key = NULL;
559 zval **param;
560
561 FOREACH_KEYVAL(zoption, key, idx, param) {
562 if (key) {
563 HTTP_CURL_OPT_SSL_STRING(CERT);
564 #if LIBCURL_VERSION_NUM >= 0x070903
565 HTTP_CURL_OPT_SSL_STRING(CERTTYPE);
566 #endif
567 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD);
568
569 HTTP_CURL_OPT_SSL_STRING(KEY);
570 HTTP_CURL_OPT_SSL_STRING(KEYTYPE);
571 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD);
572
573 HTTP_CURL_OPT_SSL_STRING(ENGINE);
574 HTTP_CURL_OPT_SSL_LONG(VERSION);
575
576 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER);
577 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST);
578 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST);
579
580
581 HTTP_CURL_OPT_STRING(CAINFO);
582 #if LIBCURL_VERSION_NUM >= 0x070908
583 HTTP_CURL_OPT_STRING(CAPATH);
584 #endif
585 HTTP_CURL_OPT_STRING(RANDOM_FILE);
586 HTTP_CURL_OPT_STRING(EGDSOCKET);
587
588 /* reset key */
589 key = NULL;
590 }
591 }
592 } else {
593 /* disable SSL verification by default */
594 HTTP_CURL_OPT(SSL_VERIFYPEER, 0);
595 HTTP_CURL_OPT(SSL_VERIFYHOST, 0);
596 }
597
598 /* request method */
599 switch (meth)
600 {
601 case HTTP_GET:
602 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1);
603 break;
604
605 case HTTP_HEAD:
606 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
607 break;
608
609 case HTTP_POST:
610 curl_easy_setopt(ch, CURLOPT_POST, 1);
611 break;
612
613 case HTTP_PUT:
614 curl_easy_setopt(ch, CURLOPT_UPLOAD, 1);
615 break;
616
617 default:
618 if (http_request_method_exists(0, meth, NULL)) {
619 curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, http_request_method_name(meth));
620 } else {
621 http_error_ex(E_WARNING, HTTP_E_CURL, "Unsupported request method: %d", meth);
622 return FAILURE;
623 }
624 break;
625 }
626
627 /* attach request body */
628 if (body && (meth != HTTP_GET) && (meth != HTTP_HEAD)) {
629 switch (body->type)
630 {
631 case HTTP_REQUEST_BODY_CSTRING:
632 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, body->data);
633 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, body->size);
634 break;
635
636 case HTTP_REQUEST_BODY_CURLPOST:
637 curl_easy_setopt(ch, CURLOPT_HTTPPOST, (struct curl_httppost *) body->data);
638 break;
639
640 case HTTP_REQUEST_BODY_UPLOADFILE:
641 curl_easy_setopt(ch, CURLOPT_READDATA, http_curl_callback_data(body));
642 curl_easy_setopt(ch, CURLOPT_INFILESIZE, body->size);
643 break;
644
645 default:
646 http_error_ex(E_WARNING, HTTP_E_CURL, "Unknown request body type: %d", body->type);
647 return FAILURE;
648 break;
649 }
650 }
651
652 return SUCCESS;
653 }
654 /* }}} */
655
656 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
657 PHP_HTTP_API STATUS _http_request_exec(CURL *ch, HashTable *info, phpstr *response, phpstr *request TSRMLS_DC)
658 {
659 CURLcode result;
660 http_curl_conv conv = {response, request, -1};
661
662 HTTP_CURL_OPT(DEBUGDATA, http_curl_callback_data(&conv));
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)) &&
746 (SUCCESS == http_request_exec(ch, info, response, NULL))) ? 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_read_callback(void *, size_t, size_t, void *) */
884 static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *s)
885 {
886 HTTP_CURL_CALLBACK_DATA(s, http_request_body *, body);
887
888 if (body->type != HTTP_REQUEST_BODY_UPLOADFILE) {
889 return 0;
890 }
891 return php_stream_read((php_stream *) body->data, data, len * n);
892 }
893 /* }}} */
894
895 /* {{{ http_curl_callback_ctx http_curl_callback_data(void *) */
896 static http_curl_callback_ctx *_http_curl_callback_data(void *data TSRMLS_DC)
897 {
898 http_curl_callback_ctx *ctx = emalloc(sizeof(http_curl_callback_ctx));
899 TSRMLS_SET_CTX(ctx->tsrm_ctx);
900 ctx->data = data;
901 return http_request_data_copy(COPY_CONTEXT, ctx);
902 }
903 /* }}} */
904
905 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
906 static int http_curl_progress_callback(void *data, double dltotal, double dlnow, double ultotal, double ulnow)
907 {
908 zval *params_pass[4], params_local[4], retval;
909 HTTP_CURL_CALLBACK_DATA(data, zval *, func);
910
911 params_pass[0] = &params_local[0];
912 params_pass[1] = &params_local[1];
913 params_pass[2] = &params_local[2];
914 params_pass[3] = &params_local[3];
915
916 INIT_PZVAL(params_pass[0]);
917 INIT_PZVAL(params_pass[1]);
918 INIT_PZVAL(params_pass[2]);
919 INIT_PZVAL(params_pass[3]);
920 ZVAL_DOUBLE(params_pass[0], dltotal);
921 ZVAL_DOUBLE(params_pass[1], dlnow);
922 ZVAL_DOUBLE(params_pass[2], ultotal);
923 ZVAL_DOUBLE(params_pass[3], ulnow);
924
925 return call_user_function(EG(function_table), NULL, func, &retval, 4, params_pass TSRMLS_CC);
926 }
927 /* }}} */
928
929 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
930 static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
931 {
932 HTTP_CURL_CALLBACK_DATA(ctx, http_curl_conv *, conv);
933
934 switch (type)
935 {
936 case CURLINFO_DATA_IN:
937 if (conv->response && conv->last_info == CURLINFO_HEADER_IN) {
938 phpstr_appends(conv->response, HTTP_CRLF);
939 }
940 case CURLINFO_HEADER_IN:
941 if (conv->response) {
942 phpstr_append(conv->response, data, length);
943 }
944 break;
945 case CURLINFO_DATA_OUT:
946 if (conv->request && conv->last_info == CURLINFO_HEADER_OUT) {
947 phpstr_appends(conv->request, HTTP_CRLF);
948 }
949 case CURLINFO_HEADER_OUT:
950 if (conv->request) {
951 phpstr_append(conv->request, data, length);
952 }
953 break;
954 }
955
956 if (type) {
957 conv->last_info = type;
958 }
959 return 0;
960 }
961 /* }}} */
962
963 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
964 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
965 {
966 zval **zoption;
967
968 if (!options || (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))) {
969 return NULL;
970 }
971
972 if (Z_TYPE_PP(zoption) != type) {
973 switch (type)
974 {
975 case IS_BOOL: convert_to_boolean_ex(zoption); break;
976 case IS_LONG: convert_to_long_ex(zoption); break;
977 case IS_DOUBLE: convert_to_double_ex(zoption); break;
978 case IS_STRING: convert_to_string_ex(zoption); break;
979 case IS_ARRAY: convert_to_array_ex(zoption); break;
980 case IS_OBJECT: convert_to_object_ex(zoption); break;
981 default:
982 break;
983 }
984 }
985
986 return *zoption;
987 }
988 /* }}} */
989
990 /*
991 * Local variables:
992 * tab-width: 4
993 * c-basic-offset: 4
994 * End:
995 * vim600: noet sw=4 ts=4 fdm=marker
996 * vim<600: noet sw=4 ts=4
997 */
998