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
); }
72 typedef struct Connection Connection
;
73 typedef struct ConnectionList ConnectionList
;
75 #define Connection_val(v) (*(Connection**)Data_custom_val(v))
77 typedef enum OcamlValues
82 Ocaml_PROGRESSFUNCTION
,
86 Ocaml_OPENSOCKETFUNCTION
,
87 Ocaml_SSH_KEYFUNCTION
,
92 /* Not used, last for size */
102 size_t refcount
; /* number of references to this structure */
104 char *curl_ERRORBUFFER
;
105 char *curl_POSTFIELDS
;
106 struct curl_slist
*curl_HTTPHEADER
;
107 struct curl_slist
*httpPostBuffers
;
108 struct curl_httppost
*httpPostFirst
;
109 struct curl_httppost
*httpPostLast
;
110 struct curl_slist
*curl_RESOLVE
;
111 struct curl_slist
*curl_QUOTE
;
112 struct curl_slist
*curl_POSTQUOTE
;
113 struct curl_slist
*curl_HTTP200ALIASES
;
114 struct curl_slist
*curl_MAIL_RCPT
;
115 struct curl_slist
*curl_CONNECT_TO
;
116 #if HAVE_DECL_CURLOPT_MIMEPOST
117 curl_mime
*curl_MIMEPOST
;
121 typedef struct CURLErrorMapping CURLErrorMapping
;
123 struct CURLErrorMapping
129 CURLErrorMapping errorMap
[] =
131 {"CURLE_OK", CURLE_OK
},
132 #if HAVE_DECL_CURLE_UNSUPPORTED_PROTOCOL
133 {"CURLE_UNSUPPORTED_PROTOCOL", CURLE_UNSUPPORTED_PROTOCOL
},
135 {"CURLE_UNSUPPORTED_PROTOCOL", -1},
137 #if HAVE_DECL_CURLE_FAILED_INIT
138 {"CURLE_FAILED_INIT", CURLE_FAILED_INIT
},
140 {"CURLE_FAILED_INIT", -1},
142 #if HAVE_DECL_CURLE_URL_MALFORMAT
143 {"CURLE_URL_MALFORMAT", CURLE_URL_MALFORMAT
},
145 {"CURLE_URL_MALFORMAT", -1},
147 #if HAVE_DECL_CURLE_URL_MALFORMAT_USER
148 {"CURLE_URL_MALFORMAT_USER", CURLE_URL_MALFORMAT_USER
},
150 {"CURLE_URL_MALFORMAT_USER", -1},
152 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_PROXY
153 {"CURLE_COULDNT_RESOLVE_PROXY", CURLE_COULDNT_RESOLVE_PROXY
},
155 {"CURLE_COULDNT_RESOLVE_PROXY", -1},
157 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_HOST
158 {"CURLE_COULDNT_RESOLVE_HOST", CURLE_COULDNT_RESOLVE_HOST
},
160 {"CURLE_COULDNT_RESOLVE_HOST", -1},
162 #if HAVE_DECL_CURLE_COULDNT_CONNECT
163 {"CURLE_COULDNT_CONNECT", CURLE_COULDNT_CONNECT
},
165 {"CURLE_COULDNT_CONNECT", -1},
167 #if HAVE_DECL_CURLE_FTP_WEIRD_SERVER_REPLY
168 {"CURLE_FTP_WEIRD_SERVER_REPLY", CURLE_FTP_WEIRD_SERVER_REPLY
},
170 {"CURLE_FTP_WEIRD_SERVER_REPLY", -1},
172 #if HAVE_DECL_CURLE_FTP_ACCESS_DENIED
173 {"CURLE_FTP_ACCESS_DENIED", CURLE_FTP_ACCESS_DENIED
},
175 {"CURLE_FTP_ACCESS_DENIED", -1},
177 #if HAVE_DECL_CURLE_FTP_USER_PASSWORD_INCORRECT
178 {"CURLE_FTP_USER_PASSWORD_INCORRECT", CURLE_FTP_USER_PASSWORD_INCORRECT
},
180 {"CURLE_FTP_USER_PASSWORD_INCORRECT", -1},
182 #if HAVE_DECL_CURLE_FTP_WEIRD_PASS_REPLY
183 {"CURLE_FTP_WEIRD_PASS_REPLY", CURLE_FTP_WEIRD_PASS_REPLY
},
185 {"CURLE_FTP_WEIRD_PASS_REPLY", -1},
187 #if HAVE_DECL_CURLE_FTP_WEIRD_USER_REPLY
188 {"CURLE_FTP_WEIRD_USER_REPLY", CURLE_FTP_WEIRD_USER_REPLY
},
190 {"CURLE_FTP_WEIRD_USER_REPLY", -1},
192 #if HAVE_DECL_CURLE_FTP_WEIRD_PASV_REPLY
193 {"CURLE_FTP_WEIRD_PASV_REPLY", CURLE_FTP_WEIRD_PASV_REPLY
},
195 {"CURLE_FTP_WEIRD_PASV_REPLY", -1},
197 #if HAVE_DECL_CURLE_FTP_WEIRD_227_FORMAT
198 {"CURLE_FTP_WEIRD_227_FORMAT", CURLE_FTP_WEIRD_227_FORMAT
},
200 {"CURLE_FTP_WEIRD_227_FORMAT", -1},
202 #if HAVE_DECL_CURLE_FTP_CANT_GET_HOST
203 {"CURLE_FTP_CANT_GET_HOST", CURLE_FTP_CANT_GET_HOST
},
205 {"CURLE_FTP_CANT_GET_HOST", -1},
207 #if HAVE_DECL_CURLE_FTP_CANT_RECONNECT
208 {"CURLE_FTP_CANT_RECONNECT", CURLE_FTP_CANT_RECONNECT
},
210 {"CURLE_FTP_CANT_RECONNECT", -1},
212 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_BINARY
213 {"CURLE_FTP_COULDNT_SET_BINARY", CURLE_FTP_COULDNT_SET_BINARY
},
215 {"CURLE_FTP_COULDNT_SET_BINARY", -1},
217 #if HAVE_DECL_CURLE_PARTIAL_FILE
218 {"CURLE_PARTIAL_FILE", CURLE_PARTIAL_FILE
},
220 {"CURLE_PARTIAL_FILE", -1},
222 #if HAVE_DECL_CURLE_FTP_COULDNT_RETR_FILE
223 {"CURLE_FTP_COULDNT_RETR_FILE", CURLE_FTP_COULDNT_RETR_FILE
},
225 {"CURLE_FTP_COULDNT_RETR_FILE", -1},
227 #if HAVE_DECL_CURLE_FTP_WRITE_ERROR
228 {"CURLE_FTP_WRITE_ERROR", CURLE_FTP_WRITE_ERROR
},
230 {"CURLE_FTP_WRITE_ERROR", -1},
232 #if HAVE_DECL_CURLE_FTP_QUOTE_ERROR
233 {"CURLE_FTP_QUOTE_ERROR", CURLE_FTP_QUOTE_ERROR
},
235 {"CURLE_FTP_QUOTE_ERROR", -1},
237 #if HAVE_DECL_CURLE_HTTP_RETURNED_ERROR
238 {"CURLE_HTTP_RETURNED_ERROR", CURLE_HTTP_RETURNED_ERROR
},
240 {"CURLE_HTTP_RETURNED_ERROR", -1},
242 #if HAVE_DECL_CURLE_WRITE_ERROR
243 {"CURLE_WRITE_ERROR", CURLE_WRITE_ERROR
},
245 {"CURLE_WRITE_ERROR", -1},
247 #if HAVE_DECL_CURLE_MALFORMAT_USER
248 {"CURLE_MALFORMAT_USER", CURLE_MALFORMAT_USER
},
250 {"CURLE_MALFORMAT_USER", -1},
252 #if HAVE_DECL_CURLE_FTP_COULDNT_STOR_FILE
253 {"CURLE_FTP_COULDNT_STOR_FILE", CURLE_FTP_COULDNT_STOR_FILE
},
255 {"CURLE_FTP_COULDNT_STOR_FILE", -1},
257 #if HAVE_DECL_CURLE_READ_ERROR
258 {"CURLE_READ_ERROR", CURLE_READ_ERROR
},
260 {"CURLE_READ_ERROR", -1},
262 #if HAVE_DECL_CURLE_OUT_OF_MEMORY
263 {"CURLE_OUT_OF_MEMORY", CURLE_OUT_OF_MEMORY
},
265 {"CURLE_OUT_OF_MEMORY", -1},
267 #if HAVE_DECL_CURLE_OPERATION_TIMEOUTED
268 {"CURLE_OPERATION_TIMEOUTED", CURLE_OPERATION_TIMEOUTED
},
270 {"CURLE_OPERATION_TIMEOUTED", -1},
272 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_ASCII
273 {"CURLE_FTP_COULDNT_SET_ASCII", CURLE_FTP_COULDNT_SET_ASCII
},
275 {"CURLE_FTP_COULDNT_SET_ASCII", -1},
277 #if HAVE_DECL_CURLE_FTP_PORT_FAILED
278 {"CURLE_FTP_PORT_FAILED", CURLE_FTP_PORT_FAILED
},
280 {"CURLE_FTP_PORT_FAILED", -1},
282 #if HAVE_DECL_CURLE_FTP_COULDNT_USE_REST
283 {"CURLE_FTP_COULDNT_USE_REST", CURLE_FTP_COULDNT_USE_REST
},
285 {"CURLE_FTP_COULDNT_USE_REST", -1},
287 #if HAVE_DECL_CURLE_FTP_COULDNT_GET_SIZE
288 {"CURLE_FTP_COULDNT_GET_SIZE", CURLE_FTP_COULDNT_GET_SIZE
},
290 {"CURLE_FTP_COULDNT_GET_SIZE", -1},
292 #if HAVE_DECL_CURLE_HTTP_RANGE_ERROR
293 {"CURLE_HTTP_RANGE_ERROR", CURLE_HTTP_RANGE_ERROR
},
295 {"CURLE_HTTP_RANGE_ERROR", -1},
297 #if HAVE_DECL_CURLE_HTTP_POST_ERROR
298 {"CURLE_HTTP_POST_ERROR", CURLE_HTTP_POST_ERROR
},
300 {"CURLE_HTTP_POST_ERROR", -1},
302 #if HAVE_DECL_CURLE_SSL_CONNECT_ERROR
303 {"CURLE_SSL_CONNECT_ERROR", CURLE_SSL_CONNECT_ERROR
},
305 {"CURLE_SSL_CONNECT_ERROR", -1},
307 #if HAVE_DECL_CURLE_BAD_DOWNLOAD_RESUME
308 {"CURLE_BAD_DOWNLOAD_RESUME", CURLE_BAD_DOWNLOAD_RESUME
},
310 {"CURLE_BAD_DOWNLOAD_RESUME", -1},
312 #if HAVE_DECL_CURLE_FILE_COULDNT_READ_FILE
313 {"CURLE_FILE_COULDNT_READ_FILE", CURLE_FILE_COULDNT_READ_FILE
},
315 {"CURLE_FILE_COULDNT_READ_FILE", -1},
317 #if HAVE_DECL_CURLE_LDAP_CANNOT_BIND
318 {"CURLE_LDAP_CANNOT_BIND", CURLE_LDAP_CANNOT_BIND
},
320 {"CURLE_LDAP_CANNOT_BIND", -1},
322 #if HAVE_DECL_CURLE_LDAP_SEARCH_FAILED
323 {"CURLE_LDAP_SEARCH_FAILED", CURLE_LDAP_SEARCH_FAILED
},
325 {"CURLE_LDAP_SEARCH_FAILED", -1},
327 #if HAVE_DECL_CURLE_LIBRARY_NOT_FOUND
328 {"CURLE_LIBRARY_NOT_FOUND", CURLE_LIBRARY_NOT_FOUND
},
330 {"CURLE_LIBRARY_NOT_FOUND", -1},
332 #if HAVE_DECL_CURLE_FUNCTION_NOT_FOUND
333 {"CURLE_FUNCTION_NOT_FOUND", CURLE_FUNCTION_NOT_FOUND
},
335 {"CURLE_FUNCTION_NOT_FOUND", -1},
337 #if HAVE_DECL_CURLE_ABORTED_BY_CALLBACK
338 {"CURLE_ABORTED_BY_CALLBACK", CURLE_ABORTED_BY_CALLBACK
},
340 {"CURLE_ABORTED_BY_CALLBACK", -1},
342 #if HAVE_DECL_CURLE_BAD_FUNCTION_ARGUMENT
343 {"CURLE_BAD_FUNCTION_ARGUMENT", CURLE_BAD_FUNCTION_ARGUMENT
},
345 {"CURLE_BAD_FUNCTION_ARGUMENT", -1},
347 #if HAVE_DECL_CURLE_BAD_CALLING_ORDER
348 {"CURLE_BAD_CALLING_ORDER", CURLE_BAD_CALLING_ORDER
},
350 {"CURLE_BAD_CALLING_ORDER", -1},
352 #if HAVE_DECL_CURLE_INTERFACE_FAILED
353 {"CURLE_INTERFACE_FAILED", CURLE_INTERFACE_FAILED
},
355 {"CURLE_INTERFACE_FAILED", -1},
357 #if HAVE_DECL_CURLE_BAD_PASSWORD_ENTERED
358 {"CURLE_BAD_PASSWORD_ENTERED", CURLE_BAD_PASSWORD_ENTERED
},
360 {"CURLE_BAD_PASSWORD_ENTERED", -1},
362 #if HAVE_DECL_CURLE_TOO_MANY_REDIRECTS
363 {"CURLE_TOO_MANY_REDIRECTS", CURLE_TOO_MANY_REDIRECTS
},
365 {"CURLE_TOO_MANY_REDIRECTS", -1},
367 #if HAVE_DECL_CURLE_UNKNOWN_TELNET_OPTION
368 {"CURLE_UNKNOWN_TELNET_OPTION", CURLE_UNKNOWN_TELNET_OPTION
},
370 {"CURLE_UNKNOWN_TELNET_OPTION", -1},
372 #if HAVE_DECL_CURLE_TELNET_OPTION_SYNTAX
373 {"CURLE_TELNET_OPTION_SYNTAX", CURLE_TELNET_OPTION_SYNTAX
},
375 {"CURLE_TELNET_OPTION_SYNTAX", -1},
377 #if HAVE_DECL_CURLE_SSL_PEER_CERTIFICATE
378 {"CURLE_SSL_PEER_CERTIFICATE", CURLE_SSL_PEER_CERTIFICATE
},
380 {"CURLE_SSL_PEER_CERTIFICATE", -1},
382 #if HAVE_DECL_CURLE_GOT_NOTHING
383 {"CURLE_GOT_NOTHING", CURLE_GOT_NOTHING
},
385 {"CURLE_GOT_NOTHING", -1},
387 #if HAVE_DECL_CURLE_SSL_ENGINE_NOTFOUND
388 {"CURLE_SSL_ENGINE_NOTFOUND", CURLE_SSL_ENGINE_NOTFOUND
},
390 {"CURLE_SSL_ENGINE_NOTFOUND", -1},
392 #if HAVE_DECL_CURLE_SSL_ENGINE_SETFAILED
393 {"CURLE_SSL_ENGINE_SETFAILED", CURLE_SSL_ENGINE_SETFAILED
},
395 {"CURLE_SSL_ENGINE_SETFAILED", -1},
397 #if HAVE_DECL_CURLE_SEND_ERROR
398 {"CURLE_SEND_ERROR", CURLE_SEND_ERROR
},
400 {"CURLE_SEND_ERROR", -1},
402 #if HAVE_DECL_CURLE_RECV_ERROR
403 {"CURLE_RECV_ERROR", CURLE_RECV_ERROR
},
405 {"CURLE_RECV_ERROR", -1},
407 #if HAVE_DECL_CURLE_SHARE_IN_USE
408 {"CURLE_SHARE_IN_USE", CURLE_SHARE_IN_USE
},
410 {"CURLE_SHARE_IN_USE", -1},
412 #if HAVE_DECL_CURLE_SSL_CERTPROBLEM
413 {"CURLE_SSL_CERTPROBLEM", CURLE_SSL_CERTPROBLEM
},
415 {"CURLE_SSL_CERTPROBLEM", -1},
417 #if HAVE_DECL_CURLE_SSL_CIPHER
418 {"CURLE_SSL_CIPHER", CURLE_SSL_CIPHER
},
420 {"CURLE_SSL_CIPHER", -1},
422 #if HAVE_DECL_CURLE_SSL_CACERT
423 {"CURLE_SSL_CACERT", CURLE_SSL_CACERT
},
425 {"CURLE_SSL_CACERT", -1},
427 #if HAVE_DECL_CURLE_BAD_CONTENT_ENCODING
428 {"CURLE_BAD_CONTENT_ENCODING", CURLE_BAD_CONTENT_ENCODING
},
430 {"CURLE_BAD_CONTENT_ENCODING", -1},
432 #if HAVE_DECL_CURLE_LDAP_INVALID_URL
433 {"CURLE_LDAP_INVALID_URL", CURLE_LDAP_INVALID_URL
},
435 {"CURLE_LDAP_INVALID_URL", -1},
437 #if HAVE_DECL_CURLE_FILESIZE_EXCEEDED
438 {"CURLE_FILESIZE_EXCEEDED", CURLE_FILESIZE_EXCEEDED
},
440 {"CURLE_FILESIZE_EXCEEDED", -1},
442 #if HAVE_DECL_CURLE_FTP_SSL_FAILED
443 {"CURLE_FTP_SSL_FAILED", CURLE_FTP_SSL_FAILED
},
445 {"CURLE_FTP_SSL_FAILED", -1},
447 #if HAVE_DECL_CURLE_SEND_FAIL_REWIND
448 {"CURLE_SEND_FAIL_REWIND", CURLE_SEND_FAIL_REWIND
},
450 {"CURLE_SEND_FAIL_REWIND", -1},
452 #if HAVE_DECL_CURLE_SSL_ENGINE_INITFAILED
453 {"CURLE_SSL_ENGINE_INITFAILED", CURLE_SSL_ENGINE_INITFAILED
},
455 {"CURLE_SSL_ENGINE_INITFAILED", -1},
457 #if HAVE_DECL_CURLE_LOGIN_DENIED
458 {"CURLE_LOGIN_DENIED", CURLE_LOGIN_DENIED
},
460 {"CURLE_LOGIN_DENIED", -1},
462 #if HAVE_DECL_CURLE_TFTP_NOTFOUND
463 {"CURLE_TFTP_NOTFOUND", CURLE_TFTP_NOTFOUND
},
465 {"CURLE_TFTP_NOTFOUND", -1},
467 #if HAVE_DECL_CURLE_TFTP_PERM
468 {"CURLE_TFTP_PERM", CURLE_TFTP_PERM
},
470 {"CURLE_TFTP_PERM", -1},
472 #if HAVE_DECL_CURLE_REMOTE_DISK_FULL
473 {"CURLE_REMOTE_DISK_FULL", CURLE_REMOTE_DISK_FULL
},
475 {"CURLE_REMOTE_DISK_FULL", -1},
477 #if HAVE_DECL_CURLE_TFTP_ILLEGAL
478 {"CURLE_TFTP_ILLEGAL", CURLE_TFTP_ILLEGAL
},
480 {"CURLE_TFTP_ILLEGAL", -1},
482 #if HAVE_DECL_CURLE_TFTP_UNKNOWNID
483 {"CURLE_TFTP_UNKNOWNID", CURLE_TFTP_UNKNOWNID
},
485 {"CURLE_TFTP_UNKNOWNID", -1},
487 #if HAVE_DECL_CURLE_REMOTE_FILE_EXISTS
488 {"CURLE_REMOTE_FILE_EXISTS", CURLE_REMOTE_FILE_EXISTS
},
490 {"CURLE_REMOTE_FILE_EXISTS", -1},
492 #if HAVE_DECL_CURLE_TFTP_NOSUCHUSER
493 {"CURLE_TFTP_NOSUCHUSER", CURLE_TFTP_NOSUCHUSER
},
495 {"CURLE_TFTP_NOSUCHUSER", -1},
497 #if HAVE_DECL_CURLE_CONV_FAILED
498 {"CURLE_CONV_FAILED", CURLE_CONV_FAILED
},
500 {"CURLE_CONV_FAILED", -1},
502 #if HAVE_DECL_CURLE_CONV_REQD
503 {"CURLE_CONV_REQD", CURLE_CONV_REQD
},
505 {"CURLE_CONV_REQD", -1},
507 #if HAVE_DECL_CURLE_SSL_CACERT_BADFILE
508 {"CURLE_SSL_CACERT_BADFILE", CURLE_SSL_CACERT_BADFILE
},
510 {"CURLE_SSL_CACERT_BADFILE", -1},
512 #if HAVE_DECL_CURLE_REMOTE_FILE_NOT_FOUND
513 {"CURLE_REMOTE_FILE_NOT_FOUND", CURLE_REMOTE_FILE_NOT_FOUND
},
515 {"CURLE_REMOTE_FILE_NOT_FOUND", -1},
517 #if HAVE_DECL_CURLE_SSH
518 {"CURLE_SSH", CURLE_SSH
},
522 #if HAVE_DECL_CURLE_SSL_SHUTDOWN_FAILED
523 {"CURLE_SSL_SHUTDOWN_FAILED", CURLE_SSL_SHUTDOWN_FAILED
},
525 {"CURLE_SSL_SHUTDOWN_FAILED", -1},
527 #if HAVE_DECL_CURLE_AGAIN
528 {"CURLE_AGAIN", CURLE_AGAIN
},
535 typedef struct CURLOptionMapping CURLOptionMapping
;
537 struct CURLOptionMapping
539 void (*optionHandler
)(Connection
*, value
);
543 static char* strdup_ml(value v
)
546 p
= (char*)malloc(caml_string_length(v
)+1);
547 memcpy(p
,String_val(v
),caml_string_length(v
)+1); // caml strings have terminating zero
551 static value
ml_copy_string(char const* p
, size_t size
)
553 value v
= caml_alloc_string(size
);
554 memcpy(&Byte(v
,0),p
,size
);
558 /* prepends to the beginning of list */
559 static struct curl_slist
* curl_slist_prepend_ml(struct curl_slist
* list
, value v
)
561 /* FIXME check NULLs */
562 struct curl_slist
* new_item
= (struct curl_slist
*)malloc(sizeof(struct curl_slist
));
564 new_item
->next
= list
;
565 new_item
->data
= strdup_ml(v
);
570 static void free_curl_slist(struct curl_slist
*slist
)
575 curl_slist_free_all(slist
);
578 static void raiseError(Connection
*conn
, CURLcode code
)
581 CAMLlocal1(exceptionData
);
582 const value
*exception
;
583 char *errorString
= "Unknown Error";
586 for (i
= 0; errorMap
[i
].name
!= NULL
; i
++)
588 if (errorMap
[i
].error
== code
)
590 errorString
= errorMap
[i
].name
;
595 exceptionData
= caml_alloc_tuple(3);
597 Store_field(exceptionData
, 0, Val_int(code
));
598 Store_field(exceptionData
, 1, Val_int(code
));
599 Store_field(exceptionData
, 2, caml_copy_string(errorString
));
601 if (conn
!= NULL
&& conn
->curl_ERRORBUFFER
!= NULL
)
603 Store_field(Field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
), 0, caml_copy_string(conn
->curl_ERRORBUFFER
));
606 exception
= caml_named_value("CurlException");
608 if (exception
== NULL
)
609 caml_failwith("CurlException not registered");
611 caml_raise_with_arg(*exception
, exceptionData
);
616 static void resetOcamlValues(Connection
* connection
)
620 for (i
= 0; i
< OcamlValuesSize
; i
++)
621 Store_field(connection
->ocamlValues
, i
, Val_unit
);
624 static Connection
* allocConnection(CURL
* h
)
626 Connection
* connection
= (Connection
*)malloc(sizeof(Connection
));
628 connection
->ocamlValues
= caml_alloc(OcamlValuesSize
, 0);
629 resetOcamlValues(connection
);
630 caml_register_global_root(&connection
->ocamlValues
);
632 connection
->handle
= h
;
633 curl_easy_setopt(h
, CURLOPT_PRIVATE
, connection
);
635 connection
->refcount
= 0;
637 connection
->curl_ERRORBUFFER
= NULL
;
638 connection
->curl_POSTFIELDS
= NULL
;
639 connection
->curl_HTTPHEADER
= NULL
;
640 connection
->httpPostBuffers
= NULL
;
641 connection
->httpPostFirst
= NULL
;
642 connection
->httpPostLast
= NULL
;
643 connection
->curl_QUOTE
= NULL
;
644 connection
->curl_POSTQUOTE
= NULL
;
645 connection
->curl_HTTP200ALIASES
= NULL
;
646 connection
->curl_RESOLVE
= NULL
;
647 connection
->curl_MAIL_RCPT
= NULL
;
648 connection
->curl_CONNECT_TO
= NULL
;
649 #if HAVE_DECL_CURLOPT_MIMEPOST
650 connection
->curl_MIMEPOST
= NULL
;
656 static Connection
*newConnection(void)
660 caml_enter_blocking_section();
661 h
= curl_easy_init();
662 caml_leave_blocking_section();
664 return allocConnection(h
);
667 static void free_if(void* p
) { if (NULL
!= p
) free(p
); }
669 static void removeConnection(Connection
*connection
, int finalization
)
671 const char* fin_url
= NULL
;
673 if (!connection
->handle
)
675 return; /* already cleaned up */
680 /* cannot engage OCaml runtime at finalization, just report leak */
681 if (CURLE_OK
!= curl_easy_getinfo(connection
->handle
, CURLINFO_EFFECTIVE_URL
, &fin_url
) || NULL
== fin_url
)
685 fprintf(stderr
,"Curl: handle %p leaked, conn %p, url %s\n", connection
->handle
, connection
, fin_url
);
690 caml_enter_blocking_section();
691 curl_easy_cleanup(connection
->handle
);
692 caml_leave_blocking_section();
695 connection
->handle
= NULL
;
697 caml_remove_global_root(&connection
->ocamlValues
);
699 free_if(connection
->curl_ERRORBUFFER
);
700 free_if(connection
->curl_POSTFIELDS
);
701 free_curl_slist(connection
->curl_HTTPHEADER
);
702 free_curl_slist(connection
->httpPostBuffers
);
703 if (connection
->httpPostFirst
!= NULL
)
704 curl_formfree(connection
->httpPostFirst
);
705 free_curl_slist(connection
->curl_RESOLVE
);
706 free_curl_slist(connection
->curl_QUOTE
);
707 free_curl_slist(connection
->curl_POSTQUOTE
);
708 free_curl_slist(connection
->curl_HTTP200ALIASES
);
709 free_curl_slist(connection
->curl_MAIL_RCPT
);
710 free_curl_slist(connection
->curl_CONNECT_TO
);
711 #if HAVE_DECL_CURLOPT_MIMEPOST
712 curl_mime_free(connection
->curl_MIMEPOST
);
716 static Connection
* getConnection(CURL
* h
)
718 Connection
* p
= NULL
;
720 if (CURLE_OK
!= curl_easy_getinfo(h
, CURLINFO_PRIVATE
, &p
) || NULL
== p
)
722 caml_failwith("Unknown handle");
729 static void checkConnection(Connection
* connection
)
734 static void checkConnection(Connection
*connection
)
736 if (connection
!= getConnection(connection
->handle
))
738 caml_failwith("Invalid Connection");
743 void op_curl_easy_finalize(value v
)
745 Connection
* conn
= Connection_val(v
);
746 /* same connection may be referenced by several different
747 OCaml values, see e.g. caml_curl_multi_remove_finished */
749 if (0 == conn
->refcount
)
751 removeConnection(conn
, 1);
756 int op_curl_easy_compare(value v1
, value v2
)
758 size_t p1
= (size_t)Connection_val(v1
);
759 size_t p2
= (size_t)Connection_val(v2
);
760 return (p1
== p2
? 0 : (p1
> p2
? 1 : -1)); /* compare addresses */
763 intnat
op_curl_easy_hash(value v
)
765 return (size_t)Connection_val(v
); /* address */
768 static struct custom_operations curl_easy_ops
= {
770 op_curl_easy_finalize
,
771 op_curl_easy_compare
,
773 custom_serialize_default
,
774 custom_deserialize_default
,
775 #if defined(custom_compare_ext_default)
776 custom_compare_ext_default
,
780 value
caml_curl_alloc(Connection
* conn
)
782 value v
= caml_alloc_custom(&curl_easy_ops
, sizeof(Connection
*), 0, 1);
783 Connection_val(v
) = conn
;
788 static size_t cb_WRITEFUNCTION(char *ptr
, size_t size
, size_t nmemb
, void *data
)
790 caml_leave_blocking_section();
793 CAMLlocal2(result
, str
);
794 Connection
*conn
= (Connection
*)data
;
796 checkConnection(conn
);
798 str
= ml_copy_string(ptr
,size
*nmemb
);
800 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_WRITEFUNCTION
), str
);
802 size_t r
= Is_exception_result(result
) ? 0 : Int_val(result
);
805 caml_enter_blocking_section();
809 static size_t cb_READFUNCTION(void *ptr
, size_t size
, size_t nmemb
, void *data
)
811 caml_leave_blocking_section();
815 Connection
*conn
= (Connection
*)data
;
818 checkConnection(conn
);
820 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_READFUNCTION
),
821 Val_int(size
*nmemb
));
823 if (Is_exception_result(result
))
825 CAMLreturnT(size_t,CURL_READFUNC_ABORT
);
828 length
= caml_string_length(result
);
832 if (length
<= size
*nmemb
)
834 memcpy(ptr
, String_val(result
), length
);
839 r
= CURL_READFUNC_ABORT
;
843 caml_enter_blocking_section();
847 static size_t cb_HEADERFUNCTION(char *ptr
, size_t size
, size_t nmemb
, void *data
)
849 caml_leave_blocking_section();
852 CAMLlocal2(result
,str
);
853 Connection
*conn
= (Connection
*)data
;
855 checkConnection(conn
);
857 str
= ml_copy_string(ptr
,size
*nmemb
);
859 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_HEADERFUNCTION
), str
);
861 size_t r
= Is_exception_result(result
) ? 0 : Int_val(result
);
864 caml_enter_blocking_section();
868 static int cb_PROGRESSFUNCTION(void *data
,
874 caml_leave_blocking_section();
878 CAMLlocalN(callbackData
, 4);
879 Connection
*conn
= (Connection
*)data
;
881 checkConnection(conn
);
883 callbackData
[0] = caml_copy_double(dlTotal
);
884 callbackData
[1] = caml_copy_double(dlNow
);
885 callbackData
[2] = caml_copy_double(ulTotal
);
886 callbackData
[3] = caml_copy_double(ulNow
);
888 result
= caml_callbackN_exn(Field(conn
->ocamlValues
, Ocaml_PROGRESSFUNCTION
),
891 int r
= Is_exception_result(result
) ? 1 : Bool_val(result
);
894 caml_enter_blocking_section();
898 static int cb_DEBUGFUNCTION(CURL
*debugConnection
,
899 curl_infotype infoType
,
904 caml_leave_blocking_section();
907 CAMLlocal3(camlDebugConnection
, camlInfoType
, camlMessage
);
908 Connection
*conn
= (Connection
*)data
;
909 (void)debugConnection
; /* not used */
911 checkConnection(conn
);
913 camlDebugConnection
= caml_curl_alloc(conn
);
914 camlMessage
= ml_copy_string(buffer
,bufferLength
);
915 camlInfoType
= Val_long(infoType
<= CURLINFO_SSL_DATA_OUT
? infoType
: CURLINFO_END
);
917 caml_callback3_exn(Field(conn
->ocamlValues
, Ocaml_DEBUGFUNCTION
),
924 caml_enter_blocking_section();
928 static curlioerr
cb_IOCTLFUNCTION(CURL
*ioctl
,
932 caml_leave_blocking_section();
935 CAMLlocal3(camlResult
, camlConnection
, camlCmd
);
936 Connection
*conn
= (Connection
*)data
;
937 curlioerr result
= CURLIOE_OK
;
938 (void)ioctl
; /* not used */
940 checkConnection(conn
);
942 if (cmd
== CURLIOCMD_NOP
)
943 camlCmd
= Val_long(0);
944 else if (cmd
== CURLIOCMD_RESTARTREAD
)
945 camlCmd
= Val_long(1);
947 caml_failwith("Invalid IOCTL Cmd!");
949 camlConnection
= caml_curl_alloc(conn
);
951 camlResult
= caml_callback2_exn(Field(conn
->ocamlValues
, Ocaml_IOCTLFUNCTION
),
955 if (Is_exception_result(camlResult
))
957 result
= CURLIOE_FAILRESTART
;
960 switch (Long_val(camlResult
))
962 case 0: /* CURLIOE_OK */
966 case 1: /* CURLIOE_UNKNOWNCMD */
967 result
= CURLIOE_UNKNOWNCMD
;
970 case 2: /* CURLIOE_FAILRESTART */
971 result
= CURLIOE_FAILRESTART
;
974 default: /* Incorrect return value, but let's handle it */
975 result
= CURLIOE_FAILRESTART
;
980 caml_enter_blocking_section();
984 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
985 static int cb_SEEKFUNCTION(void *data
,
989 caml_leave_blocking_section();
992 CAMLlocal3(camlResult
, camlOffset
, camlOrigin
);
993 Connection
*conn
= (Connection
*)data
;
995 camlOffset
= caml_copy_int64(offset
);
997 if (origin
== SEEK_SET
)
998 camlOrigin
= Val_long(0);
999 else if (origin
== SEEK_CUR
)
1000 camlOrigin
= Val_long(1);
1001 else if (origin
== SEEK_END
)
1002 camlOrigin
= Val_long(2);
1004 caml_failwith("Invalid seek code");
1006 camlResult
= caml_callback2_exn(Field(conn
->ocamlValues
,
1007 Ocaml_SEEKFUNCTION
),
1012 if (Is_exception_result(camlResult
))
1013 result
= CURL_SEEKFUNC_FAIL
;
1015 switch (Int_val(camlResult
))
1017 case 0: result
= CURL_SEEKFUNC_OK
; break;
1018 case 1: result
= CURL_SEEKFUNC_FAIL
; break;
1019 case 2: result
= CURL_SEEKFUNC_CANTSEEK
; break;
1020 default: caml_failwith("Invalid seek result");
1024 caml_enter_blocking_section();
1029 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
1030 static int cb_OPENSOCKETFUNCTION(void *data
,
1031 curlsocktype purpose
,
1032 struct curl_sockaddr
*addr
)
1034 caml_leave_blocking_section();
1038 Connection
*conn
= (Connection
*)data
;
1040 (void)purpose
; /* not used */
1042 sock
= socket(addr
->family
, addr
->socktype
, addr
->protocol
);
1047 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_OPENSOCKETFUNCTION
), Val_int(sock
));
1048 if (Is_exception_result(result
))
1056 caml_enter_blocking_section();
1057 return ((sock
== -1) ? CURL_SOCKET_BAD
: sock
);
1061 static int cb_SSH_KEYFUNCTION(CURL
*easy
,
1062 const struct curl_khkey
*knownkey
,
1063 const struct curl_khkey
*foundkey
,
1064 enum curl_khmatch match
,
1067 caml_leave_blocking_section();
1070 CAMLlocal3(v_found
, v_match
, v_result
);
1071 Connection
*conn
= (Connection
*)clientp
;
1072 int res
= CURLKHSTAT_REJECT
;
1075 case CURLKHMATCH_OK
:
1076 v_match
= Val_int(0);
1078 case CURLKHMATCH_MISMATCH
:
1079 v_match
= caml_alloc_small(1, 0);
1080 Field(v_match
, 0) = ml_copy_string(knownkey
->key
, knownkey
->len
? knownkey
->len
: strlen(knownkey
->key
));
1082 case CURLKHMATCH_MISSING
:
1083 v_match
= Val_int(1);
1086 caml_failwith("Invalid CURL_SSH_KEYFUNCTION argument");
1090 v_found
= ml_copy_string(foundkey
->key
, foundkey
->len
? foundkey
->len
: strlen(foundkey
->key
));
1091 v_result
= caml_callback2_exn(Field(conn
->ocamlValues
, Ocaml_SSH_KEYFUNCTION
), v_match
, v_found
);
1093 if (!Is_exception_result(v_result
)) {
1094 switch (Int_val(v_result
)) {
1096 res
= CURLKHSTAT_FINE_ADD_TO_FILE
;
1099 res
= CURLKHSTAT_FINE
;
1102 res
= CURLKHSTAT_REJECT
;
1105 res
= CURLKHSTAT_DEFER
;
1108 caml_failwith("Invalid CURLOPT_SSH_KEYFUNCTION return value");
1115 caml_enter_blocking_section();
1120 ** curl_global_init helper function
1123 value
caml_curl_global_init(value initOption
)
1125 CAMLparam1(initOption
);
1127 switch (Long_val(initOption
))
1129 case 0: /* CURLINIT_GLOBALALL */
1130 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_ALL
)));
1133 case 1: /* CURLINIT_GLOBALSSL */
1134 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_SSL
)));
1137 case 2: /* CURLINIT_GLOBALWIN32 */
1138 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_WIN32
)));
1141 case 3: /* CURLINIT_GLOBALNOTHING */
1142 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_NOTHING
)));
1146 caml_failwith("Invalid Initialization Option");
1150 /* Keep compiler happy, we should never get here due to caml_failwith() */
1151 CAMLreturn(Val_unit
);
1155 ** curl_global_cleanup helper function
1158 value
caml_curl_global_cleanup(void)
1162 curl_global_cleanup();
1164 CAMLreturn(Val_unit
);
1168 ** curl_easy_init helper function
1170 value
caml_curl_easy_init(void)
1175 result
= caml_curl_alloc(newConnection());
1180 value
caml_curl_easy_reset(value conn
)
1183 Connection
*connection
= Connection_val(conn
);
1185 checkConnection(connection
);
1186 curl_easy_reset(connection
->handle
);
1187 curl_easy_setopt(connection
->handle
, CURLOPT_PRIVATE
, connection
);
1188 resetOcamlValues(connection
);
1190 CAMLreturn(Val_unit
);
1194 ** curl_easy_setopt helper utility functions
1197 #define SETOPT_FUNCTION(name) \
1198 static void handle_##name##FUNCTION(Connection *conn, value option) \
1200 CAMLparam1(option); \
1201 CURLcode result = CURLE_OK; \
1202 Store_field(conn->ocamlValues, Ocaml_##name##FUNCTION, option); \
1203 result = curl_easy_setopt(conn->handle, CURLOPT_##name##FUNCTION, cb_##name##FUNCTION); \
1204 if (result != CURLE_OK) raiseError(conn, result); \
1205 result = curl_easy_setopt(conn->handle, CURLOPT_##name##DATA, conn); \
1206 if (result != CURLE_OK) raiseError(conn, result); \
1210 SETOPT_FUNCTION( WRITE
)
1211 SETOPT_FUNCTION( READ
)
1212 SETOPT_FUNCTION( HEADER
)
1213 SETOPT_FUNCTION( PROGRESS
)
1214 SETOPT_FUNCTION( DEBUG
)
1215 SETOPT_FUNCTION( SSH_KEY
)
1217 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1218 SETOPT_FUNCTION( SEEK
)
1221 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
1222 SETOPT_FUNCTION( IOCTL
)
1225 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
1226 SETOPT_FUNCTION( OPENSOCKET
)
1229 static void handle_slist(Connection
*conn
, struct curl_slist
** slist
, CURLoption curl_option
, value option
)
1232 CURLcode result
= CURLE_OK
;
1234 free_curl_slist(*slist
);
1237 while (Val_emptylist
!= option
)
1239 *slist
= curl_slist_append(*slist
, String_val(Field(option
, 0)));
1241 option
= Field(option
, 1);
1244 result
= curl_easy_setopt(conn
->handle
, curl_option
, *slist
);
1246 if (result
!= CURLE_OK
)
1247 raiseError(conn
, result
);
1252 static long convert_bit_list(long *map
, size_t map_size
, value option
)
1258 while (Val_emptylist
!= option
)
1260 index
= Int_val(Field(option
, 0));
1261 if ((index
< 0) || ((size_t)index
>= map_size
))
1262 caml_invalid_argument("convert_bit_list");
1266 option
= Field(option
, 1);
1269 CAMLreturnT(long, bits
);
1272 #define SETOPT_STRING(name) \
1273 static void handle_##name(Connection *conn, value option) \
1275 CAMLparam1(option); \
1276 CURLcode result = CURLE_OK; \
1278 result = curl_easy_setopt(conn->handle, CURLOPT_##name, String_val(option)); \
1280 if (result != CURLE_OK) \
1281 raiseError(conn, result); \
1286 #define SETOPT_VAL_(func_name, curl_option, conv_val) \
1287 static void func_name(Connection *conn, value option) \
1289 CAMLparam1(option); \
1290 CURLcode result = CURLE_OK; \
1292 result = curl_easy_setopt(conn->handle, curl_option, conv_val(option)); \
1294 if (result != CURLE_OK) \
1295 raiseError(conn, result); \
1300 #define SETOPT_VAL(name, conv) SETOPT_VAL_(handle_##name, CURLOPT_##name, conv)
1301 #define SETOPT_BOOL(name) SETOPT_VAL(name, Bool_val)
1302 #define SETOPT_LONG(name) SETOPT_VAL(name, Long_val)
1303 #define SETOPT_INT64(name) SETOPT_VAL(name, Int64_val)
1305 #if HAVE_DECL_CURLOPT_MIMEPOST
1307 static void handle_part_with_name(Connection
* conn
, curl_mimepart
* part
, value v_part_name_info
)
1309 value v_data
= Field(v_part_name_info
, 0);
1310 value v_name
= Field(v_part_name_info
, 1);
1311 value v_filename
= Field(v_part_name_info
, 2);
1314 value v_str
= Field(v_data
, 0);
1316 switch (Tag_val(v_data
)) {
1318 result
= curl_mime_data(part
, String_val(v_str
), caml_string_length(v_str
));
1321 result
= curl_mime_filedata(part
, String_val(v_str
));
1324 caml_failwith("Invalid MIMEPOST data value");
1328 if (result
!= CURLE_OK
) {
1329 raiseError(conn
, result
);
1332 if (Is_block(v_name
)) {
1333 result
= curl_mime_name(part
, String_val(Field(v_name
, 0)));
1335 if (result
!= CURLE_OK
) {
1336 raiseError(conn
, result
);
1340 if (Is_block(v_filename
)) {
1341 result
= curl_mime_filename(part
, String_val(Field(v_filename
, 0)));
1343 if (result
!= CURLE_OK
) {
1344 raiseError(conn
, result
);
1349 static void new_part(Connection
* conn
, curl_mime
* mime
, value v_part
)
1351 value v_encoding
= Field(v_part
, 0);
1352 value v_headers
= Field(v_part
, 1);
1353 value v_subparts
= Field(v_part
, 2);
1354 value v_data
= Field(v_part
, 3);
1355 value v_str
= Field(v_data
, 0);
1357 struct curl_slist
*headers
= NULL
;
1360 curl_mimepart
*part
= curl_mime_addpart(mime
);
1362 switch (Int_val(v_encoding
)) {
1364 result
= curl_mime_encoder(part
, "8bit");
1367 result
= curl_mime_encoder(part
, "binary");
1370 result
= curl_mime_encoder(part
, "7bit");
1373 result
= curl_mime_encoder(part
, "quoted-printable");
1376 result
= curl_mime_encoder(part
, "base64");
1382 caml_failwith("Invalid MIMEPOST encoding value");
1386 if (result
!= CURLE_OK
) {
1387 raiseError(conn
, result
);
1390 while (v_headers
!= Val_emptylist
) {
1391 headers
= curl_slist_append(headers
, String_val(Field(v_headers
, 0)));
1392 v_headers
= Field(v_headers
, 1);
1395 result
= curl_mime_headers(part
, headers
, 1);
1397 if (result
!= CURLE_OK
) {
1398 raiseError(conn
, result
);
1401 switch (Tag_val(v_data
)) {
1403 result
= curl_mime_data(part
, String_val(v_str
), caml_string_length(v_str
));
1406 result
= curl_mime_filedata(part
, String_val(v_str
));
1409 handle_part_with_name(conn
, part
, v_data
);
1412 caml_failwith("Invalid MIMEPOST data value");
1416 if (result
!= CURLE_OK
) {
1417 raiseError(conn
, result
);
1420 if (v_subparts
!= Val_emptylist
) {
1421 curl_mime
*mime
= curl_mime_init(conn
->handle
);
1423 while (v_subparts
!= Val_emptylist
) {
1424 new_part(conn
, mime
, Field(v_subparts
, 0));
1425 v_subparts
= Field(v_subparts
, 1);
1428 result
= curl_mime_subparts(part
, mime
);
1430 if (result
!= CURLE_OK
) {
1431 raiseError(conn
, result
);
1436 static void handle_MIMEPOST(Connection
* conn
, value v_subparts
)
1438 CAMLparam1(v_subparts
);
1439 curl_mime
*mime
= curl_mime_init(conn
->handle
);
1442 curl_mime_free(conn
->curl_MIMEPOST
);
1443 conn
->curl_MIMEPOST
= mime
;
1445 while (v_subparts
!= Val_emptylist
) {
1446 new_part(conn
, mime
, Field(v_subparts
, 0));
1447 v_subparts
= Field(v_subparts
, 1);
1450 result
= curl_easy_setopt(conn
->handle
, CURLOPT_MIMEPOST
, mime
);
1452 if (result
!= CURLE_OK
) {
1453 raiseError(conn
, result
);
1461 #define SETOPT_SLIST(name) \
1462 static void handle_##name(Connection* conn, value option) \
1464 handle_slist(conn,&(conn->curl_##name),CURLOPT_##name,option); \
1468 SETOPT_LONG( INFILESIZE
)
1469 SETOPT_STRING( PROXY
)
1470 SETOPT_LONG( PROXYPORT
)
1471 SETOPT_BOOL( HTTPPROXYTUNNEL
)
1472 SETOPT_BOOL( VERBOSE
)
1473 SETOPT_BOOL( HEADER
)
1474 SETOPT_BOOL( NOPROGRESS
)
1476 #if HAVE_DECL_CURLOPT_NOSIGNAL
1477 SETOPT_BOOL( NOSIGNAL
)
1480 SETOPT_BOOL( NOBODY
)
1481 SETOPT_BOOL( FAILONERROR
)
1482 SETOPT_BOOL( UPLOAD
)
1484 SETOPT_BOOL( FTPLISTONLY
)
1485 SETOPT_BOOL( FTPAPPEND
)
1488 static void handle_NETRC(Connection
*conn
, value option
)
1491 CURLcode result
= CURLE_OK
;
1494 switch (Long_val(option
))
1496 case 0: /* CURL_NETRC_OPTIONAL */
1497 netrc
= CURL_NETRC_OPTIONAL
;
1500 case 1:/* CURL_NETRC_IGNORED */
1501 netrc
= CURL_NETRC_IGNORED
;
1504 case 2: /* CURL_NETRC_REQUIRED */
1505 netrc
= CURL_NETRC_REQUIRED
;
1509 caml_failwith("Invalid NETRC Option");
1513 result
= curl_easy_setopt(conn
->handle
,
1517 if (result
!= CURLE_OK
)
1518 raiseError(conn
, result
);
1523 #if HAVE_DECL_CURLOPT_ENCODING
1524 static void handle_ENCODING(Connection
*conn
, value option
)
1527 CURLcode result
= CURLE_OK
;
1529 switch (Long_val(option
))
1531 case 0: /* CURL_ENCODING_NONE */
1532 result
= curl_easy_setopt(conn
->handle
,
1537 case 1: /* CURL_ENCODING_DEFLATE */
1538 result
= curl_easy_setopt(conn
->handle
,
1543 case 2: /* CURL_ENCODING_GZIP */
1544 result
= curl_easy_setopt(conn
->handle
,
1549 case 3: /* CURL_ENCODING_ANY */
1550 result
= curl_easy_setopt(conn
->handle
,
1556 caml_failwith("Invalid Encoding Option");
1560 if (result
!= CURLE_OK
)
1561 raiseError(conn
, result
);
1568 SETOPT_BOOL( FOLLOWLOCATION
)
1569 SETOPT_BOOL( TRANSFERTEXT
)
1571 SETOPT_STRING( USERPWD
)
1572 SETOPT_STRING( PROXYUSERPWD
)
1573 SETOPT_STRING( RANGE
)
1575 static void handle_ERRORBUFFER(Connection
*conn
, value option
)
1578 CURLcode result
= CURLE_OK
;
1580 Store_field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
, option
);
1582 if (conn
->curl_ERRORBUFFER
!= NULL
)
1583 free(conn
->curl_ERRORBUFFER
);
1585 conn
->curl_ERRORBUFFER
= (char*)malloc(sizeof(char) * CURL_ERROR_SIZE
);
1587 result
= curl_easy_setopt(conn
->handle
,
1588 CURLOPT_ERRORBUFFER
,
1589 conn
->curl_ERRORBUFFER
);
1591 if (result
!= CURLE_OK
)
1592 raiseError(conn
, result
);
1597 SETOPT_LONG( TIMEOUT
)
1599 static void handle_POSTFIELDS(Connection
*conn
, value option
)
1602 CURLcode result
= CURLE_OK
;
1604 if (conn
->curl_POSTFIELDS
!= NULL
)
1605 free(conn
->curl_POSTFIELDS
);
1607 conn
->curl_POSTFIELDS
= strdup_ml(option
);
1609 result
= curl_easy_setopt(conn
->handle
,
1611 conn
->curl_POSTFIELDS
);
1613 if (result
!= CURLE_OK
)
1614 raiseError(conn
, result
);
1619 SETOPT_LONG( POSTFIELDSIZE
)
1620 SETOPT_STRING( REFERER
)
1621 SETOPT_STRING( USERAGENT
)
1622 SETOPT_STRING( FTPPORT
)
1623 SETOPT_LONG( LOW_SPEED_LIMIT
)
1624 SETOPT_LONG( LOW_SPEED_TIME
)
1625 SETOPT_LONG( RESUME_FROM
)
1626 SETOPT_STRING( COOKIE
)
1628 SETOPT_SLIST( HTTPHEADER
)
1630 static void handle_HTTPPOST(Connection
*conn
, value option
)
1633 CAMLlocal3(listIter
, formItem
, contentType
);
1634 CURLcode result
= CURLE_OK
;
1638 free_curl_slist(conn
->httpPostBuffers
);
1639 if (conn
->httpPostFirst
!= NULL
)
1640 curl_formfree(conn
->httpPostFirst
);
1642 conn
->httpPostBuffers
= NULL
;
1643 conn
->httpPostFirst
= NULL
;
1644 conn
->httpPostLast
= NULL
;
1646 while (!Is_long(listIter
))
1648 formItem
= Field(listIter
, 0);
1650 switch (Tag_val(formItem
))
1652 case 0: /* CURLFORM_CONTENT */
1653 if (Wosize_val(formItem
) < 3)
1655 caml_failwith("Incorrect CURLFORM_CONTENT parameters");
1658 if (Is_long(Field(formItem
, 2)) &&
1659 Long_val(Field(formItem
, 2)) == 0)
1661 curl_formadd(&conn
->httpPostFirst
,
1662 &conn
->httpPostLast
,
1664 String_val(Field(formItem
, 0)),
1665 CURLFORM_NAMELENGTH
,
1666 caml_string_length(Field(formItem
, 0)),
1667 CURLFORM_COPYCONTENTS
,
1668 String_val(Field(formItem
, 1)),
1669 CURLFORM_CONTENTSLENGTH
,
1670 caml_string_length(Field(formItem
, 1)),
1673 else if (Is_block(Field(formItem
, 2)))
1675 contentType
= Field(formItem
, 2);
1677 curl_formadd(&conn
->httpPostFirst
,
1678 &conn
->httpPostLast
,
1680 String_val(Field(formItem
, 0)),
1681 CURLFORM_NAMELENGTH
,
1682 caml_string_length(Field(formItem
, 0)),
1683 CURLFORM_COPYCONTENTS
,
1684 String_val(Field(formItem
, 1)),
1685 CURLFORM_CONTENTSLENGTH
,
1686 caml_string_length(Field(formItem
, 1)),
1687 CURLFORM_CONTENTTYPE
,
1688 String_val(Field(contentType
, 0)),
1693 caml_failwith("Incorrect CURLFORM_CONTENT parameters");
1697 case 1: /* CURLFORM_FILECONTENT */
1698 if (Wosize_val(formItem
) < 3)
1700 caml_failwith("Incorrect CURLFORM_FILECONTENT parameters");
1703 if (Is_long(Field(formItem
, 2)) &&
1704 Long_val(Field(formItem
, 2)) == 0)
1706 curl_formadd(&conn
->httpPostFirst
,
1707 &conn
->httpPostLast
,
1709 String_val(Field(formItem
, 0)),
1710 CURLFORM_NAMELENGTH
,
1711 caml_string_length(Field(formItem
, 0)),
1712 CURLFORM_FILECONTENT
,
1713 String_val(Field(formItem
, 1)),
1716 else if (Is_block(Field(formItem
, 2)))
1718 contentType
= Field(formItem
, 2);
1720 curl_formadd(&conn
->httpPostFirst
,
1721 &conn
->httpPostLast
,
1723 String_val(Field(formItem
, 0)),
1724 CURLFORM_NAMELENGTH
,
1725 caml_string_length(Field(formItem
, 0)),
1726 CURLFORM_FILECONTENT
,
1727 String_val(Field(formItem
, 1)),
1728 CURLFORM_CONTENTTYPE
,
1729 String_val(Field(contentType
, 0)),
1734 caml_failwith("Incorrect CURLFORM_FILECONTENT parameters");
1738 case 2: /* CURLFORM_FILE */
1739 if (Wosize_val(formItem
) < 3)
1741 caml_failwith("Incorrect CURLFORM_FILE parameters");
1744 if (Is_long(Field(formItem
, 2)) &&
1745 Long_val(Field(formItem
, 2)) == 0)
1747 curl_formadd(&conn
->httpPostFirst
,
1748 &conn
->httpPostLast
,
1750 String_val(Field(formItem
, 0)),
1751 CURLFORM_NAMELENGTH
,
1752 caml_string_length(Field(formItem
, 0)),
1754 String_val(Field(formItem
, 1)),
1757 else if (Is_block(Field(formItem
, 2)))
1759 contentType
= Field(formItem
, 2);
1761 curl_formadd(&conn
->httpPostFirst
,
1762 &conn
->httpPostLast
,
1764 String_val(Field(formItem
, 0)),
1765 CURLFORM_NAMELENGTH
,
1766 caml_string_length(Field(formItem
, 0)),
1768 String_val(Field(formItem
, 1)),
1769 CURLFORM_CONTENTTYPE
,
1770 String_val(Field(contentType
, 0)),
1775 caml_failwith("Incorrect CURLFORM_FILE parameters");
1779 case 3: /* CURLFORM_BUFFER */
1780 if (Wosize_val(formItem
) < 4)
1782 caml_failwith("Incorrect CURLFORM_BUFFER parameters");
1785 if (Is_long(Field(formItem
, 3)) &&
1786 Long_val(Field(formItem
, 3)) == 0)
1788 conn
->httpPostBuffers
= curl_slist_prepend_ml(conn
->httpPostBuffers
, Field(formItem
, 2));
1790 curl_formadd(&conn
->httpPostFirst
,
1791 &conn
->httpPostLast
,
1793 String_val(Field(formItem
, 0)),
1794 CURLFORM_NAMELENGTH
,
1795 caml_string_length(Field(formItem
, 0)),
1797 String_val(Field(formItem
, 1)),
1799 conn
->httpPostBuffers
->data
,
1800 CURLFORM_BUFFERLENGTH
,
1801 caml_string_length(Field(formItem
, 2)),
1804 else if (Is_block(Field(formItem
, 3)))
1806 conn
->httpPostBuffers
= curl_slist_prepend_ml(conn
->httpPostBuffers
, Field(formItem
, 2));
1808 contentType
= Field(formItem
, 3);
1810 curl_formadd(&conn
->httpPostFirst
,
1811 &conn
->httpPostLast
,
1813 String_val(Field(formItem
, 0)),
1814 CURLFORM_NAMELENGTH
,
1815 caml_string_length(Field(formItem
, 0)),
1817 String_val(Field(formItem
, 1)),
1819 conn
->httpPostBuffers
->data
,
1820 CURLFORM_BUFFERLENGTH
,
1821 caml_string_length(Field(formItem
, 2)),
1822 CURLFORM_CONTENTTYPE
,
1823 String_val(Field(contentType
, 0)),
1828 caml_failwith("Incorrect CURLFORM_BUFFER parameters");
1833 listIter
= Field(listIter
, 1);
1836 result
= curl_easy_setopt(conn
->handle
,
1838 conn
->httpPostFirst
);
1840 if (result
!= CURLE_OK
)
1841 raiseError(conn
, result
);
1846 SETOPT_STRING( SSLCERT
)
1847 SETOPT_STRING( SSLCERTTYPE
)
1848 SETOPT_STRING( SSLCERTPASSWD
)
1849 SETOPT_STRING( SSLKEY
)
1850 SETOPT_STRING( SSLKEYTYPE
)
1851 SETOPT_STRING( SSLKEYPASSWD
)
1852 SETOPT_STRING( SSLENGINE
)
1853 SETOPT_BOOL( SSLENGINE_DEFAULT
)
1856 SETOPT_SLIST( QUOTE
)
1857 SETOPT_SLIST( POSTQUOTE
)
1859 SETOPT_STRING( COOKIEFILE
)
1860 #if HAVE_DECL_CURLOPT_CERTINFO
1861 SETOPT_BOOL( CERTINFO
)
1864 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_0
1865 #define CURL_SSLVERSION_TLSv1_0 CURL_SSLVERSION_TLSv1
1868 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_1
1869 #define CURL_SSLVERSION_TLSv1_1 CURL_SSLVERSION_TLSv1
1872 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_2
1873 #define CURL_SSLVERSION_TLSv1_2 CURL_SSLVERSION_TLSv1
1876 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_3
1877 #define CURL_SSLVERSION_TLSv1_3 CURL_SSLVERSION_TLSv1
1880 static void handle_SSLVERSION(Connection
*conn
, value option
)
1883 CURLcode result
= CURLE_OK
;
1884 int v
= CURL_SSLVERSION_DEFAULT
;
1886 switch (Long_val(option
))
1888 case 0: v
= CURL_SSLVERSION_DEFAULT
; break;
1889 case 1: v
= CURL_SSLVERSION_TLSv1
; break;
1890 case 2: v
= CURL_SSLVERSION_SSLv2
; break;
1891 case 3: v
= CURL_SSLVERSION_SSLv3
; break;
1892 case 4: v
= CURL_SSLVERSION_TLSv1_0
; break;
1893 case 5: v
= CURL_SSLVERSION_TLSv1_1
; break;
1894 case 6: v
= CURL_SSLVERSION_TLSv1_2
; break;
1895 case 7: v
= CURL_SSLVERSION_TLSv1_3
; break;
1897 caml_failwith("Invalid SSLVERSION Option");
1901 result
= curl_easy_setopt(conn
->handle
, CURLOPT_SSLVERSION
, v
);
1903 if (result
!= CURLE_OK
)
1904 raiseError(conn
, result
);
1909 static void handle_TIMECONDITION(Connection
*conn
, value option
)
1912 CURLcode result
= CURLE_OK
;
1913 int timecond
= CURL_TIMECOND_NONE
;
1915 switch (Long_val(option
))
1917 case 0: timecond
= CURL_TIMECOND_NONE
; break;
1918 case 1: timecond
= CURL_TIMECOND_IFMODSINCE
; break;
1919 case 2: timecond
= CURL_TIMECOND_IFUNMODSINCE
; break;
1920 case 3: timecond
= CURL_TIMECOND_LASTMOD
; break;
1922 caml_failwith("Invalid TIMECOND Option");
1926 result
= curl_easy_setopt(conn
->handle
, CURLOPT_TIMECONDITION
, timecond
);
1928 if (result
!= CURLE_OK
)
1929 raiseError(conn
, result
);
1934 SETOPT_VAL( TIMEVALUE
, Int32_val
)
1935 SETOPT_STRING( CUSTOMREQUEST
)
1936 SETOPT_STRING( INTERFACE
)
1938 static void handle_KRB4LEVEL(Connection
*conn
, value option
)
1941 CURLcode result
= CURLE_OK
;
1943 switch (Long_val(option
))
1945 case 0: /* KRB4_NONE */
1946 result
= curl_easy_setopt(conn
->handle
,
1951 case 1: /* KRB4_CLEAR */
1952 result
= curl_easy_setopt(conn
->handle
,
1957 case 2: /* KRB4_SAFE */
1958 result
= curl_easy_setopt(conn
->handle
,
1963 case 3: /* KRB4_CONFIDENTIAL */
1964 result
= curl_easy_setopt(conn
->handle
,
1969 case 4: /* KRB4_PRIVATE */
1970 result
= curl_easy_setopt(conn
->handle
,
1976 caml_failwith("Invalid KRB4 Option");
1980 if (result
!= CURLE_OK
)
1981 raiseError(conn
, result
);
1986 SETOPT_BOOL( SSL_VERIFYPEER
)
1987 SETOPT_STRING( CAINFO
)
1988 SETOPT_STRING( CAPATH
)
1989 SETOPT_BOOL( FILETIME
)
1990 SETOPT_LONG( MAXREDIRS
)
1991 SETOPT_LONG( MAXCONNECTS
)
1993 static void handle_CLOSEPOLICY(Connection
*conn
, value option
)
1996 CURLcode result
= CURLE_OK
;
1998 switch (Long_val(option
))
2000 case 0: /* CLOSEPOLICY_OLDEST */
2001 result
= curl_easy_setopt(conn
->handle
,
2002 CURLOPT_CLOSEPOLICY
,
2003 CURLCLOSEPOLICY_OLDEST
);
2006 case 1: /* CLOSEPOLICY_LEAST_RECENTLY_USED */
2007 result
= curl_easy_setopt(conn
->handle
,
2008 CURLOPT_CLOSEPOLICY
,
2009 CURLCLOSEPOLICY_LEAST_RECENTLY_USED
);
2013 caml_failwith("Invalid CLOSEPOLICY Option");
2017 if (result
!= CURLE_OK
)
2018 raiseError(conn
, result
);
2023 SETOPT_BOOL( FRESH_CONNECT
)
2024 SETOPT_BOOL( FORBID_REUSE
)
2025 SETOPT_STRING( RANDOM_FILE
)
2026 SETOPT_STRING( EGDSOCKET
)
2027 SETOPT_LONG( CONNECTTIMEOUT
)
2028 SETOPT_BOOL( HTTPGET
)
2030 static void handle_SSL_VERIFYHOST(Connection
*conn
, value option
)
2033 CURLcode result
= CURLE_OK
;
2035 switch (Long_val(option
))
2037 case 0: /* SSLVERIFYHOST_NONE */
2038 case 1: /* SSLVERIFYHOST_EXISTENCE */
2039 case 2: /* SSLVERIFYHOST_HOSTNAME */
2040 result
= curl_easy_setopt(conn
->handle
,
2041 CURLOPT_SSL_VERIFYHOST
,
2042 /* map EXISTENCE to HOSTNAME */
2043 Long_val(option
) == 0 ? 0 : 2);
2047 caml_failwith("Invalid SSLVERIFYHOST Option");
2051 if (result
!= CURLE_OK
)
2052 raiseError(conn
, result
);
2057 SETOPT_STRING( COOKIEJAR
)
2058 SETOPT_STRING( SSL_CIPHER_LIST
)
2060 static void handle_HTTP_VERSION(Connection
*conn
, value option
)
2063 CURLcode result
= CURLE_OK
;
2065 long version
= CURL_HTTP_VERSION_NONE
;
2067 switch (Long_val(option
))
2069 case 0: version
= CURL_HTTP_VERSION_NONE
; break;
2070 case 1: version
= CURL_HTTP_VERSION_1_0
; break;
2071 case 2: version
= CURL_HTTP_VERSION_1_1
; break;
2073 #if HAVE_DECL_CURL_HTTP_VERSION_2
2074 version
= CURL_HTTP_VERSION_2
;
2075 #elif HAVE_DECL_CURL_HTTP_VERSION_2_0
2076 version
= CURL_HTTP_VERSION_2_0
;
2080 #if HAVE_DECL_CURL_HTTP_VERSION_2TLS
2081 version
= CURL_HTTP_VERSION_2TLS
;
2085 #if HAVE_DECL_CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
2086 version
= CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
;
2090 #if HAVE_DECL_CURL_HTTP_VERSION_3
2091 version
= CURL_HTTP_VERSION_3
;
2095 caml_invalid_argument("CURLOPT_HTTP_VERSION");
2099 result
= curl_easy_setopt(conn
->handle
, CURLOPT_HTTP_VERSION
, version
);
2101 if (result
!= CURLE_OK
)
2102 raiseError(conn
, result
);
2107 SETOPT_BOOL( FTP_USE_EPSV
)
2108 SETOPT_LONG( DNS_CACHE_TIMEOUT
)
2109 SETOPT_BOOL( DNS_USE_GLOBAL_CACHE
)
2111 static void handle_PRIVATE(Connection
*conn
, value option
)
2114 Store_field(conn
->ocamlValues
, Ocaml_PRIVATE
, option
);
2118 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2119 SETOPT_SLIST( HTTP200ALIASES
)
2122 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2123 SETOPT_BOOL( UNRESTRICTED_AUTH
)
2126 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2127 SETOPT_BOOL( FTP_USE_EPRT
)
2130 #if HAVE_DECL_CURLOPT_HTTPAUTH
2131 static void handle_HTTPAUTH(Connection
*conn
, value option
)
2134 CAMLlocal1(listIter
);
2135 CURLcode result
= CURLE_OK
;
2136 long auth
= CURLAUTH_NONE
;
2140 while (!Is_long(listIter
))
2142 switch (Long_val(Field(listIter
, 0)))
2144 case 0: /* CURLAUTH_BASIC */
2145 auth
|= CURLAUTH_BASIC
;
2148 case 1: /* CURLAUTH_DIGEST */
2149 auth
|= CURLAUTH_DIGEST
;
2152 case 2: /* CURLAUTH_GSSNEGOTIATE */
2153 auth
|= CURLAUTH_GSSNEGOTIATE
;
2156 case 3: /* CURLAUTH_NTLM */
2157 auth
|= CURLAUTH_NTLM
;
2160 case 4: /* CURLAUTH_ANY */
2161 auth
|= CURLAUTH_ANY
;
2164 case 5: /* CURLAUTH_ANYSAFE */
2165 auth
|= CURLAUTH_ANYSAFE
;
2169 caml_failwith("Invalid HTTPAUTH Value");
2173 listIter
= Field(listIter
, 1);
2176 result
= curl_easy_setopt(conn
->handle
,
2180 if (result
!= CURLE_OK
)
2181 raiseError(conn
, result
);
2187 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2188 SETOPT_BOOL( FTP_CREATE_MISSING_DIRS
)
2191 #if HAVE_DECL_CURLOPT_PROXYAUTH
2192 static void handle_PROXYAUTH(Connection
*conn
, value option
)
2195 CAMLlocal1(listIter
);
2196 CURLcode result
= CURLE_OK
;
2197 long auth
= CURLAUTH_NONE
;
2201 while (!Is_long(listIter
))
2203 switch (Long_val(Field(listIter
, 0)))
2205 case 0: /* CURLAUTH_BASIC */
2206 auth
|= CURLAUTH_BASIC
;
2209 case 1: /* CURLAUTH_DIGEST */
2210 auth
|= CURLAUTH_DIGEST
;
2213 case 2: /* CURLAUTH_GSSNEGOTIATE */
2214 auth
|= CURLAUTH_GSSNEGOTIATE
;
2217 case 3: /* CURLAUTH_NTLM */
2218 auth
|= CURLAUTH_NTLM
;
2221 case 4: /* CURLAUTH_ANY */
2222 auth
|= CURLAUTH_ANY
;
2225 case 5: /* CURLAUTH_ANYSAFE */
2226 auth
|= CURLAUTH_ANYSAFE
;
2230 caml_failwith("Invalid HTTPAUTH Value");
2234 listIter
= Field(listIter
, 1);
2237 result
= curl_easy_setopt(conn
->handle
,
2241 if (result
!= CURLE_OK
)
2242 raiseError(conn
, result
);
2248 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2249 SETOPT_LONG( FTP_RESPONSE_TIMEOUT
)
2252 #if HAVE_DECL_CURLOPT_IPRESOLVE
2253 static void handle_IPRESOLVE(Connection
*conn
, value option
)
2256 CURLcode result
= CURLE_OK
;
2258 switch (Long_val(option
))
2260 case 0: /* CURL_IPRESOLVE_WHATEVER */
2261 result
= curl_easy_setopt(conn
->handle
,
2263 CURL_IPRESOLVE_WHATEVER
);
2266 case 1: /* CURL_IPRESOLVE_V4 */
2267 result
= curl_easy_setopt(conn
->handle
,
2272 case 2: /* CURL_IPRESOLVE_V6 */
2273 result
= curl_easy_setopt(conn
->handle
,
2279 caml_failwith("Invalid IPRESOLVE Value");
2283 if (result
!= CURLE_OK
)
2284 raiseError(conn
, result
);
2290 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2291 SETOPT_VAL( MAXFILESIZE
, Int32_val
)
2294 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2295 SETOPT_INT64( INFILESIZE_LARGE
)
2298 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2299 SETOPT_INT64( RESUME_FROM_LARGE
)
2302 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2303 SETOPT_INT64( MAXFILESIZE_LARGE
)
2306 #if HAVE_DECL_CURLOPT_NETRC_FILE
2307 SETOPT_STRING( NETRC_FILE
)
2310 #if HAVE_DECL_CURLOPT_FTP_SSL
2311 static void handle_FTP_SSL(Connection
*conn
, value option
)
2314 CURLcode result
= CURLE_OK
;
2316 switch (Long_val(option
))
2318 case 0: /* CURLFTPSSL_NONE */
2319 result
= curl_easy_setopt(conn
->handle
,
2324 case 1: /* CURLFTPSSL_TRY */
2325 result
= curl_easy_setopt(conn
->handle
,
2330 case 2: /* CURLFTPSSL_CONTROL */
2331 result
= curl_easy_setopt(conn
->handle
,
2333 CURLFTPSSL_CONTROL
);
2336 case 3: /* CURLFTPSSL_ALL */
2337 result
= curl_easy_setopt(conn
->handle
,
2343 caml_failwith("Invalid FTP_SSL Value");
2347 if (result
!= CURLE_OK
)
2348 raiseError(conn
, result
);
2354 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
2355 SETOPT_INT64( POSTFIELDSIZE_LARGE
)
2358 #if HAVE_DECL_CURLOPT_TCP_NODELAY
2359 /* not using SETOPT_BOOL here because of TCP_NODELAY defined in winsock.h */
2360 SETOPT_VAL_( handle_TCP_NODELAY
, CURLOPT_TCP_NODELAY
, Bool_val
)
2363 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
2364 static void handle_FTPSSLAUTH(Connection
*conn
, value option
)
2367 CURLcode result
= CURLE_OK
;
2369 switch (Long_val(option
))
2371 case 0: /* CURLFTPAUTH_DEFAULT */
2372 result
= curl_easy_setopt(conn
->handle
,
2374 CURLFTPAUTH_DEFAULT
);
2377 case 1: /* CURLFTPAUTH_SSL */
2378 result
= curl_easy_setopt(conn
->handle
,
2383 case 2: /* CURLFTPAUTH_TLS */
2384 result
= curl_easy_setopt(conn
->handle
,
2390 caml_failwith("Invalid FTPSSLAUTH value");
2394 if (result
!= CURLE_OK
)
2395 raiseError(conn
, result
);
2401 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
2402 SETOPT_STRING( FTP_ACCOUNT
)
2405 #if HAVE_DECL_CURLOPT_COOKIELIST
2406 SETOPT_STRING( COOKIELIST
)
2409 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
2410 SETOPT_BOOL( IGNORE_CONTENT_LENGTH
)
2413 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
2414 SETOPT_BOOL( FTP_SKIP_PASV_IP
)
2417 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
2418 static void handle_FTP_FILEMETHOD(Connection
*conn
, value option
)
2421 CURLcode result
= CURLE_OK
;
2423 switch (Long_val(option
))
2425 case 0: /* CURLFTPMETHOD_DEFAULT */
2426 result
= curl_easy_setopt(conn
->handle
,
2427 CURLOPT_FTP_FILEMETHOD
,
2428 CURLFTPMETHOD_DEFAULT
);
2431 case 1: /* CURLFTMETHOD_MULTICWD */
2432 result
= curl_easy_setopt(conn
->handle
,
2433 CURLOPT_FTP_FILEMETHOD
,
2434 CURLFTPMETHOD_MULTICWD
);
2437 case 2: /* CURLFTPMETHOD_NOCWD */
2438 result
= curl_easy_setopt(conn
->handle
,
2439 CURLOPT_FTP_FILEMETHOD
,
2440 CURLFTPMETHOD_NOCWD
);
2443 case 3: /* CURLFTPMETHOD_SINGLECWD */
2444 result
= curl_easy_setopt(conn
->handle
,
2445 CURLOPT_FTP_FILEMETHOD
,
2446 CURLFTPMETHOD_SINGLECWD
);
2449 caml_failwith("Invalid FTP_FILEMETHOD value");
2453 if (result
!= CURLE_OK
)
2454 raiseError(conn
, result
);
2460 #if HAVE_DECL_CURLOPT_LOCALPORT
2461 SETOPT_LONG( LOCALPORT
)
2464 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
2465 SETOPT_LONG( LOCALPORTRANGE
)
2468 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
2469 SETOPT_BOOL( CONNECT_ONLY
)
2472 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
2473 SETOPT_INT64( MAX_SEND_SPEED_LARGE
)
2476 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
2477 SETOPT_INT64( MAX_RECV_SPEED_LARGE
)
2480 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
2481 SETOPT_STRING( FTP_ALTERNATIVE_TO_USER
)
2484 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
2485 SETOPT_BOOL( SSL_SESSIONID_CACHE
)
2488 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
2489 static void handle_SSH_AUTH_TYPES(Connection
*conn
, value option
)
2492 CAMLlocal1(listIter
);
2493 CURLcode result
= CURLE_OK
;
2494 long authTypes
= CURLSSH_AUTH_NONE
;
2498 while (!Is_long(listIter
))
2500 switch (Long_val(Field(listIter
, 0)))
2502 case 0: /* CURLSSH_AUTH_ANY */
2503 authTypes
|= CURLSSH_AUTH_ANY
;
2506 case 1: /* CURLSSH_AUTH_PUBLICKEY */
2507 authTypes
|= CURLSSH_AUTH_PUBLICKEY
;
2510 case 2: /* CURLSSH_AUTH_PASSWORD */
2511 authTypes
|= CURLSSH_AUTH_PASSWORD
;
2514 case 3: /* CURLSSH_AUTH_HOST */
2515 authTypes
|= CURLSSH_AUTH_HOST
;
2518 case 4: /* CURLSSH_AUTH_KEYBOARD */
2519 authTypes
|= CURLSSH_AUTH_KEYBOARD
;
2523 caml_failwith("Invalid CURLSSH_AUTH_TYPES Value");
2527 listIter
= Field(listIter
, 1);
2530 result
= curl_easy_setopt(conn
->handle
,
2531 CURLOPT_SSH_AUTH_TYPES
,
2534 if (result
!= CURLE_OK
)
2535 raiseError(conn
, result
);
2541 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
2542 SETOPT_STRING( SSH_PUBLIC_KEYFILE
)
2545 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
2546 SETOPT_STRING( SSH_PRIVATE_KEYFILE
)
2549 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
2550 static void handle_FTP_SSL_CCC(Connection
*conn
, value option
)
2553 CURLcode result
= CURLE_OK
;
2555 switch (Long_val(option
))
2557 case 0: /* CURLFTPSSL_CCC_NONE */
2558 result
= curl_easy_setopt(conn
->handle
,
2559 CURLOPT_FTP_SSL_CCC
,
2560 CURLFTPSSL_CCC_NONE
);
2563 case 1: /* CURLFTPSSL_CCC_PASSIVE */
2564 result
= curl_easy_setopt(conn
->handle
,
2565 CURLOPT_FTP_SSL_CCC
,
2566 CURLFTPSSL_CCC_PASSIVE
);
2569 case 2: /* CURLFTPSSL_CCC_ACTIVE */
2570 result
= curl_easy_setopt(conn
->handle
,
2571 CURLOPT_FTP_SSL_CCC
,
2572 CURLFTPSSL_CCC_ACTIVE
);
2576 caml_failwith("Invalid FTPSSL_CCC value");
2580 if (result
!= CURLE_OK
)
2581 raiseError(conn
, result
);
2587 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
2588 SETOPT_LONG( TIMEOUT_MS
)
2591 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
2592 SETOPT_LONG( CONNECTTIMEOUT_MS
)
2595 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
2596 SETOPT_BOOL( HTTP_TRANSFER_DECODING
)
2599 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
2600 SETOPT_BOOL( HTTP_CONTENT_DECODING
)
2603 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
2604 SETOPT_LONG( NEW_FILE_PERMS
)
2607 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
2608 SETOPT_LONG( NEW_DIRECTORY_PERMS
)
2611 #if HAVE_DECL_CURLOPT_POST301
2612 SETOPT_BOOL( POST301
)
2615 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
2616 SETOPT_STRING( SSH_HOST_PUBLIC_KEY_MD5
)
2619 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
2620 SETOPT_STRING( COPYPOSTFIELDS
)
2623 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
2624 SETOPT_BOOL( PROXY_TRANSFER_MODE
)
2627 #if HAVE_DECL_CURLOPT_AUTOREFERER
2628 SETOPT_BOOL( AUTOREFERER
)
2631 #if HAVE_DECL_CURLOPT_PROXYTYPE
2632 static void handle_PROXYTYPE(Connection
*conn
, value option
)
2635 CURLcode result
= CURLE_OK
;
2638 switch (Long_val(option
))
2640 case 0: proxy_type
= CURLPROXY_HTTP
; break;
2641 case 1: proxy_type
= CURLPROXY_HTTP_1_0
; break;
2642 case 2: proxy_type
= CURLPROXY_SOCKS4
; break;
2643 case 3: proxy_type
= CURLPROXY_SOCKS5
; break;
2644 case 4: proxy_type
= CURLPROXY_SOCKS4A
; break;
2645 case 5: proxy_type
= CURLPROXY_SOCKS5_HOSTNAME
; break;
2647 caml_failwith("Invalid curl proxy type");
2650 result
= curl_easy_setopt(conn
->handle
,
2654 if (result
!= CURLE_OK
)
2655 raiseError(conn
, result
);
2661 #if HAVE_DECL_CURLOPT_PROTOCOLS || HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
2666 CURLPROTO_HTTP
, CURLPROTO_HTTPS
, CURLPROTO_FTP
, CURLPROTO_FTPS
, CURLPROTO_SCP
, CURLPROTO_SFTP
,
2667 CURLPROTO_TELNET
, CURLPROTO_LDAP
, CURLPROTO_LDAPS
, CURLPROTO_DICT
, CURLPROTO_FILE
, CURLPROTO_TFTP
,
2668 /* factor out with autoconf? */
2669 #if defined(CURLPROTO_IMAP)
2674 #if defined(CURLPROTO_IMAPS)
2679 #if defined(CURLPROTO_POP3)
2684 #if defined(CURLPROTO_POP3S)
2689 #if defined(CURLPROTO_SMTP)
2694 #if defined(CURLPROTO_SMTPS)
2699 #if defined(CURLPROTO_RTSP)
2704 #if defined(CURLPROTO_RTMP)
2709 #if defined(CURLPROTO_RTMPT)
2714 #if defined(CURLPROTO_RTMPE)
2719 #if defined(CURLPROTO_RTMPTE)
2724 #if defined(CURLPROTO_RTMPS)
2729 #if defined(CURLPROTO_RTMPTS)
2734 #if defined(CURLPROTO_GOPHER)
2741 static void handle_PROTOCOLSOPTION(CURLoption curlopt
, Connection
*conn
, value option
)
2744 CURLcode result
= CURLE_OK
;
2745 long bits
= convert_bit_list(protoMap
, sizeof(protoMap
) / sizeof(protoMap
[0]), option
);
2747 result
= curl_easy_setopt(conn
->handle
, curlopt
, bits
);
2749 if (result
!= CURLE_OK
)
2750 raiseError(conn
, result
);
2756 #if HAVE_DECL_CURLOPT_PROTOCOLS
2757 static void handle_PROTOCOLS(Connection
*conn
, value option
)
2759 handle_PROTOCOLSOPTION(CURLOPT_PROTOCOLS
, conn
, option
);
2763 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
2764 static void handle_REDIR_PROTOCOLS(Connection
*conn
, value option
)
2766 handle_PROTOCOLSOPTION(CURLOPT_REDIR_PROTOCOLS
, conn
, option
);
2770 #if HAVE_DECL_CURLOPT_RESOLVE
2771 SETOPT_SLIST( RESOLVE
)
2774 #if HAVE_DECL_CURLOPT_DNS_SERVERS
2775 SETOPT_STRING( DNS_SERVERS
)
2778 #if HAVE_DECL_CURLOPT_MAIL_FROM
2779 SETOPT_STRING( MAIL_FROM
)
2782 #if HAVE_DECL_CURLOPT_MAIL_RCPT
2783 SETOPT_SLIST( MAIL_RCPT
)
2786 #if HAVE_DECL_CURLOPT_PIPEWAIT
2787 SETOPT_BOOL( PIPEWAIT
)
2790 #if HAVE_DECL_CURLOPT_USERNAME
2791 SETOPT_STRING( USERNAME
)
2794 #if HAVE_DECL_CURLOPT_PASSWORD
2795 SETOPT_STRING( PASSWORD
)
2798 #if HAVE_DECL_CURLOPT_LOGIN_OPTIONS
2799 SETOPT_STRING( LOGIN_OPTIONS
)
2802 #if HAVE_DECL_CURLOPT_CONNECT_TO
2803 SETOPT_SLIST( CONNECT_TO
)
2806 #if HAVE_DECL_CURLOPT_POSTREDIR
2808 static int curlPostRedir_table
[] = {
2809 CURL_REDIR_POST_ALL
,
2810 #if defined(CURL_REDIR_POST_301)
2811 CURL_REDIR_POST_301
,
2815 #if defined(CURL_REDIR_POST_302)
2816 CURL_REDIR_POST_302
,
2820 #if defined(CURL_REDIR_POST_303)
2821 CURL_REDIR_POST_303
,
2827 static void handle_POSTREDIR(Connection
*conn
, value option
)
2830 CURLcode result
= CURLE_OK
;
2831 long bitmask
= caml_convert_flag_list(option
, curlPostRedir_table
);
2833 result
= curl_easy_setopt(conn
->handle
,
2837 if (result
!= CURLE_OK
)
2838 raiseError(conn
, result
);
2844 SETOPT_VAL( SSH_KNOWNHOSTS
, String_val
)
2847 ** curl_easy_setopt helper function
2850 #define CURLOPT(name) { handle_ ## name, "CURLOPT_"#name }
2851 #define HAVENOT(name) { NULL, "CURLOPT_"#name }
2853 CURLOptionMapping implementedOptionMap
[] =
2855 CURLOPT(WRITEFUNCTION
),
2856 CURLOPT(READFUNCTION
),
2857 CURLOPT(INFILESIZE
),
2861 CURLOPT(HTTPPROXYTUNNEL
),
2864 CURLOPT(NOPROGRESS
),
2865 #if HAVE_DECL_CURLOPT_NOSIGNAL
2871 CURLOPT(FAILONERROR
),
2874 CURLOPT(FTPLISTONLY
),
2877 #if HAVE_DECL_CURLOPT_ENCODING
2882 CURLOPT(FOLLOWLOCATION
),
2883 CURLOPT(TRANSFERTEXT
),
2886 CURLOPT(PROXYUSERPWD
),
2888 CURLOPT(ERRORBUFFER
), /* mutable buffer, as output value, do not duplicate */
2890 CURLOPT(POSTFIELDS
),
2891 CURLOPT(POSTFIELDSIZE
),
2895 CURLOPT(LOW_SPEED_LIMIT
),
2896 CURLOPT(LOW_SPEED_TIME
),
2897 CURLOPT(RESUME_FROM
),
2899 CURLOPT(HTTPHEADER
),
2902 CURLOPT(SSLCERTTYPE
),
2903 CURLOPT(SSLCERTPASSWD
),
2905 CURLOPT(SSLKEYTYPE
),
2906 CURLOPT(SSLKEYPASSWD
),
2908 CURLOPT(SSLENGINE_DEFAULT
),
2912 CURLOPT(HEADERFUNCTION
),
2913 CURLOPT(COOKIEFILE
),
2914 CURLOPT(SSLVERSION
),
2915 CURLOPT(TIMECONDITION
),
2917 CURLOPT(CUSTOMREQUEST
),
2920 CURLOPT(PROGRESSFUNCTION
),
2921 CURLOPT(SSL_VERIFYPEER
),
2926 CURLOPT(MAXCONNECTS
),
2927 CURLOPT(CLOSEPOLICY
),
2928 CURLOPT(FRESH_CONNECT
),
2929 CURLOPT(FORBID_REUSE
),
2930 CURLOPT(RANDOM_FILE
),
2932 CURLOPT(CONNECTTIMEOUT
),
2934 CURLOPT(SSL_VERIFYHOST
),
2936 CURLOPT(SSL_CIPHER_LIST
),
2937 CURLOPT(HTTP_VERSION
),
2938 CURLOPT(FTP_USE_EPSV
),
2939 CURLOPT(DNS_CACHE_TIMEOUT
),
2940 CURLOPT(DNS_USE_GLOBAL_CACHE
),
2941 CURLOPT(DEBUGFUNCTION
),
2943 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2944 CURLOPT(HTTP200ALIASES
),
2946 HAVENOT(HTTP200ALIASES
),
2948 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2949 CURLOPT(UNRESTRICTED_AUTH
),
2951 HAVENOT(UNRESTRICTED_AUTH
),
2953 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2954 CURLOPT(FTP_USE_EPRT
),
2956 HAVENOT(FTP_USE_EPRT
),
2958 #if HAVE_DECL_CURLOPT_HTTPAUTH
2963 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2964 CURLOPT(FTP_CREATE_MISSING_DIRS
),
2966 HAVENOT(FTP_CREATE_MISSING_DIRS
),
2968 #if HAVE_DECL_CURLOPT_PROXYAUTH
2973 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2974 CURLOPT(FTP_RESPONSE_TIMEOUT
),
2976 HAVENOT(FTP_RESPONSE_TIMEOUT
),
2978 #if HAVE_DECL_CURLOPT_IPRESOLVE
2983 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2984 CURLOPT(MAXFILESIZE
),
2986 HAVENOT(MAXFILESIZE
),
2988 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2989 CURLOPT(INFILESIZE_LARGE
),
2991 HAVENOT(INFILESIZE_LARGE
),
2993 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2994 CURLOPT(RESUME_FROM_LARGE
),
2996 HAVENOT(RESUME_FROM_LARGE
),
2998 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2999 CURLOPT(MAXFILESIZE_LARGE
),
3001 HAVENOT(MAXFILESIZE_LARGE
),
3003 #if HAVE_DECL_CURLOPT_NETRC_FILE
3004 CURLOPT(NETRC_FILE
),
3006 HAVENOT(NETRC_FILE
),
3008 #if HAVE_DECL_CURLOPT_FTP_SSL
3013 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
3014 CURLOPT(POSTFIELDSIZE_LARGE
),
3016 HAVENOT(POSTFIELDSIZE_LARGE
),
3018 #if HAVE_DECL_CURLOPT_TCP_NODELAY
3019 CURLOPT(TCP_NODELAY
),
3021 HAVENOT(TCP_NODELAY
),
3023 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
3024 CURLOPT(FTPSSLAUTH
),
3026 HAVENOT(FTPSSLAUTH
),
3028 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
3029 CURLOPT(IOCTLFUNCTION
),
3031 HAVENOT(IOCTLFUNCTION
),
3033 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
3034 CURLOPT(FTP_ACCOUNT
),
3036 HAVENOT(FTP_ACCOUNT
),
3038 #if HAVE_DECL_CURLOPT_COOKIELIST
3039 CURLOPT(COOKIELIST
),
3041 HAVENOT(COOKIELIST
),
3043 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
3044 CURLOPT(IGNORE_CONTENT_LENGTH
),
3046 HAVENOT(IGNORE_CONTENT_LENGTH
),
3048 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
3049 CURLOPT(FTP_SKIP_PASV_IP
),
3051 HAVENOT(FTP_SKIP_PASV_IP
),
3053 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
3054 CURLOPT(FTP_FILEMETHOD
),
3056 HAVENOT(FTP_FILEMETHOD
),
3058 #if HAVE_DECL_CURLOPT_LOCALPORT
3063 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
3064 CURLOPT(LOCALPORTRANGE
),
3066 HAVENOT(LOCALPORTRANGE
),
3068 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
3069 CURLOPT(CONNECT_ONLY
),
3071 HAVENOT(CONNECT_ONLY
),
3073 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
3074 CURLOPT(MAX_SEND_SPEED_LARGE
),
3076 HAVENOT(MAX_SEND_SPEED_LARGE
),
3078 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
3079 CURLOPT(MAX_RECV_SPEED_LARGE
),
3081 HAVENOT(MAX_RECV_SPEED_LARGE
),
3083 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
3084 CURLOPT(FTP_ALTERNATIVE_TO_USER
),
3086 HAVENOT(FTP_ALTERNATIVE_TO_USER
),
3088 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
3089 CURLOPT(SSL_SESSIONID_CACHE
),
3091 HAVENOT(SSL_SESSIONID_CACHE
),
3093 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
3094 CURLOPT(SSH_AUTH_TYPES
),
3096 HAVENOT(SSH_AUTH_TYPES
),
3098 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
3099 CURLOPT(SSH_PUBLIC_KEYFILE
),
3101 HAVENOT(SSH_PUBLIC_KEYFILE
),
3103 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
3104 CURLOPT(SSH_PRIVATE_KEYFILE
),
3106 HAVENOT(SSH_PRIVATE_KEYFILE
),
3108 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
3109 CURLOPT(FTP_SSL_CCC
),
3111 HAVENOT(FTP_SSL_CCC
),
3113 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
3114 CURLOPT(TIMEOUT_MS
),
3116 HAVENOT(TIMEOUT_MS
),
3118 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
3119 CURLOPT(CONNECTTIMEOUT_MS
),
3121 HAVENOT(CONNECTTIMEOUT_MS
),
3123 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
3124 CURLOPT(HTTP_TRANSFER_DECODING
),
3126 HAVENOT(HTTP_TRANSFER_DECODING
),
3128 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
3129 CURLOPT(HTTP_CONTENT_DECODING
),
3131 HAVENOT(HTTP_CONTENT_DECODING
),
3133 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
3134 CURLOPT(NEW_FILE_PERMS
),
3136 HAVENOT(NEW_FILE_PERMS
),
3138 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
3139 CURLOPT(NEW_DIRECTORY_PERMS
),
3141 HAVENOT(NEW_DIRECTORY_PERMS
),
3143 #if HAVE_DECL_CURLOPT_POST301
3148 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
3149 CURLOPT(SSH_HOST_PUBLIC_KEY_MD5
),
3151 HAVENOT(SSH_HOST_PUBLIC_KEY_MD5
),
3153 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
3154 CURLOPT(COPYPOSTFIELDS
),
3156 HAVENOT(COPYPOSTFIELDS
),
3158 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
3159 CURLOPT(PROXY_TRANSFER_MODE
),
3161 HAVENOT(PROXY_TRANSFER_MODE
),
3163 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
3164 CURLOPT(SEEKFUNCTION
),
3166 HAVENOT(SEEKFUNCTION
),
3168 #if HAVE_DECL_CURLOPT_AUTOREFERER
3169 CURLOPT(AUTOREFERER
),
3171 HAVENOT(AUTOREFERER
),
3173 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
3174 CURLOPT(OPENSOCKETFUNCTION
),
3176 HAVENOT(OPENSOCKETFUNCTION
),
3178 #if HAVE_DECL_CURLOPT_PROXYTYPE
3183 #if HAVE_DECL_CURLOPT_PROTOCOLS
3188 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3189 CURLOPT(REDIR_PROTOCOLS
),
3191 HAVENOT(REDIR_PROTOCOLS
),
3193 #if HAVE_DECL_CURLOPT_RESOLVE
3198 #if HAVE_DECL_CURLOPT_DNS_SERVERS
3199 CURLOPT(DNS_SERVERS
),
3201 HAVENOT(DNS_SERVERS
),
3203 #if HAVE_DECL_CURLOPT_MAIL_FROM
3208 #if HAVE_DECL_CURLOPT_MAIL_RCPT
3213 #if HAVE_DECL_CURLOPT_PIPEWAIT
3218 #if HAVE_DECL_CURLOPT_CERTINFO
3223 #if HAVE_DECL_CURLOPT_USERNAME
3228 #if HAVE_DECL_CURLOPT_PASSWORD
3233 #if HAVE_DECL_CURLOPT_LOGIN_OPTIONS
3234 CURLOPT(LOGIN_OPTIONS
),
3236 HAVENOT(LOGIN_OPTIONS
),
3238 #if HAVE_DECL_CURLOPT_CONNECT_TO
3239 CURLOPT(CONNECT_TO
),
3241 HAVENOT(CONNECT_TO
),
3243 #if HAVE_DECL_CURLOPT_POSTREDIR
3248 #if HAVE_DECL_CURLOPT_MIMEPOST
3253 CURLOPT(SSH_KNOWNHOSTS
),
3254 CURLOPT(SSH_KEYFUNCTION
),
3257 value
caml_curl_easy_setopt(value conn
, value option
)
3259 CAMLparam2(conn
, option
);
3261 Connection
*connection
= Connection_val(conn
);
3262 CURLOptionMapping
* thisOption
= NULL
;
3263 static const value
* exception
= NULL
;
3265 checkConnection(connection
);
3267 data
= Field(option
, 0);
3269 if (Tag_val(option
) < sizeof(implementedOptionMap
)/sizeof(CURLOptionMapping
))
3271 thisOption
= &implementedOptionMap
[Tag_val(option
)];
3272 if (thisOption
->optionHandler
)
3274 thisOption
->optionHandler(connection
, data
);
3278 if (NULL
== exception
)
3280 exception
= caml_named_value("Curl.NotImplemented");
3281 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented");
3284 caml_raise_with_string(*exception
, thisOption
->name
);
3289 caml_failwith("Invalid CURLOPT Option");
3292 CAMLreturn(Val_unit
);
3296 ** curl_easy_perform helper function
3299 value
caml_curl_easy_perform(value conn
)
3302 CURLcode result
= CURLE_OK
;
3303 Connection
*connection
= Connection_val(conn
);
3305 checkConnection(connection
);
3307 caml_enter_blocking_section();
3308 result
= curl_easy_perform(connection
->handle
);
3309 caml_leave_blocking_section();
3311 if (result
!= CURLE_OK
)
3312 raiseError(connection
, result
);
3314 CAMLreturn(Val_unit
);
3318 ** curl_easy_cleanup helper function
3321 value
caml_curl_easy_cleanup(value conn
)
3324 Connection
*connection
= Connection_val(conn
);
3326 checkConnection(connection
);
3328 removeConnection(connection
, 0);
3330 CAMLreturn(Val_unit
);
3334 ** curl_easy_getinfo helper function
3337 enum GetInfoResultType
{
3338 StringValue
, LongValue
, DoubleValue
, StringListValue
, StringListListValue
,
3339 OCamlValue
, /* keep last - no matching OCaml CURLINFO_ constructor */
3342 value
convertStringList(struct curl_slist
*p
)
3345 CAMLlocal3(result
, current
, next
);
3347 result
= Val_emptylist
;
3348 current
= Val_emptylist
;
3349 next
= Val_emptylist
;
3353 next
= caml_alloc_tuple(2);
3354 Store_field(next
, 0, caml_copy_string(p
->data
));
3355 Store_field(next
, 1, Val_emptylist
);
3357 if (result
== Val_emptylist
)
3360 if (current
!= Val_emptylist
)
3361 Store_field(current
, 1, next
);
3371 value
caml_curl_easy_getinfo(value conn
, value option
)
3373 CAMLparam2(conn
, option
);
3374 CAMLlocal3(result
, current
, next
);
3375 CURLcode curlResult
;
3376 Connection
*connection
= Connection_val(conn
);
3377 enum GetInfoResultType resultType
;
3378 char *strValue
= NULL
;
3381 struct curl_slist
*stringListValue
= NULL
;
3382 #if HAVE_DECL_CURLINFO_CERTINFO
3385 struct curl_slist
*to_info
;
3386 struct curl_certinfo
*to_certinfo
;
3390 checkConnection(connection
);
3392 switch(Long_val(option
))
3394 #if HAVE_DECL_CURLINFO_EFFECTIVE_URL
3395 case 0: /* CURLINFO_EFFECTIVE_URL */
3396 resultType
= StringValue
;
3398 curlResult
= curl_easy_getinfo(connection
->handle
,
3399 CURLINFO_EFFECTIVE_URL
,
3403 #pragma message("libcurl does not provide CURLINFO_EFFECTIVE_URL")
3406 #if HAVE_DECL_CURLINFO_RESPONSE_CODE || HAVE_DECL_CURLINFO_HTTP_CODE
3407 case 1: /* CURLINFO_HTTP_CODE */
3408 case 2: /* CURLINFO_RESPONSE_CODE */
3409 #if HAVE_DECL_CURLINFO_RESPONSE_CODE
3410 resultType
= LongValue
;
3412 curlResult
= curl_easy_getinfo(connection
->handle
,
3413 CURLINFO_RESPONSE_CODE
,
3416 resultType
= LongValue
;
3418 curlResult
= curl_easy_getinfo(connection
->handle
,
3425 #if HAVE_DECL_CURLINFO_TOTAL_TIME
3426 case 3: /* CURLINFO_TOTAL_TIME */
3427 resultType
= DoubleValue
;
3429 curlResult
= curl_easy_getinfo(connection
->handle
,
3430 CURLINFO_TOTAL_TIME
,
3435 #if HAVE_DECL_CURLINFO_NAMELOOKUP_TIME
3436 case 4: /* CURLINFO_NAMELOOKUP_TIME */
3437 resultType
= DoubleValue
;
3439 curlResult
= curl_easy_getinfo(connection
->handle
,
3440 CURLINFO_NAMELOOKUP_TIME
,
3445 #if HAVE_DECL_CURLINFO_CONNECT_TIME
3446 case 5: /* CURLINFO_CONNECT_TIME */
3447 resultType
= DoubleValue
;
3449 curlResult
= curl_easy_getinfo(connection
->handle
,
3450 CURLINFO_CONNECT_TIME
,
3455 #if HAVE_DECL_CURLINFO_PRETRANSFER_TIME
3456 case 6: /* CURLINFO_PRETRANSFER_TIME */
3457 resultType
= DoubleValue
;
3459 curlResult
= curl_easy_getinfo(connection
->handle
,
3460 CURLINFO_PRETRANSFER_TIME
,
3465 #if HAVE_DECL_CURLINFO_SIZE_UPLOAD
3466 case 7: /* CURLINFO_SIZE_UPLOAD */
3467 resultType
= DoubleValue
;
3469 curlResult
= curl_easy_getinfo(connection
->handle
,
3470 CURLINFO_SIZE_UPLOAD
,
3475 #if HAVE_DECL_CURLINFO_SIZE_DOWNLOAD
3476 case 8: /* CURLINFO_SIZE_DOWNLOAD */
3477 resultType
= DoubleValue
;
3479 curlResult
= curl_easy_getinfo(connection
->handle
,
3480 CURLINFO_SIZE_DOWNLOAD
,
3485 #if HAVE_DECL_CURLINFO_SPEED_DOWNLOAD
3486 case 9: /* CURLINFO_SPEED_DOWNLOAD */
3487 resultType
= DoubleValue
;
3489 curlResult
= curl_easy_getinfo(connection
->handle
,
3490 CURLINFO_SPEED_DOWNLOAD
,
3495 #if HAVE_DECL_CURLINFO_SPEED_UPLOAD
3496 case 10: /* CURLINFO_SPEED_UPLOAD */
3497 resultType
= DoubleValue
;
3499 curlResult
= curl_easy_getinfo(connection
->handle
,
3500 CURLINFO_SPEED_UPLOAD
,
3506 #if HAVE_DECL_CURLINFO_HEADER_SIZE
3507 case 11: /* CURLINFO_HEADER_SIZE */
3508 resultType
= LongValue
;
3510 curlResult
= curl_easy_getinfo(connection
->handle
,
3511 CURLINFO_HEADER_SIZE
,
3516 #if HAVE_DECL_CURLINFO_REQUEST_SIZE
3517 case 12: /* CURLINFO_REQUEST_SIZE */
3518 resultType
= LongValue
;
3520 curlResult
= curl_easy_getinfo(connection
->handle
,
3521 CURLINFO_REQUEST_SIZE
,
3526 #if HAVE_DECL_CURLINFO_SSL_VERIFYRESULT
3527 case 13: /* CURLINFO_SSL_VERIFYRESULT */
3528 resultType
= LongValue
;
3530 curlResult
= curl_easy_getinfo(connection
->handle
,
3531 CURLINFO_SSL_VERIFYRESULT
,
3536 #if HAVE_DECL_CURLINFO_FILETIME
3537 case 14: /* CURLINFO_FILETIME */
3538 resultType
= DoubleValue
;
3540 curlResult
= curl_easy_getinfo(connection
->handle
,
3544 doubleValue
= longValue
;
3548 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_DOWNLOAD
3549 case 15: /* CURLINFO_CONTENT_LENGTH_DOWNLOAD */
3550 resultType
= DoubleValue
;
3552 curlResult
= curl_easy_getinfo(connection
->handle
,
3553 CURLINFO_CONTENT_LENGTH_DOWNLOAD
,
3558 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_UPLOAD
3559 case 16: /* CURLINFO_CONTENT_LENGTH_UPLOAD */
3560 resultType
= DoubleValue
;
3562 curlResult
= curl_easy_getinfo(connection
->handle
,
3563 CURLINFO_CONTENT_LENGTH_UPLOAD
,
3568 #if HAVE_DECL_CURLINFO_STARTTRANSFER_TIME
3569 case 17: /* CURLINFO_STARTTRANSFER_TIME */
3570 resultType
= DoubleValue
;
3572 curlResult
= curl_easy_getinfo(connection
->handle
,
3573 CURLINFO_STARTTRANSFER_TIME
,
3578 #if HAVE_DECL_CURLINFO_CONTENT_TYPE
3579 case 18: /* CURLINFO_CONTENT_TYPE */
3580 resultType
= StringValue
;
3582 curlResult
= curl_easy_getinfo(connection
->handle
,
3583 CURLINFO_CONTENT_TYPE
,
3588 #if HAVE_DECL_CURLINFO_REDIRECT_TIME
3589 case 19: /* CURLINFO_REDIRECT_TIME */
3590 resultType
= DoubleValue
;
3592 curlResult
= curl_easy_getinfo(connection
->handle
,
3593 CURLINFO_REDIRECT_TIME
,
3598 #if HAVE_DECL_CURLINFO_REDIRECT_COUNT
3599 case 20: /* CURLINFO_REDIRECT_COUNT */
3600 resultType
= LongValue
;
3602 curlResult
= curl_easy_getinfo(connection
->handle
,
3603 CURLINFO_REDIRECT_COUNT
,
3608 case 21: /* CURLINFO_PRIVATE */
3609 resultType
= OCamlValue
;
3610 curlResult
= CURLE_OK
;
3611 result
= caml_alloc(1, StringValue
);
3612 Store_field(result
, 0, Field(connection
->ocamlValues
, Ocaml_PRIVATE
));
3615 #if HAVE_DECL_CURLINFO_HTTP_CONNECTCODE
3616 case 22: /* CURLINFO_HTTP_CONNECTCODE */
3617 resultType
= LongValue
;
3619 curlResult
= curl_easy_getinfo(connection
->handle
,
3620 CURLINFO_HTTP_CONNECTCODE
,
3625 #if HAVE_DECL_CURLINFO_HTTPAUTH_AVAIL
3626 case 23: /* CURLINFO_HTTPAUTH_AVAIL */
3627 resultType
= LongValue
;
3629 curlResult
= curl_easy_getinfo(connection
->handle
,
3630 CURLINFO_HTTPAUTH_AVAIL
,
3635 #if HAVE_DECL_CURLINFO_PROXYAUTH_AVAIL
3636 case 24: /* CURLINFO_PROXYAUTH_AVAIL */
3637 resultType
= LongValue
;
3639 curlResult
= curl_easy_getinfo(connection
->handle
,
3640 CURLINFO_PROXYAUTH_AVAIL
,
3645 #if HAVE_DECL_CURLINFO_OS_ERRNO
3646 case 25: /* CURLINFO_OS_ERRNO */
3647 resultType
= LongValue
;
3649 curlResult
= curl_easy_getinfo(connection
->handle
,
3655 #if HAVE_DECL_CURLINFO_NUM_CONNECTS
3656 case 26: /* CURLINFO_NUM_CONNECTS */
3657 resultType
= LongValue
;
3659 curlResult
= curl_easy_getinfo(connection
->handle
,
3660 CURLINFO_NUM_CONNECTS
,
3665 #if HAVE_DECL_CURLINFO_SSL_ENGINES
3666 case 27: /* CURLINFO_SSL_ENGINES */
3667 resultType
= StringListValue
;
3669 curlResult
= curl_easy_getinfo(connection
->handle
,
3670 CURLINFO_SSL_ENGINES
,
3675 #if HAVE_DECL_CURLINFO_COOKIELIST
3676 case 28: /* CURLINFO_COOKIELIST */
3677 resultType
= StringListValue
;
3679 curlResult
= curl_easy_getinfo(connection
->handle
,
3680 CURLINFO_COOKIELIST
,
3685 #if HAVE_DECL_CURLINFO_LASTSOCKET
3686 case 29: /* CURLINFO_LASTSOCKET */
3687 resultType
= LongValue
;
3689 curlResult
= curl_easy_getinfo(connection
->handle
,
3690 CURLINFO_LASTSOCKET
,
3695 #if HAVE_DECL_CURLINFO_FTP_ENTRY_PATH
3696 case 30: /* CURLINFO_FTP_ENTRY_PATH */
3697 resultType
= StringValue
;
3699 curlResult
= curl_easy_getinfo(connection
->handle
,
3700 CURLINFO_FTP_ENTRY_PATH
,
3705 #if HAVE_DECL_CURLINFO_REDIRECT_URL
3706 case 31: /* CURLINFO_REDIRECT_URL */
3707 resultType
= StringValue
;
3709 curlResult
= curl_easy_getinfo(connection
->handle
,
3710 CURLINFO_REDIRECT_URL
,
3714 #pragma message("libcurl does not provide CURLINFO_REDIRECT_URL")
3717 #if HAVE_DECL_CURLINFO_PRIMARY_IP
3718 case 32: /* CURLINFO_PRIMARY_IP */
3719 resultType
= StringValue
;
3721 curlResult
= curl_easy_getinfo(connection
->handle
,
3722 CURLINFO_PRIMARY_IP
,
3726 #pragma message("libcurl does not provide CURLINFO_PRIMARY_IP")
3729 #if HAVE_DECL_CURLINFO_LOCAL_IP
3730 case 33: /* CURLINFO_LOCAL_IP */
3731 resultType
= StringValue
;
3733 curlResult
= curl_easy_getinfo(connection
->handle
,
3738 #pragma message("libcurl does not provide CURLINFO_LOCAL_IP")
3741 #if HAVE_DECL_CURLINFO_LOCAL_PORT
3742 case 34: /* CURLINFO_LOCAL_PORT */
3743 resultType
= LongValue
;
3745 curlResult
= curl_easy_getinfo(connection
->handle
,
3746 CURLINFO_LOCAL_PORT
,
3750 #pragma message("libcurl does not provide CURLINFO_LOCAL_PORT")
3753 #if HAVE_DECL_CURLINFO_CONDITION_UNMET
3754 case 35: /* CURLINFO_CONDITION_UNMET */
3755 resultType
= LongValue
;
3757 curlResult
= curl_easy_getinfo(connection
->handle
,
3758 CURLINFO_CONDITION_UNMET
,
3762 #pragma message("libcurl does not provide CURLINFO_CONDITION_UNMET")
3764 #if HAVE_DECL_CURLINFO_CERTINFO
3765 case 36: /* CURLINFO_CERTINFO */
3766 resultType
= StringListListValue
;
3768 curlResult
= curl_easy_getinfo(connection
->handle
,
3772 result
= Val_emptylist
;
3773 current
= Val_emptylist
;
3774 next
= Val_emptylist
;
3776 if (curlResult
!= CURLE_OK
|| !ptr
.to_info
)
3779 for (i
= 0; i
< ptr
.to_certinfo
->num_of_certs
; i
++) {
3780 next
= caml_alloc_tuple(2);
3781 Store_field(next
, 0, convertStringList(ptr
.to_certinfo
->certinfo
[i
]));
3782 Store_field(next
, 1, current
);
3787 #pragma message("libcurl does not provide CURLINFO_CERTINFO")
3790 caml_failwith("Invalid CURLINFO Option");
3794 if (curlResult
!= CURLE_OK
)
3795 raiseError(connection
, curlResult
);
3800 result
= caml_alloc(1, StringValue
);
3801 Store_field(result
, 0, caml_copy_string(strValue
?strValue
:""));
3805 result
= caml_alloc(1, LongValue
);
3806 Store_field(result
, 0, Val_long(longValue
));
3810 result
= caml_alloc(1, DoubleValue
);
3811 Store_field(result
, 0, caml_copy_double(doubleValue
));
3814 case StringListValue
:
3815 result
= caml_alloc(1, StringListValue
);
3816 Store_field(result
, 0, convertStringList(stringListValue
));
3817 curl_slist_free_all(stringListValue
);
3820 case StringListListValue
:
3821 result
= caml_alloc(1, StringListListValue
);
3822 Store_field(result
, 0, current
);
3833 ** curl_escape helper function
3836 value
caml_curl_escape(value str
)
3842 curlResult
= curl_escape(String_val(str
), caml_string_length(str
));
3843 result
= caml_copy_string(curlResult
);
3850 ** curl_unescape helper function
3853 value
caml_curl_unescape(value str
)
3859 curlResult
= curl_unescape(String_val(str
), caml_string_length(str
));
3860 result
= caml_copy_string(curlResult
);
3867 ** curl_getdate helper function
3870 value
caml_curl_getdate(value str
, value now
)
3872 CAMLparam2(str
, now
);
3877 curlNow
= (time_t)Double_val(now
);
3878 curlResult
= curl_getdate(String_val(str
), &curlNow
);
3879 result
= caml_copy_double((double)curlResult
);
3885 ** curl_version helper function
3888 value
caml_curl_version(void)
3894 str
= curl_version();
3895 result
= caml_copy_string(str
);
3900 struct CURLVersionBitsMapping
3906 struct CURLVersionBitsMapping versionBitsMap
[] =
3908 {CURL_VERSION_IPV6
, "ipv6"},
3909 {CURL_VERSION_KERBEROS4
, "kerberos4"},
3910 {CURL_VERSION_SSL
, "ssl"},
3911 {CURL_VERSION_LIBZ
, "libz"},
3912 {CURL_VERSION_NTLM
, "ntlm"},
3913 {CURL_VERSION_GSSNEGOTIATE
, "gssnegotiate"},
3914 {CURL_VERSION_DEBUG
, "debug"},
3915 {CURL_VERSION_CURLDEBUG
, "curldebug"},
3916 {CURL_VERSION_ASYNCHDNS
, "asynchdns"},
3917 {CURL_VERSION_SPNEGO
, "spnego"},
3918 {CURL_VERSION_LARGEFILE
, "largefile"},
3919 {CURL_VERSION_IDN
, "idn"},
3920 {CURL_VERSION_SSPI
, "sspi"},
3921 {CURL_VERSION_CONV
, "conv"},
3922 #if HAVE_DECL_CURL_VERSION_TLSAUTH_SRP
3923 {CURL_VERSION_TLSAUTH_SRP
, "srp"},
3925 #if HAVE_DECL_CURL_VERSION_NTLM_WB
3926 {CURL_VERSION_NTLM_WB
, "wb"},
3930 value
caml_curl_version_info(value unit
)
3933 CAMLlocal4(v
, vlist
, vnum
, vfeatures
);
3934 const char* const* p
= NULL
;
3937 curl_version_info_data
* data
= curl_version_info(CURLVERSION_NOW
);
3938 if (NULL
== data
) caml_failwith("curl_version_info");
3940 vlist
= Val_emptylist
;
3941 for (p
= data
->protocols
; NULL
!= *p
; p
++)
3943 vlist
= Val_cons(vlist
, caml_copy_string(*p
));
3946 vfeatures
= Val_emptylist
;
3947 for (i
= 0; i
< sizeof(versionBitsMap
)/sizeof(versionBitsMap
[0]); i
++)
3949 if (0 != (versionBitsMap
[i
].code
& data
->features
))
3950 vfeatures
= Val_cons(vfeatures
, caml_copy_string(versionBitsMap
[i
].name
));
3953 vnum
= caml_alloc_tuple(3);
3954 Store_field(vnum
,0,Val_int(0xFF & (data
->version_num
>> 16)));
3955 Store_field(vnum
,1,Val_int(0xFF & (data
->version_num
>> 8)));
3956 Store_field(vnum
,2,Val_int(0xFF & (data
->version_num
)));
3958 v
= caml_alloc_tuple(12);
3959 Store_field(v
,0,caml_copy_string(data
->version
));
3960 Store_field(v
,1,vnum
);
3961 Store_field(v
,2,caml_copy_string(data
->host
));
3962 Store_field(v
,3,vfeatures
);
3963 Store_field(v
,4,data
->ssl_version
? Val_some(caml_copy_string(data
->ssl_version
)) : Val_none
);
3964 Store_field(v
,5,data
->libz_version
? Val_some(caml_copy_string(data
->libz_version
)) : Val_none
);
3965 Store_field(v
,6,vlist
);
3966 Store_field(v
,7,caml_copy_string((data
->age
>= 1 && data
->ares
) ? data
->ares
: ""));
3967 Store_field(v
,8,Val_int((data
->age
>= 1) ? data
->ares_num
: 0));
3968 Store_field(v
,9,caml_copy_string((data
->age
>= 2 && data
->libidn
) ? data
->libidn
: ""));
3969 Store_field(v
,10,Val_int((data
->age
>= 3) ? data
->iconv_ver_num
: 0));
3970 Store_field(v
,11,caml_copy_string((data
->age
>= 3 && data
->libssh_version
) ? data
->libssh_version
: ""));
3975 value
caml_curl_pause(value conn
, value opts
)
3977 CAMLparam2(conn
, opts
);
3978 CAMLlocal4(v
, vlist
, vnum
, vfeatures
);
3979 Connection
*connection
= Connection_val(conn
);
3983 while (Val_emptylist
!= opts
)
3985 switch (Int_val(Field(opts
,0)))
3987 case 0: bitmask
|= CURLPAUSE_SEND
; break;
3988 case 1: bitmask
|= CURLPAUSE_RECV
; break;
3989 case 2: bitmask
|= CURLPAUSE_ALL
; break;
3990 default: caml_failwith("wrong pauseOption");
3992 opts
= Field(opts
,1);
3995 caml_enter_blocking_section();
3996 result
= curl_easy_pause(connection
->handle
,bitmask
);
3997 caml_leave_blocking_section();
3999 if (result
!= CURLE_OK
)
4000 raiseError(connection
, result
);
4002 CAMLreturn(Val_unit
);
4006 * Curl multi stack support
4008 * Exported thin wrappers for libcurl are prefixed with caml_curl_multi_.
4009 * Other exported functions are prefixed with caml_curlm_, some of them
4010 * can/should be decomposed into smaller parts.
4013 struct ml_multi_handle
4016 value values
; /* callbacks */
4021 curlmopt_socket_function
,
4022 curlmopt_timer_function
,
4024 /* last, not used */
4028 typedef struct ml_multi_handle ml_multi_handle
;
4030 #define Multi_val(v) (*(ml_multi_handle**)Data_custom_val(v))
4031 #define CURLM_val(v) (Multi_val(v)->handle)
4033 static struct custom_operations curl_multi_ops
= {
4035 custom_finalize_default
,
4036 custom_compare_default
,
4037 custom_hash_default
,
4038 custom_serialize_default
,
4039 custom_deserialize_default
,
4040 #if defined(custom_compare_ext_default)
4041 custom_compare_ext_default
,
4045 static void raise_multi_error(char const* msg
)
4047 static const value
* exception
= NULL
;
4049 if (NULL
== exception
)
4051 exception
= caml_named_value("Curl.Multi.Error");
4052 if (NULL
== exception
) caml_invalid_argument("Curl.Multi.Error");
4055 caml_raise_with_string(*exception
, msg
);
4058 static void raise_multi_cerror(char const* func
, CURLMcode code
)
4062 static const value
* exception
= NULL
;
4064 if (NULL
== exception
)
4066 exception
= caml_named_value("Curl.Multi.CError");
4067 if (NULL
== exception
) caml_invalid_argument("Curl.Multi.CError");
4070 data
= caml_alloc_tuple(3);
4072 Store_field(data
, 0, caml_copy_string(func
));
4073 Store_field(data
, 1, Val_int(code
));
4074 Store_field(data
, 2, caml_copy_string(curl_multi_strerror(code
)));
4076 caml_raise_with_arg(*exception
, data
);
4081 static void check_mcode(char const* func
, CURLMcode code
)
4083 if (code
!= CURLM_OK
) raise_multi_cerror(func
,code
);
4086 value
caml_curl_multi_init(value unit
)
4090 ml_multi_handle
* multi
= (ml_multi_handle
*)caml_stat_alloc(sizeof(ml_multi_handle
));
4091 CURLM
* h
= curl_multi_init();
4095 caml_stat_free(multi
);
4096 raise_multi_error("caml_curl_multi_init");
4100 multi
->values
= caml_alloc(multi_values_total
, 0);
4101 caml_register_generational_global_root(&multi
->values
);
4103 v
= caml_alloc_custom(&curl_multi_ops
, sizeof(ml_multi_handle
*), 0, 1);
4104 Multi_val(v
) = multi
;
4109 value
caml_curl_multi_cleanup(value handle
)
4112 ml_multi_handle
* h
= Multi_val(handle
);
4115 CAMLreturn(Val_unit
);
4117 caml_remove_generational_global_root(&h
->values
);
4119 CURLcode rc
= curl_multi_cleanup(h
->handle
);
4122 Multi_val(handle
) = (ml_multi_handle
*)NULL
;
4124 check_mcode("curl_multi_cleanup",rc
);
4126 CAMLreturn(Val_unit
);
4129 static CURL
* curlm_remove_finished(CURLM
* multi_handle
, CURLcode
* result
)
4131 int msgs_in_queue
= 0;
4135 CURLMsg
* msg
= curl_multi_info_read(multi_handle
, &msgs_in_queue
);
4136 if (NULL
== msg
) return NULL
;
4137 if (CURLMSG_DONE
== msg
->msg
)
4139 CURL
* easy_handle
= msg
->easy_handle
;
4140 if (result
) *result
= msg
->data
.result
;
4141 if (CURLM_OK
!= curl_multi_remove_handle(multi_handle
, easy_handle
))
4143 /*caml_failwith("curlm_remove_finished");*/
4150 value
caml_curlm_remove_finished(value v_multi
)
4152 CAMLparam1(v_multi
);
4153 CAMLlocal2(v_easy
, v_tuple
);
4155 CURLM
* multi_handle
;
4157 Connection
* conn
= NULL
;
4159 multi_handle
= CURLM_val(v_multi
);
4161 caml_enter_blocking_section();
4162 handle
= curlm_remove_finished(multi_handle
,&result
);
4163 caml_leave_blocking_section();
4167 CAMLreturn(Val_none
);
4171 conn
= getConnection(handle
);
4172 if (conn
->curl_ERRORBUFFER
!= NULL
)
4174 Store_field(Field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
), 0, caml_copy_string(conn
->curl_ERRORBUFFER
));
4177 /* NB: same handle, but different block */
4178 v_easy
= caml_curl_alloc(conn
);
4179 v_tuple
= caml_alloc(2, 0);
4180 Store_field(v_tuple
,0,v_easy
);
4181 Store_field(v_tuple
,1,Val_int(result
)); /* CURLcode */
4182 CAMLreturn(Val_some(v_tuple
));
4186 value
caml_curl_multi_wait(value v_timeout_ms
, value v_multi
)
4188 CAMLparam2(v_timeout_ms
,v_multi
);
4189 CURLM
*multi_handle
= CURLM_val(v_multi
);
4190 int timeout_ms
= Int_val(v_timeout_ms
);
4194 caml_enter_blocking_section();
4195 rc
= curl_multi_wait(multi_handle
, NULL
, 0, timeout_ms
, &numfds
);
4196 caml_leave_blocking_section();
4198 check_mcode("curl_multi_wait",rc
);
4200 CAMLreturn(Val_bool(numfds
!= 0));
4203 value
caml_curl_multi_add_handle(value v_multi
, value v_easy
)
4205 CAMLparam2(v_multi
,v_easy
);
4206 CURLMcode rc
= CURLM_OK
;
4207 CURLM
* multi
= CURLM_val(v_multi
);
4208 Connection
* conn
= Connection_val(v_easy
);
4210 /* prevent collection of OCaml value while the easy handle is used
4211 and may invoke callbacks registered on OCaml side */
4214 /* may invoke callbacks so need to be consistent with locks */
4215 caml_enter_blocking_section();
4216 rc
= curl_multi_add_handle(multi
, conn
->handle
);
4219 conn
->refcount
--; /* not added, revert */
4220 caml_leave_blocking_section();
4221 check_mcode("curl_multi_add_handle",rc
);
4223 caml_leave_blocking_section();
4225 CAMLreturn(Val_unit
);
4228 value
caml_curl_multi_remove_handle(value v_multi
, value v_easy
)
4230 CAMLparam2(v_multi
,v_easy
);
4231 CURLM
* multi
= CURLM_val(v_multi
);
4232 CURLMcode rc
= CURLM_OK
;
4233 Connection
* conn
= Connection_val(v_easy
);
4235 /* may invoke callbacks so need to be consistent with locks */
4236 caml_enter_blocking_section();
4237 rc
= curl_multi_remove_handle(multi
, conn
->handle
);
4239 caml_leave_blocking_section();
4240 check_mcode("curl_multi_remove_handle",rc
);
4242 CAMLreturn(Val_unit
);
4245 value
caml_curl_multi_perform_all(value v_multi
)
4247 CAMLparam1(v_multi
);
4248 int still_running
= 0;
4249 CURLM
* h
= CURLM_val(v_multi
);
4251 caml_enter_blocking_section();
4252 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(h
, &still_running
));
4253 caml_leave_blocking_section();
4255 CAMLreturn(Val_int(still_running
));
4258 /* currently curlCode repr tag matches CURLE_ value
4259 * this is exploited below, but generally should use errorMap */
4261 value
caml_curl_strerror(value v_code
)
4264 CAMLreturn(caml_copy_string(curl_easy_strerror((CURLcode
)Int_val(v_code
))));
4267 value
caml_curl_int_of_curlCode(value v_code
)
4272 value
caml_curl_curlCode_of_int(value v
)
4274 return (Int_val(v
) < sizeof(errorMap
) / sizeof(errorMap
[0]) ? Val_some(v
) : Val_none
);
4278 * Wrappers for the curl_multi_socket_action infrastructure
4279 * Based on curl hiperfifo.c example
4284 #define Val_socket(v) win_alloc_socket(v)
4287 #error Socket_val not defined in unixsupport.h
4291 #define Socket_val(v) Long_val(v)
4294 #define Val_socket(v) Val_int(v)
4298 value
caml_curl_multi_socket_action(value v_multi
, value v_fd
, value v_kind
)
4300 CAMLparam3(v_multi
, v_fd
, v_kind
);
4301 CURLM
* h
= CURLM_val(v_multi
);
4302 int still_running
= 0;
4303 CURLMcode rc
= CURLM_OK
;
4304 curl_socket_t socket
;
4307 if (Val_none
== v_fd
)
4309 socket
= CURL_SOCKET_TIMEOUT
;
4313 socket
= Socket_val(Field(v_fd
, 0));
4316 switch (Int_val(v_kind
))
4319 case 1 : kind
|= CURL_CSELECT_IN
; break;
4320 case 2 : kind
|= CURL_CSELECT_OUT
; break;
4321 case 3 : kind
|= CURL_CSELECT_IN
| CURL_CSELECT_OUT
; break;
4323 raise_multi_error("caml_curl_multi_socket_action");
4326 /* fprintf(stdout,"fd %u kind %u\n",socket, kind); fflush(stdout); */
4328 caml_enter_blocking_section();
4330 rc
= curl_multi_socket_action(h
, socket
, kind
, &still_running
);
4331 } while (rc
== CURLM_CALL_MULTI_PERFORM
);
4332 caml_leave_blocking_section();
4334 check_mcode("curl_multi_socket_action",rc
);
4336 CAMLreturn(Val_int(still_running
));
4339 value
caml_curl_multi_socket_all(value v_multi
)
4341 CAMLparam1(v_multi
);
4342 int still_running
= 0;
4343 CURLMcode rc
= CURLM_OK
;
4344 CURLM
* h
= CURLM_val(v_multi
);
4346 caml_enter_blocking_section();
4348 rc
= curl_multi_socket_all(h
, &still_running
);
4349 } while (rc
== CURLM_CALL_MULTI_PERFORM
);
4350 caml_leave_blocking_section();
4352 check_mcode("curl_multi_socket_all",rc
);
4354 CAMLreturn(Val_int(still_running
));
4357 static int curlm_sock_cb(CURL
*e
, curl_socket_t sock
, int what
, void *cbp
, void *sockp
)
4359 caml_leave_blocking_section();
4362 CAMLlocal2(v_what
,csock
);
4364 (void)sockp
; /* not used */
4366 /* v_what = Val_int(what); */
4369 case CURL_POLL_NONE
: v_what
= Val_int(0); break;
4370 case CURL_POLL_IN
: v_what
= Val_int(1); break;
4371 case CURL_POLL_OUT
: v_what
= Val_int(2); break;
4372 case CURL_POLL_INOUT
: v_what
= Val_int(3); break;
4373 case CURL_POLL_REMOVE
: v_what
= Val_int(4); break;
4375 fprintf(stderr
, "curlm_sock_cb sock=%lld what=%d\n", (long long)sock
, what
);
4377 raise_multi_error("curlm_sock_cb"); /* FIXME exception from callback */
4379 csock
=Val_socket(sock
);
4380 caml_callback2(Field(((ml_multi_handle
*)cbp
)->values
,curlmopt_socket_function
),
4384 caml_enter_blocking_section();
4388 value
caml_curl_multi_socketfunction(value v_multi
, value v_cb
)
4390 CAMLparam2(v_multi
, v_cb
);
4391 ml_multi_handle
* multi
= Multi_val(v_multi
);
4393 Store_field(multi
->values
, curlmopt_socket_function
, v_cb
);
4395 curl_multi_setopt(multi
->handle
, CURLMOPT_SOCKETFUNCTION
, curlm_sock_cb
);
4396 curl_multi_setopt(multi
->handle
, CURLMOPT_SOCKETDATA
, multi
);
4398 CAMLreturn(Val_unit
);
4401 static int curlm_timer_cb(CURLM
*multi
, long timeout_ms
, void *userp
)
4403 caml_leave_blocking_section();
4407 caml_callback(Field(((ml_multi_handle
*)userp
)->values
,curlmopt_timer_function
), Val_long(timeout_ms
));
4410 caml_enter_blocking_section();
4414 value
caml_curl_multi_timerfunction(value v_multi
, value v_cb
)
4416 CAMLparam2(v_multi
, v_cb
);
4417 ml_multi_handle
* multi
= Multi_val(v_multi
);
4419 Store_field(multi
->values
, curlmopt_timer_function
, v_cb
);
4421 curl_multi_setopt(multi
->handle
, CURLMOPT_TIMERFUNCTION
, curlm_timer_cb
);
4422 curl_multi_setopt(multi
->handle
, CURLMOPT_TIMERDATA
, multi
);
4424 CAMLreturn(Val_unit
);
4427 value
caml_curl_multi_timeout(value v_multi
)
4429 CAMLparam1(v_multi
);
4431 CURLMcode rc
= CURLM_OK
;
4432 ml_multi_handle
* multi
= Multi_val(v_multi
);
4434 rc
= curl_multi_timeout(multi
->handle
, &ms
);
4436 check_mcode("curl_multi_timeout",rc
);
4438 CAMLreturn(Val_long(ms
));
4441 #define SETMOPT_VAL_(func_name, curl_option, conv_val) \
4442 static void func_name(CURLM *handle, value option) \
4444 CAMLparam1(option); \
4445 CURLMcode result = CURLM_OK; \
4447 result = curl_multi_setopt(handle, curl_option, conv_val(option)); \
4449 check_mcode("curl_multi_setopt "#curl_option,result); \
4454 #define SETMOPT_VAL(name, conv) SETMOPT_VAL_(handle_multi_##name, CURLMOPT_##name, conv)
4455 #define SETMOPT_BOOL(name) SETMOPT_VAL(name, Bool_val)
4456 #define SETMOPT_LONG(name) SETMOPT_VAL(name, Long_val)
4457 #define SETMOPT_INT64(name) SETMOPT_VAL(name, Int64_val)
4459 long pipeliningMap
[] =
4461 0, /* CURLPIPE_NOTHING */
4462 1, /* CURLPIPE_HTTP1 */
4463 2, /* CURLPIPE_MULTIPLEX */
4466 static void handle_multi_PIPELINING(CURLM
* handle
, value option
)
4469 CURLMcode result
= CURLM_OK
;
4471 long bits
= convert_bit_list(pipeliningMap
, sizeof(pipeliningMap
) / sizeof(pipeliningMap
[0]), option
);
4473 result
= curl_multi_setopt(handle
, CURLMOPT_PIPELINING
, bits
);
4475 check_mcode("curl_multi_setopt CURLOPT_PIPELINING",result
);
4480 #if HAVE_DECL_CURLMOPT_MAXCONNECTS
4481 SETMOPT_LONG( MAXCONNECTS
)
4484 #if HAVE_DECL_CURLMOPT_MAX_PIPELINE_LENGTH
4485 SETMOPT_LONG( MAX_PIPELINE_LENGTH
)
4488 #if HAVE_DECL_CURLMOPT_MAX_HOST_CONNECTIONS
4489 SETMOPT_LONG( MAX_HOST_CONNECTIONS
)
4492 typedef struct CURLMOptionMapping CURLMOptionMapping
;
4493 #define OPT(name) { handle_multi_## name, "CURLMOPT_"#name}
4494 #define NO_OPT(name) { NULL, "CURLMOPT_"#name}
4496 struct CURLMOptionMapping
4498 void (*optionHandler
)(CURLM
*, value
);
4502 CURLMOptionMapping implementedMOptionMap
[] = {
4504 #if HAVE_DECL_CURLMOPT_MAXCONNECTS
4507 NO_OPT( MAXCONNECTS
),
4509 #if HAVE_DECL_CURLMOPT_MAX_PIPELINE_LENGTH
4510 OPT( MAX_PIPELINE_LENGTH
),
4512 NO_OPT( MAX_PIPELINE_LENGTH
),
4514 #if HAVE_DECL_CURLMOPT_MAX_HOST_CONNECTIONS
4515 OPT( MAX_HOST_CONNECTIONS
),
4517 NO_OPT( MAX_HOST_CONNECTIONS
),
4521 value
caml_curl_multi_setopt(value v_multi
, value option
)
4523 CAMLparam2(v_multi
, option
);
4525 CURLM
*handle
= Multi_val(v_multi
)->handle
;
4526 CURLMOptionMapping
* thisOption
= NULL
;
4527 static const value
* exception
= NULL
;
4529 data
= Field(option
, 0);
4531 if (Tag_val(option
) < sizeof(implementedMOptionMap
)/sizeof(CURLMOptionMapping
))
4533 thisOption
= &implementedMOptionMap
[Tag_val(option
)];
4534 if (thisOption
->optionHandler
)
4536 thisOption
->optionHandler(handle
, data
);
4540 if (NULL
== exception
)
4542 exception
= caml_named_value("Curl.NotImplemented");
4543 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented");
4546 caml_raise_with_string(*exception
, thisOption
->name
);
4551 raise_multi_error("Invalid CURLMOPT Option");
4554 CAMLreturn(Val_unit
);
4564 #define CURL_ENUM(name,last_used) { CURL_ ## name ## _ ## last_used, CURL_ ## name ## _LAST, #name }
4566 struct used_enum check_enums
[] = {
4567 { CURLINFO_SSL_DATA_OUT
, CURLINFO_END
, "DEBUGFUNCTION curl_infotype" },
4568 #if HAVE_DECL_CURL_HTTP_VERSION_3
4569 CURL_ENUM(HTTP_VERSION
, 3),
4571 #if HAVE_DECL_CURLINFO_CERTINFO
4572 { CURLINFO_CERTINFO
& CURLINFO_MASK
, CURLINFO_LASTONE
, "CURLINFO" },
4574 #if HAVE_DECL_CURLE_AGAIN
4575 { CURLE_AGAIN
, CURL_LAST
, "CURLcode" }
4579 value
caml_curl_check_enums(value v_unit
)
4584 size_t len
= sizeof(check_enums
) / sizeof(struct used_enum
);
4586 v_r
= caml_alloc_tuple(len
);
4588 for (i
= 0; i
< len
; i
++)
4590 v
= caml_alloc_tuple(3);
4591 Store_field(v
, 0, Val_int(check_enums
[i
].last_used
));
4592 Store_field(v
, 1, Val_int(check_enums
[i
].last
));
4593 Store_field(v
, 2, caml_copy_string(check_enums
[i
].name
));
4594 Store_field(v_r
, i
, v
);