4 *** Copyright (c) 2003-2008, Lars Nilsson, <lars@quantumchamaeleon.com>
5 *** Copyright (c) 2009, ygrek, <ygrek@autistici.org>
8 #ifndef CAML_NAME_SPACE
9 #define CAML_NAME_SPACE
17 #include <caml/config.h> /* defines HAS_UNISTD */
21 /* suppress false gcc warning on seekFunction */
22 #define CURL_DISABLE_TYPECHECK
23 #include <curl/curl.h>
25 #include <caml/alloc.h>
26 #include <caml/memory.h>
27 #include <caml/mlvalues.h>
28 #include <caml/callback.h>
29 #include <caml/fail.h>
30 #include <caml/unixsupport.h>
31 #include <caml/custom.h>
32 #include <caml/threads.h>
35 #define CAMLdrop caml_local_roots = caml__frame
41 #pragma message("No config file given.")
48 #define Val_none Val_int(0)
55 some
= caml_alloc(1, 0);
56 Store_field( some
, 0, v
);
60 static value
Val_pair(value v1
, value v2
)
64 pair
= caml_alloc_small(2,0);
70 static value
Val_cons(value list
, value v
) { return Val_pair(v
,list
); }
73 * Based on curl hiperfifo.c example
78 #define Val_socket(v) win_alloc_socket(v)
81 #error Socket_val not defined in unixsupport.h
85 #define Socket_val(v) Long_val(v)
88 #define Val_socket(v) Val_int(v)
92 typedef struct Connection Connection
;
93 typedef struct ConnectionList ConnectionList
;
95 #define Connection_val(v) (*(Connection**)Data_custom_val(v))
97 typedef enum OcamlValues
101 Ocaml_HEADERFUNCTION
,
102 Ocaml_PROGRESSFUNCTION
,
103 Ocaml_XFERINFOFUNCTION
,
104 Ocaml_PREREQFUNCTION
,
108 Ocaml_OPENSOCKETFUNCTION
,
109 /* Ocaml_CLOSESOCKETFUNCTION, */
110 Ocaml_SSH_KEYFUNCTION
,
115 /* Not used, last for size */
125 size_t refcount
; /* number of references to this structure */
127 char *curl_ERRORBUFFER
;
128 char *curl_POSTFIELDS
;
129 struct curl_slist
*curl_HTTPHEADER
;
130 struct curl_slist
*httpPostBuffers
;
131 struct curl_httppost
*httpPostFirst
;
132 struct curl_httppost
*httpPostLast
;
133 struct curl_slist
*curl_RESOLVE
;
134 struct curl_slist
*curl_QUOTE
;
135 struct curl_slist
*curl_POSTQUOTE
;
136 struct curl_slist
*curl_HTTP200ALIASES
;
137 struct curl_slist
*curl_MAIL_RCPT
;
138 struct curl_slist
*curl_CONNECT_TO
;
139 #if HAVE_DECL_CURLOPT_MIMEPOST
140 curl_mime
*curl_MIMEPOST
;
144 typedef struct CURLErrorMapping CURLErrorMapping
;
146 struct CURLErrorMapping
152 CURLErrorMapping errorMap
[] =
154 {"CURLE_OK", CURLE_OK
},
155 #if HAVE_DECL_CURLE_UNSUPPORTED_PROTOCOL
156 {"CURLE_UNSUPPORTED_PROTOCOL", CURLE_UNSUPPORTED_PROTOCOL
},
158 {"CURLE_UNSUPPORTED_PROTOCOL", -1},
160 #if HAVE_DECL_CURLE_FAILED_INIT
161 {"CURLE_FAILED_INIT", CURLE_FAILED_INIT
},
163 {"CURLE_FAILED_INIT", -1},
165 #if HAVE_DECL_CURLE_URL_MALFORMAT
166 {"CURLE_URL_MALFORMAT", CURLE_URL_MALFORMAT
},
168 {"CURLE_URL_MALFORMAT", -1},
170 #if HAVE_DECL_CURLE_URL_MALFORMAT_USER
171 {"CURLE_URL_MALFORMAT_USER", CURLE_URL_MALFORMAT_USER
},
173 {"CURLE_URL_MALFORMAT_USER", -1},
175 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_PROXY
176 {"CURLE_COULDNT_RESOLVE_PROXY", CURLE_COULDNT_RESOLVE_PROXY
},
178 {"CURLE_COULDNT_RESOLVE_PROXY", -1},
180 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_HOST
181 {"CURLE_COULDNT_RESOLVE_HOST", CURLE_COULDNT_RESOLVE_HOST
},
183 {"CURLE_COULDNT_RESOLVE_HOST", -1},
185 #if HAVE_DECL_CURLE_COULDNT_CONNECT
186 {"CURLE_COULDNT_CONNECT", CURLE_COULDNT_CONNECT
},
188 {"CURLE_COULDNT_CONNECT", -1},
190 #if HAVE_DECL_CURLE_FTP_WEIRD_SERVER_REPLY
191 {"CURLE_FTP_WEIRD_SERVER_REPLY", CURLE_FTP_WEIRD_SERVER_REPLY
},
193 {"CURLE_FTP_WEIRD_SERVER_REPLY", -1},
195 #if HAVE_DECL_CURLE_FTP_ACCESS_DENIED
196 {"CURLE_FTP_ACCESS_DENIED", CURLE_FTP_ACCESS_DENIED
},
198 {"CURLE_FTP_ACCESS_DENIED", -1},
200 #if HAVE_DECL_CURLE_FTP_USER_PASSWORD_INCORRECT
201 {"CURLE_FTP_USER_PASSWORD_INCORRECT", CURLE_FTP_USER_PASSWORD_INCORRECT
},
203 {"CURLE_FTP_USER_PASSWORD_INCORRECT", -1},
205 #if HAVE_DECL_CURLE_FTP_WEIRD_PASS_REPLY
206 {"CURLE_FTP_WEIRD_PASS_REPLY", CURLE_FTP_WEIRD_PASS_REPLY
},
208 {"CURLE_FTP_WEIRD_PASS_REPLY", -1},
210 #if HAVE_DECL_CURLE_FTP_WEIRD_USER_REPLY
211 {"CURLE_FTP_WEIRD_USER_REPLY", CURLE_FTP_WEIRD_USER_REPLY
},
213 {"CURLE_FTP_WEIRD_USER_REPLY", -1},
215 #if HAVE_DECL_CURLE_FTP_WEIRD_PASV_REPLY
216 {"CURLE_FTP_WEIRD_PASV_REPLY", CURLE_FTP_WEIRD_PASV_REPLY
},
218 {"CURLE_FTP_WEIRD_PASV_REPLY", -1},
220 #if HAVE_DECL_CURLE_FTP_WEIRD_227_FORMAT
221 {"CURLE_FTP_WEIRD_227_FORMAT", CURLE_FTP_WEIRD_227_FORMAT
},
223 {"CURLE_FTP_WEIRD_227_FORMAT", -1},
225 #if HAVE_DECL_CURLE_FTP_CANT_GET_HOST
226 {"CURLE_FTP_CANT_GET_HOST", CURLE_FTP_CANT_GET_HOST
},
228 {"CURLE_FTP_CANT_GET_HOST", -1},
230 #if HAVE_DECL_CURLE_FTP_CANT_RECONNECT
231 {"CURLE_FTP_CANT_RECONNECT", CURLE_FTP_CANT_RECONNECT
},
233 {"CURLE_FTP_CANT_RECONNECT", -1},
235 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_BINARY
236 {"CURLE_FTP_COULDNT_SET_BINARY", CURLE_FTP_COULDNT_SET_BINARY
},
238 {"CURLE_FTP_COULDNT_SET_BINARY", -1},
240 #if HAVE_DECL_CURLE_PARTIAL_FILE
241 {"CURLE_PARTIAL_FILE", CURLE_PARTIAL_FILE
},
243 {"CURLE_PARTIAL_FILE", -1},
245 #if HAVE_DECL_CURLE_FTP_COULDNT_RETR_FILE
246 {"CURLE_FTP_COULDNT_RETR_FILE", CURLE_FTP_COULDNT_RETR_FILE
},
248 {"CURLE_FTP_COULDNT_RETR_FILE", -1},
250 #if HAVE_DECL_CURLE_FTP_WRITE_ERROR
251 {"CURLE_FTP_WRITE_ERROR", CURLE_FTP_WRITE_ERROR
},
253 {"CURLE_FTP_WRITE_ERROR", -1},
255 #if HAVE_DECL_CURLE_FTP_QUOTE_ERROR
256 {"CURLE_FTP_QUOTE_ERROR", CURLE_FTP_QUOTE_ERROR
},
258 {"CURLE_FTP_QUOTE_ERROR", -1},
260 #if HAVE_DECL_CURLE_HTTP_RETURNED_ERROR
261 {"CURLE_HTTP_RETURNED_ERROR", CURLE_HTTP_RETURNED_ERROR
},
263 {"CURLE_HTTP_RETURNED_ERROR", -1},
265 #if HAVE_DECL_CURLE_WRITE_ERROR
266 {"CURLE_WRITE_ERROR", CURLE_WRITE_ERROR
},
268 {"CURLE_WRITE_ERROR", -1},
270 #if HAVE_DECL_CURLE_MALFORMAT_USER
271 {"CURLE_MALFORMAT_USER", CURLE_MALFORMAT_USER
},
273 {"CURLE_MALFORMAT_USER", -1},
275 #if HAVE_DECL_CURLE_FTP_COULDNT_STOR_FILE
276 {"CURLE_FTP_COULDNT_STOR_FILE", CURLE_FTP_COULDNT_STOR_FILE
},
278 {"CURLE_FTP_COULDNT_STOR_FILE", -1},
280 #if HAVE_DECL_CURLE_READ_ERROR
281 {"CURLE_READ_ERROR", CURLE_READ_ERROR
},
283 {"CURLE_READ_ERROR", -1},
285 #if HAVE_DECL_CURLE_OUT_OF_MEMORY
286 {"CURLE_OUT_OF_MEMORY", CURLE_OUT_OF_MEMORY
},
288 {"CURLE_OUT_OF_MEMORY", -1},
290 #if HAVE_DECL_CURLE_OPERATION_TIMEOUTED
291 {"CURLE_OPERATION_TIMEOUTED", CURLE_OPERATION_TIMEOUTED
},
293 {"CURLE_OPERATION_TIMEOUTED", -1},
295 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_ASCII
296 {"CURLE_FTP_COULDNT_SET_ASCII", CURLE_FTP_COULDNT_SET_ASCII
},
298 {"CURLE_FTP_COULDNT_SET_ASCII", -1},
300 #if HAVE_DECL_CURLE_FTP_PORT_FAILED
301 {"CURLE_FTP_PORT_FAILED", CURLE_FTP_PORT_FAILED
},
303 {"CURLE_FTP_PORT_FAILED", -1},
305 #if HAVE_DECL_CURLE_FTP_COULDNT_USE_REST
306 {"CURLE_FTP_COULDNT_USE_REST", CURLE_FTP_COULDNT_USE_REST
},
308 {"CURLE_FTP_COULDNT_USE_REST", -1},
310 #if HAVE_DECL_CURLE_FTP_COULDNT_GET_SIZE
311 {"CURLE_FTP_COULDNT_GET_SIZE", CURLE_FTP_COULDNT_GET_SIZE
},
313 {"CURLE_FTP_COULDNT_GET_SIZE", -1},
315 #if HAVE_DECL_CURLE_HTTP_RANGE_ERROR
316 {"CURLE_HTTP_RANGE_ERROR", CURLE_HTTP_RANGE_ERROR
},
318 {"CURLE_HTTP_RANGE_ERROR", -1},
320 #if HAVE_DECL_CURLE_HTTP_POST_ERROR
321 {"CURLE_HTTP_POST_ERROR", CURLE_HTTP_POST_ERROR
},
323 {"CURLE_HTTP_POST_ERROR", -1},
325 #if HAVE_DECL_CURLE_SSL_CONNECT_ERROR
326 {"CURLE_SSL_CONNECT_ERROR", CURLE_SSL_CONNECT_ERROR
},
328 {"CURLE_SSL_CONNECT_ERROR", -1},
330 #if HAVE_DECL_CURLE_BAD_DOWNLOAD_RESUME
331 {"CURLE_BAD_DOWNLOAD_RESUME", CURLE_BAD_DOWNLOAD_RESUME
},
333 {"CURLE_BAD_DOWNLOAD_RESUME", -1},
335 #if HAVE_DECL_CURLE_FILE_COULDNT_READ_FILE
336 {"CURLE_FILE_COULDNT_READ_FILE", CURLE_FILE_COULDNT_READ_FILE
},
338 {"CURLE_FILE_COULDNT_READ_FILE", -1},
340 #if HAVE_DECL_CURLE_LDAP_CANNOT_BIND
341 {"CURLE_LDAP_CANNOT_BIND", CURLE_LDAP_CANNOT_BIND
},
343 {"CURLE_LDAP_CANNOT_BIND", -1},
345 #if HAVE_DECL_CURLE_LDAP_SEARCH_FAILED
346 {"CURLE_LDAP_SEARCH_FAILED", CURLE_LDAP_SEARCH_FAILED
},
348 {"CURLE_LDAP_SEARCH_FAILED", -1},
350 #if HAVE_DECL_CURLE_LIBRARY_NOT_FOUND
351 {"CURLE_LIBRARY_NOT_FOUND", CURLE_LIBRARY_NOT_FOUND
},
353 {"CURLE_LIBRARY_NOT_FOUND", -1},
355 #if HAVE_DECL_CURLE_FUNCTION_NOT_FOUND
356 {"CURLE_FUNCTION_NOT_FOUND", CURLE_FUNCTION_NOT_FOUND
},
358 {"CURLE_FUNCTION_NOT_FOUND", -1},
360 #if HAVE_DECL_CURLE_ABORTED_BY_CALLBACK
361 {"CURLE_ABORTED_BY_CALLBACK", CURLE_ABORTED_BY_CALLBACK
},
363 {"CURLE_ABORTED_BY_CALLBACK", -1},
365 #if HAVE_DECL_CURLE_BAD_FUNCTION_ARGUMENT
366 {"CURLE_BAD_FUNCTION_ARGUMENT", CURLE_BAD_FUNCTION_ARGUMENT
},
368 {"CURLE_BAD_FUNCTION_ARGUMENT", -1},
370 #if HAVE_DECL_CURLE_BAD_CALLING_ORDER
371 {"CURLE_BAD_CALLING_ORDER", CURLE_BAD_CALLING_ORDER
},
373 {"CURLE_BAD_CALLING_ORDER", -1},
375 #if HAVE_DECL_CURLE_INTERFACE_FAILED
376 {"CURLE_INTERFACE_FAILED", CURLE_INTERFACE_FAILED
},
378 {"CURLE_INTERFACE_FAILED", -1},
380 #if HAVE_DECL_CURLE_BAD_PASSWORD_ENTERED
381 {"CURLE_BAD_PASSWORD_ENTERED", CURLE_BAD_PASSWORD_ENTERED
},
383 {"CURLE_BAD_PASSWORD_ENTERED", -1},
385 #if HAVE_DECL_CURLE_TOO_MANY_REDIRECTS
386 {"CURLE_TOO_MANY_REDIRECTS", CURLE_TOO_MANY_REDIRECTS
},
388 {"CURLE_TOO_MANY_REDIRECTS", -1},
390 #if HAVE_DECL_CURLE_UNKNOWN_TELNET_OPTION
391 {"CURLE_UNKNOWN_TELNET_OPTION", CURLE_UNKNOWN_TELNET_OPTION
},
393 {"CURLE_UNKNOWN_TELNET_OPTION", -1},
395 #if HAVE_DECL_CURLE_TELNET_OPTION_SYNTAX
396 {"CURLE_TELNET_OPTION_SYNTAX", CURLE_TELNET_OPTION_SYNTAX
},
398 {"CURLE_TELNET_OPTION_SYNTAX", -1},
400 #if HAVE_DECL_CURLE_SSL_PEER_CERTIFICATE
401 {"CURLE_SSL_PEER_CERTIFICATE", CURLE_SSL_PEER_CERTIFICATE
},
403 {"CURLE_SSL_PEER_CERTIFICATE", -1},
405 #if HAVE_DECL_CURLE_GOT_NOTHING
406 {"CURLE_GOT_NOTHING", CURLE_GOT_NOTHING
},
408 {"CURLE_GOT_NOTHING", -1},
410 #if HAVE_DECL_CURLE_SSL_ENGINE_NOTFOUND
411 {"CURLE_SSL_ENGINE_NOTFOUND", CURLE_SSL_ENGINE_NOTFOUND
},
413 {"CURLE_SSL_ENGINE_NOTFOUND", -1},
415 #if HAVE_DECL_CURLE_SSL_ENGINE_SETFAILED
416 {"CURLE_SSL_ENGINE_SETFAILED", CURLE_SSL_ENGINE_SETFAILED
},
418 {"CURLE_SSL_ENGINE_SETFAILED", -1},
420 #if HAVE_DECL_CURLE_SEND_ERROR
421 {"CURLE_SEND_ERROR", CURLE_SEND_ERROR
},
423 {"CURLE_SEND_ERROR", -1},
425 #if HAVE_DECL_CURLE_RECV_ERROR
426 {"CURLE_RECV_ERROR", CURLE_RECV_ERROR
},
428 {"CURLE_RECV_ERROR", -1},
430 #if HAVE_DECL_CURLE_SHARE_IN_USE
431 {"CURLE_SHARE_IN_USE", CURLE_SHARE_IN_USE
},
433 {"CURLE_SHARE_IN_USE", -1},
435 #if HAVE_DECL_CURLE_SSL_CERTPROBLEM
436 {"CURLE_SSL_CERTPROBLEM", CURLE_SSL_CERTPROBLEM
},
438 {"CURLE_SSL_CERTPROBLEM", -1},
440 #if HAVE_DECL_CURLE_SSL_CIPHER
441 {"CURLE_SSL_CIPHER", CURLE_SSL_CIPHER
},
443 {"CURLE_SSL_CIPHER", -1},
445 #if HAVE_DECL_CURLE_SSL_CACERT
446 {"CURLE_SSL_CACERT", CURLE_SSL_CACERT
},
448 {"CURLE_SSL_CACERT", -1},
450 #if HAVE_DECL_CURLE_BAD_CONTENT_ENCODING
451 {"CURLE_BAD_CONTENT_ENCODING", CURLE_BAD_CONTENT_ENCODING
},
453 {"CURLE_BAD_CONTENT_ENCODING", -1},
455 #if HAVE_DECL_CURLE_LDAP_INVALID_URL
456 {"CURLE_LDAP_INVALID_URL", CURLE_LDAP_INVALID_URL
},
458 {"CURLE_LDAP_INVALID_URL", -1},
460 #if HAVE_DECL_CURLE_FILESIZE_EXCEEDED
461 {"CURLE_FILESIZE_EXCEEDED", CURLE_FILESIZE_EXCEEDED
},
463 {"CURLE_FILESIZE_EXCEEDED", -1},
465 #if HAVE_DECL_CURLE_FTP_SSL_FAILED
466 {"CURLE_FTP_SSL_FAILED", CURLE_FTP_SSL_FAILED
},
468 {"CURLE_FTP_SSL_FAILED", -1},
470 #if HAVE_DECL_CURLE_SEND_FAIL_REWIND
471 {"CURLE_SEND_FAIL_REWIND", CURLE_SEND_FAIL_REWIND
},
473 {"CURLE_SEND_FAIL_REWIND", -1},
475 #if HAVE_DECL_CURLE_SSL_ENGINE_INITFAILED
476 {"CURLE_SSL_ENGINE_INITFAILED", CURLE_SSL_ENGINE_INITFAILED
},
478 {"CURLE_SSL_ENGINE_INITFAILED", -1},
480 #if HAVE_DECL_CURLE_LOGIN_DENIED
481 {"CURLE_LOGIN_DENIED", CURLE_LOGIN_DENIED
},
483 {"CURLE_LOGIN_DENIED", -1},
485 #if HAVE_DECL_CURLE_TFTP_NOTFOUND
486 {"CURLE_TFTP_NOTFOUND", CURLE_TFTP_NOTFOUND
},
488 {"CURLE_TFTP_NOTFOUND", -1},
490 #if HAVE_DECL_CURLE_TFTP_PERM
491 {"CURLE_TFTP_PERM", CURLE_TFTP_PERM
},
493 {"CURLE_TFTP_PERM", -1},
495 #if HAVE_DECL_CURLE_REMOTE_DISK_FULL
496 {"CURLE_REMOTE_DISK_FULL", CURLE_REMOTE_DISK_FULL
},
498 {"CURLE_REMOTE_DISK_FULL", -1},
500 #if HAVE_DECL_CURLE_TFTP_ILLEGAL
501 {"CURLE_TFTP_ILLEGAL", CURLE_TFTP_ILLEGAL
},
503 {"CURLE_TFTP_ILLEGAL", -1},
505 #if HAVE_DECL_CURLE_TFTP_UNKNOWNID
506 {"CURLE_TFTP_UNKNOWNID", CURLE_TFTP_UNKNOWNID
},
508 {"CURLE_TFTP_UNKNOWNID", -1},
510 #if HAVE_DECL_CURLE_REMOTE_FILE_EXISTS
511 {"CURLE_REMOTE_FILE_EXISTS", CURLE_REMOTE_FILE_EXISTS
},
513 {"CURLE_REMOTE_FILE_EXISTS", -1},
515 #if HAVE_DECL_CURLE_TFTP_NOSUCHUSER
516 {"CURLE_TFTP_NOSUCHUSER", CURLE_TFTP_NOSUCHUSER
},
518 {"CURLE_TFTP_NOSUCHUSER", -1},
520 #if HAVE_DECL_CURLE_CONV_FAILED
521 {"CURLE_CONV_FAILED", CURLE_CONV_FAILED
},
523 {"CURLE_CONV_FAILED", -1},
525 #if HAVE_DECL_CURLE_CONV_REQD
526 {"CURLE_CONV_REQD", CURLE_CONV_REQD
},
528 {"CURLE_CONV_REQD", -1},
530 #if HAVE_DECL_CURLE_SSL_CACERT_BADFILE
531 {"CURLE_SSL_CACERT_BADFILE", CURLE_SSL_CACERT_BADFILE
},
533 {"CURLE_SSL_CACERT_BADFILE", -1},
535 #if HAVE_DECL_CURLE_REMOTE_FILE_NOT_FOUND
536 {"CURLE_REMOTE_FILE_NOT_FOUND", CURLE_REMOTE_FILE_NOT_FOUND
},
538 {"CURLE_REMOTE_FILE_NOT_FOUND", -1},
540 #if HAVE_DECL_CURLE_SSH
541 {"CURLE_SSH", CURLE_SSH
},
545 #if HAVE_DECL_CURLE_SSL_SHUTDOWN_FAILED
546 {"CURLE_SSL_SHUTDOWN_FAILED", CURLE_SSL_SHUTDOWN_FAILED
},
548 {"CURLE_SSL_SHUTDOWN_FAILED", -1},
550 #if HAVE_DECL_CURLE_AGAIN
551 {"CURLE_AGAIN", CURLE_AGAIN
},
555 /* sync check_enums */
559 typedef struct CURLOptionMapping CURLOptionMapping
;
561 struct CURLOptionMapping
563 void (*optionHandler
)(Connection
*, value
);
567 static char* strdup_ml(value v
)
570 p
= (char*)malloc(caml_string_length(v
)+1);
571 memcpy(p
,String_val(v
),caml_string_length(v
)+1); // caml strings have terminating zero
575 static value
ml_copy_string(char const* p
, size_t size
)
577 value v
= caml_alloc_string(size
);
578 memcpy(&Byte(v
,0),p
,size
);
582 /* prepends to the beginning of list */
583 static struct curl_slist
* curl_slist_prepend_ml(struct curl_slist
* list
, value v
)
585 /* FIXME check NULLs */
586 struct curl_slist
* new_item
= (struct curl_slist
*)malloc(sizeof(struct curl_slist
));
588 new_item
->next
= list
;
589 new_item
->data
= strdup_ml(v
);
594 static void free_curl_slist(struct curl_slist
*slist
)
599 curl_slist_free_all(slist
);
602 static void raiseError(Connection
*conn
, CURLcode code
)
605 CAMLlocal1(exceptionData
);
606 const value
*exception
;
607 char *errorString
= "Unknown Error";
610 for (i
= 0; errorMap
[i
].name
!= NULL
; i
++)
612 if (errorMap
[i
].error
== code
)
614 errorString
= errorMap
[i
].name
;
619 exceptionData
= caml_alloc_tuple(3);
621 Store_field(exceptionData
, 0, Val_int(code
));
622 Store_field(exceptionData
, 1, Val_int(code
));
623 Store_field(exceptionData
, 2, caml_copy_string(errorString
));
625 if (conn
!= NULL
&& conn
->curl_ERRORBUFFER
!= NULL
)
627 Store_field(Field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
), 0, caml_copy_string(conn
->curl_ERRORBUFFER
));
630 exception
= caml_named_value("CurlException");
632 if (exception
== NULL
)
633 caml_failwith("CurlException not registered");
635 caml_raise_with_arg(*exception
, exceptionData
);
640 static void resetOcamlValues(Connection
* connection
)
644 for (i
= 0; i
< OcamlValuesSize
; i
++)
645 Store_field(connection
->ocamlValues
, i
, Val_unit
);
648 static Connection
* allocConnection(CURL
* h
)
650 Connection
* connection
= (Connection
*)malloc(sizeof(Connection
));
652 connection
->ocamlValues
= caml_alloc(OcamlValuesSize
, 0);
653 resetOcamlValues(connection
);
654 caml_register_global_root(&connection
->ocamlValues
);
656 connection
->handle
= h
;
657 curl_easy_setopt(h
, CURLOPT_PRIVATE
, connection
);
659 connection
->refcount
= 0;
661 connection
->curl_ERRORBUFFER
= NULL
;
662 connection
->curl_POSTFIELDS
= NULL
;
663 connection
->curl_HTTPHEADER
= NULL
;
664 connection
->httpPostBuffers
= NULL
;
665 connection
->httpPostFirst
= NULL
;
666 connection
->httpPostLast
= NULL
;
667 connection
->curl_QUOTE
= NULL
;
668 connection
->curl_POSTQUOTE
= NULL
;
669 connection
->curl_HTTP200ALIASES
= NULL
;
670 connection
->curl_RESOLVE
= NULL
;
671 connection
->curl_MAIL_RCPT
= NULL
;
672 connection
->curl_CONNECT_TO
= NULL
;
673 #if HAVE_DECL_CURLOPT_MIMEPOST
674 connection
->curl_MIMEPOST
= NULL
;
680 static Connection
*newConnection(void)
684 caml_enter_blocking_section();
685 h
= curl_easy_init();
686 caml_leave_blocking_section();
688 return allocConnection(h
);
691 static void free_if(void* p
) { if (NULL
!= p
) free(p
); }
693 static void removeConnection(Connection
*connection
, int finalization
)
695 const char* fin_url
= NULL
;
697 if (!connection
->handle
)
699 return; /* already cleaned up */
704 /* cannot engage OCaml runtime at finalization, just report leak */
705 if (CURLE_OK
!= curl_easy_getinfo(connection
->handle
, CURLINFO_EFFECTIVE_URL
, &fin_url
) || NULL
== fin_url
)
709 fprintf(stderr
,"Curl: handle %p leaked, conn %p, url %s\n", connection
->handle
, connection
, fin_url
);
714 caml_enter_blocking_section();
715 curl_easy_cleanup(connection
->handle
);
716 caml_leave_blocking_section();
719 connection
->handle
= NULL
;
721 caml_remove_global_root(&connection
->ocamlValues
);
723 free_if(connection
->curl_ERRORBUFFER
);
724 free_if(connection
->curl_POSTFIELDS
);
725 free_curl_slist(connection
->curl_HTTPHEADER
);
726 free_curl_slist(connection
->httpPostBuffers
);
727 if (connection
->httpPostFirst
!= NULL
)
728 curl_formfree(connection
->httpPostFirst
);
729 free_curl_slist(connection
->curl_RESOLVE
);
730 free_curl_slist(connection
->curl_QUOTE
);
731 free_curl_slist(connection
->curl_POSTQUOTE
);
732 free_curl_slist(connection
->curl_HTTP200ALIASES
);
733 free_curl_slist(connection
->curl_MAIL_RCPT
);
734 free_curl_slist(connection
->curl_CONNECT_TO
);
735 #if HAVE_DECL_CURLOPT_MIMEPOST
736 curl_mime_free(connection
->curl_MIMEPOST
);
740 static Connection
* getConnection(CURL
* h
)
742 Connection
* p
= NULL
;
744 if (CURLE_OK
!= curl_easy_getinfo(h
, CURLINFO_PRIVATE
, &p
) || NULL
== p
)
746 caml_failwith("Unknown handle");
753 static void checkConnection(Connection
* connection
)
758 static void checkConnection(Connection
*connection
)
760 if (connection
!= getConnection(connection
->handle
))
762 caml_failwith("Invalid Connection");
767 void op_curl_easy_finalize(value v
)
769 Connection
* conn
= Connection_val(v
);
770 /* same connection may be referenced by several different
771 OCaml values, see e.g. caml_curl_multi_remove_finished */
773 if (0 == conn
->refcount
)
775 removeConnection(conn
, 1);
780 int op_curl_easy_compare(value v1
, value v2
)
782 size_t p1
= (size_t)Connection_val(v1
);
783 size_t p2
= (size_t)Connection_val(v2
);
784 return (p1
== p2
? 0 : (p1
> p2
? 1 : -1)); /* compare addresses */
787 intnat
op_curl_easy_hash(value v
)
789 return (size_t)Connection_val(v
); /* address */
792 static struct custom_operations curl_easy_ops
= {
794 op_curl_easy_finalize
,
795 op_curl_easy_compare
,
797 custom_serialize_default
,
798 custom_deserialize_default
,
799 #if defined(custom_compare_ext_default)
800 custom_compare_ext_default
,
804 value
caml_curl_alloc(Connection
* conn
)
806 value v
= caml_alloc_custom(&curl_easy_ops
, sizeof(Connection
*), 0, 1);
807 Connection_val(v
) = conn
;
812 static size_t cb_WRITEFUNCTION(char *ptr
, size_t size
, size_t nmemb
, void *data
)
814 caml_leave_blocking_section();
817 CAMLlocal2(result
, str
);
818 Connection
*conn
= (Connection
*)data
;
820 checkConnection(conn
);
822 str
= ml_copy_string(ptr
,size
*nmemb
);
824 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_WRITEFUNCTION
), str
);
826 size_t r
= Is_exception_result(result
) ? 0 : Int_val(result
);
829 caml_enter_blocking_section();
833 static size_t cb_WRITEFUNCTION2(char *ptr
, size_t size
, size_t nmemb
, void *data
)
835 caml_leave_blocking_section();
838 CAMLlocal2(result
, str
);
839 Connection
*conn
= (Connection
*)data
;
841 checkConnection(conn
);
843 str
= ml_copy_string(ptr
,size
*nmemb
);
845 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_WRITEFUNCTION
), str
);
849 if (!Is_exception_result(result
))
851 if (Is_block(result
)) /* Proceed */
857 if (0 == Int_val(result
)) /* Pause */
858 r
= CURL_WRITEFUNC_PAUSE
;
865 caml_enter_blocking_section();
869 static size_t cb_READFUNCTION(void *ptr
, size_t size
, size_t nmemb
, void *data
)
871 caml_leave_blocking_section();
875 Connection
*conn
= (Connection
*)data
;
878 checkConnection(conn
);
880 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_READFUNCTION
),
881 Val_int(size
*nmemb
));
883 size_t r
= CURL_READFUNC_ABORT
;
885 if (!Is_exception_result(result
))
887 length
= caml_string_length(result
);
889 if (length
<= size
*nmemb
)
891 memcpy(ptr
, String_val(result
), length
);
898 caml_enter_blocking_section();
902 static size_t cb_READFUNCTION2(void *ptr
, size_t size
, size_t nmemb
, void *data
)
904 caml_leave_blocking_section();
908 Connection
*conn
= (Connection
*)data
;
911 checkConnection(conn
);
913 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_READFUNCTION
),
914 Val_int(size
*nmemb
));
916 size_t r
= CURL_READFUNC_ABORT
;
918 if (!Is_exception_result(result
))
920 if (Is_block(result
)) /* Proceed */
922 result
= Field(result
,0);
924 length
= caml_string_length(result
);
926 if (length
<= size
*nmemb
)
928 memcpy(ptr
, String_val(result
), length
);
934 if (0 == Int_val(result
)) /* Pause */
935 r
= CURL_READFUNC_PAUSE
;
942 caml_enter_blocking_section();
946 static size_t cb_HEADERFUNCTION(char *ptr
, size_t size
, size_t nmemb
, void *data
)
948 caml_leave_blocking_section();
951 CAMLlocal2(result
,str
);
952 Connection
*conn
= (Connection
*)data
;
954 checkConnection(conn
);
956 str
= ml_copy_string(ptr
,size
*nmemb
);
958 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_HEADERFUNCTION
), str
);
960 size_t r
= Is_exception_result(result
) ? 0 : Int_val(result
);
963 caml_enter_blocking_section();
967 static int cb_PROGRESSFUNCTION(void *data
,
973 caml_leave_blocking_section();
977 CAMLlocalN(callbackData
, 4);
978 Connection
*conn
= (Connection
*)data
;
980 checkConnection(conn
);
982 callbackData
[0] = caml_copy_double(dlTotal
);
983 callbackData
[1] = caml_copy_double(dlNow
);
984 callbackData
[2] = caml_copy_double(ulTotal
);
985 callbackData
[3] = caml_copy_double(ulNow
);
987 result
= caml_callbackN_exn(Field(conn
->ocamlValues
, Ocaml_PROGRESSFUNCTION
),
990 int r
= Is_exception_result(result
) ? 1 : Bool_val(result
);
993 caml_enter_blocking_section();
997 static int cb_XFERINFOFUNCTION(void *data
,
1003 caml_leave_blocking_section();
1007 CAMLlocalN(callbackData
, 4);
1008 Connection
*conn
= (Connection
*)data
;
1010 checkConnection(conn
);
1012 callbackData
[0] = caml_copy_int64(dlTotal
);
1013 callbackData
[1] = caml_copy_int64(dlNow
);
1014 callbackData
[2] = caml_copy_int64(ulTotal
);
1015 callbackData
[3] = caml_copy_int64(ulNow
);
1017 result
= caml_callbackN_exn(Field(conn
->ocamlValues
, Ocaml_XFERINFOFUNCTION
),
1020 int r
= Is_exception_result(result
) ? 1 : Bool_val(result
);
1023 caml_enter_blocking_section();
1027 #if HAVE_DECL_CURLOPT_PREREQFUNCTION
1028 static int cb_PREREQFUNCTION(void *data
,
1029 char *conn_primary_ip
,
1030 char *conn_local_ip
,
1031 int conn_primary_port
,
1032 int conn_local_port
)
1034 caml_leave_blocking_section();
1038 CAMLlocalN(callbackData
, 4);
1039 Connection
*conn
= (Connection
*)data
;
1041 checkConnection(conn
);
1043 callbackData
[0] = caml_copy_string(conn_primary_ip
);
1044 callbackData
[1] = caml_copy_string(conn_local_ip
);
1045 callbackData
[2] = Val_int(conn_primary_port
);
1046 callbackData
[3] = Val_int(conn_local_port
);
1048 result
= caml_callbackN_exn(Field(conn
->ocamlValues
, Ocaml_PREREQFUNCTION
),
1051 int r
= Is_exception_result(result
) ? 1 : Bool_val(result
);
1054 caml_enter_blocking_section();
1059 static int cb_DEBUGFUNCTION(CURL
*debugConnection
,
1060 curl_infotype infoType
,
1062 size_t bufferLength
,
1065 caml_leave_blocking_section();
1068 CAMLlocal3(camlDebugConnection
, camlInfoType
, camlMessage
);
1069 Connection
*conn
= (Connection
*)data
;
1070 (void)debugConnection
; /* not used */
1072 checkConnection(conn
);
1074 camlDebugConnection
= caml_curl_alloc(conn
);
1075 camlMessage
= ml_copy_string(buffer
,bufferLength
);
1076 camlInfoType
= Val_long(infoType
<= CURLINFO_SSL_DATA_OUT
/* sync check_enums */ ? infoType
: CURLINFO_END
);
1078 caml_callback3_exn(Field(conn
->ocamlValues
, Ocaml_DEBUGFUNCTION
),
1079 camlDebugConnection
,
1085 caml_enter_blocking_section();
1089 static curlioerr
cb_IOCTLFUNCTION(CURL
*ioctl
,
1093 caml_leave_blocking_section();
1096 CAMLlocal3(camlResult
, camlConnection
, camlCmd
);
1097 Connection
*conn
= (Connection
*)data
;
1098 curlioerr result
= CURLIOE_OK
;
1099 (void)ioctl
; /* not used */
1101 checkConnection(conn
);
1103 if (cmd
== CURLIOCMD_NOP
)
1104 camlCmd
= Val_long(0);
1105 else if (cmd
== CURLIOCMD_RESTARTREAD
)
1106 camlCmd
= Val_long(1);
1108 caml_failwith("Invalid IOCTL Cmd!");
1110 camlConnection
= caml_curl_alloc(conn
);
1112 camlResult
= caml_callback2_exn(Field(conn
->ocamlValues
, Ocaml_IOCTLFUNCTION
),
1116 if (Is_exception_result(camlResult
))
1118 result
= CURLIOE_FAILRESTART
;
1121 switch (Long_val(camlResult
))
1123 case 0: /* CURLIOE_OK */
1124 result
= CURLIOE_OK
;
1127 case 1: /* CURLIOE_UNKNOWNCMD */
1128 result
= CURLIOE_UNKNOWNCMD
;
1131 case 2: /* CURLIOE_FAILRESTART */
1132 result
= CURLIOE_FAILRESTART
;
1135 default: /* Incorrect return value, but let's handle it */
1136 result
= CURLIOE_FAILRESTART
;
1141 caml_enter_blocking_section();
1145 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1146 static int cb_SEEKFUNCTION(void *data
,
1150 caml_leave_blocking_section();
1153 CAMLlocal3(camlResult
, camlOffset
, camlOrigin
);
1154 Connection
*conn
= (Connection
*)data
;
1156 camlOffset
= caml_copy_int64(offset
);
1158 if (origin
== SEEK_SET
)
1159 camlOrigin
= Val_long(0);
1160 else if (origin
== SEEK_CUR
)
1161 camlOrigin
= Val_long(1);
1162 else if (origin
== SEEK_END
)
1163 camlOrigin
= Val_long(2);
1165 caml_failwith("Invalid seek code");
1167 camlResult
= caml_callback2_exn(Field(conn
->ocamlValues
,
1168 Ocaml_SEEKFUNCTION
),
1173 if (Is_exception_result(camlResult
))
1174 result
= CURL_SEEKFUNC_FAIL
;
1176 switch (Int_val(camlResult
))
1178 case 0: result
= CURL_SEEKFUNC_OK
; break;
1179 case 1: result
= CURL_SEEKFUNC_FAIL
; break;
1180 case 2: result
= CURL_SEEKFUNC_CANTSEEK
; break;
1181 default: caml_failwith("Invalid seek result");
1185 caml_enter_blocking_section();
1190 static int cb_OPENSOCKETFUNCTION(void *data
,
1191 curlsocktype purpose
,
1192 struct curl_sockaddr
*addr
)
1194 caml_leave_blocking_section();
1198 Connection
*conn
= (Connection
*)data
;
1200 (void)purpose
; /* not used */
1202 sock
= socket(addr
->family
, addr
->socktype
, addr
->protocol
);
1207 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_OPENSOCKETFUNCTION
), Val_int(sock
));
1208 if (Is_exception_result(result
))
1216 caml_enter_blocking_section();
1217 return ((sock
== -1) ? CURL_SOCKET_BAD
: sock
);
1221 static int cb_CLOSESOCKETFUNCTION(void *data,
1222 curl_socket_t socket)
1224 caml_leave_blocking_section();
1227 CAMLlocal1(camlResult);
1228 Connection *conn = (Connection *)data;
1231 camlResult = caml_callback_exn(Field(conn->ocamlValues, Ocaml_CLOSESOCKETFUNCTION), Val_int(socket));
1232 if (Is_exception_result(camlResult))
1238 caml_enter_blocking_section();
1243 static int cb_SSH_KEYFUNCTION(CURL
*easy
,
1244 const struct curl_khkey
*knownkey
,
1245 const struct curl_khkey
*foundkey
,
1246 enum curl_khmatch match
,
1249 caml_leave_blocking_section();
1252 CAMLlocal3(v_found
, v_match
, v_result
);
1253 Connection
*conn
= (Connection
*)clientp
;
1254 int res
= CURLKHSTAT_REJECT
;
1257 case CURLKHMATCH_OK
:
1258 v_match
= Val_int(0);
1260 case CURLKHMATCH_MISMATCH
:
1261 v_match
= caml_alloc_small(1, 0);
1262 Field(v_match
, 0) = ml_copy_string(knownkey
->key
, knownkey
->len
? knownkey
->len
: strlen(knownkey
->key
));
1264 case CURLKHMATCH_MISSING
:
1265 v_match
= Val_int(1);
1268 caml_failwith("Invalid CURL_SSH_KEYFUNCTION argument");
1272 v_found
= ml_copy_string(foundkey
->key
, foundkey
->len
? foundkey
->len
: strlen(foundkey
->key
));
1273 v_result
= caml_callback2_exn(Field(conn
->ocamlValues
, Ocaml_SSH_KEYFUNCTION
), v_match
, v_found
);
1275 if (!Is_exception_result(v_result
)) {
1276 switch (Int_val(v_result
)) {
1278 res
= CURLKHSTAT_FINE_ADD_TO_FILE
;
1281 res
= CURLKHSTAT_FINE
;
1284 res
= CURLKHSTAT_REJECT
;
1287 res
= CURLKHSTAT_DEFER
;
1290 caml_failwith("Invalid CURLOPT_SSH_KEYFUNCTION return value");
1297 caml_enter_blocking_section();
1301 #if HAVE_DECL_CURL_GLOBAL_SSLSET
1303 /* Same order as in OCaml */
1304 curl_sslbackend sslBackendMap
[] = {
1305 #if HAVE_DECL_CURLSSLBACKEND_NONE
1306 CURLSSLBACKEND_NONE
,
1310 #if HAVE_DECL_CURLSSLBACKEND_OPENSSL
1311 CURLSSLBACKEND_OPENSSL
,
1315 #if HAVE_DECL_CURLSSLBACKEND_GNUTLS
1316 CURLSSLBACKEND_GNUTLS
,
1320 #if HAVE_DECL_CURLSSLBACKEND_NSS
1325 #if HAVE_DECL_CURLSSLBACKEND_GSKIT
1326 CURLSSLBACKEND_GSKIT
,
1330 #if HAVE_DECL_CURLSSLBACKEND_WOLFSSL
1331 CURLSSLBACKEND_WOLFSSL
,
1335 #if HAVE_DECL_CURLSSLBACKEND_SCHANNEL
1336 CURLSSLBACKEND_SCHANNEL
,
1340 #if HAVE_DECL_CURLSSLBACKEND_SECURETRANSPORT
1341 CURLSSLBACKEND_SECURETRANSPORT
,
1345 #if HAVE_DECL_CURLSSLBACKEND_MBEDTLS
1346 CURLSSLBACKEND_MBEDTLS
,
1350 #if HAVE_DECL_CURLSSLBACKEND_MESALINK
1351 CURLSSLBACKEND_MESALINK
,
1355 #if HAVE_DECL_CURLSSLBACKEND_BEARSSL
1356 CURLSSLBACKEND_BEARSSL
,
1362 /* Same order as in OCaml */
1363 CURLsslset sslsetMap
[] = {
1365 CURLSSLSET_UNKNOWN_BACKEND
,
1366 CURLSSLSET_TOO_LATE
,
1367 CURLSSLSET_NO_BACKENDS
,
1370 static void raiseSslsetError(CURLsslset err
)
1373 const value
*exception
;
1376 for (i
= 0, found
= -1; i
< sizeof(sslsetMap
) / sizeof(sslsetMap
[0]); i
++) {
1377 if (sslsetMap
[i
] == err
) {
1384 caml_invalid_argument("Unexpected CURLsslset value");
1386 exception
= caml_named_value("CurlSslSetException");
1387 if (exception
== NULL
) caml_invalid_argument("CurlSslSetException not registered");
1389 caml_raise_with_arg(*exception
, Val_int(found
));
1395 value
caml_curl_global_sslset(value v_backend
)
1397 CAMLparam1(v_backend
);
1399 curl_sslbackend backend
= sslBackendMap
[Int_val(v_backend
)];
1400 CURLsslset res
= curl_global_sslset(backend
, NULL
, NULL
);
1402 if (res
!= CURLSSLSET_OK
)
1403 raiseSslsetError(res
);
1405 CAMLreturn(Val_unit
);
1408 value
caml_curl_global_sslset_str(value v_backend_str
)
1410 CAMLparam1(v_backend_str
);
1412 CURLsslset res
= curl_global_sslset(-1, String_val(v_backend_str
), NULL
);
1414 if (res
!= CURLSSLSET_OK
)
1415 raiseSslsetError(res
);
1417 CAMLreturn(Val_unit
);
1420 value
caml_curl_global_sslsetavail(value v_unit
)
1424 const curl_ssl_backend
**backends
;
1428 res
= curl_global_sslset(-1, NULL
, &backends
);
1430 if (res
!= CURLSSLSET_UNKNOWN_BACKEND
)
1431 raiseSslsetError(res
);
1433 lst
= Val_emptylist
;
1435 for (i
= 0; backends
[i
] != NULL
; i
++) {
1438 for (j
= 0; j
< sizeof(sslBackendMap
) / sizeof(sslBackendMap
[0]); j
++) {
1439 if (sslBackendMap
[j
] == backends
[i
]->id
) {
1445 /* If an unknown backend is returned, it is ignored */
1447 lst
= Val_cons(lst
, Val_long(found
));
1454 value
caml_curl_global_sslsetavail_str(value v_unit
)
1458 const curl_ssl_backend
**backends
;
1462 res
= curl_global_sslset(-1, NULL
, &backends
);
1464 if (res
!= CURLSSLSET_UNKNOWN_BACKEND
)
1465 raiseSslsetError(res
);
1467 lst
= Val_emptylist
;
1469 for (i
= 0; backends
[i
] != NULL
; i
++) {
1470 lst
= Val_cons(lst
, caml_copy_string(backends
[i
]->name
));
1476 value
caml_curl_global_sslset(value v_ignored
)
1478 const value
*exception
= caml_named_value("Curl.NotImplemented");
1479 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented not registered");
1480 caml_raise_with_string(*exception
, "curl_global_sslset");
1485 value
caml_curl_global_sslset_str(value v_ignored
)
1487 /* Argument is ignored */
1488 return caml_curl_global_sslset(Val_unit
);
1490 value
caml_curl_global_sslsetavail(value v_ignored
)
1492 /* Argument is ignored */
1493 return caml_curl_global_sslset(Val_unit
);
1495 value
caml_curl_global_sslsetavail_str(value v_ignored
)
1497 /* Argument is ignored */
1498 return caml_curl_global_sslset(Val_unit
);
1500 #endif /* HAVE_DECL_CURL_GLOBAL_SSLSET */
1503 ** curl_global_init helper function
1506 value
caml_curl_global_init(value initOption
)
1508 CAMLparam1(initOption
);
1510 switch (Long_val(initOption
))
1512 case 0: /* CURLINIT_GLOBALALL */
1513 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_ALL
)));
1516 case 1: /* CURLINIT_GLOBALSSL */
1517 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_SSL
)));
1520 case 2: /* CURLINIT_GLOBALWIN32 */
1521 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_WIN32
)));
1524 case 3: /* CURLINIT_GLOBALNOTHING */
1525 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_NOTHING
)));
1529 caml_failwith("Invalid Initialization Option");
1533 /* Keep compiler happy, we should never get here due to caml_failwith() */
1534 CAMLreturn(Val_unit
);
1538 ** curl_global_cleanup helper function
1541 value
caml_curl_global_cleanup(void)
1545 curl_global_cleanup();
1547 CAMLreturn(Val_unit
);
1551 ** curl_easy_init helper function
1553 value
caml_curl_easy_init(void)
1558 result
= caml_curl_alloc(newConnection());
1563 value
caml_curl_easy_reset(value conn
)
1566 Connection
*connection
= Connection_val(conn
);
1568 checkConnection(connection
);
1569 curl_easy_reset(connection
->handle
);
1570 curl_easy_setopt(connection
->handle
, CURLOPT_PRIVATE
, connection
);
1571 resetOcamlValues(connection
);
1573 CAMLreturn(Val_unit
);
1577 ** curl_easy_setopt helper utility functions
1580 #define SETOPT_FUNCTION_(name,suffix) \
1581 static void handle_##name##suffix(Connection *conn, value option) \
1583 CAMLparam1(option); \
1584 CURLcode result = CURLE_OK; \
1585 Store_field(conn->ocamlValues, Ocaml_##name##FUNCTION, option); \
1586 result = curl_easy_setopt(conn->handle, CURLOPT_##name##FUNCTION, cb_##name##suffix); \
1587 if (result != CURLE_OK) raiseError(conn, result); \
1588 result = curl_easy_setopt(conn->handle, CURLOPT_##name##DATA, conn); \
1589 if (result != CURLE_OK) raiseError(conn, result); \
1593 #define SETOPT_FUNCTION(name) SETOPT_FUNCTION_(name,FUNCTION)
1594 #define SETOPT_FUNCTION2(name) SETOPT_FUNCTION_(name,FUNCTION2)
1596 SETOPT_FUNCTION( WRITE
)
1597 SETOPT_FUNCTION2( WRITE
)
1598 SETOPT_FUNCTION( READ
)
1599 SETOPT_FUNCTION2( READ
)
1600 SETOPT_FUNCTION( HEADER
)
1601 SETOPT_FUNCTION( PROGRESS
)
1602 #if HAVE_DECL_CURLOPT_XFERINFOFUNCTION
1603 SETOPT_FUNCTION( XFERINFO
)
1605 #if HAVE_DECL_CURLOPT_PREREQFUNCTION
1606 SETOPT_FUNCTION( PREREQ
)
1608 SETOPT_FUNCTION( DEBUG
)
1609 SETOPT_FUNCTION( SSH_KEY
)
1611 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1612 SETOPT_FUNCTION( SEEK
)
1615 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
1616 SETOPT_FUNCTION( IOCTL
)
1619 SETOPT_FUNCTION( OPENSOCKET
)
1620 /* SETOPT_FUNCTION( CLOSESOCKET) */
1622 static void handle_slist(Connection
*conn
, struct curl_slist
** slist
, CURLoption curl_option
, value option
)
1625 CURLcode result
= CURLE_OK
;
1627 free_curl_slist(*slist
);
1630 while (Val_emptylist
!= option
)
1632 *slist
= curl_slist_append(*slist
, String_val(Field(option
, 0)));
1634 option
= Field(option
, 1);
1637 result
= curl_easy_setopt(conn
->handle
, curl_option
, *slist
);
1639 if (result
!= CURLE_OK
)
1640 raiseError(conn
, result
);
1645 static long convert_bit_list(long *map
, size_t map_size
, value option
)
1651 while (Val_emptylist
!= option
)
1653 index
= Int_val(Field(option
, 0));
1654 if ((index
< 0) || ((size_t)index
>= map_size
))
1655 caml_invalid_argument("convert_bit_list");
1659 option
= Field(option
, 1);
1662 CAMLreturnT(long, bits
);
1665 #define SETOPT_STRING(name) \
1666 static void handle_##name(Connection *conn, value option) \
1668 CAMLparam1(option); \
1669 CURLcode result = CURLE_OK; \
1671 result = curl_easy_setopt(conn->handle, CURLOPT_##name, String_val(option)); \
1673 if (result != CURLE_OK) \
1674 raiseError(conn, result); \
1679 #define SETOPT_VAL_(func_name, curl_option, conv_val) \
1680 static void func_name(Connection *conn, value option) \
1682 CAMLparam1(option); \
1683 CURLcode result = CURLE_OK; \
1685 result = curl_easy_setopt(conn->handle, curl_option, conv_val(option)); \
1687 if (result != CURLE_OK) \
1688 raiseError(conn, result); \
1693 #define SETOPT_VAL(name, conv) SETOPT_VAL_(handle_##name, CURLOPT_##name, conv)
1694 #define SETOPT_BOOL(name) SETOPT_VAL(name, Bool_val)
1695 #define SETOPT_LONG(name) SETOPT_VAL(name, Long_val)
1696 #define SETOPT_INT64(name) SETOPT_VAL(name, Int64_val)
1698 #if HAVE_DECL_CURLOPT_MIMEPOST
1700 static void handle_part_with_name(Connection
* conn
, curl_mimepart
* part
, value v_part_name_info
)
1702 value v_data
= Field(v_part_name_info
, 0);
1703 value v_name
= Field(v_part_name_info
, 1);
1704 value v_filename
= Field(v_part_name_info
, 2);
1707 value v_str
= Field(v_data
, 0);
1709 switch (Tag_val(v_data
)) {
1711 result
= curl_mime_data(part
, String_val(v_str
), caml_string_length(v_str
));
1714 result
= curl_mime_filedata(part
, String_val(v_str
));
1717 caml_failwith("Invalid MIMEPOST data value");
1721 if (result
!= CURLE_OK
) {
1722 raiseError(conn
, result
);
1725 if (Is_block(v_name
)) {
1726 result
= curl_mime_name(part
, String_val(Field(v_name
, 0)));
1728 if (result
!= CURLE_OK
) {
1729 raiseError(conn
, result
);
1733 if (Is_block(v_filename
)) {
1734 result
= curl_mime_filename(part
, String_val(Field(v_filename
, 0)));
1736 if (result
!= CURLE_OK
) {
1737 raiseError(conn
, result
);
1742 static void new_part(Connection
* conn
, curl_mime
* mime
, value v_part
)
1744 value v_encoding
= Field(v_part
, 0);
1745 value v_headers
= Field(v_part
, 1);
1746 value v_subparts
= Field(v_part
, 2);
1747 value v_data
= Field(v_part
, 3);
1748 value v_str
= Field(v_data
, 0);
1750 struct curl_slist
*headers
= NULL
;
1753 curl_mimepart
*part
= curl_mime_addpart(mime
);
1755 switch (Int_val(v_encoding
)) {
1757 result
= curl_mime_encoder(part
, "8bit");
1760 result
= curl_mime_encoder(part
, "binary");
1763 result
= curl_mime_encoder(part
, "7bit");
1766 result
= curl_mime_encoder(part
, "quoted-printable");
1769 result
= curl_mime_encoder(part
, "base64");
1775 caml_failwith("Invalid MIMEPOST encoding value");
1779 if (result
!= CURLE_OK
) {
1780 raiseError(conn
, result
);
1783 while (v_headers
!= Val_emptylist
) {
1784 headers
= curl_slist_append(headers
, String_val(Field(v_headers
, 0)));
1785 v_headers
= Field(v_headers
, 1);
1788 result
= curl_mime_headers(part
, headers
, 1);
1790 if (result
!= CURLE_OK
) {
1791 raiseError(conn
, result
);
1794 switch (Tag_val(v_data
)) {
1796 result
= curl_mime_data(part
, String_val(v_str
), caml_string_length(v_str
));
1799 result
= curl_mime_filedata(part
, String_val(v_str
));
1802 handle_part_with_name(conn
, part
, v_data
);
1805 caml_failwith("Invalid MIMEPOST data value");
1809 if (result
!= CURLE_OK
) {
1810 raiseError(conn
, result
);
1813 if (v_subparts
!= Val_emptylist
) {
1814 curl_mime
*mime
= curl_mime_init(conn
->handle
);
1816 while (v_subparts
!= Val_emptylist
) {
1817 new_part(conn
, mime
, Field(v_subparts
, 0));
1818 v_subparts
= Field(v_subparts
, 1);
1821 result
= curl_mime_subparts(part
, mime
);
1823 if (result
!= CURLE_OK
) {
1824 raiseError(conn
, result
);
1829 static void handle_MIMEPOST(Connection
* conn
, value v_subparts
)
1831 CAMLparam1(v_subparts
);
1832 curl_mime
*mime
= curl_mime_init(conn
->handle
);
1835 curl_mime_free(conn
->curl_MIMEPOST
);
1836 conn
->curl_MIMEPOST
= mime
;
1838 while (v_subparts
!= Val_emptylist
) {
1839 new_part(conn
, mime
, Field(v_subparts
, 0));
1840 v_subparts
= Field(v_subparts
, 1);
1843 result
= curl_easy_setopt(conn
->handle
, CURLOPT_MIMEPOST
, mime
);
1845 if (result
!= CURLE_OK
) {
1846 raiseError(conn
, result
);
1854 #define SETOPT_SLIST(name) \
1855 static void handle_##name(Connection* conn, value option) \
1857 handle_slist(conn,&(conn->curl_##name),CURLOPT_##name,option); \
1861 SETOPT_LONG( INFILESIZE
)
1862 SETOPT_STRING( PROXY
)
1863 SETOPT_LONG( PROXYPORT
)
1864 SETOPT_BOOL( HTTPPROXYTUNNEL
)
1865 SETOPT_BOOL( VERBOSE
)
1866 SETOPT_BOOL( HEADER
)
1867 SETOPT_BOOL( NOPROGRESS
)
1869 #if HAVE_DECL_CURLOPT_NOSIGNAL
1870 SETOPT_BOOL( NOSIGNAL
)
1873 SETOPT_BOOL( NOBODY
)
1874 SETOPT_BOOL( FAILONERROR
)
1875 SETOPT_BOOL( UPLOAD
)
1877 SETOPT_BOOL( FTPLISTONLY
)
1878 SETOPT_BOOL( FTPAPPEND
)
1881 static void handle_NETRC(Connection
*conn
, value option
)
1884 CURLcode result
= CURLE_OK
;
1887 switch (Long_val(option
))
1889 case 0: /* CURL_NETRC_OPTIONAL */
1890 netrc
= CURL_NETRC_OPTIONAL
;
1893 case 1:/* CURL_NETRC_IGNORED */
1894 netrc
= CURL_NETRC_IGNORED
;
1897 case 2: /* CURL_NETRC_REQUIRED */
1898 netrc
= CURL_NETRC_REQUIRED
;
1902 caml_failwith("Invalid NETRC Option");
1906 result
= curl_easy_setopt(conn
->handle
,
1910 if (result
!= CURLE_OK
)
1911 raiseError(conn
, result
);
1916 #if HAVE_DECL_CURLOPT_ENCODING
1917 static void handle_ENCODING(Connection
*conn
, value option
)
1920 CURLcode result
= CURLE_OK
;
1922 switch (Long_val(option
))
1924 case 0: /* CURL_ENCODING_NONE */
1925 result
= curl_easy_setopt(conn
->handle
,
1930 case 1: /* CURL_ENCODING_DEFLATE */
1931 result
= curl_easy_setopt(conn
->handle
,
1936 case 2: /* CURL_ENCODING_GZIP */
1937 result
= curl_easy_setopt(conn
->handle
,
1942 case 3: /* CURL_ENCODING_ANY */
1943 result
= curl_easy_setopt(conn
->handle
,
1949 caml_failwith("Invalid Encoding Option");
1953 if (result
!= CURLE_OK
)
1954 raiseError(conn
, result
);
1961 SETOPT_BOOL( FOLLOWLOCATION
)
1962 SETOPT_BOOL( TRANSFERTEXT
)
1964 SETOPT_STRING( USERPWD
)
1965 SETOPT_STRING( PROXYUSERPWD
)
1966 SETOPT_STRING( RANGE
)
1968 static void handle_ERRORBUFFER(Connection
*conn
, value option
)
1971 CURLcode result
= CURLE_OK
;
1973 Store_field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
, option
);
1975 if (conn
->curl_ERRORBUFFER
!= NULL
)
1976 free(conn
->curl_ERRORBUFFER
);
1978 conn
->curl_ERRORBUFFER
= (char*)malloc(sizeof(char) * CURL_ERROR_SIZE
);
1980 result
= curl_easy_setopt(conn
->handle
,
1981 CURLOPT_ERRORBUFFER
,
1982 conn
->curl_ERRORBUFFER
);
1984 if (result
!= CURLE_OK
)
1985 raiseError(conn
, result
);
1990 SETOPT_LONG( TIMEOUT
)
1992 static void handle_POSTFIELDS(Connection
*conn
, value option
)
1995 CURLcode result
= CURLE_OK
;
1997 if (conn
->curl_POSTFIELDS
!= NULL
)
1998 free(conn
->curl_POSTFIELDS
);
2000 conn
->curl_POSTFIELDS
= strdup_ml(option
);
2002 result
= curl_easy_setopt(conn
->handle
,
2004 conn
->curl_POSTFIELDS
);
2006 if (result
!= CURLE_OK
)
2007 raiseError(conn
, result
);
2012 SETOPT_LONG( POSTFIELDSIZE
)
2013 SETOPT_STRING( REFERER
)
2014 SETOPT_STRING( USERAGENT
)
2015 SETOPT_STRING( FTPPORT
)
2016 SETOPT_LONG( LOW_SPEED_LIMIT
)
2017 SETOPT_LONG( LOW_SPEED_TIME
)
2018 SETOPT_LONG( RESUME_FROM
)
2019 SETOPT_STRING( COOKIE
)
2021 SETOPT_SLIST( HTTPHEADER
)
2023 long sslOptionMap
[] = {
2024 #ifdef CURLSSLOPT_ALLOW_BEAST
2025 CURLSSLOPT_ALLOW_BEAST
,
2029 #ifdef CURLSSLOPT_NO_REVOKE
2030 CURLSSLOPT_NO_REVOKE
,
2034 #ifdef CURLSSLOPT_NO_PARTIALCHAIN
2035 CURLSSLOPT_NO_PARTIALCHAIN
,
2039 #ifdef CURLSSLOPT_REVOKE_BEST_EFFORT
2040 CURLSSLOPT_REVOKE_BEST_EFFORT
,
2044 #ifdef CURLSSLOPT_NATIVE_CA
2045 CURLSSLOPT_NATIVE_CA
,
2049 #ifdef CURLSSLOPT_AUTO_CLIENT_CERT
2050 CURLSSLOPT_AUTO_CLIENT_CERT
,
2056 #if HAVE_DECL_CURLOPT_SSL_OPTIONS
2057 static void handle_SSL_OPTIONS(Connection
*conn
, value opts
)
2060 CURLcode result
= CURLE_OK
;
2061 long bits
= convert_bit_list(sslOptionMap
, sizeof(sslOptionMap
) / sizeof(sslOptionMap
[0]), opts
);
2063 result
= curl_easy_setopt(conn
->handle
, CURLOPT_SSL_OPTIONS
, bits
);
2065 if (result
!= CURLE_OK
)
2066 raiseError(conn
, result
);
2072 static void handle_HTTPPOST(Connection
*conn
, value option
)
2075 CAMLlocal3(listIter
, formItem
, contentType
);
2076 CURLcode result
= CURLE_OK
;
2080 free_curl_slist(conn
->httpPostBuffers
);
2081 if (conn
->httpPostFirst
!= NULL
)
2082 curl_formfree(conn
->httpPostFirst
);
2084 conn
->httpPostBuffers
= NULL
;
2085 conn
->httpPostFirst
= NULL
;
2086 conn
->httpPostLast
= NULL
;
2088 while (!Is_long(listIter
))
2090 formItem
= Field(listIter
, 0);
2092 switch (Tag_val(formItem
))
2094 case 0: /* CURLFORM_CONTENT */
2095 if (Wosize_val(formItem
) < 3)
2097 caml_failwith("Incorrect CURLFORM_CONTENT parameters");
2100 if (Is_long(Field(formItem
, 2)) &&
2101 Long_val(Field(formItem
, 2)) == 0)
2103 curl_formadd(&conn
->httpPostFirst
,
2104 &conn
->httpPostLast
,
2106 String_val(Field(formItem
, 0)),
2107 CURLFORM_NAMELENGTH
,
2108 caml_string_length(Field(formItem
, 0)),
2109 CURLFORM_COPYCONTENTS
,
2110 String_val(Field(formItem
, 1)),
2111 CURLFORM_CONTENTSLENGTH
,
2112 caml_string_length(Field(formItem
, 1)),
2115 else if (Is_block(Field(formItem
, 2)))
2117 contentType
= Field(formItem
, 2);
2119 curl_formadd(&conn
->httpPostFirst
,
2120 &conn
->httpPostLast
,
2122 String_val(Field(formItem
, 0)),
2123 CURLFORM_NAMELENGTH
,
2124 caml_string_length(Field(formItem
, 0)),
2125 CURLFORM_COPYCONTENTS
,
2126 String_val(Field(formItem
, 1)),
2127 CURLFORM_CONTENTSLENGTH
,
2128 caml_string_length(Field(formItem
, 1)),
2129 CURLFORM_CONTENTTYPE
,
2130 String_val(Field(contentType
, 0)),
2135 caml_failwith("Incorrect CURLFORM_CONTENT parameters");
2139 case 1: /* CURLFORM_FILECONTENT */
2140 if (Wosize_val(formItem
) < 3)
2142 caml_failwith("Incorrect CURLFORM_FILECONTENT parameters");
2145 if (Is_long(Field(formItem
, 2)) &&
2146 Long_val(Field(formItem
, 2)) == 0)
2148 curl_formadd(&conn
->httpPostFirst
,
2149 &conn
->httpPostLast
,
2151 String_val(Field(formItem
, 0)),
2152 CURLFORM_NAMELENGTH
,
2153 caml_string_length(Field(formItem
, 0)),
2154 CURLFORM_FILECONTENT
,
2155 String_val(Field(formItem
, 1)),
2158 else if (Is_block(Field(formItem
, 2)))
2160 contentType
= Field(formItem
, 2);
2162 curl_formadd(&conn
->httpPostFirst
,
2163 &conn
->httpPostLast
,
2165 String_val(Field(formItem
, 0)),
2166 CURLFORM_NAMELENGTH
,
2167 caml_string_length(Field(formItem
, 0)),
2168 CURLFORM_FILECONTENT
,
2169 String_val(Field(formItem
, 1)),
2170 CURLFORM_CONTENTTYPE
,
2171 String_val(Field(contentType
, 0)),
2176 caml_failwith("Incorrect CURLFORM_FILECONTENT parameters");
2180 case 2: /* CURLFORM_FILE */
2181 if (Wosize_val(formItem
) < 3)
2183 caml_failwith("Incorrect CURLFORM_FILE parameters");
2186 if (Is_long(Field(formItem
, 2)) &&
2187 Long_val(Field(formItem
, 2)) == 0)
2189 curl_formadd(&conn
->httpPostFirst
,
2190 &conn
->httpPostLast
,
2192 String_val(Field(formItem
, 0)),
2193 CURLFORM_NAMELENGTH
,
2194 caml_string_length(Field(formItem
, 0)),
2196 String_val(Field(formItem
, 1)),
2199 else if (Is_block(Field(formItem
, 2)))
2201 contentType
= Field(formItem
, 2);
2203 curl_formadd(&conn
->httpPostFirst
,
2204 &conn
->httpPostLast
,
2206 String_val(Field(formItem
, 0)),
2207 CURLFORM_NAMELENGTH
,
2208 caml_string_length(Field(formItem
, 0)),
2210 String_val(Field(formItem
, 1)),
2211 CURLFORM_CONTENTTYPE
,
2212 String_val(Field(contentType
, 0)),
2217 caml_failwith("Incorrect CURLFORM_FILE parameters");
2221 case 3: /* CURLFORM_BUFFER */
2222 if (Wosize_val(formItem
) < 4)
2224 caml_failwith("Incorrect CURLFORM_BUFFER parameters");
2227 if (Is_long(Field(formItem
, 3)) &&
2228 Long_val(Field(formItem
, 3)) == 0)
2230 conn
->httpPostBuffers
= curl_slist_prepend_ml(conn
->httpPostBuffers
, Field(formItem
, 2));
2232 curl_formadd(&conn
->httpPostFirst
,
2233 &conn
->httpPostLast
,
2235 String_val(Field(formItem
, 0)),
2236 CURLFORM_NAMELENGTH
,
2237 caml_string_length(Field(formItem
, 0)),
2239 String_val(Field(formItem
, 1)),
2241 conn
->httpPostBuffers
->data
,
2242 CURLFORM_BUFFERLENGTH
,
2243 caml_string_length(Field(formItem
, 2)),
2246 else if (Is_block(Field(formItem
, 3)))
2248 conn
->httpPostBuffers
= curl_slist_prepend_ml(conn
->httpPostBuffers
, Field(formItem
, 2));
2250 contentType
= Field(formItem
, 3);
2252 curl_formadd(&conn
->httpPostFirst
,
2253 &conn
->httpPostLast
,
2255 String_val(Field(formItem
, 0)),
2256 CURLFORM_NAMELENGTH
,
2257 caml_string_length(Field(formItem
, 0)),
2259 String_val(Field(formItem
, 1)),
2261 conn
->httpPostBuffers
->data
,
2262 CURLFORM_BUFFERLENGTH
,
2263 caml_string_length(Field(formItem
, 2)),
2264 CURLFORM_CONTENTTYPE
,
2265 String_val(Field(contentType
, 0)),
2270 caml_failwith("Incorrect CURLFORM_BUFFER parameters");
2275 listIter
= Field(listIter
, 1);
2278 result
= curl_easy_setopt(conn
->handle
,
2280 conn
->httpPostFirst
);
2282 if (result
!= CURLE_OK
)
2283 raiseError(conn
, result
);
2288 SETOPT_STRING( SSLCERT
)
2289 SETOPT_STRING( SSLCERTTYPE
)
2290 SETOPT_STRING( SSLCERTPASSWD
)
2291 SETOPT_STRING( SSLKEY
)
2292 SETOPT_STRING( SSLKEYTYPE
)
2293 SETOPT_STRING( SSLKEYPASSWD
)
2294 SETOPT_STRING( SSLENGINE
)
2295 SETOPT_BOOL( SSLENGINE_DEFAULT
)
2298 SETOPT_SLIST( QUOTE
)
2299 SETOPT_SLIST( POSTQUOTE
)
2301 SETOPT_STRING( COOKIEFILE
)
2302 #if HAVE_DECL_CURLOPT_CERTINFO
2303 SETOPT_BOOL( CERTINFO
)
2306 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_0
2307 #define CURL_SSLVERSION_TLSv1_0 CURL_SSLVERSION_TLSv1
2310 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_1
2311 #define CURL_SSLVERSION_TLSv1_1 CURL_SSLVERSION_TLSv1
2314 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_2
2315 #define CURL_SSLVERSION_TLSv1_2 CURL_SSLVERSION_TLSv1
2318 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_3
2319 #define CURL_SSLVERSION_TLSv1_3 CURL_SSLVERSION_TLSv1
2322 static void handle_SSLVERSION(Connection
*conn
, value option
)
2325 CURLcode result
= CURLE_OK
;
2326 int v
= CURL_SSLVERSION_DEFAULT
;
2328 switch (Long_val(option
))
2330 case 0: v
= CURL_SSLVERSION_DEFAULT
; break;
2331 case 1: v
= CURL_SSLVERSION_TLSv1
; break;
2332 case 2: v
= CURL_SSLVERSION_SSLv2
; break;
2333 case 3: v
= CURL_SSLVERSION_SSLv3
; break;
2334 case 4: v
= CURL_SSLVERSION_TLSv1_0
; break;
2335 case 5: v
= CURL_SSLVERSION_TLSv1_1
; break;
2336 case 6: v
= CURL_SSLVERSION_TLSv1_2
; break;
2337 case 7: v
= CURL_SSLVERSION_TLSv1_3
; break;
2339 caml_failwith("Invalid SSLVERSION Option");
2343 result
= curl_easy_setopt(conn
->handle
, CURLOPT_SSLVERSION
, v
);
2345 if (result
!= CURLE_OK
)
2346 raiseError(conn
, result
);
2351 static void handle_TIMECONDITION(Connection
*conn
, value option
)
2354 CURLcode result
= CURLE_OK
;
2355 int timecond
= CURL_TIMECOND_NONE
;
2357 switch (Long_val(option
))
2359 case 0: timecond
= CURL_TIMECOND_NONE
; break;
2360 case 1: timecond
= CURL_TIMECOND_IFMODSINCE
; break;
2361 case 2: timecond
= CURL_TIMECOND_IFUNMODSINCE
; break;
2362 case 3: timecond
= CURL_TIMECOND_LASTMOD
; break;
2364 caml_failwith("Invalid TIMECOND Option");
2368 result
= curl_easy_setopt(conn
->handle
, CURLOPT_TIMECONDITION
, timecond
);
2370 if (result
!= CURLE_OK
)
2371 raiseError(conn
, result
);
2376 SETOPT_VAL( TIMEVALUE
, Int32_val
)
2377 SETOPT_STRING( CUSTOMREQUEST
)
2378 SETOPT_STRING( INTERFACE
)
2380 static void handle_KRB4LEVEL(Connection
*conn
, value option
)
2383 CURLcode result
= CURLE_OK
;
2385 switch (Long_val(option
))
2387 case 0: /* KRB4_NONE */
2388 result
= curl_easy_setopt(conn
->handle
,
2393 case 1: /* KRB4_CLEAR */
2394 result
= curl_easy_setopt(conn
->handle
,
2399 case 2: /* KRB4_SAFE */
2400 result
= curl_easy_setopt(conn
->handle
,
2405 case 3: /* KRB4_CONFIDENTIAL */
2406 result
= curl_easy_setopt(conn
->handle
,
2411 case 4: /* KRB4_PRIVATE */
2412 result
= curl_easy_setopt(conn
->handle
,
2418 caml_failwith("Invalid KRB4 Option");
2422 if (result
!= CURLE_OK
)
2423 raiseError(conn
, result
);
2428 SETOPT_BOOL( SSL_VERIFYPEER
)
2429 SETOPT_STRING( CAINFO
)
2430 SETOPT_STRING( CAPATH
)
2431 SETOPT_BOOL( FILETIME
)
2432 SETOPT_LONG( MAXREDIRS
)
2433 SETOPT_LONG( MAXCONNECTS
)
2435 static void handle_CLOSEPOLICY(Connection
*conn
, value option
)
2438 CURLcode result
= CURLE_OK
;
2440 switch (Long_val(option
))
2442 case 0: /* CLOSEPOLICY_OLDEST */
2443 result
= curl_easy_setopt(conn
->handle
,
2444 CURLOPT_CLOSEPOLICY
,
2445 CURLCLOSEPOLICY_OLDEST
);
2448 case 1: /* CLOSEPOLICY_LEAST_RECENTLY_USED */
2449 result
= curl_easy_setopt(conn
->handle
,
2450 CURLOPT_CLOSEPOLICY
,
2451 CURLCLOSEPOLICY_LEAST_RECENTLY_USED
);
2455 caml_failwith("Invalid CLOSEPOLICY Option");
2459 if (result
!= CURLE_OK
)
2460 raiseError(conn
, result
);
2465 SETOPT_BOOL( FRESH_CONNECT
)
2466 SETOPT_BOOL( FORBID_REUSE
)
2467 SETOPT_STRING( RANDOM_FILE
)
2468 SETOPT_STRING( EGDSOCKET
)
2469 SETOPT_LONG( CONNECTTIMEOUT
)
2470 SETOPT_BOOL( HTTPGET
)
2472 static void handle_SSL_VERIFYHOST(Connection
*conn
, value option
)
2475 CURLcode result
= CURLE_OK
;
2477 switch (Long_val(option
))
2479 case 0: /* SSLVERIFYHOST_NONE */
2480 case 1: /* SSLVERIFYHOST_EXISTENCE */
2481 case 2: /* SSLVERIFYHOST_HOSTNAME */
2482 result
= curl_easy_setopt(conn
->handle
,
2483 CURLOPT_SSL_VERIFYHOST
,
2484 /* map EXISTENCE to HOSTNAME */
2485 Long_val(option
) == 0 ? 0 : 2);
2489 caml_failwith("Invalid SSLVERIFYHOST Option");
2493 if (result
!= CURLE_OK
)
2494 raiseError(conn
, result
);
2499 SETOPT_STRING( COOKIEJAR
)
2500 SETOPT_STRING( SSL_CIPHER_LIST
)
2502 static void handle_HTTP_VERSION(Connection
*conn
, value option
)
2505 CURLcode result
= CURLE_OK
;
2507 long version
= CURL_HTTP_VERSION_NONE
;
2509 switch (Long_val(option
))
2511 case 0: version
= CURL_HTTP_VERSION_NONE
; break;
2512 case 1: version
= CURL_HTTP_VERSION_1_0
; break;
2513 case 2: version
= CURL_HTTP_VERSION_1_1
; break;
2515 #if HAVE_DECL_CURL_HTTP_VERSION_2
2516 version
= CURL_HTTP_VERSION_2
;
2517 #elif HAVE_DECL_CURL_HTTP_VERSION_2_0
2518 version
= CURL_HTTP_VERSION_2_0
;
2522 #if HAVE_DECL_CURL_HTTP_VERSION_2TLS
2523 version
= CURL_HTTP_VERSION_2TLS
;
2527 #if HAVE_DECL_CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
2528 version
= CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
;
2532 #if HAVE_DECL_CURL_HTTP_VERSION_3
2533 version
= CURL_HTTP_VERSION_3
;
2536 /* sync check_enums */
2538 caml_invalid_argument("CURLOPT_HTTP_VERSION");
2542 result
= curl_easy_setopt(conn
->handle
, CURLOPT_HTTP_VERSION
, version
);
2544 if (result
!= CURLE_OK
)
2545 raiseError(conn
, result
);
2550 static long ocaml_HTTP_VERSION(long curl_version
)
2552 switch (curl_version
)
2554 case CURL_HTTP_VERSION_NONE
: return 0;
2555 case CURL_HTTP_VERSION_1_0
: return 1;
2556 case CURL_HTTP_VERSION_1_1
: return 2;
2557 #if HAVE_DECL_CURL_HTTP_VERSION_2
2558 case CURL_HTTP_VERSION_2
: return 3;
2559 #elif HAVE_DECL_CURL_HTTP_VERSION_2_0
2560 case CURL_HTTP_VERSION_2_0
: return 3;
2562 #if HAVE_DECL_CURL_HTTP_VERSION_2TLS
2563 case CURL_HTTP_VERSION_2TLS
: return 4;
2565 #if HAVE_DECL_CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
2566 case CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
: return 5;
2568 #if HAVE_DECL_CURL_HTTP_VERSION_3
2569 case CURL_HTTP_VERSION_3
: return 6;
2571 /* sync check_enums */
2576 SETOPT_BOOL( FTP_USE_EPSV
)
2577 SETOPT_LONG( DNS_CACHE_TIMEOUT
)
2578 SETOPT_BOOL( DNS_USE_GLOBAL_CACHE
)
2580 static void handle_PRIVATE(Connection
*conn
, value option
)
2583 Store_field(conn
->ocamlValues
, Ocaml_PRIVATE
, option
);
2587 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2588 SETOPT_SLIST( HTTP200ALIASES
)
2591 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2592 SETOPT_BOOL( UNRESTRICTED_AUTH
)
2595 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2596 SETOPT_BOOL( FTP_USE_EPRT
)
2599 #if HAVE_DECL_CURLOPT_HTTPAUTH
2600 static void handle_HTTPAUTH(Connection
*conn
, value option
)
2603 CAMLlocal1(listIter
);
2604 CURLcode result
= CURLE_OK
;
2605 long auth
= CURLAUTH_NONE
;
2609 while (!Is_long(listIter
))
2611 switch (Long_val(Field(listIter
, 0)))
2613 case 0: /* CURLAUTH_BASIC */
2614 auth
|= CURLAUTH_BASIC
;
2617 case 1: /* CURLAUTH_DIGEST */
2618 auth
|= CURLAUTH_DIGEST
;
2621 case 2: /* CURLAUTH_GSSNEGOTIATE */
2622 auth
|= CURLAUTH_GSSNEGOTIATE
;
2625 case 3: /* CURLAUTH_NTLM */
2626 auth
|= CURLAUTH_NTLM
;
2629 case 4: /* CURLAUTH_ANY */
2630 auth
|= CURLAUTH_ANY
;
2633 case 5: /* CURLAUTH_ANYSAFE */
2634 auth
|= CURLAUTH_ANYSAFE
;
2638 caml_failwith("Invalid HTTPAUTH Value");
2642 listIter
= Field(listIter
, 1);
2645 result
= curl_easy_setopt(conn
->handle
,
2649 if (result
!= CURLE_OK
)
2650 raiseError(conn
, result
);
2656 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2657 SETOPT_BOOL( FTP_CREATE_MISSING_DIRS
)
2660 #if HAVE_DECL_CURLOPT_PROXYAUTH
2661 static void handle_PROXYAUTH(Connection
*conn
, value option
)
2664 CAMLlocal1(listIter
);
2665 CURLcode result
= CURLE_OK
;
2666 long auth
= CURLAUTH_NONE
;
2670 while (!Is_long(listIter
))
2672 switch (Long_val(Field(listIter
, 0)))
2674 case 0: /* CURLAUTH_BASIC */
2675 auth
|= CURLAUTH_BASIC
;
2678 case 1: /* CURLAUTH_DIGEST */
2679 auth
|= CURLAUTH_DIGEST
;
2682 case 2: /* CURLAUTH_GSSNEGOTIATE */
2683 auth
|= CURLAUTH_GSSNEGOTIATE
;
2686 case 3: /* CURLAUTH_NTLM */
2687 auth
|= CURLAUTH_NTLM
;
2690 case 4: /* CURLAUTH_ANY */
2691 auth
|= CURLAUTH_ANY
;
2694 case 5: /* CURLAUTH_ANYSAFE */
2695 auth
|= CURLAUTH_ANYSAFE
;
2699 caml_failwith("Invalid HTTPAUTH Value");
2703 listIter
= Field(listIter
, 1);
2706 result
= curl_easy_setopt(conn
->handle
,
2710 if (result
!= CURLE_OK
)
2711 raiseError(conn
, result
);
2717 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2718 SETOPT_LONG( FTP_RESPONSE_TIMEOUT
)
2721 #if HAVE_DECL_CURLOPT_IPRESOLVE
2722 static void handle_IPRESOLVE(Connection
*conn
, value option
)
2725 CURLcode result
= CURLE_OK
;
2727 switch (Long_val(option
))
2729 case 0: /* CURL_IPRESOLVE_WHATEVER */
2730 result
= curl_easy_setopt(conn
->handle
,
2732 CURL_IPRESOLVE_WHATEVER
);
2735 case 1: /* CURL_IPRESOLVE_V4 */
2736 result
= curl_easy_setopt(conn
->handle
,
2741 case 2: /* CURL_IPRESOLVE_V6 */
2742 result
= curl_easy_setopt(conn
->handle
,
2748 caml_failwith("Invalid IPRESOLVE Value");
2752 if (result
!= CURLE_OK
)
2753 raiseError(conn
, result
);
2759 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2760 SETOPT_VAL( MAXFILESIZE
, Int32_val
)
2763 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2764 SETOPT_INT64( INFILESIZE_LARGE
)
2767 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2768 SETOPT_INT64( RESUME_FROM_LARGE
)
2771 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2772 SETOPT_INT64( MAXFILESIZE_LARGE
)
2775 #if HAVE_DECL_CURLOPT_NETRC_FILE
2776 SETOPT_STRING( NETRC_FILE
)
2779 #if HAVE_DECL_CURLOPT_FTP_SSL
2780 static void handle_FTP_SSL(Connection
*conn
, value option
)
2783 CURLcode result
= CURLE_OK
;
2785 switch (Long_val(option
))
2787 case 0: /* CURLFTPSSL_NONE */
2788 result
= curl_easy_setopt(conn
->handle
,
2793 case 1: /* CURLFTPSSL_TRY */
2794 result
= curl_easy_setopt(conn
->handle
,
2799 case 2: /* CURLFTPSSL_CONTROL */
2800 result
= curl_easy_setopt(conn
->handle
,
2802 CURLFTPSSL_CONTROL
);
2805 case 3: /* CURLFTPSSL_ALL */
2806 result
= curl_easy_setopt(conn
->handle
,
2812 caml_failwith("Invalid FTP_SSL Value");
2816 if (result
!= CURLE_OK
)
2817 raiseError(conn
, result
);
2823 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
2824 SETOPT_INT64( POSTFIELDSIZE_LARGE
)
2827 #if HAVE_DECL_CURLOPT_TCP_NODELAY
2828 /* not using SETOPT_BOOL here because of TCP_NODELAY defined in winsock.h */
2829 SETOPT_VAL_( handle_TCP_NODELAY
, CURLOPT_TCP_NODELAY
, Bool_val
)
2832 #if HAVE_DECL_CURLOPT_TCP_FASTOPEN
2833 SETOPT_BOOL( TCP_FASTOPEN
)
2836 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
2837 static void handle_FTPSSLAUTH(Connection
*conn
, value option
)
2840 CURLcode result
= CURLE_OK
;
2842 switch (Long_val(option
))
2844 case 0: /* CURLFTPAUTH_DEFAULT */
2845 result
= curl_easy_setopt(conn
->handle
,
2847 CURLFTPAUTH_DEFAULT
);
2850 case 1: /* CURLFTPAUTH_SSL */
2851 result
= curl_easy_setopt(conn
->handle
,
2856 case 2: /* CURLFTPAUTH_TLS */
2857 result
= curl_easy_setopt(conn
->handle
,
2863 caml_failwith("Invalid FTPSSLAUTH value");
2867 if (result
!= CURLE_OK
)
2868 raiseError(conn
, result
);
2874 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
2875 SETOPT_STRING( FTP_ACCOUNT
)
2878 #if HAVE_DECL_CURLOPT_COOKIELIST
2879 SETOPT_STRING( COOKIELIST
)
2882 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
2883 SETOPT_BOOL( IGNORE_CONTENT_LENGTH
)
2886 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
2887 SETOPT_BOOL( FTP_SKIP_PASV_IP
)
2890 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
2891 static void handle_FTP_FILEMETHOD(Connection
*conn
, value option
)
2894 CURLcode result
= CURLE_OK
;
2896 switch (Long_val(option
))
2898 case 0: /* CURLFTPMETHOD_DEFAULT */
2899 result
= curl_easy_setopt(conn
->handle
,
2900 CURLOPT_FTP_FILEMETHOD
,
2901 CURLFTPMETHOD_DEFAULT
);
2904 case 1: /* CURLFTMETHOD_MULTICWD */
2905 result
= curl_easy_setopt(conn
->handle
,
2906 CURLOPT_FTP_FILEMETHOD
,
2907 CURLFTPMETHOD_MULTICWD
);
2910 case 2: /* CURLFTPMETHOD_NOCWD */
2911 result
= curl_easy_setopt(conn
->handle
,
2912 CURLOPT_FTP_FILEMETHOD
,
2913 CURLFTPMETHOD_NOCWD
);
2916 case 3: /* CURLFTPMETHOD_SINGLECWD */
2917 result
= curl_easy_setopt(conn
->handle
,
2918 CURLOPT_FTP_FILEMETHOD
,
2919 CURLFTPMETHOD_SINGLECWD
);
2922 caml_failwith("Invalid FTP_FILEMETHOD value");
2926 if (result
!= CURLE_OK
)
2927 raiseError(conn
, result
);
2933 #if HAVE_DECL_CURLOPT_LOCALPORT
2934 SETOPT_LONG( LOCALPORT
)
2937 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
2938 SETOPT_LONG( LOCALPORTRANGE
)
2941 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
2942 SETOPT_BOOL( CONNECT_ONLY
)
2945 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
2946 SETOPT_INT64( MAX_SEND_SPEED_LARGE
)
2949 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
2950 SETOPT_INT64( MAX_RECV_SPEED_LARGE
)
2953 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
2954 SETOPT_STRING( FTP_ALTERNATIVE_TO_USER
)
2957 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
2958 SETOPT_BOOL( SSL_SESSIONID_CACHE
)
2961 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
2962 static void handle_SSH_AUTH_TYPES(Connection
*conn
, value option
)
2965 CAMLlocal1(listIter
);
2966 CURLcode result
= CURLE_OK
;
2967 long authTypes
= CURLSSH_AUTH_NONE
;
2971 while (!Is_long(listIter
))
2973 switch (Long_val(Field(listIter
, 0)))
2975 case 0: /* CURLSSH_AUTH_ANY */
2976 authTypes
|= CURLSSH_AUTH_ANY
;
2979 case 1: /* CURLSSH_AUTH_PUBLICKEY */
2980 authTypes
|= CURLSSH_AUTH_PUBLICKEY
;
2983 case 2: /* CURLSSH_AUTH_PASSWORD */
2984 authTypes
|= CURLSSH_AUTH_PASSWORD
;
2987 case 3: /* CURLSSH_AUTH_HOST */
2988 authTypes
|= CURLSSH_AUTH_HOST
;
2991 case 4: /* CURLSSH_AUTH_KEYBOARD */
2992 authTypes
|= CURLSSH_AUTH_KEYBOARD
;
2996 caml_failwith("Invalid CURLSSH_AUTH_TYPES Value");
3000 listIter
= Field(listIter
, 1);
3003 result
= curl_easy_setopt(conn
->handle
,
3004 CURLOPT_SSH_AUTH_TYPES
,
3007 if (result
!= CURLE_OK
)
3008 raiseError(conn
, result
);
3014 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
3015 SETOPT_STRING( SSH_PUBLIC_KEYFILE
)
3018 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
3019 SETOPT_STRING( SSH_PRIVATE_KEYFILE
)
3022 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
3023 static void handle_FTP_SSL_CCC(Connection
*conn
, value option
)
3026 CURLcode result
= CURLE_OK
;
3028 switch (Long_val(option
))
3030 case 0: /* CURLFTPSSL_CCC_NONE */
3031 result
= curl_easy_setopt(conn
->handle
,
3032 CURLOPT_FTP_SSL_CCC
,
3033 CURLFTPSSL_CCC_NONE
);
3036 case 1: /* CURLFTPSSL_CCC_PASSIVE */
3037 result
= curl_easy_setopt(conn
->handle
,
3038 CURLOPT_FTP_SSL_CCC
,
3039 CURLFTPSSL_CCC_PASSIVE
);
3042 case 2: /* CURLFTPSSL_CCC_ACTIVE */
3043 result
= curl_easy_setopt(conn
->handle
,
3044 CURLOPT_FTP_SSL_CCC
,
3045 CURLFTPSSL_CCC_ACTIVE
);
3049 caml_failwith("Invalid FTPSSL_CCC value");
3053 if (result
!= CURLE_OK
)
3054 raiseError(conn
, result
);
3060 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
3061 SETOPT_LONG( TIMEOUT_MS
)
3064 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
3065 SETOPT_LONG( CONNECTTIMEOUT_MS
)
3068 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
3069 SETOPT_BOOL( HTTP_TRANSFER_DECODING
)
3072 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
3073 SETOPT_BOOL( HTTP_CONTENT_DECODING
)
3076 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
3077 SETOPT_LONG( NEW_FILE_PERMS
)
3080 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
3081 SETOPT_LONG( NEW_DIRECTORY_PERMS
)
3084 #if HAVE_DECL_CURLOPT_POST301
3085 SETOPT_BOOL( POST301
)
3088 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
3089 SETOPT_STRING( SSH_HOST_PUBLIC_KEY_MD5
)
3092 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
3093 SETOPT_STRING( COPYPOSTFIELDS
)
3096 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
3097 SETOPT_BOOL( PROXY_TRANSFER_MODE
)
3100 #if HAVE_DECL_CURLOPT_AUTOREFERER
3101 SETOPT_BOOL( AUTOREFERER
)
3104 #if HAVE_DECL_CURLOPT_PROXYTYPE
3105 static void handle_PROXYTYPE(Connection
*conn
, value option
)
3108 CURLcode result
= CURLE_OK
;
3111 switch (Long_val(option
))
3113 case 0: proxy_type
= CURLPROXY_HTTP
; break;
3114 case 1: proxy_type
= CURLPROXY_HTTP_1_0
; break;
3115 case 2: proxy_type
= CURLPROXY_SOCKS4
; break;
3116 case 3: proxy_type
= CURLPROXY_SOCKS5
; break;
3117 case 4: proxy_type
= CURLPROXY_SOCKS4A
; break;
3118 case 5: proxy_type
= CURLPROXY_SOCKS5_HOSTNAME
; break;
3120 caml_failwith("Invalid curl proxy type");
3123 result
= curl_easy_setopt(conn
->handle
,
3127 if (result
!= CURLE_OK
)
3128 raiseError(conn
, result
);
3134 #if HAVE_DECL_CURLOPT_PROTOCOLS || HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3139 CURLPROTO_HTTP
, CURLPROTO_HTTPS
, CURLPROTO_FTP
, CURLPROTO_FTPS
, CURLPROTO_SCP
, CURLPROTO_SFTP
,
3140 CURLPROTO_TELNET
, CURLPROTO_LDAP
, CURLPROTO_LDAPS
, CURLPROTO_DICT
, CURLPROTO_FILE
, CURLPROTO_TFTP
,
3141 /* factor out with autoconf? */
3142 #if defined(CURLPROTO_IMAP)
3147 #if defined(CURLPROTO_IMAPS)
3152 #if defined(CURLPROTO_POP3)
3157 #if defined(CURLPROTO_POP3S)
3162 #if defined(CURLPROTO_SMTP)
3167 #if defined(CURLPROTO_SMTPS)
3172 #if defined(CURLPROTO_RTSP)
3177 #if defined(CURLPROTO_RTMP)
3182 #if defined(CURLPROTO_RTMPT)
3187 #if defined(CURLPROTO_RTMPE)
3192 #if defined(CURLPROTO_RTMPTE)
3197 #if defined(CURLPROTO_RTMPS)
3202 #if defined(CURLPROTO_RTMPTS)
3207 #if defined(CURLPROTO_GOPHER)
3214 static void handle_PROTOCOLSOPTION(CURLoption curlopt
, Connection
*conn
, value option
)
3217 CURLcode result
= CURLE_OK
;
3218 long bits
= convert_bit_list(protoMap
, sizeof(protoMap
) / sizeof(protoMap
[0]), option
);
3220 result
= curl_easy_setopt(conn
->handle
, curlopt
, bits
);
3222 if (result
!= CURLE_OK
)
3223 raiseError(conn
, result
);
3229 #if HAVE_DECL_CURLOPT_PROTOCOLS
3230 static void handle_PROTOCOLS(Connection
*conn
, value option
)
3232 handle_PROTOCOLSOPTION(CURLOPT_PROTOCOLS
, conn
, option
);
3236 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3237 static void handle_REDIR_PROTOCOLS(Connection
*conn
, value option
)
3239 handle_PROTOCOLSOPTION(CURLOPT_REDIR_PROTOCOLS
, conn
, option
);
3243 #if HAVE_DECL_CURLOPT_RESOLVE
3244 SETOPT_SLIST( RESOLVE
)
3247 #if HAVE_DECL_CURLOPT_DNS_SERVERS
3248 SETOPT_STRING( DNS_SERVERS
)
3251 #if HAVE_DECL_CURLOPT_MAIL_FROM
3252 SETOPT_STRING( MAIL_FROM
)
3255 #if HAVE_DECL_CURLOPT_MAIL_RCPT
3256 SETOPT_SLIST( MAIL_RCPT
)
3259 #if HAVE_DECL_CURLOPT_PIPEWAIT
3260 SETOPT_BOOL( PIPEWAIT
)
3263 #if HAVE_DECL_CURLOPT_USERNAME
3264 SETOPT_STRING( USERNAME
)
3267 #if HAVE_DECL_CURLOPT_PASSWORD
3268 SETOPT_STRING( PASSWORD
)
3271 #if HAVE_DECL_CURLOPT_LOGIN_OPTIONS
3272 SETOPT_STRING( LOGIN_OPTIONS
)
3275 #if HAVE_DECL_CURLOPT_CONNECT_TO
3276 SETOPT_SLIST( CONNECT_TO
)
3279 #if HAVE_DECL_CURLOPT_POSTREDIR
3281 static int curlPostRedir_table
[] = {
3282 CURL_REDIR_POST_ALL
,
3283 #if defined(CURL_REDIR_POST_301)
3284 CURL_REDIR_POST_301
,
3288 #if defined(CURL_REDIR_POST_302)
3289 CURL_REDIR_POST_302
,
3293 #if defined(CURL_REDIR_POST_303)
3294 CURL_REDIR_POST_303
,
3300 static void handle_POSTREDIR(Connection
*conn
, value option
)
3303 CURLcode result
= CURLE_OK
;
3304 long bitmask
= caml_convert_flag_list(option
, curlPostRedir_table
);
3306 result
= curl_easy_setopt(conn
->handle
,
3310 if (result
!= CURLE_OK
)
3311 raiseError(conn
, result
);
3317 SETOPT_VAL( SSH_KNOWNHOSTS
, String_val
)
3319 SETOPT_LONG( BUFFERSIZE
)
3321 #if HAVE_DECL_CURLOPT_DOH_URL
3322 SETOPT_STRING( DOH_URL
)
3325 #if HAVE_DECL_CURLOPT_AWS_SIGV4
3326 SETOPT_STRING( AWS_SIGV4
)
3329 SETOPT_BOOL( TCP_KEEPALIVE
)
3331 SETOPT_LONG( TCP_KEEPIDLE
)
3333 SETOPT_LONG( TCP_KEEPINTVL
)
3336 ** curl_easy_setopt helper function
3339 #define HAVE(name) { handle_ ## name, "CURLOPT_"#name }
3340 #define HAVENOT(name) { NULL, "CURLOPT_"#name }
3342 CURLOptionMapping implementedOptionMap
[] =
3344 HAVE(WRITEFUNCTION
),
3350 HAVE(HTTPPROXYTUNNEL
),
3354 #if HAVE_DECL_CURLOPT_NOSIGNAL
3366 #if HAVE_DECL_CURLOPT_ENCODING
3371 HAVE(FOLLOWLOCATION
),
3377 HAVE(ERRORBUFFER
), /* mutable buffer, as output value, do not duplicate */
3380 HAVE(POSTFIELDSIZE
),
3384 HAVE(LOW_SPEED_LIMIT
),
3385 HAVE(LOW_SPEED_TIME
),
3392 HAVE(SSLCERTPASSWD
),
3397 HAVE(SSLENGINE_DEFAULT
),
3401 HAVE(HEADERFUNCTION
),
3404 HAVE(TIMECONDITION
),
3406 HAVE(CUSTOMREQUEST
),
3409 HAVE(PROGRESSFUNCTION
),
3410 HAVE(SSL_VERIFYPEER
),
3417 HAVE(FRESH_CONNECT
),
3421 HAVE(CONNECTTIMEOUT
),
3423 HAVE(SSL_VERIFYHOST
),
3425 HAVE(SSL_CIPHER_LIST
),
3428 HAVE(DNS_CACHE_TIMEOUT
),
3429 HAVE(DNS_USE_GLOBAL_CACHE
),
3430 HAVE(DEBUGFUNCTION
),
3432 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
3433 HAVE(HTTP200ALIASES
),
3435 HAVENOT(HTTP200ALIASES
),
3437 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
3438 HAVE(UNRESTRICTED_AUTH
),
3440 HAVENOT(UNRESTRICTED_AUTH
),
3442 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
3445 HAVENOT(FTP_USE_EPRT
),
3447 #if HAVE_DECL_CURLOPT_HTTPAUTH
3452 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
3453 HAVE(FTP_CREATE_MISSING_DIRS
),
3455 HAVENOT(FTP_CREATE_MISSING_DIRS
),
3457 #if HAVE_DECL_CURLOPT_PROXYAUTH
3462 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
3463 HAVE(FTP_RESPONSE_TIMEOUT
),
3465 HAVENOT(FTP_RESPONSE_TIMEOUT
),
3467 #if HAVE_DECL_CURLOPT_IPRESOLVE
3472 #if HAVE_DECL_CURLOPT_MAXFILESIZE
3475 HAVENOT(MAXFILESIZE
),
3477 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
3478 HAVE(INFILESIZE_LARGE
),
3480 HAVENOT(INFILESIZE_LARGE
),
3482 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
3483 HAVE(RESUME_FROM_LARGE
),
3485 HAVENOT(RESUME_FROM_LARGE
),
3487 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
3488 HAVE(MAXFILESIZE_LARGE
),
3490 HAVENOT(MAXFILESIZE_LARGE
),
3492 #if HAVE_DECL_CURLOPT_NETRC_FILE
3495 HAVENOT(NETRC_FILE
),
3497 #if HAVE_DECL_CURLOPT_FTP_SSL
3502 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
3503 HAVE(POSTFIELDSIZE_LARGE
),
3505 HAVENOT(POSTFIELDSIZE_LARGE
),
3507 #if HAVE_DECL_CURLOPT_TCP_NODELAY
3510 HAVENOT(TCP_NODELAY
),
3512 #if HAVE_DECL_CURLOPT_TCP_FASTOPEN
3515 HAVENOT(TCP_FASTOPEN
),
3517 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
3520 HAVENOT(FTPSSLAUTH
),
3522 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
3523 HAVE(IOCTLFUNCTION
),
3525 HAVENOT(IOCTLFUNCTION
),
3527 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
3530 HAVENOT(FTP_ACCOUNT
),
3532 #if HAVE_DECL_CURLOPT_COOKIELIST
3535 HAVENOT(COOKIELIST
),
3537 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
3538 HAVE(IGNORE_CONTENT_LENGTH
),
3540 HAVENOT(IGNORE_CONTENT_LENGTH
),
3542 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
3543 HAVE(FTP_SKIP_PASV_IP
),
3545 HAVENOT(FTP_SKIP_PASV_IP
),
3547 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
3548 HAVE(FTP_FILEMETHOD
),
3550 HAVENOT(FTP_FILEMETHOD
),
3552 #if HAVE_DECL_CURLOPT_LOCALPORT
3557 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
3558 HAVE(LOCALPORTRANGE
),
3560 HAVENOT(LOCALPORTRANGE
),
3562 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
3565 HAVENOT(CONNECT_ONLY
),
3567 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
3568 HAVE(MAX_SEND_SPEED_LARGE
),
3570 HAVENOT(MAX_SEND_SPEED_LARGE
),
3572 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
3573 HAVE(MAX_RECV_SPEED_LARGE
),
3575 HAVENOT(MAX_RECV_SPEED_LARGE
),
3577 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
3578 HAVE(FTP_ALTERNATIVE_TO_USER
),
3580 HAVENOT(FTP_ALTERNATIVE_TO_USER
),
3582 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
3583 HAVE(SSL_SESSIONID_CACHE
),
3585 HAVENOT(SSL_SESSIONID_CACHE
),
3587 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
3588 HAVE(SSH_AUTH_TYPES
),
3590 HAVENOT(SSH_AUTH_TYPES
),
3592 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
3593 HAVE(SSH_PUBLIC_KEYFILE
),
3595 HAVENOT(SSH_PUBLIC_KEYFILE
),
3597 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
3598 HAVE(SSH_PRIVATE_KEYFILE
),
3600 HAVENOT(SSH_PRIVATE_KEYFILE
),
3602 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
3605 HAVENOT(FTP_SSL_CCC
),
3607 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
3610 HAVENOT(TIMEOUT_MS
),
3612 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
3613 HAVE(CONNECTTIMEOUT_MS
),
3615 HAVENOT(CONNECTTIMEOUT_MS
),
3617 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
3618 HAVE(HTTP_TRANSFER_DECODING
),
3620 HAVENOT(HTTP_TRANSFER_DECODING
),
3622 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
3623 HAVE(HTTP_CONTENT_DECODING
),
3625 HAVENOT(HTTP_CONTENT_DECODING
),
3627 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
3628 HAVE(NEW_FILE_PERMS
),
3630 HAVENOT(NEW_FILE_PERMS
),
3632 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
3633 HAVE(NEW_DIRECTORY_PERMS
),
3635 HAVENOT(NEW_DIRECTORY_PERMS
),
3637 #if HAVE_DECL_CURLOPT_POST301
3642 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
3643 HAVE(SSH_HOST_PUBLIC_KEY_MD5
),
3645 HAVENOT(SSH_HOST_PUBLIC_KEY_MD5
),
3647 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
3648 HAVE(COPYPOSTFIELDS
),
3650 HAVENOT(COPYPOSTFIELDS
),
3652 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
3653 HAVE(PROXY_TRANSFER_MODE
),
3655 HAVENOT(PROXY_TRANSFER_MODE
),
3657 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
3660 HAVENOT(SEEKFUNCTION
),
3662 #if HAVE_DECL_CURLOPT_AUTOREFERER
3665 HAVENOT(AUTOREFERER
),
3667 HAVE(OPENSOCKETFUNCTION
),
3668 /*HAVE(CLOSESOCKETFUNCTION),*/
3669 #if HAVE_DECL_CURLOPT_PROXYTYPE
3674 #if HAVE_DECL_CURLOPT_PROTOCOLS
3679 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3680 HAVE(REDIR_PROTOCOLS
),
3682 HAVENOT(REDIR_PROTOCOLS
),
3684 #if HAVE_DECL_CURLOPT_RESOLVE
3689 #if HAVE_DECL_CURLOPT_DNS_SERVERS
3692 HAVENOT(DNS_SERVERS
),
3694 #if HAVE_DECL_CURLOPT_MAIL_FROM
3699 #if HAVE_DECL_CURLOPT_MAIL_RCPT
3704 #if HAVE_DECL_CURLOPT_PIPEWAIT
3709 #if HAVE_DECL_CURLOPT_CERTINFO
3714 #if HAVE_DECL_CURLOPT_USERNAME
3719 #if HAVE_DECL_CURLOPT_PASSWORD
3724 #if HAVE_DECL_CURLOPT_LOGIN_OPTIONS
3725 HAVE(LOGIN_OPTIONS
),
3727 HAVENOT(LOGIN_OPTIONS
),
3729 #if HAVE_DECL_CURLOPT_CONNECT_TO
3732 HAVENOT(CONNECT_TO
),
3734 #if HAVE_DECL_CURLOPT_POSTREDIR
3739 #if HAVE_DECL_CURLOPT_MIMEPOST
3744 HAVE(SSH_KNOWNHOSTS
),
3745 HAVE(SSH_KEYFUNCTION
),
3747 #if HAVE_DECL_CURLOPT_DOH_URL
3752 #if HAVE_DECL_CURLOPT_SSL_OPTIONS
3755 HAVENOT(SSL_OPTIONS
),
3757 HAVE(WRITEFUNCTION2
),
3758 HAVE(READFUNCTION2
),
3759 #if HAVE_DECL_CURLOPT_XFERINFOFUNCTION
3760 HAVE(XFERINFOFUNCTION
),
3762 HAVENOT(XFERINFOFUNCTION
),
3764 #if HAVE_DECL_CURLOPT_PREREQFUNCTION
3765 HAVE(PREREQFUNCTION
),
3767 HAVENOT(PREREQFUNCTION
),
3769 #if HAVE_DECL_CURLOPT_AWS_SIGV4
3774 HAVE(TCP_KEEPALIVE
),
3776 HAVE(TCP_KEEPINTVL
),
3779 value
caml_curl_easy_setopt(value conn
, value option
)
3781 CAMLparam2(conn
, option
);
3783 Connection
*connection
= Connection_val(conn
);
3784 CURLOptionMapping
* thisOption
= NULL
;
3785 static const value
* exception
= NULL
;
3787 checkConnection(connection
);
3789 data
= Field(option
, 0);
3791 if (Tag_val(option
) < sizeof(implementedOptionMap
)/sizeof(CURLOptionMapping
))
3793 thisOption
= &implementedOptionMap
[Tag_val(option
)];
3794 if (thisOption
->optionHandler
)
3796 thisOption
->optionHandler(connection
, data
);
3800 if (NULL
== exception
)
3802 exception
= caml_named_value("Curl.NotImplemented");
3803 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented");
3806 caml_raise_with_string(*exception
, thisOption
->name
);
3811 caml_failwith("Invalid CURLOPT Option");
3814 CAMLreturn(Val_unit
);
3818 ** curl_easy_perform helper function
3821 value
caml_curl_easy_perform(value conn
)
3824 CURLcode result
= CURLE_OK
;
3825 Connection
*connection
= Connection_val(conn
);
3827 checkConnection(connection
);
3829 caml_enter_blocking_section();
3830 result
= curl_easy_perform(connection
->handle
);
3831 caml_leave_blocking_section();
3833 if (result
!= CURLE_OK
)
3834 raiseError(connection
, result
);
3836 CAMLreturn(Val_unit
);
3840 ** curl_easy_cleanup helper function
3843 value
caml_curl_easy_cleanup(value conn
)
3846 Connection
*connection
= Connection_val(conn
);
3848 checkConnection(connection
);
3850 removeConnection(connection
, 0);
3852 CAMLreturn(Val_unit
);
3856 ** curl_easy_getinfo helper function
3859 enum GetInfoResultType
{
3860 StringValue
, LongValue
, DoubleValue
, StringListValue
, StringListListValue
,
3861 SocketValue
, OCamlValue
, /* keep last - no matching OCaml CURLINFO_ constructor */
3864 value
convertStringList(struct curl_slist
*p
)
3867 CAMLlocal3(result
, current
, next
);
3869 result
= Val_emptylist
;
3870 current
= Val_emptylist
;
3871 next
= Val_emptylist
;
3875 next
= caml_alloc_tuple(2);
3876 Store_field(next
, 0, caml_copy_string(p
->data
));
3877 Store_field(next
, 1, Val_emptylist
);
3879 if (result
== Val_emptylist
)
3882 if (current
!= Val_emptylist
)
3883 Store_field(current
, 1, next
);
3893 value
caml_curl_easy_getinfo(value conn
, value option
)
3895 CAMLparam2(conn
, option
);
3896 CAMLlocal3(result
, current
, next
);
3897 CURLcode curlResult
;
3898 Connection
*connection
= Connection_val(conn
);
3899 enum GetInfoResultType resultType
;
3900 char *strValue
= NULL
;
3903 curl_socket_t socketValue
;
3904 struct curl_slist
*stringListValue
= NULL
;
3905 #if HAVE_DECL_CURLINFO_CERTINFO
3908 struct curl_slist
*to_info
;
3909 struct curl_certinfo
*to_certinfo
;
3913 checkConnection(connection
);
3915 switch(Long_val(option
))
3917 #if HAVE_DECL_CURLINFO_EFFECTIVE_URL
3918 case 0: /* CURLINFO_EFFECTIVE_URL */
3919 resultType
= StringValue
;
3921 curlResult
= curl_easy_getinfo(connection
->handle
,
3922 CURLINFO_EFFECTIVE_URL
,
3926 #pragma message("libcurl does not provide CURLINFO_EFFECTIVE_URL")
3929 #if HAVE_DECL_CURLINFO_RESPONSE_CODE || HAVE_DECL_CURLINFO_HTTP_CODE
3930 case 1: /* CURLINFO_HTTP_CODE */
3931 case 2: /* CURLINFO_RESPONSE_CODE */
3932 #if HAVE_DECL_CURLINFO_RESPONSE_CODE
3933 resultType
= LongValue
;
3935 curlResult
= curl_easy_getinfo(connection
->handle
,
3936 CURLINFO_RESPONSE_CODE
,
3939 resultType
= LongValue
;
3941 curlResult
= curl_easy_getinfo(connection
->handle
,
3948 #if HAVE_DECL_CURLINFO_TOTAL_TIME
3949 case 3: /* CURLINFO_TOTAL_TIME */
3950 resultType
= DoubleValue
;
3952 curlResult
= curl_easy_getinfo(connection
->handle
,
3953 CURLINFO_TOTAL_TIME
,
3958 #if HAVE_DECL_CURLINFO_NAMELOOKUP_TIME
3959 case 4: /* CURLINFO_NAMELOOKUP_TIME */
3960 resultType
= DoubleValue
;
3962 curlResult
= curl_easy_getinfo(connection
->handle
,
3963 CURLINFO_NAMELOOKUP_TIME
,
3968 #if HAVE_DECL_CURLINFO_CONNECT_TIME
3969 case 5: /* CURLINFO_CONNECT_TIME */
3970 resultType
= DoubleValue
;
3972 curlResult
= curl_easy_getinfo(connection
->handle
,
3973 CURLINFO_CONNECT_TIME
,
3978 #if HAVE_DECL_CURLINFO_PRETRANSFER_TIME
3979 case 6: /* CURLINFO_PRETRANSFER_TIME */
3980 resultType
= DoubleValue
;
3982 curlResult
= curl_easy_getinfo(connection
->handle
,
3983 CURLINFO_PRETRANSFER_TIME
,
3988 #if HAVE_DECL_CURLINFO_SIZE_UPLOAD
3989 case 7: /* CURLINFO_SIZE_UPLOAD */
3990 resultType
= DoubleValue
;
3992 curlResult
= curl_easy_getinfo(connection
->handle
,
3993 CURLINFO_SIZE_UPLOAD
,
3998 #if HAVE_DECL_CURLINFO_SIZE_DOWNLOAD
3999 case 8: /* CURLINFO_SIZE_DOWNLOAD */
4000 resultType
= DoubleValue
;
4002 curlResult
= curl_easy_getinfo(connection
->handle
,
4003 CURLINFO_SIZE_DOWNLOAD
,
4008 #if HAVE_DECL_CURLINFO_SPEED_DOWNLOAD
4009 case 9: /* CURLINFO_SPEED_DOWNLOAD */
4010 resultType
= DoubleValue
;
4012 curlResult
= curl_easy_getinfo(connection
->handle
,
4013 CURLINFO_SPEED_DOWNLOAD
,
4018 #if HAVE_DECL_CURLINFO_SPEED_UPLOAD
4019 case 10: /* CURLINFO_SPEED_UPLOAD */
4020 resultType
= DoubleValue
;
4022 curlResult
= curl_easy_getinfo(connection
->handle
,
4023 CURLINFO_SPEED_UPLOAD
,
4029 #if HAVE_DECL_CURLINFO_HEADER_SIZE
4030 case 11: /* CURLINFO_HEADER_SIZE */
4031 resultType
= LongValue
;
4033 curlResult
= curl_easy_getinfo(connection
->handle
,
4034 CURLINFO_HEADER_SIZE
,
4039 #if HAVE_DECL_CURLINFO_REQUEST_SIZE
4040 case 12: /* CURLINFO_REQUEST_SIZE */
4041 resultType
= LongValue
;
4043 curlResult
= curl_easy_getinfo(connection
->handle
,
4044 CURLINFO_REQUEST_SIZE
,
4049 #if HAVE_DECL_CURLINFO_SSL_VERIFYRESULT
4050 case 13: /* CURLINFO_SSL_VERIFYRESULT */
4051 resultType
= LongValue
;
4053 curlResult
= curl_easy_getinfo(connection
->handle
,
4054 CURLINFO_SSL_VERIFYRESULT
,
4059 #if HAVE_DECL_CURLINFO_FILETIME
4060 case 14: /* CURLINFO_FILETIME */
4061 resultType
= DoubleValue
;
4063 curlResult
= curl_easy_getinfo(connection
->handle
,
4067 doubleValue
= longValue
;
4071 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_DOWNLOAD
4072 case 15: /* CURLINFO_CONTENT_LENGTH_DOWNLOAD */
4073 resultType
= DoubleValue
;
4075 curlResult
= curl_easy_getinfo(connection
->handle
,
4076 CURLINFO_CONTENT_LENGTH_DOWNLOAD
,
4081 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_UPLOAD
4082 case 16: /* CURLINFO_CONTENT_LENGTH_UPLOAD */
4083 resultType
= DoubleValue
;
4085 curlResult
= curl_easy_getinfo(connection
->handle
,
4086 CURLINFO_CONTENT_LENGTH_UPLOAD
,
4091 #if HAVE_DECL_CURLINFO_STARTTRANSFER_TIME
4092 case 17: /* CURLINFO_STARTTRANSFER_TIME */
4093 resultType
= DoubleValue
;
4095 curlResult
= curl_easy_getinfo(connection
->handle
,
4096 CURLINFO_STARTTRANSFER_TIME
,
4101 #if HAVE_DECL_CURLINFO_CONTENT_TYPE
4102 case 18: /* CURLINFO_CONTENT_TYPE */
4103 resultType
= StringValue
;
4105 curlResult
= curl_easy_getinfo(connection
->handle
,
4106 CURLINFO_CONTENT_TYPE
,
4111 #if HAVE_DECL_CURLINFO_REDIRECT_TIME
4112 case 19: /* CURLINFO_REDIRECT_TIME */
4113 resultType
= DoubleValue
;
4115 curlResult
= curl_easy_getinfo(connection
->handle
,
4116 CURLINFO_REDIRECT_TIME
,
4121 #if HAVE_DECL_CURLINFO_REDIRECT_COUNT
4122 case 20: /* CURLINFO_REDIRECT_COUNT */
4123 resultType
= LongValue
;
4125 curlResult
= curl_easy_getinfo(connection
->handle
,
4126 CURLINFO_REDIRECT_COUNT
,
4131 case 21: /* CURLINFO_PRIVATE */
4132 resultType
= OCamlValue
;
4133 curlResult
= CURLE_OK
;
4134 result
= caml_alloc(1, StringValue
);
4135 Store_field(result
, 0, Field(connection
->ocamlValues
, Ocaml_PRIVATE
));
4138 #if HAVE_DECL_CURLINFO_HTTP_CONNECTCODE
4139 case 22: /* CURLINFO_HTTP_CONNECTCODE */
4140 resultType
= LongValue
;
4142 curlResult
= curl_easy_getinfo(connection
->handle
,
4143 CURLINFO_HTTP_CONNECTCODE
,
4148 #if HAVE_DECL_CURLINFO_HTTPAUTH_AVAIL
4149 case 23: /* CURLINFO_HTTPAUTH_AVAIL */
4150 resultType
= LongValue
;
4152 curlResult
= curl_easy_getinfo(connection
->handle
,
4153 CURLINFO_HTTPAUTH_AVAIL
,
4158 #if HAVE_DECL_CURLINFO_PROXYAUTH_AVAIL
4159 case 24: /* CURLINFO_PROXYAUTH_AVAIL */
4160 resultType
= LongValue
;
4162 curlResult
= curl_easy_getinfo(connection
->handle
,
4163 CURLINFO_PROXYAUTH_AVAIL
,
4168 #if HAVE_DECL_CURLINFO_OS_ERRNO
4169 case 25: /* CURLINFO_OS_ERRNO */
4170 resultType
= LongValue
;
4172 curlResult
= curl_easy_getinfo(connection
->handle
,
4178 #if HAVE_DECL_CURLINFO_NUM_CONNECTS
4179 case 26: /* CURLINFO_NUM_CONNECTS */
4180 resultType
= LongValue
;
4182 curlResult
= curl_easy_getinfo(connection
->handle
,
4183 CURLINFO_NUM_CONNECTS
,
4188 #if HAVE_DECL_CURLINFO_SSL_ENGINES
4189 case 27: /* CURLINFO_SSL_ENGINES */
4190 resultType
= StringListValue
;
4192 curlResult
= curl_easy_getinfo(connection
->handle
,
4193 CURLINFO_SSL_ENGINES
,
4198 #if HAVE_DECL_CURLINFO_COOKIELIST
4199 case 28: /* CURLINFO_COOKIELIST */
4200 resultType
= StringListValue
;
4202 curlResult
= curl_easy_getinfo(connection
->handle
,
4203 CURLINFO_COOKIELIST
,
4208 #if HAVE_DECL_CURLINFO_LASTSOCKET
4209 case 29: /* CURLINFO_LASTSOCKET */
4210 resultType
= LongValue
;
4212 curlResult
= curl_easy_getinfo(connection
->handle
,
4213 CURLINFO_LASTSOCKET
,
4218 #if HAVE_DECL_CURLINFO_FTP_ENTRY_PATH
4219 case 30: /* CURLINFO_FTP_ENTRY_PATH */
4220 resultType
= StringValue
;
4222 curlResult
= curl_easy_getinfo(connection
->handle
,
4223 CURLINFO_FTP_ENTRY_PATH
,
4228 #if HAVE_DECL_CURLINFO_REDIRECT_URL
4229 case 31: /* CURLINFO_REDIRECT_URL */
4230 resultType
= StringValue
;
4232 curlResult
= curl_easy_getinfo(connection
->handle
,
4233 CURLINFO_REDIRECT_URL
,
4237 #pragma message("libcurl does not provide CURLINFO_REDIRECT_URL")
4240 #if HAVE_DECL_CURLINFO_PRIMARY_IP
4241 case 32: /* CURLINFO_PRIMARY_IP */
4242 resultType
= StringValue
;
4244 curlResult
= curl_easy_getinfo(connection
->handle
,
4245 CURLINFO_PRIMARY_IP
,
4249 #pragma message("libcurl does not provide CURLINFO_PRIMARY_IP")
4252 #if HAVE_DECL_CURLINFO_LOCAL_IP
4253 case 33: /* CURLINFO_LOCAL_IP */
4254 resultType
= StringValue
;
4256 curlResult
= curl_easy_getinfo(connection
->handle
,
4261 #pragma message("libcurl does not provide CURLINFO_LOCAL_IP")
4264 #if HAVE_DECL_CURLINFO_LOCAL_PORT
4265 case 34: /* CURLINFO_LOCAL_PORT */
4266 resultType
= LongValue
;
4268 curlResult
= curl_easy_getinfo(connection
->handle
,
4269 CURLINFO_LOCAL_PORT
,
4273 #pragma message("libcurl does not provide CURLINFO_LOCAL_PORT")
4276 #if HAVE_DECL_CURLINFO_CONDITION_UNMET
4277 case 35: /* CURLINFO_CONDITION_UNMET */
4278 resultType
= LongValue
;
4280 curlResult
= curl_easy_getinfo(connection
->handle
,
4281 CURLINFO_CONDITION_UNMET
,
4285 #pragma message("libcurl does not provide CURLINFO_CONDITION_UNMET")
4287 #if HAVE_DECL_CURLINFO_CERTINFO
4288 case 36: /* CURLINFO_CERTINFO */
4289 resultType
= StringListListValue
;
4291 curlResult
= curl_easy_getinfo(connection
->handle
,
4295 result
= Val_emptylist
;
4296 current
= Val_emptylist
;
4297 next
= Val_emptylist
;
4299 if (curlResult
!= CURLE_OK
|| !ptr
.to_info
)
4302 for (i
= 0; i
< ptr
.to_certinfo
->num_of_certs
; i
++) {
4303 next
= caml_alloc_tuple(2);
4304 Store_field(next
, 0, convertStringList(ptr
.to_certinfo
->certinfo
[i
]));
4305 Store_field(next
, 1, current
);
4310 #pragma message("libcurl does not provide CURLINFO_CERTINFO")
4312 #if HAVE_DECL_CURLINFO_ACTIVESOCKET
4313 case 37: /* CURLINFO_ACTIVESOCKET */
4314 resultType
= SocketValue
;
4316 curlResult
= curl_easy_getinfo(connection
->handle
,
4317 CURLINFO_ACTIVESOCKET
,
4321 #pragma message("libcurl does not provide CURLINFO_ACTIVESOCKET")
4323 #if HAVE_DECL_CURLINFO_HTTP_VERSION
4324 case 38: /* CURLINFO_HTTP_VERSION */
4325 resultType
= LongValue
;
4327 curlResult
= curl_easy_getinfo(connection
->handle
,
4328 CURLINFO_HTTP_VERSION
,
4331 longValue
= ocaml_HTTP_VERSION(longValue
);
4335 #pragma message("libcurl does not provide CURLINFO_HTTP_VERSION")
4337 /* sync check_enums */
4339 caml_failwith("Invalid CURLINFO Option");
4343 if (curlResult
!= CURLE_OK
)
4344 raiseError(connection
, curlResult
);
4349 result
= caml_alloc(1, StringValue
);
4350 Store_field(result
, 0, caml_copy_string(strValue
?strValue
:""));
4354 result
= caml_alloc(1, LongValue
);
4355 Store_field(result
, 0, Val_long(longValue
));
4359 result
= caml_alloc(1, DoubleValue
);
4360 Store_field(result
, 0, caml_copy_double(doubleValue
));
4363 case StringListValue
:
4364 result
= caml_alloc(1, StringListValue
);
4365 Store_field(result
, 0, convertStringList(stringListValue
));
4366 curl_slist_free_all(stringListValue
);
4369 case StringListListValue
:
4370 result
= caml_alloc(1, StringListListValue
);
4371 Store_field(result
, 0, current
);
4375 result
= caml_alloc(1, SocketValue
);
4376 Store_field(result
, 0, Val_socket(socketValue
));
4387 ** curl_escape helper function
4390 value
caml_curl_escape(value str
)
4396 curlResult
= curl_escape(String_val(str
), caml_string_length(str
));
4397 result
= caml_copy_string(curlResult
);
4404 ** curl_unescape helper function
4407 value
caml_curl_unescape(value str
)
4413 curlResult
= curl_unescape(String_val(str
), caml_string_length(str
));
4414 result
= caml_copy_string(curlResult
);
4421 ** curl_getdate helper function
4424 value
caml_curl_getdate(value str
, value now
)
4426 CAMLparam2(str
, now
);
4431 curlNow
= (time_t)Double_val(now
);
4432 curlResult
= curl_getdate(String_val(str
), &curlNow
);
4433 result
= caml_copy_double((double)curlResult
);
4439 ** curl_version helper function
4442 value
caml_curl_version(void)
4448 str
= curl_version();
4449 result
= caml_copy_string(str
);
4454 struct CURLVersionBitsMapping
4460 struct CURLVersionBitsMapping versionBitsMap
[] =
4462 {CURL_VERSION_IPV6
, "ipv6"},
4463 {CURL_VERSION_KERBEROS4
, "kerberos4"},
4464 {CURL_VERSION_SSL
, "ssl"},
4465 {CURL_VERSION_LIBZ
, "libz"},
4466 {CURL_VERSION_NTLM
, "ntlm"},
4467 {CURL_VERSION_GSSNEGOTIATE
, "gssnegotiate"},
4468 {CURL_VERSION_DEBUG
, "debug"},
4469 {CURL_VERSION_CURLDEBUG
, "curldebug"},
4470 {CURL_VERSION_ASYNCHDNS
, "asynchdns"},
4471 {CURL_VERSION_SPNEGO
, "spnego"},
4472 {CURL_VERSION_LARGEFILE
, "largefile"},
4473 {CURL_VERSION_IDN
, "idn"},
4474 {CURL_VERSION_SSPI
, "sspi"},
4475 {CURL_VERSION_CONV
, "conv"},
4476 #if HAVE_DECL_CURL_VERSION_TLSAUTH_SRP
4477 {CURL_VERSION_TLSAUTH_SRP
, "srp"},
4479 #if HAVE_DECL_CURL_VERSION_NTLM_WB
4480 {CURL_VERSION_NTLM_WB
, "wb"},
4484 value
caml_curl_version_info(value unit
)
4487 CAMLlocal4(v
, vlist
, vnum
, vfeatures
);
4488 const char* const* p
= NULL
;
4491 curl_version_info_data
* data
= curl_version_info(CURLVERSION_NOW
);
4492 if (NULL
== data
) caml_failwith("curl_version_info");
4494 vlist
= Val_emptylist
;
4495 for (p
= data
->protocols
; NULL
!= *p
; p
++)
4497 vlist
= Val_cons(vlist
, caml_copy_string(*p
));
4500 vfeatures
= Val_emptylist
;
4501 for (i
= 0; i
< sizeof(versionBitsMap
)/sizeof(versionBitsMap
[0]); i
++)
4503 if (0 != (versionBitsMap
[i
].code
& data
->features
))
4504 vfeatures
= Val_cons(vfeatures
, caml_copy_string(versionBitsMap
[i
].name
));
4507 vnum
= caml_alloc_tuple(3);
4508 Store_field(vnum
,0,Val_int(0xFF & (data
->version_num
>> 16)));
4509 Store_field(vnum
,1,Val_int(0xFF & (data
->version_num
>> 8)));
4510 Store_field(vnum
,2,Val_int(0xFF & (data
->version_num
)));
4512 v
= caml_alloc_tuple(12);
4513 Store_field(v
,0,caml_copy_string(data
->version
));
4514 Store_field(v
,1,vnum
);
4515 Store_field(v
,2,caml_copy_string(data
->host
));
4516 Store_field(v
,3,vfeatures
);
4517 Store_field(v
,4,data
->ssl_version
? Val_some(caml_copy_string(data
->ssl_version
)) : Val_none
);
4518 Store_field(v
,5,data
->libz_version
? Val_some(caml_copy_string(data
->libz_version
)) : Val_none
);
4519 Store_field(v
,6,vlist
);
4520 Store_field(v
,7,caml_copy_string((data
->age
>= 1 && data
->ares
) ? data
->ares
: ""));
4521 Store_field(v
,8,Val_int((data
->age
>= 1) ? data
->ares_num
: 0));
4522 Store_field(v
,9,caml_copy_string((data
->age
>= 2 && data
->libidn
) ? data
->libidn
: ""));
4523 Store_field(v
,10,Val_int((data
->age
>= 3) ? data
->iconv_ver_num
: 0));
4524 Store_field(v
,11,caml_copy_string((data
->age
>= 3 && data
->libssh_version
) ? data
->libssh_version
: ""));
4529 value
caml_curl_pause(value conn
, value opts
)
4531 CAMLparam2(conn
, opts
);
4532 CAMLlocal4(v
, vlist
, vnum
, vfeatures
);
4533 Connection
*connection
= Connection_val(conn
);
4537 while (Val_emptylist
!= opts
)
4539 switch (Int_val(Field(opts
,0)))
4541 case 0: bitmask
|= CURLPAUSE_SEND
; break;
4542 case 1: bitmask
|= CURLPAUSE_RECV
; break;
4543 case 2: bitmask
|= CURLPAUSE_ALL
; break;
4544 default: caml_failwith("wrong pauseOption");
4546 opts
= Field(opts
,1);
4549 caml_enter_blocking_section();
4550 result
= curl_easy_pause(connection
->handle
,bitmask
);
4551 caml_leave_blocking_section();
4553 if (result
!= CURLE_OK
)
4554 raiseError(connection
, result
);
4556 CAMLreturn(Val_unit
);
4560 * Curl multi stack support
4562 * Exported thin wrappers for libcurl are prefixed with caml_curl_multi_.
4563 * Other exported functions are prefixed with caml_curlm_, some of them
4564 * can/should be decomposed into smaller parts.
4567 struct ml_multi_handle
4570 value values
; /* callbacks */
4575 curlmopt_socket_function
,
4576 curlmopt_timer_function
,
4578 /* last, not used */
4582 typedef struct ml_multi_handle ml_multi_handle
;
4584 #define Multi_val(v) (*(ml_multi_handle**)Data_custom_val(v))
4585 #define CURLM_val(v) (Multi_val(v)->handle)
4587 static struct custom_operations curl_multi_ops
= {
4589 custom_finalize_default
,
4590 custom_compare_default
,
4591 custom_hash_default
,
4592 custom_serialize_default
,
4593 custom_deserialize_default
,
4594 #if defined(custom_compare_ext_default)
4595 custom_compare_ext_default
,
4599 static void raise_multi_error(char const* msg
)
4601 static const value
* exception
= NULL
;
4603 if (NULL
== exception
)
4605 exception
= caml_named_value("Curl.Multi.Error");
4606 if (NULL
== exception
) caml_invalid_argument("Curl.Multi.Error");
4609 caml_raise_with_string(*exception
, msg
);
4612 static void raise_multi_cerror(char const* func
, CURLMcode code
)
4616 static const value
* exception
= NULL
;
4618 if (NULL
== exception
)
4620 exception
= caml_named_value("Curl.Multi.CError");
4621 if (NULL
== exception
) caml_invalid_argument("Curl.Multi.CError");
4624 data
= caml_alloc(4, 0);
4626 Store_field(data
, 0, *exception
);
4627 Store_field(data
, 1, caml_copy_string(func
));
4628 Store_field(data
, 2, Val_int(code
));
4629 Store_field(data
, 3, caml_copy_string(curl_multi_strerror(code
)));
4636 static void check_mcode(char const* func
, CURLMcode code
)
4638 if (code
!= CURLM_OK
) raise_multi_cerror(func
,code
);
4641 value
caml_curl_multi_init(value unit
)
4645 ml_multi_handle
* multi
= (ml_multi_handle
*)caml_stat_alloc(sizeof(ml_multi_handle
));
4646 CURLM
* h
= curl_multi_init();
4650 caml_stat_free(multi
);
4651 raise_multi_error("caml_curl_multi_init");
4655 multi
->values
= caml_alloc(multi_values_total
, 0);
4656 caml_register_generational_global_root(&multi
->values
);
4658 v
= caml_alloc_custom(&curl_multi_ops
, sizeof(ml_multi_handle
*), 0, 1);
4659 Multi_val(v
) = multi
;
4664 value
caml_curl_multi_cleanup(value handle
)
4667 ml_multi_handle
* h
= Multi_val(handle
);
4670 CAMLreturn(Val_unit
);
4672 caml_remove_generational_global_root(&h
->values
);
4674 CURLcode rc
= curl_multi_cleanup(h
->handle
);
4677 Multi_val(handle
) = (ml_multi_handle
*)NULL
;
4679 check_mcode("curl_multi_cleanup",rc
);
4681 CAMLreturn(Val_unit
);
4684 static CURL
* curlm_remove_finished(CURLM
* multi_handle
, CURLcode
* result
)
4686 int msgs_in_queue
= 0;
4690 CURLMsg
* msg
= curl_multi_info_read(multi_handle
, &msgs_in_queue
);
4691 if (NULL
== msg
) return NULL
;
4692 if (CURLMSG_DONE
== msg
->msg
)
4694 CURL
* easy_handle
= msg
->easy_handle
;
4695 if (result
) *result
= msg
->data
.result
;
4696 if (CURLM_OK
!= curl_multi_remove_handle(multi_handle
, easy_handle
))
4698 /*caml_failwith("curlm_remove_finished");*/
4705 value
caml_curlm_remove_finished(value v_multi
)
4707 CAMLparam1(v_multi
);
4708 CAMLlocal2(v_easy
, v_tuple
);
4710 CURLM
* multi_handle
;
4712 Connection
* conn
= NULL
;
4714 multi_handle
= CURLM_val(v_multi
);
4716 caml_enter_blocking_section();
4717 handle
= curlm_remove_finished(multi_handle
,&result
);
4718 caml_leave_blocking_section();
4722 CAMLreturn(Val_none
);
4726 conn
= getConnection(handle
);
4727 if (conn
->curl_ERRORBUFFER
!= NULL
)
4729 Store_field(Field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
), 0, caml_copy_string(conn
->curl_ERRORBUFFER
));
4732 /* NB: same handle, but different block */
4733 v_easy
= caml_curl_alloc(conn
);
4734 v_tuple
= caml_alloc(2, 0);
4735 Store_field(v_tuple
,0,v_easy
);
4736 Store_field(v_tuple
,1,Val_int(result
)); /* CURLcode */
4737 CAMLreturn(Val_some(v_tuple
));
4741 static int curlWait_table
[] = {
4747 static int list_length(value v
)
4750 while (v
!= Val_emptylist
) {
4757 static struct curl_waitfd
*convert_extra_fds(value v_extra_fds
, int *nr_extra_fds
)
4759 /* NB this function doesn't trigger GC so can avoid registering local roots */
4762 int nfds
= list_length(v_extra_fds
);
4763 *nr_extra_fds
= nfds
;
4765 if (nfds
== 0) return NULL
;
4767 struct curl_waitfd
*extra_fds
= caml_stat_alloc(sizeof(extra_fds
[0]) * nfds
);
4770 while (v_extra_fds
!= Val_emptylist
)
4772 v_extra_fd
= Field(v_extra_fds
, 0);
4773 extra_fds
[i
].fd
= Socket_val(Field(v_extra_fd
, 0));
4774 extra_fds
[i
].events
= caml_convert_flag_list(Field(v_extra_fd
, 1), curlWait_table
);
4775 extra_fds
[i
].revents
= 0;
4777 v_extra_fds
= Field(v_extra_fds
, 1);
4783 /* Assumes v_extra_fds and extra_fds to have the same number of elements */
4784 static void update_extra_fds(value v_extra_fds
, struct curl_waitfd
*extra_fds
)
4786 CAMLparam1(v_extra_fds
);
4787 CAMLlocal2(v_extra_fd
, lst
);
4790 while (v_extra_fds
!= Val_emptylist
)
4792 v_extra_fd
= Field(v_extra_fds
, 0);
4793 lst
= Val_emptylist
;
4794 for (int j
= 0; j
< sizeof(curlWait_table
)/sizeof(curlWait_table
[0]); j
++)
4796 if (curlWait_table
[j
] & extra_fds
[i
].revents
)
4797 lst
= Val_cons(Val_int(j
), lst
);
4799 Store_field(v_extra_fd
, 2, lst
);
4801 v_extra_fds
= Field(v_extra_fds
, 1);
4807 value
caml_curl_multi_wait(value v_timeout_ms
, value v_extra_fds
, value v_multi
)
4809 CAMLparam3(v_timeout_ms
,v_extra_fds
,v_multi
);
4810 CURLM
*multi_handle
= CURLM_val(v_multi
);
4811 int timeout_ms
= Int_val(v_timeout_ms
);
4813 int nr_extra_fds
= 0;
4814 struct curl_waitfd
*extra_fds
= convert_extra_fds(v_extra_fds
, &nr_extra_fds
);
4817 caml_enter_blocking_section();
4818 rc
= curl_multi_wait(multi_handle
, extra_fds
, nr_extra_fds
, timeout_ms
, &numfds
);
4819 caml_leave_blocking_section();
4821 if (extra_fds
!= NULL
) {
4822 update_extra_fds(v_extra_fds
, extra_fds
);
4823 caml_stat_free(extra_fds
);
4826 check_mcode("curl_multi_wait",rc
);
4828 CAMLreturn(Val_bool(numfds
!= 0));
4831 value
caml_curl_multi_poll(value v_timeout_ms
, value v_extra_fds
, value v_multi
)
4833 CAMLparam3(v_timeout_ms
,v_extra_fds
,v_multi
);
4834 CURLM
*multi_handle
= CURLM_val(v_multi
);
4835 int timeout_ms
= Int_val(v_timeout_ms
);
4837 int nr_extra_fds
= 0;
4838 struct curl_waitfd
*extra_fds
= convert_extra_fds(v_extra_fds
, &nr_extra_fds
);
4841 caml_enter_blocking_section();
4842 #if HAVE_DECL_CURL_MULTI_POLL
4843 rc
= curl_multi_poll(multi_handle
, extra_fds
, nr_extra_fds
, timeout_ms
, &numfds
);
4845 rc
= curl_multi_wait(multi_handle
, extra_fds
, nr_extra_fds
, timeout_ms
, &numfds
);
4847 caml_leave_blocking_section();
4849 if (extra_fds
!= NULL
) {
4850 update_extra_fds(v_extra_fds
, extra_fds
);
4851 caml_stat_free(extra_fds
);
4854 check_mcode("curl_multi_poll",rc
);
4856 CAMLreturn(Val_bool(numfds
!= 0));
4859 value
caml_curl_multi_add_handle(value v_multi
, value v_easy
)
4861 CAMLparam2(v_multi
,v_easy
);
4862 CURLMcode rc
= CURLM_OK
;
4863 CURLM
* multi
= CURLM_val(v_multi
);
4864 Connection
* conn
= Connection_val(v_easy
);
4866 /* prevent collection of OCaml value while the easy handle is used
4867 and may invoke callbacks registered on OCaml side */
4870 /* may invoke callbacks so need to be consistent with locks */
4871 caml_enter_blocking_section();
4872 rc
= curl_multi_add_handle(multi
, conn
->handle
);
4875 conn
->refcount
--; /* not added, revert */
4876 caml_leave_blocking_section();
4877 check_mcode("curl_multi_add_handle",rc
);
4879 caml_leave_blocking_section();
4881 CAMLreturn(Val_unit
);
4884 value
caml_curl_multi_remove_handle(value v_multi
, value v_easy
)
4886 CAMLparam2(v_multi
,v_easy
);
4887 CURLM
* multi
= CURLM_val(v_multi
);
4888 CURLMcode rc
= CURLM_OK
;
4889 Connection
* conn
= Connection_val(v_easy
);
4891 /* may invoke callbacks so need to be consistent with locks */
4892 caml_enter_blocking_section();
4893 rc
= curl_multi_remove_handle(multi
, conn
->handle
);
4895 caml_leave_blocking_section();
4896 check_mcode("curl_multi_remove_handle",rc
);
4898 CAMLreturn(Val_unit
);
4901 value
caml_curl_multi_perform_all(value v_multi
)
4903 CAMLparam1(v_multi
);
4904 int still_running
= 0;
4905 CURLM
* h
= CURLM_val(v_multi
);
4907 caml_enter_blocking_section();
4908 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(h
, &still_running
));
4909 caml_leave_blocking_section();
4911 CAMLreturn(Val_int(still_running
));
4914 /* currently curlCode repr tag matches CURLE_ value
4915 * this is exploited below, but generally should use errorMap */
4917 value
caml_curl_strerror(value v_code
)
4920 CAMLreturn(caml_copy_string(curl_easy_strerror((CURLcode
)Int_val(v_code
))));
4923 value
caml_curl_int_of_curlCode(value v_code
)
4928 value
caml_curl_curlCode_of_int(value v
)
4930 return (Int_val(v
) < sizeof(errorMap
) / sizeof(errorMap
[0]) ? Val_some(v
) : Val_none
);
4933 value
caml_curl_multi_socket_action(value v_multi
, value v_fd
, value v_kind
)
4935 CAMLparam3(v_multi
, v_fd
, v_kind
);
4936 CURLM
* h
= CURLM_val(v_multi
);
4937 int still_running
= 0;
4938 CURLMcode rc
= CURLM_OK
;
4939 curl_socket_t socket
;
4942 if (Val_none
== v_fd
)
4944 socket
= CURL_SOCKET_TIMEOUT
;
4948 socket
= Socket_val(Field(v_fd
, 0));
4951 switch (Int_val(v_kind
))
4954 case 1 : kind
|= CURL_CSELECT_IN
; break;
4955 case 2 : kind
|= CURL_CSELECT_OUT
; break;
4956 case 3 : kind
|= CURL_CSELECT_IN
| CURL_CSELECT_OUT
; break;
4958 raise_multi_error("caml_curl_multi_socket_action");
4961 /* fprintf(stdout,"fd %u kind %u\n",socket, kind); fflush(stdout); */
4963 caml_enter_blocking_section();
4965 rc
= curl_multi_socket_action(h
, socket
, kind
, &still_running
);
4966 } while (rc
== CURLM_CALL_MULTI_PERFORM
);
4967 caml_leave_blocking_section();
4969 check_mcode("curl_multi_socket_action",rc
);
4971 CAMLreturn(Val_int(still_running
));
4974 value
caml_curl_multi_socket_all(value v_multi
)
4976 CAMLparam1(v_multi
);
4977 int still_running
= 0;
4978 CURLMcode rc
= CURLM_OK
;
4979 CURLM
* h
= CURLM_val(v_multi
);
4981 caml_enter_blocking_section();
4983 rc
= curl_multi_socket_all(h
, &still_running
);
4984 } while (rc
== CURLM_CALL_MULTI_PERFORM
);
4985 caml_leave_blocking_section();
4987 check_mcode("curl_multi_socket_all",rc
);
4989 CAMLreturn(Val_int(still_running
));
4992 static int curlm_sock_cb(CURL
*e
, curl_socket_t sock
, int what
, void *cbp
, void *sockp
)
4994 caml_leave_blocking_section();
4997 CAMLlocal2(v_what
,csock
);
4999 (void)sockp
; /* not used */
5001 /* v_what = Val_int(what); */
5004 case CURL_POLL_NONE
: v_what
= Val_int(0); break;
5005 case CURL_POLL_IN
: v_what
= Val_int(1); break;
5006 case CURL_POLL_OUT
: v_what
= Val_int(2); break;
5007 case CURL_POLL_INOUT
: v_what
= Val_int(3); break;
5008 case CURL_POLL_REMOVE
: v_what
= Val_int(4); break;
5010 fprintf(stderr
, "curlm_sock_cb sock=%lld what=%d\n", (long long)sock
, what
);
5012 raise_multi_error("curlm_sock_cb"); /* FIXME exception from callback */
5014 csock
=Val_socket(sock
);
5015 caml_callback2(Field(((ml_multi_handle
*)cbp
)->values
,curlmopt_socket_function
),
5019 caml_enter_blocking_section();
5023 value
caml_curl_multi_socketfunction(value v_multi
, value v_cb
)
5025 CAMLparam2(v_multi
, v_cb
);
5026 ml_multi_handle
* multi
= Multi_val(v_multi
);
5028 Store_field(multi
->values
, curlmopt_socket_function
, v_cb
);
5030 curl_multi_setopt(multi
->handle
, CURLMOPT_SOCKETFUNCTION
, curlm_sock_cb
);
5031 curl_multi_setopt(multi
->handle
, CURLMOPT_SOCKETDATA
, multi
);
5033 CAMLreturn(Val_unit
);
5036 static int curlm_timer_cb(CURLM
*multi
, long timeout_ms
, void *userp
)
5038 caml_leave_blocking_section();
5042 caml_callback(Field(((ml_multi_handle
*)userp
)->values
,curlmopt_timer_function
), Val_long(timeout_ms
));
5045 caml_enter_blocking_section();
5049 value
caml_curl_multi_timerfunction(value v_multi
, value v_cb
)
5051 CAMLparam2(v_multi
, v_cb
);
5052 ml_multi_handle
* multi
= Multi_val(v_multi
);
5054 Store_field(multi
->values
, curlmopt_timer_function
, v_cb
);
5056 curl_multi_setopt(multi
->handle
, CURLMOPT_TIMERFUNCTION
, curlm_timer_cb
);
5057 curl_multi_setopt(multi
->handle
, CURLMOPT_TIMERDATA
, multi
);
5059 CAMLreturn(Val_unit
);
5062 value
caml_curl_multi_timeout(value v_multi
)
5064 CAMLparam1(v_multi
);
5066 CURLMcode rc
= CURLM_OK
;
5067 ml_multi_handle
* multi
= Multi_val(v_multi
);
5069 rc
= curl_multi_timeout(multi
->handle
, &ms
);
5071 check_mcode("curl_multi_timeout",rc
);
5073 CAMLreturn(Val_long(ms
));
5076 #define SETMOPT_VAL_(func_name, curl_option, conv_val) \
5077 static void func_name(CURLM *handle, value option) \
5079 CAMLparam1(option); \
5080 CURLMcode result = CURLM_OK; \
5082 result = curl_multi_setopt(handle, curl_option, conv_val(option)); \
5084 check_mcode("curl_multi_setopt "#curl_option,result); \
5089 #define SETMOPT_VAL(name, conv) SETMOPT_VAL_(handle_multi_##name, CURLMOPT_##name, conv)
5090 #define SETMOPT_BOOL(name) SETMOPT_VAL(name, Bool_val)
5091 #define SETMOPT_LONG(name) SETMOPT_VAL(name, Long_val)
5092 #define SETMOPT_INT64(name) SETMOPT_VAL(name, Int64_val)
5094 long pipeliningMap
[] =
5096 0, /* CURLPIPE_NOTHING */
5097 1, /* CURLPIPE_HTTP1 */
5098 2, /* CURLPIPE_MULTIPLEX */
5101 static void handle_multi_PIPELINING(CURLM
* handle
, value option
)
5104 CURLMcode result
= CURLM_OK
;
5106 long bits
= convert_bit_list(pipeliningMap
, sizeof(pipeliningMap
) / sizeof(pipeliningMap
[0]), option
);
5108 result
= curl_multi_setopt(handle
, CURLMOPT_PIPELINING
, bits
);
5110 check_mcode("curl_multi_setopt CURLOPT_PIPELINING",result
);
5115 #if HAVE_DECL_CURLMOPT_MAXCONNECTS
5116 SETMOPT_LONG( MAXCONNECTS
)
5119 #if HAVE_DECL_CURLMOPT_MAX_PIPELINE_LENGTH
5120 SETMOPT_LONG( MAX_PIPELINE_LENGTH
)
5123 #if HAVE_DECL_CURLMOPT_MAX_HOST_CONNECTIONS
5124 SETMOPT_LONG( MAX_HOST_CONNECTIONS
)
5127 #if HAVE_DECL_CURLMOPT_MAX_TOTAL_CONNECTIONS
5128 SETMOPT_LONG( MAX_TOTAL_CONNECTIONS
)
5131 typedef struct CURLMOptionMapping CURLMOptionMapping
;
5132 #define OPT(name) { handle_multi_## name, "CURLMOPT_"#name}
5133 #define NO_OPT(name) { NULL, "CURLMOPT_"#name}
5135 struct CURLMOptionMapping
5137 void (*optionHandler
)(CURLM
*, value
);
5141 CURLMOptionMapping implementedMOptionMap
[] = {
5143 #if HAVE_DECL_CURLMOPT_MAXCONNECTS
5146 NO_OPT( MAXCONNECTS
),
5148 #if HAVE_DECL_CURLMOPT_MAX_PIPELINE_LENGTH
5149 OPT( MAX_PIPELINE_LENGTH
),
5151 NO_OPT( MAX_PIPELINE_LENGTH
),
5153 #if HAVE_DECL_CURLMOPT_MAX_HOST_CONNECTIONS
5154 OPT( MAX_HOST_CONNECTIONS
),
5156 NO_OPT( MAX_HOST_CONNECTIONS
),
5158 #if HAVE_DECL_CURLMOPT_MAX_TOTAL_CONNECTIONS
5159 OPT( MAX_TOTAL_CONNECTIONS
),
5161 NO_OPT( MAX_TOTAL_CONNECTIONS
),
5165 value
caml_curl_multi_setopt(value v_multi
, value option
)
5167 CAMLparam2(v_multi
, option
);
5169 CURLM
*handle
= Multi_val(v_multi
)->handle
;
5170 CURLMOptionMapping
* thisOption
= NULL
;
5171 static const value
* exception
= NULL
;
5173 data
= Field(option
, 0);
5175 if (Tag_val(option
) < sizeof(implementedMOptionMap
)/sizeof(CURLMOptionMapping
))
5177 thisOption
= &implementedMOptionMap
[Tag_val(option
)];
5178 if (thisOption
->optionHandler
)
5180 thisOption
->optionHandler(handle
, data
);
5184 if (NULL
== exception
)
5186 exception
= caml_named_value("Curl.NotImplemented");
5187 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented");
5190 caml_raise_with_string(*exception
, thisOption
->name
);
5195 raise_multi_error("Invalid CURLMOPT Option");
5198 CAMLreturn(Val_unit
);
5208 #define CURL_ENUM(name,last_used) { CURL_ ## name ## _ ## last_used, CURL_ ## name ## _LAST, #name }
5210 struct used_enum check_enums
[] = {
5211 { CURLINFO_SSL_DATA_OUT
, CURLINFO_END
, "DEBUGFUNCTION curl_infotype" },
5212 #if HAVE_DECL_CURL_HTTP_VERSION_3
5213 CURL_ENUM(HTTP_VERSION
, 3),
5215 { 38, CURLINFO_LASTONE
, "CURLINFO" },
5216 #if HAVE_DECL_CURLE_AGAIN
5217 { CURLE_AGAIN
, CURL_LAST
, "CURLcode" }
5221 value
caml_curl_check_enums(value v_unit
)
5226 size_t len
= sizeof(check_enums
) / sizeof(struct used_enum
);
5228 v_r
= caml_alloc_tuple(len
);
5230 for (i
= 0; i
< len
; i
++)
5232 v
= caml_alloc_tuple(3);
5233 Store_field(v
, 0, Val_int(check_enums
[i
].last_used
));
5234 Store_field(v
, 1, Val_int(check_enums
[i
].last
));
5235 Store_field(v
, 2, caml_copy_string(check_enums
[i
].name
));
5236 Store_field(v_r
, i
, v
);