remove 90% of the request method cruft; request methods are now simple strings
[m6w6/ext-http] / php_http_curl.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #if PHP_HTTP_HAVE_CURL
16
17 #include <curl/curl.h>
18 #define PHP_HTTP_CURL_VERSION(x, y, z) (LIBCURL_VERSION_NUM >= (((x)<<16) + ((y)<<8) + (z)))
19
20 #if PHP_HTTP_HAVE_EVENT
21 # include <event.h>
22 #endif
23
24 typedef struct php_http_curl_request {
25 CURL *handle;
26
27 struct {
28 HashTable cache;
29
30 struct curl_slist *headers;
31 php_http_buffer_t cookies;
32
33 long redirects;
34
35 struct {
36 uint count;
37 double delay;
38 } retry;
39
40 } options;
41
42 php_http_request_progress_t progress;
43
44 } php_http_curl_request_t;
45
46 typedef struct php_http_curl_request_storage {
47 char *url;
48 char *cookiestore;
49 char errorbuffer[0x100];
50 } php_http_curl_request_storage_t;
51
52 typedef struct php_http_curl_request_pool {
53 CURLM *handle;
54
55 int unfinished; /* int because of curl_multi_perform() */
56
57 #if PHP_HTTP_HAVE_EVENT
58 struct event *timeout;
59 unsigned useevents:1;
60 unsigned runsocket:1;
61 #endif
62 } php_http_curl_request_pool_t;
63
64 typedef struct php_http_curl_request_datashare {
65 CURLSH *handle;
66 } php_http_curl_request_datashare_t;
67
68 #define PHP_HTTP_CURL_OPT_STRING(OPTION, ldiff, obdc) \
69 { \
70 char *K = #OPTION; \
71 PHP_HTTP_CURL_OPT_STRING_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION, obdc); \
72 }
73 #define PHP_HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
74 if (!strcasecmp(key.str, keyname)) { \
75 zval *copy = cache_option(&curl->options.cache, keyname, strlen(keyname)+1, 0, php_http_ztyp(IS_STRING, *param)); \
76 if (obdc) { \
77 if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(copy) TSRMLS_CC)) { \
78 return FAILURE; \
79 } \
80 } \
81 curl_easy_setopt(ch, optname, Z_STRVAL_P(copy)); \
82 zval_ptr_dtor(&copy); \
83 continue; \
84 }
85 #define PHP_HTTP_CURL_OPT_LONG(OPTION, ldiff) \
86 { \
87 char *K = #OPTION; \
88 PHP_HTTP_CURL_OPT_LONG_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION); \
89 }
90 #define PHP_HTTP_CURL_OPT_LONG_EX(keyname, optname) \
91 if (!strcasecmp(key.str, keyname)) { \
92 zval *copy = php_http_ztyp(IS_LONG, *param); \
93 curl_easy_setopt(ch, optname, Z_LVAL_P(copy)); \
94 zval_ptr_dtor(&copy); \
95 continue; \
96 }
97
98 static inline php_http_curl_request_storage_t *get_storage(CURL *ch) {
99 php_http_curl_request_storage_t *st = NULL;
100
101 curl_easy_getinfo(ch, CURLINFO_PRIVATE, &st);
102
103 if (!st) {
104 st = pecalloc(1, sizeof(*st), 1);
105 curl_easy_setopt(ch, CURLOPT_PRIVATE, st);
106 curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, st->errorbuffer);
107 }
108
109 return st;
110 }
111
112 /* resource_factory ops */
113
114 static void *php_http_curl_ctor(void *opaque TSRMLS_DC)
115 {
116 void *ch;
117
118 if ((ch = curl_easy_init())) {
119 get_storage(ch);
120 return ch;
121 }
122 return NULL;
123 }
124
125 static void *php_http_curl_copy(void *opaque, void *handle TSRMLS_DC)
126 {
127 void *ch;
128
129 if ((ch = curl_easy_duphandle(handle))) {
130 curl_easy_reset(ch);
131 get_storage(ch);
132 return ch;
133 }
134 return NULL;
135 }
136
137 static void php_http_curl_dtor(void *opaque, void *handle TSRMLS_DC)
138 {
139 php_http_curl_request_storage_t *st = get_storage(handle);
140
141 curl_easy_cleanup(handle);
142
143 if (st) {
144 if (st->url) {
145 pefree(st->url, 1);
146 }
147 if (st->cookiestore) {
148 pefree(st->cookiestore, 1);
149 }
150 pefree(st, 1);
151 }
152 }
153
154 static void *php_http_curlm_ctor(void *opaque TSRMLS_DC)
155 {
156 return curl_multi_init();
157 }
158
159 static void php_http_curlm_dtor(void *opaque, void *handle TSRMLS_DC)
160 {
161 curl_multi_cleanup(handle);
162 }
163
164 static void *php_http_curlsh_ctor(void *opaque TSRMLS_DC)
165 {
166 return curl_share_init();
167 }
168
169 static void php_http_curlsh_dtor(void *opaque, void *handle TSRMLS_DC)
170 {
171 curl_share_cleanup(handle);
172 }
173
174 /* callbacks */
175
176 static size_t php_http_curl_read_callback(void *data, size_t len, size_t n, void *ctx)
177 {
178 php_http_message_body_t *body = ctx;
179
180 if (body) {
181 TSRMLS_FETCH_FROM_CTX(body->ts);
182 return php_stream_read(php_http_message_body_stream(body), data, len * n);
183 }
184 return 0;
185 }
186
187 static int php_http_curl_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow)
188 {
189 php_http_request_t *h = ctx;
190 php_http_curl_request_t *curl = h->ctx;
191 TSRMLS_FETCH_FROM_CTX(h->ts);
192
193 curl->progress.state.dl.total = dltotal;
194 curl->progress.state.dl.now = dlnow;
195 curl->progress.state.ul.total = ultotal;
196 curl->progress.state.ul.now = ulnow;
197
198 php_http_request_progress_notify(&curl->progress TSRMLS_CC);
199
200 return 0;
201 }
202
203 static curlioerr php_http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx)
204 {
205 php_http_message_body_t *body = ctx;
206
207 if (cmd != CURLIOCMD_RESTARTREAD) {
208 return CURLIOE_UNKNOWNCMD;
209 }
210
211 if (body) {
212 TSRMLS_FETCH_FROM_CTX(body->ts);
213
214 if (SUCCESS == php_stream_rewind(php_http_message_body_stream(body))) {
215 return CURLIOE_OK;
216 }
217 }
218
219 return CURLIOE_FAILRESTART;
220 }
221
222 static int php_http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
223 {
224 php_http_request_t *h = ctx;
225 php_http_curl_request_t *curl = h->ctx;
226 unsigned flags = 0;
227 TSRMLS_FETCH_FROM_CTX(h->ts);
228
229 /* catch progress */
230 switch (type) {
231 case CURLINFO_TEXT:
232 if (php_memnstr(data, ZEND_STRL("About to connect"), data + length)) {
233 curl->progress.state.info = "resolve";
234 } else if (php_memnstr(data, ZEND_STRL("Trying"), data + length)) {
235 curl->progress.state.info = "connect";
236 } else if (php_memnstr(data, ZEND_STRL("Connected"), data + length)) {
237 curl->progress.state.info = "connected";
238 } else if (php_memnstr(data, ZEND_STRL("left intact"), data + length)) {
239 curl->progress.state.info = "not disconnected";
240 } else if (php_memnstr(data, ZEND_STRL("closed"), data + length)) {
241 curl->progress.state.info = "disconnected";
242 } else if (php_memnstr(data, ZEND_STRL("Issue another request"), data + length)) {
243 curl->progress.state.info = "redirect";
244 }
245 php_http_request_progress_notify(&curl->progress TSRMLS_CC);
246 break;
247 case CURLINFO_HEADER_OUT:
248 case CURLINFO_DATA_OUT:
249 case CURLINFO_SSL_DATA_OUT:
250 curl->progress.state.info = "send";
251 break;
252 case CURLINFO_HEADER_IN:
253 case CURLINFO_DATA_IN:
254 case CURLINFO_SSL_DATA_IN:
255 curl->progress.state.info = "receive";
256 break;
257 default:
258 break;
259 }
260 /* process data */
261 switch (type) {
262 case CURLINFO_HEADER_IN:
263 case CURLINFO_DATA_IN:
264 case CURLINFO_HEADER_OUT:
265 case CURLINFO_DATA_OUT:
266 php_http_buffer_append(h->buffer, data, length);
267
268 if (curl->options.redirects) {
269 flags |= PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS;
270 }
271
272 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse(h->parser, h->buffer, flags, &h->message)) {
273 return -1;
274 }
275 break;
276 default:
277 break;
278 }
279
280 #if 0
281 /* debug */
282 _dpf(type, data, length);
283 #endif
284
285 return 0;
286 }
287
288 static int php_http_curl_dummy_callback(char *data, size_t n, size_t l, void *s)
289 {
290 return n*l;
291 }
292
293 static STATUS php_http_curl_request_prepare(php_http_request_t *h, const char *meth, const char *url, php_http_message_body_t *body)
294 {
295 php_http_curl_request_t *curl = h->ctx;
296 php_http_curl_request_storage_t *storage = get_storage(curl->handle);
297 TSRMLS_FETCH_FROM_CTX(h->ts);
298
299 storage->errorbuffer[0] = '\0';
300 if (storage->url) {
301 pefree(storage->url, 1);
302 }
303 storage->url = pestrdup(url, 1);
304 curl_easy_setopt(curl->handle, CURLOPT_URL, storage->url);
305
306 /* request method */
307 switch (php_http_request_method_is(meth, 4, "GET", "HEAD", "POST", "PUT")) {
308 case 0:
309 curl_easy_setopt(curl->handle, CURLOPT_HTTPGET, 1L);
310 break;
311
312 case 1:
313 curl_easy_setopt(curl->handle, CURLOPT_NOBODY, 1L);
314 break;
315
316 case 2:
317 curl_easy_setopt(curl->handle, CURLOPT_POST, 1L);
318 break;
319
320 case 3:
321 curl_easy_setopt(curl->handle, CURLOPT_UPLOAD, 1L);
322 break;
323
324 default: {
325 if (meth) {
326 curl_easy_setopt(curl->handle, CURLOPT_CUSTOMREQUEST, meth);
327 } else {
328 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD, "Unsupported request method: '%s' (%s)", meth, url);
329 return FAILURE;
330 }
331 break;
332 }
333 }
334
335 /* attach request body */
336 if (body) {
337 /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
338 * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
339 * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
340 * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
341 * does not allow a request body.
342 */
343 size_t body_size = php_http_message_body_size(body);
344
345 curl_easy_setopt(curl->handle, CURLOPT_IOCTLDATA, body);
346 curl_easy_setopt(curl->handle, CURLOPT_READDATA, body);
347 curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, body_size);
348 curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, body_size);
349 }
350
351 return SUCCESS;
352 }
353
354 static void php_http_curl_request_pool_responsehandler(php_http_request_pool_t *pool)
355 {
356 int remaining = 0;
357 zval **requests;
358 php_http_curl_request_pool_t *curl = pool->ctx;
359 TSRMLS_FETCH_FROM_CTX(pool->ts);
360
361 do {
362 CURLMsg *msg = curl_multi_info_read(curl->handle, &remaining);
363
364 if (msg && CURLMSG_DONE == msg->msg) {
365 zval **request;
366
367 if (CURLE_OK != msg->data.result) {
368 php_http_curl_request_storage_t *st = get_storage(msg->easy_handle);
369 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), STR_PTR(st->errorbuffer), STR_PTR(st->url));
370 }
371
372 php_http_request_pool_requests(pool, &requests, NULL);
373 for (request = requests; *request; ++request) {
374 php_http_request_object_t *obj = zend_object_store_get_object(*request TSRMLS_CC);
375
376 if (msg->easy_handle == ((php_http_curl_request_t *) (obj->request->ctx))->handle) {
377 Z_ADDREF_PP(request);
378 zend_llist_add_element(&pool->requests.finished, request);
379 php_http_request_object_responsehandler(obj, *request TSRMLS_CC);
380 }
381
382 zval_ptr_dtor(request);
383 }
384 efree(requests);
385 }
386 } while (remaining);
387 }
388
389
390 #if PHP_HTTP_HAVE_EVENT
391
392 typedef struct php_http_request_pool_event {
393 struct event evnt;
394 php_http_request_pool_t *pool;
395 } php_http_request_pool_event_t;
396
397 static inline int etoca(short action) {
398 switch (action & (EV_READ|EV_WRITE)) {
399 case EV_READ:
400 return CURL_CSELECT_IN;
401 break;
402 case EV_WRITE:
403 return CURL_CSELECT_OUT;
404 break;
405 case EV_READ|EV_WRITE:
406 return CURL_CSELECT_IN|CURL_CSELECT_OUT;
407 break;
408 default:
409 return 0;
410 }
411 }
412
413 static void php_http_curl_request_pool_timeout_callback(int socket, short action, void *event_data)
414 {
415 php_http_request_pool_t *pool = event_data;
416 php_http_curl_request_pool_t *curl = pool->ctx;
417
418 #if DBG_EVENTS
419 fprintf(stderr, "T");
420 #endif
421 if (curl->useevents) {
422 CURLMcode rc;
423 TSRMLS_FETCH_FROM_CTX(pool->ts);
424
425 while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, socket, etoca(action), &curl->unfinished)));
426
427 if (CURLM_OK != rc) {
428 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "%s", curl_multi_strerror(rc));
429 }
430
431 php_http_curl_request_pool_responsehandler(pool);
432 }
433 }
434
435 static void php_http_curl_request_pool_event_callback(int socket, short action, void *event_data)
436 {
437 php_http_request_pool_t *pool = event_data;
438 php_http_curl_request_pool_t *curl = pool->ctx;
439
440 #if DBG_EVENTS
441 fprintf(stderr, "E");
442 #endif
443 if (curl->useevents) {
444 CURLMcode rc = CURLE_OK;
445 TSRMLS_FETCH_FROM_CTX(pool->ts);
446
447 while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, socket, etoca(action), &curl->unfinished)));
448
449 if (CURLM_OK != rc) {
450 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "%s", curl_multi_strerror(rc));
451 }
452
453 php_http_curl_request_pool_responsehandler(pool);
454
455 /* remove timeout if there are no transfers left */
456 if (!curl->unfinished && event_initialized(curl->timeout) && event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
457 event_del(curl->timeout);
458 }
459 }
460 }
461
462 static int php_http_curl_request_pool_socket_callback(CURL *easy, curl_socket_t sock, int action, void *socket_data, void *assign_data)
463 {
464 php_http_request_pool_t *pool = socket_data;
465 php_http_curl_request_pool_t *curl = pool->ctx;
466
467 #if DBG_EVENTS
468 fprintf(stderr, "S");
469 #endif
470 if (curl->useevents) {
471 int events = EV_PERSIST;
472 php_http_request_pool_event_t *ev = assign_data;
473 TSRMLS_FETCH_FROM_CTX(pool->ts);
474
475 if (!ev) {
476 ev = ecalloc(1, sizeof(php_http_request_pool_event_t));
477 ev->pool = pool;
478 curl_multi_assign(curl->handle, sock, ev);
479 event_base_set(PHP_HTTP_G->curl.event_base, &ev->evnt);
480 } else {
481 event_del(&ev->evnt);
482 }
483
484 switch (action) {
485 case CURL_POLL_IN:
486 events |= EV_READ;
487 break;
488 case CURL_POLL_OUT:
489 events |= EV_WRITE;
490 break;
491 case CURL_POLL_INOUT:
492 events |= EV_READ|EV_WRITE;
493 break;
494
495 case CURL_POLL_REMOVE:
496 efree(ev);
497 /* no break */
498 case CURL_POLL_NONE:
499 return 0;
500
501 default:
502 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "Unknown socket action %d", action);
503 return -1;
504 }
505
506 event_set(&ev->evnt, sock, events, php_http_curl_request_pool_event_callback, pool);
507 event_add(&ev->evnt, NULL);
508 }
509
510 return 0;
511 }
512
513 static void php_http_curl_request_pool_timer_callback(CURLM *multi, long timeout_ms, void *timer_data)
514 {
515 php_http_request_pool_t *pool = timer_data;
516 php_http_curl_request_pool_t *curl = pool->ctx;
517
518 #if DBG_EVENTS
519 fprintf(stderr, "%ld", timeout_ms);
520 #endif
521 if (curl->useevents) {
522
523 if (timeout_ms < 0) {
524 php_http_curl_request_pool_timeout_callback(CURL_SOCKET_TIMEOUT, CURL_CSELECT_IN|CURL_CSELECT_OUT, pool);
525 } else if (timeout_ms > 0 || !event_initialized(curl->timeout) || !event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
526 struct timeval timeout;
527 TSRMLS_FETCH_FROM_CTX(pool->ts);
528
529 if (!event_initialized(curl->timeout)) {
530 event_set(curl->timeout, -1, 0, php_http_curl_request_pool_timeout_callback, pool);
531 event_base_set(PHP_HTTP_G->curl.event_base, curl->timeout);
532 } else if (event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
533 event_del(curl->timeout);
534 }
535
536 timeout.tv_sec = timeout_ms / 1000;
537 timeout.tv_usec = (timeout_ms % 1000) * 1000;
538
539 event_add(curl->timeout, &timeout);
540 }
541 }
542 }
543
544 #endif /* HAVE_EVENT */
545
546
547 static inline zval *cache_option(HashTable *cache, char *key, size_t keylen, ulong h, zval *opt)
548 {
549 Z_ADDREF_P(opt);
550
551 if (h) {
552 zend_hash_quick_update(cache, key, keylen, h, &opt, sizeof(zval *), NULL);
553 } else {
554 zend_hash_update(cache, key, keylen, &opt, sizeof(zval *), NULL);
555 }
556
557 return opt;
558 }
559
560 static inline zval *get_option(HashTable *cache, HashTable *options, char *key, size_t keylen, int type)
561 {
562 if (options) {
563 zval **zoption;
564 ulong h = zend_hash_func(key, keylen);
565
566 if (SUCCESS == zend_hash_quick_find(options, key, keylen, h, (void *) &zoption)) {
567 zval *option = php_http_ztyp(type, *zoption);
568
569 if (cache) {
570 zval *cached = cache_option(cache, key, keylen, h, option);
571
572 zval_ptr_dtor(&option);
573 return cached;
574 }
575 return option;
576 }
577 }
578
579 return NULL;
580 }
581
582 static STATUS set_options(php_http_request_t *h, HashTable *options)
583 {
584 zval *zoption;
585 int range_req = 0;
586 php_http_curl_request_t *curl = h->ctx;
587 CURL *ch = curl->handle;
588 TSRMLS_FETCH_FROM_CTX(h->ts);
589
590 /* proxy */
591 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyhost"), IS_STRING))) {
592 curl_easy_setopt(ch, CURLOPT_PROXY, Z_STRVAL_P(zoption));
593 /* type */
594 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytype"), IS_LONG))) {
595 curl_easy_setopt(ch, CURLOPT_PROXYTYPE, Z_LVAL_P(zoption));
596 }
597 /* port */
598 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyport"), IS_LONG))) {
599 curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
600 }
601 /* user:pass */
602 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauth"), IS_STRING)) && Z_STRLEN_P(zoption)) {
603 curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption));
604 }
605 /* auth method */
606 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauthtype"), IS_LONG))) {
607 curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
608 }
609 /* tunnel */
610 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytunnel"), IS_BOOL)) && Z_BVAL_P(zoption)) {
611 curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 1L);
612 }
613 }
614 #if PHP_HTTP_CURL_VERSION(7,19,4)
615 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("noproxy"), IS_STRING))) {
616 curl_easy_setopt(ch, CURLOPT_NOPROXY, Z_STRVAL_P(zoption));
617 }
618 #endif
619
620 /* dns */
621 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("dns_cache_timeout"), IS_LONG))) {
622 curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, Z_LVAL_P(zoption));
623 }
624 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ipresolve"), IS_LONG)) && Z_LVAL_P(zoption)) {
625 curl_easy_setopt(ch, CURLOPT_IPRESOLVE, Z_LVAL_P(zoption));
626 }
627
628 /* limits */
629 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_limit"), IS_LONG))) {
630 curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, Z_LVAL_P(zoption));
631 }
632 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_time"), IS_LONG))) {
633 curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, Z_LVAL_P(zoption));
634 }
635 /* LSF weirdance
636 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_send_speed"), IS_LONG))) {
637 curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
638 }
639 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_recv_speed"), IS_LONG))) {
640 curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
641 }
642 */
643 /* crashes
644 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("maxconnects"), IS_LONG))) {
645 curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, Z_LVAL_P(zoption));
646 } */
647 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("fresh_connect"), IS_BOOL)) && Z_BVAL_P(zoption)) {
648 curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
649 }
650 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("forbid_reuse"), IS_BOOL)) && Z_BVAL_P(zoption)) {
651 curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 1L);
652 }
653
654 /* outgoing interface */
655 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("interface"), IS_STRING))) {
656 curl_easy_setopt(ch, CURLOPT_INTERFACE, Z_STRVAL_P(zoption));
657
658 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("portrange"), IS_ARRAY))) {
659 zval **prs, **pre;
660
661 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
662 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &prs)) {
663 zend_hash_move_forward(Z_ARRVAL_P(zoption));
664 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &pre)) {
665 zval *prs_cpy = php_http_ztyp(IS_LONG, *prs);
666 zval *pre_cpy = php_http_ztyp(IS_LONG, *pre);
667
668 if (Z_LVAL_P(prs_cpy) && Z_LVAL_P(pre_cpy)) {
669 curl_easy_setopt(ch, CURLOPT_LOCALPORT, MIN(Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy)));
670 curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, labs(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L);
671 }
672 zval_ptr_dtor(&prs_cpy);
673 zval_ptr_dtor(&pre_cpy);
674 }
675 }
676 }
677 }
678
679 /* another port */
680 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("port"), IS_LONG))) {
681 curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption));
682 }
683
684 /* RFC4007 zone_id */
685 #if PHP_HTTP_CURL_VERSION(7,19,0)
686 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("address_scope"), IS_LONG))) {
687 curl_easy_setopt(ch, CURLOPT_ADDRESS_SCOPE, Z_LVAL_P(zoption));
688 }
689 #endif
690
691 /* auth */
692 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauth"), IS_STRING)) && Z_STRLEN_P(zoption)) {
693 curl_easy_setopt(ch, CURLOPT_USERPWD, Z_STRVAL_P(zoption));
694 }
695 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauthtype"), IS_LONG))) {
696 curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
697 }
698
699 /* redirects, defaults to 0 */
700 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("redirect"), IS_LONG))) {
701 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1L : 0L);
702 curl_easy_setopt(ch, CURLOPT_MAXREDIRS, curl->options.redirects = Z_LVAL_P(zoption));
703 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("unrestrictedauth"), IS_BOOL))) {
704 curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
705 }
706 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("postredir"), IS_BOOL))) {
707 #if PHP_HTTP_CURL_VERSION(7,19,1)
708 curl_easy_setopt(ch, CURLOPT_POSTREDIR, Z_BVAL_P(zoption) ? 1L : 0L);
709 #else
710 curl_easy_setopt(ch, CURLOPT_POST301, Z_BVAL_P(zoption) ? 1L : 0L);
711 #endif
712 }
713 } else {
714 curl->options.redirects = 0;
715 }
716
717 /* retries, defaults to 0 */
718 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrycount"), IS_LONG))) {
719 curl->options.retry.count = Z_LVAL_P(zoption);
720 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrydelay"), IS_DOUBLE))) {
721 curl->options.retry.delay = Z_DVAL_P(zoption);
722 } else {
723 curl->options.retry.delay = 0;
724 }
725 } else {
726 curl->options.retry.count = 0;
727 }
728
729 /* referer */
730 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("referer"), IS_STRING)) && Z_STRLEN_P(zoption)) {
731 curl_easy_setopt(ch, CURLOPT_REFERER, Z_STRVAL_P(zoption));
732 }
733
734 /* useragent, default "PECL::HTTP/version (PHP/version)" */
735 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("useragent"), IS_STRING))) {
736 /* allow to send no user agent, not even default one */
737 if (Z_STRLEN_P(zoption)) {
738 curl_easy_setopt(ch, CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
739 } else {
740 curl_easy_setopt(ch, CURLOPT_USERAGENT, NULL);
741 }
742 }
743
744 /* resume */
745 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("resume"), IS_LONG)) && (Z_LVAL_P(zoption) > 0)) {
746 range_req = 1;
747 curl_easy_setopt(ch, CURLOPT_RESUME_FROM, Z_LVAL_P(zoption));
748 }
749 /* or range of kind array(array(0,499), array(100,1499)) */
750 else if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("range"), IS_ARRAY)) && zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
751 HashPosition pos1, pos2;
752 zval **rr, **rb, **re;
753 php_http_buffer_t rs;
754
755 php_http_buffer_init(&rs);
756 FOREACH_VAL(pos1, zoption, rr) {
757 if (Z_TYPE_PP(rr) == IS_ARRAY) {
758 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr), &pos2);
759 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &rb, &pos2)) {
760 zend_hash_move_forward_ex(Z_ARRVAL_PP(rr), &pos2);
761 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &re, &pos2)) {
762 if ( ((Z_TYPE_PP(rb) == IS_LONG) || ((Z_TYPE_PP(rb) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(rb), Z_STRLEN_PP(rb), NULL, NULL, 1))) &&
763 ((Z_TYPE_PP(re) == IS_LONG) || ((Z_TYPE_PP(re) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(re), Z_STRLEN_PP(re), NULL, NULL, 1)))) {
764 zval *rbl = php_http_ztyp(IS_LONG, *rb);
765 zval *rel = php_http_ztyp(IS_LONG, *re);
766
767 if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) {
768 php_http_buffer_appendf(&rs, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel));
769 }
770 zval_ptr_dtor(&rbl);
771 zval_ptr_dtor(&rel);
772 }
773 }
774 }
775 }
776 }
777
778 if (PHP_HTTP_BUFFER_LEN(&rs)) {
779 zval *cached_range;
780
781 /* ditch last comma */
782 PHP_HTTP_BUFFER_VAL(&rs)[PHP_HTTP_BUFFER_LEN(&rs)-- -1] = '\0';
783 /* cache string */
784 MAKE_STD_ZVAL(cached_range);
785 ZVAL_STRINGL(cached_range, PHP_HTTP_BUFFER_VAL(&rs), PHP_HTTP_BUFFER_LEN(&rs), 0);
786 curl_easy_setopt(ch, CURLOPT_RANGE, Z_STRVAL_P(cache_option(&curl->options.cache, ZEND_STRS("range"), 0, cached_range)));
787 zval_ptr_dtor(&cached_range);
788 }
789 }
790
791 /* additional headers, array('name' => 'value') */
792 if (curl->options.headers) {
793 curl_slist_free_all(curl->options.headers);
794 curl->options.headers = NULL;
795 }
796 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("headers"), IS_ARRAY))) {
797 php_http_array_hashkey_t header_key = php_http_array_hashkey_init(0);
798 zval **header_val;
799 HashPosition pos;
800 php_http_buffer_t header;
801
802 php_http_buffer_init(&header);
803 FOREACH_KEYVAL(pos, zoption, header_key, header_val) {
804 if (header_key.type == HASH_KEY_IS_STRING) {
805 zval *header_cpy = php_http_ztyp(IS_STRING, *header_val);
806
807 if (!strcasecmp(header_key.str, "range")) {
808 range_req = 1;
809 }
810
811 php_http_buffer_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(header_cpy));
812 php_http_buffer_fix(&header);
813 curl->options.headers = curl_slist_append(curl->options.headers, PHP_HTTP_BUFFER_VAL(&header));
814 php_http_buffer_reset(&header);
815
816 zval_ptr_dtor(&header_cpy);
817 }
818 }
819 php_http_buffer_dtor(&header);
820 }
821 /* etag */
822 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("etag"), IS_STRING)) && Z_STRLEN_P(zoption)) {
823 zend_bool is_quoted = !((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"'));
824 php_http_buffer_t header;
825
826 php_http_buffer_init(&header);
827 php_http_buffer_appendf(&header, is_quoted?"%s: %s":"%s: \"%s\"", range_req?"If-Match":"If-None-Match", Z_STRVAL_P(zoption));
828 php_http_buffer_fix(&header);
829 curl->options.headers = curl_slist_append(curl->options.headers, PHP_HTTP_BUFFER_VAL(&header));
830 php_http_buffer_dtor(&header);
831 }
832 /* compression */
833 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("compress"), IS_BOOL)) && Z_LVAL_P(zoption)) {
834 curl->options.headers = curl_slist_append(curl->options.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
835 }
836 curl_easy_setopt(ch, CURLOPT_HTTPHEADER, curl->options.headers);
837
838 /* lastmodified */
839 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("lastmodified"), IS_LONG))) {
840 if (Z_LVAL_P(zoption)) {
841 if (Z_LVAL_P(zoption) > 0) {
842 curl_easy_setopt(ch, CURLOPT_TIMEVALUE, Z_LVAL_P(zoption));
843 } else {
844 curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) PHP_HTTP_G->env.request.time + Z_LVAL_P(zoption));
845 }
846 curl_easy_setopt(ch, CURLOPT_TIMECONDITION, (long) (range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE));
847 } else {
848 curl_easy_setopt(ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
849 }
850 }
851
852 /* cookies, array('name' => 'value') */
853 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookies"), IS_ARRAY))) {
854 php_http_buffer_dtor(&curl->options.cookies);
855 if (zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
856 zval *urlenc_cookies = NULL;
857 /* check whether cookies should not be urlencoded; default is to urlencode them */
858 if ((!(urlenc_cookies = get_option(&curl->options.cache, options, ZEND_STRS("encodecookies"), IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) {
859 if (SUCCESS == php_http_url_encode_hash_ex(HASH_OF(zoption), &curl->options.cookies, ZEND_STRS(";"), ZEND_STRS("="), NULL, 0 TSRMLS_CC)) {
860 php_http_buffer_fix(&curl->options.cookies);
861 curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data);
862 }
863 } else {
864 HashPosition pos;
865 php_http_array_hashkey_t cookie_key = php_http_array_hashkey_init(0);
866 zval **cookie_val;
867
868 FOREACH_KEYVAL(pos, zoption, cookie_key, cookie_val) {
869 if (cookie_key.type == HASH_KEY_IS_STRING) {
870 zval *val = php_http_ztyp(IS_STRING, *cookie_val);
871 php_http_buffer_appendf(&curl->options.cookies, "%s=%s; ", cookie_key.str, Z_STRVAL_P(val));
872 zval_ptr_dtor(&val);
873 }
874 }
875
876 php_http_buffer_fix(&curl->options.cookies);
877 if (PHP_HTTP_BUFFER_LEN(&curl->options.cookies)) {
878 curl_easy_setopt(ch, CURLOPT_COOKIE, PHP_HTTP_BUFFER_VAL(&curl->options.cookies));
879 }
880 }
881 }
882 }
883
884 /* don't load session cookies from cookiestore */
885 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiesession"), IS_BOOL)) && Z_BVAL_P(zoption)) {
886 curl_easy_setopt(ch, CURLOPT_COOKIESESSION, 1L);
887 }
888
889 /* cookiestore, read initial cookies from that file and store cookies back into that file */
890 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiestore"), IS_STRING))) {
891 php_http_curl_request_storage_t *storage = get_storage(curl->handle);
892
893 if (Z_STRLEN_P(zoption)) {
894 if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(zoption) TSRMLS_CC)) {
895 return FAILURE;
896 }
897 }
898 if (storage->cookiestore) {
899 pefree(storage->cookiestore, 1);
900 }
901 storage->cookiestore = pestrndup(Z_STRVAL_P(zoption), Z_STRLEN_P(zoption), 1);
902 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, storage->cookiestore);
903 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, storage->cookiestore);
904 }
905
906 /* maxfilesize */
907 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("maxfilesize"), IS_LONG))) {
908 curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption));
909 }
910
911 /* http protocol */
912 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("protocol"), IS_LONG))) {
913 curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, Z_LVAL_P(zoption));
914 }
915
916 /* timeout, defaults to 0 */
917 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("timeout"), IS_DOUBLE))) {
918 curl_easy_setopt(ch, CURLOPT_TIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
919 }
920 /* connecttimeout, defaults to 0 */
921 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("connecttimeout"), IS_DOUBLE))) {
922 curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
923 }
924
925 /* ssl */
926 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ssl"), IS_ARRAY))) {
927 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
928 zval **param;
929 HashPosition pos;
930
931 FOREACH_KEYVAL(pos, zoption, key, param) {
932 if (key.type == HASH_KEY_IS_STRING) {
933 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT, 0, 1);
934 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE, 0, 0);
935 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD, 0, 0);
936
937 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY, 0, 0);
938 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE, 0, 0);
939 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD, 0, 0);
940
941 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE, 0, 0);
942 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION, 0);
943
944 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER, 1);
945 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST, 1);
946 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST, 1, 0);
947
948 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAINFO, -3, 1);
949 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAPATH, -3, 1);
950 PHP_HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE, -3, 1);
951 PHP_HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET, -3, 1);
952 #if PHP_HTTP_CURL_VERSION(7,19,0)
953 PHP_HTTP_CURL_OPT_STRING(CURLOPT_ISSUERCERT, -3, 1);
954 #if defined(PHP_HTTP_HAVE_OPENSSL)
955 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CRLFILE, -3, 1);
956 #endif
957 #endif
958 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
959 PHP_HTTP_CURL_OPT_LONG(CURLOPT_CERTINFO, -3);
960 #endif
961 }
962 }
963 }
964 return SUCCESS;
965 }
966
967 static STATUS get_info(CURL *ch, HashTable *info)
968 {
969 char *c;
970 long l;
971 double d;
972 struct curl_slist *s, *p;
973 zval *subarray, array;
974 INIT_PZVAL_ARRAY(&array, info);
975
976 /* BEGIN::CURLINFO */
977 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_EFFECTIVE_URL, &c)) {
978 add_assoc_string_ex(&array, "effective_url", sizeof("effective_url"), c ? c : "", 1);
979 }
980 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &l)) {
981 add_assoc_long_ex(&array, "response_code", sizeof("response_code"), l);
982 }
983 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TOTAL_TIME, &d)) {
984 add_assoc_double_ex(&array, "total_time", sizeof("total_time"), d);
985 }
986 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NAMELOOKUP_TIME, &d)) {
987 add_assoc_double_ex(&array, "namelookup_time", sizeof("namelookup_time"), d);
988 }
989 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONNECT_TIME, &d)) {
990 add_assoc_double_ex(&array, "connect_time", sizeof("connect_time"), d);
991 }
992 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRETRANSFER_TIME, &d)) {
993 add_assoc_double_ex(&array, "pretransfer_time", sizeof("pretransfer_time"), d);
994 }
995 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_UPLOAD, &d)) {
996 add_assoc_double_ex(&array, "size_upload", sizeof("size_upload"), d);
997 }
998 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_DOWNLOAD, &d)) {
999 add_assoc_double_ex(&array, "size_download", sizeof("size_download"), d);
1000 }
1001 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_DOWNLOAD, &d)) {
1002 add_assoc_double_ex(&array, "speed_download", sizeof("speed_download"), d);
1003 }
1004 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_UPLOAD, &d)) {
1005 add_assoc_double_ex(&array, "speed_upload", sizeof("speed_upload"), d);
1006 }
1007 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HEADER_SIZE, &l)) {
1008 add_assoc_long_ex(&array, "header_size", sizeof("header_size"), l);
1009 }
1010 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REQUEST_SIZE, &l)) {
1011 add_assoc_long_ex(&array, "request_size", sizeof("request_size"), l);
1012 }
1013 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_VERIFYRESULT, &l)) {
1014 add_assoc_long_ex(&array, "ssl_verifyresult", sizeof("ssl_verifyresult"), l);
1015 }
1016 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_FILETIME, &l)) {
1017 add_assoc_long_ex(&array, "filetime", sizeof("filetime"), l);
1018 }
1019 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) {
1020 add_assoc_double_ex(&array, "content_length_download", sizeof("content_length_download"), d);
1021 }
1022 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_UPLOAD, &d)) {
1023 add_assoc_double_ex(&array, "content_length_upload", sizeof("content_length_upload"), d);
1024 }
1025 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_STARTTRANSFER_TIME, &d)) {
1026 add_assoc_double_ex(&array, "starttransfer_time", sizeof("starttransfer_time"), d);
1027 }
1028 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_TYPE, &c)) {
1029 add_assoc_string_ex(&array, "content_type", sizeof("content_type"), c ? c : "", 1);
1030 }
1031 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_TIME, &d)) {
1032 add_assoc_double_ex(&array, "redirect_time", sizeof("redirect_time"), d);
1033 }
1034 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_COUNT, &l)) {
1035 add_assoc_long_ex(&array, "redirect_count", sizeof("redirect_count"), l);
1036 }
1037 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTP_CONNECTCODE, &l)) {
1038 add_assoc_long_ex(&array, "connect_code", sizeof("connect_code"), l);
1039 }
1040 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTPAUTH_AVAIL, &l)) {
1041 add_assoc_long_ex(&array, "httpauth_avail", sizeof("httpauth_avail"), l);
1042 }
1043 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROXYAUTH_AVAIL, &l)) {
1044 add_assoc_long_ex(&array, "proxyauth_avail", sizeof("proxyauth_avail"), l);
1045 }
1046 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_OS_ERRNO, &l)) {
1047 add_assoc_long_ex(&array, "os_errno", sizeof("os_errno"), l);
1048 }
1049 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NUM_CONNECTS, &l)) {
1050 add_assoc_long_ex(&array, "num_connects", sizeof("num_connects"), l);
1051 }
1052 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_ENGINES, &s)) {
1053 MAKE_STD_ZVAL(subarray);
1054 array_init(subarray);
1055 for (p = s; p; p = p->next) {
1056 if (p->data) {
1057 add_next_index_string(subarray, p->data, 1);
1058 }
1059 }
1060 add_assoc_zval_ex(&array, "ssl_engines", sizeof("ssl_engines"), subarray);
1061 curl_slist_free_all(s);
1062 }
1063 #if PHP_HTTP_CURL_VERSION(7,14,1)
1064 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_COOKIELIST, &s)) {
1065 MAKE_STD_ZVAL(subarray);
1066 array_init(subarray);
1067 for (p = s; p; p = p->next) {
1068 if (p->data) {
1069 add_next_index_string(subarray, p->data, 1);
1070 }
1071 }
1072 add_assoc_zval_ex(&array, "cookies", sizeof("cookies"), subarray);
1073 curl_slist_free_all(s);
1074 }
1075 #endif
1076 #if PHP_HTTP_CURL_VERSION(7,18,2)
1077 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_URL, &c)) {
1078 add_assoc_string_ex(&array, "redirect_url", sizeof("redirect_url"), c ? c : "", 1);
1079 }
1080 #endif
1081 #if PHP_HTTP_CURL_VERSION(7,19,0)
1082 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_IP, &c)) {
1083 add_assoc_string_ex(&array, "primary_ip", sizeof("primary_ip"), c ? c : "", 1);
1084 }
1085 #endif
1086 #if PHP_HTTP_CURL_VERSION(7,19,0)
1087 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_APPCONNECT_TIME, &d)) {
1088 add_assoc_double_ex(&array, "appconnect_time", sizeof("appconnect_time"), d);
1089 }
1090 #endif
1091 #if PHP_HTTP_CURL_VERSION(7,19,4)
1092 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONDITION_UNMET, &l)) {
1093 add_assoc_long_ex(&array, "condition_unmet", sizeof("condition_unmet"), l);
1094 }
1095 #endif
1096 /* END::CURLINFO */
1097 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
1098 {
1099 int i;
1100 zval *ci_array;
1101 struct curl_certinfo *ci;
1102 char *colon, *keyname;
1103
1104 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CERTINFO, &ci)) {
1105 MAKE_STD_ZVAL(ci_array);
1106 array_init(ci_array);
1107
1108 for (i = 0; i < ci->num_of_certs; ++i) {
1109 s = ci->certinfo[i];
1110
1111 MAKE_STD_ZVAL(subarray);
1112 array_init(subarray);
1113 for (p = s; p; p = p->next) {
1114 if (p->data) {
1115 if ((colon = strchr(p->data, ':'))) {
1116 keyname = estrndup(p->data, colon - p->data);
1117 add_assoc_string_ex(subarray, keyname, colon - p->data + 1, colon + 1, 1);
1118 efree(keyname);
1119 } else {
1120 add_next_index_string(subarray, p->data, 1);
1121 }
1122 }
1123 }
1124 add_next_index_zval(ci_array, subarray);
1125 }
1126 add_assoc_zval_ex(&array, "certinfo", sizeof("certinfo"), ci_array);
1127 }
1128 }
1129 #endif
1130 add_assoc_string_ex(&array, "error", sizeof("error"), get_storage(ch)->errorbuffer, 1);
1131
1132 return SUCCESS;
1133 }
1134
1135
1136 /* request datashare handler ops */
1137
1138 static php_http_request_datashare_t *php_http_curl_request_datashare_init(php_http_request_datashare_t *h, void *handle)
1139 {
1140 php_http_curl_request_datashare_t *curl;
1141 TSRMLS_FETCH_FROM_CTX(h->ts);
1142
1143 if (!handle && !(handle = php_http_resource_factory_handle_ctor(h->rf TSRMLS_CC))) {
1144 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "could not initialize curl share handle");
1145 return NULL;
1146 }
1147
1148 curl = ecalloc(1, sizeof(*curl));
1149 curl->handle = handle;
1150 h->ctx = curl;
1151
1152 return h;
1153 }
1154
1155 static void php_http_curl_request_datashare_dtor(php_http_request_datashare_t *h)
1156 {
1157 php_http_curl_request_datashare_t *curl = h->ctx;
1158 TSRMLS_FETCH_FROM_CTX(h->ts);
1159
1160 php_http_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC);
1161
1162 efree(curl);
1163 h->ctx = NULL;
1164 }
1165
1166 static STATUS php_http_curl_request_datashare_attach(php_http_request_datashare_t *h, php_http_request_t *r)
1167 {
1168 CURLcode rc;
1169 php_http_curl_request_datashare_t *curl = h->ctx;
1170 php_http_curl_request_t *recurl = r->ctx;
1171 TSRMLS_FETCH_FROM_CTX(h->ts);
1172
1173 if (CURLE_OK != (rc = curl_easy_setopt(recurl->handle, CURLOPT_SHARE, curl->handle))) {
1174 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not attach request to the datashare: %s", curl_easy_strerror(rc));
1175 return FAILURE;
1176 }
1177 return SUCCESS;
1178 }
1179
1180 static STATUS php_http_curl_request_datashare_detach(php_http_request_datashare_t *h, php_http_request_t *r)
1181 {
1182 CURLcode rc;
1183 php_http_curl_request_t *recurl = r->ctx;
1184 TSRMLS_FETCH_FROM_CTX(h->ts);
1185
1186
1187 if (CURLE_OK != (rc = curl_easy_setopt(recurl->handle, CURLOPT_SHARE, NULL))) {
1188 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not detach request from the datashare: %s", curl_share_strerror(rc));
1189 return FAILURE;
1190 }
1191 return SUCCESS;
1192 }
1193
1194 static STATUS php_http_curl_request_datashare_setopt(php_http_request_datashare_t *h, php_http_request_datashare_setopt_opt_t opt, void *arg)
1195 {
1196 CURLSHcode rc;
1197 php_http_curl_request_datashare_t *curl = h->ctx;
1198
1199 switch (opt) {
1200 case PHP_HTTP_REQUEST_DATASHARE_OPT_COOKIES:
1201 if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE))) {
1202 TSRMLS_FETCH_FROM_CTX(h->ts);
1203
1204 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not %s sharing of cookie data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc));
1205 return FAILURE;
1206 }
1207 break;
1208
1209 case PHP_HTTP_REQUEST_DATASHARE_OPT_RESOLVER:
1210 if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_DNS))) {
1211 TSRMLS_FETCH_FROM_CTX(h->ts);
1212
1213 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not %s sharing of resolver data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc));
1214 return FAILURE;
1215 }
1216 break;
1217
1218 default:
1219 return FAILURE;
1220 }
1221
1222 return SUCCESS;
1223 }
1224
1225 static php_http_resource_factory_ops_t php_http_curlsh_resource_factory_ops = {
1226 php_http_curlsh_ctor,
1227 NULL,
1228 php_http_curlsh_dtor
1229 };
1230
1231 static php_http_request_datashare_ops_t php_http_curl_request_datashare_ops = {
1232 &php_http_curlsh_resource_factory_ops,
1233 php_http_curl_request_datashare_init,
1234 NULL /* copy */,
1235 php_http_curl_request_datashare_dtor,
1236 NULL /*reset */,
1237 php_http_curl_request_datashare_attach,
1238 php_http_curl_request_datashare_detach,
1239 php_http_curl_request_datashare_setopt,
1240 };
1241
1242 PHP_HTTP_API php_http_request_datashare_ops_t *php_http_curl_get_request_datashare_ops(void)
1243 {
1244 return &php_http_curl_request_datashare_ops;
1245 }
1246
1247
1248 /* request pool handler ops */
1249
1250 static php_http_request_pool_t *php_http_curl_request_pool_init(php_http_request_pool_t *h, void *handle)
1251 {
1252 php_http_curl_request_pool_t *curl;
1253 TSRMLS_FETCH_FROM_CTX(h->ts);
1254
1255 if (!handle && !(handle = php_http_resource_factory_handle_ctor(h->rf TSRMLS_CC))) {
1256 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "could not initialize curl pool handle");
1257 return NULL;
1258 }
1259
1260 curl = ecalloc(1, sizeof(*curl));
1261 curl->handle = handle;
1262 curl->unfinished = 0;
1263 h->ctx = curl;
1264
1265 return h;
1266 }
1267
1268 static void php_http_curl_request_pool_dtor(php_http_request_pool_t *h)
1269 {
1270 php_http_curl_request_pool_t *curl = h->ctx;
1271 TSRMLS_FETCH_FROM_CTX(h->ts);
1272
1273 #if PHP_HTTP_HAVE_EVENT
1274 if (curl->timeout) {
1275 efree(curl->timeout);
1276 curl->timeout = NULL;
1277 }
1278 #endif
1279 curl->unfinished = 0;
1280 php_http_request_pool_reset(h);
1281
1282 php_http_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC);
1283
1284 efree(curl);
1285 h->ctx = NULL;
1286 }
1287
1288 static STATUS php_http_curl_request_pool_attach(php_http_request_pool_t *h, php_http_request_t *r, const char *m, const char *url, php_http_message_body_t *body)
1289 {
1290 php_http_curl_request_pool_t *curl = h->ctx;
1291 php_http_curl_request_t *recurl = r->ctx;
1292 CURLMcode rs;
1293 TSRMLS_FETCH_FROM_CTX(h->ts);
1294
1295 if (SUCCESS != php_http_curl_request_prepare(r, m, url, body)) {
1296 return FAILURE;
1297 }
1298
1299 if (CURLM_OK == (rs = curl_multi_add_handle(curl->handle, recurl->handle))) {
1300 ++curl->unfinished;
1301 return SUCCESS;
1302 } else {
1303 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "Could not attach request to pool: %s", curl_multi_strerror(rs));
1304 return FAILURE;
1305 }
1306 }
1307
1308 static STATUS php_http_curl_request_pool_detach(php_http_request_pool_t *h, php_http_request_t *r)
1309 {
1310 php_http_curl_request_pool_t *curl = h->ctx;
1311 php_http_curl_request_t *recurl = r->ctx;
1312 CURLMcode rs = curl_multi_remove_handle(curl->handle, recurl->handle);
1313 TSRMLS_FETCH_FROM_CTX(h->ts);
1314
1315 if (CURLM_OK == rs) {
1316 return SUCCESS;
1317 } else {
1318 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "Could not detach request from pool: %s", curl_multi_strerror(rs));
1319 return FAILURE;
1320 }
1321 }
1322
1323 #ifdef PHP_WIN32
1324 # define SELECT_ERROR SOCKET_ERROR
1325 #else
1326 # define SELECT_ERROR -1
1327 #endif
1328
1329 static STATUS php_http_curl_request_pool_wait(php_http_request_pool_t *h, struct timeval *custom_timeout)
1330 {
1331 int MAX;
1332 fd_set R, W, E;
1333 struct timeval timeout;
1334 php_http_curl_request_pool_t *curl = h->ctx;
1335
1336 #if PHP_HTTP_HAVE_EVENT
1337 if (curl->useevents) {
1338 TSRMLS_FETCH_FROM_CTX(h->ts);
1339
1340 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "not implemented");
1341 return FAILURE;
1342 }
1343 #endif
1344
1345 if (custom_timeout && timerisset(custom_timeout)) {
1346 timeout = *custom_timeout;
1347 } else {
1348 long max_tout = 1000;
1349
1350 if ((CURLM_OK == curl_multi_timeout(curl->handle, &max_tout)) && (max_tout > 0)) {
1351 timeout.tv_sec = max_tout / 1000;
1352 timeout.tv_usec = (max_tout % 1000) * 1000;
1353 } else {
1354 timeout.tv_sec = 0;
1355 timeout.tv_usec = 1000;
1356 }
1357 }
1358
1359 FD_ZERO(&R);
1360 FD_ZERO(&W);
1361 FD_ZERO(&E);
1362
1363 if (CURLM_OK == curl_multi_fdset(curl->handle, &R, &W, &E, &MAX)) {
1364 if (MAX == -1) {
1365 php_http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / PHP_HTTP_MCROSEC));
1366 return SUCCESS;
1367 } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
1368 return SUCCESS;
1369 }
1370 }
1371 return FAILURE;
1372 }
1373
1374 static int php_http_curl_request_pool_once(php_http_request_pool_t *h)
1375 {
1376 php_http_curl_request_pool_t *curl = h->ctx;
1377
1378 #if PHP_HTTP_HAVE_EVENT
1379 if (curl->useevents) {
1380 TSRMLS_FETCH_FROM_CTX(h->ts);
1381 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "not implemented");
1382 return FAILURE;
1383 }
1384 #endif
1385
1386 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl->handle, &curl->unfinished));
1387
1388 php_http_curl_request_pool_responsehandler(h);
1389
1390 return curl->unfinished;
1391
1392 }
1393 #if PHP_HTTP_HAVE_EVENT
1394 static void dolog(int i, const char *m) {
1395 fprintf(stderr, "%d: %s\n", i, m);
1396 }
1397 #endif
1398 static STATUS php_http_curl_request_pool_exec(php_http_request_pool_t *h)
1399 {
1400 TSRMLS_FETCH_FROM_CTX(h->ts);
1401
1402 #if PHP_HTTP_HAVE_EVENT
1403 php_http_curl_request_pool_t *curl = h->ctx;
1404
1405 if (curl->useevents) {
1406 event_set_log_callback(dolog);
1407 do {
1408 #if DBG_EVENTS
1409 fprintf(stderr, "X");
1410 #endif
1411 event_base_dispatch(PHP_HTTP_G->curl.event_base);
1412 } while (curl->unfinished);
1413 } else
1414 #endif
1415 {
1416 while (php_http_curl_request_pool_once(h)) {
1417 if (SUCCESS != php_http_curl_request_pool_wait(h, NULL)) {
1418 #ifdef PHP_WIN32
1419 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
1420 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "WinSock error: %d", WSAGetLastError());
1421 #else
1422 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, strerror(errno));
1423 #endif
1424 return FAILURE;
1425 }
1426 }
1427 }
1428
1429 return SUCCESS;
1430 }
1431
1432 static STATUS php_http_curl_request_pool_setopt(php_http_request_pool_t *h, php_http_request_pool_setopt_opt_t opt, void *arg)
1433 {
1434 php_http_curl_request_pool_t *curl = h->ctx;
1435
1436 switch (opt) {
1437 case PHP_HTTP_REQUEST_POOL_OPT_ENABLE_PIPELINING:
1438 if (CURLM_OK != curl_multi_setopt(curl->handle, CURLMOPT_PIPELINING, (long) *((zend_bool *) arg))) {
1439 return FAILURE;
1440 }
1441 break;
1442
1443 case PHP_HTTP_REQUEST_POOL_OPT_USE_EVENTS:
1444 #if PHP_HTTP_HAVE_EVENT
1445 if ((curl->useevents = *((zend_bool *) arg))) {
1446 if (!curl->timeout) {
1447 curl->timeout = ecalloc(1, sizeof(struct event));
1448 }
1449 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, h);
1450 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, php_http_curl_request_pool_socket_callback);
1451 curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, h);
1452 curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, php_http_curl_request_pool_timer_callback);
1453 } else {
1454 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, NULL);
1455 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, NULL);
1456 curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, NULL);
1457 curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, NULL);
1458 }
1459 break;
1460 #endif
1461
1462 default:
1463 return FAILURE;
1464 }
1465 return SUCCESS;
1466 }
1467
1468 static php_http_resource_factory_ops_t php_http_curlm_resource_factory_ops = {
1469 php_http_curlm_ctor,
1470 NULL,
1471 php_http_curlm_dtor
1472 };
1473
1474 static php_http_request_pool_ops_t php_http_curl_request_pool_ops = {
1475 &php_http_curlm_resource_factory_ops,
1476 php_http_curl_request_pool_init,
1477 NULL /* copy */,
1478 php_http_curl_request_pool_dtor,
1479 NULL /*reset */,
1480 php_http_curl_request_pool_exec,
1481 php_http_curl_request_pool_wait,
1482 php_http_curl_request_pool_once,
1483 php_http_curl_request_pool_attach,
1484 php_http_curl_request_pool_detach,
1485 php_http_curl_request_pool_setopt,
1486 };
1487
1488 PHP_HTTP_API php_http_request_pool_ops_t *php_http_curl_get_request_pool_ops(void)
1489 {
1490 return &php_http_curl_request_pool_ops;
1491 }
1492
1493 /* request handler ops */
1494
1495 static STATUS php_http_curl_request_reset(php_http_request_t *h);
1496
1497 static php_http_request_t *php_http_curl_request_init(php_http_request_t *h, void *handle)
1498 {
1499 php_http_curl_request_t *ctx;
1500 TSRMLS_FETCH_FROM_CTX(h->ts);
1501
1502 if (!handle && !(handle = php_http_resource_factory_handle_ctor(h->rf TSRMLS_CC))) {
1503 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "could not initialize curl handle");
1504 return NULL;
1505 }
1506
1507 ctx = ecalloc(1, sizeof(*ctx));
1508 ctx->handle = handle;
1509 php_http_buffer_init(&ctx->options.cookies);
1510 zend_hash_init(&ctx->options.cache, 0, NULL, ZVAL_PTR_DTOR, 0);
1511 h->ctx = ctx;
1512
1513 #if defined(ZTS)
1514 curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
1515 #endif
1516 curl_easy_setopt(handle, CURLOPT_HEADER, 0L);
1517 curl_easy_setopt(handle, CURLOPT_FILETIME, 1L);
1518 curl_easy_setopt(handle, CURLOPT_AUTOREFERER, 1L);
1519 curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L);
1520 curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);
1521 curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, NULL);
1522 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, php_http_curl_dummy_callback);
1523 curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, php_http_curl_raw_callback);
1524 curl_easy_setopt(handle, CURLOPT_READFUNCTION, php_http_curl_read_callback);
1525 curl_easy_setopt(handle, CURLOPT_IOCTLFUNCTION, php_http_curl_ioctl_callback);
1526 curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, php_http_curl_progress_callback);
1527 curl_easy_setopt(handle, CURLOPT_DEBUGDATA, h);
1528 curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, h);
1529
1530 php_http_curl_request_reset(h);
1531
1532 return h;
1533 }
1534
1535 static php_http_request_t *php_http_curl_request_copy(php_http_request_t *from, php_http_request_t *to)
1536 {
1537 php_http_curl_request_t *ctx = from->ctx;
1538 void *copy;
1539 TSRMLS_FETCH_FROM_CTX(from->ts);
1540
1541 if (!(copy = php_http_resource_factory_handle_copy(from->rf, ctx->handle TSRMLS_CC))) {
1542 return NULL;
1543 }
1544
1545 if (to) {
1546 return php_http_curl_request_init(to, copy);
1547 } else {
1548 return php_http_request_init(NULL, from->ops, from->rf, copy TSRMLS_CC);
1549 }
1550 }
1551
1552 static void php_http_curl_request_dtor(php_http_request_t *h)
1553 {
1554 php_http_curl_request_t *ctx = h->ctx;
1555 TSRMLS_FETCH_FROM_CTX(h->ts);
1556
1557 curl_easy_setopt(ctx->handle, CURLOPT_NOPROGRESS, 1L);
1558 curl_easy_setopt(ctx->handle, CURLOPT_PROGRESSFUNCTION, NULL);
1559 curl_easy_setopt(ctx->handle, CURLOPT_VERBOSE, 0L);
1560 curl_easy_setopt(ctx->handle, CURLOPT_DEBUGFUNCTION, NULL);
1561
1562 php_http_resource_factory_handle_dtor(h->rf, ctx->handle TSRMLS_CC);
1563
1564 php_http_buffer_dtor(&ctx->options.cookies);
1565 zend_hash_destroy(&ctx->options.cache);
1566
1567 if (ctx->options.headers) {
1568 curl_slist_free_all(ctx->options.headers);
1569 ctx->options.headers = NULL;
1570 }
1571 php_http_request_progress_dtor(&ctx->progress TSRMLS_CC);
1572
1573 efree(ctx);
1574 h->ctx = NULL;
1575 }
1576 static STATUS php_http_curl_request_reset(php_http_request_t *h)
1577 {
1578 CURL *ch = ((php_http_curl_request_t *) h->ctx)->handle;
1579 php_http_curl_request_storage_t *st;
1580
1581 if ((st = get_storage(ch))) {
1582 if (st->url) {
1583 pefree(st->url, 1);
1584 st->url = NULL;
1585 }
1586 if (st->cookiestore) {
1587 pefree(st->cookiestore, 1);
1588 st->cookiestore = NULL;
1589 }
1590 st->errorbuffer[0] = '\0';
1591 }
1592
1593 curl_easy_setopt(ch, CURLOPT_URL, NULL);
1594 #if PHP_HTTP_CURL_VERSION(7,19,4)
1595 curl_easy_setopt(ch, CURLOPT_NOPROXY, NULL);
1596 #endif
1597 curl_easy_setopt(ch, CURLOPT_PROXY, NULL);
1598 curl_easy_setopt(ch, CURLOPT_PROXYPORT, 0L);
1599 curl_easy_setopt(ch, CURLOPT_PROXYTYPE, 0L);
1600 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1601 #if PHP_HTTP_CURL_VERSION(7,19,1)
1602 curl_easy_setopt(ch, CURLOPT_PROXYUSERNAME, NULL);
1603 curl_easy_setopt(ch, CURLOPT_PROXYPASSWORD, NULL);
1604 #endif
1605 curl_easy_setopt(ch, CURLOPT_PROXYAUTH, 0L);
1606 curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 0L);
1607 curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, 60L);
1608 curl_easy_setopt(ch, CURLOPT_IPRESOLVE, 0);
1609 curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, 0L);
1610 curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, 0L);
1611 /* LFS weirdance
1612 curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) 0);
1613 curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) 0);
1614 */
1615 /* crashes
1616 curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, 5L); */
1617 curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 0L);
1618 curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 0L);
1619 curl_easy_setopt(ch, CURLOPT_INTERFACE, NULL);
1620 curl_easy_setopt(ch, CURLOPT_PORT, 0L);
1621 #if PHP_HTTP_CURL_VERSION(7,19,0)
1622 curl_easy_setopt(ch, CURLOPT_ADDRESS_SCOPE, 0L);
1623 #endif
1624 curl_easy_setopt(ch, CURLOPT_LOCALPORT, 0L);
1625 curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, 0L);
1626 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1627 #if PHP_HTTP_CURL_VERSION(7,19,1)
1628 curl_easy_setopt(ch, CURLOPT_USERNAME, NULL);
1629 curl_easy_setopt(ch, CURLOPT_PASSWORD, NULL);
1630 #endif
1631 curl_easy_setopt(ch, CURLOPT_HTTPAUTH, 0L);
1632 curl_easy_setopt(ch, CURLOPT_ENCODING, NULL);
1633 /* we do this ourself anyway */
1634 curl_easy_setopt(ch, CURLOPT_HTTP_CONTENT_DECODING, 0L);
1635 curl_easy_setopt(ch, CURLOPT_HTTP_TRANSFER_DECODING, 0L);
1636 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0L);
1637 #if PHP_HTTP_CURL_VERSION(7,19,1)
1638 curl_easy_setopt(ch, CURLOPT_POSTREDIR, 0L);
1639 #else
1640 curl_easy_setopt(ch, CURLOPT_POST301, 0L);
1641 #endif
1642 curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, 0L);
1643 curl_easy_setopt(ch, CURLOPT_REFERER, NULL);
1644 curl_easy_setopt(ch, CURLOPT_USERAGENT, "PECL::HTTP/" PHP_HTTP_EXT_VERSION " (PHP/" PHP_VERSION ")");
1645 curl_easy_setopt(ch, CURLOPT_HTTPHEADER, NULL);
1646 curl_easy_setopt(ch, CURLOPT_COOKIE, NULL);
1647 curl_easy_setopt(ch, CURLOPT_COOKIESESSION, 0L);
1648 /* these options would enable curl's cookie engine by default which we don't want
1649 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, NULL);
1650 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, NULL); */
1651 curl_easy_setopt(ch, CURLOPT_COOKIELIST, NULL);
1652 curl_easy_setopt(ch, CURLOPT_RANGE, NULL);
1653 curl_easy_setopt(ch, CURLOPT_RESUME_FROM, 0L);
1654 curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, 0L);
1655 curl_easy_setopt(ch, CURLOPT_TIMECONDITION, 0L);
1656 curl_easy_setopt(ch, CURLOPT_TIMEVALUE, 0L);
1657 curl_easy_setopt(ch, CURLOPT_TIMEOUT, 0L);
1658 curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT, 3);
1659 curl_easy_setopt(ch, CURLOPT_SSLCERT, NULL);
1660 curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, NULL);
1661 curl_easy_setopt(ch, CURLOPT_SSLCERTPASSWD, NULL);
1662 curl_easy_setopt(ch, CURLOPT_SSLKEY, NULL);
1663 curl_easy_setopt(ch, CURLOPT_SSLKEYTYPE, NULL);
1664 curl_easy_setopt(ch, CURLOPT_SSLKEYPASSWD, NULL);
1665 curl_easy_setopt(ch, CURLOPT_SSLENGINE, NULL);
1666 curl_easy_setopt(ch, CURLOPT_SSLVERSION, 0L);
1667 curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L);
1668 curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L);
1669 curl_easy_setopt(ch, CURLOPT_SSL_CIPHER_LIST, NULL);
1670 #if PHP_HTTP_CURL_VERSION(7,19,0)
1671 curl_easy_setopt(ch, CURLOPT_ISSUERCERT, NULL);
1672 #if defined(PHP_HTTP_HAVE_OPENSSL)
1673 curl_easy_setopt(ch, CURLOPT_CRLFILE, NULL);
1674 #endif
1675 #endif
1676 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
1677 curl_easy_setopt(ch, CURLOPT_CERTINFO, NULL);
1678 #endif
1679 #ifdef PHP_HTTP_CURL_CAINFO
1680 curl_easy_setopt(ch, CURLOPT_CAINFO, PHP_HTTP_CURL_CAINFO);
1681 #else
1682 curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
1683 #endif
1684 curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
1685 curl_easy_setopt(ch, CURLOPT_RANDOM_FILE, NULL);
1686 curl_easy_setopt(ch, CURLOPT_EGDSOCKET, NULL);
1687 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, NULL);
1688 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, 0L);
1689 curl_easy_setopt(ch, CURLOPT_HTTPPOST, NULL);
1690 curl_easy_setopt(ch, CURLOPT_IOCTLDATA, NULL);
1691 curl_easy_setopt(ch, CURLOPT_READDATA, NULL);
1692 curl_easy_setopt(ch, CURLOPT_INFILESIZE, 0L);
1693 curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
1694 curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, NULL);
1695 curl_easy_setopt(ch, CURLOPT_NOBODY, 0L);
1696 curl_easy_setopt(ch, CURLOPT_POST, 0L);
1697 curl_easy_setopt(ch, CURLOPT_UPLOAD, 0L);
1698 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1L);
1699
1700 return SUCCESS;
1701 }
1702
1703 static STATUS php_http_curl_request_exec(php_http_request_t *h, const char *meth, const char *url, php_http_message_body_t *body)
1704 {
1705 uint tries = 0;
1706 CURLcode result;
1707 php_http_curl_request_t *curl = h->ctx;
1708 php_http_curl_request_storage_t *storage = get_storage(curl->handle);
1709 TSRMLS_FETCH_FROM_CTX(h->ts);
1710
1711 if (SUCCESS != php_http_curl_request_prepare(h, meth, url, body)) {
1712 return FAILURE;
1713 }
1714
1715 retry:
1716 if (CURLE_OK != (result = curl_easy_perform(curl->handle))) {
1717 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), storage->errorbuffer, storage->url);
1718
1719 if (EG(exception)) {
1720 add_property_long(EG(exception), "curlCode", result);
1721 }
1722
1723 if (curl->options.retry.count > tries++) {
1724 switch (result) {
1725 case CURLE_COULDNT_RESOLVE_PROXY:
1726 case CURLE_COULDNT_RESOLVE_HOST:
1727 case CURLE_COULDNT_CONNECT:
1728 case CURLE_WRITE_ERROR:
1729 case CURLE_READ_ERROR:
1730 case CURLE_OPERATION_TIMEDOUT:
1731 case CURLE_SSL_CONNECT_ERROR:
1732 case CURLE_GOT_NOTHING:
1733 case CURLE_SSL_ENGINE_SETFAILED:
1734 case CURLE_SEND_ERROR:
1735 case CURLE_RECV_ERROR:
1736 case CURLE_SSL_ENGINE_INITFAILED:
1737 case CURLE_LOGIN_DENIED:
1738 if (curl->options.retry.delay >= PHP_HTTP_DIFFSEC) {
1739 php_http_sleep(curl->options.retry.delay);
1740 }
1741 goto retry;
1742 default:
1743 break;
1744 }
1745 } else {
1746 return FAILURE;
1747 }
1748 }
1749
1750 return SUCCESS;
1751 }
1752
1753 static STATUS php_http_curl_request_setopt(php_http_request_t *h, php_http_request_setopt_opt_t opt, void *arg)
1754 {
1755 php_http_curl_request_t *curl = h->ctx;
1756 TSRMLS_FETCH_FROM_CTX(h->ts);
1757
1758 switch (opt) {
1759 case PHP_HTTP_REQUEST_OPT_SETTINGS:
1760 return set_options(h, arg);
1761 break;
1762
1763 case PHP_HTTP_REQUEST_OPT_PROGRESS_CALLBACK:
1764 if (curl->progress.in_cb) {
1765 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "Cannot change progress callback while executing it");
1766 return FAILURE;
1767 }
1768 if (curl->progress.callback) {
1769 php_http_request_progress_dtor(&curl->progress TSRMLS_CC);
1770 }
1771 curl->progress.callback = arg;
1772 break;
1773
1774 case PHP_HTTP_REQUEST_OPT_COOKIES_ENABLE:
1775 /* are cookies already enabled anyway? */
1776 if (!get_storage(curl->handle)->cookiestore) {
1777 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIEFILE, "")) {
1778 return FAILURE;
1779 }
1780 }
1781 break;
1782
1783 case PHP_HTTP_REQUEST_OPT_COOKIES_RESET:
1784 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "ALL")) {
1785 return FAILURE;
1786 }
1787 break;
1788
1789 case PHP_HTTP_REQUEST_OPT_COOKIES_RESET_SESSION:
1790 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "SESS")) {
1791 return FAILURE;
1792 }
1793 break;
1794
1795 case PHP_HTTP_REQUEST_OPT_COOKIES_FLUSH:
1796 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "FLUSH")) {
1797 return FAILURE;
1798 }
1799 break;
1800
1801 default:
1802 return FAILURE;
1803 }
1804
1805 return SUCCESS;
1806 }
1807
1808 static STATUS php_http_curl_request_getopt(php_http_request_t *h, php_http_request_getopt_opt_t opt, void *arg)
1809 {
1810 php_http_curl_request_t *curl = h->ctx;
1811
1812 switch (opt) {
1813 case PHP_HTTP_REQUEST_OPT_PROGRESS_INFO:
1814 *((php_http_request_progress_t **) arg) = &curl->progress;
1815 break;
1816
1817 case PHP_HTTP_REQUEST_OPT_TRANSFER_INFO:
1818 get_info(curl->handle, arg);
1819 break;
1820
1821 default:
1822 return FAILURE;
1823 }
1824
1825 return SUCCESS;
1826 }
1827
1828 static php_http_resource_factory_ops_t php_http_curl_resource_factory_ops = {
1829 php_http_curl_ctor,
1830 php_http_curl_copy,
1831 php_http_curl_dtor
1832 };
1833
1834 static php_http_request_ops_t php_http_curl_request_ops = {
1835 &php_http_curl_resource_factory_ops,
1836 php_http_curl_request_init,
1837 php_http_curl_request_copy,
1838 php_http_curl_request_dtor,
1839 php_http_curl_request_reset,
1840 php_http_curl_request_exec,
1841 php_http_curl_request_setopt,
1842 php_http_curl_request_getopt
1843 };
1844
1845 PHP_HTTP_API php_http_request_ops_t *php_http_curl_get_request_ops(void)
1846 {
1847 return &php_http_curl_request_ops;
1848 }
1849
1850 #if defined(ZTS) && defined(PHP_HTTP_HAVE_SSL)
1851 # ifdef PHP_WIN32
1852 # define PHP_HTTP_NEED_OPENSSL_TSL
1853 # include <openssl/crypto.h>
1854 # else /* !PHP_WIN32 */
1855 # if defined(PHP_HTTP_HAVE_OPENSSL)
1856 # define PHP_HTTP_NEED_OPENSSL_TSL
1857 # include <openssl/crypto.h>
1858 # elif defined(PHP_HTTP_HAVE_GNUTLS)
1859 # define PHP_HTTP_NEED_GNUTLS_TSL
1860 # include <gcrypt.h>
1861 # else
1862 # warning \
1863 "libcurl was compiled with SSL support, but configure could not determine which" \
1864 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
1865 "cause random crashes on SSL requests"
1866 # endif /* PHP_HTTP_HAVE_OPENSSL || PHP_HTTP_HAVE_GNUTLS */
1867 # endif /* PHP_WIN32 */
1868 #endif /* ZTS && PHP_HTTP_HAVE_SSL */
1869
1870
1871 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
1872 static MUTEX_T *php_http_openssl_tsl = NULL;
1873
1874 static void php_http_openssl_thread_lock(int mode, int n, const char * file, int line)
1875 {
1876 if (mode & CRYPTO_LOCK) {
1877 tsrm_mutex_lock(php_http_openssl_tsl[n]);
1878 } else {
1879 tsrm_mutex_unlock(php_http_openssl_tsl[n]);
1880 }
1881 }
1882
1883 static ulong php_http_openssl_thread_id(void)
1884 {
1885 return (ulong) tsrm_thread_id();
1886 }
1887 #endif
1888 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
1889 static int php_http_gnutls_mutex_create(void **m)
1890 {
1891 if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
1892 return SUCCESS;
1893 } else {
1894 return FAILURE;
1895 }
1896 }
1897
1898 static int php_http_gnutls_mutex_destroy(void **m)
1899 {
1900 tsrm_mutex_free(*((MUTEX_T *) m));
1901 return SUCCESS;
1902 }
1903
1904 static int php_http_gnutls_mutex_lock(void **m)
1905 {
1906 return tsrm_mutex_lock(*((MUTEX_T *) m));
1907 }
1908
1909 static int php_http_gnutls_mutex_unlock(void **m)
1910 {
1911 return tsrm_mutex_unlock(*((MUTEX_T *) m));
1912 }
1913
1914 static struct gcry_thread_cbs php_http_gnutls_tsl = {
1915 GCRY_THREAD_OPTION_USER,
1916 NULL,
1917 php_http_gnutls_mutex_create,
1918 php_http_gnutls_mutex_destroy,
1919 php_http_gnutls_mutex_lock,
1920 php_http_gnutls_mutex_unlock
1921 };
1922 #endif
1923
1924 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpCURL, method, 0, req_args)
1925 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpCURL, method, 0)
1926 #define PHP_HTTP_CURL_ME(method, visibility) PHP_ME(HttpCURL, method, PHP_HTTP_ARGS(HttpCURL, method), visibility)
1927 #define PHP_HTTP_CURL_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpCURL, method))
1928 #define PHP_HTTP_CURL_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpCURL_##al), PHP_HTTP_ARGS(HttpCURL, al), vis)
1929
1930 PHP_HTTP_EMPTY_ARGS(__construct);
1931
1932 zend_class_entry *php_http_curl_class_entry;
1933 zend_function_entry php_http_curl_method_entry[] = {
1934 PHP_HTTP_CURL_ME(__construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
1935
1936 EMPTY_FUNCTION_ENTRY
1937 };
1938
1939 PHP_METHOD(HttpCURL, __construct) {
1940 }
1941
1942 PHP_MINIT_FUNCTION(http_curl)
1943 {
1944 php_http_request_factory_driver_t driver = {
1945 &php_http_curl_request_ops,
1946 &php_http_curl_request_pool_ops,
1947 &php_http_curl_request_datashare_ops
1948 };
1949
1950 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
1951 /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
1952 if (!CRYPTO_get_id_callback()) {
1953 int i, c = CRYPTO_num_locks();
1954
1955 php_http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
1956
1957 for (i = 0; i < c; ++i) {
1958 php_http_openssl_tsl[i] = tsrm_mutex_alloc();
1959 }
1960
1961 CRYPTO_set_id_callback(php_http_openssl_thread_id);
1962 CRYPTO_set_locking_callback(php_http_openssl_thread_lock);
1963 }
1964 #endif
1965 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
1966 gcry_control(GCRYCTL_SET_THREAD_CBS, &php_http_gnutls_tsl);
1967 #endif
1968
1969 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
1970 return FAILURE;
1971 }
1972
1973 if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_request_datashare.curl"), &php_http_curlsh_resource_factory_ops, NULL, NULL)) {
1974 return FAILURE;
1975 }
1976
1977 if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_request_pool.curl"), &php_http_curlm_resource_factory_ops, NULL, NULL)) {
1978 return FAILURE;
1979 }
1980
1981 if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_request.curl"), &php_http_curl_resource_factory_ops, NULL, NULL)) {
1982 return FAILURE;
1983 }
1984
1985 if (SUCCESS != php_http_request_factory_add_driver(ZEND_STRL("curl"), &driver)) {
1986 return FAILURE;
1987 }
1988
1989 PHP_HTTP_REGISTER_CLASS(http, CURL, http_curl, php_http_curl_class_entry, 0);
1990
1991 /*
1992 * HTTP Protocol Version Constants
1993 */
1994 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("HTTP_VERSION_1_0"), CURL_HTTP_VERSION_1_0 TSRMLS_CC);
1995 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("HTTP_VERSION_1_1"), CURL_HTTP_VERSION_1_1 TSRMLS_CC);
1996 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("HTTP_VERSION_NONE"), CURL_HTTP_VERSION_NONE TSRMLS_CC); /* to be removed */
1997 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("HTTP_VERSION_ANY"), CURL_HTTP_VERSION_NONE TSRMLS_CC);
1998
1999 /*
2000 * SSL Version Constants
2001 */
2002 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("SSL_VERSION_TLSv1"), CURL_SSLVERSION_TLSv1 TSRMLS_CC);
2003 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("SSL_VERSION_SSLv2"), CURL_SSLVERSION_SSLv2 TSRMLS_CC);
2004 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("SSL_VERSION_SSLv3"), CURL_SSLVERSION_SSLv3 TSRMLS_CC);
2005 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("SSL_VERSION_ANY"), CURL_SSLVERSION_DEFAULT TSRMLS_CC);
2006
2007 /*
2008 * DNS IPvX resolving
2009 */
2010 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("IPRESOLVE_V4"), CURL_IPRESOLVE_V4 TSRMLS_CC);
2011 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("IPRESOLVE_V6"), CURL_IPRESOLVE_V6 TSRMLS_CC);
2012 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("IPRESOLVE_ANY"), CURL_IPRESOLVE_WHATEVER TSRMLS_CC);
2013
2014 /*
2015 * Auth Constants
2016 */
2017 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_BASIC"), CURLAUTH_BASIC TSRMLS_CC);
2018 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_DIGEST"), CURLAUTH_DIGEST TSRMLS_CC);
2019 #if PHP_HTTP_CURL_VERSION(7,19,3)
2020 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_DIGEST_IE"), CURLAUTH_DIGEST_IE TSRMLS_CC);
2021 #endif
2022 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_NTLM"), CURLAUTH_NTLM TSRMLS_CC);
2023 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_GSSNEG"), CURLAUTH_GSSNEGOTIATE TSRMLS_CC);
2024 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_ANY"), CURLAUTH_ANY TSRMLS_CC);
2025
2026 /*
2027 * Proxy Type Constants
2028 */
2029 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_SOCKS4"), CURLPROXY_SOCKS4 TSRMLS_CC);
2030 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_SOCKS4A"), CURLPROXY_SOCKS5 TSRMLS_CC);
2031 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_SOCKS5_HOSTNAME"), CURLPROXY_SOCKS5 TSRMLS_CC);
2032 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_SOCKS5"), CURLPROXY_SOCKS5 TSRMLS_CC);
2033 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_HTTP"), CURLPROXY_HTTP TSRMLS_CC);
2034 # if PHP_HTTP_CURL_VERSION(7,19,4)
2035 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_HTTP_1_0"), CURLPROXY_HTTP_1_0 TSRMLS_CC);
2036 # endif
2037
2038 /*
2039 * Post Redirection Constants
2040 */
2041 #if PHP_HTTP_CURL_VERSION(7,19,1)
2042 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("POSTREDIR_301"), CURL_REDIR_POST_301 TSRMLS_CC);
2043 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("POSTREDIR_302"), CURL_REDIR_POST_302 TSRMLS_CC);
2044 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("POSTREDIR_ALL"), CURL_REDIR_POST_ALL TSRMLS_CC);
2045 #endif
2046
2047 return SUCCESS;
2048 }
2049
2050 PHP_MSHUTDOWN_FUNCTION(http_curl)
2051 {
2052 curl_global_cleanup();
2053 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
2054 if (php_http_openssl_tsl) {
2055 int i, c = CRYPTO_num_locks();
2056
2057 CRYPTO_set_id_callback(NULL);
2058 CRYPTO_set_locking_callback(NULL);
2059
2060 for (i = 0; i < c; ++i) {
2061 tsrm_mutex_free(php_http_openssl_tsl[i]);
2062 }
2063
2064 free(php_http_openssl_tsl);
2065 php_http_openssl_tsl = NULL;
2066 }
2067 #endif
2068 return SUCCESS;
2069 }
2070
2071 PHP_RINIT_FUNCTION(http_curl)
2072 {
2073 #if PHP_HTTP_HAVE_EVENT
2074 if (!PHP_HTTP_G->curl.event_base && !(PHP_HTTP_G->curl.event_base = event_init())) {
2075 return FAILURE;
2076 }
2077 #endif
2078
2079 return SUCCESS;
2080 }
2081
2082 #endif /* PHP_HTTP_HAVE_CURL */
2083
2084 /*
2085 * Local variables:
2086 * tab-width: 4
2087 * c-basic-offset: 4
2088 * End:
2089 * vim600: noet sw=4 ts=4 fdm=marker
2090 * vim<600: noet sw=4 ts=4
2091 */
2092