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