- improved put support
[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 case HTTP_REQUEST_BODY_UPLOADFILE:
198 php_stream_close(body->data);
199 break;
200 }
201 }
202 /* }}} */
203
204 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
205 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)
206 {
207 CURLcode result = CURLE_OK;
208 STATUS status = SUCCESS;
209 zend_bool clean_curl = 0, range_req = 0;
210 zval *zoption;
211
212 /* check/init CURL handle */
213 if (ch) {
214 #if LIBCURL_VERSION_NUM >= 0x070c01
215 curl_easy_reset(ch);
216 #endif
217 } else {
218 if (ch = curl_easy_init()) {
219 clean_curl = 1;
220 } else {
221 http_error(E_WARNING, HTTP_E_CURL, "Could not initialize curl");
222 return FAILURE;
223 }
224 }
225
226 /* set options */
227 if (url) {
228 HTTP_CURL_OPT(URL, url);
229 }
230
231 HTTP_CURL_OPT(HEADER, 0);
232 HTTP_CURL_OPT(FILETIME, 1);
233 HTTP_CURL_OPT(AUTOREFERER, 1);
234 HTTP_CURL_OPT(READFUNCTION, http_curl_read_callback);
235 HTTP_CURL_OPT(WRITEFUNCTION, http_curl_write_callback);
236 HTTP_CURL_OPT(HEADERFUNCTION, http_curl_write_callback);
237
238 if (response) {
239 HTTP_CURL_OPT(WRITEDATA, response);
240 HTTP_CURL_OPT(WRITEHEADER, response);
241 }
242
243 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
244 HTTP_CURL_OPT(NOSIGNAL, 1);
245 #endif
246 #if LIBCURL_VERSION_NUM < 0x070c00
247 HTTP_CURL_OPT(ERRORBUFFER, HTTP_G(curlerr));
248 #endif
249
250 /* progress callback */
251 if (zoption = http_curl_getopt(options, "onprogress", 0)) {
252 HTTP_CURL_OPT(NOPROGRESS, 0);
253 HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
254 HTTP_CURL_OPT(PROGRESSDATA, zoption);
255 } else {
256 HTTP_CURL_OPT(NOPROGRESS, 1);
257 }
258
259 /* debug callback */
260 if (zoption = http_curl_getopt(options, "ondebug", 0)) {
261 HTTP_CURL_OPT(VERBOSE, 1);
262 HTTP_CURL_OPT(DEBUGFUNCTION, http_curl_debug_callback);
263 HTTP_CURL_OPT(DEBUGDATA, zoption);
264 } else {
265 HTTP_CURL_OPT(VERBOSE, 0);
266 }
267
268 /* proxy */
269 if (zoption = http_curl_getopt(options, "proxyhost", IS_STRING)) {
270 HTTP_CURL_OPT(PROXY, http_curl_copystr(Z_STRVAL_P(zoption)));
271 /* port */
272 if (zoption = http_curl_getopt(options, "proxyport", IS_LONG)) {
273 HTTP_CURL_OPT(PROXYPORT, Z_LVAL_P(zoption));
274 }
275 /* user:pass */
276 if (zoption = http_curl_getopt(options, "proxyauth", IS_STRING)) {
277 HTTP_CURL_OPT(PROXYUSERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
278 }
279 #if LIBCURL_VERSION_NUM >= 0x070a07
280 /* auth method */
281 if (zoption = http_curl_getopt(options, "proxyauthtype", IS_LONG)) {
282 HTTP_CURL_OPT(PROXYAUTH, Z_LVAL_P(zoption));
283 }
284 #endif
285 }
286
287 /* outgoing interface */
288 if (zoption = http_curl_getopt(options, "interface", IS_STRING)) {
289 HTTP_CURL_OPT(INTERFACE, http_curl_copystr(Z_STRVAL_P(zoption)));
290 }
291
292 /* another port */
293 if (zoption = http_curl_getopt(options, "port", IS_LONG)) {
294 HTTP_CURL_OPT(PORT, Z_LVAL_P(zoption));
295 }
296
297 /* auth */
298 if (zoption = http_curl_getopt(options, "httpauth", IS_STRING)) {
299 HTTP_CURL_OPT(USERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
300 }
301 #if LIBCURL_VERSION_NUM >= 0x070a06
302 if (zoption = http_curl_getopt(options, "httpauthtype", IS_LONG)) {
303 HTTP_CURL_OPT(HTTPAUTH, Z_LVAL_P(zoption));
304 }
305 #endif
306
307 /* compress, empty string enables deflate and gzip */
308 if (zoption = http_curl_getopt(options, "compress", IS_BOOL)) {
309 if (Z_LVAL_P(zoption)) {
310 HTTP_CURL_OPT(ENCODING, http_curl_copystr(""));
311 }
312 }
313
314 /* redirects, defaults to 0 */
315 if (zoption = http_curl_getopt(options, "redirect", IS_LONG)) {
316 HTTP_CURL_OPT(FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
317 HTTP_CURL_OPT(MAXREDIRS, Z_LVAL_P(zoption));
318 if (zoption = http_curl_getopt(options, "unrestrictedauth", IS_BOOL)) {
319 HTTP_CURL_OPT(UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
320 }
321 } else {
322 HTTP_CURL_OPT(FOLLOWLOCATION, 0);
323 }
324
325 /* referer */
326 if (zoption = http_curl_getopt(options, "referer", IS_STRING)) {
327 HTTP_CURL_OPT(REFERER, http_curl_copystr(Z_STRVAL_P(zoption)));
328 }
329
330 /* useragent, default "PECL::HTTP/version (PHP/version)" */
331 if (zoption = http_curl_getopt(options, "useragent", IS_STRING)) {
332 HTTP_CURL_OPT(USERAGENT, http_curl_copystr(Z_STRVAL_P(zoption)));
333 } else {
334 HTTP_CURL_OPT(USERAGENT,
335 "PECL::HTTP/" HTTP_PEXT_VERSION " (PHP/" PHP_VERSION ")");
336 }
337
338 /* additional headers, array('name' => 'value') */
339 if (zoption = http_curl_getopt(options, "headers", IS_ARRAY)) {
340 char *header_key;
341 long header_idx;
342 struct curl_slist *headers = NULL;
343
344 FOREACH_KEY(zoption, header_key, header_idx) {
345 if (header_key) {
346 zval **header_val;
347 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val)) {
348 char header[1024] = {0};
349 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
350 headers = curl_slist_append(headers, http_curl_copystr(header));
351 }
352
353 /* reset */
354 header_key = NULL;
355 }
356 }
357
358 if (headers) {
359 HTTP_CURL_OPT(HTTPHEADER, headers);
360 }
361 }
362
363 /* cookies, array('name' => 'value') */
364 if (zoption = http_curl_getopt(options, "cookies", IS_ARRAY)) {
365 char *cookie_key = NULL;
366 long cookie_idx = 0;
367 phpstr *qstr = phpstr_new();
368
369 FOREACH_KEY(zoption, cookie_key, cookie_idx) {
370 if (cookie_key) {
371 zval **cookie_val;
372 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val)) {
373 phpstr_appendf(qstr, "%s=%s; ", cookie_key, Z_STRVAL_PP(cookie_val));
374 }
375
376 /* reset */
377 cookie_key = NULL;
378 }
379 }
380
381 if (qstr->used) {
382 phpstr_fix(qstr);
383 HTTP_CURL_OPT(COOKIE, http_curl_copystr(qstr->data));
384 }
385 phpstr_free(qstr);
386 }
387
388 /* cookiestore */
389 if (zoption = http_curl_getopt(options, "cookiestore", IS_STRING)) {
390 HTTP_CURL_OPT(COOKIEFILE, http_curl_copystr(Z_STRVAL_P(zoption)));
391 HTTP_CURL_OPT(COOKIEJAR, http_curl_copystr(Z_STRVAL_P(zoption)));
392 }
393
394 /* resume */
395 if (zoption = http_curl_getopt(options, "resume", IS_LONG)) {
396 range_req = 1;
397 HTTP_CURL_OPT(RESUME_FROM, Z_LVAL_P(zoption));
398 }
399
400 /* maxfilesize */
401 if (zoption = http_curl_getopt(options, "maxfilesize", IS_LONG)) {
402 HTTP_CURL_OPT(MAXFILESIZE, Z_LVAL_P(zoption));
403 }
404
405 /* lastmodified */
406 if (zoption = http_curl_getopt(options, "lastmodified", IS_LONG)) {
407 HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
408 HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption));
409 }
410
411 /* timeout */
412 if (zoption = http_curl_getopt(options, "timeout", IS_LONG)) {
413 HTTP_CURL_OPT(TIMEOUT, Z_LVAL_P(zoption));
414 }
415
416 /* connecttimeout, defaults to 1 */
417 if (zoption = http_curl_getopt(options, "connecttimeout", IS_LONG)) {
418 HTTP_CURL_OPT(CONNECTTIMEOUT, Z_LVAL_P(zoption));
419 } else {
420 HTTP_CURL_OPT(CONNECTTIMEOUT, 1);
421 }
422
423 /* ssl */
424 if (zoption = http_curl_getopt(options, "ssl", IS_ARRAY)) {
425 ulong idx;
426 char *key = NULL;
427 zval **param;
428
429 FOREACH_KEYVAL(zoption, key, idx, param) {
430 if (key) {fprintf(stderr, "%s\n", key);
431 HTTP_CURL_OPT_SSL_STRING(CERT);
432 #if LIBCURL_VERSION_NUM >= 0x070903
433 HTTP_CURL_OPT_SSL_STRING(CERTTYPE);
434 #endif
435 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD);
436
437 HTTP_CURL_OPT_SSL_STRING(KEY);
438 HTTP_CURL_OPT_SSL_STRING(KEYTYPE);
439 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD);
440
441 HTTP_CURL_OPT_SSL_STRING(ENGINE);
442 HTTP_CURL_OPT_SSL_LONG(VERSION);
443
444 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER);
445 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST);
446 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST);
447
448
449 HTTP_CURL_OPT_STRING(CAINFO);
450 #if LIBCURL_VERSION_NUM >= 0x070908
451 HTTP_CURL_OPT_STRING(CAPATH);
452 #endif
453 HTTP_CURL_OPT_STRING(RANDOM_FILE);
454 HTTP_CURL_OPT_STRING(EGDSOCKET);
455
456 /* reset key */
457 key = NULL;
458 }
459 }
460 } else {
461 /* disable SSL verification by default */
462 HTTP_CURL_OPT(SSL_VERIFYPEER, 0);
463 HTTP_CURL_OPT(SSL_VERIFYHOST, 0);
464 }
465
466 /* request method */
467 switch (meth)
468 {
469 case HTTP_GET:
470 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1);
471 break;
472
473 case HTTP_HEAD:
474 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
475 break;
476
477 case HTTP_POST:
478 curl_easy_setopt(ch, CURLOPT_POST, 1);
479 break;
480
481 case HTTP_PUT:
482 curl_easy_setopt(ch, CURLOPT_UPLOAD, 1);
483 break;
484
485 default:
486 if ((meth > HTTP_NO_REQUEST_METHOD) && (meth < HTTP_MAX_REQUEST_METHOD)) {
487 curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, http_request_methods[meth]);
488 } else {
489 http_error_ex(E_WARNING, HTTP_E_CURL, "Unsupported request method: %d", meth);
490 status = FAILURE;
491 goto http_request_end;
492 }
493 break;
494 }
495
496 /* attach request body */
497 if (body && (meth != HTTP_GET) && (meth != HTTP_HEAD)) {
498 switch (body->type)
499 {
500 case HTTP_REQUEST_BODY_CSTRING:
501 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, (char *) body->data);
502 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, body->size);
503 break;
504
505 case HTTP_REQUEST_BODY_CURLPOST:
506 curl_easy_setopt(ch, CURLOPT_HTTPPOST, (struct curl_httppost *) body->data);
507 break;
508
509 case HTTP_REQUEST_BODY_UPLOADFILE:
510 case HTTP_REQUEST_BODY_UPLOADDATA:
511 curl_easy_setopt(ch, CURLOPT_READDATA, body);
512 curl_easy_setopt(ch, CURLOPT_INFILESIZE, body->size);
513 break;
514
515 default:
516 http_error_ex(E_WARNING, HTTP_E_CURL, "Unkown request body type: %d", body->type);
517 status = FAILURE;
518 goto http_request_end;
519 break;
520 }
521 }
522
523 /* perform request */
524 if (CURLE_OK == (result = curl_easy_perform(ch))) {
525 /* get curl info */
526 if (info) {
527 zval array;
528 Z_ARRVAL(array) = info;
529
530 HTTP_CURL_INFO(EFFECTIVE_URL);
531 #if LIBCURL_VERSION_NUM >= 0x070a07
532 HTTP_CURL_INFO(RESPONSE_CODE);
533 #else
534 HTTP_CURL_INFO_EX(HTTP_CODE, RESPONSE_CODE);
535 #endif
536 HTTP_CURL_INFO(HTTP_CONNECTCODE);
537 #if LIBCURL_VERSION_NUM >= 0x070500
538 HTTP_CURL_INFO(FILETIME);
539 #endif
540 HTTP_CURL_INFO(TOTAL_TIME);
541 HTTP_CURL_INFO(NAMELOOKUP_TIME);
542 HTTP_CURL_INFO(CONNECT_TIME);
543 HTTP_CURL_INFO(PRETRANSFER_TIME);
544 HTTP_CURL_INFO(STARTTRANSFER_TIME);
545 #if LIBCURL_VERSION_NUM >= 0x070907
546 HTTP_CURL_INFO(REDIRECT_TIME);
547 HTTP_CURL_INFO(REDIRECT_COUNT);
548 #endif
549 HTTP_CURL_INFO(SIZE_UPLOAD);
550 HTTP_CURL_INFO(SIZE_DOWNLOAD);
551 HTTP_CURL_INFO(SPEED_DOWNLOAD);
552 HTTP_CURL_INFO(SPEED_UPLOAD);
553 HTTP_CURL_INFO(HEADER_SIZE);
554 HTTP_CURL_INFO(REQUEST_SIZE);
555 HTTP_CURL_INFO(SSL_VERIFYRESULT);
556 #if LIBCURL_VERSION_NUM >= 0x070c03
557 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
558 #endif
559 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD);
560 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD);
561 HTTP_CURL_INFO(CONTENT_TYPE);
562 #if LIBCURL_VERSION_NUM >= 0x070a03
563 /*HTTP_CURL_INFO(PRIVATE);*/
564 #endif
565 #if LIBCURL_VERSION_NUM >= 0x070a08
566 HTTP_CURL_INFO(HTTPAUTH_AVAIL);
567 HTTP_CURL_INFO(PROXYAUTH_AVAIL);
568 #endif
569 #if LIBCURL_VERSION_NUM >= 0x070c02
570 /*HTTP_CURL_INFO(OS_ERRNO);*/
571 #endif
572 #if LIBCURL_VERSION_NUM >= 0x070c03
573 HTTP_CURL_INFO(NUM_CONNECTS);
574 #endif
575 }
576 } else {
577 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not perform request: %s", curl_easy_strerror(result));
578 status = FAILURE;
579 }
580
581 http_request_end:
582 /* free strings copied with http_curl_copystr() */
583 zend_llist_clean(&HTTP_G(to_free));
584
585 /* clean curl handle if acquired */
586 if (clean_curl) {
587 curl_easy_cleanup(ch);
588 ch = NULL;
589 }
590
591 /* finalize response */
592 if (response) {
593 phpstr_fix(PHPSTR(response));
594 }
595
596 return status;
597 }
598 /* }}} */
599
600 /* {{{ char *http_request_method_string(http_request_method) */
601 PHP_HTTP_API const char *_http_request_method_string(http_request_method m)
602 {
603 if ((m > HTTP_NO_REQUEST_METHOD) && (m < HTTP_MAX_REQUEST_METHOD)) {
604 return http_request_methods[m];
605 }
606 return http_request_methods[0];
607 }
608 /* }}} */
609
610 /* {{{ char *http_request_methods[] */
611 static const char *const http_request_methods[] = {
612 "UNKOWN",
613 /* HTTP/1.1 */
614 "GET",
615 "HEAD",
616 "POST",
617 "PUT",
618 "DELETE",
619 "OPTIONS",
620 "TRACE",
621 "CONNECT",
622 /* WebDAV - RFC 2518 */
623 "PROPFIND",
624 "PROPPATCH",
625 "MKCOL",
626 "COPY",
627 "MOVE",
628 "LOCK",
629 "UNLOCK",
630 /* WebDAV Versioning - RFC 3253 */
631 "VERSION-CONTROL",
632 "REPORT",
633 "CHECKOUT",
634 "CHECKIN",
635 "UNCHECKOUT",
636 "MKWORKSPACE",
637 "UPDATE",
638 "LABEL",
639 "MERGE",
640 "BASELINE-CONTROL",
641 "MKACTIVITY",
642 /* WebDAV Access Control - RFC 3744 */
643 "ACL",
644 NULL
645 };
646 /* }}} */
647
648 /* {{{ static inline char *http_curl_copystr(char *) */
649 static inline char *_http_curl_copystr(const char *str TSRMLS_DC)
650 {
651 char *new_str = estrdup(str);
652 zend_llist_add_element(&HTTP_G(to_free), &new_str);
653 return new_str;
654 }
655 /* }}} */
656
657 /* {{{ static size_t http_curl_write_callback(char *, size_t, size_t, void *) */
658 static size_t http_curl_write_callback(char *buf, size_t len, size_t n, void *s)
659 {
660 return s ? phpstr_append(PHPSTR(s), buf, len * n) : len * n;
661 }
662 /* }}} */
663
664 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
665 static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *s)
666 {
667 static char *offset = NULL, *original = NULL;
668 http_request_body *body = (http_request_body *) s;
669
670 switch (body->type)
671 {
672 case HTTP_REQUEST_BODY_UPLOADFILE:
673 {
674 TSRMLS_FETCH();
675 return php_stream_read((php_stream *) body->data, data, len * n);
676 }
677 break;
678
679 case HTTP_REQUEST_BODY_UPLOADDATA:
680 {
681 size_t avail;
682 if (original != s) {
683 original = offset = s;
684 }
685 if ((avail = body->size - (offset - original)) < 1) {
686 return 0;
687 }
688 if (avail < (len * n)) {
689 memcpy(data, offset, avail);
690 offset += avail;
691 return avail;
692 } else {
693 memcpy(data, offset, len * n);
694 offset += len * n;
695 return len * n;
696 }
697 }
698 break;
699
700 default:
701 return 0;
702 break;
703 }
704 }
705 /* }}} */
706
707 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
708 static int http_curl_progress_callback(void *data, double dltotal, double dlnow, double ultotal, double ulnow)
709 {
710 zval *params_pass[4], params_local[4], retval, *func = (zval *) data;
711 TSRMLS_FETCH();
712
713 params_pass[0] = &params_local[0];
714 params_pass[1] = &params_local[1];
715 params_pass[2] = &params_local[2];
716 params_pass[3] = &params_local[3];
717
718 ZVAL_DOUBLE(params_pass[0], dltotal);
719 ZVAL_DOUBLE(params_pass[1], dlnow);
720 ZVAL_DOUBLE(params_pass[2], ultotal);
721 ZVAL_DOUBLE(params_pass[3], ulnow);
722
723 return call_user_function(EG(function_table), NULL, func, &retval, 4, params_pass TSRMLS_CC);
724 }
725 /* }}} */
726
727 static int http_curl_debug_callback(CURL *ch, curl_infotype type, char *string, size_t length, void *data)
728 {
729 zval *params_pass[2], params_local[2], retval, *func = (zval *) data;
730 TSRMLS_FETCH();
731
732 params_pass[0] = &params_local[0];
733 params_pass[1] = &params_local[1];
734
735 ZVAL_LONG(params_pass[0], type);
736 ZVAL_STRINGL(params_pass[1], string, length, 1);
737
738 call_user_function(EG(function_table), NULL, func, &retval, 2, params_pass TSRMLS_CC);
739
740 return 0;
741 }
742 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
743 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
744 {
745 zval **zoption;
746
747 if (!options || (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))) {
748 return NULL;
749 }
750
751 if (Z_TYPE_PP(zoption) != type) {
752 switch (type)
753 {
754 case IS_BOOL: convert_to_boolean_ex(zoption); break;
755 case IS_LONG: convert_to_long_ex(zoption); break;
756 case IS_DOUBLE: convert_to_double_ex(zoption); break;
757 case IS_STRING: convert_to_string_ex(zoption); break;
758 case IS_ARRAY: convert_to_array_ex(zoption); break;
759 case IS_OBJECT: convert_to_object_ex(zoption); break;
760 default:
761 break;
762 }
763 }
764
765 return *zoption;
766 }
767 /* }}} */
768
769 /*
770 * Local variables:
771 * tab-width: 4
772 * c-basic-offset: 4
773 * End:
774 * vim600: noet sw=4 ts=4 fdm=marker
775 * vim<600: noet sw=4 ts=4
776 */
777