simplify callbacks wrappers
[ocurl.git] / curl-helper.c
blob541b196e594ffb38483ee6ffc607d0b72ba83634
1 /***
2 *** curl-helper.c
3 ***
4 *** Copyright (c) 2003-2008, Lars Nilsson, <lars@quantumchamaeleon.com>
5 *** Copyright (c) 2009, ygrek, <ygrek@autistici.org>
6 ***/
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <unistd.h>
13 /* suppress false gcc warning on seekFunction */
14 #define CURL_DISABLE_TYPECHECK
15 #include <curl/curl.h>
17 #define CAML_NAME_SPACE
18 #include <caml/alloc.h>
19 #include <caml/memory.h>
20 #include <caml/mlvalues.h>
21 #include <caml/callback.h>
22 #include <caml/fail.h>
23 #include <caml/unixsupport.h>
24 #include <caml/custom.h>
25 #include <caml/threads.h>
27 #ifndef CAMLdrop
28 #define CAMLdrop caml_local_roots = caml__frame
29 #endif
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #else
34 #pragma message("No config file given.")
35 #endif
37 #define Val_none Val_int(0)
39 static __inline value
40 Val_some( value v )
42 CAMLparam1( v );
43 CAMLlocal1( some );
44 some = caml_alloc(1, 0);
45 Store_field( some, 0, v );
46 CAMLreturn( some );
49 static value Val_pair(value v1, value v2)
51 CAMLparam2(v1,v2);
52 CAMLlocal1(pair);
53 pair = caml_alloc_small(2,0);
54 Field(pair,0) = v1;
55 Field(pair,1) = v2;
56 CAMLreturn(pair);
59 static value Val_cons(value list, value v) { return Val_pair(v,list); }
61 typedef struct Connection Connection;
62 typedef struct ConnectionList ConnectionList;
64 #define Connection_val(v) (*(Connection**)Data_custom_val(v))
66 typedef enum OcamlValues
68 Ocaml_WRITEFUNCTION,
69 Ocaml_READFUNCTION,
70 Ocaml_ERRORBUFFER,
71 Ocaml_POSTFIELDS,
72 Ocaml_HTTPHEADER,
73 Ocaml_HTTPPOST,
74 Ocaml_QUOTE,
75 Ocaml_POSTQUOTE,
76 Ocaml_HEADERFUNCTION,
77 Ocaml_PROGRESSFUNCTION,
78 Ocaml_DEBUGFUNCTION,
79 Ocaml_HTTP200ALIASES,
80 Ocaml_IOCTLFUNCTION,
81 Ocaml_SEEKFUNCTION,
82 Ocaml_OPENSOCKETFUNCTION,
84 Ocaml_URL,
85 Ocaml_PROXY,
86 Ocaml_USERPWD,
87 Ocaml_PROXYUSERPWD,
88 Ocaml_RANGE,
89 Ocaml_REFERER,
90 Ocaml_USERAGENT,
91 Ocaml_FTPPORT,
92 Ocaml_COOKIE,
93 Ocaml_HTTPPOSTSTRINGS,
94 Ocaml_SSLCERT,
95 Ocaml_SSLCERTTYPE,
96 Ocaml_SSLCERTPASSWD,
97 Ocaml_SSLKEY,
98 Ocaml_SSLKEYTYPE,
99 Ocaml_SSLKEYPASSWD,
100 Ocaml_SSLENGINE,
101 Ocaml_COOKIEFILE,
102 Ocaml_CUSTOMREQUEST,
103 Ocaml_INTERFACE,
104 Ocaml_CAINFO,
105 Ocaml_CAPATH,
106 Ocaml_RANDOM_FILE,
107 Ocaml_EGDSOCKET,
108 Ocaml_COOKIEJAR,
109 Ocaml_SSL_CIPHER_LIST,
110 Ocaml_PRIVATE,
111 Ocaml_NETRC_FILE,
112 Ocaml_FTP_ACCOUNT,
113 Ocaml_COOKIELIST,
114 Ocaml_FTP_ALTERNATIVE_TO_USER,
115 Ocaml_SSH_PUBLIC_KEYFILE,
116 Ocaml_SSH_PRIVATE_KEYFILE,
117 Ocaml_SSH_HOST_PUBLIC_KEY_MD5,
118 Ocaml_COPYPOSTFIELDS,
119 Ocaml_DNS_SERVERS,
120 Ocaml_MAIL_FROM,
121 Ocaml_MAIL_RCPT,
122 Ocaml_RESOLVE,
124 /* Not used, last for size */
125 OcamlValuesSize
126 } OcamlValue;
128 struct Connection
130 CURL *connection;
131 Connection *next;
132 Connection *prev;
134 value ocamlValues;
136 size_t refcount; /* number of references to this structure */
138 char *curl_URL;
139 char *curl_PROXY;
140 char *curl_USERPWD;
141 char *curl_PROXYUSERPWD;
142 char *curl_RANGE;
143 char *curl_ERRORBUFFER;
144 char *curl_POSTFIELDS;
145 int curl_POSTFIELDSIZE;
146 char *curl_REFERER;
147 char *curl_USERAGENT;
148 char *curl_FTPPORT;
149 char *curl_COOKIE;
150 struct curl_slist *curl_HTTPHEADER;
151 struct curl_slist *httpPostBuffers;
152 struct curl_httppost *httpPostFirst;
153 struct curl_httppost *httpPostLast;
154 struct curl_slist *curl_RESOLVE;
155 char *curl_SSLCERT;
156 char *curl_SSLCERTTYPE;
157 char *curl_SSLCERTPASSWD;
158 char *curl_SSLKEY;
159 char *curl_SSLKEYTYPE;
160 char *curl_SSLKEYPASSWD;
161 char *curl_SSLENGINE;
162 struct curl_slist *curl_QUOTE;
163 struct curl_slist *curl_POSTQUOTE;
164 char *curl_COOKIEFILE;
165 char *curl_CUSTOMREQUEST;
166 char *curl_INTERFACE;
167 char *curl_CAINFO;
168 char *curl_CAPATH;
169 char *curl_RANDOM_FILE;
170 char *curl_EGDSOCKET;
171 char *curl_COOKIEJAR;
172 char *curl_SSL_CIPHER_LIST;
173 char *curl_PRIVATE;
174 struct curl_slist *curl_HTTP200ALIASES;
175 char *curl_NETRC_FILE;
176 char *curl_FTP_ACCOUNT;
177 char *curl_COOKIELIST;
178 char *curl_FTP_ALTERNATIVE_TO_USER;
179 char *curl_SSH_PUBLIC_KEYFILE;
180 char *curl_SSH_PRIVATE_KEYFILE;
181 char *curl_SSH_HOST_PUBLIC_KEY_MD5;
182 char *curl_COPYPOSTFIELDS;
183 char *curl_DNS_SERVERS;
184 char *curl_MAIL_FROM;
185 struct curl_slist *curl_MAIL_RCPT;
188 struct ConnectionList
190 Connection *head;
191 Connection *tail;
194 static ConnectionList connectionList = {NULL, NULL};
196 typedef struct CURLErrorMapping CURLErrorMapping;
198 struct CURLErrorMapping
200 char *name;
201 CURLcode error;
204 CURLErrorMapping errorMap[] =
206 {"CURLE_OK", CURLE_OK},
207 #if HAVE_DECL_CURLE_UNSUPPORTED_PROTOCOL
208 {"CURLE_UNSUPPORTED_PROTOCOL", CURLE_UNSUPPORTED_PROTOCOL},
209 #else
210 {"CURLE_UNSUPPORTED_PROTOCOL", -1},
211 #endif
212 #if HAVE_DECL_CURLE_FAILED_INIT
213 {"CURLE_FAILED_INIT", CURLE_FAILED_INIT},
214 #else
215 {"CURLE_FAILED_INIT", -1},
216 #endif
217 #if HAVE_DECL_CURLE_URL_MALFORMAT
218 {"CURLE_URL_MALFORMAT", CURLE_URL_MALFORMAT},
219 #else
220 {"CURLE_URL_MALFORMAT", -1},
221 #endif
222 #if HAVE_DECL_CURLE_URL_MALFORMAT_USER
223 {"CURLE_URL_MALFORMAT_USER", CURLE_URL_MALFORMAT_USER},
224 #else
225 {"CURLE_URL_MALFORMAT_USER", -1},
226 #endif
227 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_PROXY
228 {"CURLE_COULDNT_RESOLVE_PROXY", CURLE_COULDNT_RESOLVE_PROXY},
229 #else
230 {"CURLE_COULDNT_RESOLVE_PROXY", -1},
231 #endif
232 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_HOST
233 {"CURLE_COULDNT_RESOLVE_HOST", CURLE_COULDNT_RESOLVE_HOST},
234 #else
235 {"CURLE_COULDNT_RESOLVE_HOST", -1},
236 #endif
237 #if HAVE_DECL_CURLE_COULDNT_CONNECT
238 {"CURLE_COULDNT_CONNECT", CURLE_COULDNT_CONNECT},
239 #else
240 {"CURLE_COULDNT_CONNECT", -1},
241 #endif
242 #if HAVE_DECL_CURLE_FTP_WEIRD_SERVER_REPLY
243 {"CURLE_FTP_WEIRD_SERVER_REPLY", CURLE_FTP_WEIRD_SERVER_REPLY},
244 #else
245 {"CURLE_FTP_WEIRD_SERVER_REPLY", -1},
246 #endif
247 #if HAVE_DECL_CURLE_FTP_ACCESS_DENIED
248 {"CURLE_FTP_ACCESS_DENIED", CURLE_FTP_ACCESS_DENIED},
249 #else
250 {"CURLE_FTP_ACCESS_DENIED", -1},
251 #endif
252 #if HAVE_DECL_CURLE_FTP_USER_PASSWORD_INCORRECT
253 {"CURLE_FTP_USER_PASSWORD_INCORRECT", CURLE_FTP_USER_PASSWORD_INCORRECT},
254 #else
255 {"CURLE_FTP_USER_PASSWORD_INCORRECT", -1},
256 #endif
257 #if HAVE_DECL_CURLE_FTP_WEIRD_PASS_REPLY
258 {"CURLE_FTP_WEIRD_PASS_REPLY", CURLE_FTP_WEIRD_PASS_REPLY},
259 #else
260 {"CURLE_FTP_WEIRD_PASS_REPLY", -1},
261 #endif
262 #if HAVE_DECL_CURLE_FTP_WEIRD_USER_REPLY
263 {"CURLE_FTP_WEIRD_USER_REPLY", CURLE_FTP_WEIRD_USER_REPLY},
264 #else
265 {"CURLE_FTP_WEIRD_USER_REPLY", -1},
266 #endif
267 #if HAVE_DECL_CURLE_FTP_WEIRD_PASV_REPLY
268 {"CURLE_FTP_WEIRD_PASV_REPLY", CURLE_FTP_WEIRD_PASV_REPLY},
269 #else
270 {"CURLE_FTP_WEIRD_PASV_REPLY", -1},
271 #endif
272 #if HAVE_DECL_CURLE_FTP_WEIRD_227_FORMAT
273 {"CURLE_FTP_WEIRD_227_FORMAT", CURLE_FTP_WEIRD_227_FORMAT},
274 #else
275 {"CURLE_FTP_WEIRD_227_FORMAT", -1},
276 #endif
277 #if HAVE_DECL_CURLE_FTP_CANT_GET_HOST
278 {"CURLE_FTP_CANT_GET_HOST", CURLE_FTP_CANT_GET_HOST},
279 #else
280 {"CURLE_FTP_CANT_GET_HOST", -1},
281 #endif
282 #if HAVE_DECL_CURLE_FTP_CANT_RECONNECT
283 {"CURLE_FTP_CANT_RECONNECT", CURLE_FTP_CANT_RECONNECT},
284 #else
285 {"CURLE_FTP_CANT_RECONNECT", -1},
286 #endif
287 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_BINARY
288 {"CURLE_FTP_COULDNT_SET_BINARY", CURLE_FTP_COULDNT_SET_BINARY},
289 #else
290 {"CURLE_FTP_COULDNT_SET_BINARY", -1},
291 #endif
292 #if HAVE_DECL_CURLE_PARTIAL_FILE
293 {"CURLE_PARTIAL_FILE", CURLE_PARTIAL_FILE},
294 #else
295 {"CURLE_PARTIAL_FILE", -1},
296 #endif
297 #if HAVE_DECL_CURLE_FTP_COULDNT_RETR_FILE
298 {"CURLE_FTP_COULDNT_RETR_FILE", CURLE_FTP_COULDNT_RETR_FILE},
299 #else
300 {"CURLE_FTP_COULDNT_RETR_FILE", -1},
301 #endif
302 #if HAVE_DECL_CURLE_FTP_WRITE_ERROR
303 {"CURLE_FTP_WRITE_ERROR", CURLE_FTP_WRITE_ERROR},
304 #else
305 {"CURLE_FTP_WRITE_ERROR", -1},
306 #endif
307 #if HAVE_DECL_CURLE_FTP_QUOTE_ERROR
308 {"CURLE_FTP_QUOTE_ERROR", CURLE_FTP_QUOTE_ERROR},
309 #else
310 {"CURLE_FTP_QUOTE_ERROR", -1},
311 #endif
312 #if HAVE_DECL_CURLE_HTTP_RETURNED_ERROR
313 {"CURLE_HTTP_RETURNED_ERROR", CURLE_HTTP_RETURNED_ERROR},
314 #else
315 {"CURLE_HTTP_RETURNED_ERROR", -1},
316 #endif
317 #if HAVE_DECL_CURLE_WRITE_ERROR
318 {"CURLE_WRITE_ERROR", CURLE_WRITE_ERROR},
319 #else
320 {"CURLE_WRITE_ERROR", -1},
321 #endif
322 #if HAVE_DECL_CURLE_MALFORMAT_USER
323 {"CURLE_MALFORMAT_USER", CURLE_MALFORMAT_USER},
324 #else
325 {"CURLE_MALFORMAT_USER", -1},
326 #endif
327 #if HAVE_DECL_CURLE_FTP_COULDNT_STOR_FILE
328 {"CURLE_FTP_COULDNT_STOR_FILE", CURLE_FTP_COULDNT_STOR_FILE},
329 #else
330 {"CURLE_FTP_COULDNT_STOR_FILE", -1},
331 #endif
332 #if HAVE_DECL_CURLE_READ_ERROR
333 {"CURLE_READ_ERROR", CURLE_READ_ERROR},
334 #else
335 {"CURLE_READ_ERROR", -1},
336 #endif
337 #if HAVE_DECL_CURLE_OUT_OF_MEMORY
338 {"CURLE_OUT_OF_MEMORY", CURLE_OUT_OF_MEMORY},
339 #else
340 {"CURLE_OUT_OF_MEMORY", -1},
341 #endif
342 #if HAVE_DECL_CURLE_OPERATION_TIMEOUTED
343 {"CURLE_OPERATION_TIMEOUTED", CURLE_OPERATION_TIMEOUTED},
344 #else
345 {"CURLE_OPERATION_TIMEOUTED", -1},
346 #endif
347 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_ASCII
348 {"CURLE_FTP_COULDNT_SET_ASCII", CURLE_FTP_COULDNT_SET_ASCII},
349 #else
350 {"CURLE_FTP_COULDNT_SET_ASCII", -1},
351 #endif
352 #if HAVE_DECL_CURLE_FTP_PORT_FAILED
353 {"CURLE_FTP_PORT_FAILED", CURLE_FTP_PORT_FAILED},
354 #else
355 {"CURLE_FTP_PORT_FAILED", -1},
356 #endif
357 #if HAVE_DECL_CURLE_FTP_COULDNT_USE_REST
358 {"CURLE_FTP_COULDNT_USE_REST", CURLE_FTP_COULDNT_USE_REST},
359 #else
360 {"CURLE_FTP_COULDNT_USE_REST", -1},
361 #endif
362 #if HAVE_DECL_CURLE_FTP_COULDNT_GET_SIZE
363 {"CURLE_FTP_COULDNT_GET_SIZE", CURLE_FTP_COULDNT_GET_SIZE},
364 #else
365 {"CURLE_FTP_COULDNT_GET_SIZE", -1},
366 #endif
367 #if HAVE_DECL_CURLE_HTTP_RANGE_ERROR
368 {"CURLE_HTTP_RANGE_ERROR", CURLE_HTTP_RANGE_ERROR},
369 #else
370 {"CURLE_HTTP_RANGE_ERROR", -1},
371 #endif
372 #if HAVE_DECL_CURLE_HTTP_POST_ERROR
373 {"CURLE_HTTP_POST_ERROR", CURLE_HTTP_POST_ERROR},
374 #else
375 {"CURLE_HTTP_POST_ERROR", -1},
376 #endif
377 #if HAVE_DECL_CURLE_SSL_CONNECT_ERROR
378 {"CURLE_SSL_CONNECT_ERROR", CURLE_SSL_CONNECT_ERROR},
379 #else
380 {"CURLE_SSL_CONNECT_ERROR", -1},
381 #endif
382 #if HAVE_DECL_CURLE_BAD_DOWNLOAD_RESUME
383 {"CURLE_BAD_DOWNLOAD_RESUME", CURLE_BAD_DOWNLOAD_RESUME},
384 #else
385 {"CURLE_BAD_DOWNLOAD_RESUME", -1},
386 #endif
387 #if HAVE_DECL_CURLE_FILE_COULDNT_READ_FILE
388 {"CURLE_FILE_COULDNT_READ_FILE", CURLE_FILE_COULDNT_READ_FILE},
389 #else
390 {"CURLE_FILE_COULDNT_READ_FILE", -1},
391 #endif
392 #if HAVE_DECL_CURLE_LDAP_CANNOT_BIND
393 {"CURLE_LDAP_CANNOT_BIND", CURLE_LDAP_CANNOT_BIND},
394 #else
395 {"CURLE_LDAP_CANNOT_BIND", -1},
396 #endif
397 #if HAVE_DECL_CURLE_LDAP_SEARCH_FAILED
398 {"CURLE_LDAP_SEARCH_FAILED", CURLE_LDAP_SEARCH_FAILED},
399 #else
400 {"CURLE_LDAP_SEARCH_FAILED", -1},
401 #endif
402 #if HAVE_DECL_CURLE_LIBRARY_NOT_FOUND
403 {"CURLE_LIBRARY_NOT_FOUND", CURLE_LIBRARY_NOT_FOUND},
404 #else
405 {"CURLE_LIBRARY_NOT_FOUND", -1},
406 #endif
407 #if HAVE_DECL_CURLE_FUNCTION_NOT_FOUND
408 {"CURLE_FUNCTION_NOT_FOUND", CURLE_FUNCTION_NOT_FOUND},
409 #else
410 {"CURLE_FUNCTION_NOT_FOUND", -1},
411 #endif
412 #if HAVE_DECL_CURLE_ABORTED_BY_CALLBACK
413 {"CURLE_ABORTED_BY_CALLBACK", CURLE_ABORTED_BY_CALLBACK},
414 #else
415 {"CURLE_ABORTED_BY_CALLBACK", -1},
416 #endif
417 #if HAVE_DECL_CURLE_BAD_FUNCTION_ARGUMENT
418 {"CURLE_BAD_FUNCTION_ARGUMENT", CURLE_BAD_FUNCTION_ARGUMENT},
419 #else
420 {"CURLE_BAD_FUNCTION_ARGUMENT", -1},
421 #endif
422 #if HAVE_DECL_CURLE_BAD_CALLING_ORDER
423 {"CURLE_BAD_CALLING_ORDER", CURLE_BAD_CALLING_ORDER},
424 #else
425 {"CURLE_BAD_CALLING_ORDER", -1},
426 #endif
427 #if HAVE_DECL_CURLE_INTERFACE_FAILED
428 {"CURLE_INTERFACE_FAILED", CURLE_INTERFACE_FAILED},
429 #else
430 {"CURLE_INTERFACE_FAILED", -1},
431 #endif
432 #if HAVE_DECL_CURLE_BAD_PASSWORD_ENTERED
433 {"CURLE_BAD_PASSWORD_ENTERED", CURLE_BAD_PASSWORD_ENTERED},
434 #else
435 {"CURLE_BAD_PASSWORD_ENTERED", -1},
436 #endif
437 #if HAVE_DECL_CURLE_TOO_MANY_REDIRECTS
438 {"CURLE_TOO_MANY_REDIRECTS", CURLE_TOO_MANY_REDIRECTS},
439 #else
440 {"CURLE_TOO_MANY_REDIRECTS", -1},
441 #endif
442 #if HAVE_DECL_CURLE_UNKNOWN_TELNET_OPTION
443 {"CURLE_UNKNOWN_TELNET_OPTION", CURLE_UNKNOWN_TELNET_OPTION},
444 #else
445 {"CURLE_UNKNOWN_TELNET_OPTION", -1},
446 #endif
447 #if HAVE_DECL_CURLE_TELNET_OPTION_SYNTAX
448 {"CURLE_TELNET_OPTION_SYNTAX", CURLE_TELNET_OPTION_SYNTAX},
449 #else
450 {"CURLE_TELNET_OPTION_SYNTAX", -1},
451 #endif
452 #if HAVE_DECL_CURLE_SSL_PEER_CERTIFICATE
453 {"CURLE_SSL_PEER_CERTIFICATE", CURLE_SSL_PEER_CERTIFICATE},
454 #else
455 {"CURLE_SSL_PEER_CERTIFICATE", -1},
456 #endif
457 #if HAVE_DECL_CURLE_GOT_NOTHING
458 {"CURLE_GOT_NOTHING", CURLE_GOT_NOTHING},
459 #else
460 {"CURLE_GOT_NOTHING", -1},
461 #endif
462 #if HAVE_DECL_CURLE_SSL_ENGINE_NOTFOUND
463 {"CURLE_SSL_ENGINE_NOTFOUND", CURLE_SSL_ENGINE_NOTFOUND},
464 #else
465 {"CURLE_SSL_ENGINE_NOTFOUND", -1},
466 #endif
467 #if HAVE_DECL_CURLE_SSL_ENGINE_SETFAILED
468 {"CURLE_SSL_ENGINE_SETFAILED", CURLE_SSL_ENGINE_SETFAILED},
469 #else
470 {"CURLE_SSL_ENGINE_SETFAILED", -1},
471 #endif
472 #if HAVE_DECL_CURLE_SEND_ERROR
473 {"CURLE_SEND_ERROR", CURLE_SEND_ERROR},
474 #else
475 {"CURLE_SEND_ERROR", -1},
476 #endif
477 #if HAVE_DECL_CURLE_RECV_ERROR
478 {"CURLE_RECV_ERROR", CURLE_RECV_ERROR},
479 #else
480 {"CURLE_RECV_ERROR", -1},
481 #endif
482 #if HAVE_DECL_CURLE_SHARE_IN_USE
483 {"CURLE_SHARE_IN_USE", CURLE_SHARE_IN_USE},
484 #else
485 {"CURLE_SHARE_IN_USE", -1},
486 #endif
487 #if HAVE_DECL_CURLE_SSL_CERTPROBLEM
488 {"CURLE_SSL_CERTPROBLEM", CURLE_SSL_CERTPROBLEM},
489 #else
490 {"CURLE_SSL_CERTPROBLEM", -1},
491 #endif
492 #if HAVE_DECL_CURLE_SSL_CIPHER
493 {"CURLE_SSL_CIPHER", CURLE_SSL_CIPHER},
494 #else
495 {"CURLE_SSL_CIPHER", -1},
496 #endif
497 #if HAVE_DECL_CURLE_SSL_CACERT
498 {"CURLE_SSL_CACERT", CURLE_SSL_CACERT},
499 #else
500 {"CURLE_SSL_CACERT", -1},
501 #endif
502 #if HAVE_DECL_CURLE_BAD_CONTENT_ENCODING
503 {"CURLE_BAD_CONTENT_ENCODING", CURLE_BAD_CONTENT_ENCODING},
504 #else
505 {"CURLE_BAD_CONTENT_ENCODING", -1},
506 #endif
507 #if HAVE_DECL_CURLE_LDAP_INVALID_URL
508 {"CURLE_LDAP_INVALID_URL", CURLE_LDAP_INVALID_URL},
509 #else
510 {"CURLE_LDAP_INVALID_URL", -1},
511 #endif
512 #if HAVE_DECL_CURLE_FILESIZE_EXCEEDED
513 {"CURLE_FILESIZE_EXCEEDED", CURLE_FILESIZE_EXCEEDED},
514 #else
515 {"CURLE_FILESIZE_EXCEEDED", -1},
516 #endif
517 #if HAVE_DECL_CURLE_FTP_SSL_FAILED
518 {"CURLE_FTP_SSL_FAILED", CURLE_FTP_SSL_FAILED},
519 #else
520 {"CURLE_FTP_SSL_FAILED", -1},
521 #endif
522 #if HAVE_DECL_CURLE_SEND_FAIL_REWIND
523 {"CURLE_SEND_FAIL_REWIND", CURLE_SEND_FAIL_REWIND},
524 #else
525 {"CURLE_SEND_FAIL_REWIND", -1},
526 #endif
527 #if HAVE_DECL_CURLE_SSL_ENGINE_INITFAILED
528 {"CURLE_SSL_ENGINE_INITFAILED", CURLE_SSL_ENGINE_INITFAILED},
529 #else
530 {"CURLE_SSL_ENGINE_INITFAILED", -1},
531 #endif
532 #if HAVE_DECL_CURLE_LOGIN_DENIED
533 {"CURLE_LOGIN_DENIED", CURLE_LOGIN_DENIED},
534 #else
535 {"CURLE_LOGIN_DENIED", -1},
536 #endif
537 #if HAVE_DECL_CURLE_TFTP_NOTFOUND
538 {"CURLE_TFTP_NOTFOUND", CURLE_TFTP_NOTFOUND},
539 #else
540 {"CURLE_TFTP_NOTFOUND", -1},
541 #endif
542 #if HAVE_DECL_CURLE_TFTP_PERM
543 {"CURLE_TFTP_PERM", CURLE_TFTP_PERM},
544 #else
545 {"CURLE_TFTP_PERM", -1},
546 #endif
547 #if HAVE_DECL_CURLE_REMOTE_DISK_FULL
548 {"CURLE_REMOTE_DISK_FULL", CURLE_REMOTE_DISK_FULL},
549 #else
550 {"CURLE_REMOTE_DISK_FULL", -1},
551 #endif
552 #if HAVE_DECL_CURLE_TFTP_ILLEGAL
553 {"CURLE_TFTP_ILLEGAL", CURLE_TFTP_ILLEGAL},
554 #else
555 {"CURLE_TFTP_ILLEGAL", -1},
556 #endif
557 #if HAVE_DECL_CURLE_TFTP_UNKNOWNID
558 {"CURLE_TFTP_UNKNOWNID", CURLE_TFTP_UNKNOWNID},
559 #else
560 {"CURLE_TFTP_UNKNOWNID", -1},
561 #endif
562 #if HAVE_DECL_CURLE_REMOTE_FILE_EXISTS
563 {"CURLE_REMOTE_FILE_EXISTS", CURLE_REMOTE_FILE_EXISTS},
564 #else
565 {"CURLE_REMOTE_FILE_EXISTS", -1},
566 #endif
567 #if HAVE_DECL_CURLE_TFTP_NOSUCHUSER
568 {"CURLE_TFTP_NOSUCHUSER", CURLE_TFTP_NOSUCHUSER},
569 #else
570 {"CURLE_TFTP_NOSUCHUSER", -1},
571 #endif
572 #if HAVE_DECL_CURLE_CONV_FAILED
573 {"CURLE_CONV_FAILED", CURLE_CONV_FAILED},
574 #else
575 {"CURLE_CONV_FAILED", -1},
576 #endif
577 #if HAVE_DECL_CURLE_CONV_REQD
578 {"CURLE_CONV_REQD", CURLE_CONV_REQD},
579 #else
580 {"CURLE_CONV_REQD", -1},
581 #endif
582 #if HAVE_DECL_CURLE_SSL_CACERT_BADFILE
583 {"CURLE_SSL_CACERT_BADFILE", CURLE_SSL_CACERT_BADFILE},
584 #else
585 {"CURLE_SSL_CACERT_BADFILE", -1},
586 #endif
587 #if HAVE_DECL_CURLE_REMOTE_FILE_NOT_FOUND
588 {"CURLE_REMOTE_FILE_NOT_FOUND", CURLE_REMOTE_FILE_NOT_FOUND},
589 #else
590 {"CURLE_REMOTE_FILE_NOT_FOUND", -1},
591 #endif
592 #if HAVE_DECL_CURLE_SSH
593 {"CURLE_SSH", CURLE_SSH},
594 #else
595 {"CURLE_SSH", -1},
596 #endif
597 #if HAVE_DECL_CURLE_SSL_SHUTDOWN_FAILED
598 {"CURLE_SSL_SHUTDOWN_FAILED", CURLE_SSL_SHUTDOWN_FAILED},
599 #else
600 {"CURLE_SSL_SHUTDOWN_FAILED", -1},
601 #endif
602 #if HAVE_DECL_CURLE_AGAIN
603 {"CURLE_AGAIN", CURLE_AGAIN},
604 #else
605 {"CURLE_AGAIN", -1},
606 #endif
607 {NULL, 0}
610 typedef struct CURLOptionMapping CURLOptionMapping;
612 struct CURLOptionMapping
614 void (*optionHandler)(Connection *, value);
615 char *name;
616 OcamlValue ocamlValue;
619 static char* strdup_ml(value v)
621 char* p = NULL;
622 p = malloc(caml_string_length(v)+1);
623 memcpy(p,String_val(v),caml_string_length(v)+1); // caml strings have terminating zero
624 return p;
627 /* prepends to the beginning of list */
628 static struct curl_slist* curl_slist_prepend_ml(struct curl_slist* list, value v)
630 /* FIXME check NULLs */
631 struct curl_slist* new_item = malloc(sizeof(struct curl_slist));
633 new_item->next = list;
634 new_item->data = strdup_ml(v);
636 return new_item;
639 static void free_curl_slist(struct curl_slist *slist)
641 if (NULL == slist)
642 return;
644 curl_slist_free_all(slist);
647 static void raiseError(Connection *conn, CURLcode code)
649 CAMLparam0();
650 CAMLlocal1(exceptionData);
651 value *exception;
652 char *errorString = "Unknown Error";
653 int i;
655 for (i = 0; errorMap[i].name != NULL; i++)
657 if (errorMap[i].error == code)
659 errorString = errorMap[i].name;
660 break;
664 exceptionData = caml_alloc_tuple(3);
666 Store_field(exceptionData, 0, Val_int(code));
667 Store_field(exceptionData, 1, Val_int(code));
668 Store_field(exceptionData, 2, caml_copy_string(errorString));
670 if (conn != NULL && conn->curl_ERRORBUFFER != NULL)
672 Store_field(Field(conn->ocamlValues, Ocaml_ERRORBUFFER), 0, caml_copy_string(conn->curl_ERRORBUFFER));
675 exception = caml_named_value("CurlException");
677 if (exception == NULL)
678 caml_failwith("CurlException not registered");
680 caml_raise_with_arg(*exception, exceptionData);
682 CAMLreturn0;
685 static void resetOcamlValues(Connection* connection)
687 int i;
689 for (i = 0; i < OcamlValuesSize; i++)
690 Store_field(connection->ocamlValues, i, Val_unit);
693 static Connection* allocConnection(CURL* h)
695 Connection* connection = (Connection *)malloc(sizeof(Connection));
697 connection->ocamlValues = caml_alloc(OcamlValuesSize, 0);
698 resetOcamlValues(connection);
699 caml_register_global_root(&connection->ocamlValues);
701 connection->connection = h;
703 connection->next = NULL;
704 connection->prev = NULL;
706 if (connectionList.tail == NULL)
708 connectionList.tail = connection;
709 connectionList.head = connection;
711 else
713 connection->prev = connectionList.head;
714 connectionList.head->next = connection;
715 connectionList.head = connection;
718 connection->refcount = 0;
720 connection->curl_URL = NULL;
721 connection->curl_PROXY = NULL;
722 connection->curl_USERPWD = NULL;
723 connection->curl_PROXYUSERPWD = NULL;
724 connection->curl_RANGE = NULL;
725 connection->curl_ERRORBUFFER = NULL;
726 connection->curl_POSTFIELDS = NULL;
727 connection->curl_POSTFIELDSIZE = -1;
728 connection->curl_REFERER = NULL;
729 connection->curl_USERAGENT = NULL;
730 connection->curl_FTPPORT = NULL;
731 connection->curl_COOKIE = NULL;
732 connection->curl_HTTPHEADER = NULL;
733 connection->httpPostBuffers = NULL;
734 connection->httpPostFirst = NULL;
735 connection->httpPostLast = NULL;
736 connection->curl_SSLCERT = NULL;
737 connection->curl_SSLCERTTYPE = NULL;
738 connection->curl_SSLCERTPASSWD = NULL;
739 connection->curl_SSLKEY = NULL;
740 connection->curl_SSLKEYTYPE = NULL;
741 connection->curl_SSLKEYPASSWD = NULL;
742 connection->curl_SSLENGINE = NULL;
743 connection->curl_QUOTE = NULL;
744 connection->curl_POSTQUOTE = NULL;
745 connection->curl_COOKIEFILE = NULL;
746 connection->curl_CUSTOMREQUEST = NULL;
747 connection->curl_INTERFACE = NULL;
748 connection->curl_CAINFO = NULL;
749 connection->curl_CAPATH = NULL;
750 connection->curl_RANDOM_FILE = NULL;
751 connection->curl_EGDSOCKET = NULL;
752 connection->curl_COOKIEJAR = NULL;
753 connection->curl_SSL_CIPHER_LIST = NULL;
754 connection->curl_PRIVATE = NULL;
755 connection->curl_HTTP200ALIASES = NULL;
756 connection->curl_NETRC_FILE = NULL;
757 connection->curl_FTP_ACCOUNT = NULL;
758 connection->curl_COOKIELIST = NULL;
759 connection->curl_FTP_ALTERNATIVE_TO_USER = NULL;
760 connection->curl_SSH_PUBLIC_KEYFILE = NULL;
761 connection->curl_SSH_PRIVATE_KEYFILE = NULL;
762 connection->curl_COPYPOSTFIELDS = NULL;
763 connection->curl_RESOLVE = NULL;
764 connection->curl_DNS_SERVERS = NULL;
765 connection->curl_MAIL_FROM = NULL;
766 connection->curl_MAIL_RCPT = NULL;
768 return connection;
771 static Connection *newConnection(void)
773 CURL* h;
775 caml_enter_blocking_section();
776 h = curl_easy_init();
777 caml_leave_blocking_section();
779 return allocConnection(h);
782 static void free_if(void* p) { if (NULL != p) free(p); }
784 static void removeConnection(Connection *connection, int finalization)
786 const char* fin_url = NULL;
788 if (!connection->connection)
790 return; /* already cleaned up */
793 if (finalization)
795 /* cannot engage OCaml runtime at finalization, just report leak */
796 if (CURLE_OK != curl_easy_getinfo(connection->connection, CURLINFO_EFFECTIVE_URL, &fin_url) || NULL == fin_url)
798 fin_url = "unknown";
800 fprintf(stderr,"Curl: handle %p leaked, conn %p, url %s\n", connection->connection, connection, fin_url);
801 fflush(stderr);
803 else
805 caml_enter_blocking_section();
806 curl_easy_cleanup(connection->connection);
807 caml_leave_blocking_section();
810 connection->connection = NULL;
812 if (connectionList.tail == connection)
813 connectionList.tail = connectionList.tail->next;
814 if (connectionList.head == connection)
815 connectionList.head = connectionList.head->prev;
817 if (connection->next != NULL)
818 connection->next->prev = connection->prev;
819 if (connection->prev != NULL)
820 connection->prev->next = connection->next;
822 caml_remove_global_root(&connection->ocamlValues);
824 free_if(connection->curl_URL);
825 free_if(connection->curl_PROXY);
826 free_if(connection->curl_USERPWD);
827 free_if(connection->curl_PROXYUSERPWD);
828 free_if(connection->curl_RANGE);
829 free_if(connection->curl_ERRORBUFFER);
830 free_if(connection->curl_POSTFIELDS);
831 free_if(connection->curl_REFERER);
832 free_if(connection->curl_USERAGENT);
833 free_if(connection->curl_FTPPORT);
834 free_if(connection->curl_COOKIE);
835 free_curl_slist(connection->curl_HTTPHEADER);
836 free_curl_slist(connection->httpPostBuffers);
837 if (connection->httpPostFirst != NULL)
838 curl_formfree(connection->httpPostFirst);
839 free_curl_slist(connection->curl_RESOLVE);
840 free_if(connection->curl_SSLCERT);
841 free_if(connection->curl_SSLCERTTYPE);
842 free_if(connection->curl_SSLCERTPASSWD);
843 free_if(connection->curl_SSLKEY);
844 free_if(connection->curl_SSLKEYTYPE);
845 free_if(connection->curl_SSLKEYPASSWD);
846 free_if(connection->curl_SSLENGINE);
847 free_curl_slist(connection->curl_QUOTE);
848 free_curl_slist(connection->curl_POSTQUOTE);
849 free_if(connection->curl_COOKIEFILE);
850 free_if(connection->curl_CUSTOMREQUEST);
851 free_if(connection->curl_INTERFACE);
852 free_if(connection->curl_CAINFO);
853 free_if(connection->curl_CAPATH);
854 free_if(connection->curl_RANDOM_FILE);
855 free_if(connection->curl_EGDSOCKET);
856 free_if(connection->curl_COOKIEJAR);
857 free_if(connection->curl_SSL_CIPHER_LIST);
858 free_if(connection->curl_PRIVATE);
859 free_curl_slist(connection->curl_HTTP200ALIASES);
860 free_if(connection->curl_NETRC_FILE);
861 free_if(connection->curl_FTP_ACCOUNT);
862 free_if(connection->curl_COOKIELIST);
863 free_if(connection->curl_FTP_ALTERNATIVE_TO_USER);
864 free_if(connection->curl_SSH_PUBLIC_KEYFILE);
865 free_if(connection->curl_SSH_PRIVATE_KEYFILE);
866 free_if(connection->curl_COPYPOSTFIELDS);
867 free_if(connection->curl_DNS_SERVERS);
868 free_if(connection->curl_MAIL_FROM);
869 free_curl_slist(connection->curl_MAIL_RCPT);
872 #if 1
873 static void checkConnection(Connection * connection)
875 (void)connection;
877 #else
878 static void checkConnection(Connection *connection)
880 Connection *listIter;
882 listIter = connectionList.tail;
884 while (listIter != NULL)
886 if (listIter == connection)
887 return;
889 listIter = listIter->next;
892 caml_failwith("Invalid Connection");
894 #endif
896 static Connection* findConnection(CURL* h)
898 Connection *listIter;
900 listIter = connectionList.tail;
902 while (listIter != NULL)
904 if (listIter->connection == h)
905 return listIter;
907 listIter = listIter->next;
910 caml_failwith("Unknown handle");
913 void op_curl_easy_finalize(value v)
915 Connection* conn = Connection_val(v);
916 /* same connection may be referenced by several different
917 OCaml values, see e.g. caml_curl_multi_remove_finished */
918 conn->refcount--;
919 if (0 == conn->refcount)
921 removeConnection(conn, 1);
922 free(conn);
926 int op_curl_easy_compare(value v1, value v2)
928 size_t p1 = (size_t)Connection_val(v1);
929 size_t p2 = (size_t)Connection_val(v2);
930 return (p1 == p2 ? 0 : (p1 > p2 ? 1 : -1)); /* compare addresses */
933 intnat op_curl_easy_hash(value v)
935 return (size_t)Connection_val(v); /* address */
938 static struct custom_operations curl_easy_ops = {
939 "ygrek.curl_easy",
940 op_curl_easy_finalize,
941 op_curl_easy_compare,
942 op_curl_easy_hash,
943 custom_serialize_default,
944 custom_deserialize_default,
945 #if defined(custom_compare_ext_default)
946 custom_compare_ext_default,
947 #endif
950 value caml_curl_alloc(Connection* conn)
952 value v = caml_alloc_custom(&curl_easy_ops, sizeof(Connection*), 0, 1);
953 Connection_val(v) = conn;
954 conn->refcount++;
955 return v;
958 static size_t cb_WRITEFUNCTION(char *ptr, size_t size, size_t nmemb, void *data)
960 caml_leave_blocking_section();
962 CAMLparam0();
963 CAMLlocal2(result, str);
964 Connection *conn = (Connection *)data;
965 size_t i;
967 checkConnection(conn);
969 str = caml_alloc_string(size*nmemb);
971 for (i = 0; i < size*nmemb; i++)
972 Byte(str, i) = ptr[i];
974 result = caml_callback_exn(Field(conn->ocamlValues, Ocaml_WRITEFUNCTION), str);
976 size_t r = Is_exception_result(result) ? 0 : Int_val(result);
977 CAMLdrop;
979 caml_enter_blocking_section();
980 return r;
983 static size_t cb_READFUNCTION(void *ptr, size_t size, size_t nmemb, void *data)
985 caml_leave_blocking_section();
987 CAMLparam0();
988 CAMLlocal1(result);
989 Connection *conn = (Connection *)data;
990 size_t length;
992 checkConnection(conn);
994 result = caml_callback_exn(Field(conn->ocamlValues, Ocaml_READFUNCTION),
995 Val_int(size*nmemb));
997 if (Is_exception_result(result))
999 CAMLreturnT(size_t,CURL_READFUNC_ABORT);
1002 length = caml_string_length(result);
1004 size_t r;
1006 if (length <= size*nmemb)
1008 memcpy(ptr, String_val(result), length);
1009 r = length;
1011 else
1013 r = CURL_READFUNC_ABORT;
1015 CAMLdrop;
1017 caml_enter_blocking_section();
1018 return r;
1021 static size_t cb_HEADERFUNCTION(char *ptr, size_t size, size_t nmemb, void *data)
1023 caml_leave_blocking_section();
1025 CAMLparam0();
1026 CAMLlocal2(result,str);
1027 Connection *conn = (Connection *)data;
1028 size_t i;
1030 checkConnection(conn);
1032 str = caml_alloc_string(size*nmemb);
1034 for (i = 0; i < size*nmemb; i++)
1035 Byte(str, i) = ptr[i];
1037 result = caml_callback_exn(Field(conn->ocamlValues, Ocaml_HEADERFUNCTION), str);
1039 size_t r = Is_exception_result(result) ? 0 : Int_val(result);
1040 CAMLdrop;
1042 caml_enter_blocking_section();
1043 return r;
1046 static int cb_PROGRESSFUNCTION(void *data,
1047 double dlTotal,
1048 double dlNow,
1049 double ulTotal,
1050 double ulNow)
1052 caml_leave_blocking_section();
1054 CAMLparam0();
1055 CAMLlocal1(result);
1056 CAMLlocalN(callbackData, 4);
1057 Connection *conn = (Connection *)data;
1059 checkConnection(conn);
1061 callbackData[0] = caml_copy_double(dlTotal);
1062 callbackData[1] = caml_copy_double(dlNow);
1063 callbackData[2] = caml_copy_double(ulTotal);
1064 callbackData[3] = caml_copy_double(ulNow);
1066 result = caml_callbackN_exn(Field(conn->ocamlValues, Ocaml_PROGRESSFUNCTION),
1067 4, callbackData);
1069 int r = Is_exception_result(result) ? 1 : Bool_val(result);
1070 CAMLdrop;
1072 caml_enter_blocking_section();
1073 return r;
1076 static int cb_DEBUGFUNCTION(CURL *debugConnection,
1077 curl_infotype infoType,
1078 char *buffer,
1079 size_t bufferLength,
1080 void *data)
1082 caml_leave_blocking_section();
1084 CAMLparam0();
1085 CAMLlocal3(camlDebugConnection, camlInfoType, camlMessage);
1086 size_t i;
1087 Connection *conn = (Connection *)data;
1088 (void)debugConnection; /* not used */
1090 checkConnection(conn);
1092 camlDebugConnection = (value)conn;
1093 camlInfoType = Val_long(infoType);
1094 camlMessage = caml_alloc_string(bufferLength);
1096 for (i = 0; i < bufferLength; i++)
1097 Byte(camlMessage, i) = buffer[i];
1099 caml_callback3_exn(Field(conn->ocamlValues, Ocaml_DEBUGFUNCTION),
1100 camlDebugConnection,
1101 camlInfoType,
1102 camlMessage);
1104 CAMLdrop;
1106 caml_enter_blocking_section();
1107 return 0;
1110 static curlioerr cb_IOCTLFUNCTION(CURL *ioctl,
1111 int cmd,
1112 void *data)
1114 caml_leave_blocking_section();
1116 CAMLparam0();
1117 CAMLlocal3(camlResult, camlConnection, camlCmd);
1118 Connection *conn = (Connection *)data;
1119 curlioerr result = CURLIOE_OK;
1120 (void)ioctl; /* not used */
1122 checkConnection(conn);
1124 if (cmd == CURLIOCMD_NOP)
1125 camlCmd = Val_long(0);
1126 else if (cmd == CURLIOCMD_RESTARTREAD)
1127 camlCmd = Val_long(1);
1128 else
1129 caml_failwith("Invalid IOCTL Cmd!");
1131 camlConnection = caml_curl_alloc(conn);
1133 camlResult = caml_callback2_exn(Field(conn->ocamlValues, Ocaml_IOCTLFUNCTION),
1134 camlConnection,
1135 camlCmd);
1137 if (Is_exception_result(camlResult))
1139 result = CURLIOE_FAILRESTART;
1141 else
1142 switch (Long_val(camlResult))
1144 case 0: /* CURLIOE_OK */
1145 result = CURLIOE_OK;
1146 break;
1148 case 1: /* CURLIOE_UNKNOWNCMD */
1149 result = CURLIOE_UNKNOWNCMD;
1150 break;
1152 case 2: /* CURLIOE_FAILRESTART */
1153 result = CURLIOE_FAILRESTART;
1154 break;
1156 default: /* Incorrect return value, but let's handle it */
1157 result = CURLIOE_FAILRESTART;
1158 break;
1160 CAMLdrop;
1162 caml_enter_blocking_section();
1163 return result;
1166 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1167 static int cb_SEEKFUNCTION(void *data,
1168 curl_off_t offset,
1169 int origin)
1171 caml_leave_blocking_section();
1173 CAMLparam0();
1174 CAMLlocal3(camlResult, camlOffset, camlOrigin);
1175 Connection *conn = (Connection *)data;
1177 camlOffset = caml_copy_int64(offset);
1179 if (origin == SEEK_SET)
1180 camlOrigin = Val_long(0);
1181 else if (origin == SEEK_CUR)
1182 camlOrigin = Val_long(1);
1183 else if (origin == SEEK_END)
1184 camlOrigin = Val_long(2);
1185 else
1186 caml_failwith("Invalid seek code");
1188 camlResult = caml_callback2_exn(Field(conn->ocamlValues,
1189 Ocaml_SEEKFUNCTION),
1190 camlOffset,
1191 camlOrigin);
1193 int result;
1194 if (Is_exception_result(camlResult))
1195 result = CURL_SEEKFUNC_FAIL;
1196 else
1197 switch (Int_val(camlResult))
1199 case 0: result = CURL_SEEKFUNC_OK; break;
1200 case 1: result = CURL_SEEKFUNC_FAIL; break;
1201 case 2: result = CURL_SEEKFUNC_CANTSEEK; break;
1202 default: caml_failwith("Invalid seek result");
1204 CAMLdrop;
1206 caml_enter_blocking_section();
1207 return result;
1209 #endif
1211 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
1212 static int cb_OPENSOCKETFUNCTION(void *data,
1213 curlsocktype purpose,
1214 struct curl_sockaddr *addr)
1216 caml_leave_blocking_section();
1218 CAMLparam0();
1219 CAMLlocal1(result);
1220 Connection *conn = (Connection *)data;
1221 int sock = -1;
1222 (void)purpose; /* not used */
1224 sock = socket(addr->family, addr->socktype, addr->protocol);
1226 if (-1 != sock)
1228 /* FIXME windows */
1229 result = caml_callback_exn(Field(conn->ocamlValues, Ocaml_OPENSOCKETFUNCTION), Val_int(sock));
1230 if (Is_exception_result(result))
1232 close(sock);
1233 sock = -1;
1236 CAMLdrop;
1238 caml_enter_blocking_section();
1239 return ((sock == -1) ? CURL_SOCKET_BAD : sock);
1241 #endif
1244 ** curl_global_init helper function
1247 CAMLprim value helper_curl_global_init(value initOption)
1249 CAMLparam1(initOption);
1251 switch (Long_val(initOption))
1253 case 0: /* CURLINIT_GLOBALALL */
1254 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_ALL)));
1255 break;
1257 case 1: /* CURLINIT_GLOBALSSL */
1258 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_SSL)));
1259 break;
1261 case 2: /* CURLINIT_GLOBALWIN32 */
1262 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_WIN32)));
1263 break;
1265 case 3: /* CURLINIT_GLOBALNOTHING */
1266 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_NOTHING)));
1267 break;
1269 default:
1270 caml_failwith("Invalid Initialization Option");
1271 break;
1274 /* Keep compiler happy, we should never get here due to caml_failwith() */
1275 CAMLreturn(Val_unit);
1279 ** curl_global_cleanup helper function
1282 CAMLprim value helper_curl_global_cleanup(void)
1284 CAMLparam0();
1286 curl_global_cleanup();
1288 CAMLreturn(Val_unit);
1292 ** curl_easy_init helper function
1294 CAMLprim value helper_curl_easy_init(void)
1296 CAMLparam0();
1297 CAMLlocal1(result);
1299 result = caml_curl_alloc(newConnection());
1301 CAMLreturn(result);
1304 CAMLprim value helper_curl_easy_reset(value conn)
1306 CAMLparam1(conn);
1307 Connection *connection = Connection_val(conn);
1309 checkConnection(connection);
1310 curl_easy_reset(connection->connection);
1311 resetOcamlValues(connection);
1313 CAMLreturn(Val_unit);
1317 ** curl_easy_setopt helper utility functions
1320 #define SETOPT_FUNCTION(name) \
1321 static void handle_##name##FUNCTION(Connection *conn, value option) \
1323 CAMLparam1(option); \
1324 CURLcode result = CURLE_OK; \
1325 Store_field(conn->ocamlValues, Ocaml_##name##FUNCTION, option); \
1326 result = curl_easy_setopt(conn->connection, CURLOPT_##name##FUNCTION, cb_##name##FUNCTION); \
1327 if (result != CURLE_OK) raiseError(conn, result); \
1328 result = curl_easy_setopt(conn->connection, CURLOPT_##name##DATA, conn); \
1329 if (result != CURLE_OK) raiseError(conn, result); \
1330 CAMLreturn0; \
1333 SETOPT_FUNCTION( WRITE)
1334 SETOPT_FUNCTION( READ)
1335 SETOPT_FUNCTION( HEADER)
1336 SETOPT_FUNCTION( PROGRESS)
1337 SETOPT_FUNCTION( DEBUG)
1339 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1340 SETOPT_FUNCTION( SEEK)
1341 #endif
1343 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
1344 SETOPT_FUNCTION( IOCTL)
1345 #endif
1347 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
1348 SETOPT_FUNCTION( OPENSOCKET)
1349 #endif
1351 static void handle_slist(Connection *conn, struct curl_slist** slist, OcamlValue caml_option, CURLoption curl_option, value option)
1353 CAMLparam1(option);
1354 CURLcode result = CURLE_OK;
1356 Store_field(conn->ocamlValues, caml_option, option);
1358 free_curl_slist(*slist);
1359 *slist = NULL;
1361 while (Val_emptylist != option)
1363 *slist = curl_slist_append(*slist, String_val(Field(option, 0)));
1365 option = Field(option, 1);
1368 result = curl_easy_setopt(conn->connection, curl_option, *slist);
1370 if (result != CURLE_OK)
1371 raiseError(conn, result);
1373 CAMLreturn0;
1376 static long convert_bit_list(long *map, size_t map_size, value option)
1378 CAMLparam1(option);
1379 long bits = 0;
1380 int index;
1382 while (Val_emptylist != option)
1384 index = Int_val(Field(option, 0));
1385 if ((index < 0) || ((size_t)index >= map_size))
1386 caml_invalid_argument("convert_bit_list");
1388 bits |= map[index];
1390 option = Field(option, 1);
1393 CAMLreturnT(long, bits);
1396 #define SETOPT_STRING(name) \
1397 static void handle_##name(Connection *conn, value option) \
1399 CAMLparam1(option); \
1400 CURLcode result = CURLE_OK; \
1402 Store_field(conn->ocamlValues, Ocaml_##name, option); \
1404 if (conn->curl_##name != NULL) \
1405 free(conn->curl_##name); \
1407 conn->curl_##name = strdup(String_val(option)); \
1409 result = curl_easy_setopt(conn->connection, CURLOPT_##name, conn->curl_##name); \
1411 if (result != CURLE_OK) \
1412 raiseError(conn, result); \
1414 CAMLreturn0; \
1417 #define SETOPT_VAL_(func_name, curl_option, conv_val) \
1418 static void func_name(Connection *conn, value option) \
1420 CAMLparam1(option); \
1421 CURLcode result = CURLE_OK; \
1423 result = curl_easy_setopt(conn->connection, curl_option, conv_val(option)); \
1425 if (result != CURLE_OK) \
1426 raiseError(conn, result); \
1428 CAMLreturn0; \
1431 #define SETOPT_VAL(name, conv) SETOPT_VAL_(handle_##name, CURLOPT_##name, conv)
1432 #define SETOPT_BOOL(name) SETOPT_VAL(name, Bool_val)
1433 #define SETOPT_LONG(name) SETOPT_VAL(name, Long_val)
1434 #define SETOPT_INT64(name) SETOPT_VAL(name, Int64_val)
1436 #define SETOPT_SLIST(name) \
1437 static void handle_##name(Connection* conn, value option) \
1439 handle_slist(conn,&(conn->curl_##name),Ocaml_##name,CURLOPT_##name,option); \
1442 SETOPT_STRING( URL)
1443 SETOPT_LONG( INFILESIZE)
1444 SETOPT_STRING( PROXY)
1445 SETOPT_LONG( PROXYPORT)
1446 SETOPT_BOOL( HTTPPROXYTUNNEL)
1447 SETOPT_BOOL( VERBOSE)
1448 SETOPT_BOOL( HEADER)
1449 SETOPT_BOOL( NOPROGRESS)
1451 #if HAVE_DECL_CURLOPT_NOSIGNAL
1452 SETOPT_BOOL( NOSIGNAL)
1453 #endif
1455 SETOPT_BOOL( NOBODY)
1456 SETOPT_BOOL( FAILONERROR)
1457 SETOPT_BOOL( UPLOAD)
1458 SETOPT_BOOL( POST)
1459 SETOPT_BOOL( FTPLISTONLY)
1460 SETOPT_BOOL( FTPAPPEND)
1463 static void handle_NETRC(Connection *conn, value option)
1465 CAMLparam1(option);
1466 CURLcode result = CURLE_OK;
1467 long netrc;
1469 switch (Long_val(option))
1471 case 0: /* CURL_NETRC_OPTIONAL */
1472 netrc = CURL_NETRC_OPTIONAL;
1473 break;
1475 case 1:/* CURL_NETRC_IGNORED */
1476 netrc = CURL_NETRC_IGNORED;
1477 break;
1479 case 2: /* CURL_NETRC_REQUIRED */
1480 netrc = CURL_NETRC_REQUIRED;
1481 break;
1483 default:
1484 caml_failwith("Invalid NETRC Option");
1485 break;
1488 result = curl_easy_setopt(conn->connection,
1489 CURLOPT_NETRC,
1490 netrc);
1492 if (result != CURLE_OK)
1493 raiseError(conn, result);
1495 CAMLreturn0;
1498 #if HAVE_DECL_CURLOPT_ENCODING
1499 static void handle_ENCODING(Connection *conn, value option)
1501 CAMLparam1(option);
1502 CURLcode result = CURLE_OK;
1504 switch (Long_val(option))
1506 case 0: /* CURL_ENCODING_NONE */
1507 result = curl_easy_setopt(conn->connection,
1508 CURLOPT_ENCODING,
1509 "identity");
1510 break;
1512 case 1: /* CURL_ENCODING_DEFLATE */
1513 result = curl_easy_setopt(conn->connection,
1514 CURLOPT_ENCODING,
1515 "deflate");
1516 break;
1518 case 2: /* CURL_ENCODING_GZIP */
1519 result = curl_easy_setopt(conn->connection,
1520 CURLOPT_ENCODING,
1521 "gzip");
1522 break;
1524 case 3: /* CURL_ENCODING_ANY */
1525 result = curl_easy_setopt(conn->connection,
1526 CURLOPT_ENCODING,
1527 "");
1528 break;
1530 default:
1531 caml_failwith("Invalid Encoding Option");
1532 break;
1535 if (result != CURLE_OK)
1536 raiseError(conn, result);
1538 CAMLreturn0;
1540 #endif
1543 SETOPT_BOOL( FOLLOWLOCATION)
1544 SETOPT_BOOL( TRANSFERTEXT)
1545 SETOPT_BOOL( PUT)
1546 SETOPT_STRING( USERPWD)
1547 SETOPT_STRING( PROXYUSERPWD)
1548 SETOPT_STRING( RANGE)
1550 static void handle_ERRORBUFFER(Connection *conn, value option)
1552 CAMLparam1(option);
1553 CURLcode result = CURLE_OK;
1555 Store_field(conn->ocamlValues, Ocaml_ERRORBUFFER, option);
1557 if (conn->curl_ERRORBUFFER != NULL)
1558 free(conn->curl_ERRORBUFFER);
1560 conn->curl_ERRORBUFFER = malloc(sizeof(char) * CURL_ERROR_SIZE);
1562 result = curl_easy_setopt(conn->connection,
1563 CURLOPT_ERRORBUFFER,
1564 conn->curl_ERRORBUFFER);
1566 if (result != CURLE_OK)
1567 raiseError(conn, result);
1569 CAMLreturn0;
1572 SETOPT_LONG( TIMEOUT)
1574 static void handle_POSTFIELDS(Connection *conn, value option)
1576 CAMLparam1(option);
1577 CURLcode result = CURLE_OK;
1579 Store_field(conn->ocamlValues, Ocaml_POSTFIELDS, option);
1581 if (conn->curl_POSTFIELDS != NULL)
1582 free(conn->curl_POSTFIELDS);
1584 conn->curl_POSTFIELDS = strdup_ml(option);
1586 result = curl_easy_setopt(conn->connection,
1587 CURLOPT_POSTFIELDS,
1588 conn->curl_POSTFIELDS);
1590 if (result != CURLE_OK)
1591 raiseError(conn, result);
1593 CAMLreturn0;
1596 SETOPT_LONG( POSTFIELDSIZE)
1597 SETOPT_STRING( REFERER)
1598 SETOPT_STRING( USERAGENT)
1599 SETOPT_STRING( FTPPORT)
1600 SETOPT_LONG( LOW_SPEED_LIMIT)
1601 SETOPT_LONG( LOW_SPEED_TIME)
1602 SETOPT_LONG( RESUME_FROM)
1603 SETOPT_STRING( COOKIE)
1605 SETOPT_SLIST( HTTPHEADER)
1607 static void handle_HTTPPOST(Connection *conn, value option)
1609 CAMLparam1(option);
1610 CAMLlocal3(listIter, formItem, contentType);
1611 CURLcode result = CURLE_OK;
1613 listIter = option;
1615 Store_field(conn->ocamlValues, Ocaml_HTTPPOST, option);
1617 free_curl_slist(conn->httpPostBuffers);
1618 if (conn->httpPostFirst != NULL)
1619 curl_formfree(conn->httpPostFirst);
1621 conn->httpPostBuffers = NULL;
1622 conn->httpPostFirst = NULL;
1623 conn->httpPostLast = NULL;
1625 while (!Is_long(listIter))
1627 formItem = Field(listIter, 0);
1629 switch (Tag_val(formItem))
1631 case 0: /* CURLFORM_CONTENT */
1632 if (Wosize_val(formItem) < 3)
1634 caml_failwith("Incorrect CURLFORM_CONTENT parameters");
1637 if (Is_long(Field(formItem, 2)) &&
1638 Long_val(Field(formItem, 2)) == 0)
1640 curl_formadd(&conn->httpPostFirst,
1641 &conn->httpPostLast,
1642 CURLFORM_COPYNAME,
1643 String_val(Field(formItem, 0)),
1644 CURLFORM_NAMELENGTH,
1645 caml_string_length(Field(formItem, 0)),
1646 CURLFORM_COPYCONTENTS,
1647 String_val(Field(formItem, 1)),
1648 CURLFORM_CONTENTSLENGTH,
1649 caml_string_length(Field(formItem, 1)),
1650 CURLFORM_END);
1652 else if (Is_block(Field(formItem, 2)))
1654 contentType = Field(formItem, 2);
1656 curl_formadd(&conn->httpPostFirst,
1657 &conn->httpPostLast,
1658 CURLFORM_COPYNAME,
1659 String_val(Field(formItem, 0)),
1660 CURLFORM_NAMELENGTH,
1661 caml_string_length(Field(formItem, 0)),
1662 CURLFORM_COPYCONTENTS,
1663 String_val(Field(formItem, 1)),
1664 CURLFORM_CONTENTSLENGTH,
1665 caml_string_length(Field(formItem, 1)),
1666 CURLFORM_CONTENTTYPE,
1667 String_val(Field(contentType, 0)),
1668 CURLFORM_END);
1670 else
1672 caml_failwith("Incorrect CURLFORM_CONTENT parameters");
1674 break;
1676 case 1: /* CURLFORM_FILECONTENT */
1677 if (Wosize_val(formItem) < 3)
1679 caml_failwith("Incorrect CURLFORM_FILECONTENT parameters");
1682 if (Is_long(Field(formItem, 2)) &&
1683 Long_val(Field(formItem, 2)) == 0)
1685 curl_formadd(&conn->httpPostFirst,
1686 &conn->httpPostLast,
1687 CURLFORM_COPYNAME,
1688 String_val(Field(formItem, 0)),
1689 CURLFORM_NAMELENGTH,
1690 caml_string_length(Field(formItem, 0)),
1691 CURLFORM_FILECONTENT,
1692 String_val(Field(formItem, 1)),
1693 CURLFORM_END);
1695 else if (Is_block(Field(formItem, 2)))
1697 contentType = Field(formItem, 2);
1699 curl_formadd(&conn->httpPostFirst,
1700 &conn->httpPostLast,
1701 CURLFORM_COPYNAME,
1702 String_val(Field(formItem, 0)),
1703 CURLFORM_NAMELENGTH,
1704 caml_string_length(Field(formItem, 0)),
1705 CURLFORM_FILECONTENT,
1706 String_val(Field(formItem, 1)),
1707 CURLFORM_CONTENTTYPE,
1708 String_val(Field(contentType, 0)),
1709 CURLFORM_END);
1711 else
1713 caml_failwith("Incorrect CURLFORM_FILECONTENT parameters");
1715 break;
1717 case 2: /* CURLFORM_FILE */
1718 if (Wosize_val(formItem) < 3)
1720 caml_failwith("Incorrect CURLFORM_FILE parameters");
1723 if (Is_long(Field(formItem, 2)) &&
1724 Long_val(Field(formItem, 2)) == 0)
1726 curl_formadd(&conn->httpPostFirst,
1727 &conn->httpPostLast,
1728 CURLFORM_COPYNAME,
1729 String_val(Field(formItem, 0)),
1730 CURLFORM_NAMELENGTH,
1731 caml_string_length(Field(formItem, 0)),
1732 CURLFORM_FILE,
1733 String_val(Field(formItem, 1)),
1734 CURLFORM_END);
1736 else if (Is_block(Field(formItem, 2)))
1738 contentType = Field(formItem, 2);
1740 curl_formadd(&conn->httpPostFirst,
1741 &conn->httpPostLast,
1742 CURLFORM_COPYNAME,
1743 String_val(Field(formItem, 0)),
1744 CURLFORM_NAMELENGTH,
1745 caml_string_length(Field(formItem, 0)),
1746 CURLFORM_FILE,
1747 String_val(Field(formItem, 1)),
1748 CURLFORM_CONTENTTYPE,
1749 String_val(Field(contentType, 0)),
1750 CURLFORM_END);
1752 else
1754 caml_failwith("Incorrect CURLFORM_FILE parameters");
1756 break;
1758 case 3: /* CURLFORM_BUFFER */
1759 if (Wosize_val(formItem) < 4)
1761 caml_failwith("Incorrect CURLFORM_BUFFER parameters");
1764 if (Is_long(Field(formItem, 3)) &&
1765 Long_val(Field(formItem, 3)) == 0)
1767 conn->httpPostBuffers = curl_slist_prepend_ml(conn->httpPostBuffers, Field(formItem, 2));
1769 curl_formadd(&conn->httpPostFirst,
1770 &conn->httpPostLast,
1771 CURLFORM_COPYNAME,
1772 String_val(Field(formItem, 0)),
1773 CURLFORM_NAMELENGTH,
1774 caml_string_length(Field(formItem, 0)),
1775 CURLFORM_BUFFER,
1776 String_val(Field(formItem, 1)),
1777 CURLFORM_BUFFERPTR,
1778 conn->httpPostBuffers->data,
1779 CURLFORM_BUFFERLENGTH,
1780 caml_string_length(Field(formItem, 2)),
1781 CURLFORM_END);
1783 else if (Is_block(Field(formItem, 3)))
1785 conn->httpPostBuffers = curl_slist_prepend_ml(conn->httpPostBuffers, Field(formItem, 2));
1787 contentType = Field(formItem, 3);
1789 curl_formadd(&conn->httpPostFirst,
1790 &conn->httpPostLast,
1791 CURLFORM_COPYNAME,
1792 String_val(Field(formItem, 0)),
1793 CURLFORM_NAMELENGTH,
1794 caml_string_length(Field(formItem, 0)),
1795 CURLFORM_BUFFER,
1796 String_val(Field(formItem, 1)),
1797 CURLFORM_BUFFERPTR,
1798 conn->httpPostBuffers->data,
1799 CURLFORM_BUFFERLENGTH,
1800 caml_string_length(Field(formItem, 2)),
1801 CURLFORM_CONTENTTYPE,
1802 String_val(Field(contentType, 0)),
1803 CURLFORM_END);
1805 else
1807 caml_failwith("Incorrect CURLFORM_BUFFER parameters");
1809 break;
1812 listIter = Field(listIter, 1);
1815 result = curl_easy_setopt(conn->connection,
1816 CURLOPT_HTTPPOST,
1817 conn->httpPostFirst);
1819 if (result != CURLE_OK)
1820 raiseError(conn, result);
1822 CAMLreturn0;
1825 SETOPT_STRING( SSLCERT)
1826 SETOPT_STRING( SSLCERTTYPE)
1827 SETOPT_STRING( SSLCERTPASSWD)
1828 SETOPT_STRING( SSLKEY)
1829 SETOPT_STRING( SSLKEYTYPE)
1830 SETOPT_STRING( SSLKEYPASSWD)
1831 SETOPT_STRING( SSLENGINE)
1832 SETOPT_BOOL( SSLENGINE_DEFAULT)
1833 SETOPT_BOOL( CRLF)
1835 SETOPT_SLIST( QUOTE)
1836 SETOPT_SLIST( POSTQUOTE)
1838 SETOPT_STRING( COOKIEFILE)
1839 SETOPT_LONG( SSLVERSION)
1841 static void handle_TIMECONDITION(Connection *conn, value option)
1843 CAMLparam1(option);
1844 CURLcode result = CURLE_OK;
1845 int timecond = CURL_TIMECOND_NONE;
1847 switch (Long_val(option))
1849 case 0: timecond = CURL_TIMECOND_NONE; break;
1850 case 1: timecond = CURL_TIMECOND_IFMODSINCE; break;
1851 case 2: timecond = CURL_TIMECOND_IFUNMODSINCE; break;
1852 case 3: timecond = CURL_TIMECOND_LASTMOD; break;
1853 default:
1854 caml_failwith("Invalid TIMECOND Option");
1855 break;
1858 result = curl_easy_setopt(conn->connection, CURLOPT_TIMECONDITION, timecond);
1860 if (result != CURLE_OK)
1861 raiseError(conn, result);
1863 CAMLreturn0;
1866 SETOPT_VAL( TIMEVALUE, Int32_val)
1867 SETOPT_STRING( CUSTOMREQUEST)
1868 SETOPT_STRING( INTERFACE)
1870 static void handle_KRB4LEVEL(Connection *conn, value option)
1872 CAMLparam1(option);
1873 CURLcode result = CURLE_OK;
1875 switch (Long_val(option))
1877 case 0: /* KRB4_NONE */
1878 result = curl_easy_setopt(conn->connection,
1879 CURLOPT_KRB4LEVEL,
1880 NULL);
1881 break;
1883 case 1: /* KRB4_CLEAR */
1884 result = curl_easy_setopt(conn->connection,
1885 CURLOPT_KRB4LEVEL,
1886 "clear");
1887 break;
1889 case 2: /* KRB4_SAFE */
1890 result = curl_easy_setopt(conn->connection,
1891 CURLOPT_KRB4LEVEL,
1892 "safe");
1893 break;
1895 case 3: /* KRB4_CONFIDENTIAL */
1896 result = curl_easy_setopt(conn->connection,
1897 CURLOPT_KRB4LEVEL,
1898 "confidential");
1899 break;
1901 case 4: /* KRB4_PRIVATE */
1902 result = curl_easy_setopt(conn->connection,
1903 CURLOPT_KRB4LEVEL,
1904 "private");
1905 break;
1907 default:
1908 caml_failwith("Invalid KRB4 Option");
1909 break;
1912 if (result != CURLE_OK)
1913 raiseError(conn, result);
1915 CAMLreturn0;
1918 SETOPT_BOOL( SSL_VERIFYPEER)
1919 SETOPT_STRING( CAINFO)
1920 SETOPT_STRING( CAPATH)
1921 SETOPT_BOOL( FILETIME)
1922 SETOPT_LONG( MAXREDIRS)
1923 SETOPT_LONG( MAXCONNECTS)
1925 static void handle_CLOSEPOLICY(Connection *conn, value option)
1927 CAMLparam1(option);
1928 CURLcode result = CURLE_OK;
1930 switch (Long_val(option))
1932 case 0: /* CLOSEPOLICY_OLDEST */
1933 result = curl_easy_setopt(conn->connection,
1934 CURLOPT_CLOSEPOLICY,
1935 CURLCLOSEPOLICY_OLDEST);
1936 break;
1938 case 1: /* CLOSEPOLICY_LEAST_RECENTLY_USED */
1939 result = curl_easy_setopt(conn->connection,
1940 CURLOPT_CLOSEPOLICY,
1941 CURLCLOSEPOLICY_LEAST_RECENTLY_USED);
1942 break;
1944 default:
1945 caml_failwith("Invalid CLOSEPOLICY Option");
1946 break;
1949 if (result != CURLE_OK)
1950 raiseError(conn, result);
1952 CAMLreturn0;
1955 SETOPT_BOOL( FRESH_CONNECT)
1956 SETOPT_BOOL( FORBID_REUSE)
1957 SETOPT_STRING( RANDOM_FILE)
1958 SETOPT_STRING( EGDSOCKET)
1959 SETOPT_LONG( CONNECTTIMEOUT)
1960 SETOPT_BOOL( HTTPGET)
1962 static void handle_SSL_VERIFYHOST(Connection *conn, value option)
1964 CAMLparam1(option);
1965 CURLcode result = CURLE_OK;
1967 switch (Long_val(option))
1969 case 0: /* SSLVERIFYHOST_NONE */
1970 case 1: /* SSLVERIFYHOST_EXISTENCE */
1971 case 2: /* SSLVERIFYHOST_HOSTNAME */
1972 result = curl_easy_setopt(conn->connection,
1973 CURLOPT_SSL_VERIFYHOST,
1974 /* map EXISTENCE to HOSTNAME */
1975 Long_val(option) == 0 ? 0 : 2);
1976 break;
1978 default:
1979 caml_failwith("Invalid SSLVERIFYHOST Option");
1980 break;
1983 if (result != CURLE_OK)
1984 raiseError(conn, result);
1986 CAMLreturn0;
1989 SETOPT_STRING( COOKIEJAR)
1990 SETOPT_STRING( SSL_CIPHER_LIST)
1992 static void handle_HTTP_VERSION(Connection *conn, value option)
1994 CAMLparam1(option);
1995 CURLcode result = CURLE_OK;
1997 long version = CURL_HTTP_VERSION_NONE;
1999 switch (Long_val(option))
2001 case 0: version = CURL_HTTP_VERSION_NONE; break;
2002 case 1: version = CURL_HTTP_VERSION_1_0; break;
2003 case 2: version = CURL_HTTP_VERSION_1_1; break;
2004 case 3:
2005 #if defined(CURL_HTTP_VERSION_2)
2006 version = CURL_HTTP_VERSION_2;
2007 #elif defined(CURL_HTTP_VERSION_2_0)
2008 version = CURL_HTTP_VERSION_2_0;
2009 #endif
2010 break;
2011 case 4:
2012 #if defined(CURL_HTTP_VERSION_2TLS)
2013 version = CURL_HTTP_VERSION_2TLS;
2014 #endif
2015 break;
2016 default:
2017 caml_invalid_argument("CURLOPT_HTTP_VERSION");
2018 break;
2021 result = curl_easy_setopt(conn->connection, CURLOPT_HTTP_VERSION, version);
2023 if (result != CURLE_OK)
2024 raiseError(conn, result);
2026 CAMLreturn0;
2029 SETOPT_BOOL( FTP_USE_EPSV)
2030 SETOPT_LONG( DNS_CACHE_TIMEOUT)
2031 SETOPT_BOOL( DNS_USE_GLOBAL_CACHE)
2033 #if HAVE_DECL_CURLOPT_PRIVATE
2034 SETOPT_STRING( PRIVATE)
2035 #endif
2037 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2038 SETOPT_SLIST( HTTP200ALIASES)
2039 #endif
2041 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2042 SETOPT_BOOL( UNRESTRICTED_AUTH)
2043 #endif
2045 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2046 SETOPT_BOOL( FTP_USE_EPRT)
2047 #endif
2049 #if HAVE_DECL_CURLOPT_HTTPAUTH
2050 static void handle_HTTPAUTH(Connection *conn, value option)
2052 CAMLparam1(option);
2053 CAMLlocal1(listIter);
2054 CURLcode result = CURLE_OK;
2055 long auth = CURLAUTH_NONE;
2057 listIter = option;
2059 while (!Is_long(listIter))
2061 switch (Long_val(Field(listIter, 0)))
2063 case 0: /* CURLAUTH_BASIC */
2064 auth |= CURLAUTH_BASIC;
2065 break;
2067 case 1: /* CURLAUTH_DIGEST */
2068 auth |= CURLAUTH_DIGEST;
2069 break;
2071 case 2: /* CURLAUTH_GSSNEGOTIATE */
2072 auth |= CURLAUTH_GSSNEGOTIATE;
2073 break;
2075 case 3: /* CURLAUTH_NTLM */
2076 auth |= CURLAUTH_NTLM;
2077 break;
2079 case 4: /* CURLAUTH_ANY */
2080 auth |= CURLAUTH_ANY;
2081 break;
2083 case 5: /* CURLAUTH_ANYSAFE */
2084 auth |= CURLAUTH_ANYSAFE;
2085 break;
2087 default:
2088 caml_failwith("Invalid HTTPAUTH Value");
2089 break;
2092 listIter = Field(listIter, 1);
2095 result = curl_easy_setopt(conn->connection,
2096 CURLOPT_HTTPAUTH,
2097 auth);
2099 if (result != CURLE_OK)
2100 raiseError(conn, result);
2102 CAMLreturn0;
2104 #endif
2106 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2107 SETOPT_BOOL( FTP_CREATE_MISSING_DIRS)
2108 #endif
2110 #if HAVE_DECL_CURLOPT_PROXYAUTH
2111 static void handle_PROXYAUTH(Connection *conn, value option)
2113 CAMLparam1(option);
2114 CAMLlocal1(listIter);
2115 CURLcode result = CURLE_OK;
2116 long auth = CURLAUTH_NONE;
2118 listIter = option;
2120 while (!Is_long(listIter))
2122 switch (Long_val(Field(listIter, 0)))
2124 case 0: /* CURLAUTH_BASIC */
2125 auth |= CURLAUTH_BASIC;
2126 break;
2128 case 1: /* CURLAUTH_DIGEST */
2129 auth |= CURLAUTH_DIGEST;
2130 break;
2132 case 2: /* CURLAUTH_GSSNEGOTIATE */
2133 auth |= CURLAUTH_GSSNEGOTIATE;
2134 break;
2136 case 3: /* CURLAUTH_NTLM */
2137 auth |= CURLAUTH_NTLM;
2138 break;
2140 case 4: /* CURLAUTH_ANY */
2141 auth |= CURLAUTH_ANY;
2142 break;
2144 case 5: /* CURLAUTH_ANYSAFE */
2145 auth |= CURLAUTH_ANYSAFE;
2146 break;
2148 default:
2149 caml_failwith("Invalid HTTPAUTH Value");
2150 break;
2153 listIter = Field(listIter, 1);
2156 result = curl_easy_setopt(conn->connection,
2157 CURLOPT_PROXYAUTH,
2158 auth);
2160 if (result != CURLE_OK)
2161 raiseError(conn, result);
2163 CAMLreturn0;
2165 #endif
2167 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2168 SETOPT_LONG( FTP_RESPONSE_TIMEOUT)
2169 #endif
2171 #if HAVE_DECL_CURLOPT_IPRESOLVE
2172 static void handle_IPRESOLVE(Connection *conn, value option)
2174 CAMLparam1(option);
2175 CURLcode result = CURLE_OK;
2177 switch (Long_val(option))
2179 case 0: /* CURL_IPRESOLVE_WHATEVER */
2180 result = curl_easy_setopt(conn->connection,
2181 CURLOPT_IPRESOLVE,
2182 CURL_IPRESOLVE_WHATEVER);
2183 break;
2185 case 1: /* CURL_IPRESOLVE_V4 */
2186 result = curl_easy_setopt(conn->connection,
2187 CURLOPT_IPRESOLVE,
2188 CURL_IPRESOLVE_V4);
2189 break;
2191 case 2: /* CURL_IPRESOLVE_V6 */
2192 result = curl_easy_setopt(conn->connection,
2193 CURLOPT_IPRESOLVE,
2194 CURL_IPRESOLVE_V6);
2195 break;
2197 default:
2198 caml_failwith("Invalid IPRESOLVE Value");
2199 break;
2202 if (result != CURLE_OK)
2203 raiseError(conn, result);
2205 CAMLreturn0;
2207 #endif
2209 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2210 SETOPT_VAL( MAXFILESIZE, Int32_val)
2211 #endif
2213 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2214 SETOPT_INT64( INFILESIZE_LARGE)
2215 #endif
2217 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2218 SETOPT_INT64( RESUME_FROM_LARGE)
2219 #endif
2221 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2222 SETOPT_INT64( MAXFILESIZE_LARGE)
2223 #endif
2225 #if HAVE_DECL_CURLOPT_NETRC_FILE
2226 SETOPT_STRING( NETRC_FILE)
2227 #endif
2229 #if HAVE_DECL_CURLOPT_FTP_SSL
2230 static void handle_FTP_SSL(Connection *conn, value option)
2232 CAMLparam1(option);
2233 CURLcode result = CURLE_OK;
2235 switch (Long_val(option))
2237 case 0: /* CURLFTPSSL_NONE */
2238 result = curl_easy_setopt(conn->connection,
2239 CURLOPT_FTP_SSL,
2240 CURLFTPSSL_NONE);
2241 break;
2243 case 1: /* CURLFTPSSL_TRY */
2244 result = curl_easy_setopt(conn->connection,
2245 CURLOPT_FTP_SSL,
2246 CURLFTPSSL_TRY);
2247 break;
2249 case 2: /* CURLFTPSSL_CONTROL */
2250 result = curl_easy_setopt(conn->connection,
2251 CURLOPT_FTP_SSL,
2252 CURLFTPSSL_CONTROL);
2253 break;
2255 case 3: /* CURLFTPSSL_ALL */
2256 result = curl_easy_setopt(conn->connection,
2257 CURLOPT_FTP_SSL,
2258 CURLFTPSSL_ALL);
2259 break;
2261 default:
2262 caml_failwith("Invalid FTP_SSL Value");
2263 break;
2266 if (result != CURLE_OK)
2267 raiseError(conn, result);
2269 CAMLreturn0;
2271 #endif
2273 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
2274 SETOPT_INT64( POSTFIELDSIZE_LARGE)
2275 #endif
2277 #if HAVE_DECL_CURLOPT_TCP_NODELAY
2278 /* not using SETOPT_BOOL here because of TCP_NODELAY defined in winsock.h */
2279 SETOPT_VAL_( handle_TCP_NODELAY, CURLOPT_TCP_NODELAY, Bool_val)
2280 #endif
2282 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
2283 static void handle_FTPSSLAUTH(Connection *conn, value option)
2285 CAMLparam1(option);
2286 CURLcode result = CURLE_OK;
2288 switch (Long_val(option))
2290 case 0: /* CURLFTPAUTH_DEFAULT */
2291 result = curl_easy_setopt(conn->connection,
2292 CURLOPT_FTPSSLAUTH,
2293 CURLFTPAUTH_DEFAULT);
2294 break;
2296 case 1: /* CURLFTPAUTH_SSL */
2297 result = curl_easy_setopt(conn->connection,
2298 CURLOPT_FTPSSLAUTH,
2299 CURLFTPAUTH_SSL);
2300 break;
2302 case 2: /* CURLFTPAUTH_TLS */
2303 result = curl_easy_setopt(conn->connection,
2304 CURLOPT_FTPSSLAUTH,
2305 CURLFTPAUTH_TLS);
2306 break;
2308 default:
2309 caml_failwith("Invalid FTPSSLAUTH value");
2310 break;
2313 if (result != CURLE_OK)
2314 raiseError(conn, result);
2316 CAMLreturn0;
2318 #endif
2320 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
2321 SETOPT_STRING( FTP_ACCOUNT)
2322 #endif
2324 #if HAVE_DECL_CURLOPT_COOKIELIST
2325 SETOPT_STRING( COOKIELIST)
2326 #endif
2328 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
2329 SETOPT_BOOL( IGNORE_CONTENT_LENGTH)
2330 #endif
2332 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
2333 SETOPT_BOOL( FTP_SKIP_PASV_IP)
2334 #endif
2336 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
2337 static void handle_FTP_FILEMETHOD(Connection *conn, value option)
2339 CAMLparam1(option);
2340 CURLcode result = CURLE_OK;
2342 switch (Long_val(option))
2344 case 0: /* CURLFTPMETHOD_DEFAULT */
2345 result = curl_easy_setopt(conn->connection,
2346 CURLOPT_FTP_FILEMETHOD,
2347 CURLFTPMETHOD_DEFAULT);
2348 break;
2350 case 1: /* CURLFTMETHOD_MULTICWD */
2351 result = curl_easy_setopt(conn->connection,
2352 CURLOPT_FTP_FILEMETHOD,
2353 CURLFTPMETHOD_MULTICWD);
2354 break;
2356 case 2: /* CURLFTPMETHOD_NOCWD */
2357 result = curl_easy_setopt(conn->connection,
2358 CURLOPT_FTP_FILEMETHOD,
2359 CURLFTPMETHOD_NOCWD);
2360 break;
2362 case 3: /* CURLFTPMETHOD_SINGLECWD */
2363 result = curl_easy_setopt(conn->connection,
2364 CURLOPT_FTP_FILEMETHOD,
2365 CURLFTPMETHOD_SINGLECWD);
2367 default:
2368 caml_failwith("Invalid FTP_FILEMETHOD value");
2369 break;
2372 if (result != CURLE_OK)
2373 raiseError(conn, result);
2375 CAMLreturn0;
2377 #endif
2379 #if HAVE_DECL_CURLOPT_LOCALPORT
2380 SETOPT_LONG( LOCALPORT)
2381 #endif
2383 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
2384 SETOPT_LONG( LOCALPORTRANGE)
2385 #endif
2387 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
2388 SETOPT_BOOL( CONNECT_ONLY)
2389 #endif
2391 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
2392 SETOPT_INT64( MAX_SEND_SPEED_LARGE)
2393 #endif
2395 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
2396 SETOPT_INT64( MAX_RECV_SPEED_LARGE)
2397 #endif
2399 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
2400 SETOPT_STRING( FTP_ALTERNATIVE_TO_USER)
2401 #endif
2403 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
2404 SETOPT_BOOL( SSL_SESSIONID_CACHE)
2405 #endif
2407 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
2408 static void handle_SSH_AUTH_TYPES(Connection *conn, value option)
2410 CAMLparam1(option);
2411 CAMLlocal1(listIter);
2412 CURLcode result = CURLE_OK;
2413 long authTypes = CURLSSH_AUTH_NONE;
2415 listIter = option;
2417 while (!Is_long(listIter))
2419 switch (Long_val(Field(listIter, 0)))
2421 case 0: /* CURLSSH_AUTH_ANY */
2422 authTypes |= CURLSSH_AUTH_ANY;
2423 break;
2425 case 1: /* CURLSSH_AUTH_PUBLICKEY */
2426 authTypes |= CURLSSH_AUTH_PUBLICKEY;
2427 break;
2429 case 2: /* CURLSSH_AUTH_PASSWORD */
2430 authTypes |= CURLSSH_AUTH_PASSWORD;
2431 break;
2433 case 3: /* CURLSSH_AUTH_HOST */
2434 authTypes |= CURLSSH_AUTH_HOST;
2435 break;
2437 case 4: /* CURLSSH_AUTH_KEYBOARD */
2438 authTypes |= CURLSSH_AUTH_KEYBOARD;
2439 break;
2441 default:
2442 caml_failwith("Invalid CURLSSH_AUTH_TYPES Value");
2443 break;
2446 listIter = Field(listIter, 1);
2449 result = curl_easy_setopt(conn->connection,
2450 CURLOPT_SSH_AUTH_TYPES,
2451 authTypes);
2453 if (result != CURLE_OK)
2454 raiseError(conn, result);
2456 CAMLreturn0;
2458 #endif
2460 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
2461 SETOPT_STRING( SSH_PUBLIC_KEYFILE)
2462 #endif
2464 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
2465 SETOPT_STRING( SSH_PRIVATE_KEYFILE)
2466 #endif
2468 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
2469 static void handle_FTP_SSL_CCC(Connection *conn, value option)
2471 CAMLparam1(option);
2472 CURLcode result = CURLE_OK;
2474 switch (Long_val(option))
2476 case 0: /* CURLFTPSSL_CCC_NONE */
2477 result = curl_easy_setopt(conn->connection,
2478 CURLOPT_FTP_SSL_CCC,
2479 CURLFTPSSL_CCC_NONE);
2480 break;
2482 case 1: /* CURLFTPSSL_CCC_PASSIVE */
2483 result = curl_easy_setopt(conn->connection,
2484 CURLOPT_FTP_SSL_CCC,
2485 CURLFTPSSL_CCC_PASSIVE);
2486 break;
2488 case 2: /* CURLFTPSSL_CCC_ACTIVE */
2489 result = curl_easy_setopt(conn->connection,
2490 CURLOPT_FTP_SSL_CCC,
2491 CURLFTPSSL_CCC_ACTIVE);
2492 break;
2494 default:
2495 caml_failwith("Invalid FTPSSL_CCC value");
2496 break;
2499 if (result != CURLE_OK)
2500 raiseError(conn, result);
2502 CAMLreturn0;
2504 #endif
2506 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
2507 SETOPT_LONG( TIMEOUT_MS)
2508 #endif
2510 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
2511 SETOPT_LONG( CONNECTTIMEOUT_MS)
2512 #endif
2514 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
2515 SETOPT_BOOL( HTTP_TRANSFER_DECODING)
2516 #endif
2518 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
2519 SETOPT_BOOL( HTTP_CONTENT_DECODING)
2520 #endif
2522 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
2523 SETOPT_LONG( NEW_FILE_PERMS)
2524 #endif
2526 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
2527 SETOPT_LONG( NEW_DIRECTORY_PERMS)
2528 #endif
2530 #if HAVE_DECL_CURLOPT_POST301
2531 SETOPT_BOOL( POST301)
2532 #endif
2534 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
2535 SETOPT_STRING( SSH_HOST_PUBLIC_KEY_MD5)
2536 #endif
2538 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
2539 SETOPT_STRING( COPYPOSTFIELDS)
2540 #endif
2542 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
2543 SETOPT_BOOL( PROXY_TRANSFER_MODE)
2544 #endif
2546 #if HAVE_DECL_CURLOPT_AUTOREFERER
2547 SETOPT_BOOL( AUTOREFERER)
2548 #endif
2550 #if HAVE_DECL_CURLOPT_PROXYTYPE
2551 static void handle_PROXYTYPE(Connection *conn, value option)
2553 CAMLparam1(option);
2554 CURLcode result = CURLE_OK;
2555 long proxy_type;
2557 switch (Long_val(option))
2559 case 0: proxy_type = CURLPROXY_HTTP; break;
2560 case 1: proxy_type = CURLPROXY_HTTP_1_0; break;
2561 case 2: proxy_type = CURLPROXY_SOCKS4; break;
2562 case 3: proxy_type = CURLPROXY_SOCKS5; break;
2563 case 4: proxy_type = CURLPROXY_SOCKS4A; break;
2564 case 5: proxy_type = CURLPROXY_SOCKS5_HOSTNAME; break;
2565 default:
2566 caml_failwith("Invalid curl proxy type");
2569 result = curl_easy_setopt(conn->connection,
2570 CURLOPT_PROXYTYPE,
2571 proxy_type);
2573 if (result != CURLE_OK)
2574 raiseError(conn, result);
2576 CAMLreturn0;
2578 #endif
2580 #if HAVE_DECL_CURLOPT_PROTOCOLS || HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
2582 long protoMap[] =
2584 CURLPROTO_ALL,
2585 CURLPROTO_HTTP, CURLPROTO_HTTPS, CURLPROTO_FTP, CURLPROTO_FTPS, CURLPROTO_SCP, CURLPROTO_SFTP,
2586 CURLPROTO_TELNET, CURLPROTO_LDAP, CURLPROTO_LDAPS, CURLPROTO_DICT, CURLPROTO_FILE, CURLPROTO_TFTP,
2587 /* factor out with autoconf? */
2588 #if defined(CURLPROTO_IMAP)
2589 CURLPROTO_IMAP,
2590 #else
2592 #endif
2593 #if defined(CURLPROTO_IMAPS)
2594 CURLPROTO_IMAPS,
2595 #else
2597 #endif
2598 #if defined(CURLPROTO_POP3)
2599 CURLPROTO_POP3,
2600 #else
2602 #endif
2603 #if defined(CURLPROTO_POP3S)
2604 CURLPROTO_POP3S,
2605 #else
2607 #endif
2608 #if defined(CURLPROTO_SMTP)
2609 CURLPROTO_SMTP,
2610 #else
2612 #endif
2613 #if defined(CURLPROTO_SMTPS)
2614 CURLPROTO_SMTPS,
2615 #else
2617 #endif
2618 #if defined(CURLPROTO_RTSP)
2619 CURLPROTO_RTSP,
2620 #else
2622 #endif
2623 #if defined(CURLPROTO_RTMP)
2624 CURLPROTO_RTMP,
2625 #else
2627 #endif
2628 #if defined(CURLPROTO_RTMPT)
2629 CURLPROTO_RTMPT,
2630 #else
2632 #endif
2633 #if defined(CURLPROTO_RTMPE)
2634 CURLPROTO_RTMPE,
2635 #else
2637 #endif
2638 #if defined(CURLPROTO_RTMPTE)
2639 CURLPROTO_RTMPTE,
2640 #else
2642 #endif
2643 #if defined(CURLPROTO_RTMPS)
2644 CURLPROTO_RTMPS,
2645 #else
2647 #endif
2648 #if defined(CURLPROTO_RTMPTS)
2649 CURLPROTO_RTMPTS,
2650 #else
2652 #endif
2653 #if defined(CURLPROTO_GOPHER)
2654 CURLPROTO_GOPHER,
2655 #else
2657 #endif
2660 static void handle_PROTOCOLSOPTION(CURLoption curlopt, Connection *conn, value option)
2662 CAMLparam1(option);
2663 CURLcode result = CURLE_OK;
2664 long bits = convert_bit_list(protoMap, sizeof(protoMap) / sizeof(protoMap[0]), option);
2666 result = curl_easy_setopt(conn->connection, curlopt, bits);
2668 if (result != CURLE_OK)
2669 raiseError(conn, result);
2671 CAMLreturn0;
2673 #endif
2675 #if HAVE_DECL_CURLOPT_PROTOCOLS
2676 static void handle_PROTOCOLS(Connection *conn, value option)
2678 handle_PROTOCOLSOPTION(CURLOPT_PROTOCOLS, conn, option);
2680 #endif
2682 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
2683 static void handle_REDIR_PROTOCOLS(Connection *conn, value option)
2685 handle_PROTOCOLSOPTION(CURLOPT_REDIR_PROTOCOLS, conn, option);
2687 #endif
2689 #if HAVE_DECL_CURLOPT_RESOLVE
2690 SETOPT_SLIST( RESOLVE)
2691 #endif
2693 #if HAVE_DECL_CURLOPT_DNS_SERVERS
2694 SETOPT_STRING( DNS_SERVERS)
2695 #endif
2697 #if HAVE_DECL_CURLOPT_MAIL_FROM
2698 SETOPT_STRING( MAIL_FROM)
2699 #endif
2701 #if HAVE_DECL_CURLOPT_MAIL_RCPT
2702 SETOPT_SLIST( MAIL_RCPT)
2703 #endif
2705 #if HAVE_DECL_CURLOPT_PIPEWAIT
2706 SETOPT_BOOL( PIPEWAIT)
2707 #endif
2710 ** curl_easy_setopt helper function
2713 #define MAP(name) { handle_ ## name, "CURLOPT_"#name, Ocaml_##name }
2714 #define MAP_NO(name) { NULL, "CURLOPT_"#name , Ocaml_##name }
2715 #define IMM(name) { handle_ ## name, "CURLOPT_"#name, -1 }
2716 #define IMM_NO(name) { NULL, "CURLOPT_"#name , -1 }
2718 CURLOptionMapping implementedOptionMap[] =
2720 MAP(WRITEFUNCTION),
2721 MAP(READFUNCTION),
2722 IMM(INFILESIZE),
2723 MAP(URL),
2724 MAP(PROXY),
2725 IMM(PROXYPORT),
2726 IMM(HTTPPROXYTUNNEL),
2727 IMM(VERBOSE),
2728 IMM(HEADER),
2729 IMM(NOPROGRESS),
2730 #if HAVE_DECL_CURLOPT_NOSIGNAL
2731 IMM(NOSIGNAL),
2732 #else
2733 IMM_NO(NOSIGNAL),
2734 #endif
2735 IMM(NOBODY),
2736 IMM(FAILONERROR),
2737 IMM(UPLOAD),
2738 IMM(POST),
2739 IMM(FTPLISTONLY),
2740 IMM(FTPAPPEND),
2741 IMM(NETRC),
2742 #if HAVE_DECL_CURLOPT_ENCODING
2743 IMM(ENCODING),
2744 #else
2745 IMM_NO(ENCODING),
2746 #endif
2747 IMM(FOLLOWLOCATION),
2748 IMM(TRANSFERTEXT),
2749 IMM(PUT),
2750 MAP(USERPWD),
2751 MAP(PROXYUSERPWD),
2752 MAP(RANGE),
2753 IMM(ERRORBUFFER), /* mutable buffer, as output value, do not duplicate */
2754 IMM(TIMEOUT),
2755 MAP(POSTFIELDS),
2756 IMM(POSTFIELDSIZE),
2757 MAP(REFERER),
2758 MAP(USERAGENT),
2759 MAP(FTPPORT),
2760 IMM(LOW_SPEED_LIMIT),
2761 IMM(LOW_SPEED_TIME),
2762 IMM(RESUME_FROM),
2763 MAP(COOKIE),
2764 MAP(HTTPHEADER),
2765 MAP(HTTPPOST),
2766 MAP(SSLCERT),
2767 MAP(SSLCERTTYPE),
2768 MAP(SSLCERTPASSWD),
2769 MAP(SSLKEY),
2770 MAP(SSLKEYTYPE),
2771 MAP(SSLKEYPASSWD),
2772 MAP(SSLENGINE),
2773 IMM(SSLENGINE_DEFAULT),
2774 IMM(CRLF),
2775 MAP(QUOTE),
2776 MAP(POSTQUOTE),
2777 MAP(HEADERFUNCTION),
2778 MAP(COOKIEFILE),
2779 IMM(SSLVERSION),
2780 IMM(TIMECONDITION),
2781 IMM(TIMEVALUE),
2782 MAP(CUSTOMREQUEST),
2783 MAP(INTERFACE),
2784 IMM(KRB4LEVEL),
2785 MAP(PROGRESSFUNCTION),
2786 IMM(SSL_VERIFYPEER),
2787 MAP(CAINFO),
2788 MAP(CAPATH),
2789 IMM(FILETIME),
2790 IMM(MAXREDIRS),
2791 IMM(MAXCONNECTS),
2792 IMM(CLOSEPOLICY),
2793 IMM(FRESH_CONNECT),
2794 IMM(FORBID_REUSE),
2795 MAP(RANDOM_FILE),
2796 MAP(EGDSOCKET),
2797 IMM(CONNECTTIMEOUT),
2798 IMM(HTTPGET),
2799 IMM(SSL_VERIFYHOST),
2800 MAP(COOKIEJAR),
2801 MAP(SSL_CIPHER_LIST),
2802 IMM(HTTP_VERSION),
2803 IMM(FTP_USE_EPSV),
2804 IMM(DNS_CACHE_TIMEOUT),
2805 IMM(DNS_USE_GLOBAL_CACHE),
2806 MAP(DEBUGFUNCTION),
2807 #if HAVE_DECL_CURLOPT_PRIVATE
2808 MAP(PRIVATE),
2809 #else
2810 MAP_NO(PRIVATE),
2811 #endif
2812 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2813 MAP(HTTP200ALIASES),
2814 #else
2815 MAP_NO(HTTP200ALIASES),
2816 #endif
2817 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2818 IMM(UNRESTRICTED_AUTH),
2819 #else
2820 IMM_NO(UNRESTRICTED_AUTH),
2821 #endif
2822 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2823 IMM(FTP_USE_EPRT),
2824 #else
2825 IMM_NO(FTP_USE_EPRT),
2826 #endif
2827 #if HAVE_DECL_CURLOPT_HTTPAUTH
2828 IMM(HTTPAUTH),
2829 #else
2830 IMM_NO(HTTPAUTH),
2831 #endif
2832 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2833 IMM(FTP_CREATE_MISSING_DIRS),
2834 #else
2835 IMM_NO(FTP_CREATE_MISSING_DIRS),
2836 #endif
2837 #if HAVE_DECL_CURLOPT_PROXYAUTH
2838 IMM(PROXYAUTH),
2839 #else
2840 IMM_NO(PROXYAUTH),
2841 #endif
2842 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2843 IMM(FTP_RESPONSE_TIMEOUT),
2844 #else
2845 IMM_NO(FTP_RESPONSE_TIMEOUT),
2846 #endif
2847 #if HAVE_DECL_CURLOPT_IPRESOLVE
2848 IMM(IPRESOLVE),
2849 #else
2850 IMM_NO(IPRESOLVE),
2851 #endif
2852 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2853 IMM(MAXFILESIZE),
2854 #else
2855 IMM_NO(MAXFILESIZE),
2856 #endif
2857 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2858 IMM(INFILESIZE_LARGE),
2859 #else
2860 IMM_NO(INFILESIZE_LARGE),
2861 #endif
2862 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2863 IMM(RESUME_FROM_LARGE),
2864 #else
2865 IMM_NO(RESUME_FROM_LARGE),
2866 #endif
2867 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2868 IMM(MAXFILESIZE_LARGE),
2869 #else
2870 IMM_NO(MAXFILESIZE_LARGE),
2871 #endif
2872 #if HAVE_DECL_CURLOPT_NETRC_FILE
2873 MAP(NETRC_FILE),
2874 #else
2875 MAP_NO(NETRC_FILE),
2876 #endif
2877 #if HAVE_DECL_CURLOPT_FTP_SSL
2878 IMM(FTP_SSL),
2879 #else
2880 IMM_NO(FTP_SSL),
2881 #endif
2882 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
2883 IMM(POSTFIELDSIZE_LARGE),
2884 #else
2885 IMM_NO(POSTFIELDSIZE_LARGE),
2886 #endif
2887 #if HAVE_DECL_CURLOPT_TCP_NODELAY
2888 IMM(TCP_NODELAY),
2889 #else
2890 IMM_NO(TCP_NODELAY),
2891 #endif
2892 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
2893 IMM(FTPSSLAUTH),
2894 #else
2895 IMM_NO(FTPSSLAUTH),
2896 #endif
2897 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
2898 MAP(IOCTLFUNCTION),
2899 #else
2900 MAP_NO(IOCTLFUNCTION),
2901 #endif
2902 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
2903 MAP(FTP_ACCOUNT),
2904 #else
2905 MAP_NO(FTP_ACCOUNT),
2906 #endif
2907 #if HAVE_DECL_CURLOPT_COOKIELIST
2908 MAP(COOKIELIST),
2909 #else
2910 MAP_NO(COOKIELIST),
2911 #endif
2912 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
2913 IMM(IGNORE_CONTENT_LENGTH),
2914 #else
2915 IMM_NO(IGNORE_CONTENT_LENGTH),
2916 #endif
2917 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
2918 IMM(FTP_SKIP_PASV_IP),
2919 #else
2920 IMM_NO(FTP_SKIP_PASV_IP),
2921 #endif
2922 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
2923 IMM(FTP_FILEMETHOD),
2924 #else
2925 IMM_NO(FTP_FILEMETHOD),
2926 #endif
2927 #if HAVE_DECL_CURLOPT_LOCALPORT
2928 IMM(LOCALPORT),
2929 #else
2930 IMM_NO(LOCALPORT),
2931 #endif
2932 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
2933 IMM(LOCALPORTRANGE),
2934 #else
2935 IMM_NO(LOCALPORTRANGE),
2936 #endif
2937 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
2938 IMM(CONNECT_ONLY),
2939 #else
2940 IMM_NO(CONNECT_ONLY),
2941 #endif
2942 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
2943 IMM(MAX_SEND_SPEED_LARGE),
2944 #else
2945 IMM_NO(MAX_SEND_SPEED_LARGE),
2946 #endif
2947 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
2948 IMM(MAX_RECV_SPEED_LARGE),
2949 #else
2950 IMM_NO(MAX_RECV_SPEED_LARGE),
2951 #endif
2952 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
2953 MAP(FTP_ALTERNATIVE_TO_USER),
2954 #else
2955 MAP_NO(FTP_ALTERNATIVE_TO_USER),
2956 #endif
2957 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
2958 IMM(SSL_SESSIONID_CACHE),
2959 #else
2960 IMM_NO(SSL_SESSIONID_CACHE),
2961 #endif
2962 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
2963 IMM(SSH_AUTH_TYPES),
2964 #else
2965 IMM_NO(SSH_AUTH_TYPES),
2966 #endif
2967 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
2968 MAP(SSH_PUBLIC_KEYFILE),
2969 #else
2970 MAP_NO(SSH_PUBLIC_KEYFILE),
2971 #endif
2972 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
2973 MAP(SSH_PRIVATE_KEYFILE),
2974 #else
2975 MAP_NO(SSH_PRIVATE_KEYFILE),
2976 #endif
2977 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
2978 IMM(FTP_SSL_CCC),
2979 #else
2980 IMM_NO(FTP_SSL_CCC),
2981 #endif
2982 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
2983 IMM(TIMEOUT_MS),
2984 #else
2985 IMM_NO(TIMEOUT_MS),
2986 #endif
2987 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
2988 IMM(CONNECTTIMEOUT_MS),
2989 #else
2990 IMM_NO(CONNECTTIMEOUT_MS),
2991 #endif
2992 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
2993 IMM(HTTP_TRANSFER_DECODING),
2994 #else
2995 IMM_NO(HTTP_TRANSFER_DECODING),
2996 #endif
2997 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
2998 IMM(HTTP_CONTENT_DECODING),
2999 #else
3000 IMM_NO(HTTP_CONTENT_DECODING),
3001 #endif
3002 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
3003 IMM(NEW_FILE_PERMS),
3004 #else
3005 IMM_NO(NEW_FILE_PERMS),
3006 #endif
3007 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
3008 IMM(NEW_DIRECTORY_PERMS),
3009 #else
3010 IMM_NO(NEW_DIRECTORY_PERMS),
3011 #endif
3012 #if HAVE_DECL_CURLOPT_POST301
3013 IMM(POST301),
3014 #else
3015 IMM_NO(POST301),
3016 #endif
3017 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
3018 MAP(SSH_HOST_PUBLIC_KEY_MD5),
3019 #else
3020 MAP_NO(SSH_HOST_PUBLIC_KEY_MD5),
3021 #endif
3022 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
3023 MAP(COPYPOSTFIELDS),
3024 #else
3025 MAP_NO(COPYPOSTFIELDS),
3026 #endif
3027 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
3028 IMM(PROXY_TRANSFER_MODE),
3029 #else
3030 IMM_NO(PROXY_TRANSFER_MODE),
3031 #endif
3032 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
3033 MAP(SEEKFUNCTION),
3034 #else
3035 MAP_NO(SEEKFUNCTION),
3036 #endif
3037 #if HAVE_DECL_CURLOPT_AUTOREFERER
3038 IMM(AUTOREFERER),
3039 #else
3040 IMM_NO(AUTOREFERER),
3041 #endif
3042 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
3043 MAP(OPENSOCKETFUNCTION),
3044 #else
3045 MAP_NO(OPENSOCKETFUNCTION),
3046 #endif
3047 #if HAVE_DECL_CURLOPT_PROXYTYPE
3048 IMM(PROXYTYPE),
3049 #else
3050 IMM_NO(PROXYTYPE),
3051 #endif
3052 #if HAVE_DECL_CURLOPT_PROTOCOLS
3053 IMM(PROTOCOLS),
3054 #else
3055 IMM_NO(PROTOCOLS),
3056 #endif
3057 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3058 IMM(REDIR_PROTOCOLS),
3059 #else
3060 IMM_NO(REDIR_PROTOCOLS),
3061 #endif
3062 #if HAVE_DECL_CURLOPT_RESOLVE
3063 MAP(RESOLVE),
3064 #else
3065 MAP_NO(RESOLVE),
3066 #endif
3067 #if HAVE_DECL_CURLOPT_DNS_SERVERS
3068 MAP(DNS_SERVERS),
3069 #else
3070 MAP_NO(DNS_SERVERS),
3071 #endif
3072 #if HAVE_DECL_CURLOPT_MAIL_FROM
3073 MAP(MAIL_FROM),
3074 #else
3075 MAP_NO(MAIL_FROM),
3076 #endif
3077 #if HAVE_DECL_CURLOPT_MAIL_RCPT
3078 MAP(MAIL_RCPT),
3079 #else
3080 MAP_NO(MAIL_RCPT),
3081 #endif
3082 #if HAVE_DECL_CURLOPT_PIPEWAIT
3083 IMM(PIPEWAIT),
3084 #else
3085 IMM_NO(PIPEWAIT),
3086 #endif
3089 static Connection *duplicateConnection(Connection *original)
3091 Connection *connection = NULL;
3092 CURL* h = NULL;
3093 size_t i = 0;
3094 CURLOptionMapping* this = NULL;
3096 caml_enter_blocking_section();
3097 h = curl_easy_duphandle(original->connection);
3098 caml_leave_blocking_section();
3100 connection = allocConnection(h);
3102 for (i = 0; i < sizeof(implementedOptionMap)/sizeof(CURLOptionMapping); i++)
3104 this = &implementedOptionMap[i];
3105 if (-1 == this->ocamlValue) continue;
3106 if (this->optionHandler && (Field(original->ocamlValues, this->ocamlValue) != Val_unit))
3108 this->optionHandler(connection, Field(original->ocamlValues, this->ocamlValue));
3112 return connection;
3115 CAMLprim value helper_curl_easy_setopt(value conn, value option)
3117 CAMLparam2(conn, option);
3118 CAMLlocal1(data);
3119 Connection *connection = Connection_val(conn);
3120 CURLOptionMapping* thisOption = NULL;
3121 static value* exception = NULL;
3123 checkConnection(connection);
3125 data = Field(option, 0);
3127 if (Tag_val(option) < sizeof(implementedOptionMap)/sizeof(CURLOptionMapping))
3129 thisOption = &implementedOptionMap[Tag_val(option)];
3130 if (thisOption->optionHandler)
3132 thisOption->optionHandler(connection, data);
3134 else
3136 if (NULL == exception)
3138 exception = caml_named_value("Curl.NotImplemented");
3139 if (NULL == exception) caml_invalid_argument("Curl.NotImplemented");
3142 caml_raise_with_string(*exception, thisOption->name);
3145 else
3147 caml_failwith("Invalid CURLOPT Option");
3150 CAMLreturn(Val_unit);
3154 ** curl_easy_perform helper function
3157 CAMLprim value helper_curl_easy_perform(value conn)
3159 CAMLparam1(conn);
3160 CURLcode result = CURLE_OK;
3161 Connection *connection = Connection_val(conn);
3163 checkConnection(connection);
3165 caml_enter_blocking_section();
3166 result = curl_easy_perform(connection->connection);
3167 caml_leave_blocking_section();
3169 if (result != CURLE_OK)
3170 raiseError(connection, result);
3172 CAMLreturn(Val_unit);
3176 ** curl_easy_cleanup helper function
3179 CAMLprim value helper_curl_easy_cleanup(value conn)
3181 CAMLparam1(conn);
3182 Connection *connection = Connection_val(conn);
3184 checkConnection(connection);
3186 removeConnection(connection, 0);
3188 CAMLreturn(Val_unit);
3192 ** curl_easy_duphandle helper function
3195 CAMLprim value helper_curl_easy_duphandle(value conn)
3197 CAMLparam1(conn);
3198 CAMLlocal1(result);
3199 Connection *connection = Connection_val(conn);
3201 checkConnection(connection);
3203 result = caml_curl_alloc(duplicateConnection(connection));
3205 CAMLreturn(result);
3209 ** curl_easy_getinfo helper function
3212 enum GetInfoResultType {
3213 StringValue, LongValue, DoubleValue, StringListValue
3216 value convertStringList(struct curl_slist *slist)
3218 CAMLparam0();
3219 CAMLlocal3(result, current, next);
3220 struct curl_slist *p = slist;
3222 result = Val_int(0);
3223 current = Val_int(0);
3224 next = Val_int(0);
3226 while (p != NULL)
3228 next = caml_alloc_tuple(2);
3229 Store_field(next, 0, caml_copy_string(p->data));
3230 Store_field(next, 1, Val_int(0));
3232 if (result == Val_int(0))
3233 result = next;
3235 if (current != Val_int(0))
3236 Store_field(current, 1, next);
3238 current = next;
3240 p = p->next;
3243 curl_slist_free_all(slist);
3245 CAMLreturn(result);
3248 CAMLprim value helper_curl_easy_getinfo(value conn, value option)
3250 CAMLparam2(conn, option);
3251 CAMLlocal1(result);
3252 CURLcode curlResult;
3253 Connection *connection = Connection_val(conn);
3254 enum GetInfoResultType resultType;
3255 char *strValue = NULL;
3256 double doubleValue;
3257 long longValue;
3258 struct curl_slist *stringListValue = NULL;
3260 checkConnection(connection);
3262 switch(Long_val(option))
3264 #if HAVE_DECL_CURLINFO_EFFECTIVE_URL
3265 case 0: /* CURLINFO_EFFECTIVE_URL */
3266 resultType = StringValue;
3268 curlResult = curl_easy_getinfo(connection->connection,
3269 CURLINFO_EFFECTIVE_URL,
3270 &strValue);
3271 break;
3272 #else
3273 #pragma message("libcurl does not provide CURLINFO_EFFECTIVE_URL")
3274 #endif
3276 #if HAVE_DECL_CURLINFO_RESPONSE_CODE || HAVE_DECL_CURLINFO_HTTP_CODE
3277 case 1: /* CURLINFO_HTTP_CODE */
3278 case 2: /* CURLINFO_RESPONSE_CODE */
3279 #if HAVE_DECL_CURLINFO_RESPONSE_CODE
3280 resultType = LongValue;
3282 curlResult = curl_easy_getinfo(connection->connection,
3283 CURLINFO_RESPONSE_CODE,
3284 &longValue);
3285 #else
3286 resultType = LongValue;
3288 curlResult = curl_easy_getinfo(connection->connection,
3289 CURLINFO_HTTP_CODE,
3290 &longValue);
3291 #endif
3292 break;
3293 #endif
3295 #if HAVE_DECL_CURLINFO_TOTAL_TIME
3296 case 3: /* CURLINFO_TOTAL_TIME */
3297 resultType = DoubleValue;
3299 curlResult = curl_easy_getinfo(connection->connection,
3300 CURLINFO_TOTAL_TIME,
3301 &doubleValue);
3302 break;
3303 #endif
3305 #if HAVE_DECL_CURLINFO_NAMELOOKUP_TIME
3306 case 4: /* CURLINFO_NAMELOOKUP_TIME */
3307 resultType = DoubleValue;
3309 curlResult = curl_easy_getinfo(connection->connection,
3310 CURLINFO_NAMELOOKUP_TIME,
3311 &doubleValue);
3312 break;
3313 #endif
3315 #if HAVE_DECL_CURLINFO_CONNECT_TIME
3316 case 5: /* CURLINFO_CONNECT_TIME */
3317 resultType = DoubleValue;
3319 curlResult = curl_easy_getinfo(connection->connection,
3320 CURLINFO_CONNECT_TIME,
3321 &doubleValue);
3322 break;
3323 #endif
3325 #if HAVE_DECL_CURLINFO_PRETRANSFER_TIME
3326 case 6: /* CURLINFO_PRETRANSFER_TIME */
3327 resultType = DoubleValue;
3329 curlResult = curl_easy_getinfo(connection->connection,
3330 CURLINFO_PRETRANSFER_TIME,
3331 &doubleValue);
3332 break;
3333 #endif
3335 #if HAVE_DECL_CURLINFO_SIZE_UPLOAD
3336 case 7: /* CURLINFO_SIZE_UPLOAD */
3337 resultType = DoubleValue;
3339 curlResult = curl_easy_getinfo(connection->connection,
3340 CURLINFO_SIZE_UPLOAD,
3341 &doubleValue);
3342 break;
3343 #endif
3345 #if HAVE_DECL_CURLINFO_SIZE_DOWNLOAD
3346 case 8: /* CURLINFO_SIZE_DOWNLOAD */
3347 resultType = DoubleValue;
3349 curlResult = curl_easy_getinfo(connection->connection,
3350 CURLINFO_SIZE_DOWNLOAD,
3351 &doubleValue);
3352 break;
3353 #endif
3355 #if HAVE_DECL_CURLINFO_SPEED_DOWNLOAD
3356 case 9: /* CURLINFO_SPEED_DOWNLOAD */
3357 resultType = DoubleValue;
3359 curlResult = curl_easy_getinfo(connection->connection,
3360 CURLINFO_SPEED_DOWNLOAD,
3361 &doubleValue);
3362 break;
3363 #endif
3365 #if HAVE_DECL_CURLINFO_SPEED_UPLOAD
3366 case 10: /* CURLINFO_SPEED_UPLOAD */
3367 resultType = DoubleValue;
3369 curlResult = curl_easy_getinfo(connection->connection,
3370 CURLINFO_SPEED_UPLOAD,
3371 &doubleValue);
3372 break;
3374 #endif
3376 #if HAVE_DECL_CURLINFO_HEADER_SIZE
3377 case 11: /* CURLINFO_HEADER_SIZE */
3378 resultType = LongValue;
3380 curlResult = curl_easy_getinfo(connection->connection,
3381 CURLINFO_HEADER_SIZE,
3382 &longValue);
3383 break;
3384 #endif
3386 #if HAVE_DECL_CURLINFO_REQUEST_SIZE
3387 case 12: /* CURLINFO_REQUEST_SIZE */
3388 resultType = LongValue;
3390 curlResult = curl_easy_getinfo(connection->connection,
3391 CURLINFO_REQUEST_SIZE,
3392 &longValue);
3393 break;
3394 #endif
3396 #if HAVE_DECL_CURLINFO_SSL_VERIFYRESULT
3397 case 13: /* CURLINFO_SSL_VERIFYRESULT */
3398 resultType = LongValue;
3400 curlResult = curl_easy_getinfo(connection->connection,
3401 CURLINFO_SSL_VERIFYRESULT,
3402 &longValue);
3403 break;
3404 #endif
3406 #if HAVE_DECL_CURLINFO_FILETIME
3407 case 14: /* CURLINFO_FILETIME */
3408 resultType = DoubleValue;
3410 curlResult = curl_easy_getinfo(connection->connection,
3411 CURLINFO_FILETIME,
3412 &longValue);
3414 doubleValue = longValue;
3415 break;
3416 #endif
3418 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_DOWNLOAD
3419 case 15: /* CURLINFO_CONTENT_LENGTH_DOWNLOAD */
3420 resultType = DoubleValue;
3422 curlResult = curl_easy_getinfo(connection->connection,
3423 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
3424 &doubleValue);
3425 break;
3426 #endif
3428 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_UPLOAD
3429 case 16: /* CURLINFO_CONTENT_LENGTH_UPLOAD */
3430 resultType = DoubleValue;
3432 curlResult = curl_easy_getinfo(connection->connection,
3433 CURLINFO_CONTENT_LENGTH_UPLOAD,
3434 &doubleValue);
3435 break;
3436 #endif
3438 #if HAVE_DECL_CURLINFO_STARTTRANSFER_TIME
3439 case 17: /* CURLINFO_STARTTRANSFER_TIME */
3440 resultType = DoubleValue;
3442 curlResult = curl_easy_getinfo(connection->connection,
3443 CURLINFO_STARTTRANSFER_TIME,
3444 &doubleValue);
3445 break;
3446 #endif
3448 #if HAVE_DECL_CURLINFO_CONTENT_TYPE
3449 case 18: /* CURLINFO_CONTENT_TYPE */
3450 resultType = StringValue;
3452 curlResult = curl_easy_getinfo(connection->connection,
3453 CURLINFO_CONTENT_TYPE,
3454 &strValue);
3455 break;
3456 #endif
3458 #if HAVE_DECL_CURLINFO_REDIRECT_TIME
3459 case 19: /* CURLINFO_REDIRECT_TIME */
3460 resultType = DoubleValue;
3462 curlResult = curl_easy_getinfo(connection->connection,
3463 CURLINFO_REDIRECT_TIME,
3464 &doubleValue);
3465 break;
3466 #endif
3468 #if HAVE_DECL_CURLINFO_REDIRECT_COUNT
3469 case 20: /* CURLINFO_REDIRECT_COUNT */
3470 resultType = LongValue;
3472 curlResult = curl_easy_getinfo(connection->connection,
3473 CURLINFO_REDIRECT_COUNT,
3474 &longValue);
3475 break;
3476 #endif
3478 #if HAVE_DECL_CURLINFO_PRIVATE
3479 case 21: /* CURLINFO_PRIVATE */
3480 resultType = StringValue;
3482 curlResult = curl_easy_getinfo(connection->connection,
3483 CURLINFO_PRIVATE,
3484 &strValue);
3485 break;
3486 #endif
3488 #if HAVE_DECL_CURLINFO_HTTP_CONNECTCODE
3489 case 22: /* CURLINFO_HTTP_CONNECTCODE */
3490 resultType = LongValue;
3492 curlResult = curl_easy_getinfo(connection->connection,
3493 CURLINFO_HTTP_CONNECTCODE,
3494 &longValue);
3495 break;
3496 #endif
3498 #if HAVE_DECL_CURLINFO_HTTPAUTH_AVAIL
3499 case 23: /* CURLINFO_HTTPAUTH_AVAIL */
3500 resultType = LongValue;
3502 curlResult = curl_easy_getinfo(connection->connection,
3503 CURLINFO_HTTPAUTH_AVAIL,
3504 &longValue);
3505 break;
3506 #endif
3508 #if HAVE_DECL_CURLINFO_PROXYAUTH_AVAIL
3509 case 24: /* CURLINFO_PROXYAUTH_AVAIL */
3510 resultType = LongValue;
3512 curlResult = curl_easy_getinfo(connection->connection,
3513 CURLINFO_PROXYAUTH_AVAIL,
3514 &longValue);
3515 break;
3516 #endif
3518 #if HAVE_DECL_CURLINFO_OS_ERRNO
3519 case 25: /* CURLINFO_OS_ERRNO */
3520 resultType = LongValue;
3522 curlResult = curl_easy_getinfo(connection->connection,
3523 CURLINFO_OS_ERRNO,
3524 &longValue);
3525 break;
3526 #endif
3528 #if HAVE_DECL_CURLINFO_NUM_CONNECTS
3529 case 26: /* CURLINFO_NUM_CONNECTS */
3530 resultType = LongValue;
3532 curlResult = curl_easy_getinfo(connection->connection,
3533 CURLINFO_NUM_CONNECTS,
3534 &longValue);
3535 break;
3536 #endif
3538 #if HAVE_DECL_CURLINFO_SSL_ENGINES
3539 case 27: /* CURLINFO_SSL_ENGINES */
3540 resultType = StringListValue;
3542 curlResult = curl_easy_getinfo(connection->connection,
3543 CURLINFO_SSL_ENGINES,
3544 &stringListValue);
3545 break;
3546 #endif
3548 #if HAVE_DECL_CURLINFO_COOKIELIST
3549 case 28: /* CURLINFO_COOKIELIST */
3550 resultType = StringListValue;
3552 curlResult = curl_easy_getinfo(connection->connection,
3553 CURLINFO_COOKIELIST,
3554 &stringListValue);
3555 break;
3556 #endif
3558 #if HAVE_DECL_CURLINFO_LASTSOCKET
3559 case 29: /* CURLINFO_LASTSOCKET */
3560 resultType = LongValue;
3562 curlResult = curl_easy_getinfo(connection->connection,
3563 CURLINFO_LASTSOCKET,
3564 &longValue);
3565 break;
3566 #endif
3568 #if HAVE_DECL_CURLINFO_FTP_ENTRY_PATH
3569 case 30: /* CURLINFO_FTP_ENTRY_PATH */
3570 resultType = StringValue;
3572 curlResult = curl_easy_getinfo(connection->connection,
3573 CURLINFO_FTP_ENTRY_PATH,
3574 &strValue);
3575 break;
3576 #endif
3578 #if HAVE_DECL_CURLINFO_REDIRECT_URL
3579 case 31: /* CURLINFO_REDIRECT_URL */
3580 resultType = StringValue;
3582 curlResult = curl_easy_getinfo(connection->connection,
3583 CURLINFO_REDIRECT_URL,
3584 &strValue);
3585 break;
3586 #else
3587 #pragma message("libcurl does not provide CURLINFO_REDIRECT_URL")
3588 #endif
3590 #if HAVE_DECL_CURLINFO_PRIMARY_IP
3591 case 32: /* CURLINFO_PRIMARY_IP */
3592 resultType = StringValue;
3594 curlResult = curl_easy_getinfo(connection->connection,
3595 CURLINFO_PRIMARY_IP,
3596 &strValue);
3597 break;
3598 #else
3599 #pragma message("libcurl does not provide CURLINFO_PRIMARY_IP")
3600 #endif
3602 #if HAVE_DECL_CURLINFO_LOCAL_IP
3603 case 33: /* CURLINFO_LOCAL_IP */
3604 resultType = StringValue;
3606 curlResult = curl_easy_getinfo(connection->connection,
3607 CURLINFO_LOCAL_IP,
3608 &strValue);
3609 break;
3610 #else
3611 #pragma message("libcurl does not provide CURLINFO_LOCAL_IP")
3612 #endif
3614 #if HAVE_DECL_CURLINFO_LOCAL_PORT
3615 case 34: /* CURLINFO_LOCAL_PORT */
3616 resultType = LongValue;
3618 curlResult = curl_easy_getinfo(connection->connection,
3619 CURLINFO_LOCAL_PORT,
3620 &longValue);
3621 break;
3622 #else
3623 #pragma message("libcurl does not provide CURLINFO_LOCAL_PORT")
3624 #endif
3626 #if HAVE_DECL_CURLINFO_CONDITION_UNMET
3627 case 35: /* CURLINFO_CONDITION_UNMET */
3628 resultType = LongValue;
3630 curlResult = curl_easy_getinfo(connection->connection,
3631 CURLINFO_CONDITION_UNMET,
3632 &longValue);
3633 break;
3634 #else
3635 #pragma message("libcurl does not provide CURLINFO_CONDITION_UNMET")
3636 #endif
3638 default:
3639 caml_failwith("Invalid CURLINFO Option");
3640 break;
3643 if (curlResult != CURLE_OK)
3644 raiseError(connection, curlResult);
3646 switch (resultType)
3648 case StringValue:
3649 result = caml_alloc(1, StringValue);
3650 Store_field(result, 0, caml_copy_string(strValue?strValue:""));
3651 break;
3653 case LongValue:
3654 result = caml_alloc(1, LongValue);
3655 Store_field(result, 0, Val_long(longValue));
3656 break;
3658 case DoubleValue:
3659 result = caml_alloc(1, DoubleValue);
3660 Store_field(result, 0, caml_copy_double(doubleValue));
3661 break;
3663 case StringListValue:
3664 result = caml_alloc(1, StringListValue);
3665 Store_field(result, 0, convertStringList(stringListValue));
3666 break;
3669 CAMLreturn(result);
3673 ** curl_escape helper function
3676 CAMLprim value helper_curl_escape(value str)
3678 CAMLparam1(str);
3679 CAMLlocal1(result);
3680 char *curlResult;
3682 curlResult = curl_escape(String_val(str), caml_string_length(str));
3683 result = caml_copy_string(curlResult);
3684 free(curlResult);
3686 CAMLreturn(result);
3690 ** curl_unescape helper function
3693 CAMLprim value helper_curl_unescape(value str)
3695 CAMLparam1(str);
3696 CAMLlocal1(result);
3697 char *curlResult;
3699 curlResult = curl_unescape(String_val(str), caml_string_length(str));
3700 result = caml_copy_string(curlResult);
3701 free(curlResult);
3703 CAMLreturn(result);
3707 ** curl_getdate helper function
3710 CAMLprim value helper_curl_getdate(value str, value now)
3712 CAMLparam2(str, now);
3713 CAMLlocal1(result);
3714 time_t curlResult;
3715 time_t curlNow;
3717 curlNow = (time_t)Double_val(now);
3718 curlResult = curl_getdate(String_val(str), &curlNow);
3719 result = caml_copy_double((double)curlResult);
3721 CAMLreturn(result);
3725 ** curl_version helper function
3728 CAMLprim value helper_curl_version(void)
3730 CAMLparam0();
3731 CAMLlocal1(result);
3732 char *str;
3734 str = curl_version();
3735 result = caml_copy_string(str);
3737 CAMLreturn(result);
3740 struct CURLVersionBitsMapping
3742 int code;
3743 char *name;
3746 struct CURLVersionBitsMapping versionBitsMap[] =
3748 {CURL_VERSION_IPV6, "ipv6"},
3749 {CURL_VERSION_KERBEROS4, "kerberos4"},
3750 {CURL_VERSION_SSL, "ssl"},
3751 {CURL_VERSION_LIBZ, "libz"},
3752 {CURL_VERSION_NTLM, "ntlm"},
3753 {CURL_VERSION_GSSNEGOTIATE, "gssnegotiate"},
3754 {CURL_VERSION_DEBUG, "debug"},
3755 {CURL_VERSION_CURLDEBUG, "curldebug"},
3756 {CURL_VERSION_ASYNCHDNS, "asynchdns"},
3757 {CURL_VERSION_SPNEGO, "spnego"},
3758 {CURL_VERSION_LARGEFILE, "largefile"},
3759 {CURL_VERSION_IDN, "idn"},
3760 {CURL_VERSION_SSPI, "sspi"},
3761 {CURL_VERSION_CONV, "conv"},
3762 #if HAVE_DECL_CURL_VERSION_TLSAUTH_SRP
3763 {CURL_VERSION_TLSAUTH_SRP, "srp"},
3764 #endif
3765 #if HAVE_DECL_CURL_VERSION_NTLM_WB
3766 {CURL_VERSION_NTLM_WB, "wb"},
3767 #endif
3770 CAMLprim value caml_curl_version_info(value unit)
3772 CAMLparam1(unit);
3773 CAMLlocal4(v, vlist, vnum, vfeatures);
3774 const char* const* p = NULL;
3775 size_t i = 0;
3777 curl_version_info_data* data = curl_version_info(CURLVERSION_NOW);
3778 if (NULL == data) caml_failwith("curl_version_info");
3780 vlist = Val_emptylist;
3781 for (p = data->protocols; NULL != *p; p++)
3783 vlist = Val_cons(vlist, caml_copy_string(*p));
3786 vfeatures = Val_emptylist;
3787 for (i = 0; i < sizeof(versionBitsMap)/sizeof(versionBitsMap[0]); i++)
3789 if (0 != (versionBitsMap[i].code & data->features))
3790 vfeatures = Val_cons(vfeatures, caml_copy_string(versionBitsMap[i].name));
3793 vnum = caml_alloc_tuple(3);
3794 Store_field(vnum,0,Val_int(0xFF & (data->version_num >> 16)));
3795 Store_field(vnum,1,Val_int(0xFF & (data->version_num >> 8)));
3796 Store_field(vnum,2,Val_int(0xFF & (data->version_num)));
3798 v = caml_alloc_tuple(12);
3799 Store_field(v,0,caml_copy_string(data->version));
3800 Store_field(v,1,vnum);
3801 Store_field(v,2,caml_copy_string(data->host));
3802 Store_field(v,3,vfeatures);
3803 Store_field(v,4,data->ssl_version ? Val_some(caml_copy_string(data->ssl_version)) : Val_none);
3804 Store_field(v,5,data->libz_version ? Val_some(caml_copy_string(data->libz_version)) : Val_none);
3805 Store_field(v,6,vlist);
3806 Store_field(v,7,caml_copy_string((data->age >= 1 && data->ares) ? data->ares : ""));
3807 Store_field(v,8,Val_int((data->age >= 1) ? data->ares_num : 0));
3808 Store_field(v,9,caml_copy_string((data->age >= 2 && data->libidn) ? data->libidn : ""));
3809 Store_field(v,10,Val_int((data->age >= 3) ? data->iconv_ver_num : 0));
3810 Store_field(v,11,caml_copy_string((data->age >= 3 && data->libssh_version) ? data->libssh_version : ""));
3812 CAMLreturn(v);
3815 CAMLprim value caml_curl_pause(value conn, value opts)
3817 CAMLparam2(conn, opts);
3818 CAMLlocal4(v, vlist, vnum, vfeatures);
3819 Connection *connection = Connection_val(conn);
3820 int bitmask = 0;
3821 CURLcode result;
3823 while (Val_emptylist != opts)
3825 switch (Int_val(Field(opts,0)))
3827 case 0: bitmask |= CURLPAUSE_SEND; break;
3828 case 1: bitmask |= CURLPAUSE_RECV; break;
3829 case 2: bitmask |= CURLPAUSE_ALL; break;
3830 default: caml_failwith("wrong pauseOption");
3832 opts = Field(opts,1);
3835 result = curl_easy_pause(connection->connection,bitmask);
3836 if (result != CURLE_OK)
3837 raiseError(connection, result);
3839 CAMLreturn(Val_unit);
3843 * Curl multi stack support
3845 * Exported thin wrappers for libcurl are prefixed with caml_curl_multi_.
3846 * Other exported functions are prefixed with caml_curlm_, some of them
3847 * can/should be decomposed into smaller parts.
3850 struct ml_multi_handle
3852 CURLM* handle;
3853 value values; /* callbacks */
3856 enum
3858 curlmopt_socket_function,
3859 curlmopt_timer_function,
3861 /* last, not used */
3862 multi_values_total
3865 typedef struct ml_multi_handle ml_multi_handle;
3867 #define Multi_val(v) (*(ml_multi_handle**)Data_custom_val(v))
3868 #define CURLM_val(v) (Multi_val(v)->handle)
3870 static struct custom_operations curl_multi_ops = {
3871 "ygrek.curl_multi",
3872 custom_finalize_default,
3873 custom_compare_default,
3874 custom_hash_default,
3875 custom_serialize_default,
3876 custom_deserialize_default,
3877 #if defined(custom_compare_ext_default)
3878 custom_compare_ext_default,
3879 #endif
3882 CAMLprim value caml_curl_multi_init(value unit)
3884 CAMLparam1(unit);
3885 CAMLlocal1(v);
3886 ml_multi_handle* multi = (ml_multi_handle*)caml_stat_alloc(sizeof(ml_multi_handle));
3887 CURLM* h = curl_multi_init();
3889 if (!h)
3891 caml_stat_free(multi);
3892 caml_failwith("caml_curl_multi_init");
3895 multi->handle = h;
3896 multi->values = caml_alloc(multi_values_total, 0);
3897 caml_register_generational_global_root(&multi->values);
3899 v = caml_alloc_custom(&curl_multi_ops, sizeof(ml_multi_handle*), 0, 1);
3900 Multi_val(v) = multi;
3902 CAMLreturn(v);
3905 CAMLprim value caml_curl_multi_cleanup(value handle)
3907 CAMLparam1(handle);
3908 ml_multi_handle* h = Multi_val(handle);
3910 if (NULL == h)
3911 CAMLreturn(Val_unit);
3913 caml_remove_generational_global_root(&h->values);
3915 if (CURLM_OK != curl_multi_cleanup(h->handle))
3916 caml_failwith("caml_curl_multi_cleanup");
3918 caml_stat_free(h);
3919 Multi_val(handle) = (ml_multi_handle*)NULL;
3921 CAMLreturn(Val_unit);
3924 static CURL* curlm_remove_finished(CURLM* multi_handle, CURLcode* result)
3926 int msgs_in_queue = 0;
3928 while (1)
3930 CURLMsg* msg = curl_multi_info_read(multi_handle, &msgs_in_queue);
3931 if (NULL == msg) return NULL;
3932 if (CURLMSG_DONE == msg->msg)
3934 CURL* easy_handle = msg->easy_handle;
3935 if (result) *result = msg->data.result;
3936 if (CURLM_OK != curl_multi_remove_handle(multi_handle, easy_handle))
3938 /*caml_failwith("curlm_remove_finished");*/
3940 return easy_handle;
3945 CAMLprim value caml_curlm_remove_finished(value v_multi)
3947 CAMLparam1(v_multi);
3948 CAMLlocal2(v_easy, v_tuple);
3949 CURL* handle;
3950 CURLM* multi_handle;
3951 CURLcode result;
3952 Connection* conn = NULL;
3954 multi_handle = CURLM_val(v_multi);
3956 caml_enter_blocking_section();
3957 handle = curlm_remove_finished(multi_handle,&result);
3958 caml_leave_blocking_section();
3960 if (NULL == handle)
3962 CAMLreturn(Val_none);
3964 else
3966 conn = findConnection(handle);
3967 if (conn->curl_ERRORBUFFER != NULL)
3969 Store_field(Field(conn->ocamlValues, Ocaml_ERRORBUFFER), 0, caml_copy_string(conn->curl_ERRORBUFFER));
3971 conn->refcount--;
3972 /* NB: same handle, but different block */
3973 v_easy = caml_curl_alloc(conn);
3974 v_tuple = caml_alloc(2, 0);
3975 Store_field(v_tuple,0,v_easy);
3976 Store_field(v_tuple,1,Val_int(result)); /* CURLcode */
3977 CAMLreturn(Val_some(v_tuple));
3981 static int curlm_wait_data(CURLM* multi_handle)
3983 struct timeval timeout;
3984 CURLMcode ret;
3986 fd_set fdread;
3987 fd_set fdwrite;
3988 fd_set fdexcep;
3989 int maxfd = -1;
3991 FD_ZERO(&fdread);
3992 FD_ZERO(&fdwrite);
3993 FD_ZERO(&fdexcep);
3995 /* set a suitable timeout */
3996 timeout.tv_sec = 1;
3997 timeout.tv_usec = 0;
3999 /* get file descriptors from the transfers */
4000 ret = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
4002 if (ret == CURLM_OK && maxfd >= 0)
4004 int rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
4005 if (-1 != rc) return 0;
4007 return 1;
4010 CAMLprim value caml_curlm_wait_data(value v_multi)
4012 CAMLparam1(v_multi);
4013 int ret;
4014 CURLM* h = CURLM_val(v_multi);
4016 caml_enter_blocking_section();
4017 ret = curlm_wait_data(h);
4018 caml_leave_blocking_section();
4020 CAMLreturn(Val_bool(0 == ret));
4023 CAMLprim value caml_curl_multi_add_handle(value v_multi, value v_easy)
4025 CAMLparam2(v_multi,v_easy);
4026 CURLM* multi = CURLM_val(v_multi);
4027 Connection* conn = Connection_val(v_easy);
4029 /* prevent collection of OCaml value while the easy handle is used
4030 and may invoke callbacks registered on OCaml side */
4031 conn->refcount++;
4033 /* may invoke callbacks so need to be consistent with locks */
4034 caml_enter_blocking_section();
4035 if (CURLM_OK != curl_multi_add_handle(multi, conn->connection))
4037 conn->refcount--; /* not added, revert */
4038 caml_leave_blocking_section();
4039 caml_failwith("caml_curl_multi_add_handle");
4041 caml_leave_blocking_section();
4043 CAMLreturn(Val_unit);
4046 CAMLprim value caml_curl_multi_remove_handle(value v_multi, value v_easy)
4048 CAMLparam2(v_multi,v_easy);
4049 CURLM* multi = CURLM_val(v_multi);
4050 Connection* conn = Connection_val(v_easy);
4052 /* may invoke callbacks so need to be consistent with locks */
4053 caml_enter_blocking_section();
4054 if (CURLM_OK != curl_multi_remove_handle(multi, conn->connection))
4056 caml_leave_blocking_section();
4057 caml_failwith("caml_curl_multi_remove_handle");
4059 conn->refcount--;
4060 caml_leave_blocking_section();
4062 CAMLreturn(Val_unit);
4065 CAMLprim value caml_curl_multi_perform_all(value v_multi)
4067 CAMLparam1(v_multi);
4068 int still_running = 0;
4069 CURLM* h = CURLM_val(v_multi);
4071 caml_enter_blocking_section();
4072 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(h, &still_running));
4073 caml_leave_blocking_section();
4075 CAMLreturn(Val_int(still_running));
4078 CAMLprim value helper_curl_easy_strerror(value v_code)
4080 CAMLparam1(v_code);
4081 CAMLreturn(caml_copy_string(curl_easy_strerror(Int_val(v_code))));
4085 * Wrappers for the curl_multi_socket_action infrastructure
4086 * Based on curl hiperfifo.c example
4089 #ifdef _WIN32
4090 #ifndef Val_socket
4091 #define Val_socket(v) win_alloc_socket(v)
4092 #endif
4093 #ifndef Socket_val
4094 #error Socket_val not defined in unixsupport.h
4095 #endif
4096 #else /* _WIN32 */
4097 #ifndef Socket_val
4098 #define Socket_val(v) Long_val(v)
4099 #endif
4100 #ifndef Val_socket
4101 #define Val_socket(v) Val_int(v)
4102 #endif
4103 #endif /* _WIN32 */
4105 static void raise_multi_error(char const* msg)
4107 static value* exception = NULL;
4109 if (NULL == exception)
4111 exception = caml_named_value("Curl.Multi.Error");
4112 if (NULL == exception) caml_invalid_argument("Curl.Multi.Error");
4115 caml_raise_with_string(*exception, msg);
4118 static void check_mcode(CURLMcode code)
4120 char const *s = NULL;
4121 switch (code)
4123 case CURLM_OK : return;
4124 case CURLM_CALL_MULTI_PERFORM : s="CURLM_CALL_MULTI_PERFORM"; break;
4125 case CURLM_BAD_HANDLE : s="CURLM_BAD_HANDLE"; break;
4126 case CURLM_BAD_EASY_HANDLE : s="CURLM_BAD_EASY_HANDLE"; break;
4127 case CURLM_OUT_OF_MEMORY : s="CURLM_OUT_OF_MEMORY"; break;
4128 case CURLM_INTERNAL_ERROR : s="CURLM_INTERNAL_ERROR"; break;
4129 case CURLM_UNKNOWN_OPTION : s="CURLM_UNKNOWN_OPTION"; break;
4130 case CURLM_LAST : s="CURLM_LAST"; break;
4131 case CURLM_BAD_SOCKET : s="CURLM_BAD_SOCKET"; break;
4132 default : s="CURLM_unknown"; break;
4134 raise_multi_error(s);
4137 CAMLprim value caml_curl_multi_socket_action(value v_multi, value v_fd, value v_kind)
4139 CAMLparam3(v_multi, v_fd, v_kind);
4140 CURLM* h = CURLM_val(v_multi);
4141 int still_running = 0;
4142 CURLMcode rc = CURLM_OK;
4143 curl_socket_t socket;
4144 int kind = 0;
4146 if (Val_none == v_fd)
4148 socket = CURL_SOCKET_TIMEOUT;
4150 else
4152 socket = Socket_val(Field(v_fd, 0));
4155 switch (Int_val(v_kind))
4157 case 0 : break;
4158 case 1 : kind |= CURL_CSELECT_IN; break;
4159 case 2 : kind |= CURL_CSELECT_OUT; break;
4160 case 3 : kind |= CURL_CSELECT_IN | CURL_CSELECT_OUT; break;
4161 default:
4162 raise_multi_error("caml_curl_multi_socket_action");
4165 /* fprintf(stdout,"fd %u kind %u\n",socket, kind); fflush(stdout); */
4167 caml_enter_blocking_section();
4168 do {
4169 rc = curl_multi_socket_action(h, socket, kind, &still_running);
4170 } while (rc == CURLM_CALL_MULTI_PERFORM);
4171 caml_leave_blocking_section();
4173 check_mcode(rc);
4175 CAMLreturn(Val_int(still_running));
4178 CAMLprim value caml_curl_multi_socket_all(value v_multi)
4180 CAMLparam1(v_multi);
4181 int still_running = 0;
4182 CURLMcode rc = CURLM_OK;
4183 CURLM* h = CURLM_val(v_multi);
4185 caml_enter_blocking_section();
4186 do {
4187 rc = curl_multi_socket_all(h, &still_running);
4188 } while (rc == CURLM_CALL_MULTI_PERFORM);
4189 caml_leave_blocking_section();
4191 check_mcode(rc);
4193 CAMLreturn(Val_int(still_running));
4196 static int curlm_sock_cb(CURL *e, curl_socket_t sock, int what, void *cbp, void *sockp)
4198 caml_leave_blocking_section();
4200 CAMLparam0();
4201 CAMLlocal2(v_what,csock);
4202 (void)e;
4203 (void)sockp; /* not used */
4205 /* v_what = Val_int(what); */
4206 switch (what)
4208 case CURL_POLL_NONE : v_what = Val_int(0); break;
4209 case CURL_POLL_IN : v_what = Val_int(1); break;
4210 case CURL_POLL_OUT : v_what = Val_int(2); break;
4211 case CURL_POLL_INOUT : v_what = Val_int(3); break;
4212 case CURL_POLL_REMOVE : v_what = Val_int(4); break;
4213 default:
4214 fprintf(stderr, "curlm_sock_cb sock=%d what=%d\n", sock, what);
4215 fflush(stderr);
4216 raise_multi_error("curlm_sock_cb"); /* FIXME exception from callback */
4218 csock=Val_socket(sock);
4219 caml_callback2(Field(((ml_multi_handle*)cbp)->values,curlmopt_socket_function),
4220 csock, v_what);
4221 CAMLdrop;
4223 caml_enter_blocking_section();
4224 return 0;
4227 CAMLprim value caml_curl_multi_socketfunction(value v_multi, value v_cb)
4229 CAMLparam2(v_multi, v_cb);
4230 ml_multi_handle* multi = Multi_val(v_multi);
4232 Store_field(multi->values, curlmopt_socket_function, v_cb);
4234 curl_multi_setopt(multi->handle, CURLMOPT_SOCKETFUNCTION, curlm_sock_cb);
4235 curl_multi_setopt(multi->handle, CURLMOPT_SOCKETDATA, multi);
4237 CAMLreturn(Val_unit);
4240 static int curlm_timer_cb(CURLM *multi, long timeout_ms, void *userp)
4242 caml_leave_blocking_section();
4244 CAMLparam0();
4245 (void)multi;
4246 caml_callback(Field(((ml_multi_handle*)userp)->values,curlmopt_timer_function), Val_long(timeout_ms));
4247 CAMLdrop;
4249 caml_enter_blocking_section();
4250 return 0;
4253 CAMLprim value caml_curl_multi_timerfunction(value v_multi, value v_cb)
4255 CAMLparam2(v_multi, v_cb);
4256 ml_multi_handle* multi = Multi_val(v_multi);
4258 Store_field(multi->values, curlmopt_timer_function, v_cb);
4260 curl_multi_setopt(multi->handle, CURLMOPT_TIMERFUNCTION, curlm_timer_cb);
4261 curl_multi_setopt(multi->handle, CURLMOPT_TIMERDATA, multi);
4263 CAMLreturn(Val_unit);
4266 CAMLprim value caml_curl_multi_timeout(value v_multi)
4268 CAMLparam1(v_multi);
4269 long ms = 0;
4270 CURLMcode rc = CURLM_OK;
4271 ml_multi_handle* multi = Multi_val(v_multi);
4273 rc = curl_multi_timeout(multi->handle, &ms);
4275 check_mcode(rc);
4277 CAMLreturn(Val_long(ms));
4280 #define SETMOPT_VAL_(func_name, curl_option, conv_val) \
4281 static void func_name(CURLM *handle, value option) \
4283 CAMLparam1(option); \
4284 CURLcode result = CURLM_OK; \
4286 result = curl_multi_setopt(handle, curl_option, conv_val(option)); \
4288 check_mcode(result); \
4290 CAMLreturn0; \
4293 #define SETMOPT_VAL(name, conv) SETMOPT_VAL_(handle_multi_##name, CURLMOPT_##name, conv)
4294 #define SETMOPT_BOOL(name) SETMOPT_VAL(name, Bool_val)
4295 #define SETMOPT_LONG(name) SETMOPT_VAL(name, Long_val)
4296 #define SETMOPT_INT64(name) SETMOPT_VAL(name, Int64_val)
4298 long pipeliningMap[] =
4300 0, /* CURLPIPE_NOTHING */
4301 1, /* CURLPIPE_HTTP1 */
4302 2, /* CURLPIPE_MULTIPLEX */
4305 static void handle_multi_PIPELINING(CURLM* handle, value option)
4307 CAMLparam1(option);
4308 CURLcode result = CURLM_OK;
4310 long bits = convert_bit_list(pipeliningMap, sizeof(pipeliningMap) / sizeof(pipeliningMap[0]), option);
4312 result = curl_multi_setopt(handle, CURLMOPT_PIPELINING, bits);
4314 check_mcode(result);
4316 CAMLreturn0;
4319 #if HAVE_DECL_CURLMOPT_MAXCONNECTS
4320 SETMOPT_LONG( MAXCONNECTS)
4321 #endif
4323 #if HAVE_DECL_CURLMOPT_MAX_PIPELINE_LENGTH
4324 SETMOPT_LONG( MAX_PIPELINE_LENGTH)
4325 #endif
4327 #if HAVE_DECL_CURLMOPT_MAX_HOST_CONNECTIONS
4328 SETMOPT_LONG( MAX_HOST_CONNECTIONS)
4329 #endif
4331 typedef struct CURLMOptionMapping CURLMOptionMapping;
4332 #define OPT(name) { handle_multi_## name, "CURLMOPT_"#name}
4333 #define NO_OPT(name) { NULL, "CURLMOPT_"#name}
4335 struct CURLMOptionMapping
4337 void (*optionHandler)(CURLM *, value);
4338 char *name;
4341 CURLMOptionMapping implementedMOptionMap[] = {
4342 OPT( PIPELINING),
4343 #if HAVE_DECL_CURLMOPT_MAXCONNECTS
4344 OPT( MAXCONNECTS),
4345 #else
4346 NO_OPT( MAXCONNECTS),
4347 #endif
4348 #if HAVE_DECL_CURLMOPT_MAX_PIPELINE_LENGTH
4349 OPT( MAX_PIPELINE_LENGTH),
4350 #else
4351 NO_OPT( MAX_PIPELINE_LENGTH),
4352 #endif
4353 #if HAVE_DECL_CURLMOPT_MAX_HOST_CONNECTIONS
4354 OPT( MAX_HOST_CONNECTIONS),
4355 #else
4356 NO_OPT( MAX_HOST_CONNECTIONS),
4357 #endif
4360 CAMLprim value caml_curl_multi_setopt(value v_multi, value option)
4362 CAMLparam2(v_multi, option);
4363 CAMLlocal1(data);
4364 CURLM *handle = Multi_val(v_multi)->handle;
4365 CURLMOptionMapping* thisOption = NULL;
4366 static value* exception = NULL;
4368 data = Field(option, 0);
4370 if (Tag_val(option) < sizeof(implementedMOptionMap)/sizeof(CURLMOptionMapping))
4372 thisOption = &implementedMOptionMap[Tag_val(option)];
4373 if (thisOption->optionHandler)
4375 thisOption->optionHandler(handle, data);
4377 else
4379 if (NULL == exception)
4381 exception = caml_named_value("Curl.NotImplemented");
4382 if (NULL == exception) caml_invalid_argument("Curl.NotImplemented");
4385 caml_raise_with_string(*exception, thisOption->name);
4388 else
4390 caml_failwith("Invalid CURLMOPT Option");
4393 CAMLreturn(Val_unit);