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