- missing STATUS
[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
22 #ifdef PHP_WIN32
23 # include <winsock2.h>
24 #endif
25
26 #include <curl/curl.h>
27
28 #include "phpstr/phpstr.h"
29
30 #include "php.h"
31 #include "php_http.h"
32 #include "php_http_std_defs.h"
33 #include "php_http_api.h"
34 #include "php_http_request_api.h"
35 #include "php_http_url_api.h"
36
37 ZEND_EXTERN_MODULE_GLOBALS(http)
38
39 #if LIBCURL_VERSION_NUM < 0x070c00
40 # define curl_easy_strerror(code) HTTP_G(curlerr)
41 #endif
42
43 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
44 #define HTTP_CURL_INFO_EX(I, X) \
45 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
46 { \
47 case CURLINFO_STRING: \
48 { \
49 char *c; \
50 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
51 add_assoc_string(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
52 } \
53 } \
54 break; \
55 \
56 case CURLINFO_DOUBLE: \
57 { \
58 double d; \
59 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
60 add_assoc_double(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), d); \
61 } \
62 } \
63 break; \
64 \
65 case CURLINFO_LONG: \
66 { \
67 long l; \
68 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
69 add_assoc_long(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), l); \
70 } \
71 } \
72 break; \
73 }
74
75 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
76 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
77 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
78 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
79 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
80 if (!strcasecmp(key, #keyname)) { \
81 convert_to_string_ex(param); \
82 HTTP_CURL_OPT(optname, http_curl_copystr(Z_STRVAL_PP(param))); \
83 key = NULL; \
84 continue; \
85 }
86 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
87 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
88 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
89 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
90 if (!strcasecmp(key, #keyname)) { \
91 convert_to_long_ex(param); \
92 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
93 key = NULL; \
94 continue; \
95 }
96
97
98 static const char *const http_request_methods[HTTP_MAX_REQUEST_METHOD + 1];
99 #define http_curl_copystr(s) _http_curl_copystr((s) TSRMLS_CC)
100 static inline char *_http_curl_copystr(const char *str TSRMLS_DC);
101 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
102 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
103 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
104 static size_t http_curl_write_callback(char *, size_t, size_t, void *);
105 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
106 static int http_curl_progress_callback(void *, double, double, double, double);
107 static int http_curl_debug_callback(CURL *, curl_infotype, char *, size_t, void *);
108
109
110 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
111 PHP_HTTP_API STATUS _http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files TSRMLS_DC)
112 {
113 if (files && (zend_hash_num_elements(files) > 0)) {
114 char *key = NULL;
115 ulong idx;
116 zval **data;
117 struct curl_httppost *http_post_data[2] = {NULL, NULL};
118
119 /* normal data */
120 FOREACH_HASH_KEYVAL(fields, key, idx, data) {
121 CURLcode err;
122 if (key) {
123 convert_to_string_ex(data);
124 err = curl_formadd(&http_post_data[0], &http_post_data[1],
125 CURLFORM_COPYNAME, key,
126 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
127 CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data),
128 CURLFORM_END
129 );
130 if (CURLE_OK != err) {
131 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not encode post fields: %s", curl_easy_strerror(err));
132 curl_formfree(http_post_data[0]);
133 return FAILURE;
134 }
135
136 /* reset */
137 key = NULL;
138 }
139 }
140
141 /* file data */
142 FOREACH_HASH_VAL(files, data) {
143 CURLcode err;
144 zval **file, **type, **name;
145 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) &&
146 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) &&
147 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
148 err = curl_formadd(&http_post_data[0], &http_post_data[1],
149 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
150 CURLFORM_FILE, Z_STRVAL_PP(file),
151 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
152 CURLFORM_END
153 );
154 if (CURLE_OK != err) {
155 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not encode post files: %s", curl_easy_strerror(err));
156 curl_formfree(http_post_data[0]);
157 return FAILURE;
158 }
159 }
160 }
161
162 body->type = HTTP_REQUEST_BODY_CURLPOST;
163 body->data = http_post_data[0];
164 body->size = 0;
165
166 } else {
167 char *encoded;
168 size_t encoded_len;
169
170 if (SUCCESS != http_urlencode_hash_ex(fields, 1, NULL, 0, &encoded, &encoded_len)) {
171 http_error(E_WARNING, HTTP_E_ENCODE, "Could not encode post data");
172 return FAILURE;
173 }
174
175 body->type = HTTP_REQUEST_BODY_CSTRING;
176 body->data = encoded;
177 body->size = encoded_len;
178 }
179
180 return SUCCESS;
181 }
182 /* }}} */
183
184 /* {{{ void http_request_body_dtor(http_request_body *) */
185 PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC)
186 {
187 switch (body->type)
188 {
189 case HTTP_REQUEST_BODY_CSTRING:
190 efree(body->data);
191 break;
192
193 case HTTP_REQUEST_BODY_CURLPOST:
194 curl_formfree(body->data);
195 break;
196 }
197 }
198 /* }}} */
199
200 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
201 PHP_HTTP_API STATUS _http_request_ex(CURL *ch, http_request_method meth, const char *url, http_request_body *body, HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
202 {
203 CURLcode result = CURLE_OK;
204 STATUS status = SUCCESS;
205 zend_bool clean_curl = 0, range_req = 0;
206 zval *zoption;
207
208 /* check/init CURL handle */
209 if (ch) {
210 #if LIBCURL_VERSION_NUM >= 0x070c01
211 curl_easy_reset(ch);
212 #endif
213 } else {
214 if (ch = curl_easy_init()) {
215 clean_curl = 1;
216 } else {
217 http_error(E_WARNING, HTTP_E_CURL, "Could not initialize curl");
218 return FAILURE;
219 }
220 }
221
222 /* set options */
223 if (url) {
224 HTTP_CURL_OPT(URL, url);
225 }
226
227 HTTP_CURL_OPT(HEADER, 0);
228 HTTP_CURL_OPT(FILETIME, 1);
229 HTTP_CURL_OPT(AUTOREFERER, 1);
230 HTTP_CURL_OPT(READFUNCTION, http_curl_read_callback);
231 HTTP_CURL_OPT(WRITEFUNCTION, http_curl_write_callback);
232 HTTP_CURL_OPT(HEADERFUNCTION, http_curl_write_callback);
233
234 if (response) {
235 HTTP_CURL_OPT(WRITEDATA, response);
236 HTTP_CURL_OPT(WRITEHEADER, response);
237 }
238
239 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
240 HTTP_CURL_OPT(NOSIGNAL, 1);
241 #endif
242 #if LIBCURL_VERSION_NUM < 0x070c00
243 HTTP_CURL_OPT(ERRORBUFFER, HTTP_G(curlerr));
244 #endif
245
246 /* progress callback */
247 if (zoption = http_curl_getopt(options, "onprogress", 0)) {
248 HTTP_CURL_OPT(NOPROGRESS, 0);
249 HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
250 HTTP_CURL_OPT(PROGRESSDATA, zoption);
251 } else {
252 HTTP_CURL_OPT(NOPROGRESS, 1);
253 }
254
255 /* debug callback */
256 if (zoption = http_curl_getopt(options, "ondebug", 0)) {
257 HTTP_CURL_OPT(VERBOSE, 1);
258 HTTP_CURL_OPT(DEBUGFUNCTION, http_curl_debug_callback);
259 HTTP_CURL_OPT(DEBUGDATA, zoption);
260 } else {
261 HTTP_CURL_OPT(VERBOSE, 0);
262 }
263
264 /* proxy */
265 if (zoption = http_curl_getopt(options, "proxyhost", IS_STRING)) {
266 HTTP_CURL_OPT(PROXY, http_curl_copystr(Z_STRVAL_P(zoption)));
267 /* port */
268 if (zoption = http_curl_getopt(options, "proxyport", IS_LONG)) {
269 HTTP_CURL_OPT(PROXYPORT, Z_LVAL_P(zoption));
270 }
271 /* user:pass */
272 if (zoption = http_curl_getopt(options, "proxyauth", IS_STRING)) {
273 HTTP_CURL_OPT(PROXYUSERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
274 }
275 #if LIBCURL_VERSION_NUM >= 0x070a07
276 /* auth method */
277 if (zoption = http_curl_getopt(options, "proxyauthtype", IS_LONG)) {
278 HTTP_CURL_OPT(PROXYAUTH, Z_LVAL_P(zoption));
279 }
280 #endif
281 }
282
283 /* outgoing interface */
284 if (zoption = http_curl_getopt(options, "interface", IS_STRING)) {
285 HTTP_CURL_OPT(INTERFACE, http_curl_copystr(Z_STRVAL_P(zoption)));
286 }
287
288 /* another port */
289 if (zoption = http_curl_getopt(options, "port", IS_LONG)) {
290 HTTP_CURL_OPT(PORT, Z_LVAL_P(zoption));
291 }
292
293 /* auth */
294 if (zoption = http_curl_getopt(options, "httpauth", IS_STRING)) {
295 HTTP_CURL_OPT(USERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
296 }
297 #if LIBCURL_VERSION_NUM >= 0x070a06
298 if (zoption = http_curl_getopt(options, "httpauthtype", IS_LONG)) {
299 HTTP_CURL_OPT(HTTPAUTH, Z_LVAL_P(zoption));
300 }
301 #endif
302
303 /* compress, empty string enables deflate and gzip */
304 if (zoption = http_curl_getopt(options, "compress", IS_BOOL)) {
305 if (Z_LVAL_P(zoption)) {
306 HTTP_CURL_OPT(ENCODING, http_curl_copystr(""));
307 }
308 }
309
310 /* redirects, defaults to 0 */
311 if (zoption = http_curl_getopt(options, "redirect", IS_LONG)) {
312 HTTP_CURL_OPT(FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
313 HTTP_CURL_OPT(MAXREDIRS, Z_LVAL_P(zoption));
314 if (zoption = http_curl_getopt(options, "unrestrictedauth", IS_BOOL)) {
315 HTTP_CURL_OPT(UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
316 }
317 } else {
318 HTTP_CURL_OPT(FOLLOWLOCATION, 0);
319 }
320
321 /* referer */
322 if (zoption = http_curl_getopt(options, "referer", IS_STRING)) {
323 HTTP_CURL_OPT(REFERER, http_curl_copystr(Z_STRVAL_P(zoption)));
324 }
325
326 /* useragent, default "PECL::HTTP/version (PHP/version)" */
327 if (zoption = http_curl_getopt(options, "useragent", IS_STRING)) {
328 HTTP_CURL_OPT(USERAGENT, http_curl_copystr(Z_STRVAL_P(zoption)));
329 } else {
330 HTTP_CURL_OPT(USERAGENT,
331 "PECL::HTTP/" HTTP_PEXT_VERSION " (PHP/" PHP_VERSION ")");
332 }
333
334 /* additional headers, array('name' => 'value') */
335 if (zoption = http_curl_getopt(options, "headers", IS_ARRAY)) {
336 char *header_key;
337 long header_idx;
338 struct curl_slist *headers = NULL;
339
340 FOREACH_KEY(zoption, header_key, header_idx) {
341 if (header_key) {
342 zval **header_val;
343 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val)) {
344 char header[1024] = {0};
345 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
346 headers = curl_slist_append(headers, http_curl_copystr(header));
347 }
348
349 /* reset */
350 header_key = NULL;
351 }
352 }
353
354 if (headers) {
355 HTTP_CURL_OPT(HTTPHEADER, headers);
356 }
357 }
358
359 /* cookies, array('name' => 'value') */
360 if (zoption = http_curl_getopt(options, "cookies", IS_ARRAY)) {
361 char *cookie_key = NULL;
362 long cookie_idx = 0;
363 phpstr *qstr = phpstr_new();
364
365 FOREACH_KEY(zoption, cookie_key, cookie_idx) {
366 if (cookie_key) {
367 zval **cookie_val;
368 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val)) {
369 phpstr_appendf(qstr, "%s=%s; ", cookie_key, Z_STRVAL_PP(cookie_val));
370 }
371
372 /* reset */
373 cookie_key = NULL;
374 }
375 }
376
377 if (qstr->used) {
378 phpstr_fix(qstr);
379 HTTP_CURL_OPT(COOKIE, http_curl_copystr(qstr->data));
380 }
381 phpstr_free(qstr);
382 }
383
384 /* cookiestore */
385 if (zoption = http_curl_getopt(options, "cookiestore", IS_STRING)) {
386 HTTP_CURL_OPT(COOKIEFILE, http_curl_copystr(Z_STRVAL_P(zoption)));
387 HTTP_CURL_OPT(COOKIEJAR, http_curl_copystr(Z_STRVAL_P(zoption)));
388 }
389
390 /* resume */
391 if (zoption = http_curl_getopt(options, "resume", IS_LONG)) {
392 range_req = 1;
393 HTTP_CURL_OPT(RESUME_FROM, Z_LVAL_P(zoption));
394 }
395
396 /* maxfilesize */
397 if (zoption = http_curl_getopt(options, "maxfilesize", IS_LONG)) {
398 HTTP_CURL_OPT(MAXFILESIZE, Z_LVAL_P(zoption));
399 }
400
401 /* lastmodified */
402 if (zoption = http_curl_getopt(options, "lastmodified", IS_LONG)) {
403 HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
404 HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption));
405 }
406
407 /* timeout */
408 if (zoption = http_curl_getopt(options, "timeout", IS_LONG)) {
409 HTTP_CURL_OPT(TIMEOUT, Z_LVAL_P(zoption));
410 }
411
412 /* connecttimeout, defaults to 1 */
413 if (zoption = http_curl_getopt(options, "connecttimeout", IS_LONG)) {
414 HTTP_CURL_OPT(CONNECTTIMEOUT, Z_LVAL_P(zoption));
415 } else {
416 HTTP_CURL_OPT(CONNECTTIMEOUT, 1);
417 }
418
419 /* ssl */
420 if (zoption = http_curl_getopt(options, "ssl", IS_ARRAY)) {
421 ulong idx;
422 char *key = NULL;
423 zval **param;
424
425 FOREACH_KEYVAL(zoption, key, idx, param) {
426 if (key) {fprintf(stderr, "%s\n", key);
427 HTTP_CURL_OPT_SSL_STRING(CERT);
428 #if LIBCURL_VERSION_NUM >= 0x070903
429 HTTP_CURL_OPT_SSL_STRING(CERTTYPE);
430 #endif
431 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD);
432
433 HTTP_CURL_OPT_SSL_STRING(KEY);
434 HTTP_CURL_OPT_SSL_STRING(KEYTYPE);
435 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD);
436
437 HTTP_CURL_OPT_SSL_STRING(ENGINE);
438 HTTP_CURL_OPT_SSL_LONG(VERSION);
439
440 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER);
441 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST);
442 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST);
443
444
445 HTTP_CURL_OPT_STRING(CAINFO);
446 #if LIBCURL_VERSION_NUM >= 0x070908
447 HTTP_CURL_OPT_STRING(CAPATH);
448 #endif
449 HTTP_CURL_OPT_STRING(RANDOM_FILE);
450 HTTP_CURL_OPT_STRING(EGDSOCKET);
451
452 /* reset key */
453 key = NULL;
454 }
455 }
456 } else {
457 /* disable SSL verification by default */
458 HTTP_CURL_OPT(SSL_VERIFYPEER, 0);
459 HTTP_CURL_OPT(SSL_VERIFYHOST, 0);
460 }
461
462 /* request method */
463 switch (meth)
464 {
465 case HTTP_GET:
466 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1);
467 break;
468
469 case HTTP_HEAD:
470 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
471 break;
472
473 case HTTP_POST:
474 curl_easy_setopt(ch, CURLOPT_POST, 1);
475 break;
476
477 case HTTP_PUT:
478 curl_easy_setopt(ch, CURLOPT_UPLOAD, 1);
479 break;
480
481 default:
482 if ((meth > HTTP_NO_REQUEST_METHOD) && (meth < HTTP_MAX_REQUEST_METHOD)) {
483 curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, http_request_methods[meth]);
484 } else {
485 http_error_ex(E_WARNING, HTTP_E_CURL, "Unsupported request method: %d", meth);
486 status = FAILURE;
487 goto http_request_end;
488 }
489 break;
490 }
491
492 /* attach request body */
493 if (body && (meth != HTTP_GET) && (meth != HTTP_HEAD)) {
494 switch (body->type)
495 {
496 case HTTP_REQUEST_BODY_CSTRING:
497 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, (char *) body->data);
498 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, body->size);
499 break;
500
501 case HTTP_REQUEST_BODY_CURLPOST:
502 curl_easy_setopt(ch, CURLOPT_HTTPPOST, (struct curl_httppost *) body->data);
503 break;
504
505 case HTTP_REQUEST_BODY_UPLOADFILE:
506 case HTTP_REQUEST_BODY_UPLOADDATA:
507 curl_easy_setopt(ch, CURLOPT_READDATA, body);
508 curl_easy_setopt(ch, CURLOPT_INFILESIZE, body->size);
509 break;
510
511 default:
512 http_error_ex(E_WARNING, HTTP_E_CURL, "Unkown request body type: %d", body->type);
513 status = FAILURE;
514 goto http_request_end;
515 break;
516 }
517 }
518
519 /* perform request */
520 if (CURLE_OK == (result = curl_easy_perform(ch))) {
521 /* get curl info */
522 if (info) {
523 zval array;
524 Z_ARRVAL(array) = info;
525
526 HTTP_CURL_INFO(EFFECTIVE_URL);
527 #if LIBCURL_VERSION_NUM >= 0x070a07
528 HTTP_CURL_INFO(RESPONSE_CODE);
529 #else
530 HTTP_CURL_INFO_EX(HTTP_CODE, RESPONSE_CODE);
531 #endif
532 HTTP_CURL_INFO(HTTP_CONNECTCODE);
533 #if LIBCURL_VERSION_NUM >= 0x070500
534 HTTP_CURL_INFO(FILETIME);
535 #endif
536 HTTP_CURL_INFO(TOTAL_TIME);
537 HTTP_CURL_INFO(NAMELOOKUP_TIME);
538 HTTP_CURL_INFO(CONNECT_TIME);
539 HTTP_CURL_INFO(PRETRANSFER_TIME);
540 HTTP_CURL_INFO(STARTTRANSFER_TIME);
541 #if LIBCURL_VERSION_NUM >= 0x070907
542 HTTP_CURL_INFO(REDIRECT_TIME);
543 HTTP_CURL_INFO(REDIRECT_COUNT);
544 #endif
545 HTTP_CURL_INFO(SIZE_UPLOAD);
546 HTTP_CURL_INFO(SIZE_DOWNLOAD);
547 HTTP_CURL_INFO(SPEED_DOWNLOAD);
548 HTTP_CURL_INFO(SPEED_UPLOAD);
549 HTTP_CURL_INFO(HEADER_SIZE);
550 HTTP_CURL_INFO(REQUEST_SIZE);
551 HTTP_CURL_INFO(SSL_VERIFYRESULT);
552 #if LIBCURL_VERSION_NUM >= 0x070c03
553 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
554 #endif
555 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD);
556 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD);
557 HTTP_CURL_INFO(CONTENT_TYPE);
558 #if LIBCURL_VERSION_NUM >= 0x070a03
559 /*HTTP_CURL_INFO(PRIVATE);*/
560 #endif
561 #if LIBCURL_VERSION_NUM >= 0x070a08
562 HTTP_CURL_INFO(HTTPAUTH_AVAIL);
563 HTTP_CURL_INFO(PROXYAUTH_AVAIL);
564 #endif
565 #if LIBCURL_VERSION_NUM >= 0x070c02
566 /*HTTP_CURL_INFO(OS_ERRNO);*/
567 #endif
568 #if LIBCURL_VERSION_NUM >= 0x070c03
569 HTTP_CURL_INFO(NUM_CONNECTS);
570 #endif
571 }
572 } else {
573 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not perform request: %s", curl_easy_strerror(result));
574 status = FAILURE;
575 }
576
577 http_request_end:
578 /* free strings copied with http_curl_copystr() */
579 zend_llist_clean(&HTTP_G(to_free));
580
581 /* clean curl handle if acquired */
582 if (clean_curl) {
583 curl_easy_cleanup(ch);
584 ch = NULL;
585 }
586
587 /* finalize response */
588 if (response) {
589 phpstr_fix(PHPSTR(response));
590 }
591
592 return status;
593 }
594 /* }}} */
595
596 /* {{{ char *http_request_method_string(http_request_method) */
597 PHP_HTTP_API const char *_http_request_method_string(http_request_method m)
598 {
599 if ((m > HTTP_NO_REQUEST_METHOD) && (m < HTTP_MAX_REQUEST_METHOD)) {
600 return http_request_methods[m];
601 }
602 return http_request_methods[0];
603 }
604 /* }}} */
605
606 /* {{{ char *http_request_methods[] */
607 static const char *const http_request_methods[] = {
608 "UNKOWN",
609 /* HTTP/1.1 */
610 "GET",
611 "HEAD",
612 "POST",
613 "PUT",
614 "DELETE",
615 "OPTIONS",
616 "TRACE",
617 "CONNECT",
618 /* WebDAV - RFC 2518 */
619 "PROPFIND",
620 "PROPPATCH",
621 "MKCOL",
622 "COPY",
623 "MOVE",
624 "LOCK",
625 "UNLOCK",
626 /* WebDAV Versioning - RFC 3253 */
627 "VERSION-CONTROL",
628 "REPORT",
629 "CHECKOUT",
630 "CHECKIN",
631 "UNCHECKOUT",
632 "MKWORKSPACE",
633 "UPDATE",
634 "LABEL",
635 "MERGE",
636 "BASELINE-CONTROL",
637 "MKACTIVITY",
638 /* WebDAV Access Control - RFC 3744 */
639 "ACL",
640 NULL
641 };
642 /* }}} */
643
644 /* {{{ static inline char *http_curl_copystr(char *) */
645 static inline char *_http_curl_copystr(const char *str TSRMLS_DC)
646 {
647 char *new_str = estrdup(str);
648 zend_llist_add_element(&HTTP_G(to_free), &new_str);
649 return new_str;
650 }
651 /* }}} */
652
653 /* {{{ static size_t http_curl_write_callback(char *, size_t, size_t, void *) */
654 static size_t http_curl_write_callback(char *buf, size_t len, size_t n, void *s)
655 {
656 return s ? phpstr_append(PHPSTR(s), buf, len * n) : len * n;
657 }
658 /* }}} */
659
660 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
661 static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *s)
662 {
663 static char *offset = NULL, *original = NULL;
664 http_request_body *body = (http_request_body *) s;
665
666 switch (body->type)
667 {
668 case HTTP_REQUEST_BODY_UPLOADFILE:
669 {
670 TSRMLS_FETCH();
671 return php_stream_read((php_stream *) body->data, data, len * n);
672 }
673 break;
674
675 case HTTP_REQUEST_BODY_UPLOADDATA:
676 {
677 size_t avail;
678 if (original != s) {
679 original = offset = s;
680 }
681 if ((avail = body->size - (offset - original)) < 1) {
682 return 0;
683 }
684 if (avail < (len * n)) {
685 memcpy(data, offset, avail);
686 offset += avail;
687 return avail;
688 } else {
689 memcpy(data, offset, len * n);
690 offset += len * n;
691 return len * n;
692 }
693 }
694 break;
695
696 default:
697 return 0;
698 break;
699 }
700 }
701 /* }}} */
702
703 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
704 static int http_curl_progress_callback(void *data, double dltotal, double dlnow, double ultotal, double ulnow)
705 {
706 zval *params_pass[4], params_local[4], retval, *func = (zval *) data;
707 TSRMLS_FETCH();
708
709 params_pass[0] = &params_local[0];
710 params_pass[1] = &params_local[1];
711 params_pass[2] = &params_local[2];
712 params_pass[3] = &params_local[3];
713
714 ZVAL_DOUBLE(params_pass[0], dltotal);
715 ZVAL_DOUBLE(params_pass[1], dlnow);
716 ZVAL_DOUBLE(params_pass[2], ultotal);
717 ZVAL_DOUBLE(params_pass[3], ulnow);
718
719 return call_user_function(EG(function_table), NULL, func, &retval, 4, params_pass TSRMLS_CC);
720 }
721 /* }}} */
722
723 static int http_curl_debug_callback(CURL *ch, curl_infotype type, char *string, size_t length, void *data)
724 {
725 zval *params_pass[2], params_local[2], retval, *func = (zval *) data;
726 TSRMLS_FETCH();
727
728 params_pass[0] = &params_local[0];
729 params_pass[1] = &params_local[1];
730
731 ZVAL_LONG(params_pass[0], type);
732 ZVAL_STRINGL(params_pass[1], string, length, 1);
733
734 call_user_function(EG(function_table), NULL, func, &retval, 2, params_pass TSRMLS_CC);
735
736 return 0;
737 }
738 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
739 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
740 {
741 zval **zoption;
742
743 if (!options || (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))) {
744 return NULL;
745 }
746
747 if (Z_TYPE_PP(zoption) != type) {
748 switch (type)
749 {
750 case IS_BOOL: convert_to_boolean_ex(zoption); break;
751 case IS_LONG: convert_to_long_ex(zoption); break;
752 case IS_DOUBLE: convert_to_double_ex(zoption); break;
753 case IS_STRING: convert_to_string_ex(zoption); break;
754 case IS_ARRAY: convert_to_array_ex(zoption); break;
755 case IS_OBJECT: convert_to_object_ex(zoption); break;
756 default:
757 break;
758 }
759 }
760
761 return *zoption;
762 }
763 /* }}} */
764
765 /*
766 * Local variables:
767 * tab-width: 4
768 * c-basic-offset: 4
769 * End:
770 * vim600: noet sw=4 ts=4 fdm=marker
771 * vim<600: noet sw=4 ts=4
772 */
773