4 *** Copyright (c) 2003-2008, Lars Nilsson, <lars@quantumchamaeleon.com>
5 *** Copyright (c) 2009, ygrek, <ygrek@autistici.org>
13 /* suppress false gcc warning on seekFunction */
14 #define CURL_DISABLE_TYPECHECK
15 #include <curl/curl.h>
17 #define CAML_NAME_SPACE
18 #include <caml/alloc.h>
19 #include <caml/memory.h>
20 #include <caml/mlvalues.h>
21 #include <caml/callback.h>
22 #include <caml/fail.h>
23 #include <caml/unixsupport.h>
24 #include <caml/custom.h>
25 #include <caml/threads.h>
28 #define CAMLdrop caml_local_roots = caml__frame
34 #pragma message("No config file given.")
37 #define Val_none Val_int(0)
44 some
= caml_alloc(1, 0);
45 Store_field( some
, 0, v
);
49 static value
Val_pair(value v1
, value v2
)
53 pair
= caml_alloc_small(2,0);
59 static value
Val_cons(value list
, value v
) { return Val_pair(v
,list
); }
61 typedef struct Connection Connection
;
62 typedef struct ConnectionList ConnectionList
;
64 #define Connection_val(v) (*(Connection**)Data_custom_val(v))
66 typedef enum OcamlValues
77 Ocaml_PROGRESSFUNCTION
,
82 Ocaml_OPENSOCKETFUNCTION
,
93 Ocaml_HTTPPOSTSTRINGS
,
109 Ocaml_SSL_CIPHER_LIST
,
114 Ocaml_FTP_ALTERNATIVE_TO_USER
,
115 Ocaml_SSH_PUBLIC_KEYFILE
,
116 Ocaml_SSH_PRIVATE_KEYFILE
,
117 Ocaml_SSH_HOST_PUBLIC_KEY_MD5
,
118 Ocaml_COPYPOSTFIELDS
,
124 /* Not used, last for size */
136 size_t refcount
; /* number of references to this structure */
141 char *curl_PROXYUSERPWD
;
143 char *curl_ERRORBUFFER
;
144 char *curl_POSTFIELDS
;
145 int curl_POSTFIELDSIZE
;
147 char *curl_USERAGENT
;
150 struct curl_slist
*curl_HTTPHEADER
;
151 struct curl_slist
*httpPostBuffers
;
152 struct curl_httppost
*httpPostFirst
;
153 struct curl_httppost
*httpPostLast
;
154 struct curl_slist
*curl_RESOLVE
;
156 char *curl_SSLCERTTYPE
;
157 char *curl_SSLCERTPASSWD
;
159 char *curl_SSLKEYTYPE
;
160 char *curl_SSLKEYPASSWD
;
161 char *curl_SSLENGINE
;
162 struct curl_slist
*curl_QUOTE
;
163 struct curl_slist
*curl_POSTQUOTE
;
164 char *curl_COOKIEFILE
;
165 char *curl_CUSTOMREQUEST
;
166 char *curl_INTERFACE
;
169 char *curl_RANDOM_FILE
;
170 char *curl_EGDSOCKET
;
171 char *curl_COOKIEJAR
;
172 char *curl_SSL_CIPHER_LIST
;
174 struct curl_slist
*curl_HTTP200ALIASES
;
175 char *curl_NETRC_FILE
;
176 char *curl_FTP_ACCOUNT
;
177 char *curl_COOKIELIST
;
178 char *curl_FTP_ALTERNATIVE_TO_USER
;
179 char *curl_SSH_PUBLIC_KEYFILE
;
180 char *curl_SSH_PRIVATE_KEYFILE
;
181 char *curl_SSH_HOST_PUBLIC_KEY_MD5
;
182 char *curl_COPYPOSTFIELDS
;
183 char *curl_DNS_SERVERS
;
184 char *curl_MAIL_FROM
;
185 struct curl_slist
*curl_MAIL_RCPT
;
188 struct ConnectionList
194 static ConnectionList connectionList
= {NULL
, NULL
};
196 typedef struct CURLErrorMapping CURLErrorMapping
;
198 struct CURLErrorMapping
204 CURLErrorMapping errorMap
[] =
206 {"CURLE_OK", CURLE_OK
},
207 #if HAVE_DECL_CURLE_UNSUPPORTED_PROTOCOL
208 {"CURLE_UNSUPPORTED_PROTOCOL", CURLE_UNSUPPORTED_PROTOCOL
},
210 {"CURLE_UNSUPPORTED_PROTOCOL", -1},
212 #if HAVE_DECL_CURLE_FAILED_INIT
213 {"CURLE_FAILED_INIT", CURLE_FAILED_INIT
},
215 {"CURLE_FAILED_INIT", -1},
217 #if HAVE_DECL_CURLE_URL_MALFORMAT
218 {"CURLE_URL_MALFORMAT", CURLE_URL_MALFORMAT
},
220 {"CURLE_URL_MALFORMAT", -1},
222 #if HAVE_DECL_CURLE_URL_MALFORMAT_USER
223 {"CURLE_URL_MALFORMAT_USER", CURLE_URL_MALFORMAT_USER
},
225 {"CURLE_URL_MALFORMAT_USER", -1},
227 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_PROXY
228 {"CURLE_COULDNT_RESOLVE_PROXY", CURLE_COULDNT_RESOLVE_PROXY
},
230 {"CURLE_COULDNT_RESOLVE_PROXY", -1},
232 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_HOST
233 {"CURLE_COULDNT_RESOLVE_HOST", CURLE_COULDNT_RESOLVE_HOST
},
235 {"CURLE_COULDNT_RESOLVE_HOST", -1},
237 #if HAVE_DECL_CURLE_COULDNT_CONNECT
238 {"CURLE_COULDNT_CONNECT", CURLE_COULDNT_CONNECT
},
240 {"CURLE_COULDNT_CONNECT", -1},
242 #if HAVE_DECL_CURLE_FTP_WEIRD_SERVER_REPLY
243 {"CURLE_FTP_WEIRD_SERVER_REPLY", CURLE_FTP_WEIRD_SERVER_REPLY
},
245 {"CURLE_FTP_WEIRD_SERVER_REPLY", -1},
247 #if HAVE_DECL_CURLE_FTP_ACCESS_DENIED
248 {"CURLE_FTP_ACCESS_DENIED", CURLE_FTP_ACCESS_DENIED
},
250 {"CURLE_FTP_ACCESS_DENIED", -1},
252 #if HAVE_DECL_CURLE_FTP_USER_PASSWORD_INCORRECT
253 {"CURLE_FTP_USER_PASSWORD_INCORRECT", CURLE_FTP_USER_PASSWORD_INCORRECT
},
255 {"CURLE_FTP_USER_PASSWORD_INCORRECT", -1},
257 #if HAVE_DECL_CURLE_FTP_WEIRD_PASS_REPLY
258 {"CURLE_FTP_WEIRD_PASS_REPLY", CURLE_FTP_WEIRD_PASS_REPLY
},
260 {"CURLE_FTP_WEIRD_PASS_REPLY", -1},
262 #if HAVE_DECL_CURLE_FTP_WEIRD_USER_REPLY
263 {"CURLE_FTP_WEIRD_USER_REPLY", CURLE_FTP_WEIRD_USER_REPLY
},
265 {"CURLE_FTP_WEIRD_USER_REPLY", -1},
267 #if HAVE_DECL_CURLE_FTP_WEIRD_PASV_REPLY
268 {"CURLE_FTP_WEIRD_PASV_REPLY", CURLE_FTP_WEIRD_PASV_REPLY
},
270 {"CURLE_FTP_WEIRD_PASV_REPLY", -1},
272 #if HAVE_DECL_CURLE_FTP_WEIRD_227_FORMAT
273 {"CURLE_FTP_WEIRD_227_FORMAT", CURLE_FTP_WEIRD_227_FORMAT
},
275 {"CURLE_FTP_WEIRD_227_FORMAT", -1},
277 #if HAVE_DECL_CURLE_FTP_CANT_GET_HOST
278 {"CURLE_FTP_CANT_GET_HOST", CURLE_FTP_CANT_GET_HOST
},
280 {"CURLE_FTP_CANT_GET_HOST", -1},
282 #if HAVE_DECL_CURLE_FTP_CANT_RECONNECT
283 {"CURLE_FTP_CANT_RECONNECT", CURLE_FTP_CANT_RECONNECT
},
285 {"CURLE_FTP_CANT_RECONNECT", -1},
287 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_BINARY
288 {"CURLE_FTP_COULDNT_SET_BINARY", CURLE_FTP_COULDNT_SET_BINARY
},
290 {"CURLE_FTP_COULDNT_SET_BINARY", -1},
292 #if HAVE_DECL_CURLE_PARTIAL_FILE
293 {"CURLE_PARTIAL_FILE", CURLE_PARTIAL_FILE
},
295 {"CURLE_PARTIAL_FILE", -1},
297 #if HAVE_DECL_CURLE_FTP_COULDNT_RETR_FILE
298 {"CURLE_FTP_COULDNT_RETR_FILE", CURLE_FTP_COULDNT_RETR_FILE
},
300 {"CURLE_FTP_COULDNT_RETR_FILE", -1},
302 #if HAVE_DECL_CURLE_FTP_WRITE_ERROR
303 {"CURLE_FTP_WRITE_ERROR", CURLE_FTP_WRITE_ERROR
},
305 {"CURLE_FTP_WRITE_ERROR", -1},
307 #if HAVE_DECL_CURLE_FTP_QUOTE_ERROR
308 {"CURLE_FTP_QUOTE_ERROR", CURLE_FTP_QUOTE_ERROR
},
310 {"CURLE_FTP_QUOTE_ERROR", -1},
312 #if HAVE_DECL_CURLE_HTTP_RETURNED_ERROR
313 {"CURLE_HTTP_RETURNED_ERROR", CURLE_HTTP_RETURNED_ERROR
},
315 {"CURLE_HTTP_RETURNED_ERROR", -1},
317 #if HAVE_DECL_CURLE_WRITE_ERROR
318 {"CURLE_WRITE_ERROR", CURLE_WRITE_ERROR
},
320 {"CURLE_WRITE_ERROR", -1},
322 #if HAVE_DECL_CURLE_MALFORMAT_USER
323 {"CURLE_MALFORMAT_USER", CURLE_MALFORMAT_USER
},
325 {"CURLE_MALFORMAT_USER", -1},
327 #if HAVE_DECL_CURLE_FTP_COULDNT_STOR_FILE
328 {"CURLE_FTP_COULDNT_STOR_FILE", CURLE_FTP_COULDNT_STOR_FILE
},
330 {"CURLE_FTP_COULDNT_STOR_FILE", -1},
332 #if HAVE_DECL_CURLE_READ_ERROR
333 {"CURLE_READ_ERROR", CURLE_READ_ERROR
},
335 {"CURLE_READ_ERROR", -1},
337 #if HAVE_DECL_CURLE_OUT_OF_MEMORY
338 {"CURLE_OUT_OF_MEMORY", CURLE_OUT_OF_MEMORY
},
340 {"CURLE_OUT_OF_MEMORY", -1},
342 #if HAVE_DECL_CURLE_OPERATION_TIMEOUTED
343 {"CURLE_OPERATION_TIMEOUTED", CURLE_OPERATION_TIMEOUTED
},
345 {"CURLE_OPERATION_TIMEOUTED", -1},
347 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_ASCII
348 {"CURLE_FTP_COULDNT_SET_ASCII", CURLE_FTP_COULDNT_SET_ASCII
},
350 {"CURLE_FTP_COULDNT_SET_ASCII", -1},
352 #if HAVE_DECL_CURLE_FTP_PORT_FAILED
353 {"CURLE_FTP_PORT_FAILED", CURLE_FTP_PORT_FAILED
},
355 {"CURLE_FTP_PORT_FAILED", -1},
357 #if HAVE_DECL_CURLE_FTP_COULDNT_USE_REST
358 {"CURLE_FTP_COULDNT_USE_REST", CURLE_FTP_COULDNT_USE_REST
},
360 {"CURLE_FTP_COULDNT_USE_REST", -1},
362 #if HAVE_DECL_CURLE_FTP_COULDNT_GET_SIZE
363 {"CURLE_FTP_COULDNT_GET_SIZE", CURLE_FTP_COULDNT_GET_SIZE
},
365 {"CURLE_FTP_COULDNT_GET_SIZE", -1},
367 #if HAVE_DECL_CURLE_HTTP_RANGE_ERROR
368 {"CURLE_HTTP_RANGE_ERROR", CURLE_HTTP_RANGE_ERROR
},
370 {"CURLE_HTTP_RANGE_ERROR", -1},
372 #if HAVE_DECL_CURLE_HTTP_POST_ERROR
373 {"CURLE_HTTP_POST_ERROR", CURLE_HTTP_POST_ERROR
},
375 {"CURLE_HTTP_POST_ERROR", -1},
377 #if HAVE_DECL_CURLE_SSL_CONNECT_ERROR
378 {"CURLE_SSL_CONNECT_ERROR", CURLE_SSL_CONNECT_ERROR
},
380 {"CURLE_SSL_CONNECT_ERROR", -1},
382 #if HAVE_DECL_CURLE_BAD_DOWNLOAD_RESUME
383 {"CURLE_BAD_DOWNLOAD_RESUME", CURLE_BAD_DOWNLOAD_RESUME
},
385 {"CURLE_BAD_DOWNLOAD_RESUME", -1},
387 #if HAVE_DECL_CURLE_FILE_COULDNT_READ_FILE
388 {"CURLE_FILE_COULDNT_READ_FILE", CURLE_FILE_COULDNT_READ_FILE
},
390 {"CURLE_FILE_COULDNT_READ_FILE", -1},
392 #if HAVE_DECL_CURLE_LDAP_CANNOT_BIND
393 {"CURLE_LDAP_CANNOT_BIND", CURLE_LDAP_CANNOT_BIND
},
395 {"CURLE_LDAP_CANNOT_BIND", -1},
397 #if HAVE_DECL_CURLE_LDAP_SEARCH_FAILED
398 {"CURLE_LDAP_SEARCH_FAILED", CURLE_LDAP_SEARCH_FAILED
},
400 {"CURLE_LDAP_SEARCH_FAILED", -1},
402 #if HAVE_DECL_CURLE_LIBRARY_NOT_FOUND
403 {"CURLE_LIBRARY_NOT_FOUND", CURLE_LIBRARY_NOT_FOUND
},
405 {"CURLE_LIBRARY_NOT_FOUND", -1},
407 #if HAVE_DECL_CURLE_FUNCTION_NOT_FOUND
408 {"CURLE_FUNCTION_NOT_FOUND", CURLE_FUNCTION_NOT_FOUND
},
410 {"CURLE_FUNCTION_NOT_FOUND", -1},
412 #if HAVE_DECL_CURLE_ABORTED_BY_CALLBACK
413 {"CURLE_ABORTED_BY_CALLBACK", CURLE_ABORTED_BY_CALLBACK
},
415 {"CURLE_ABORTED_BY_CALLBACK", -1},
417 #if HAVE_DECL_CURLE_BAD_FUNCTION_ARGUMENT
418 {"CURLE_BAD_FUNCTION_ARGUMENT", CURLE_BAD_FUNCTION_ARGUMENT
},
420 {"CURLE_BAD_FUNCTION_ARGUMENT", -1},
422 #if HAVE_DECL_CURLE_BAD_CALLING_ORDER
423 {"CURLE_BAD_CALLING_ORDER", CURLE_BAD_CALLING_ORDER
},
425 {"CURLE_BAD_CALLING_ORDER", -1},
427 #if HAVE_DECL_CURLE_INTERFACE_FAILED
428 {"CURLE_INTERFACE_FAILED", CURLE_INTERFACE_FAILED
},
430 {"CURLE_INTERFACE_FAILED", -1},
432 #if HAVE_DECL_CURLE_BAD_PASSWORD_ENTERED
433 {"CURLE_BAD_PASSWORD_ENTERED", CURLE_BAD_PASSWORD_ENTERED
},
435 {"CURLE_BAD_PASSWORD_ENTERED", -1},
437 #if HAVE_DECL_CURLE_TOO_MANY_REDIRECTS
438 {"CURLE_TOO_MANY_REDIRECTS", CURLE_TOO_MANY_REDIRECTS
},
440 {"CURLE_TOO_MANY_REDIRECTS", -1},
442 #if HAVE_DECL_CURLE_UNKNOWN_TELNET_OPTION
443 {"CURLE_UNKNOWN_TELNET_OPTION", CURLE_UNKNOWN_TELNET_OPTION
},
445 {"CURLE_UNKNOWN_TELNET_OPTION", -1},
447 #if HAVE_DECL_CURLE_TELNET_OPTION_SYNTAX
448 {"CURLE_TELNET_OPTION_SYNTAX", CURLE_TELNET_OPTION_SYNTAX
},
450 {"CURLE_TELNET_OPTION_SYNTAX", -1},
452 #if HAVE_DECL_CURLE_SSL_PEER_CERTIFICATE
453 {"CURLE_SSL_PEER_CERTIFICATE", CURLE_SSL_PEER_CERTIFICATE
},
455 {"CURLE_SSL_PEER_CERTIFICATE", -1},
457 #if HAVE_DECL_CURLE_GOT_NOTHING
458 {"CURLE_GOT_NOTHING", CURLE_GOT_NOTHING
},
460 {"CURLE_GOT_NOTHING", -1},
462 #if HAVE_DECL_CURLE_SSL_ENGINE_NOTFOUND
463 {"CURLE_SSL_ENGINE_NOTFOUND", CURLE_SSL_ENGINE_NOTFOUND
},
465 {"CURLE_SSL_ENGINE_NOTFOUND", -1},
467 #if HAVE_DECL_CURLE_SSL_ENGINE_SETFAILED
468 {"CURLE_SSL_ENGINE_SETFAILED", CURLE_SSL_ENGINE_SETFAILED
},
470 {"CURLE_SSL_ENGINE_SETFAILED", -1},
472 #if HAVE_DECL_CURLE_SEND_ERROR
473 {"CURLE_SEND_ERROR", CURLE_SEND_ERROR
},
475 {"CURLE_SEND_ERROR", -1},
477 #if HAVE_DECL_CURLE_RECV_ERROR
478 {"CURLE_RECV_ERROR", CURLE_RECV_ERROR
},
480 {"CURLE_RECV_ERROR", -1},
482 #if HAVE_DECL_CURLE_SHARE_IN_USE
483 {"CURLE_SHARE_IN_USE", CURLE_SHARE_IN_USE
},
485 {"CURLE_SHARE_IN_USE", -1},
487 #if HAVE_DECL_CURLE_SSL_CERTPROBLEM
488 {"CURLE_SSL_CERTPROBLEM", CURLE_SSL_CERTPROBLEM
},
490 {"CURLE_SSL_CERTPROBLEM", -1},
492 #if HAVE_DECL_CURLE_SSL_CIPHER
493 {"CURLE_SSL_CIPHER", CURLE_SSL_CIPHER
},
495 {"CURLE_SSL_CIPHER", -1},
497 #if HAVE_DECL_CURLE_SSL_CACERT
498 {"CURLE_SSL_CACERT", CURLE_SSL_CACERT
},
500 {"CURLE_SSL_CACERT", -1},
502 #if HAVE_DECL_CURLE_BAD_CONTENT_ENCODING
503 {"CURLE_BAD_CONTENT_ENCODING", CURLE_BAD_CONTENT_ENCODING
},
505 {"CURLE_BAD_CONTENT_ENCODING", -1},
507 #if HAVE_DECL_CURLE_LDAP_INVALID_URL
508 {"CURLE_LDAP_INVALID_URL", CURLE_LDAP_INVALID_URL
},
510 {"CURLE_LDAP_INVALID_URL", -1},
512 #if HAVE_DECL_CURLE_FILESIZE_EXCEEDED
513 {"CURLE_FILESIZE_EXCEEDED", CURLE_FILESIZE_EXCEEDED
},
515 {"CURLE_FILESIZE_EXCEEDED", -1},
517 #if HAVE_DECL_CURLE_FTP_SSL_FAILED
518 {"CURLE_FTP_SSL_FAILED", CURLE_FTP_SSL_FAILED
},
520 {"CURLE_FTP_SSL_FAILED", -1},
522 #if HAVE_DECL_CURLE_SEND_FAIL_REWIND
523 {"CURLE_SEND_FAIL_REWIND", CURLE_SEND_FAIL_REWIND
},
525 {"CURLE_SEND_FAIL_REWIND", -1},
527 #if HAVE_DECL_CURLE_SSL_ENGINE_INITFAILED
528 {"CURLE_SSL_ENGINE_INITFAILED", CURLE_SSL_ENGINE_INITFAILED
},
530 {"CURLE_SSL_ENGINE_INITFAILED", -1},
532 #if HAVE_DECL_CURLE_LOGIN_DENIED
533 {"CURLE_LOGIN_DENIED", CURLE_LOGIN_DENIED
},
535 {"CURLE_LOGIN_DENIED", -1},
537 #if HAVE_DECL_CURLE_TFTP_NOTFOUND
538 {"CURLE_TFTP_NOTFOUND", CURLE_TFTP_NOTFOUND
},
540 {"CURLE_TFTP_NOTFOUND", -1},
542 #if HAVE_DECL_CURLE_TFTP_PERM
543 {"CURLE_TFTP_PERM", CURLE_TFTP_PERM
},
545 {"CURLE_TFTP_PERM", -1},
547 #if HAVE_DECL_CURLE_REMOTE_DISK_FULL
548 {"CURLE_REMOTE_DISK_FULL", CURLE_REMOTE_DISK_FULL
},
550 {"CURLE_REMOTE_DISK_FULL", -1},
552 #if HAVE_DECL_CURLE_TFTP_ILLEGAL
553 {"CURLE_TFTP_ILLEGAL", CURLE_TFTP_ILLEGAL
},
555 {"CURLE_TFTP_ILLEGAL", -1},
557 #if HAVE_DECL_CURLE_TFTP_UNKNOWNID
558 {"CURLE_TFTP_UNKNOWNID", CURLE_TFTP_UNKNOWNID
},
560 {"CURLE_TFTP_UNKNOWNID", -1},
562 #if HAVE_DECL_CURLE_REMOTE_FILE_EXISTS
563 {"CURLE_REMOTE_FILE_EXISTS", CURLE_REMOTE_FILE_EXISTS
},
565 {"CURLE_REMOTE_FILE_EXISTS", -1},
567 #if HAVE_DECL_CURLE_TFTP_NOSUCHUSER
568 {"CURLE_TFTP_NOSUCHUSER", CURLE_TFTP_NOSUCHUSER
},
570 {"CURLE_TFTP_NOSUCHUSER", -1},
572 #if HAVE_DECL_CURLE_CONV_FAILED
573 {"CURLE_CONV_FAILED", CURLE_CONV_FAILED
},
575 {"CURLE_CONV_FAILED", -1},
577 #if HAVE_DECL_CURLE_CONV_REQD
578 {"CURLE_CONV_REQD", CURLE_CONV_REQD
},
580 {"CURLE_CONV_REQD", -1},
582 #if HAVE_DECL_CURLE_SSL_CACERT_BADFILE
583 {"CURLE_SSL_CACERT_BADFILE", CURLE_SSL_CACERT_BADFILE
},
585 {"CURLE_SSL_CACERT_BADFILE", -1},
587 #if HAVE_DECL_CURLE_REMOTE_FILE_NOT_FOUND
588 {"CURLE_REMOTE_FILE_NOT_FOUND", CURLE_REMOTE_FILE_NOT_FOUND
},
590 {"CURLE_REMOTE_FILE_NOT_FOUND", -1},
592 #if HAVE_DECL_CURLE_SSH
593 {"CURLE_SSH", CURLE_SSH
},
597 #if HAVE_DECL_CURLE_SSL_SHUTDOWN_FAILED
598 {"CURLE_SSL_SHUTDOWN_FAILED", CURLE_SSL_SHUTDOWN_FAILED
},
600 {"CURLE_SSL_SHUTDOWN_FAILED", -1},
602 #if HAVE_DECL_CURLE_AGAIN
603 {"CURLE_AGAIN", CURLE_AGAIN
},
610 typedef struct CURLOptionMapping CURLOptionMapping
;
612 struct CURLOptionMapping
614 void (*optionHandler
)(Connection
*, value
);
616 OcamlValue ocamlValue
;
619 static char* strdup_ml(value v
)
622 p
= malloc(caml_string_length(v
)+1);
623 memcpy(p
,String_val(v
),caml_string_length(v
)+1); // caml strings have terminating zero
627 /* prepends to the beginning of list */
628 static struct curl_slist
* curl_slist_prepend_ml(struct curl_slist
* list
, value v
)
630 /* FIXME check NULLs */
631 struct curl_slist
* new_item
= malloc(sizeof(struct curl_slist
));
633 new_item
->next
= list
;
634 new_item
->data
= strdup_ml(v
);
639 static void free_curl_slist(struct curl_slist
*slist
)
644 curl_slist_free_all(slist
);
647 static void raiseError(Connection
*conn
, CURLcode code
)
650 CAMLlocal1(exceptionData
);
652 char *errorString
= "Unknown Error";
655 for (i
= 0; errorMap
[i
].name
!= NULL
; i
++)
657 if (errorMap
[i
].error
== code
)
659 errorString
= errorMap
[i
].name
;
664 exceptionData
= caml_alloc_tuple(3);
666 Store_field(exceptionData
, 0, Val_int(code
));
667 Store_field(exceptionData
, 1, Val_int(code
));
668 Store_field(exceptionData
, 2, caml_copy_string(errorString
));
670 if (conn
!= NULL
&& conn
->curl_ERRORBUFFER
!= NULL
)
672 Store_field(Field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
), 0, caml_copy_string(conn
->curl_ERRORBUFFER
));
675 exception
= caml_named_value("CurlException");
677 if (exception
== NULL
)
678 caml_failwith("CurlException not registered");
680 caml_raise_with_arg(*exception
, exceptionData
);
685 static void resetOcamlValues(Connection
* connection
)
689 for (i
= 0; i
< OcamlValuesSize
; i
++)
690 Store_field(connection
->ocamlValues
, i
, Val_unit
);
693 static Connection
* allocConnection(CURL
* h
)
695 Connection
* connection
= (Connection
*)malloc(sizeof(Connection
));
697 connection
->ocamlValues
= caml_alloc(OcamlValuesSize
, 0);
698 resetOcamlValues(connection
);
699 caml_register_global_root(&connection
->ocamlValues
);
701 connection
->connection
= h
;
703 connection
->next
= NULL
;
704 connection
->prev
= NULL
;
706 if (connectionList
.tail
== NULL
)
708 connectionList
.tail
= connection
;
709 connectionList
.head
= connection
;
713 connection
->prev
= connectionList
.head
;
714 connectionList
.head
->next
= connection
;
715 connectionList
.head
= connection
;
718 connection
->refcount
= 0;
720 connection
->curl_URL
= NULL
;
721 connection
->curl_PROXY
= NULL
;
722 connection
->curl_USERPWD
= NULL
;
723 connection
->curl_PROXYUSERPWD
= NULL
;
724 connection
->curl_RANGE
= NULL
;
725 connection
->curl_ERRORBUFFER
= NULL
;
726 connection
->curl_POSTFIELDS
= NULL
;
727 connection
->curl_POSTFIELDSIZE
= -1;
728 connection
->curl_REFERER
= NULL
;
729 connection
->curl_USERAGENT
= NULL
;
730 connection
->curl_FTPPORT
= NULL
;
731 connection
->curl_COOKIE
= NULL
;
732 connection
->curl_HTTPHEADER
= NULL
;
733 connection
->httpPostBuffers
= NULL
;
734 connection
->httpPostFirst
= NULL
;
735 connection
->httpPostLast
= NULL
;
736 connection
->curl_SSLCERT
= NULL
;
737 connection
->curl_SSLCERTTYPE
= NULL
;
738 connection
->curl_SSLCERTPASSWD
= NULL
;
739 connection
->curl_SSLKEY
= NULL
;
740 connection
->curl_SSLKEYTYPE
= NULL
;
741 connection
->curl_SSLKEYPASSWD
= NULL
;
742 connection
->curl_SSLENGINE
= NULL
;
743 connection
->curl_QUOTE
= NULL
;
744 connection
->curl_POSTQUOTE
= NULL
;
745 connection
->curl_COOKIEFILE
= NULL
;
746 connection
->curl_CUSTOMREQUEST
= NULL
;
747 connection
->curl_INTERFACE
= NULL
;
748 connection
->curl_CAINFO
= NULL
;
749 connection
->curl_CAPATH
= NULL
;
750 connection
->curl_RANDOM_FILE
= NULL
;
751 connection
->curl_EGDSOCKET
= NULL
;
752 connection
->curl_COOKIEJAR
= NULL
;
753 connection
->curl_SSL_CIPHER_LIST
= NULL
;
754 connection
->curl_PRIVATE
= NULL
;
755 connection
->curl_HTTP200ALIASES
= NULL
;
756 connection
->curl_NETRC_FILE
= NULL
;
757 connection
->curl_FTP_ACCOUNT
= NULL
;
758 connection
->curl_COOKIELIST
= NULL
;
759 connection
->curl_FTP_ALTERNATIVE_TO_USER
= NULL
;
760 connection
->curl_SSH_PUBLIC_KEYFILE
= NULL
;
761 connection
->curl_SSH_PRIVATE_KEYFILE
= NULL
;
762 connection
->curl_COPYPOSTFIELDS
= NULL
;
763 connection
->curl_RESOLVE
= NULL
;
764 connection
->curl_DNS_SERVERS
= NULL
;
765 connection
->curl_MAIL_FROM
= NULL
;
766 connection
->curl_MAIL_RCPT
= NULL
;
771 static Connection
*newConnection(void)
775 caml_enter_blocking_section();
776 h
= curl_easy_init();
777 caml_leave_blocking_section();
779 return allocConnection(h
);
782 static void free_if(void* p
) { if (NULL
!= p
) free(p
); }
784 static void removeConnection(Connection
*connection
, int finalization
)
786 const char* fin_url
= NULL
;
788 if (!connection
->connection
)
790 return; /* already cleaned up */
795 /* cannot engage OCaml runtime at finalization, just report leak */
796 if (CURLE_OK
!= curl_easy_getinfo(connection
->connection
, CURLINFO_EFFECTIVE_URL
, &fin_url
) || NULL
== fin_url
)
800 fprintf(stderr
,"Curl: handle %p leaked, conn %p, url %s\n", connection
->connection
, connection
, fin_url
);
805 caml_enter_blocking_section();
806 curl_easy_cleanup(connection
->connection
);
807 caml_leave_blocking_section();
810 connection
->connection
= NULL
;
812 if (connectionList
.tail
== connection
)
813 connectionList
.tail
= connectionList
.tail
->next
;
814 if (connectionList
.head
== connection
)
815 connectionList
.head
= connectionList
.head
->prev
;
817 if (connection
->next
!= NULL
)
818 connection
->next
->prev
= connection
->prev
;
819 if (connection
->prev
!= NULL
)
820 connection
->prev
->next
= connection
->next
;
822 caml_remove_global_root(&connection
->ocamlValues
);
824 free_if(connection
->curl_URL
);
825 free_if(connection
->curl_PROXY
);
826 free_if(connection
->curl_USERPWD
);
827 free_if(connection
->curl_PROXYUSERPWD
);
828 free_if(connection
->curl_RANGE
);
829 free_if(connection
->curl_ERRORBUFFER
);
830 free_if(connection
->curl_POSTFIELDS
);
831 free_if(connection
->curl_REFERER
);
832 free_if(connection
->curl_USERAGENT
);
833 free_if(connection
->curl_FTPPORT
);
834 free_if(connection
->curl_COOKIE
);
835 free_curl_slist(connection
->curl_HTTPHEADER
);
836 free_curl_slist(connection
->httpPostBuffers
);
837 if (connection
->httpPostFirst
!= NULL
)
838 curl_formfree(connection
->httpPostFirst
);
839 free_curl_slist(connection
->curl_RESOLVE
);
840 free_if(connection
->curl_SSLCERT
);
841 free_if(connection
->curl_SSLCERTTYPE
);
842 free_if(connection
->curl_SSLCERTPASSWD
);
843 free_if(connection
->curl_SSLKEY
);
844 free_if(connection
->curl_SSLKEYTYPE
);
845 free_if(connection
->curl_SSLKEYPASSWD
);
846 free_if(connection
->curl_SSLENGINE
);
847 free_curl_slist(connection
->curl_QUOTE
);
848 free_curl_slist(connection
->curl_POSTQUOTE
);
849 free_if(connection
->curl_COOKIEFILE
);
850 free_if(connection
->curl_CUSTOMREQUEST
);
851 free_if(connection
->curl_INTERFACE
);
852 free_if(connection
->curl_CAINFO
);
853 free_if(connection
->curl_CAPATH
);
854 free_if(connection
->curl_RANDOM_FILE
);
855 free_if(connection
->curl_EGDSOCKET
);
856 free_if(connection
->curl_COOKIEJAR
);
857 free_if(connection
->curl_SSL_CIPHER_LIST
);
858 free_if(connection
->curl_PRIVATE
);
859 free_curl_slist(connection
->curl_HTTP200ALIASES
);
860 free_if(connection
->curl_NETRC_FILE
);
861 free_if(connection
->curl_FTP_ACCOUNT
);
862 free_if(connection
->curl_COOKIELIST
);
863 free_if(connection
->curl_FTP_ALTERNATIVE_TO_USER
);
864 free_if(connection
->curl_SSH_PUBLIC_KEYFILE
);
865 free_if(connection
->curl_SSH_PRIVATE_KEYFILE
);
866 free_if(connection
->curl_COPYPOSTFIELDS
);
867 free_if(connection
->curl_DNS_SERVERS
);
868 free_if(connection
->curl_MAIL_FROM
);
869 free_curl_slist(connection
->curl_MAIL_RCPT
);
873 static void checkConnection(Connection
* connection
)
878 static void checkConnection(Connection
*connection
)
880 Connection
*listIter
;
882 listIter
= connectionList
.tail
;
884 while (listIter
!= NULL
)
886 if (listIter
== connection
)
889 listIter
= listIter
->next
;
892 caml_failwith("Invalid Connection");
896 static Connection
* findConnection(CURL
* h
)
898 Connection
*listIter
;
900 listIter
= connectionList
.tail
;
902 while (listIter
!= NULL
)
904 if (listIter
->connection
== h
)
907 listIter
= listIter
->next
;
910 caml_failwith("Unknown handle");
913 void op_curl_easy_finalize(value v
)
915 Connection
* conn
= Connection_val(v
);
916 /* same connection may be referenced by several different
917 OCaml values, see e.g. caml_curl_multi_remove_finished */
919 if (0 == conn
->refcount
)
921 removeConnection(conn
, 1);
926 int op_curl_easy_compare(value v1
, value v2
)
928 size_t p1
= (size_t)Connection_val(v1
);
929 size_t p2
= (size_t)Connection_val(v2
);
930 return (p1
== p2
? 0 : (p1
> p2
? 1 : -1)); /* compare addresses */
933 intnat
op_curl_easy_hash(value v
)
935 return (size_t)Connection_val(v
); /* address */
938 static struct custom_operations curl_easy_ops
= {
940 op_curl_easy_finalize
,
941 op_curl_easy_compare
,
943 custom_serialize_default
,
944 custom_deserialize_default
,
945 #if defined(custom_compare_ext_default)
946 custom_compare_ext_default
,
950 value
caml_curl_alloc(Connection
* conn
)
952 value v
= caml_alloc_custom(&curl_easy_ops
, sizeof(Connection
*), 0, 1);
953 Connection_val(v
) = conn
;
958 static size_t cb_WRITEFUNCTION(char *ptr
, size_t size
, size_t nmemb
, void *data
)
960 caml_leave_blocking_section();
963 CAMLlocal2(result
, str
);
964 Connection
*conn
= (Connection
*)data
;
967 checkConnection(conn
);
969 str
= caml_alloc_string(size
*nmemb
);
971 for (i
= 0; i
< size
*nmemb
; i
++)
972 Byte(str
, i
) = ptr
[i
];
974 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_WRITEFUNCTION
), str
);
976 size_t r
= Is_exception_result(result
) ? 0 : Int_val(result
);
979 caml_enter_blocking_section();
983 static size_t cb_READFUNCTION(void *ptr
, size_t size
, size_t nmemb
, void *data
)
985 caml_leave_blocking_section();
989 Connection
*conn
= (Connection
*)data
;
992 checkConnection(conn
);
994 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_READFUNCTION
),
995 Val_int(size
*nmemb
));
997 if (Is_exception_result(result
))
999 CAMLreturnT(size_t,CURL_READFUNC_ABORT
);
1002 length
= caml_string_length(result
);
1006 if (length
<= size
*nmemb
)
1008 memcpy(ptr
, String_val(result
), length
);
1013 r
= CURL_READFUNC_ABORT
;
1017 caml_enter_blocking_section();
1021 static size_t cb_HEADERFUNCTION(char *ptr
, size_t size
, size_t nmemb
, void *data
)
1023 caml_leave_blocking_section();
1026 CAMLlocal2(result
,str
);
1027 Connection
*conn
= (Connection
*)data
;
1030 checkConnection(conn
);
1032 str
= caml_alloc_string(size
*nmemb
);
1034 for (i
= 0; i
< size
*nmemb
; i
++)
1035 Byte(str
, i
) = ptr
[i
];
1037 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_HEADERFUNCTION
), str
);
1039 size_t r
= Is_exception_result(result
) ? 0 : Int_val(result
);
1042 caml_enter_blocking_section();
1046 static int cb_PROGRESSFUNCTION(void *data
,
1052 caml_leave_blocking_section();
1056 CAMLlocalN(callbackData
, 4);
1057 Connection
*conn
= (Connection
*)data
;
1059 checkConnection(conn
);
1061 callbackData
[0] = caml_copy_double(dlTotal
);
1062 callbackData
[1] = caml_copy_double(dlNow
);
1063 callbackData
[2] = caml_copy_double(ulTotal
);
1064 callbackData
[3] = caml_copy_double(ulNow
);
1066 result
= caml_callbackN_exn(Field(conn
->ocamlValues
, Ocaml_PROGRESSFUNCTION
),
1069 int r
= Is_exception_result(result
) ? 1 : Bool_val(result
);
1072 caml_enter_blocking_section();
1076 static int cb_DEBUGFUNCTION(CURL
*debugConnection
,
1077 curl_infotype infoType
,
1079 size_t bufferLength
,
1082 caml_leave_blocking_section();
1085 CAMLlocal3(camlDebugConnection
, camlInfoType
, camlMessage
);
1087 Connection
*conn
= (Connection
*)data
;
1088 (void)debugConnection
; /* not used */
1090 checkConnection(conn
);
1092 camlDebugConnection
= (value
)conn
;
1093 camlInfoType
= Val_long(infoType
);
1094 camlMessage
= caml_alloc_string(bufferLength
);
1096 for (i
= 0; i
< bufferLength
; i
++)
1097 Byte(camlMessage
, i
) = buffer
[i
];
1099 caml_callback3_exn(Field(conn
->ocamlValues
, Ocaml_DEBUGFUNCTION
),
1100 camlDebugConnection
,
1106 caml_enter_blocking_section();
1110 static curlioerr
cb_IOCTLFUNCTION(CURL
*ioctl
,
1114 caml_leave_blocking_section();
1117 CAMLlocal3(camlResult
, camlConnection
, camlCmd
);
1118 Connection
*conn
= (Connection
*)data
;
1119 curlioerr result
= CURLIOE_OK
;
1120 (void)ioctl
; /* not used */
1122 checkConnection(conn
);
1124 if (cmd
== CURLIOCMD_NOP
)
1125 camlCmd
= Val_long(0);
1126 else if (cmd
== CURLIOCMD_RESTARTREAD
)
1127 camlCmd
= Val_long(1);
1129 caml_failwith("Invalid IOCTL Cmd!");
1131 camlConnection
= caml_curl_alloc(conn
);
1133 camlResult
= caml_callback2_exn(Field(conn
->ocamlValues
, Ocaml_IOCTLFUNCTION
),
1137 if (Is_exception_result(camlResult
))
1139 result
= CURLIOE_FAILRESTART
;
1142 switch (Long_val(camlResult
))
1144 case 0: /* CURLIOE_OK */
1145 result
= CURLIOE_OK
;
1148 case 1: /* CURLIOE_UNKNOWNCMD */
1149 result
= CURLIOE_UNKNOWNCMD
;
1152 case 2: /* CURLIOE_FAILRESTART */
1153 result
= CURLIOE_FAILRESTART
;
1156 default: /* Incorrect return value, but let's handle it */
1157 result
= CURLIOE_FAILRESTART
;
1162 caml_enter_blocking_section();
1166 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1167 static int cb_SEEKFUNCTION(void *data
,
1171 caml_leave_blocking_section();
1174 CAMLlocal3(camlResult
, camlOffset
, camlOrigin
);
1175 Connection
*conn
= (Connection
*)data
;
1177 camlOffset
= caml_copy_int64(offset
);
1179 if (origin
== SEEK_SET
)
1180 camlOrigin
= Val_long(0);
1181 else if (origin
== SEEK_CUR
)
1182 camlOrigin
= Val_long(1);
1183 else if (origin
== SEEK_END
)
1184 camlOrigin
= Val_long(2);
1186 caml_failwith("Invalid seek code");
1188 camlResult
= caml_callback2_exn(Field(conn
->ocamlValues
,
1189 Ocaml_SEEKFUNCTION
),
1194 if (Is_exception_result(camlResult
))
1195 result
= CURL_SEEKFUNC_FAIL
;
1197 switch (Int_val(camlResult
))
1199 case 0: result
= CURL_SEEKFUNC_OK
; break;
1200 case 1: result
= CURL_SEEKFUNC_FAIL
; break;
1201 case 2: result
= CURL_SEEKFUNC_CANTSEEK
; break;
1202 default: caml_failwith("Invalid seek result");
1206 caml_enter_blocking_section();
1211 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
1212 static int cb_OPENSOCKETFUNCTION(void *data
,
1213 curlsocktype purpose
,
1214 struct curl_sockaddr
*addr
)
1216 caml_leave_blocking_section();
1220 Connection
*conn
= (Connection
*)data
;
1222 (void)purpose
; /* not used */
1224 sock
= socket(addr
->family
, addr
->socktype
, addr
->protocol
);
1229 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_OPENSOCKETFUNCTION
), Val_int(sock
));
1230 if (Is_exception_result(result
))
1238 caml_enter_blocking_section();
1239 return ((sock
== -1) ? CURL_SOCKET_BAD
: sock
);
1244 ** curl_global_init helper function
1247 CAMLprim value
helper_curl_global_init(value initOption
)
1249 CAMLparam1(initOption
);
1251 switch (Long_val(initOption
))
1253 case 0: /* CURLINIT_GLOBALALL */
1254 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_ALL
)));
1257 case 1: /* CURLINIT_GLOBALSSL */
1258 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_SSL
)));
1261 case 2: /* CURLINIT_GLOBALWIN32 */
1262 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_WIN32
)));
1265 case 3: /* CURLINIT_GLOBALNOTHING */
1266 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_NOTHING
)));
1270 caml_failwith("Invalid Initialization Option");
1274 /* Keep compiler happy, we should never get here due to caml_failwith() */
1275 CAMLreturn(Val_unit
);
1279 ** curl_global_cleanup helper function
1282 CAMLprim value
helper_curl_global_cleanup(void)
1286 curl_global_cleanup();
1288 CAMLreturn(Val_unit
);
1292 ** curl_easy_init helper function
1294 CAMLprim value
helper_curl_easy_init(void)
1299 result
= caml_curl_alloc(newConnection());
1304 CAMLprim value
helper_curl_easy_reset(value conn
)
1307 Connection
*connection
= Connection_val(conn
);
1309 checkConnection(connection
);
1310 curl_easy_reset(connection
->connection
);
1311 resetOcamlValues(connection
);
1313 CAMLreturn(Val_unit
);
1317 ** curl_easy_setopt helper utility functions
1320 #define SETOPT_FUNCTION(name) \
1321 static void handle_##name##FUNCTION(Connection *conn, value option) \
1323 CAMLparam1(option); \
1324 CURLcode result = CURLE_OK; \
1325 Store_field(conn->ocamlValues, Ocaml_##name##FUNCTION, option); \
1326 result = curl_easy_setopt(conn->connection, CURLOPT_##name##FUNCTION, cb_##name##FUNCTION); \
1327 if (result != CURLE_OK) raiseError(conn, result); \
1328 result = curl_easy_setopt(conn->connection, CURLOPT_##name##DATA, conn); \
1329 if (result != CURLE_OK) raiseError(conn, result); \
1333 SETOPT_FUNCTION( WRITE
)
1334 SETOPT_FUNCTION( READ
)
1335 SETOPT_FUNCTION( HEADER
)
1336 SETOPT_FUNCTION( PROGRESS
)
1337 SETOPT_FUNCTION( DEBUG
)
1339 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1340 SETOPT_FUNCTION( SEEK
)
1343 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
1344 SETOPT_FUNCTION( IOCTL
)
1347 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
1348 SETOPT_FUNCTION( OPENSOCKET
)
1351 static void handle_slist(Connection
*conn
, struct curl_slist
** slist
, OcamlValue caml_option
, CURLoption curl_option
, value option
)
1354 CURLcode result
= CURLE_OK
;
1356 Store_field(conn
->ocamlValues
, caml_option
, option
);
1358 free_curl_slist(*slist
);
1361 while (Val_emptylist
!= option
)
1363 *slist
= curl_slist_append(*slist
, String_val(Field(option
, 0)));
1365 option
= Field(option
, 1);
1368 result
= curl_easy_setopt(conn
->connection
, curl_option
, *slist
);
1370 if (result
!= CURLE_OK
)
1371 raiseError(conn
, result
);
1376 static long convert_bit_list(long *map
, size_t map_size
, value option
)
1382 while (Val_emptylist
!= option
)
1384 index
= Int_val(Field(option
, 0));
1385 if ((index
< 0) || ((size_t)index
>= map_size
))
1386 caml_invalid_argument("convert_bit_list");
1390 option
= Field(option
, 1);
1393 CAMLreturnT(long, bits
);
1396 #define SETOPT_STRING(name) \
1397 static void handle_##name(Connection *conn, value option) \
1399 CAMLparam1(option); \
1400 CURLcode result = CURLE_OK; \
1402 Store_field(conn->ocamlValues, Ocaml_##name, option); \
1404 if (conn->curl_##name != NULL) \
1405 free(conn->curl_##name); \
1407 conn->curl_##name = strdup(String_val(option)); \
1409 result = curl_easy_setopt(conn->connection, CURLOPT_##name, conn->curl_##name); \
1411 if (result != CURLE_OK) \
1412 raiseError(conn, result); \
1417 #define SETOPT_VAL_(func_name, curl_option, conv_val) \
1418 static void func_name(Connection *conn, value option) \
1420 CAMLparam1(option); \
1421 CURLcode result = CURLE_OK; \
1423 result = curl_easy_setopt(conn->connection, curl_option, conv_val(option)); \
1425 if (result != CURLE_OK) \
1426 raiseError(conn, result); \
1431 #define SETOPT_VAL(name, conv) SETOPT_VAL_(handle_##name, CURLOPT_##name, conv)
1432 #define SETOPT_BOOL(name) SETOPT_VAL(name, Bool_val)
1433 #define SETOPT_LONG(name) SETOPT_VAL(name, Long_val)
1434 #define SETOPT_INT64(name) SETOPT_VAL(name, Int64_val)
1436 #define SETOPT_SLIST(name) \
1437 static void handle_##name(Connection* conn, value option) \
1439 handle_slist(conn,&(conn->curl_##name),Ocaml_##name,CURLOPT_##name,option); \
1443 SETOPT_LONG( INFILESIZE
)
1444 SETOPT_STRING( PROXY
)
1445 SETOPT_LONG( PROXYPORT
)
1446 SETOPT_BOOL( HTTPPROXYTUNNEL
)
1447 SETOPT_BOOL( VERBOSE
)
1448 SETOPT_BOOL( HEADER
)
1449 SETOPT_BOOL( NOPROGRESS
)
1451 #if HAVE_DECL_CURLOPT_NOSIGNAL
1452 SETOPT_BOOL( NOSIGNAL
)
1455 SETOPT_BOOL( NOBODY
)
1456 SETOPT_BOOL( FAILONERROR
)
1457 SETOPT_BOOL( UPLOAD
)
1459 SETOPT_BOOL( FTPLISTONLY
)
1460 SETOPT_BOOL( FTPAPPEND
)
1463 static void handle_NETRC(Connection
*conn
, value option
)
1466 CURLcode result
= CURLE_OK
;
1469 switch (Long_val(option
))
1471 case 0: /* CURL_NETRC_OPTIONAL */
1472 netrc
= CURL_NETRC_OPTIONAL
;
1475 case 1:/* CURL_NETRC_IGNORED */
1476 netrc
= CURL_NETRC_IGNORED
;
1479 case 2: /* CURL_NETRC_REQUIRED */
1480 netrc
= CURL_NETRC_REQUIRED
;
1484 caml_failwith("Invalid NETRC Option");
1488 result
= curl_easy_setopt(conn
->connection
,
1492 if (result
!= CURLE_OK
)
1493 raiseError(conn
, result
);
1498 #if HAVE_DECL_CURLOPT_ENCODING
1499 static void handle_ENCODING(Connection
*conn
, value option
)
1502 CURLcode result
= CURLE_OK
;
1504 switch (Long_val(option
))
1506 case 0: /* CURL_ENCODING_NONE */
1507 result
= curl_easy_setopt(conn
->connection
,
1512 case 1: /* CURL_ENCODING_DEFLATE */
1513 result
= curl_easy_setopt(conn
->connection
,
1518 case 2: /* CURL_ENCODING_GZIP */
1519 result
= curl_easy_setopt(conn
->connection
,
1524 case 3: /* CURL_ENCODING_ANY */
1525 result
= curl_easy_setopt(conn
->connection
,
1531 caml_failwith("Invalid Encoding Option");
1535 if (result
!= CURLE_OK
)
1536 raiseError(conn
, result
);
1543 SETOPT_BOOL( FOLLOWLOCATION
)
1544 SETOPT_BOOL( TRANSFERTEXT
)
1546 SETOPT_STRING( USERPWD
)
1547 SETOPT_STRING( PROXYUSERPWD
)
1548 SETOPT_STRING( RANGE
)
1550 static void handle_ERRORBUFFER(Connection
*conn
, value option
)
1553 CURLcode result
= CURLE_OK
;
1555 Store_field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
, option
);
1557 if (conn
->curl_ERRORBUFFER
!= NULL
)
1558 free(conn
->curl_ERRORBUFFER
);
1560 conn
->curl_ERRORBUFFER
= malloc(sizeof(char) * CURL_ERROR_SIZE
);
1562 result
= curl_easy_setopt(conn
->connection
,
1563 CURLOPT_ERRORBUFFER
,
1564 conn
->curl_ERRORBUFFER
);
1566 if (result
!= CURLE_OK
)
1567 raiseError(conn
, result
);
1572 SETOPT_LONG( TIMEOUT
)
1574 static void handle_POSTFIELDS(Connection
*conn
, value option
)
1577 CURLcode result
= CURLE_OK
;
1579 Store_field(conn
->ocamlValues
, Ocaml_POSTFIELDS
, option
);
1581 if (conn
->curl_POSTFIELDS
!= NULL
)
1582 free(conn
->curl_POSTFIELDS
);
1584 conn
->curl_POSTFIELDS
= strdup_ml(option
);
1586 result
= curl_easy_setopt(conn
->connection
,
1588 conn
->curl_POSTFIELDS
);
1590 if (result
!= CURLE_OK
)
1591 raiseError(conn
, result
);
1596 SETOPT_LONG( POSTFIELDSIZE
)
1597 SETOPT_STRING( REFERER
)
1598 SETOPT_STRING( USERAGENT
)
1599 SETOPT_STRING( FTPPORT
)
1600 SETOPT_LONG( LOW_SPEED_LIMIT
)
1601 SETOPT_LONG( LOW_SPEED_TIME
)
1602 SETOPT_LONG( RESUME_FROM
)
1603 SETOPT_STRING( COOKIE
)
1605 SETOPT_SLIST( HTTPHEADER
)
1607 static void handle_HTTPPOST(Connection
*conn
, value option
)
1610 CAMLlocal3(listIter
, formItem
, contentType
);
1611 CURLcode result
= CURLE_OK
;
1615 Store_field(conn
->ocamlValues
, Ocaml_HTTPPOST
, option
);
1617 free_curl_slist(conn
->httpPostBuffers
);
1618 if (conn
->httpPostFirst
!= NULL
)
1619 curl_formfree(conn
->httpPostFirst
);
1621 conn
->httpPostBuffers
= NULL
;
1622 conn
->httpPostFirst
= NULL
;
1623 conn
->httpPostLast
= NULL
;
1625 while (!Is_long(listIter
))
1627 formItem
= Field(listIter
, 0);
1629 switch (Tag_val(formItem
))
1631 case 0: /* CURLFORM_CONTENT */
1632 if (Wosize_val(formItem
) < 3)
1634 caml_failwith("Incorrect CURLFORM_CONTENT parameters");
1637 if (Is_long(Field(formItem
, 2)) &&
1638 Long_val(Field(formItem
, 2)) == 0)
1640 curl_formadd(&conn
->httpPostFirst
,
1641 &conn
->httpPostLast
,
1643 String_val(Field(formItem
, 0)),
1644 CURLFORM_NAMELENGTH
,
1645 caml_string_length(Field(formItem
, 0)),
1646 CURLFORM_COPYCONTENTS
,
1647 String_val(Field(formItem
, 1)),
1648 CURLFORM_CONTENTSLENGTH
,
1649 caml_string_length(Field(formItem
, 1)),
1652 else if (Is_block(Field(formItem
, 2)))
1654 contentType
= Field(formItem
, 2);
1656 curl_formadd(&conn
->httpPostFirst
,
1657 &conn
->httpPostLast
,
1659 String_val(Field(formItem
, 0)),
1660 CURLFORM_NAMELENGTH
,
1661 caml_string_length(Field(formItem
, 0)),
1662 CURLFORM_COPYCONTENTS
,
1663 String_val(Field(formItem
, 1)),
1664 CURLFORM_CONTENTSLENGTH
,
1665 caml_string_length(Field(formItem
, 1)),
1666 CURLFORM_CONTENTTYPE
,
1667 String_val(Field(contentType
, 0)),
1672 caml_failwith("Incorrect CURLFORM_CONTENT parameters");
1676 case 1: /* CURLFORM_FILECONTENT */
1677 if (Wosize_val(formItem
) < 3)
1679 caml_failwith("Incorrect CURLFORM_FILECONTENT parameters");
1682 if (Is_long(Field(formItem
, 2)) &&
1683 Long_val(Field(formItem
, 2)) == 0)
1685 curl_formadd(&conn
->httpPostFirst
,
1686 &conn
->httpPostLast
,
1688 String_val(Field(formItem
, 0)),
1689 CURLFORM_NAMELENGTH
,
1690 caml_string_length(Field(formItem
, 0)),
1691 CURLFORM_FILECONTENT
,
1692 String_val(Field(formItem
, 1)),
1695 else if (Is_block(Field(formItem
, 2)))
1697 contentType
= Field(formItem
, 2);
1699 curl_formadd(&conn
->httpPostFirst
,
1700 &conn
->httpPostLast
,
1702 String_val(Field(formItem
, 0)),
1703 CURLFORM_NAMELENGTH
,
1704 caml_string_length(Field(formItem
, 0)),
1705 CURLFORM_FILECONTENT
,
1706 String_val(Field(formItem
, 1)),
1707 CURLFORM_CONTENTTYPE
,
1708 String_val(Field(contentType
, 0)),
1713 caml_failwith("Incorrect CURLFORM_FILECONTENT parameters");
1717 case 2: /* CURLFORM_FILE */
1718 if (Wosize_val(formItem
) < 3)
1720 caml_failwith("Incorrect CURLFORM_FILE parameters");
1723 if (Is_long(Field(formItem
, 2)) &&
1724 Long_val(Field(formItem
, 2)) == 0)
1726 curl_formadd(&conn
->httpPostFirst
,
1727 &conn
->httpPostLast
,
1729 String_val(Field(formItem
, 0)),
1730 CURLFORM_NAMELENGTH
,
1731 caml_string_length(Field(formItem
, 0)),
1733 String_val(Field(formItem
, 1)),
1736 else if (Is_block(Field(formItem
, 2)))
1738 contentType
= Field(formItem
, 2);
1740 curl_formadd(&conn
->httpPostFirst
,
1741 &conn
->httpPostLast
,
1743 String_val(Field(formItem
, 0)),
1744 CURLFORM_NAMELENGTH
,
1745 caml_string_length(Field(formItem
, 0)),
1747 String_val(Field(formItem
, 1)),
1748 CURLFORM_CONTENTTYPE
,
1749 String_val(Field(contentType
, 0)),
1754 caml_failwith("Incorrect CURLFORM_FILE parameters");
1758 case 3: /* CURLFORM_BUFFER */
1759 if (Wosize_val(formItem
) < 4)
1761 caml_failwith("Incorrect CURLFORM_BUFFER parameters");
1764 if (Is_long(Field(formItem
, 3)) &&
1765 Long_val(Field(formItem
, 3)) == 0)
1767 conn
->httpPostBuffers
= curl_slist_prepend_ml(conn
->httpPostBuffers
, Field(formItem
, 2));
1769 curl_formadd(&conn
->httpPostFirst
,
1770 &conn
->httpPostLast
,
1772 String_val(Field(formItem
, 0)),
1773 CURLFORM_NAMELENGTH
,
1774 caml_string_length(Field(formItem
, 0)),
1776 String_val(Field(formItem
, 1)),
1778 conn
->httpPostBuffers
->data
,
1779 CURLFORM_BUFFERLENGTH
,
1780 caml_string_length(Field(formItem
, 2)),
1783 else if (Is_block(Field(formItem
, 3)))
1785 conn
->httpPostBuffers
= curl_slist_prepend_ml(conn
->httpPostBuffers
, Field(formItem
, 2));
1787 contentType
= Field(formItem
, 3);
1789 curl_formadd(&conn
->httpPostFirst
,
1790 &conn
->httpPostLast
,
1792 String_val(Field(formItem
, 0)),
1793 CURLFORM_NAMELENGTH
,
1794 caml_string_length(Field(formItem
, 0)),
1796 String_val(Field(formItem
, 1)),
1798 conn
->httpPostBuffers
->data
,
1799 CURLFORM_BUFFERLENGTH
,
1800 caml_string_length(Field(formItem
, 2)),
1801 CURLFORM_CONTENTTYPE
,
1802 String_val(Field(contentType
, 0)),
1807 caml_failwith("Incorrect CURLFORM_BUFFER parameters");
1812 listIter
= Field(listIter
, 1);
1815 result
= curl_easy_setopt(conn
->connection
,
1817 conn
->httpPostFirst
);
1819 if (result
!= CURLE_OK
)
1820 raiseError(conn
, result
);
1825 SETOPT_STRING( SSLCERT
)
1826 SETOPT_STRING( SSLCERTTYPE
)
1827 SETOPT_STRING( SSLCERTPASSWD
)
1828 SETOPT_STRING( SSLKEY
)
1829 SETOPT_STRING( SSLKEYTYPE
)
1830 SETOPT_STRING( SSLKEYPASSWD
)
1831 SETOPT_STRING( SSLENGINE
)
1832 SETOPT_BOOL( SSLENGINE_DEFAULT
)
1835 SETOPT_SLIST( QUOTE
)
1836 SETOPT_SLIST( POSTQUOTE
)
1838 SETOPT_STRING( COOKIEFILE
)
1839 SETOPT_LONG( SSLVERSION
)
1841 static void handle_TIMECONDITION(Connection
*conn
, value option
)
1844 CURLcode result
= CURLE_OK
;
1845 int timecond
= CURL_TIMECOND_NONE
;
1847 switch (Long_val(option
))
1849 case 0: timecond
= CURL_TIMECOND_NONE
; break;
1850 case 1: timecond
= CURL_TIMECOND_IFMODSINCE
; break;
1851 case 2: timecond
= CURL_TIMECOND_IFUNMODSINCE
; break;
1852 case 3: timecond
= CURL_TIMECOND_LASTMOD
; break;
1854 caml_failwith("Invalid TIMECOND Option");
1858 result
= curl_easy_setopt(conn
->connection
, CURLOPT_TIMECONDITION
, timecond
);
1860 if (result
!= CURLE_OK
)
1861 raiseError(conn
, result
);
1866 SETOPT_VAL( TIMEVALUE
, Int32_val
)
1867 SETOPT_STRING( CUSTOMREQUEST
)
1868 SETOPT_STRING( INTERFACE
)
1870 static void handle_KRB4LEVEL(Connection
*conn
, value option
)
1873 CURLcode result
= CURLE_OK
;
1875 switch (Long_val(option
))
1877 case 0: /* KRB4_NONE */
1878 result
= curl_easy_setopt(conn
->connection
,
1883 case 1: /* KRB4_CLEAR */
1884 result
= curl_easy_setopt(conn
->connection
,
1889 case 2: /* KRB4_SAFE */
1890 result
= curl_easy_setopt(conn
->connection
,
1895 case 3: /* KRB4_CONFIDENTIAL */
1896 result
= curl_easy_setopt(conn
->connection
,
1901 case 4: /* KRB4_PRIVATE */
1902 result
= curl_easy_setopt(conn
->connection
,
1908 caml_failwith("Invalid KRB4 Option");
1912 if (result
!= CURLE_OK
)
1913 raiseError(conn
, result
);
1918 SETOPT_BOOL( SSL_VERIFYPEER
)
1919 SETOPT_STRING( CAINFO
)
1920 SETOPT_STRING( CAPATH
)
1921 SETOPT_BOOL( FILETIME
)
1922 SETOPT_LONG( MAXREDIRS
)
1923 SETOPT_LONG( MAXCONNECTS
)
1925 static void handle_CLOSEPOLICY(Connection
*conn
, value option
)
1928 CURLcode result
= CURLE_OK
;
1930 switch (Long_val(option
))
1932 case 0: /* CLOSEPOLICY_OLDEST */
1933 result
= curl_easy_setopt(conn
->connection
,
1934 CURLOPT_CLOSEPOLICY
,
1935 CURLCLOSEPOLICY_OLDEST
);
1938 case 1: /* CLOSEPOLICY_LEAST_RECENTLY_USED */
1939 result
= curl_easy_setopt(conn
->connection
,
1940 CURLOPT_CLOSEPOLICY
,
1941 CURLCLOSEPOLICY_LEAST_RECENTLY_USED
);
1945 caml_failwith("Invalid CLOSEPOLICY Option");
1949 if (result
!= CURLE_OK
)
1950 raiseError(conn
, result
);
1955 SETOPT_BOOL( FRESH_CONNECT
)
1956 SETOPT_BOOL( FORBID_REUSE
)
1957 SETOPT_STRING( RANDOM_FILE
)
1958 SETOPT_STRING( EGDSOCKET
)
1959 SETOPT_LONG( CONNECTTIMEOUT
)
1960 SETOPT_BOOL( HTTPGET
)
1962 static void handle_SSL_VERIFYHOST(Connection
*conn
, value option
)
1965 CURLcode result
= CURLE_OK
;
1967 switch (Long_val(option
))
1969 case 0: /* SSLVERIFYHOST_NONE */
1970 case 1: /* SSLVERIFYHOST_EXISTENCE */
1971 case 2: /* SSLVERIFYHOST_HOSTNAME */
1972 result
= curl_easy_setopt(conn
->connection
,
1973 CURLOPT_SSL_VERIFYHOST
,
1974 /* map EXISTENCE to HOSTNAME */
1975 Long_val(option
) == 0 ? 0 : 2);
1979 caml_failwith("Invalid SSLVERIFYHOST Option");
1983 if (result
!= CURLE_OK
)
1984 raiseError(conn
, result
);
1989 SETOPT_STRING( COOKIEJAR
)
1990 SETOPT_STRING( SSL_CIPHER_LIST
)
1992 static void handle_HTTP_VERSION(Connection
*conn
, value option
)
1995 CURLcode result
= CURLE_OK
;
1997 long version
= CURL_HTTP_VERSION_NONE
;
1999 switch (Long_val(option
))
2001 case 0: version
= CURL_HTTP_VERSION_NONE
; break;
2002 case 1: version
= CURL_HTTP_VERSION_1_0
; break;
2003 case 2: version
= CURL_HTTP_VERSION_1_1
; break;
2005 #if defined(CURL_HTTP_VERSION_2)
2006 version
= CURL_HTTP_VERSION_2
;
2007 #elif defined(CURL_HTTP_VERSION_2_0)
2008 version
= CURL_HTTP_VERSION_2_0
;
2012 #if defined(CURL_HTTP_VERSION_2TLS)
2013 version
= CURL_HTTP_VERSION_2TLS
;
2017 caml_invalid_argument("CURLOPT_HTTP_VERSION");
2021 result
= curl_easy_setopt(conn
->connection
, CURLOPT_HTTP_VERSION
, version
);
2023 if (result
!= CURLE_OK
)
2024 raiseError(conn
, result
);
2029 SETOPT_BOOL( FTP_USE_EPSV
)
2030 SETOPT_LONG( DNS_CACHE_TIMEOUT
)
2031 SETOPT_BOOL( DNS_USE_GLOBAL_CACHE
)
2033 #if HAVE_DECL_CURLOPT_PRIVATE
2034 SETOPT_STRING( PRIVATE
)
2037 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2038 SETOPT_SLIST( HTTP200ALIASES
)
2041 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2042 SETOPT_BOOL( UNRESTRICTED_AUTH
)
2045 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2046 SETOPT_BOOL( FTP_USE_EPRT
)
2049 #if HAVE_DECL_CURLOPT_HTTPAUTH
2050 static void handle_HTTPAUTH(Connection
*conn
, value option
)
2053 CAMLlocal1(listIter
);
2054 CURLcode result
= CURLE_OK
;
2055 long auth
= CURLAUTH_NONE
;
2059 while (!Is_long(listIter
))
2061 switch (Long_val(Field(listIter
, 0)))
2063 case 0: /* CURLAUTH_BASIC */
2064 auth
|= CURLAUTH_BASIC
;
2067 case 1: /* CURLAUTH_DIGEST */
2068 auth
|= CURLAUTH_DIGEST
;
2071 case 2: /* CURLAUTH_GSSNEGOTIATE */
2072 auth
|= CURLAUTH_GSSNEGOTIATE
;
2075 case 3: /* CURLAUTH_NTLM */
2076 auth
|= CURLAUTH_NTLM
;
2079 case 4: /* CURLAUTH_ANY */
2080 auth
|= CURLAUTH_ANY
;
2083 case 5: /* CURLAUTH_ANYSAFE */
2084 auth
|= CURLAUTH_ANYSAFE
;
2088 caml_failwith("Invalid HTTPAUTH Value");
2092 listIter
= Field(listIter
, 1);
2095 result
= curl_easy_setopt(conn
->connection
,
2099 if (result
!= CURLE_OK
)
2100 raiseError(conn
, result
);
2106 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2107 SETOPT_BOOL( FTP_CREATE_MISSING_DIRS
)
2110 #if HAVE_DECL_CURLOPT_PROXYAUTH
2111 static void handle_PROXYAUTH(Connection
*conn
, value option
)
2114 CAMLlocal1(listIter
);
2115 CURLcode result
= CURLE_OK
;
2116 long auth
= CURLAUTH_NONE
;
2120 while (!Is_long(listIter
))
2122 switch (Long_val(Field(listIter
, 0)))
2124 case 0: /* CURLAUTH_BASIC */
2125 auth
|= CURLAUTH_BASIC
;
2128 case 1: /* CURLAUTH_DIGEST */
2129 auth
|= CURLAUTH_DIGEST
;
2132 case 2: /* CURLAUTH_GSSNEGOTIATE */
2133 auth
|= CURLAUTH_GSSNEGOTIATE
;
2136 case 3: /* CURLAUTH_NTLM */
2137 auth
|= CURLAUTH_NTLM
;
2140 case 4: /* CURLAUTH_ANY */
2141 auth
|= CURLAUTH_ANY
;
2144 case 5: /* CURLAUTH_ANYSAFE */
2145 auth
|= CURLAUTH_ANYSAFE
;
2149 caml_failwith("Invalid HTTPAUTH Value");
2153 listIter
= Field(listIter
, 1);
2156 result
= curl_easy_setopt(conn
->connection
,
2160 if (result
!= CURLE_OK
)
2161 raiseError(conn
, result
);
2167 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2168 SETOPT_LONG( FTP_RESPONSE_TIMEOUT
)
2171 #if HAVE_DECL_CURLOPT_IPRESOLVE
2172 static void handle_IPRESOLVE(Connection
*conn
, value option
)
2175 CURLcode result
= CURLE_OK
;
2177 switch (Long_val(option
))
2179 case 0: /* CURL_IPRESOLVE_WHATEVER */
2180 result
= curl_easy_setopt(conn
->connection
,
2182 CURL_IPRESOLVE_WHATEVER
);
2185 case 1: /* CURL_IPRESOLVE_V4 */
2186 result
= curl_easy_setopt(conn
->connection
,
2191 case 2: /* CURL_IPRESOLVE_V6 */
2192 result
= curl_easy_setopt(conn
->connection
,
2198 caml_failwith("Invalid IPRESOLVE Value");
2202 if (result
!= CURLE_OK
)
2203 raiseError(conn
, result
);
2209 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2210 SETOPT_VAL( MAXFILESIZE
, Int32_val
)
2213 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2214 SETOPT_INT64( INFILESIZE_LARGE
)
2217 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2218 SETOPT_INT64( RESUME_FROM_LARGE
)
2221 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2222 SETOPT_INT64( MAXFILESIZE_LARGE
)
2225 #if HAVE_DECL_CURLOPT_NETRC_FILE
2226 SETOPT_STRING( NETRC_FILE
)
2229 #if HAVE_DECL_CURLOPT_FTP_SSL
2230 static void handle_FTP_SSL(Connection
*conn
, value option
)
2233 CURLcode result
= CURLE_OK
;
2235 switch (Long_val(option
))
2237 case 0: /* CURLFTPSSL_NONE */
2238 result
= curl_easy_setopt(conn
->connection
,
2243 case 1: /* CURLFTPSSL_TRY */
2244 result
= curl_easy_setopt(conn
->connection
,
2249 case 2: /* CURLFTPSSL_CONTROL */
2250 result
= curl_easy_setopt(conn
->connection
,
2252 CURLFTPSSL_CONTROL
);
2255 case 3: /* CURLFTPSSL_ALL */
2256 result
= curl_easy_setopt(conn
->connection
,
2262 caml_failwith("Invalid FTP_SSL Value");
2266 if (result
!= CURLE_OK
)
2267 raiseError(conn
, result
);
2273 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
2274 SETOPT_INT64( POSTFIELDSIZE_LARGE
)
2277 #if HAVE_DECL_CURLOPT_TCP_NODELAY
2278 /* not using SETOPT_BOOL here because of TCP_NODELAY defined in winsock.h */
2279 SETOPT_VAL_( handle_TCP_NODELAY
, CURLOPT_TCP_NODELAY
, Bool_val
)
2282 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
2283 static void handle_FTPSSLAUTH(Connection
*conn
, value option
)
2286 CURLcode result
= CURLE_OK
;
2288 switch (Long_val(option
))
2290 case 0: /* CURLFTPAUTH_DEFAULT */
2291 result
= curl_easy_setopt(conn
->connection
,
2293 CURLFTPAUTH_DEFAULT
);
2296 case 1: /* CURLFTPAUTH_SSL */
2297 result
= curl_easy_setopt(conn
->connection
,
2302 case 2: /* CURLFTPAUTH_TLS */
2303 result
= curl_easy_setopt(conn
->connection
,
2309 caml_failwith("Invalid FTPSSLAUTH value");
2313 if (result
!= CURLE_OK
)
2314 raiseError(conn
, result
);
2320 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
2321 SETOPT_STRING( FTP_ACCOUNT
)
2324 #if HAVE_DECL_CURLOPT_COOKIELIST
2325 SETOPT_STRING( COOKIELIST
)
2328 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
2329 SETOPT_BOOL( IGNORE_CONTENT_LENGTH
)
2332 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
2333 SETOPT_BOOL( FTP_SKIP_PASV_IP
)
2336 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
2337 static void handle_FTP_FILEMETHOD(Connection
*conn
, value option
)
2340 CURLcode result
= CURLE_OK
;
2342 switch (Long_val(option
))
2344 case 0: /* CURLFTPMETHOD_DEFAULT */
2345 result
= curl_easy_setopt(conn
->connection
,
2346 CURLOPT_FTP_FILEMETHOD
,
2347 CURLFTPMETHOD_DEFAULT
);
2350 case 1: /* CURLFTMETHOD_MULTICWD */
2351 result
= curl_easy_setopt(conn
->connection
,
2352 CURLOPT_FTP_FILEMETHOD
,
2353 CURLFTPMETHOD_MULTICWD
);
2356 case 2: /* CURLFTPMETHOD_NOCWD */
2357 result
= curl_easy_setopt(conn
->connection
,
2358 CURLOPT_FTP_FILEMETHOD
,
2359 CURLFTPMETHOD_NOCWD
);
2362 case 3: /* CURLFTPMETHOD_SINGLECWD */
2363 result
= curl_easy_setopt(conn
->connection
,
2364 CURLOPT_FTP_FILEMETHOD
,
2365 CURLFTPMETHOD_SINGLECWD
);
2368 caml_failwith("Invalid FTP_FILEMETHOD value");
2372 if (result
!= CURLE_OK
)
2373 raiseError(conn
, result
);
2379 #if HAVE_DECL_CURLOPT_LOCALPORT
2380 SETOPT_LONG( LOCALPORT
)
2383 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
2384 SETOPT_LONG( LOCALPORTRANGE
)
2387 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
2388 SETOPT_BOOL( CONNECT_ONLY
)
2391 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
2392 SETOPT_INT64( MAX_SEND_SPEED_LARGE
)
2395 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
2396 SETOPT_INT64( MAX_RECV_SPEED_LARGE
)
2399 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
2400 SETOPT_STRING( FTP_ALTERNATIVE_TO_USER
)
2403 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
2404 SETOPT_BOOL( SSL_SESSIONID_CACHE
)
2407 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
2408 static void handle_SSH_AUTH_TYPES(Connection
*conn
, value option
)
2411 CAMLlocal1(listIter
);
2412 CURLcode result
= CURLE_OK
;
2413 long authTypes
= CURLSSH_AUTH_NONE
;
2417 while (!Is_long(listIter
))
2419 switch (Long_val(Field(listIter
, 0)))
2421 case 0: /* CURLSSH_AUTH_ANY */
2422 authTypes
|= CURLSSH_AUTH_ANY
;
2425 case 1: /* CURLSSH_AUTH_PUBLICKEY */
2426 authTypes
|= CURLSSH_AUTH_PUBLICKEY
;
2429 case 2: /* CURLSSH_AUTH_PASSWORD */
2430 authTypes
|= CURLSSH_AUTH_PASSWORD
;
2433 case 3: /* CURLSSH_AUTH_HOST */
2434 authTypes
|= CURLSSH_AUTH_HOST
;
2437 case 4: /* CURLSSH_AUTH_KEYBOARD */
2438 authTypes
|= CURLSSH_AUTH_KEYBOARD
;
2442 caml_failwith("Invalid CURLSSH_AUTH_TYPES Value");
2446 listIter
= Field(listIter
, 1);
2449 result
= curl_easy_setopt(conn
->connection
,
2450 CURLOPT_SSH_AUTH_TYPES
,
2453 if (result
!= CURLE_OK
)
2454 raiseError(conn
, result
);
2460 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
2461 SETOPT_STRING( SSH_PUBLIC_KEYFILE
)
2464 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
2465 SETOPT_STRING( SSH_PRIVATE_KEYFILE
)
2468 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
2469 static void handle_FTP_SSL_CCC(Connection
*conn
, value option
)
2472 CURLcode result
= CURLE_OK
;
2474 switch (Long_val(option
))
2476 case 0: /* CURLFTPSSL_CCC_NONE */
2477 result
= curl_easy_setopt(conn
->connection
,
2478 CURLOPT_FTP_SSL_CCC
,
2479 CURLFTPSSL_CCC_NONE
);
2482 case 1: /* CURLFTPSSL_CCC_PASSIVE */
2483 result
= curl_easy_setopt(conn
->connection
,
2484 CURLOPT_FTP_SSL_CCC
,
2485 CURLFTPSSL_CCC_PASSIVE
);
2488 case 2: /* CURLFTPSSL_CCC_ACTIVE */
2489 result
= curl_easy_setopt(conn
->connection
,
2490 CURLOPT_FTP_SSL_CCC
,
2491 CURLFTPSSL_CCC_ACTIVE
);
2495 caml_failwith("Invalid FTPSSL_CCC value");
2499 if (result
!= CURLE_OK
)
2500 raiseError(conn
, result
);
2506 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
2507 SETOPT_LONG( TIMEOUT_MS
)
2510 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
2511 SETOPT_LONG( CONNECTTIMEOUT_MS
)
2514 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
2515 SETOPT_BOOL( HTTP_TRANSFER_DECODING
)
2518 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
2519 SETOPT_BOOL( HTTP_CONTENT_DECODING
)
2522 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
2523 SETOPT_LONG( NEW_FILE_PERMS
)
2526 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
2527 SETOPT_LONG( NEW_DIRECTORY_PERMS
)
2530 #if HAVE_DECL_CURLOPT_POST301
2531 SETOPT_BOOL( POST301
)
2534 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
2535 SETOPT_STRING( SSH_HOST_PUBLIC_KEY_MD5
)
2538 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
2539 SETOPT_STRING( COPYPOSTFIELDS
)
2542 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
2543 SETOPT_BOOL( PROXY_TRANSFER_MODE
)
2546 #if HAVE_DECL_CURLOPT_AUTOREFERER
2547 SETOPT_BOOL( AUTOREFERER
)
2550 #if HAVE_DECL_CURLOPT_PROXYTYPE
2551 static void handle_PROXYTYPE(Connection
*conn
, value option
)
2554 CURLcode result
= CURLE_OK
;
2557 switch (Long_val(option
))
2559 case 0: proxy_type
= CURLPROXY_HTTP
; break;
2560 case 1: proxy_type
= CURLPROXY_HTTP_1_0
; break;
2561 case 2: proxy_type
= CURLPROXY_SOCKS4
; break;
2562 case 3: proxy_type
= CURLPROXY_SOCKS5
; break;
2563 case 4: proxy_type
= CURLPROXY_SOCKS4A
; break;
2564 case 5: proxy_type
= CURLPROXY_SOCKS5_HOSTNAME
; break;
2566 caml_failwith("Invalid curl proxy type");
2569 result
= curl_easy_setopt(conn
->connection
,
2573 if (result
!= CURLE_OK
)
2574 raiseError(conn
, result
);
2580 #if HAVE_DECL_CURLOPT_PROTOCOLS || HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
2585 CURLPROTO_HTTP
, CURLPROTO_HTTPS
, CURLPROTO_FTP
, CURLPROTO_FTPS
, CURLPROTO_SCP
, CURLPROTO_SFTP
,
2586 CURLPROTO_TELNET
, CURLPROTO_LDAP
, CURLPROTO_LDAPS
, CURLPROTO_DICT
, CURLPROTO_FILE
, CURLPROTO_TFTP
,
2587 /* factor out with autoconf? */
2588 #if defined(CURLPROTO_IMAP)
2593 #if defined(CURLPROTO_IMAPS)
2598 #if defined(CURLPROTO_POP3)
2603 #if defined(CURLPROTO_POP3S)
2608 #if defined(CURLPROTO_SMTP)
2613 #if defined(CURLPROTO_SMTPS)
2618 #if defined(CURLPROTO_RTSP)
2623 #if defined(CURLPROTO_RTMP)
2628 #if defined(CURLPROTO_RTMPT)
2633 #if defined(CURLPROTO_RTMPE)
2638 #if defined(CURLPROTO_RTMPTE)
2643 #if defined(CURLPROTO_RTMPS)
2648 #if defined(CURLPROTO_RTMPTS)
2653 #if defined(CURLPROTO_GOPHER)
2660 static void handle_PROTOCOLSOPTION(CURLoption curlopt
, Connection
*conn
, value option
)
2663 CURLcode result
= CURLE_OK
;
2664 long bits
= convert_bit_list(protoMap
, sizeof(protoMap
) / sizeof(protoMap
[0]), option
);
2666 result
= curl_easy_setopt(conn
->connection
, curlopt
, bits
);
2668 if (result
!= CURLE_OK
)
2669 raiseError(conn
, result
);
2675 #if HAVE_DECL_CURLOPT_PROTOCOLS
2676 static void handle_PROTOCOLS(Connection
*conn
, value option
)
2678 handle_PROTOCOLSOPTION(CURLOPT_PROTOCOLS
, conn
, option
);
2682 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
2683 static void handle_REDIR_PROTOCOLS(Connection
*conn
, value option
)
2685 handle_PROTOCOLSOPTION(CURLOPT_REDIR_PROTOCOLS
, conn
, option
);
2689 #if HAVE_DECL_CURLOPT_RESOLVE
2690 SETOPT_SLIST( RESOLVE
)
2693 #if HAVE_DECL_CURLOPT_DNS_SERVERS
2694 SETOPT_STRING( DNS_SERVERS
)
2697 #if HAVE_DECL_CURLOPT_MAIL_FROM
2698 SETOPT_STRING( MAIL_FROM
)
2701 #if HAVE_DECL_CURLOPT_MAIL_RCPT
2702 SETOPT_SLIST( MAIL_RCPT
)
2705 #if HAVE_DECL_CURLOPT_PIPEWAIT
2706 SETOPT_BOOL( PIPEWAIT
)
2710 ** curl_easy_setopt helper function
2713 #define MAP(name) { handle_ ## name, "CURLOPT_"#name, Ocaml_##name }
2714 #define MAP_NO(name) { NULL, "CURLOPT_"#name , Ocaml_##name }
2715 #define IMM(name) { handle_ ## name, "CURLOPT_"#name, -1 }
2716 #define IMM_NO(name) { NULL, "CURLOPT_"#name , -1 }
2718 CURLOptionMapping implementedOptionMap
[] =
2726 IMM(HTTPPROXYTUNNEL
),
2730 #if HAVE_DECL_CURLOPT_NOSIGNAL
2742 #if HAVE_DECL_CURLOPT_ENCODING
2747 IMM(FOLLOWLOCATION
),
2753 IMM(ERRORBUFFER
), /* mutable buffer, as output value, do not duplicate */
2760 IMM(LOW_SPEED_LIMIT
),
2761 IMM(LOW_SPEED_TIME
),
2773 IMM(SSLENGINE_DEFAULT
),
2777 MAP(HEADERFUNCTION
),
2785 MAP(PROGRESSFUNCTION
),
2786 IMM(SSL_VERIFYPEER
),
2797 IMM(CONNECTTIMEOUT
),
2799 IMM(SSL_VERIFYHOST
),
2801 MAP(SSL_CIPHER_LIST
),
2804 IMM(DNS_CACHE_TIMEOUT
),
2805 IMM(DNS_USE_GLOBAL_CACHE
),
2807 #if HAVE_DECL_CURLOPT_PRIVATE
2812 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2813 MAP(HTTP200ALIASES
),
2815 MAP_NO(HTTP200ALIASES
),
2817 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2818 IMM(UNRESTRICTED_AUTH
),
2820 IMM_NO(UNRESTRICTED_AUTH
),
2822 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2825 IMM_NO(FTP_USE_EPRT
),
2827 #if HAVE_DECL_CURLOPT_HTTPAUTH
2832 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2833 IMM(FTP_CREATE_MISSING_DIRS
),
2835 IMM_NO(FTP_CREATE_MISSING_DIRS
),
2837 #if HAVE_DECL_CURLOPT_PROXYAUTH
2842 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2843 IMM(FTP_RESPONSE_TIMEOUT
),
2845 IMM_NO(FTP_RESPONSE_TIMEOUT
),
2847 #if HAVE_DECL_CURLOPT_IPRESOLVE
2852 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2855 IMM_NO(MAXFILESIZE
),
2857 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2858 IMM(INFILESIZE_LARGE
),
2860 IMM_NO(INFILESIZE_LARGE
),
2862 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2863 IMM(RESUME_FROM_LARGE
),
2865 IMM_NO(RESUME_FROM_LARGE
),
2867 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2868 IMM(MAXFILESIZE_LARGE
),
2870 IMM_NO(MAXFILESIZE_LARGE
),
2872 #if HAVE_DECL_CURLOPT_NETRC_FILE
2877 #if HAVE_DECL_CURLOPT_FTP_SSL
2882 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
2883 IMM(POSTFIELDSIZE_LARGE
),
2885 IMM_NO(POSTFIELDSIZE_LARGE
),
2887 #if HAVE_DECL_CURLOPT_TCP_NODELAY
2890 IMM_NO(TCP_NODELAY
),
2892 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
2897 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
2900 MAP_NO(IOCTLFUNCTION
),
2902 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
2905 MAP_NO(FTP_ACCOUNT
),
2907 #if HAVE_DECL_CURLOPT_COOKIELIST
2912 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
2913 IMM(IGNORE_CONTENT_LENGTH
),
2915 IMM_NO(IGNORE_CONTENT_LENGTH
),
2917 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
2918 IMM(FTP_SKIP_PASV_IP
),
2920 IMM_NO(FTP_SKIP_PASV_IP
),
2922 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
2923 IMM(FTP_FILEMETHOD
),
2925 IMM_NO(FTP_FILEMETHOD
),
2927 #if HAVE_DECL_CURLOPT_LOCALPORT
2932 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
2933 IMM(LOCALPORTRANGE
),
2935 IMM_NO(LOCALPORTRANGE
),
2937 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
2940 IMM_NO(CONNECT_ONLY
),
2942 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
2943 IMM(MAX_SEND_SPEED_LARGE
),
2945 IMM_NO(MAX_SEND_SPEED_LARGE
),
2947 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
2948 IMM(MAX_RECV_SPEED_LARGE
),
2950 IMM_NO(MAX_RECV_SPEED_LARGE
),
2952 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
2953 MAP(FTP_ALTERNATIVE_TO_USER
),
2955 MAP_NO(FTP_ALTERNATIVE_TO_USER
),
2957 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
2958 IMM(SSL_SESSIONID_CACHE
),
2960 IMM_NO(SSL_SESSIONID_CACHE
),
2962 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
2963 IMM(SSH_AUTH_TYPES
),
2965 IMM_NO(SSH_AUTH_TYPES
),
2967 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
2968 MAP(SSH_PUBLIC_KEYFILE
),
2970 MAP_NO(SSH_PUBLIC_KEYFILE
),
2972 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
2973 MAP(SSH_PRIVATE_KEYFILE
),
2975 MAP_NO(SSH_PRIVATE_KEYFILE
),
2977 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
2980 IMM_NO(FTP_SSL_CCC
),
2982 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
2987 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
2988 IMM(CONNECTTIMEOUT_MS
),
2990 IMM_NO(CONNECTTIMEOUT_MS
),
2992 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
2993 IMM(HTTP_TRANSFER_DECODING
),
2995 IMM_NO(HTTP_TRANSFER_DECODING
),
2997 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
2998 IMM(HTTP_CONTENT_DECODING
),
3000 IMM_NO(HTTP_CONTENT_DECODING
),
3002 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
3003 IMM(NEW_FILE_PERMS
),
3005 IMM_NO(NEW_FILE_PERMS
),
3007 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
3008 IMM(NEW_DIRECTORY_PERMS
),
3010 IMM_NO(NEW_DIRECTORY_PERMS
),
3012 #if HAVE_DECL_CURLOPT_POST301
3017 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
3018 MAP(SSH_HOST_PUBLIC_KEY_MD5
),
3020 MAP_NO(SSH_HOST_PUBLIC_KEY_MD5
),
3022 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
3023 MAP(COPYPOSTFIELDS
),
3025 MAP_NO(COPYPOSTFIELDS
),
3027 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
3028 IMM(PROXY_TRANSFER_MODE
),
3030 IMM_NO(PROXY_TRANSFER_MODE
),
3032 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
3035 MAP_NO(SEEKFUNCTION
),
3037 #if HAVE_DECL_CURLOPT_AUTOREFERER
3040 IMM_NO(AUTOREFERER
),
3042 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
3043 MAP(OPENSOCKETFUNCTION
),
3045 MAP_NO(OPENSOCKETFUNCTION
),
3047 #if HAVE_DECL_CURLOPT_PROXYTYPE
3052 #if HAVE_DECL_CURLOPT_PROTOCOLS
3057 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3058 IMM(REDIR_PROTOCOLS
),
3060 IMM_NO(REDIR_PROTOCOLS
),
3062 #if HAVE_DECL_CURLOPT_RESOLVE
3067 #if HAVE_DECL_CURLOPT_DNS_SERVERS
3070 MAP_NO(DNS_SERVERS
),
3072 #if HAVE_DECL_CURLOPT_MAIL_FROM
3077 #if HAVE_DECL_CURLOPT_MAIL_RCPT
3082 #if HAVE_DECL_CURLOPT_PIPEWAIT
3089 static Connection
*duplicateConnection(Connection
*original
)
3091 Connection
*connection
= NULL
;
3094 CURLOptionMapping
* this = NULL
;
3096 caml_enter_blocking_section();
3097 h
= curl_easy_duphandle(original
->connection
);
3098 caml_leave_blocking_section();
3100 connection
= allocConnection(h
);
3102 for (i
= 0; i
< sizeof(implementedOptionMap
)/sizeof(CURLOptionMapping
); i
++)
3104 this = &implementedOptionMap
[i
];
3105 if (-1 == this->ocamlValue
) continue;
3106 if (this->optionHandler
&& (Field(original
->ocamlValues
, this->ocamlValue
) != Val_unit
))
3108 this->optionHandler(connection
, Field(original
->ocamlValues
, this->ocamlValue
));
3115 CAMLprim value
helper_curl_easy_setopt(value conn
, value option
)
3117 CAMLparam2(conn
, option
);
3119 Connection
*connection
= Connection_val(conn
);
3120 CURLOptionMapping
* thisOption
= NULL
;
3121 static value
* exception
= NULL
;
3123 checkConnection(connection
);
3125 data
= Field(option
, 0);
3127 if (Tag_val(option
) < sizeof(implementedOptionMap
)/sizeof(CURLOptionMapping
))
3129 thisOption
= &implementedOptionMap
[Tag_val(option
)];
3130 if (thisOption
->optionHandler
)
3132 thisOption
->optionHandler(connection
, data
);
3136 if (NULL
== exception
)
3138 exception
= caml_named_value("Curl.NotImplemented");
3139 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented");
3142 caml_raise_with_string(*exception
, thisOption
->name
);
3147 caml_failwith("Invalid CURLOPT Option");
3150 CAMLreturn(Val_unit
);
3154 ** curl_easy_perform helper function
3157 CAMLprim value
helper_curl_easy_perform(value conn
)
3160 CURLcode result
= CURLE_OK
;
3161 Connection
*connection
= Connection_val(conn
);
3163 checkConnection(connection
);
3165 caml_enter_blocking_section();
3166 result
= curl_easy_perform(connection
->connection
);
3167 caml_leave_blocking_section();
3169 if (result
!= CURLE_OK
)
3170 raiseError(connection
, result
);
3172 CAMLreturn(Val_unit
);
3176 ** curl_easy_cleanup helper function
3179 CAMLprim value
helper_curl_easy_cleanup(value conn
)
3182 Connection
*connection
= Connection_val(conn
);
3184 checkConnection(connection
);
3186 removeConnection(connection
, 0);
3188 CAMLreturn(Val_unit
);
3192 ** curl_easy_duphandle helper function
3195 CAMLprim value
helper_curl_easy_duphandle(value conn
)
3199 Connection
*connection
= Connection_val(conn
);
3201 checkConnection(connection
);
3203 result
= caml_curl_alloc(duplicateConnection(connection
));
3209 ** curl_easy_getinfo helper function
3212 enum GetInfoResultType
{
3213 StringValue
, LongValue
, DoubleValue
, StringListValue
3216 value
convertStringList(struct curl_slist
*slist
)
3219 CAMLlocal3(result
, current
, next
);
3220 struct curl_slist
*p
= slist
;
3222 result
= Val_int(0);
3223 current
= Val_int(0);
3228 next
= caml_alloc_tuple(2);
3229 Store_field(next
, 0, caml_copy_string(p
->data
));
3230 Store_field(next
, 1, Val_int(0));
3232 if (result
== Val_int(0))
3235 if (current
!= Val_int(0))
3236 Store_field(current
, 1, next
);
3243 curl_slist_free_all(slist
);
3248 CAMLprim value
helper_curl_easy_getinfo(value conn
, value option
)
3250 CAMLparam2(conn
, option
);
3252 CURLcode curlResult
;
3253 Connection
*connection
= Connection_val(conn
);
3254 enum GetInfoResultType resultType
;
3255 char *strValue
= NULL
;
3258 struct curl_slist
*stringListValue
= NULL
;
3260 checkConnection(connection
);
3262 switch(Long_val(option
))
3264 #if HAVE_DECL_CURLINFO_EFFECTIVE_URL
3265 case 0: /* CURLINFO_EFFECTIVE_URL */
3266 resultType
= StringValue
;
3268 curlResult
= curl_easy_getinfo(connection
->connection
,
3269 CURLINFO_EFFECTIVE_URL
,
3273 #pragma message("libcurl does not provide CURLINFO_EFFECTIVE_URL")
3276 #if HAVE_DECL_CURLINFO_RESPONSE_CODE || HAVE_DECL_CURLINFO_HTTP_CODE
3277 case 1: /* CURLINFO_HTTP_CODE */
3278 case 2: /* CURLINFO_RESPONSE_CODE */
3279 #if HAVE_DECL_CURLINFO_RESPONSE_CODE
3280 resultType
= LongValue
;
3282 curlResult
= curl_easy_getinfo(connection
->connection
,
3283 CURLINFO_RESPONSE_CODE
,
3286 resultType
= LongValue
;
3288 curlResult
= curl_easy_getinfo(connection
->connection
,
3295 #if HAVE_DECL_CURLINFO_TOTAL_TIME
3296 case 3: /* CURLINFO_TOTAL_TIME */
3297 resultType
= DoubleValue
;
3299 curlResult
= curl_easy_getinfo(connection
->connection
,
3300 CURLINFO_TOTAL_TIME
,
3305 #if HAVE_DECL_CURLINFO_NAMELOOKUP_TIME
3306 case 4: /* CURLINFO_NAMELOOKUP_TIME */
3307 resultType
= DoubleValue
;
3309 curlResult
= curl_easy_getinfo(connection
->connection
,
3310 CURLINFO_NAMELOOKUP_TIME
,
3315 #if HAVE_DECL_CURLINFO_CONNECT_TIME
3316 case 5: /* CURLINFO_CONNECT_TIME */
3317 resultType
= DoubleValue
;
3319 curlResult
= curl_easy_getinfo(connection
->connection
,
3320 CURLINFO_CONNECT_TIME
,
3325 #if HAVE_DECL_CURLINFO_PRETRANSFER_TIME
3326 case 6: /* CURLINFO_PRETRANSFER_TIME */
3327 resultType
= DoubleValue
;
3329 curlResult
= curl_easy_getinfo(connection
->connection
,
3330 CURLINFO_PRETRANSFER_TIME
,
3335 #if HAVE_DECL_CURLINFO_SIZE_UPLOAD
3336 case 7: /* CURLINFO_SIZE_UPLOAD */
3337 resultType
= DoubleValue
;
3339 curlResult
= curl_easy_getinfo(connection
->connection
,
3340 CURLINFO_SIZE_UPLOAD
,
3345 #if HAVE_DECL_CURLINFO_SIZE_DOWNLOAD
3346 case 8: /* CURLINFO_SIZE_DOWNLOAD */
3347 resultType
= DoubleValue
;
3349 curlResult
= curl_easy_getinfo(connection
->connection
,
3350 CURLINFO_SIZE_DOWNLOAD
,
3355 #if HAVE_DECL_CURLINFO_SPEED_DOWNLOAD
3356 case 9: /* CURLINFO_SPEED_DOWNLOAD */
3357 resultType
= DoubleValue
;
3359 curlResult
= curl_easy_getinfo(connection
->connection
,
3360 CURLINFO_SPEED_DOWNLOAD
,
3365 #if HAVE_DECL_CURLINFO_SPEED_UPLOAD
3366 case 10: /* CURLINFO_SPEED_UPLOAD */
3367 resultType
= DoubleValue
;
3369 curlResult
= curl_easy_getinfo(connection
->connection
,
3370 CURLINFO_SPEED_UPLOAD
,
3376 #if HAVE_DECL_CURLINFO_HEADER_SIZE
3377 case 11: /* CURLINFO_HEADER_SIZE */
3378 resultType
= LongValue
;
3380 curlResult
= curl_easy_getinfo(connection
->connection
,
3381 CURLINFO_HEADER_SIZE
,
3386 #if HAVE_DECL_CURLINFO_REQUEST_SIZE
3387 case 12: /* CURLINFO_REQUEST_SIZE */
3388 resultType
= LongValue
;
3390 curlResult
= curl_easy_getinfo(connection
->connection
,
3391 CURLINFO_REQUEST_SIZE
,
3396 #if HAVE_DECL_CURLINFO_SSL_VERIFYRESULT
3397 case 13: /* CURLINFO_SSL_VERIFYRESULT */
3398 resultType
= LongValue
;
3400 curlResult
= curl_easy_getinfo(connection
->connection
,
3401 CURLINFO_SSL_VERIFYRESULT
,
3406 #if HAVE_DECL_CURLINFO_FILETIME
3407 case 14: /* CURLINFO_FILETIME */
3408 resultType
= DoubleValue
;
3410 curlResult
= curl_easy_getinfo(connection
->connection
,
3414 doubleValue
= longValue
;
3418 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_DOWNLOAD
3419 case 15: /* CURLINFO_CONTENT_LENGTH_DOWNLOAD */
3420 resultType
= DoubleValue
;
3422 curlResult
= curl_easy_getinfo(connection
->connection
,
3423 CURLINFO_CONTENT_LENGTH_DOWNLOAD
,
3428 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_UPLOAD
3429 case 16: /* CURLINFO_CONTENT_LENGTH_UPLOAD */
3430 resultType
= DoubleValue
;
3432 curlResult
= curl_easy_getinfo(connection
->connection
,
3433 CURLINFO_CONTENT_LENGTH_UPLOAD
,
3438 #if HAVE_DECL_CURLINFO_STARTTRANSFER_TIME
3439 case 17: /* CURLINFO_STARTTRANSFER_TIME */
3440 resultType
= DoubleValue
;
3442 curlResult
= curl_easy_getinfo(connection
->connection
,
3443 CURLINFO_STARTTRANSFER_TIME
,
3448 #if HAVE_DECL_CURLINFO_CONTENT_TYPE
3449 case 18: /* CURLINFO_CONTENT_TYPE */
3450 resultType
= StringValue
;
3452 curlResult
= curl_easy_getinfo(connection
->connection
,
3453 CURLINFO_CONTENT_TYPE
,
3458 #if HAVE_DECL_CURLINFO_REDIRECT_TIME
3459 case 19: /* CURLINFO_REDIRECT_TIME */
3460 resultType
= DoubleValue
;
3462 curlResult
= curl_easy_getinfo(connection
->connection
,
3463 CURLINFO_REDIRECT_TIME
,
3468 #if HAVE_DECL_CURLINFO_REDIRECT_COUNT
3469 case 20: /* CURLINFO_REDIRECT_COUNT */
3470 resultType
= LongValue
;
3472 curlResult
= curl_easy_getinfo(connection
->connection
,
3473 CURLINFO_REDIRECT_COUNT
,
3478 #if HAVE_DECL_CURLINFO_PRIVATE
3479 case 21: /* CURLINFO_PRIVATE */
3480 resultType
= StringValue
;
3482 curlResult
= curl_easy_getinfo(connection
->connection
,
3488 #if HAVE_DECL_CURLINFO_HTTP_CONNECTCODE
3489 case 22: /* CURLINFO_HTTP_CONNECTCODE */
3490 resultType
= LongValue
;
3492 curlResult
= curl_easy_getinfo(connection
->connection
,
3493 CURLINFO_HTTP_CONNECTCODE
,
3498 #if HAVE_DECL_CURLINFO_HTTPAUTH_AVAIL
3499 case 23: /* CURLINFO_HTTPAUTH_AVAIL */
3500 resultType
= LongValue
;
3502 curlResult
= curl_easy_getinfo(connection
->connection
,
3503 CURLINFO_HTTPAUTH_AVAIL
,
3508 #if HAVE_DECL_CURLINFO_PROXYAUTH_AVAIL
3509 case 24: /* CURLINFO_PROXYAUTH_AVAIL */
3510 resultType
= LongValue
;
3512 curlResult
= curl_easy_getinfo(connection
->connection
,
3513 CURLINFO_PROXYAUTH_AVAIL
,
3518 #if HAVE_DECL_CURLINFO_OS_ERRNO
3519 case 25: /* CURLINFO_OS_ERRNO */
3520 resultType
= LongValue
;
3522 curlResult
= curl_easy_getinfo(connection
->connection
,
3528 #if HAVE_DECL_CURLINFO_NUM_CONNECTS
3529 case 26: /* CURLINFO_NUM_CONNECTS */
3530 resultType
= LongValue
;
3532 curlResult
= curl_easy_getinfo(connection
->connection
,
3533 CURLINFO_NUM_CONNECTS
,
3538 #if HAVE_DECL_CURLINFO_SSL_ENGINES
3539 case 27: /* CURLINFO_SSL_ENGINES */
3540 resultType
= StringListValue
;
3542 curlResult
= curl_easy_getinfo(connection
->connection
,
3543 CURLINFO_SSL_ENGINES
,
3548 #if HAVE_DECL_CURLINFO_COOKIELIST
3549 case 28: /* CURLINFO_COOKIELIST */
3550 resultType
= StringListValue
;
3552 curlResult
= curl_easy_getinfo(connection
->connection
,
3553 CURLINFO_COOKIELIST
,
3558 #if HAVE_DECL_CURLINFO_LASTSOCKET
3559 case 29: /* CURLINFO_LASTSOCKET */
3560 resultType
= LongValue
;
3562 curlResult
= curl_easy_getinfo(connection
->connection
,
3563 CURLINFO_LASTSOCKET
,
3568 #if HAVE_DECL_CURLINFO_FTP_ENTRY_PATH
3569 case 30: /* CURLINFO_FTP_ENTRY_PATH */
3570 resultType
= StringValue
;
3572 curlResult
= curl_easy_getinfo(connection
->connection
,
3573 CURLINFO_FTP_ENTRY_PATH
,
3578 #if HAVE_DECL_CURLINFO_REDIRECT_URL
3579 case 31: /* CURLINFO_REDIRECT_URL */
3580 resultType
= StringValue
;
3582 curlResult
= curl_easy_getinfo(connection
->connection
,
3583 CURLINFO_REDIRECT_URL
,
3587 #pragma message("libcurl does not provide CURLINFO_REDIRECT_URL")
3590 #if HAVE_DECL_CURLINFO_PRIMARY_IP
3591 case 32: /* CURLINFO_PRIMARY_IP */
3592 resultType
= StringValue
;
3594 curlResult
= curl_easy_getinfo(connection
->connection
,
3595 CURLINFO_PRIMARY_IP
,
3599 #pragma message("libcurl does not provide CURLINFO_PRIMARY_IP")
3602 #if HAVE_DECL_CURLINFO_LOCAL_IP
3603 case 33: /* CURLINFO_LOCAL_IP */
3604 resultType
= StringValue
;
3606 curlResult
= curl_easy_getinfo(connection
->connection
,
3611 #pragma message("libcurl does not provide CURLINFO_LOCAL_IP")
3614 #if HAVE_DECL_CURLINFO_LOCAL_PORT
3615 case 34: /* CURLINFO_LOCAL_PORT */
3616 resultType
= LongValue
;
3618 curlResult
= curl_easy_getinfo(connection
->connection
,
3619 CURLINFO_LOCAL_PORT
,
3623 #pragma message("libcurl does not provide CURLINFO_LOCAL_PORT")
3626 #if HAVE_DECL_CURLINFO_CONDITION_UNMET
3627 case 35: /* CURLINFO_CONDITION_UNMET */
3628 resultType
= LongValue
;
3630 curlResult
= curl_easy_getinfo(connection
->connection
,
3631 CURLINFO_CONDITION_UNMET
,
3635 #pragma message("libcurl does not provide CURLINFO_CONDITION_UNMET")
3639 caml_failwith("Invalid CURLINFO Option");
3643 if (curlResult
!= CURLE_OK
)
3644 raiseError(connection
, curlResult
);
3649 result
= caml_alloc(1, StringValue
);
3650 Store_field(result
, 0, caml_copy_string(strValue
?strValue
:""));
3654 result
= caml_alloc(1, LongValue
);
3655 Store_field(result
, 0, Val_long(longValue
));
3659 result
= caml_alloc(1, DoubleValue
);
3660 Store_field(result
, 0, caml_copy_double(doubleValue
));
3663 case StringListValue
:
3664 result
= caml_alloc(1, StringListValue
);
3665 Store_field(result
, 0, convertStringList(stringListValue
));
3673 ** curl_escape helper function
3676 CAMLprim value
helper_curl_escape(value str
)
3682 curlResult
= curl_escape(String_val(str
), caml_string_length(str
));
3683 result
= caml_copy_string(curlResult
);
3690 ** curl_unescape helper function
3693 CAMLprim value
helper_curl_unescape(value str
)
3699 curlResult
= curl_unescape(String_val(str
), caml_string_length(str
));
3700 result
= caml_copy_string(curlResult
);
3707 ** curl_getdate helper function
3710 CAMLprim value
helper_curl_getdate(value str
, value now
)
3712 CAMLparam2(str
, now
);
3717 curlNow
= (time_t)Double_val(now
);
3718 curlResult
= curl_getdate(String_val(str
), &curlNow
);
3719 result
= caml_copy_double((double)curlResult
);
3725 ** curl_version helper function
3728 CAMLprim value
helper_curl_version(void)
3734 str
= curl_version();
3735 result
= caml_copy_string(str
);
3740 struct CURLVersionBitsMapping
3746 struct CURLVersionBitsMapping versionBitsMap
[] =
3748 {CURL_VERSION_IPV6
, "ipv6"},
3749 {CURL_VERSION_KERBEROS4
, "kerberos4"},
3750 {CURL_VERSION_SSL
, "ssl"},
3751 {CURL_VERSION_LIBZ
, "libz"},
3752 {CURL_VERSION_NTLM
, "ntlm"},
3753 {CURL_VERSION_GSSNEGOTIATE
, "gssnegotiate"},
3754 {CURL_VERSION_DEBUG
, "debug"},
3755 {CURL_VERSION_CURLDEBUG
, "curldebug"},
3756 {CURL_VERSION_ASYNCHDNS
, "asynchdns"},
3757 {CURL_VERSION_SPNEGO
, "spnego"},
3758 {CURL_VERSION_LARGEFILE
, "largefile"},
3759 {CURL_VERSION_IDN
, "idn"},
3760 {CURL_VERSION_SSPI
, "sspi"},
3761 {CURL_VERSION_CONV
, "conv"},
3762 #if HAVE_DECL_CURL_VERSION_TLSAUTH_SRP
3763 {CURL_VERSION_TLSAUTH_SRP
, "srp"},
3765 #if HAVE_DECL_CURL_VERSION_NTLM_WB
3766 {CURL_VERSION_NTLM_WB
, "wb"},
3770 CAMLprim value
caml_curl_version_info(value unit
)
3773 CAMLlocal4(v
, vlist
, vnum
, vfeatures
);
3774 const char* const* p
= NULL
;
3777 curl_version_info_data
* data
= curl_version_info(CURLVERSION_NOW
);
3778 if (NULL
== data
) caml_failwith("curl_version_info");
3780 vlist
= Val_emptylist
;
3781 for (p
= data
->protocols
; NULL
!= *p
; p
++)
3783 vlist
= Val_cons(vlist
, caml_copy_string(*p
));
3786 vfeatures
= Val_emptylist
;
3787 for (i
= 0; i
< sizeof(versionBitsMap
)/sizeof(versionBitsMap
[0]); i
++)
3789 if (0 != (versionBitsMap
[i
].code
& data
->features
))
3790 vfeatures
= Val_cons(vfeatures
, caml_copy_string(versionBitsMap
[i
].name
));
3793 vnum
= caml_alloc_tuple(3);
3794 Store_field(vnum
,0,Val_int(0xFF & (data
->version_num
>> 16)));
3795 Store_field(vnum
,1,Val_int(0xFF & (data
->version_num
>> 8)));
3796 Store_field(vnum
,2,Val_int(0xFF & (data
->version_num
)));
3798 v
= caml_alloc_tuple(12);
3799 Store_field(v
,0,caml_copy_string(data
->version
));
3800 Store_field(v
,1,vnum
);
3801 Store_field(v
,2,caml_copy_string(data
->host
));
3802 Store_field(v
,3,vfeatures
);
3803 Store_field(v
,4,data
->ssl_version
? Val_some(caml_copy_string(data
->ssl_version
)) : Val_none
);
3804 Store_field(v
,5,data
->libz_version
? Val_some(caml_copy_string(data
->libz_version
)) : Val_none
);
3805 Store_field(v
,6,vlist
);
3806 Store_field(v
,7,caml_copy_string((data
->age
>= 1 && data
->ares
) ? data
->ares
: ""));
3807 Store_field(v
,8,Val_int((data
->age
>= 1) ? data
->ares_num
: 0));
3808 Store_field(v
,9,caml_copy_string((data
->age
>= 2 && data
->libidn
) ? data
->libidn
: ""));
3809 Store_field(v
,10,Val_int((data
->age
>= 3) ? data
->iconv_ver_num
: 0));
3810 Store_field(v
,11,caml_copy_string((data
->age
>= 3 && data
->libssh_version
) ? data
->libssh_version
: ""));
3815 CAMLprim value
caml_curl_pause(value conn
, value opts
)
3817 CAMLparam2(conn
, opts
);
3818 CAMLlocal4(v
, vlist
, vnum
, vfeatures
);
3819 Connection
*connection
= Connection_val(conn
);
3823 while (Val_emptylist
!= opts
)
3825 switch (Int_val(Field(opts
,0)))
3827 case 0: bitmask
|= CURLPAUSE_SEND
; break;
3828 case 1: bitmask
|= CURLPAUSE_RECV
; break;
3829 case 2: bitmask
|= CURLPAUSE_ALL
; break;
3830 default: caml_failwith("wrong pauseOption");
3832 opts
= Field(opts
,1);
3835 result
= curl_easy_pause(connection
->connection
,bitmask
);
3836 if (result
!= CURLE_OK
)
3837 raiseError(connection
, result
);
3839 CAMLreturn(Val_unit
);
3843 * Curl multi stack support
3845 * Exported thin wrappers for libcurl are prefixed with caml_curl_multi_.
3846 * Other exported functions are prefixed with caml_curlm_, some of them
3847 * can/should be decomposed into smaller parts.
3850 struct ml_multi_handle
3853 value values
; /* callbacks */
3858 curlmopt_socket_function
,
3859 curlmopt_timer_function
,
3861 /* last, not used */
3865 typedef struct ml_multi_handle ml_multi_handle
;
3867 #define Multi_val(v) (*(ml_multi_handle**)Data_custom_val(v))
3868 #define CURLM_val(v) (Multi_val(v)->handle)
3870 static struct custom_operations curl_multi_ops
= {
3872 custom_finalize_default
,
3873 custom_compare_default
,
3874 custom_hash_default
,
3875 custom_serialize_default
,
3876 custom_deserialize_default
,
3877 #if defined(custom_compare_ext_default)
3878 custom_compare_ext_default
,
3882 CAMLprim value
caml_curl_multi_init(value unit
)
3886 ml_multi_handle
* multi
= (ml_multi_handle
*)caml_stat_alloc(sizeof(ml_multi_handle
));
3887 CURLM
* h
= curl_multi_init();
3891 caml_stat_free(multi
);
3892 caml_failwith("caml_curl_multi_init");
3896 multi
->values
= caml_alloc(multi_values_total
, 0);
3897 caml_register_generational_global_root(&multi
->values
);
3899 v
= caml_alloc_custom(&curl_multi_ops
, sizeof(ml_multi_handle
*), 0, 1);
3900 Multi_val(v
) = multi
;
3905 CAMLprim value
caml_curl_multi_cleanup(value handle
)
3908 ml_multi_handle
* h
= Multi_val(handle
);
3911 CAMLreturn(Val_unit
);
3913 caml_remove_generational_global_root(&h
->values
);
3915 if (CURLM_OK
!= curl_multi_cleanup(h
->handle
))
3916 caml_failwith("caml_curl_multi_cleanup");
3919 Multi_val(handle
) = (ml_multi_handle
*)NULL
;
3921 CAMLreturn(Val_unit
);
3924 static CURL
* curlm_remove_finished(CURLM
* multi_handle
, CURLcode
* result
)
3926 int msgs_in_queue
= 0;
3930 CURLMsg
* msg
= curl_multi_info_read(multi_handle
, &msgs_in_queue
);
3931 if (NULL
== msg
) return NULL
;
3932 if (CURLMSG_DONE
== msg
->msg
)
3934 CURL
* easy_handle
= msg
->easy_handle
;
3935 if (result
) *result
= msg
->data
.result
;
3936 if (CURLM_OK
!= curl_multi_remove_handle(multi_handle
, easy_handle
))
3938 /*caml_failwith("curlm_remove_finished");*/
3945 CAMLprim value
caml_curlm_remove_finished(value v_multi
)
3947 CAMLparam1(v_multi
);
3948 CAMLlocal2(v_easy
, v_tuple
);
3950 CURLM
* multi_handle
;
3952 Connection
* conn
= NULL
;
3954 multi_handle
= CURLM_val(v_multi
);
3956 caml_enter_blocking_section();
3957 handle
= curlm_remove_finished(multi_handle
,&result
);
3958 caml_leave_blocking_section();
3962 CAMLreturn(Val_none
);
3966 conn
= findConnection(handle
);
3967 if (conn
->curl_ERRORBUFFER
!= NULL
)
3969 Store_field(Field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
), 0, caml_copy_string(conn
->curl_ERRORBUFFER
));
3972 /* NB: same handle, but different block */
3973 v_easy
= caml_curl_alloc(conn
);
3974 v_tuple
= caml_alloc(2, 0);
3975 Store_field(v_tuple
,0,v_easy
);
3976 Store_field(v_tuple
,1,Val_int(result
)); /* CURLcode */
3977 CAMLreturn(Val_some(v_tuple
));
3981 static int curlm_wait_data(CURLM
* multi_handle
)
3983 struct timeval timeout
;
3995 /* set a suitable timeout */
3997 timeout
.tv_usec
= 0;
3999 /* get file descriptors from the transfers */
4000 ret
= curl_multi_fdset(multi_handle
, &fdread
, &fdwrite
, &fdexcep
, &maxfd
);
4002 if (ret
== CURLM_OK
&& maxfd
>= 0)
4004 int rc
= select(maxfd
+1, &fdread
, &fdwrite
, &fdexcep
, &timeout
);
4005 if (-1 != rc
) return 0;
4010 CAMLprim value
caml_curlm_wait_data(value v_multi
)
4012 CAMLparam1(v_multi
);
4014 CURLM
* h
= CURLM_val(v_multi
);
4016 caml_enter_blocking_section();
4017 ret
= curlm_wait_data(h
);
4018 caml_leave_blocking_section();
4020 CAMLreturn(Val_bool(0 == ret
));
4023 CAMLprim value
caml_curl_multi_add_handle(value v_multi
, value v_easy
)
4025 CAMLparam2(v_multi
,v_easy
);
4026 CURLM
* multi
= CURLM_val(v_multi
);
4027 Connection
* conn
= Connection_val(v_easy
);
4029 /* prevent collection of OCaml value while the easy handle is used
4030 and may invoke callbacks registered on OCaml side */
4033 /* may invoke callbacks so need to be consistent with locks */
4034 caml_enter_blocking_section();
4035 if (CURLM_OK
!= curl_multi_add_handle(multi
, conn
->connection
))
4037 conn
->refcount
--; /* not added, revert */
4038 caml_leave_blocking_section();
4039 caml_failwith("caml_curl_multi_add_handle");
4041 caml_leave_blocking_section();
4043 CAMLreturn(Val_unit
);
4046 CAMLprim value
caml_curl_multi_remove_handle(value v_multi
, value v_easy
)
4048 CAMLparam2(v_multi
,v_easy
);
4049 CURLM
* multi
= CURLM_val(v_multi
);
4050 Connection
* conn
= Connection_val(v_easy
);
4052 /* may invoke callbacks so need to be consistent with locks */
4053 caml_enter_blocking_section();
4054 if (CURLM_OK
!= curl_multi_remove_handle(multi
, conn
->connection
))
4056 caml_leave_blocking_section();
4057 caml_failwith("caml_curl_multi_remove_handle");
4060 caml_leave_blocking_section();
4062 CAMLreturn(Val_unit
);
4065 CAMLprim value
caml_curl_multi_perform_all(value v_multi
)
4067 CAMLparam1(v_multi
);
4068 int still_running
= 0;
4069 CURLM
* h
= CURLM_val(v_multi
);
4071 caml_enter_blocking_section();
4072 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(h
, &still_running
));
4073 caml_leave_blocking_section();
4075 CAMLreturn(Val_int(still_running
));
4078 CAMLprim value
helper_curl_easy_strerror(value v_code
)
4081 CAMLreturn(caml_copy_string(curl_easy_strerror(Int_val(v_code
))));
4085 * Wrappers for the curl_multi_socket_action infrastructure
4086 * Based on curl hiperfifo.c example
4091 #define Val_socket(v) win_alloc_socket(v)
4094 #error Socket_val not defined in unixsupport.h
4098 #define Socket_val(v) Long_val(v)
4101 #define Val_socket(v) Val_int(v)
4105 static void raise_multi_error(char const* msg
)
4107 static value
* exception
= NULL
;
4109 if (NULL
== exception
)
4111 exception
= caml_named_value("Curl.Multi.Error");
4112 if (NULL
== exception
) caml_invalid_argument("Curl.Multi.Error");
4115 caml_raise_with_string(*exception
, msg
);
4118 static void check_mcode(CURLMcode code
)
4120 char const *s
= NULL
;
4123 case CURLM_OK
: return;
4124 case CURLM_CALL_MULTI_PERFORM
: s
="CURLM_CALL_MULTI_PERFORM"; break;
4125 case CURLM_BAD_HANDLE
: s
="CURLM_BAD_HANDLE"; break;
4126 case CURLM_BAD_EASY_HANDLE
: s
="CURLM_BAD_EASY_HANDLE"; break;
4127 case CURLM_OUT_OF_MEMORY
: s
="CURLM_OUT_OF_MEMORY"; break;
4128 case CURLM_INTERNAL_ERROR
: s
="CURLM_INTERNAL_ERROR"; break;
4129 case CURLM_UNKNOWN_OPTION
: s
="CURLM_UNKNOWN_OPTION"; break;
4130 case CURLM_LAST
: s
="CURLM_LAST"; break;
4131 case CURLM_BAD_SOCKET
: s
="CURLM_BAD_SOCKET"; break;
4132 default : s
="CURLM_unknown"; break;
4134 raise_multi_error(s
);
4137 CAMLprim value
caml_curl_multi_socket_action(value v_multi
, value v_fd
, value v_kind
)
4139 CAMLparam3(v_multi
, v_fd
, v_kind
);
4140 CURLM
* h
= CURLM_val(v_multi
);
4141 int still_running
= 0;
4142 CURLMcode rc
= CURLM_OK
;
4143 curl_socket_t socket
;
4146 if (Val_none
== v_fd
)
4148 socket
= CURL_SOCKET_TIMEOUT
;
4152 socket
= Socket_val(Field(v_fd
, 0));
4155 switch (Int_val(v_kind
))
4158 case 1 : kind
|= CURL_CSELECT_IN
; break;
4159 case 2 : kind
|= CURL_CSELECT_OUT
; break;
4160 case 3 : kind
|= CURL_CSELECT_IN
| CURL_CSELECT_OUT
; break;
4162 raise_multi_error("caml_curl_multi_socket_action");
4165 /* fprintf(stdout,"fd %u kind %u\n",socket, kind); fflush(stdout); */
4167 caml_enter_blocking_section();
4169 rc
= curl_multi_socket_action(h
, socket
, kind
, &still_running
);
4170 } while (rc
== CURLM_CALL_MULTI_PERFORM
);
4171 caml_leave_blocking_section();
4175 CAMLreturn(Val_int(still_running
));
4178 CAMLprim value
caml_curl_multi_socket_all(value v_multi
)
4180 CAMLparam1(v_multi
);
4181 int still_running
= 0;
4182 CURLMcode rc
= CURLM_OK
;
4183 CURLM
* h
= CURLM_val(v_multi
);
4185 caml_enter_blocking_section();
4187 rc
= curl_multi_socket_all(h
, &still_running
);
4188 } while (rc
== CURLM_CALL_MULTI_PERFORM
);
4189 caml_leave_blocking_section();
4193 CAMLreturn(Val_int(still_running
));
4196 static int curlm_sock_cb(CURL
*e
, curl_socket_t sock
, int what
, void *cbp
, void *sockp
)
4198 caml_leave_blocking_section();
4201 CAMLlocal2(v_what
,csock
);
4203 (void)sockp
; /* not used */
4205 /* v_what = Val_int(what); */
4208 case CURL_POLL_NONE
: v_what
= Val_int(0); break;
4209 case CURL_POLL_IN
: v_what
= Val_int(1); break;
4210 case CURL_POLL_OUT
: v_what
= Val_int(2); break;
4211 case CURL_POLL_INOUT
: v_what
= Val_int(3); break;
4212 case CURL_POLL_REMOVE
: v_what
= Val_int(4); break;
4214 fprintf(stderr
, "curlm_sock_cb sock=%d what=%d\n", sock
, what
);
4216 raise_multi_error("curlm_sock_cb"); /* FIXME exception from callback */
4218 csock
=Val_socket(sock
);
4219 caml_callback2(Field(((ml_multi_handle
*)cbp
)->values
,curlmopt_socket_function
),
4223 caml_enter_blocking_section();
4227 CAMLprim value
caml_curl_multi_socketfunction(value v_multi
, value v_cb
)
4229 CAMLparam2(v_multi
, v_cb
);
4230 ml_multi_handle
* multi
= Multi_val(v_multi
);
4232 Store_field(multi
->values
, curlmopt_socket_function
, v_cb
);
4234 curl_multi_setopt(multi
->handle
, CURLMOPT_SOCKETFUNCTION
, curlm_sock_cb
);
4235 curl_multi_setopt(multi
->handle
, CURLMOPT_SOCKETDATA
, multi
);
4237 CAMLreturn(Val_unit
);
4240 static int curlm_timer_cb(CURLM
*multi
, long timeout_ms
, void *userp
)
4242 caml_leave_blocking_section();
4246 caml_callback(Field(((ml_multi_handle
*)userp
)->values
,curlmopt_timer_function
), Val_long(timeout_ms
));
4249 caml_enter_blocking_section();
4253 CAMLprim value
caml_curl_multi_timerfunction(value v_multi
, value v_cb
)
4255 CAMLparam2(v_multi
, v_cb
);
4256 ml_multi_handle
* multi
= Multi_val(v_multi
);
4258 Store_field(multi
->values
, curlmopt_timer_function
, v_cb
);
4260 curl_multi_setopt(multi
->handle
, CURLMOPT_TIMERFUNCTION
, curlm_timer_cb
);
4261 curl_multi_setopt(multi
->handle
, CURLMOPT_TIMERDATA
, multi
);
4263 CAMLreturn(Val_unit
);
4266 CAMLprim value
caml_curl_multi_timeout(value v_multi
)
4268 CAMLparam1(v_multi
);
4270 CURLMcode rc
= CURLM_OK
;
4271 ml_multi_handle
* multi
= Multi_val(v_multi
);
4273 rc
= curl_multi_timeout(multi
->handle
, &ms
);
4277 CAMLreturn(Val_long(ms
));
4280 #define SETMOPT_VAL_(func_name, curl_option, conv_val) \
4281 static void func_name(CURLM *handle, value option) \
4283 CAMLparam1(option); \
4284 CURLcode result = CURLM_OK; \
4286 result = curl_multi_setopt(handle, curl_option, conv_val(option)); \
4288 check_mcode(result); \
4293 #define SETMOPT_VAL(name, conv) SETMOPT_VAL_(handle_multi_##name, CURLMOPT_##name, conv)
4294 #define SETMOPT_BOOL(name) SETMOPT_VAL(name, Bool_val)
4295 #define SETMOPT_LONG(name) SETMOPT_VAL(name, Long_val)
4296 #define SETMOPT_INT64(name) SETMOPT_VAL(name, Int64_val)
4298 long pipeliningMap
[] =
4300 0, /* CURLPIPE_NOTHING */
4301 1, /* CURLPIPE_HTTP1 */
4302 2, /* CURLPIPE_MULTIPLEX */
4305 static void handle_multi_PIPELINING(CURLM
* handle
, value option
)
4308 CURLcode result
= CURLM_OK
;
4310 long bits
= convert_bit_list(pipeliningMap
, sizeof(pipeliningMap
) / sizeof(pipeliningMap
[0]), option
);
4312 result
= curl_multi_setopt(handle
, CURLMOPT_PIPELINING
, bits
);
4314 check_mcode(result
);
4319 #if HAVE_DECL_CURLMOPT_MAXCONNECTS
4320 SETMOPT_LONG( MAXCONNECTS
)
4323 #if HAVE_DECL_CURLMOPT_MAX_PIPELINE_LENGTH
4324 SETMOPT_LONG( MAX_PIPELINE_LENGTH
)
4327 #if HAVE_DECL_CURLMOPT_MAX_HOST_CONNECTIONS
4328 SETMOPT_LONG( MAX_HOST_CONNECTIONS
)
4331 typedef struct CURLMOptionMapping CURLMOptionMapping
;
4332 #define OPT(name) { handle_multi_## name, "CURLMOPT_"#name}
4333 #define NO_OPT(name) { NULL, "CURLMOPT_"#name}
4335 struct CURLMOptionMapping
4337 void (*optionHandler
)(CURLM
*, value
);
4341 CURLMOptionMapping implementedMOptionMap
[] = {
4343 #if HAVE_DECL_CURLMOPT_MAXCONNECTS
4346 NO_OPT( MAXCONNECTS
),
4348 #if HAVE_DECL_CURLMOPT_MAX_PIPELINE_LENGTH
4349 OPT( MAX_PIPELINE_LENGTH
),
4351 NO_OPT( MAX_PIPELINE_LENGTH
),
4353 #if HAVE_DECL_CURLMOPT_MAX_HOST_CONNECTIONS
4354 OPT( MAX_HOST_CONNECTIONS
),
4356 NO_OPT( MAX_HOST_CONNECTIONS
),
4360 CAMLprim value
caml_curl_multi_setopt(value v_multi
, value option
)
4362 CAMLparam2(v_multi
, option
);
4364 CURLM
*handle
= Multi_val(v_multi
)->handle
;
4365 CURLMOptionMapping
* thisOption
= NULL
;
4366 static value
* exception
= NULL
;
4368 data
= Field(option
, 0);
4370 if (Tag_val(option
) < sizeof(implementedMOptionMap
)/sizeof(CURLMOptionMapping
))
4372 thisOption
= &implementedMOptionMap
[Tag_val(option
)];
4373 if (thisOption
->optionHandler
)
4375 thisOption
->optionHandler(handle
, data
);
4379 if (NULL
== exception
)
4381 exception
= caml_named_value("Curl.NotImplemented");
4382 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented");
4385 caml_raise_with_string(*exception
, thisOption
->name
);
4390 caml_failwith("Invalid CURLMOPT Option");
4393 CAMLreturn(Val_unit
);