release 0.7.6
[ocurl.git] / curl-helper.c
blobe515b4df573ba60a5504e2891d35e2e88ea75b56
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 #include <caml/alloc.h>
18 #include <caml/memory.h>
19 #include <caml/mlvalues.h>
20 #include <caml/callback.h>
21 #include <caml/fail.h>
22 #include <caml/unixsupport.h>
23 #include <caml/custom.h>
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #else
28 #pragma message("No config file given.")
29 #endif
31 void leave_blocking_section(void);
32 void enter_blocking_section(void);
34 #define Val_none Val_int(0)
36 static __inline value
37 Val_some( value v )
39 CAMLparam1( v );
40 CAMLlocal1( some );
41 some = caml_alloc(1, 0);
42 Store_field( some, 0, v );
43 CAMLreturn( some );
46 static value Val_pair(value v1, value v2)
48 CAMLparam2(v1,v2);
49 CAMLlocal1(pair);
50 pair = caml_alloc_small(2,0);
51 Field(pair,0) = v1;
52 Field(pair,1) = v2;
53 CAMLreturn(pair);
56 static value Val_cons(value list, value v) { return Val_pair(v,list); }
58 typedef struct Connection Connection;
59 typedef struct ConnectionList ConnectionList;
61 #define Connection_val(v) (*(Connection**)Data_custom_val(v))
63 typedef enum OcamlValues
65 Ocaml_WRITEFUNCTION,
66 Ocaml_READFUNCTION,
67 Ocaml_ERRORBUFFER,
68 Ocaml_POSTFIELDS,
69 Ocaml_HTTPHEADER,
70 Ocaml_HTTPPOST,
71 Ocaml_QUOTE,
72 Ocaml_POSTQUOTE,
73 Ocaml_HEADERFUNCTION,
74 Ocaml_PROGRESSFUNCTION,
75 Ocaml_DEBUGFUNCTION,
76 Ocaml_HTTP200ALIASES,
77 Ocaml_IOCTLFUNCTION,
78 Ocaml_SEEKFUNCTION,
79 Ocaml_OPENSOCKETFUNCTION,
81 Ocaml_URL,
82 Ocaml_PROXY,
83 Ocaml_USERPWD,
84 Ocaml_PROXYUSERPWD,
85 Ocaml_RANGE,
86 Ocaml_REFERER,
87 Ocaml_USERAGENT,
88 Ocaml_FTPPORT,
89 Ocaml_COOKIE,
90 Ocaml_HTTPPOSTSTRINGS,
91 Ocaml_SSLCERT,
92 Ocaml_SSLCERTTYPE,
93 Ocaml_SSLCERTPASSWD,
94 Ocaml_SSLKEY,
95 Ocaml_SSLKEYTYPE,
96 Ocaml_SSLKEYPASSWD,
97 Ocaml_SSLENGINE,
98 Ocaml_COOKIEFILE,
99 Ocaml_CUSTOMREQUEST,
100 Ocaml_INTERFACE,
101 Ocaml_CAINFO,
102 Ocaml_CAPATH,
103 Ocaml_RANDOM_FILE,
104 Ocaml_EGDSOCKET,
105 Ocaml_COOKIEJAR,
106 Ocaml_SSL_CIPHER_LIST,
107 Ocaml_PRIVATE,
108 Ocaml_NETRC_FILE,
109 Ocaml_FTP_ACCOUNT,
110 Ocaml_COOKIELIST,
111 Ocaml_FTP_ALTERNATIVE_TO_USER,
112 Ocaml_SSH_PUBLIC_KEYFILE,
113 Ocaml_SSH_PRIVATE_KEYFILE,
114 Ocaml_SSH_HOST_PUBLIC_KEY_MD5,
115 Ocaml_COPYPOSTFIELDS,
117 Ocaml_DNS_SERVERS,
119 Ocaml_MAIL_FROM,
120 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 #if HAVE_DECL_CURLE_UNSUPPORTED_PROTOCOL
207 {"CURLE_UNSUPPORTED_PROTOCOL", CURLE_UNSUPPORTED_PROTOCOL},
208 #else
209 {"CURLE_UNSUPPORTED_PROTOCOL", -1},
210 #endif
211 #if HAVE_DECL_CURLE_FAILED_INIT
212 {"CURLE_FAILED_INIT", CURLE_FAILED_INIT},
213 #else
214 {"CURLE_FAILED_INIT", -1},
215 #endif
216 #if HAVE_DECL_CURLE_URL_MALFORMAT
217 {"CURLE_URL_MALFORMAT", CURLE_URL_MALFORMAT},
218 #else
219 {"CURLE_URL_MALFORMAT", -1},
220 #endif
221 #if HAVE_DECL_CURLE_URL_MALFORMAT_USER
222 {"CURLE_URL_MALFORMAT_USER", CURLE_URL_MALFORMAT_USER},
223 #else
224 {"CURLE_URL_MALFORMAT_USER", -1},
225 #endif
226 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_PROXY
227 {"CURLE_COULDNT_RESOLVE_PROXY", CURLE_COULDNT_RESOLVE_PROXY},
228 #else
229 {"CURLE_COULDNT_RESOLVE_PROXY", -1},
230 #endif
231 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_HOST
232 {"CURLE_COULDNT_RESOLVE_HOST", CURLE_COULDNT_RESOLVE_HOST},
233 #else
234 {"CURLE_COULDNT_RESOLVE_HOST", -1},
235 #endif
236 #if HAVE_DECL_CURLE_COULDNT_CONNECT
237 {"CURLE_COULDNT_CONNECT", CURLE_COULDNT_CONNECT},
238 #else
239 {"CURLE_COULDNT_CONNECT", -1},
240 #endif
241 #if HAVE_DECL_CURLE_FTP_WEIRD_SERVER_REPLY
242 {"CURLE_FTP_WEIRD_SERVER_REPLY", CURLE_FTP_WEIRD_SERVER_REPLY},
243 #else
244 {"CURLE_FTP_WEIRD_SERVER_REPLY", -1},
245 #endif
246 #if HAVE_DECL_CURLE_FTP_ACCESS_DENIED
247 {"CURLE_FTP_ACCESS_DENIED", CURLE_FTP_ACCESS_DENIED},
248 #else
249 {"CURLE_FTP_ACCESS_DENIED", -1},
250 #endif
251 #if HAVE_DECL_CURLE_FTP_USER_PASSWORD_INCORRECT
252 {"CURLE_FTP_USER_PASSWORD_INCORRECT", CURLE_FTP_USER_PASSWORD_INCORRECT},
253 #else
254 {"CURLE_FTP_USER_PASSWORD_INCORRECT", -1},
255 #endif
256 #if HAVE_DECL_CURLE_FTP_WEIRD_PASS_REPLY
257 {"CURLE_FTP_WEIRD_PASS_REPLY", CURLE_FTP_WEIRD_PASS_REPLY},
258 #else
259 {"CURLE_FTP_WEIRD_PASS_REPLY", -1},
260 #endif
261 #if HAVE_DECL_CURLE_FTP_WEIRD_USER_REPLY
262 {"CURLE_FTP_WEIRD_USER_REPLY", CURLE_FTP_WEIRD_USER_REPLY},
263 #else
264 {"CURLE_FTP_WEIRD_USER_REPLY", -1},
265 #endif
266 #if HAVE_DECL_CURLE_FTP_WEIRD_PASV_REPLY
267 {"CURLE_FTP_WEIRD_PASV_REPLY", CURLE_FTP_WEIRD_PASV_REPLY},
268 #else
269 {"CURLE_FTP_WEIRD_PASV_REPLY", -1},
270 #endif
271 #if HAVE_DECL_CURLE_FTP_WEIRD_227_FORMAT
272 {"CURLE_FTP_WEIRD_227_FORMAT", CURLE_FTP_WEIRD_227_FORMAT},
273 #else
274 {"CURLE_FTP_WEIRD_227_FORMAT", -1},
275 #endif
276 #if HAVE_DECL_CURLE_FTP_CANT_GET_HOST
277 {"CURLE_FTP_CANT_GET_HOST", CURLE_FTP_CANT_GET_HOST},
278 #else
279 {"CURLE_FTP_CANT_GET_HOST", -1},
280 #endif
281 #if HAVE_DECL_CURLE_FTP_CANT_RECONNECT
282 {"CURLE_FTP_CANT_RECONNECT", CURLE_FTP_CANT_RECONNECT},
283 #else
284 {"CURLE_FTP_CANT_RECONNECT", -1},
285 #endif
286 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_BINARY
287 {"CURLE_FTP_COULDNT_SET_BINARY", CURLE_FTP_COULDNT_SET_BINARY},
288 #else
289 {"CURLE_FTP_COULDNT_SET_BINARY", -1},
290 #endif
291 #if HAVE_DECL_CURLE_PARTIAL_FILE
292 {"CURLE_PARTIAL_FILE", CURLE_PARTIAL_FILE},
293 #else
294 {"CURLE_PARTIAL_FILE", -1},
295 #endif
296 #if HAVE_DECL_CURLE_FTP_COULDNT_RETR_FILE
297 {"CURLE_FTP_COULDNT_RETR_FILE", CURLE_FTP_COULDNT_RETR_FILE},
298 #else
299 {"CURLE_FTP_COULDNT_RETR_FILE", -1},
300 #endif
301 #if HAVE_DECL_CURLE_FTP_WRITE_ERROR
302 {"CURLE_FTP_WRITE_ERROR", CURLE_FTP_WRITE_ERROR},
303 #else
304 {"CURLE_FTP_WRITE_ERROR", -1},
305 #endif
306 #if HAVE_DECL_CURLE_FTP_QUOTE_ERROR
307 {"CURLE_FTP_QUOTE_ERROR", CURLE_FTP_QUOTE_ERROR},
308 #else
309 {"CURLE_FTP_QUOTE_ERROR", -1},
310 #endif
311 #if HAVE_DECL_CURLE_HTTP_NOT_FOUND
312 {"CURLE_HTTP_NOT_FOUND", CURLE_HTTP_NOT_FOUND},
313 #else
314 {"CURLE_HTTP_NOT_FOUND", -1},
315 #endif
316 #if HAVE_DECL_CURLE_WRITE_ERROR
317 {"CURLE_WRITE_ERROR", CURLE_WRITE_ERROR},
318 #else
319 {"CURLE_WRITE_ERROR", -1},
320 #endif
321 #if HAVE_DECL_CURLE_MALFORMAT_USER
322 {"CURLE_MALFORMAT_USER", CURLE_MALFORMAT_USER},
323 #else
324 {"CURLE_MALFORMAT_USER", -1},
325 #endif
326 #if HAVE_DECL_CURLE_FTP_COULDNT_STOR_FILE
327 {"CURLE_FTP_COULDNT_STOR_FILE", CURLE_FTP_COULDNT_STOR_FILE},
328 #else
329 {"CURLE_FTP_COULDNT_STOR_FILE", -1},
330 #endif
331 #if HAVE_DECL_CURLE_READ_ERROR
332 {"CURLE_READ_ERROR", CURLE_READ_ERROR},
333 #else
334 {"CURLE_READ_ERROR", -1},
335 #endif
336 #if HAVE_DECL_CURLE_OUT_OF_MEMORY
337 {"CURLE_OUT_OF_MEMORY", CURLE_OUT_OF_MEMORY},
338 #else
339 {"CURLE_OUT_OF_MEMORY", -1},
340 #endif
341 #if HAVE_DECL_CURLE_OPERATION_TIMEOUTED
342 {"CURLE_OPERATION_TIMEOUTED", CURLE_OPERATION_TIMEOUTED},
343 #else
344 {"CURLE_OPERATION_TIMEOUTED", -1},
345 #endif
346 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_ASCII
347 {"CURLE_FTP_COULDNT_SET_ASCII", CURLE_FTP_COULDNT_SET_ASCII},
348 #else
349 {"CURLE_FTP_COULDNT_SET_ASCII", -1},
350 #endif
351 #if HAVE_DECL_CURLE_FTP_PORT_FAILED
352 {"CURLE_FTP_PORT_FAILED", CURLE_FTP_PORT_FAILED},
353 #else
354 {"CURLE_FTP_PORT_FAILED", -1},
355 #endif
356 #if HAVE_DECL_CURLE_FTP_COULDNT_USE_REST
357 {"CURLE_FTP_COULDNT_USE_REST", CURLE_FTP_COULDNT_USE_REST},
358 #else
359 {"CURLE_FTP_COULDNT_USE_REST", -1},
360 #endif
361 #if HAVE_DECL_CURLE_FTP_COULDNT_GET_SIZE
362 {"CURLE_FTP_COULDNT_GET_SIZE", CURLE_FTP_COULDNT_GET_SIZE},
363 #else
364 {"CURLE_FTP_COULDNT_GET_SIZE", -1},
365 #endif
366 #if HAVE_DECL_CURLE_HTTP_RANGE_ERROR
367 {"CURLE_HTTP_RANGE_ERROR", CURLE_HTTP_RANGE_ERROR},
368 #else
369 {"CURLE_HTTP_RANGE_ERROR", -1},
370 #endif
371 #if HAVE_DECL_CURLE_HTTP_POST_ERROR
372 {"CURLE_HTTP_POST_ERROR", CURLE_HTTP_POST_ERROR},
373 #else
374 {"CURLE_HTTP_POST_ERROR", -1},
375 #endif
376 #if HAVE_DECL_CURLE_SSL_CONNECT_ERROR
377 {"CURLE_SSL_CONNECT_ERROR", CURLE_SSL_CONNECT_ERROR},
378 #else
379 {"CURLE_SSL_CONNECT_ERROR", -1},
380 #endif
381 #if HAVE_DECL_CURLE_FTP_BAD_DOWNLOAD_RESUME
382 {"CURLE_FTP_BAD_DOWNLOAD_RESUME", CURLE_FTP_BAD_DOWNLOAD_RESUME},
383 #else
384 {"CURLE_FTP_BAD_DOWNLOAD_RESUME", -1},
385 #endif
386 #if HAVE_DECL_CURLE_FILE_COULDNT_READ_FILE
387 {"CURLE_FILE_COULDNT_READ_FILE", CURLE_FILE_COULDNT_READ_FILE},
388 #else
389 {"CURLE_FILE_COULDNT_READ_FILE", -1},
390 #endif
391 #if HAVE_DECL_CURLE_LDAP_CANNOT_BIND
392 {"CURLE_LDAP_CANNOT_BIND", CURLE_LDAP_CANNOT_BIND},
393 #else
394 {"CURLE_LDAP_CANNOT_BIND", -1},
395 #endif
396 #if HAVE_DECL_CURLE_LDAP_SEARCH_FAILED
397 {"CURLE_LDAP_SEARCH_FAILED", CURLE_LDAP_SEARCH_FAILED},
398 #else
399 {"CURLE_LDAP_SEARCH_FAILED", -1},
400 #endif
401 #if HAVE_DECL_CURLE_LIBRARY_NOT_FOUND
402 {"CURLE_LIBRARY_NOT_FOUND", CURLE_LIBRARY_NOT_FOUND},
403 #else
404 {"CURLE_LIBRARY_NOT_FOUND", -1},
405 #endif
406 #if HAVE_DECL_CURLE_FUNCTION_NOT_FOUND
407 {"CURLE_FUNCTION_NOT_FOUND", CURLE_FUNCTION_NOT_FOUND},
408 #else
409 {"CURLE_FUNCTION_NOT_FOUND", -1},
410 #endif
411 #if HAVE_DECL_CURLE_ABORTED_BY_CALLBACK
412 {"CURLE_ABORTED_BY_CALLBACK", CURLE_ABORTED_BY_CALLBACK},
413 #else
414 {"CURLE_ABORTED_BY_CALLBACK", -1},
415 #endif
416 #if HAVE_DECL_CURLE_BAD_FUNCTION_ARGUMENT
417 {"CURLE_BAD_FUNCTION_ARGUMENT", CURLE_BAD_FUNCTION_ARGUMENT},
418 #else
419 {"CURLE_BAD_FUNCTION_ARGUMENT", -1},
420 #endif
421 #if HAVE_DECL_CURLE_BAD_CALLING_ORDER
422 {"CURLE_BAD_CALLING_ORDER", CURLE_BAD_CALLING_ORDER},
423 #else
424 {"CURLE_BAD_CALLING_ORDER", -1},
425 #endif
426 #if HAVE_DECL_CURLE_HTTP_PORT_FAILED
427 {"CURLE_HTTP_PORT_FAILED", CURLE_HTTP_PORT_FAILED},
428 #else
429 {"CURLE_HTTP_PORT_FAILED", -1},
430 #endif
431 #if HAVE_DECL_CURLE_BAD_PASSWORD_ENTERED
432 {"CURLE_BAD_PASSWORD_ENTERED", CURLE_BAD_PASSWORD_ENTERED},
433 #else
434 {"CURLE_BAD_PASSWORD_ENTERED", -1},
435 #endif
436 #if HAVE_DECL_CURLE_TOO_MANY_REDIRECTS
437 {"CURLE_TOO_MANY_REDIRECTS", CURLE_TOO_MANY_REDIRECTS},
438 #else
439 {"CURLE_TOO_MANY_REDIRECTS", -1},
440 #endif
441 #if HAVE_DECL_CURLE_UNKNOWN_TELNET_OPTION
442 {"CURLE_UNKNOWN_TELNET_OPTION", CURLE_UNKNOWN_TELNET_OPTION},
443 #else
444 {"CURLE_UNKNOWN_TELNET_OPTION", -1},
445 #endif
446 #if HAVE_DECL_CURLE_TELNET_OPTION_SYNTAX
447 {"CURLE_TELNET_OPTION_SYNTAX", CURLE_TELNET_OPTION_SYNTAX},
448 #else
449 {"CURLE_TELNET_OPTION_SYNTAX", -1},
450 #endif
451 #if HAVE_DECL_CURLE_SSL_PEER_CERTIFICATE
452 {"CURLE_SSL_PEER_CERTIFICATE", CURLE_SSL_PEER_CERTIFICATE},
453 #else
454 {"CURLE_SSL_PEER_CERTIFICATE", -1},
455 #endif
456 #if HAVE_DECL_CURLE_GOT_NOTHING
457 {"CURLE_GOT_NOTHING", CURLE_GOT_NOTHING},
458 #else
459 {"CURLE_GOT_NOTHING", -1},
460 #endif
461 #if HAVE_DECL_CURLE_SSL_ENGINE_NOT_FOUND
462 {"CURLE_SSL_ENGINE_NOT_FOUND", CURLE_SSL_ENGINE_NOTFOUND},
463 #else
464 {"CURLE_SSL_ENGINE_NOT_FOUND", -1},
465 #endif
466 #if HAVE_DECL_CURLE_SSL_ENGINE_SET_FAILED
467 {"CURLE_SSL_ENGINE_SET_FAILED", CURLE_SSL_ENGINE_SETFAILED},
468 #else
469 {"CURLE_SSL_ENGINE_SET_FAILED", -1},
470 #endif
471 #if HAVE_DECL_CURLE_SEND_ERROR
472 {"CURLE_SEND_ERROR", CURLE_SEND_ERROR},
473 #else
474 {"CURLE_SEND_ERROR", -1},
475 #endif
476 #if HAVE_DECL_CURLE_RECV_ERROR
477 {"CURLE_RECV_ERROR", CURLE_RECV_ERROR},
478 #else
479 {"CURLE_RECV_ERROR", -1},
480 #endif
481 #if HAVE_DECL_CURLE_SHARE_IN_USE
482 {"CURLE_SHARE_IN_USE", CURLE_SHARE_IN_USE},
483 #else
484 {"CURLE_SHARE_IN_USE", -1},
485 #endif
486 #if HAVE_DECL_CURLE_SSL_CERTPROBLEM
487 {"CURLE_SSL_CERTPROBLEN", CURLE_SSL_CERTPROBLEM},
488 #else
489 {"CURLE_SSL_CERTPROBLEN", -1},
490 #endif
491 #if HAVE_DECL_CURLE_SSL_CIPHER
492 {"CURLE_SSL_CIPHER", CURLE_SSL_CIPHER},
493 #else
494 {"CURLE_SSL_CIPHER", -1},
495 #endif
496 #if HAVE_DECL_CURLE_SSL_CACERT
497 {"CURLE_SSL_CACERT", CURLE_SSL_CACERT},
498 #else
499 {"CURLE_SSL_CACERT", -1},
500 #endif
501 #if HAVE_DECL_CURLE_BAD_CONTENT_ENCODING
502 {"CURLE_BAD_CONTENT_ENCODING", CURLE_BAD_CONTENT_ENCODING},
503 #else
504 {"CURLE_BAD_CONTENT_ENCODING", -1},
505 #endif
506 #if HAVE_DECL_CURLE_LDAP_INVALID_URL
507 {"CURLE_LDAP_INVALID_URL", CURLE_LDAP_INVALID_URL},
508 #else
509 {"CURLE_LDAP_INVALID_URL", -1},
510 #endif
511 #if HAVE_DECL_CURLE_FILESIZE_EXCEEDED
512 {"CURLE_FILESIZE_EXCEEDED", CURLE_FILESIZE_EXCEEDED},
513 #else
514 {"CURLE_FILESIZE_EXCEEDED", -1},
515 #endif
516 #if HAVE_DECL_CURLE_FTP_SSL_FAILED
517 {"CURLE_FTP_SSL_FAILED", CURLE_FTP_SSL_FAILED},
518 #else
519 {"CURLE_FTP_SSL_FAILED", -1},
520 #endif
521 #if HAVE_DECL_CURLE_SEND_FAIL_REWIND
522 {"CURLE_SEND_FAIL_REWIND", CURLE_SEND_FAIL_REWIND},
523 #else
524 {"CURLE_SEND_FAIL_REWIND", -1},
525 #endif
526 #if HAVE_DECL_CURLE_SSL_ENGINE_INITFAILED
527 {"CURLE_SSL_ENGINE_INITFAILED", CURLE_SSL_ENGINE_INITFAILED},
528 #else
529 {"CURLE_SSL_ENGINE_INITFAILED", -1},
530 #endif
531 #if HAVE_DECL_CURLE_LOGIN_DENIED
532 {"CURLE_LOGIN_DENIED", CURLE_LOGIN_DENIED},
533 #else
534 {"CURLE_LOGIN_DENIED", -1},
535 #endif
536 #if HAVE_DECL_CURLE_TFTP_NOTFOUND
537 {"CURLE_TFTP_NOTFOUND", CURLE_TFTP_NOTFOUND},
538 #else
539 {"CURLE_TFTP_NOTFOUND", -1},
540 #endif
541 #if HAVE_DECL_CURLE_TFTP_PERM
542 {"CURLE_TFTP_PERM", CURLE_TFTP_PERM},
543 #else
544 {"CURLE_TFTP_PERM", -1},
545 #endif
546 #if HAVE_DECL_CURLE_REMOTE_DISK_FULL
547 {"CURLE_REMOTE_DISK_FULL", CURLE_REMOTE_DISK_FULL},
548 #else
549 {"CURLE_REMOTE_DISK_FULL", -1},
550 #endif
551 #if HAVE_DECL_CURLE_TFTP_ILLEGAL
552 {"CURLE_TFTP_ILLEGAL", CURLE_TFTP_ILLEGAL},
553 #else
554 {"CURLE_TFTP_ILLEGAL", -1},
555 #endif
556 #if HAVE_DECL_CURLE_TFTP_UNKNOWNID
557 {"CURLE_TFTP_UNKNOWNID", CURLE_TFTP_UNKNOWNID},
558 #else
559 {"CURLE_TFTP_UNKNOWNID", -1},
560 #endif
561 #if HAVE_DECL_CURLE_REMOTE_FILE_EXISTS
562 {"CURLE_REMOTE_FILE_EXISTS", CURLE_REMOTE_FILE_EXISTS},
563 #else
564 {"CURLE_REMOTE_FILE_EXISTS", -1},
565 #endif
566 #if HAVE_DECL_CURLE_TFTP_NOSUCHUSER
567 {"CURLE_TFTP_NOSUCHUSER", CURLE_TFTP_NOSUCHUSER},
568 #else
569 {"CURLE_TFTP_NOSUCHUSER", -1},
570 #endif
571 #if HAVE_DECL_CURLE_CONV_FAILED
572 {"CURLE_CONV_FAILED", CURLE_CONV_FAILED},
573 #else
574 {"CURLE_CONV_FAILED", -1},
575 #endif
576 #if HAVE_DECL_CURLE_CONV_REQUIRED
577 {"CURLE_CONV_REQUIRED", CURLE_CONV_REQUIRED},
578 #else
579 {"CURLE_CONV_REQUIRED", -1},
580 #endif
581 #if HAVE_DECL_CURLE_SSL_CACERT_BADFILE
582 {"CURLE_SSL_CACERT_BADFILE", CURLE_SSL_CACERT_BADFILE},
583 #else
584 {"CURLE_SSL_CACERT_BADFILE", -1},
585 #endif
586 #if HAVE_DECL_CURLE_REMOTE_FILE_NOT_FOUND
587 {"CURLE_REMOTE_FILE_NOT_FOUND", CURLE_REMOTE_FILE_NOT_FOUND},
588 #else
589 {"CURLE_REMOTE_FILE_NOT_FOUND", -1},
590 #endif
591 #if HAVE_DECL_CURLE_SSH
592 {"CURLE_SSH", CURLE_SSH},
593 #else
594 {"CURLE_SSH", -1},
595 #endif
596 #if HAVE_DECL_CURLE_SSL_SHUTDOWN_FAILED
597 {"CURLE_SSL_SHUTDOWN_FAILED", CURLE_SSL_SHUTDOWN_FAILED},
598 #else
599 {"CURLE_SSL_SHUTDOWN_FAILED", -1},
600 #endif
601 #if HAVE_DECL_CURLE_AGAIN
602 {"CURLE_AGAIN", CURLE_AGAIN},
603 #else
604 {"CURLE_AGAIN", -1},
605 #endif
606 {"CURLE_OK", CURLE_OK},
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(3, 0);
666 Store_field(exceptionData, 0, Val_int(code));
667 Store_field(exceptionData, 1, Val_int(code));
668 Store_field(exceptionData, 2, copy_string(errorString));
670 if (conn != NULL && conn->curl_ERRORBUFFER != NULL)
672 Store_field(Field(conn->ocamlValues, Ocaml_ERRORBUFFER), 0,
673 copy_string(conn->curl_ERRORBUFFER));
676 exception = caml_named_value("CurlException");
678 if (exception == NULL)
679 caml_failwith("CurlException not registered");
681 raise_with_arg(*exception, exceptionData);
683 CAMLreturn0;
686 static void resetOcamlValues(Connection* connection)
688 int i;
690 for (i = 0; i < OcamlValuesSize; i++)
691 Store_field(connection->ocamlValues, i, Val_unit);
694 static Connection* allocConnection(CURL* h)
696 Connection* connection = (Connection *)malloc(sizeof(Connection));
698 connection->ocamlValues = caml_alloc(OcamlValuesSize, 0);
699 resetOcamlValues(connection);
700 register_global_root(&connection->ocamlValues);
702 connection->connection = h;
704 connection->next = NULL;
705 connection->prev = NULL;
707 if (connectionList.tail == NULL)
709 connectionList.tail = connection;
710 connectionList.head = connection;
712 else
714 connection->prev = connectionList.head;
715 connectionList.head->next = connection;
716 connectionList.head = connection;
719 connection->refcount = 0;
721 connection->curl_URL = NULL;
722 connection->curl_PROXY = NULL;
723 connection->curl_USERPWD = NULL;
724 connection->curl_PROXYUSERPWD = NULL;
725 connection->curl_RANGE = NULL;
726 connection->curl_ERRORBUFFER = NULL;
727 connection->curl_POSTFIELDS = NULL;
728 connection->curl_POSTFIELDSIZE = -1;
729 connection->curl_REFERER = NULL;
730 connection->curl_USERAGENT = NULL;
731 connection->curl_FTPPORT = NULL;
732 connection->curl_COOKIE = NULL;
733 connection->curl_HTTPHEADER = NULL;
734 connection->httpPostBuffers = NULL;
735 connection->httpPostFirst = NULL;
736 connection->httpPostLast = NULL;
737 connection->curl_SSLCERT = NULL;
738 connection->curl_SSLCERTTYPE = NULL;
739 connection->curl_SSLCERTPASSWD = NULL;
740 connection->curl_SSLKEY = NULL;
741 connection->curl_SSLKEYTYPE = NULL;
742 connection->curl_SSLKEYPASSWD = NULL;
743 connection->curl_SSLENGINE = NULL;
744 connection->curl_QUOTE = NULL;
745 connection->curl_POSTQUOTE = NULL;
746 connection->curl_COOKIEFILE = NULL;
747 connection->curl_CUSTOMREQUEST = NULL;
748 connection->curl_INTERFACE = NULL;
749 connection->curl_CAINFO = NULL;
750 connection->curl_CAPATH = NULL;
751 connection->curl_RANDOM_FILE = NULL;
752 connection->curl_EGDSOCKET = NULL;
753 connection->curl_COOKIEJAR = NULL;
754 connection->curl_SSL_CIPHER_LIST = NULL;
755 connection->curl_PRIVATE = NULL;
756 connection->curl_HTTP200ALIASES = NULL;
757 connection->curl_NETRC_FILE = NULL;
758 connection->curl_FTP_ACCOUNT = NULL;
759 connection->curl_COOKIELIST = NULL;
760 connection->curl_FTP_ALTERNATIVE_TO_USER = NULL;
761 connection->curl_SSH_PUBLIC_KEYFILE = NULL;
762 connection->curl_SSH_PRIVATE_KEYFILE = NULL;
763 connection->curl_COPYPOSTFIELDS = NULL;
764 connection->curl_RESOLVE = NULL;
765 connection->curl_DNS_SERVERS = NULL;
766 connection->curl_MAIL_FROM = NULL;
767 connection->curl_MAIL_RCPT = NULL;
769 return connection;
772 static Connection *newConnection(void)
774 CURL* h;
776 caml_enter_blocking_section();
777 h = curl_easy_init();
778 caml_leave_blocking_section();
780 return allocConnection(h);
783 static void free_if(void* p) { if (NULL != p) free(p); }
785 static void removeConnection(Connection *connection, int finalization)
787 const char* fin_url = NULL;
789 if (!connection->connection)
791 return; /* already cleaned up */
794 if (finalization)
796 /* cannot engage OCaml runtime at finalization, just report leak */
797 if (CURLE_OK != curl_easy_getinfo(connection->connection, CURLINFO_EFFECTIVE_URL, &fin_url) || NULL == fin_url)
799 fin_url = "unknown";
801 fprintf(stderr,"Curl: handle %p leaked, conn %p, url %s\n", connection->connection, connection, fin_url);
802 fflush(stderr);
804 else
806 enter_blocking_section();
807 curl_easy_cleanup(connection->connection);
808 leave_blocking_section();
811 connection->connection = NULL;
813 if (connectionList.tail == connection)
814 connectionList.tail = connectionList.tail->next;
815 if (connectionList.head == connection)
816 connectionList.head = connectionList.head->prev;
818 if (connection->next != NULL)
819 connection->next->prev = connection->prev;
820 if (connection->prev != NULL)
821 connection->prev->next = connection->next;
823 remove_global_root(&connection->ocamlValues);
825 free_if(connection->curl_URL);
826 free_if(connection->curl_PROXY);
827 free_if(connection->curl_USERPWD);
828 free_if(connection->curl_PROXYUSERPWD);
829 free_if(connection->curl_RANGE);
830 free_if(connection->curl_ERRORBUFFER);
831 free_if(connection->curl_POSTFIELDS);
832 free_if(connection->curl_REFERER);
833 free_if(connection->curl_USERAGENT);
834 free_if(connection->curl_FTPPORT);
835 free_if(connection->curl_COOKIE);
836 free_curl_slist(connection->curl_HTTPHEADER);
837 free_curl_slist(connection->httpPostBuffers);
838 if (connection->httpPostFirst != NULL)
839 curl_formfree(connection->httpPostFirst);
840 free_curl_slist(connection->curl_RESOLVE);
841 free_if(connection->curl_SSLCERT);
842 free_if(connection->curl_SSLCERTTYPE);
843 free_if(connection->curl_SSLCERTPASSWD);
844 free_if(connection->curl_SSLKEY);
845 free_if(connection->curl_SSLKEYTYPE);
846 free_if(connection->curl_SSLKEYPASSWD);
847 free_if(connection->curl_SSLENGINE);
848 free_curl_slist(connection->curl_QUOTE);
849 free_curl_slist(connection->curl_POSTQUOTE);
850 free_if(connection->curl_COOKIEFILE);
851 free_if(connection->curl_CUSTOMREQUEST);
852 free_if(connection->curl_INTERFACE);
853 free_if(connection->curl_CAINFO);
854 free_if(connection->curl_CAPATH);
855 free_if(connection->curl_RANDOM_FILE);
856 free_if(connection->curl_EGDSOCKET);
857 free_if(connection->curl_COOKIEJAR);
858 free_if(connection->curl_SSL_CIPHER_LIST);
859 free_if(connection->curl_PRIVATE);
860 free_curl_slist(connection->curl_HTTP200ALIASES);
861 free_if(connection->curl_NETRC_FILE);
862 free_if(connection->curl_FTP_ACCOUNT);
863 free_if(connection->curl_COOKIELIST);
864 free_if(connection->curl_FTP_ALTERNATIVE_TO_USER);
865 free_if(connection->curl_SSH_PUBLIC_KEYFILE);
866 free_if(connection->curl_SSH_PRIVATE_KEYFILE);
867 free_if(connection->curl_COPYPOSTFIELDS);
868 free_if(connection->curl_DNS_SERVERS);
869 free_if(connection->curl_MAIL_FROM);
870 free_curl_slist(connection->curl_MAIL_RCPT);
873 #if 1
874 static void checkConnection(Connection * connection)
876 (void)connection;
878 #else
879 static void checkConnection(Connection *connection)
881 Connection *listIter;
883 listIter = connectionList.tail;
885 while (listIter != NULL)
887 if (listIter == connection)
888 return;
890 listIter = listIter->next;
893 failwith("Invalid Connection");
895 #endif
897 static Connection* findConnection(CURL* h)
899 Connection *listIter;
901 listIter = connectionList.tail;
903 while (listIter != NULL)
905 if (listIter->connection == h)
906 return listIter;
908 listIter = listIter->next;
911 failwith("Unknown handle");
914 void op_curl_easy_finalize(value v)
916 Connection* conn = Connection_val(v);
917 /* same connection may be referenced by several different
918 OCaml values, see e.g. caml_curl_multi_remove_finished */
919 conn->refcount--;
920 if (0 == conn->refcount)
922 removeConnection(conn, 1);
923 free(conn);
927 int op_curl_easy_compare(value v1, value v2)
929 size_t p1 = (size_t)Connection_val(v1);
930 size_t p2 = (size_t)Connection_val(v2);
931 return (p1 == p2 ? 0 : (p1 > p2 ? 1 : -1)); /* compare addresses */
934 intnat op_curl_easy_hash(value v)
936 return (size_t)Connection_val(v); /* address */
939 static struct custom_operations curl_easy_ops = {
940 "ygrek.curl_easy",
941 op_curl_easy_finalize,
942 op_curl_easy_compare,
943 op_curl_easy_hash,
944 custom_serialize_default,
945 custom_deserialize_default,
946 #if defined(custom_compare_ext_default)
947 custom_compare_ext_default,
948 #endif
951 value caml_curl_alloc(Connection* conn)
953 value v = caml_alloc_custom(&curl_easy_ops, sizeof(Connection*), 0, 1);
954 Connection_val(v) = conn;
955 conn->refcount++;
956 return v;
959 #define WRAP_DATA_CALLBACK(name) \
960 static size_t cb_##name(char *ptr, size_t size, size_t nmemb, void *data)\
962 size_t result;\
963 leave_blocking_section();\
964 result = cb_##name##_nolock(ptr,size,nmemb,data);\
965 enter_blocking_section();\
966 return result;\
969 static size_t cb_WRITEFUNCTION_nolock(char *ptr, size_t size, size_t nmemb, void *data)
971 CAMLparam0();
972 CAMLlocal2(result, str);
973 Connection *conn = (Connection *)data;
974 size_t i;
976 checkConnection(conn);
978 str = alloc_string(size*nmemb);
980 for (i = 0; i < size*nmemb; i++)
981 Byte(str, i) = ptr[i];
983 result = callback_exn(Field(conn->ocamlValues, Ocaml_WRITEFUNCTION), str);
985 CAMLreturnT(size_t, Is_exception_result(result) ? 0 : Int_val(result));
988 WRAP_DATA_CALLBACK( WRITEFUNCTION)
990 static size_t cb_READFUNCTION_nolock(void *ptr, size_t size, size_t nmemb, void *data)
992 CAMLparam0();
993 CAMLlocal1(result);
994 Connection *conn = (Connection *)data;
995 size_t length;
997 checkConnection(conn);
999 result = callback_exn(Field(conn->ocamlValues, Ocaml_READFUNCTION),
1000 Val_int(size*nmemb));
1002 if (Is_exception_result(result))
1004 CAMLreturnT(size_t,CURL_READFUNC_ABORT);
1007 length = string_length(result);
1009 if (length <= size*nmemb)
1011 memcpy(ptr, String_val(result), length);
1013 CAMLreturnT(size_t,length);
1015 else
1017 CAMLreturnT(size_t,CURL_READFUNC_ABORT);
1021 WRAP_DATA_CALLBACK( READFUNCTION)
1023 static size_t cb_HEADERFUNCTION_nolock(char *ptr, size_t size, size_t nmemb, void *data)
1025 CAMLparam0();
1026 CAMLlocal2(result,str);
1027 Connection *conn = (Connection *)data;
1028 size_t i;
1030 checkConnection(conn);
1032 str = alloc_string(size*nmemb);
1034 for (i = 0; i < size*nmemb; i++)
1035 Byte(str, i) = ptr[i];
1037 result = callback_exn(Field(conn->ocamlValues, Ocaml_HEADERFUNCTION), str);
1039 CAMLreturnT(size_t, Is_exception_result(result) ? 0 : Int_val(result));
1042 WRAP_DATA_CALLBACK( HEADERFUNCTION)
1044 static int cb_PROGRESSFUNCTION_nolock(void *data,
1045 double dlTotal,
1046 double dlNow,
1047 double ulTotal,
1048 double ulNow)
1050 CAMLparam0();
1051 CAMLlocal1(result);
1052 CAMLlocalN(callbackData, 4);
1053 Connection *conn = (Connection *)data;
1055 checkConnection(conn);
1057 callbackData[0] = copy_double(dlTotal);
1058 callbackData[1] = copy_double(dlNow);
1059 callbackData[2] = copy_double(ulTotal);
1060 callbackData[3] = copy_double(ulNow);
1062 result = callbackN_exn(Field(conn->ocamlValues, Ocaml_PROGRESSFUNCTION),
1063 4, callbackData);
1065 CAMLreturnT(int, Is_exception_result(result) ? 1 : Bool_val(result));
1068 static int cb_PROGRESSFUNCTION(void *data,
1069 double dlTotal,
1070 double dlNow,
1071 double ulTotal,
1072 double ulNow)
1074 int r;
1075 leave_blocking_section();
1076 r = cb_PROGRESSFUNCTION_nolock(data,dlTotal,dlNow,ulTotal,ulNow);
1077 enter_blocking_section();
1078 return r;
1081 static int cb_DEBUGFUNCTION_nolock(CURL *debugConnection,
1082 curl_infotype infoType,
1083 char *buffer,
1084 size_t bufferLength,
1085 void *data)
1087 CAMLparam0();
1088 CAMLlocal3(camlDebugConnection, camlInfoType, camlMessage);
1089 size_t i;
1090 Connection *conn = (Connection *)data;
1091 (void)debugConnection; /* not used */
1093 checkConnection(conn);
1095 camlDebugConnection = (value)conn;
1096 camlInfoType = Val_long(infoType);
1097 camlMessage = alloc_string(bufferLength);
1099 for (i = 0; i < bufferLength; i++)
1100 Byte(camlMessage, i) = buffer[i];
1102 callback3_exn(Field(conn->ocamlValues, Ocaml_DEBUGFUNCTION),
1103 camlDebugConnection,
1104 camlInfoType,
1105 camlMessage);
1107 CAMLreturnT(int, 0);
1110 static int cb_DEBUGFUNCTION(CURL *debugConnection,
1111 curl_infotype infoType,
1112 char *buffer,
1113 size_t bufferLength,
1114 void *data)
1116 int r;
1117 leave_blocking_section();
1118 r = cb_DEBUGFUNCTION_nolock(debugConnection, infoType, buffer, bufferLength, data);
1119 enter_blocking_section();
1120 return r;
1123 static curlioerr cb_IOCTLFUNCTION_nolock(CURL *ioctl,
1124 int cmd,
1125 void *data)
1127 CAMLparam0();
1128 CAMLlocal3(camlResult, camlConnection, camlCmd);
1129 Connection *conn = (Connection *)data;
1130 curlioerr result = CURLIOE_OK;
1131 (void)ioctl; /* not used */
1133 checkConnection(conn);
1135 if (cmd == CURLIOCMD_NOP)
1136 camlCmd = Val_long(0);
1137 else if (cmd == CURLIOCMD_RESTARTREAD)
1138 camlCmd = Val_long(1);
1139 else
1140 failwith("Invalid IOCTL Cmd!");
1142 camlConnection = caml_curl_alloc(conn);
1144 camlResult = callback2_exn(Field(conn->ocamlValues, Ocaml_IOCTLFUNCTION),
1145 camlConnection,
1146 camlCmd);
1148 if (Is_exception_result(camlResult))
1150 result = CURLIOE_FAILRESTART;
1152 else
1153 switch (Long_val(camlResult))
1155 case 0: /* CURLIOE_OK */
1156 result = CURLIOE_OK;
1157 break;
1159 case 1: /* CURLIOE_UNKNOWNCMD */
1160 result = CURLIOE_UNKNOWNCMD;
1161 break;
1163 case 2: /* CURLIOE_FAILRESTART */
1164 result = CURLIOE_FAILRESTART;
1165 break;
1167 default: /* Incorrect return value, but let's handle it */
1168 result = CURLIOE_FAILRESTART;
1169 break;
1172 CAMLreturnT(curlioerr, result);
1175 static curlioerr cb_IOCTLFUNCTION(CURL *ioctl,
1176 int cmd,
1177 void *data)
1179 curlioerr r;
1180 leave_blocking_section();
1181 r = cb_IOCTLFUNCTION_nolock(ioctl, cmd, data);
1182 enter_blocking_section();
1183 return r;
1186 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1187 static int cb_SEEKFUNCTION_nolock(void *data,
1188 curl_off_t offset,
1189 int origin)
1191 CAMLparam0();
1192 CAMLlocal3(camlResult, camlOffset, camlOrigin);
1193 Connection *conn = (Connection *)data;
1195 camlOffset = copy_int64(offset);
1197 if (origin == SEEK_SET)
1198 camlOrigin = Val_long(0);
1199 else if (origin == SEEK_CUR)
1200 camlOrigin = Val_long(1);
1201 else if (origin == SEEK_END)
1202 camlOrigin = Val_long(2);
1203 else
1204 failwith("Invalid seek code");
1206 camlResult = callback2_exn(Field(conn->ocamlValues,
1207 Ocaml_SEEKFUNCTION),
1208 camlOffset,
1209 camlOrigin);
1211 int result;
1212 if (Is_exception_result(camlResult))
1213 result = CURL_SEEKFUNC_FAIL;
1214 else
1215 switch (Int_val(camlResult))
1217 case 0: result = CURL_SEEKFUNC_OK; break;
1218 case 1: result = CURL_SEEKFUNC_FAIL; break;
1219 case 2: result = CURL_SEEKFUNC_CANTSEEK; break;
1220 default: failwith("Invalid seek result");
1223 CAMLreturnT(int, result);
1226 static int cb_SEEKFUNCTION(void *data,
1227 curl_off_t offset,
1228 int origin)
1230 int r;
1231 leave_blocking_section();
1232 r = cb_SEEKFUNCTION_nolock(data,offset,origin);
1233 enter_blocking_section();
1234 return r;
1237 #endif
1239 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
1240 static int cb_OPENSOCKETFUNCTION_nolock(void *data,
1241 curlsocktype purpose,
1242 struct curl_sockaddr *addr)
1244 CAMLparam0();
1245 CAMLlocal1(result);
1246 Connection *conn = (Connection *)data;
1247 int sock = -1;
1248 (void)purpose; /* not used */
1250 sock = socket(addr->family, addr->socktype, addr->protocol);
1252 if (-1 != sock)
1254 /* FIXME windows */
1255 result = callback_exn(Field(conn->ocamlValues, Ocaml_OPENSOCKETFUNCTION), Val_int(sock));
1256 if (Is_exception_result(result))
1258 close(sock);
1259 sock = -1;
1263 CAMLreturnT(int, (sock == -1) ? CURL_SOCKET_BAD : sock);
1266 static int cb_OPENSOCKETFUNCTION(void *data,
1267 curlsocktype purpose,
1268 struct curl_sockaddr *address)
1270 int r;
1271 leave_blocking_section();
1272 r = cb_OPENSOCKETFUNCTION_nolock(data,purpose,address);
1273 enter_blocking_section();
1274 return r;
1277 #endif
1280 ** curl_global_init helper function
1283 CAMLprim value helper_curl_global_init(value initOption)
1285 CAMLparam1(initOption);
1287 switch (Long_val(initOption))
1289 case 0: /* CURLINIT_GLOBALALL */
1290 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_ALL)));
1291 break;
1293 case 1: /* CURLINIT_GLOBALSSL */
1294 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_SSL)));
1295 break;
1297 case 2: /* CURLINIT_GLOBALWIN32 */
1298 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_WIN32)));
1299 break;
1301 case 3: /* CURLINIT_GLOBALNOTHING */
1302 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_NOTHING)));
1303 break;
1305 default:
1306 failwith("Invalid Initialization Option");
1307 break;
1310 /* Keep compiler happy, we should never get here due to failwith() */
1311 CAMLreturn(Val_unit);
1315 ** curl_global_cleanup helper function
1318 CAMLprim value helper_curl_global_cleanup(void)
1320 CAMLparam0();
1322 curl_global_cleanup();
1324 CAMLreturn(Val_unit);
1328 ** curl_easy_init helper function
1330 CAMLprim value helper_curl_easy_init(void)
1332 CAMLparam0();
1333 CAMLlocal1(result);
1335 result = caml_curl_alloc(newConnection());
1337 CAMLreturn(result);
1340 CAMLprim value helper_curl_easy_reset(value conn)
1342 CAMLparam1(conn);
1343 Connection *connection = Connection_val(conn);
1345 checkConnection(connection);
1346 curl_easy_reset(connection->connection);
1347 resetOcamlValues(connection);
1349 CAMLreturn(Val_unit);
1353 ** curl_easy_setopt helper utility functions
1356 #define SETOPT_FUNCTION(name) \
1357 static void handle_##name##FUNCTION(Connection *conn, value option) \
1359 CAMLparam1(option); \
1360 CURLcode result = CURLE_OK; \
1361 Store_field(conn->ocamlValues, Ocaml_##name##FUNCTION, option); \
1362 result = curl_easy_setopt(conn->connection, CURLOPT_##name##FUNCTION, cb_##name##FUNCTION); \
1363 if (result != CURLE_OK) raiseError(conn, result); \
1364 result = curl_easy_setopt(conn->connection, CURLOPT_##name##DATA, conn); \
1365 if (result != CURLE_OK) raiseError(conn, result); \
1366 CAMLreturn0; \
1369 SETOPT_FUNCTION( WRITE)
1370 SETOPT_FUNCTION( READ)
1371 SETOPT_FUNCTION( HEADER)
1372 SETOPT_FUNCTION( PROGRESS)
1373 SETOPT_FUNCTION( DEBUG)
1375 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1376 SETOPT_FUNCTION( SEEK)
1377 #endif
1379 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
1380 SETOPT_FUNCTION( IOCTL)
1381 #endif
1383 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
1384 SETOPT_FUNCTION( OPENSOCKET)
1385 #endif
1387 static void handle_slist(Connection *conn, struct curl_slist** slist, OcamlValue caml_option, CURLoption curl_option, value option)
1389 CAMLparam1(option);
1390 CURLcode result = CURLE_OK;
1392 Store_field(conn->ocamlValues, caml_option, option);
1394 free_curl_slist(*slist);
1395 *slist = NULL;
1397 while (Val_emptylist != option)
1399 *slist = curl_slist_append(*slist, String_val(Field(option, 0)));
1401 option = Field(option, 1);
1404 result = curl_easy_setopt(conn->connection, curl_option, *slist);
1406 if (result != CURLE_OK)
1407 raiseError(conn, result);
1409 CAMLreturn0;
1412 #define SETOPT_STRING(name) \
1413 static void handle_##name(Connection *conn, value option) \
1415 CAMLparam1(option); \
1416 CURLcode result = CURLE_OK; \
1418 Store_field(conn->ocamlValues, Ocaml_##name, option); \
1420 if (conn->curl_##name != NULL) \
1421 free(conn->curl_##name); \
1423 conn->curl_##name = strdup(String_val(option)); \
1425 result = curl_easy_setopt(conn->connection, CURLOPT_##name, conn->curl_##name); \
1427 if (result != CURLE_OK) \
1428 raiseError(conn, result); \
1430 CAMLreturn0; \
1433 #define SETOPT_VAL_(func_name, curl_option, conv_val) \
1434 static void func_name(Connection *conn, value option) \
1436 CAMLparam1(option); \
1437 CURLcode result = CURLE_OK; \
1439 result = curl_easy_setopt(conn->connection, curl_option, conv_val(option)); \
1441 if (result != CURLE_OK) \
1442 raiseError(conn, result); \
1444 CAMLreturn0; \
1447 #define SETOPT_VAL(name, conv) SETOPT_VAL_(handle_##name, CURLOPT_##name, conv)
1448 #define SETOPT_BOOL(name) SETOPT_VAL(name, Bool_val)
1449 #define SETOPT_LONG(name) SETOPT_VAL(name, Long_val)
1450 #define SETOPT_INT64(name) SETOPT_VAL(name, Int64_val)
1452 #define SETOPT_SLIST(name) \
1453 static void handle_##name(Connection* conn, value option) \
1455 handle_slist(conn,&(conn->curl_##name),Ocaml_##name,CURLOPT_##name,option); \
1458 SETOPT_STRING( URL)
1459 SETOPT_LONG( INFILESIZE)
1460 SETOPT_STRING( PROXY)
1461 SETOPT_LONG( PROXYPORT)
1462 SETOPT_BOOL( HTTPPROXYTUNNEL)
1463 SETOPT_BOOL( VERBOSE)
1464 SETOPT_BOOL( HEADER)
1465 SETOPT_BOOL( NOPROGRESS)
1467 #if HAVE_DECL_CURLOPT_NOSIGNAL
1468 SETOPT_BOOL( NOSIGNAL)
1469 #endif
1471 SETOPT_BOOL( NOBODY)
1472 SETOPT_BOOL( FAILONERROR)
1473 SETOPT_BOOL( UPLOAD)
1474 SETOPT_BOOL( POST)
1475 SETOPT_BOOL( FTPLISTONLY)
1476 SETOPT_BOOL( FTPAPPEND)
1479 static void handle_NETRC(Connection *conn, value option)
1481 CAMLparam1(option);
1482 CURLcode result = CURLE_OK;
1483 long netrc;
1485 switch (Long_val(option))
1487 case 0: /* CURL_NETRC_OPTIONAL */
1488 netrc = CURL_NETRC_OPTIONAL;
1489 break;
1491 case 1:/* CURL_NETRC_IGNORED */
1492 netrc = CURL_NETRC_IGNORED;
1493 break;
1495 case 2: /* CURL_NETRC_REQUIRED */
1496 netrc = CURL_NETRC_REQUIRED;
1497 break;
1499 default:
1500 failwith("Invalid NETRC Option");
1501 break;
1504 result = curl_easy_setopt(conn->connection,
1505 CURLOPT_NETRC,
1506 netrc);
1508 if (result != CURLE_OK)
1509 raiseError(conn, result);
1511 CAMLreturn0;
1514 #if HAVE_DECL_CURLOPT_ENCODING
1515 static void handle_ENCODING(Connection *conn, value option)
1517 CAMLparam1(option);
1518 CURLcode result = CURLE_OK;
1520 switch (Long_val(option))
1522 case 0: /* CURL_ENCODING_NONE */
1523 result = curl_easy_setopt(conn->connection,
1524 CURLOPT_ENCODING,
1525 "identity");
1526 break;
1528 case 1: /* CURL_ENCODING_DEFLATE */
1529 result = curl_easy_setopt(conn->connection,
1530 CURLOPT_ENCODING,
1531 "deflate");
1532 break;
1534 case 2: /* CURL_ENCODING_GZIP */
1535 result = curl_easy_setopt(conn->connection,
1536 CURLOPT_ENCODING,
1537 "gzip");
1538 break;
1540 case 3: /* CURL_ENCODING_ANY */
1541 result = curl_easy_setopt(conn->connection,
1542 CURLOPT_ENCODING,
1543 "");
1544 break;
1546 default:
1547 failwith("Invalid Encoding Option");
1548 break;
1551 if (result != CURLE_OK)
1552 raiseError(conn, result);
1554 CAMLreturn0;
1556 #endif
1559 SETOPT_BOOL( FOLLOWLOCATION)
1560 SETOPT_BOOL( TRANSFERTEXT)
1561 SETOPT_BOOL( PUT)
1562 SETOPT_STRING( USERPWD)
1563 SETOPT_STRING( PROXYUSERPWD)
1564 SETOPT_STRING( RANGE)
1566 static void handle_ERRORBUFFER(Connection *conn, value option)
1568 CAMLparam1(option);
1569 CURLcode result = CURLE_OK;
1571 Store_field(conn->ocamlValues, Ocaml_ERRORBUFFER, option);
1573 if (conn->curl_ERRORBUFFER != NULL)
1574 free(conn->curl_ERRORBUFFER);
1576 conn->curl_ERRORBUFFER = malloc(sizeof(char) * CURL_ERROR_SIZE);
1578 result = curl_easy_setopt(conn->connection,
1579 CURLOPT_ERRORBUFFER,
1580 conn->curl_ERRORBUFFER);
1582 if (result != CURLE_OK)
1583 raiseError(conn, result);
1585 CAMLreturn0;
1588 SETOPT_LONG( TIMEOUT)
1590 static void handle_POSTFIELDS(Connection *conn, value option)
1592 CAMLparam1(option);
1593 CURLcode result = CURLE_OK;
1595 Store_field(conn->ocamlValues, Ocaml_POSTFIELDS, option);
1597 if (conn->curl_POSTFIELDS != NULL)
1598 free(conn->curl_POSTFIELDS);
1600 conn->curl_POSTFIELDS = strdup_ml(option);
1602 result = curl_easy_setopt(conn->connection,
1603 CURLOPT_POSTFIELDS,
1604 conn->curl_POSTFIELDS);
1606 if (result != CURLE_OK)
1607 raiseError(conn, result);
1609 CAMLreturn0;
1612 SETOPT_LONG( POSTFIELDSIZE)
1613 SETOPT_STRING( REFERER)
1614 SETOPT_STRING( USERAGENT)
1615 SETOPT_STRING( FTPPORT)
1616 SETOPT_LONG( LOW_SPEED_LIMIT)
1617 SETOPT_LONG( LOW_SPEED_TIME)
1618 SETOPT_LONG( RESUME_FROM)
1619 SETOPT_STRING( COOKIE)
1621 SETOPT_SLIST( HTTPHEADER)
1623 static void handle_HTTPPOST(Connection *conn, value option)
1625 CAMLparam1(option);
1626 CAMLlocal3(listIter, formItem, contentType);
1627 CURLcode result = CURLE_OK;
1629 listIter = option;
1631 Store_field(conn->ocamlValues, Ocaml_HTTPPOST, option);
1633 free_curl_slist(conn->httpPostBuffers);
1634 if (conn->httpPostFirst != NULL)
1635 curl_formfree(conn->httpPostFirst);
1637 conn->httpPostBuffers = NULL;
1638 conn->httpPostFirst = NULL;
1639 conn->httpPostLast = NULL;
1641 while (!Is_long(listIter))
1643 formItem = Field(listIter, 0);
1645 switch (Tag_val(formItem))
1647 case 0: /* CURLFORM_CONTENT */
1648 if (Wosize_val(formItem) < 3)
1650 failwith("Incorrect CURLFORM_CONTENT parameters");
1653 if (Is_long(Field(formItem, 2)) &&
1654 Long_val(Field(formItem, 2)) == 0)
1656 curl_formadd(&conn->httpPostFirst,
1657 &conn->httpPostLast,
1658 CURLFORM_COPYNAME,
1659 String_val(Field(formItem, 0)),
1660 CURLFORM_NAMELENGTH,
1661 string_length(Field(formItem, 0)),
1662 CURLFORM_COPYCONTENTS,
1663 String_val(Field(formItem, 1)),
1664 CURLFORM_CONTENTSLENGTH,
1665 string_length(Field(formItem, 1)),
1666 CURLFORM_END);
1668 else if (Is_block(Field(formItem, 2)))
1670 contentType = Field(formItem, 2);
1672 curl_formadd(&conn->httpPostFirst,
1673 &conn->httpPostLast,
1674 CURLFORM_COPYNAME,
1675 String_val(Field(formItem, 0)),
1676 CURLFORM_NAMELENGTH,
1677 string_length(Field(formItem, 0)),
1678 CURLFORM_COPYCONTENTS,
1679 String_val(Field(formItem, 1)),
1680 CURLFORM_CONTENTSLENGTH,
1681 string_length(Field(formItem, 1)),
1682 CURLFORM_CONTENTTYPE,
1683 String_val(Field(contentType, 0)),
1684 CURLFORM_END);
1686 else
1688 failwith("Incorrect CURLFORM_CONTENT parameters");
1690 break;
1692 case 1: /* CURLFORM_FILECONTENT */
1693 if (Wosize_val(formItem) < 3)
1695 failwith("Incorrect CURLFORM_FILECONTENT parameters");
1698 if (Is_long(Field(formItem, 2)) &&
1699 Long_val(Field(formItem, 2)) == 0)
1701 curl_formadd(&conn->httpPostFirst,
1702 &conn->httpPostLast,
1703 CURLFORM_COPYNAME,
1704 String_val(Field(formItem, 0)),
1705 CURLFORM_NAMELENGTH,
1706 string_length(Field(formItem, 0)),
1707 CURLFORM_FILECONTENT,
1708 String_val(Field(formItem, 1)),
1709 CURLFORM_END);
1711 else if (Is_block(Field(formItem, 2)))
1713 contentType = Field(formItem, 2);
1715 curl_formadd(&conn->httpPostFirst,
1716 &conn->httpPostLast,
1717 CURLFORM_COPYNAME,
1718 String_val(Field(formItem, 0)),
1719 CURLFORM_NAMELENGTH,
1720 string_length(Field(formItem, 0)),
1721 CURLFORM_FILECONTENT,
1722 String_val(Field(formItem, 1)),
1723 CURLFORM_CONTENTTYPE,
1724 String_val(Field(contentType, 0)),
1725 CURLFORM_END);
1727 else
1729 failwith("Incorrect CURLFORM_FILECONTENT parameters");
1731 break;
1733 case 2: /* CURLFORM_FILE */
1734 if (Wosize_val(formItem) < 3)
1736 failwith("Incorrect CURLFORM_FILE parameters");
1739 if (Is_long(Field(formItem, 2)) &&
1740 Long_val(Field(formItem, 2)) == 0)
1742 curl_formadd(&conn->httpPostFirst,
1743 &conn->httpPostLast,
1744 CURLFORM_COPYNAME,
1745 String_val(Field(formItem, 0)),
1746 CURLFORM_NAMELENGTH,
1747 string_length(Field(formItem, 0)),
1748 CURLFORM_FILE,
1749 String_val(Field(formItem, 1)),
1750 CURLFORM_END);
1752 else if (Is_block(Field(formItem, 2)))
1754 contentType = Field(formItem, 2);
1756 curl_formadd(&conn->httpPostFirst,
1757 &conn->httpPostLast,
1758 CURLFORM_COPYNAME,
1759 String_val(Field(formItem, 0)),
1760 CURLFORM_NAMELENGTH,
1761 string_length(Field(formItem, 0)),
1762 CURLFORM_FILE,
1763 String_val(Field(formItem, 1)),
1764 CURLFORM_CONTENTTYPE,
1765 String_val(Field(contentType, 0)),
1766 CURLFORM_END);
1768 else
1770 failwith("Incorrect CURLFORM_FILE parameters");
1772 break;
1774 case 3: /* CURLFORM_BUFFER */
1775 if (Wosize_val(formItem) < 4)
1777 failwith("Incorrect CURLFORM_BUFFER parameters");
1780 if (Is_long(Field(formItem, 3)) &&
1781 Long_val(Field(formItem, 3)) == 0)
1783 conn->httpPostBuffers = curl_slist_prepend_ml(conn->httpPostBuffers, Field(formItem, 2));
1785 curl_formadd(&conn->httpPostFirst,
1786 &conn->httpPostLast,
1787 CURLFORM_COPYNAME,
1788 String_val(Field(formItem, 0)),
1789 CURLFORM_NAMELENGTH,
1790 string_length(Field(formItem, 0)),
1791 CURLFORM_BUFFER,
1792 String_val(Field(formItem, 1)),
1793 CURLFORM_BUFFERPTR,
1794 conn->httpPostBuffers->data,
1795 CURLFORM_BUFFERLENGTH,
1796 string_length(Field(formItem, 2)),
1797 CURLFORM_END);
1799 else if (Is_block(Field(formItem, 3)))
1801 conn->httpPostBuffers = curl_slist_prepend_ml(conn->httpPostBuffers, Field(formItem, 2));
1803 contentType = Field(formItem, 3);
1805 curl_formadd(&conn->httpPostFirst,
1806 &conn->httpPostLast,
1807 CURLFORM_COPYNAME,
1808 String_val(Field(formItem, 0)),
1809 CURLFORM_NAMELENGTH,
1810 string_length(Field(formItem, 0)),
1811 CURLFORM_BUFFER,
1812 String_val(Field(formItem, 1)),
1813 CURLFORM_BUFFERPTR,
1814 conn->httpPostBuffers->data,
1815 CURLFORM_BUFFERLENGTH,
1816 string_length(Field(formItem, 2)),
1817 CURLFORM_CONTENTTYPE,
1818 String_val(Field(contentType, 0)),
1819 CURLFORM_END);
1821 else
1823 failwith("Incorrect CURLFORM_BUFFER parameters");
1825 break;
1828 listIter = Field(listIter, 1);
1831 result = curl_easy_setopt(conn->connection,
1832 CURLOPT_HTTPPOST,
1833 conn->httpPostFirst);
1835 if (result != CURLE_OK)
1836 raiseError(conn, result);
1838 CAMLreturn0;
1841 SETOPT_STRING( SSLCERT)
1842 SETOPT_STRING( SSLCERTTYPE)
1843 SETOPT_STRING( SSLCERTPASSWD)
1844 SETOPT_STRING( SSLKEY)
1845 SETOPT_STRING( SSLKEYTYPE)
1846 SETOPT_STRING( SSLKEYPASSWD)
1847 SETOPT_STRING( SSLENGINE)
1848 SETOPT_BOOL( SSLENGINE_DEFAULT)
1849 SETOPT_BOOL( CRLF)
1851 SETOPT_SLIST( QUOTE)
1852 SETOPT_SLIST( POSTQUOTE)
1854 SETOPT_STRING( COOKIEFILE)
1855 SETOPT_LONG( SSLVERSION)
1857 static void handle_TIMECONDITION(Connection *conn, value option)
1859 CAMLparam1(option);
1860 CURLcode result = CURLE_OK;
1861 int timecond = CURL_TIMECOND_NONE;
1863 switch (Long_val(option))
1865 case 0: timecond = CURL_TIMECOND_NONE; break;
1866 case 1: timecond = CURL_TIMECOND_IFMODSINCE; break;
1867 case 2: timecond = CURL_TIMECOND_IFUNMODSINCE; break;
1868 case 3: timecond = CURL_TIMECOND_LASTMOD; break;
1869 default:
1870 failwith("Invalid TIMECOND Option");
1871 break;
1874 result = curl_easy_setopt(conn->connection, CURLOPT_TIMECONDITION, timecond);
1876 if (result != CURLE_OK)
1877 raiseError(conn, result);
1879 CAMLreturn0;
1882 SETOPT_VAL( TIMEVALUE, Int32_val)
1883 SETOPT_STRING( CUSTOMREQUEST)
1884 SETOPT_STRING( INTERFACE)
1886 static void handle_KRB4LEVEL(Connection *conn, value option)
1888 CAMLparam1(option);
1889 CURLcode result = CURLE_OK;
1891 switch (Long_val(option))
1893 case 0: /* KRB4_NONE */
1894 result = curl_easy_setopt(conn->connection,
1895 CURLOPT_KRB4LEVEL,
1896 NULL);
1897 break;
1899 case 1: /* KRB4_CLEAR */
1900 result = curl_easy_setopt(conn->connection,
1901 CURLOPT_KRB4LEVEL,
1902 "clear");
1903 break;
1905 case 2: /* KRB4_SAFE */
1906 result = curl_easy_setopt(conn->connection,
1907 CURLOPT_KRB4LEVEL,
1908 "safe");
1909 break;
1911 case 3: /* KRB4_CONFIDENTIAL */
1912 result = curl_easy_setopt(conn->connection,
1913 CURLOPT_KRB4LEVEL,
1914 "confidential");
1915 break;
1917 case 4: /* KRB4_PRIVATE */
1918 result = curl_easy_setopt(conn->connection,
1919 CURLOPT_KRB4LEVEL,
1920 "private");
1921 break;
1923 default:
1924 failwith("Invalid KRB4 Option");
1925 break;
1928 if (result != CURLE_OK)
1929 raiseError(conn, result);
1931 CAMLreturn0;
1934 SETOPT_BOOL( SSL_VERIFYPEER)
1935 SETOPT_STRING( CAINFO)
1936 SETOPT_STRING( CAPATH)
1937 SETOPT_BOOL( FILETIME)
1938 SETOPT_LONG( MAXREDIRS)
1939 SETOPT_LONG( MAXCONNECTS)
1941 static void handle_CLOSEPOLICY(Connection *conn, value option)
1943 CAMLparam1(option);
1944 CURLcode result = CURLE_OK;
1946 switch (Long_val(option))
1948 case 0: /* CLOSEPOLICY_OLDEST */
1949 result = curl_easy_setopt(conn->connection,
1950 CURLOPT_CLOSEPOLICY,
1951 CURLCLOSEPOLICY_OLDEST);
1952 break;
1954 case 1: /* CLOSEPOLICY_LEAST_RECENTLY_USED */
1955 result = curl_easy_setopt(conn->connection,
1956 CURLOPT_CLOSEPOLICY,
1957 CURLCLOSEPOLICY_LEAST_RECENTLY_USED);
1958 break;
1960 default:
1961 failwith("Invalid CLOSEPOLICY Option");
1962 break;
1965 if (result != CURLE_OK)
1966 raiseError(conn, result);
1968 CAMLreturn0;
1971 SETOPT_BOOL( FRESH_CONNECT)
1972 SETOPT_BOOL( FORBID_REUSE)
1973 SETOPT_STRING( RANDOM_FILE)
1974 SETOPT_STRING( EGDSOCKET)
1975 SETOPT_LONG( CONNECTTIMEOUT)
1976 SETOPT_BOOL( HTTPGET)
1978 static void handle_SSL_VERIFYHOST(Connection *conn, value option)
1980 CAMLparam1(option);
1981 CURLcode result = CURLE_OK;
1983 switch (Long_val(option))
1985 case 0: /* SSLVERIFYHOST_NONE */
1986 case 1: /* SSLVERIFYHOST_EXISTENCE */
1987 case 2: /* SSLVERIFYHOST_HOSTNAME */
1988 result = curl_easy_setopt(conn->connection,
1989 CURLOPT_SSL_VERIFYHOST,
1990 /* map EXISTENCE to HOSTNAME */
1991 Long_val(option) == 0 ? 0 : 2);
1992 break;
1994 default:
1995 failwith("Invalid SSLVERIFYHOST Option");
1996 break;
1999 if (result != CURLE_OK)
2000 raiseError(conn, result);
2002 CAMLreturn0;
2005 SETOPT_STRING( COOKIEJAR)
2006 SETOPT_STRING( SSL_CIPHER_LIST)
2008 static void handle_HTTP_VERSION(Connection *conn, value option)
2010 CAMLparam1(option);
2011 CURLcode result = CURLE_OK;
2013 switch (Long_val(option))
2015 case 0: /* HTTP_VERSION_NONE */
2016 result = curl_easy_setopt(conn->connection,
2017 CURLOPT_HTTP_VERSION,
2018 CURL_HTTP_VERSION_NONE);
2019 break;
2021 case 1: /* HTTP_VERSION_1_0 */
2022 result = curl_easy_setopt(conn->connection,
2023 CURLOPT_HTTP_VERSION,
2024 CURL_HTTP_VERSION_1_0);
2025 break;
2027 case 2: /* HTTP_VERSION_1_1 */
2028 result = curl_easy_setopt(conn->connection,
2029 CURLOPT_HTTP_VERSION,
2030 CURL_HTTP_VERSION_1_1);
2031 break;
2033 default:
2034 failwith("Invalid HTTP_VERSION Option");
2035 break;
2038 if (result != CURLE_OK)
2039 raiseError(conn, result);
2041 CAMLreturn0;
2044 SETOPT_BOOL( FTP_USE_EPSV)
2045 SETOPT_LONG( DNS_CACHE_TIMEOUT)
2046 SETOPT_BOOL( DNS_USE_GLOBAL_CACHE)
2048 #if HAVE_DECL_CURLOPT_PRIVATE
2049 SETOPT_STRING( PRIVATE)
2050 #endif
2052 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2053 SETOPT_SLIST( HTTP200ALIASES)
2054 #endif
2056 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2057 SETOPT_BOOL( UNRESTRICTED_AUTH)
2058 #endif
2060 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2061 SETOPT_BOOL( FTP_USE_EPRT)
2062 #endif
2064 #if HAVE_DECL_CURLOPT_HTTPAUTH
2065 static void handle_HTTPAUTH(Connection *conn, value option)
2067 CAMLparam1(option);
2068 CAMLlocal1(listIter);
2069 CURLcode result = CURLE_OK;
2070 long auth = CURLAUTH_NONE;
2072 listIter = option;
2074 while (!Is_long(listIter))
2076 switch (Long_val(Field(listIter, 0)))
2078 case 0: /* CURLAUTH_BASIC */
2079 auth |= CURLAUTH_BASIC;
2080 break;
2082 case 1: /* CURLAUTH_DIGEST */
2083 auth |= CURLAUTH_DIGEST;
2084 break;
2086 case 2: /* CURLAUTH_GSSNEGOTIATE */
2087 auth |= CURLAUTH_GSSNEGOTIATE;
2088 break;
2090 case 3: /* CURLAUTH_NTLM */
2091 auth |= CURLAUTH_NTLM;
2092 break;
2094 case 4: /* CURLAUTH_ANY */
2095 auth |= CURLAUTH_ANY;
2096 break;
2098 case 5: /* CURLAUTH_ANYSAFE */
2099 auth |= CURLAUTH_ANYSAFE;
2100 break;
2102 default:
2103 failwith("Invalid HTTPAUTH Value");
2104 break;
2107 listIter = Field(listIter, 1);
2110 result = curl_easy_setopt(conn->connection,
2111 CURLOPT_HTTPAUTH,
2112 auth);
2114 if (result != CURLE_OK)
2115 raiseError(conn, result);
2117 CAMLreturn0;
2119 #endif
2121 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2122 SETOPT_BOOL( FTP_CREATE_MISSING_DIRS)
2123 #endif
2125 #if HAVE_DECL_CURLOPT_PROXYAUTH
2126 static void handle_PROXYAUTH(Connection *conn, value option)
2128 CAMLparam1(option);
2129 CAMLlocal1(listIter);
2130 CURLcode result = CURLE_OK;
2131 long auth = CURLAUTH_NONE;
2133 listIter = option;
2135 while (!Is_long(listIter))
2137 switch (Long_val(Field(listIter, 0)))
2139 case 0: /* CURLAUTH_BASIC */
2140 auth |= CURLAUTH_BASIC;
2141 break;
2143 case 1: /* CURLAUTH_DIGEST */
2144 auth |= CURLAUTH_DIGEST;
2145 break;
2147 case 2: /* CURLAUTH_GSSNEGOTIATE */
2148 auth |= CURLAUTH_GSSNEGOTIATE;
2149 break;
2151 case 3: /* CURLAUTH_NTLM */
2152 auth |= CURLAUTH_NTLM;
2153 break;
2155 case 4: /* CURLAUTH_ANY */
2156 auth |= CURLAUTH_ANY;
2157 break;
2159 case 5: /* CURLAUTH_ANYSAFE */
2160 auth |= CURLAUTH_ANYSAFE;
2161 break;
2163 default:
2164 failwith("Invalid HTTPAUTH Value");
2165 break;
2168 listIter = Field(listIter, 1);
2171 result = curl_easy_setopt(conn->connection,
2172 CURLOPT_PROXYAUTH,
2173 auth);
2175 if (result != CURLE_OK)
2176 raiseError(conn, result);
2178 CAMLreturn0;
2180 #endif
2182 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2183 SETOPT_LONG( FTP_RESPONSE_TIMEOUT)
2184 #endif
2186 #if HAVE_DECL_CURLOPT_IPRESOLVE
2187 static void handle_IPRESOLVE(Connection *conn, value option)
2189 CAMLparam1(option);
2190 CURLcode result = CURLE_OK;
2192 switch (Long_val(option))
2194 case 0: /* CURL_IPRESOLVE_WHATEVER */
2195 result = curl_easy_setopt(conn->connection,
2196 CURLOPT_IPRESOLVE,
2197 CURL_IPRESOLVE_WHATEVER);
2198 break;
2200 case 1: /* CURL_IPRESOLVE_V4 */
2201 result = curl_easy_setopt(conn->connection,
2202 CURLOPT_IPRESOLVE,
2203 CURL_IPRESOLVE_V4);
2204 break;
2206 case 2: /* CURL_IPRESOLVE_V6 */
2207 result = curl_easy_setopt(conn->connection,
2208 CURLOPT_IPRESOLVE,
2209 CURL_IPRESOLVE_V6);
2210 break;
2212 default:
2213 failwith("Invalid IPRESOLVE Value");
2214 break;
2217 if (result != CURLE_OK)
2218 raiseError(conn, result);
2220 CAMLreturn0;
2222 #endif
2224 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2225 SETOPT_VAL( MAXFILESIZE, Int32_val)
2226 #endif
2228 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2229 SETOPT_INT64( INFILESIZE_LARGE)
2230 #endif
2232 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2233 SETOPT_INT64( RESUME_FROM_LARGE)
2234 #endif
2236 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2237 SETOPT_INT64( MAXFILESIZE_LARGE)
2238 #endif
2240 #if HAVE_DECL_CURLOPT_NETRC_FILE
2241 SETOPT_STRING( NETRC_FILE)
2242 #endif
2244 #if HAVE_DECL_CURLOPT_FTP_SSL
2245 static void handle_FTP_SSL(Connection *conn, value option)
2247 CAMLparam1(option);
2248 CURLcode result = CURLE_OK;
2250 switch (Long_val(option))
2252 case 0: /* CURLFTPSSL_NONE */
2253 result = curl_easy_setopt(conn->connection,
2254 CURLOPT_FTP_SSL,
2255 CURLFTPSSL_NONE);
2256 break;
2258 case 1: /* CURLFTPSSL_TRY */
2259 result = curl_easy_setopt(conn->connection,
2260 CURLOPT_FTP_SSL,
2261 CURLFTPSSL_TRY);
2262 break;
2264 case 2: /* CURLFTPSSL_CONTROL */
2265 result = curl_easy_setopt(conn->connection,
2266 CURLOPT_FTP_SSL,
2267 CURLFTPSSL_CONTROL);
2268 break;
2270 case 3: /* CURLFTPSSL_ALL */
2271 result = curl_easy_setopt(conn->connection,
2272 CURLOPT_FTP_SSL,
2273 CURLFTPSSL_ALL);
2274 break;
2276 default:
2277 failwith("Invalid FTP_SSL Value");
2278 break;
2281 if (result != CURLE_OK)
2282 raiseError(conn, result);
2284 CAMLreturn0;
2286 #endif
2288 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
2289 SETOPT_INT64( POSTFIELDSIZE_LARGE)
2290 #endif
2292 #if HAVE_DECL_CURLOPT_TCP_NODELAY
2293 /* not using SETOPT_BOOL here because of TCP_NODELAY defined in winsock.h */
2294 SETOPT_VAL_( handle_TCP_NODELAY, CURLOPT_TCP_NODELAY, Bool_val)
2295 #endif
2297 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
2298 static void handle_FTPSSLAUTH(Connection *conn, value option)
2300 CAMLparam1(option);
2301 CURLcode result = CURLE_OK;
2303 switch (Long_val(option))
2305 case 0: /* CURLFTPAUTH_DEFAULT */
2306 result = curl_easy_setopt(conn->connection,
2307 CURLOPT_FTPSSLAUTH,
2308 CURLFTPAUTH_DEFAULT);
2309 break;
2311 case 1: /* CURLFTPAUTH_SSL */
2312 result = curl_easy_setopt(conn->connection,
2313 CURLOPT_FTPSSLAUTH,
2314 CURLFTPAUTH_SSL);
2315 break;
2317 case 2: /* CURLFTPAUTH_TLS */
2318 result = curl_easy_setopt(conn->connection,
2319 CURLOPT_FTPSSLAUTH,
2320 CURLFTPAUTH_TLS);
2321 break;
2323 default:
2324 failwith("Invalid FTPSSLAUTH value");
2325 break;
2328 if (result != CURLE_OK)
2329 raiseError(conn, result);
2331 CAMLreturn0;
2333 #endif
2335 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
2336 SETOPT_STRING( FTP_ACCOUNT)
2337 #endif
2339 #if HAVE_DECL_CURLOPT_COOKIELIST
2340 SETOPT_STRING( COOKIELIST)
2341 #endif
2343 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
2344 SETOPT_BOOL( IGNORE_CONTENT_LENGTH)
2345 #endif
2347 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
2348 SETOPT_BOOL( FTP_SKIP_PASV_IP)
2349 #endif
2351 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
2352 static void handle_FTP_FILEMETHOD(Connection *conn, value option)
2354 CAMLparam1(option);
2355 CURLcode result = CURLE_OK;
2357 switch (Long_val(option))
2359 case 0: /* CURLFTPMETHOD_DEFAULT */
2360 result = curl_easy_setopt(conn->connection,
2361 CURLOPT_FTP_FILEMETHOD,
2362 CURLFTPMETHOD_DEFAULT);
2363 break;
2365 case 1: /* CURLFTMETHOD_MULTICWD */
2366 result = curl_easy_setopt(conn->connection,
2367 CURLOPT_FTP_FILEMETHOD,
2368 CURLFTPMETHOD_MULTICWD);
2369 break;
2371 case 2: /* CURLFTPMETHOD_NOCWD */
2372 result = curl_easy_setopt(conn->connection,
2373 CURLOPT_FTP_FILEMETHOD,
2374 CURLFTPMETHOD_NOCWD);
2375 break;
2377 case 3: /* CURLFTPMETHOD_SINGLECWD */
2378 result = curl_easy_setopt(conn->connection,
2379 CURLOPT_FTP_FILEMETHOD,
2380 CURLFTPMETHOD_SINGLECWD);
2382 default:
2383 failwith("Invalid FTP_FILEMETHOD value");
2384 break;
2387 if (result != CURLE_OK)
2388 raiseError(conn, result);
2390 CAMLreturn0;
2392 #endif
2394 #if HAVE_DECL_CURLOPT_LOCALPORT
2395 SETOPT_LONG( LOCALPORT)
2396 #endif
2398 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
2399 SETOPT_LONG( LOCALPORTRANGE)
2400 #endif
2402 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
2403 SETOPT_BOOL( CONNECT_ONLY)
2404 #endif
2406 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
2407 SETOPT_INT64( MAX_SEND_SPEED_LARGE)
2408 #endif
2410 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
2411 SETOPT_INT64( MAX_RECV_SPEED_LARGE)
2412 #endif
2414 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
2415 SETOPT_STRING( FTP_ALTERNATIVE_TO_USER)
2416 #endif
2418 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
2419 SETOPT_BOOL( SSL_SESSIONID_CACHE)
2420 #endif
2422 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
2423 static void handle_SSH_AUTH_TYPES(Connection *conn, value option)
2425 CAMLparam1(option);
2426 CAMLlocal1(listIter);
2427 CURLcode result = CURLE_OK;
2428 long authTypes = CURLSSH_AUTH_NONE;
2430 listIter = option;
2432 while (!Is_long(listIter))
2434 switch (Long_val(Field(listIter, 0)))
2436 case 0: /* CURLSSH_AUTH_ANY */
2437 authTypes |= CURLSSH_AUTH_ANY;
2438 break;
2440 case 1: /* CURLSSH_AUTH_PUBLICKEY */
2441 authTypes |= CURLSSH_AUTH_PUBLICKEY;
2442 break;
2444 case 2: /* CURLSSH_AUTH_PASSWORD */
2445 authTypes |= CURLSSH_AUTH_PASSWORD;
2446 break;
2448 case 3: /* CURLSSH_AUTH_HOST */
2449 authTypes |= CURLSSH_AUTH_HOST;
2450 break;
2452 case 4: /* CURLSSH_AUTH_KEYBOARD */
2453 authTypes |= CURLSSH_AUTH_KEYBOARD;
2454 break;
2456 default:
2457 failwith("Invalid CURLSSH_AUTH_TYPES Value");
2458 break;
2461 listIter = Field(listIter, 1);
2464 result = curl_easy_setopt(conn->connection,
2465 CURLOPT_SSH_AUTH_TYPES,
2466 authTypes);
2468 if (result != CURLE_OK)
2469 raiseError(conn, result);
2471 CAMLreturn0;
2473 #endif
2475 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
2476 SETOPT_STRING( SSH_PUBLIC_KEYFILE)
2477 #endif
2479 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
2480 SETOPT_STRING( SSH_PRIVATE_KEYFILE)
2481 #endif
2483 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
2484 static void handle_FTP_SSL_CCC(Connection *conn, value option)
2486 CAMLparam1(option);
2487 CURLcode result = CURLE_OK;
2489 switch (Long_val(option))
2491 case 0: /* CURLFTPSSL_CCC_NONE */
2492 result = curl_easy_setopt(conn->connection,
2493 CURLOPT_FTP_SSL_CCC,
2494 CURLFTPSSL_CCC_NONE);
2495 break;
2497 case 1: /* CURLFTPSSL_CCC_PASSIVE */
2498 result = curl_easy_setopt(conn->connection,
2499 CURLOPT_FTP_SSL_CCC,
2500 CURLFTPSSL_CCC_PASSIVE);
2501 break;
2503 case 2: /* CURLFTPSSL_CCC_ACTIVE */
2504 result = curl_easy_setopt(conn->connection,
2505 CURLOPT_FTP_SSL_CCC,
2506 CURLFTPSSL_CCC_ACTIVE);
2507 break;
2509 default:
2510 failwith("Invalid FTPSSL_CCC value");
2511 break;
2514 if (result != CURLE_OK)
2515 raiseError(conn, result);
2517 CAMLreturn0;
2519 #endif
2521 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
2522 SETOPT_LONG( TIMEOUT_MS)
2523 #endif
2525 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
2526 SETOPT_LONG( CONNECTTIMEOUT_MS)
2527 #endif
2529 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
2530 SETOPT_BOOL( HTTP_TRANSFER_DECODING)
2531 #endif
2533 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
2534 SETOPT_BOOL( HTTP_CONTENT_DECODING)
2535 #endif
2537 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
2538 SETOPT_LONG( NEW_FILE_PERMS)
2539 #endif
2541 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
2542 SETOPT_LONG( NEW_DIRECTORY_PERMS)
2543 #endif
2545 #if HAVE_DECL_CURLOPT_POST301
2546 SETOPT_BOOL( POST301)
2547 #endif
2549 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
2550 SETOPT_STRING( SSH_HOST_PUBLIC_KEY_MD5)
2551 #endif
2553 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
2554 SETOPT_STRING( COPYPOSTFIELDS)
2555 #endif
2557 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
2558 SETOPT_BOOL( PROXY_TRANSFER_MODE)
2559 #endif
2561 #if HAVE_DECL_CURLOPT_AUTOREFERER
2562 SETOPT_BOOL( AUTOREFERER)
2563 #endif
2565 #if HAVE_DECL_CURLOPT_PROXYTYPE
2566 static void handle_PROXYTYPE(Connection *conn, value option)
2568 CAMLparam1(option);
2569 CURLcode result = CURLE_OK;
2570 long proxy_type;
2572 switch (Long_val(option))
2574 case 0: proxy_type = CURLPROXY_HTTP; break;
2575 case 1: proxy_type = CURLPROXY_HTTP_1_0; break;
2576 case 2: proxy_type = CURLPROXY_SOCKS4; break;
2577 case 3: proxy_type = CURLPROXY_SOCKS5; break;
2578 case 4: proxy_type = CURLPROXY_SOCKS4A; break;
2579 case 5: proxy_type = CURLPROXY_SOCKS5_HOSTNAME; break;
2580 default:
2581 failwith("Invalid curl proxy type");
2584 result = curl_easy_setopt(conn->connection,
2585 CURLOPT_PROXYTYPE,
2586 proxy_type);
2588 if (result != CURLE_OK)
2589 raiseError(conn, result);
2591 CAMLreturn0;
2593 #endif
2595 #if HAVE_DECL_CURLOPT_PROTOCOLS || HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
2597 long protoMap[] =
2599 CURLPROTO_ALL,
2600 CURLPROTO_HTTP, CURLPROTO_HTTPS, CURLPROTO_FTP, CURLPROTO_FTPS, CURLPROTO_SCP, CURLPROTO_SFTP,
2601 CURLPROTO_TELNET, CURLPROTO_LDAP, CURLPROTO_LDAPS, CURLPROTO_DICT, CURLPROTO_FILE, CURLPROTO_TFTP,
2602 /* factor out with autoconf? */
2603 #if defined(CURLPROTO_IMAP)
2604 CURLPROTO_IMAP,
2605 #else
2607 #endif
2608 #if defined(CURLPROTO_IMAPS)
2609 CURLPROTO_IMAPS,
2610 #else
2612 #endif
2613 #if defined(CURLPROTO_POP3)
2614 CURLPROTO_POP3,
2615 #else
2617 #endif
2618 #if defined(CURLPROTO_POP3S)
2619 CURLPROTO_POP3S,
2620 #else
2622 #endif
2623 #if defined(CURLPROTO_SMTP)
2624 CURLPROTO_SMTP,
2625 #else
2627 #endif
2628 #if defined(CURLPROTO_SMTPS)
2629 CURLPROTO_SMTPS,
2630 #else
2632 #endif
2633 #if defined(CURLPROTO_RTSP)
2634 CURLPROTO_RTSP,
2635 #else
2637 #endif
2638 #if defined(CURLPROTO_RTMP)
2639 CURLPROTO_RTMP,
2640 #else
2642 #endif
2643 #if defined(CURLPROTO_RTMPT)
2644 CURLPROTO_RTMPT,
2645 #else
2647 #endif
2648 #if defined(CURLPROTO_RTMPE)
2649 CURLPROTO_RTMPE,
2650 #else
2652 #endif
2653 #if defined(CURLPROTO_RTMPTE)
2654 CURLPROTO_RTMPTE,
2655 #else
2657 #endif
2658 #if defined(CURLPROTO_RTMPS)
2659 CURLPROTO_RTMPS,
2660 #else
2662 #endif
2663 #if defined(CURLPROTO_RTMPTS)
2664 CURLPROTO_RTMPTS,
2665 #else
2667 #endif
2668 #if defined(CURLPROTO_GOPHER)
2669 CURLPROTO_GOPHER,
2670 #else
2672 #endif
2675 static void handle_PROTOCOLSOPTION(CURLoption curlopt, Connection *conn, value option)
2677 CAMLparam1(option);
2678 CURLcode result = CURLE_OK;
2679 long protocols = 0;
2680 int index;
2682 while (Val_emptylist != option)
2684 index = Int_val(Field(option, 0));
2685 if ((index < 0) || ((size_t)index >= sizeof(protoMap) / sizeof(protoMap[0])))
2686 failwith("Invalid curl protocol");
2688 protocols = protocols | protoMap[index];
2690 option = Field(option, 1);
2693 result = curl_easy_setopt(conn->connection,
2694 curlopt,
2695 protocols);
2697 if (result != CURLE_OK)
2698 raiseError(conn, result);
2700 CAMLreturn0;
2702 #endif
2704 #if HAVE_DECL_CURLOPT_PROTOCOLS
2705 static void handle_PROTOCOLS(Connection *conn, value option)
2707 handle_PROTOCOLSOPTION(CURLOPT_PROTOCOLS, conn, option);
2709 #endif
2711 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
2712 static void handle_REDIR_PROTOCOLS(Connection *conn, value option)
2714 handle_PROTOCOLSOPTION(CURLOPT_REDIR_PROTOCOLS, conn, option);
2716 #endif
2718 #if HAVE_DECL_CURLOPT_RESOLVE
2719 SETOPT_SLIST( RESOLVE)
2720 #endif
2722 #if HAVE_DECL_CURLOPT_DNS_SERVERS
2723 SETOPT_STRING( DNS_SERVERS)
2724 #endif
2726 #if HAVE_DECL_CURLOPT_MAIL_FROM
2727 SETOPT_STRING( MAIL_FROM)
2728 #endif
2730 #if HAVE_DECL_CURLOPT_MAIL_RCPT
2731 SETOPT_SLIST( MAIL_RCPT)
2732 #endif
2735 ** curl_easy_setopt helper function
2738 #define MAP(name) { handle_ ## name, "CURLOPT_"#name, Ocaml_##name }
2739 #define MAP_NO(name) { NULL, "CURLOPT_"#name , Ocaml_##name }
2740 #define IMM(name) { handle_ ## name, "CURLOPT_"#name, -1 }
2741 #define IMM_NO(name) { NULL, "CURLOPT_"#name , -1 }
2743 CURLOptionMapping implementedOptionMap[] =
2745 MAP(WRITEFUNCTION),
2746 MAP(READFUNCTION),
2747 IMM(INFILESIZE),
2748 MAP(URL),
2749 MAP(PROXY),
2750 IMM(PROXYPORT),
2751 IMM(HTTPPROXYTUNNEL),
2752 IMM(VERBOSE),
2753 IMM(HEADER),
2754 IMM(NOPROGRESS),
2755 #if HAVE_DECL_CURLOPT_NOSIGNAL
2756 IMM(NOSIGNAL),
2757 #else
2758 IMM_NO(NOSIGNAL),
2759 #endif
2760 IMM(NOBODY),
2761 IMM(FAILONERROR),
2762 IMM(UPLOAD),
2763 IMM(POST),
2764 IMM(FTPLISTONLY),
2765 IMM(FTPAPPEND),
2766 IMM(NETRC),
2767 #if HAVE_DECL_CURLOPT_ENCODING
2768 IMM(ENCODING),
2769 #else
2770 IMM_NO(ENCODING),
2771 #endif
2772 IMM(FOLLOWLOCATION),
2773 IMM(TRANSFERTEXT),
2774 IMM(PUT),
2775 MAP(USERPWD),
2776 MAP(PROXYUSERPWD),
2777 MAP(RANGE),
2778 IMM(ERRORBUFFER), /* mutable buffer, as output value, do not duplicate */
2779 IMM(TIMEOUT),
2780 MAP(POSTFIELDS),
2781 IMM(POSTFIELDSIZE),
2782 MAP(REFERER),
2783 MAP(USERAGENT),
2784 MAP(FTPPORT),
2785 IMM(LOW_SPEED_LIMIT),
2786 IMM(LOW_SPEED_TIME),
2787 IMM(RESUME_FROM),
2788 MAP(COOKIE),
2789 MAP(HTTPHEADER),
2790 MAP(HTTPPOST),
2791 MAP(SSLCERT),
2792 MAP(SSLCERTTYPE),
2793 MAP(SSLCERTPASSWD),
2794 MAP(SSLKEY),
2795 MAP(SSLKEYTYPE),
2796 MAP(SSLKEYPASSWD),
2797 MAP(SSLENGINE),
2798 IMM(SSLENGINE_DEFAULT),
2799 IMM(CRLF),
2800 MAP(QUOTE),
2801 MAP(POSTQUOTE),
2802 MAP(HEADERFUNCTION),
2803 MAP(COOKIEFILE),
2804 IMM(SSLVERSION),
2805 IMM(TIMECONDITION),
2806 IMM(TIMEVALUE),
2807 MAP(CUSTOMREQUEST),
2808 MAP(INTERFACE),
2809 IMM(KRB4LEVEL),
2810 MAP(PROGRESSFUNCTION),
2811 IMM(SSL_VERIFYPEER),
2812 MAP(CAINFO),
2813 MAP(CAPATH),
2814 IMM(FILETIME),
2815 IMM(MAXREDIRS),
2816 IMM(MAXCONNECTS),
2817 IMM(CLOSEPOLICY),
2818 IMM(FRESH_CONNECT),
2819 IMM(FORBID_REUSE),
2820 MAP(RANDOM_FILE),
2821 MAP(EGDSOCKET),
2822 IMM(CONNECTTIMEOUT),
2823 IMM(HTTPGET),
2824 IMM(SSL_VERIFYHOST),
2825 MAP(COOKIEJAR),
2826 MAP(SSL_CIPHER_LIST),
2827 IMM(HTTP_VERSION),
2828 IMM(FTP_USE_EPSV),
2829 IMM(DNS_CACHE_TIMEOUT),
2830 IMM(DNS_USE_GLOBAL_CACHE),
2831 MAP(DEBUGFUNCTION),
2832 #if HAVE_DECL_CURLOPT_PRIVATE
2833 MAP(PRIVATE),
2834 #else
2835 MAP_NO(PRIVATE),
2836 #endif
2837 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2838 MAP(HTTP200ALIASES),
2839 #else
2840 MAP_NO(HTTP200ALIASES),
2841 #endif
2842 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2843 IMM(UNRESTRICTED_AUTH),
2844 #else
2845 IMM_NO(UNRESTRICTED_AUTH),
2846 #endif
2847 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2848 IMM(FTP_USE_EPRT),
2849 #else
2850 IMM_NO(FTP_USE_EPRT),
2851 #endif
2852 #if HAVE_DECL_CURLOPT_HTTPAUTH
2853 IMM(HTTPAUTH),
2854 #else
2855 IMM_NO(HTTPAUTH),
2856 #endif
2857 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2858 IMM(FTP_CREATE_MISSING_DIRS),
2859 #else
2860 IMM_NO(FTP_CREATE_MISSING_DIRS),
2861 #endif
2862 #if HAVE_DECL_CURLOPT_PROXYAUTH
2863 IMM(PROXYAUTH),
2864 #else
2865 IMM_NO(PROXYAUTH),
2866 #endif
2867 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2868 IMM(FTP_RESPONSE_TIMEOUT),
2869 #else
2870 IMM_NO(FTP_RESPONSE_TIMEOUT),
2871 #endif
2872 #if HAVE_DECL_CURLOPT_IPRESOLVE
2873 IMM(IPRESOLVE),
2874 #else
2875 IMM_NO(IPRESOLVE),
2876 #endif
2877 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2878 IMM(MAXFILESIZE),
2879 #else
2880 IMM_NO(MAXFILESIZE),
2881 #endif
2882 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2883 IMM(INFILESIZE_LARGE),
2884 #else
2885 IMM_NO(INFILESIZE_LARGE),
2886 #endif
2887 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2888 IMM(RESUME_FROM_LARGE),
2889 #else
2890 IMM_NO(RESUME_FROM_LARGE),
2891 #endif
2892 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2893 IMM(MAXFILESIZE_LARGE),
2894 #else
2895 IMM_NO(MAXFILESIZE_LARGE),
2896 #endif
2897 #if HAVE_DECL_CURLOPT_NETRC_FILE
2898 MAP(NETRC_FILE),
2899 #else
2900 MAP_NO(NETRC_FILE),
2901 #endif
2902 #if HAVE_DECL_CURLOPT_FTP_SSL
2903 IMM(FTP_SSL),
2904 #else
2905 IMM_NO(FTP_SSL),
2906 #endif
2907 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
2908 IMM(POSTFIELDSIZE_LARGE),
2909 #else
2910 IMM_NO(POSTFIELDSIZE_LARGE),
2911 #endif
2912 #if HAVE_DECL_CURLOPT_TCP_NODELAY
2913 IMM(TCP_NODELAY),
2914 #else
2915 IMM_NO(TCP_NODELAY),
2916 #endif
2917 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
2918 IMM(FTPSSLAUTH),
2919 #else
2920 IMM_NO(FTPSSLAUTH),
2921 #endif
2922 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
2923 MAP(IOCTLFUNCTION),
2924 #else
2925 MAP_NO(IOCTLFUNCTION),
2926 #endif
2927 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
2928 MAP(FTP_ACCOUNT),
2929 #else
2930 MAP_NO(FTP_ACCOUNT),
2931 #endif
2932 #if HAVE_DECL_CURLOPT_COOKIELIST
2933 MAP(COOKIELIST),
2934 #else
2935 MAP_NO(COOKIELIST),
2936 #endif
2937 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
2938 IMM(IGNORE_CONTENT_LENGTH),
2939 #else
2940 IMM_NO(IGNORE_CONTENT_LENGTH),
2941 #endif
2942 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
2943 IMM(FTP_SKIP_PASV_IP),
2944 #else
2945 IMM_NO(FTP_SKIP_PASV_IP),
2946 #endif
2947 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
2948 IMM(FTP_FILEMETHOD),
2949 #else
2950 IMM_NO(FTP_FILEMETHOD),
2951 #endif
2952 #if HAVE_DECL_CURLOPT_LOCALPORT
2953 IMM(LOCALPORT),
2954 #else
2955 IMM_NO(LOCALPORT),
2956 #endif
2957 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
2958 IMM(LOCALPORTRANGE),
2959 #else
2960 IMM_NO(LOCALPORTRANGE),
2961 #endif
2962 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
2963 IMM(CONNECT_ONLY),
2964 #else
2965 IMM_NO(CONNECT_ONLY),
2966 #endif
2967 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
2968 IMM(MAX_SEND_SPEED_LARGE),
2969 #else
2970 IMM_NO(MAX_SEND_SPEED_LARGE),
2971 #endif
2972 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
2973 IMM(MAX_RECV_SPEED_LARGE),
2974 #else
2975 IMM_NO(MAX_RECV_SPEED_LARGE),
2976 #endif
2977 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
2978 MAP(FTP_ALTERNATIVE_TO_USER),
2979 #else
2980 MAP_NO(FTP_ALTERNATIVE_TO_USER),
2981 #endif
2982 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
2983 IMM(SSL_SESSIONID_CACHE),
2984 #else
2985 IMM_NO(SSL_SESSIONID_CACHE),
2986 #endif
2987 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
2988 IMM(SSH_AUTH_TYPES),
2989 #else
2990 IMM_NO(SSH_AUTH_TYPES),
2991 #endif
2992 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
2993 MAP(SSH_PUBLIC_KEYFILE),
2994 #else
2995 MAP_NO(SSH_PUBLIC_KEYFILE),
2996 #endif
2997 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
2998 MAP(SSH_PRIVATE_KEYFILE),
2999 #else
3000 MAP_NO(SSH_PRIVATE_KEYFILE),
3001 #endif
3002 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
3003 IMM(FTP_SSL_CCC),
3004 #else
3005 IMM_NO(FTP_SSL_CCC),
3006 #endif
3007 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
3008 IMM(TIMEOUT_MS),
3009 #else
3010 IMM_NO(TIMEOUT_MS),
3011 #endif
3012 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
3013 IMM(CONNECTTIMEOUT_MS),
3014 #else
3015 IMM_NO(CONNECTTIMEOUT_MS),
3016 #endif
3017 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
3018 IMM(HTTP_TRANSFER_DECODING),
3019 #else
3020 IMM_NO(HTTP_TRANSFER_DECODING),
3021 #endif
3022 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
3023 IMM(HTTP_CONTENT_DECODING),
3024 #else
3025 IMM_NO(HTTP_CONTENT_DECODING),
3026 #endif
3027 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
3028 IMM(NEW_FILE_PERMS),
3029 #else
3030 IMM_NO(NEW_FILE_PERMS),
3031 #endif
3032 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
3033 IMM(NEW_DIRECTORY_PERMS),
3034 #else
3035 IMM_NO(NEW_DIRECTORY_PERMS),
3036 #endif
3037 #if HAVE_DECL_CURLOPT_POST301
3038 IMM(POST301),
3039 #else
3040 IMM_NO(POST301),
3041 #endif
3042 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
3043 MAP(SSH_HOST_PUBLIC_KEY_MD5),
3044 #else
3045 MAP_NO(SSH_HOST_PUBLIC_KEY_MD5),
3046 #endif
3047 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
3048 MAP(COPYPOSTFIELDS),
3049 #else
3050 MAP_NO(COPYPOSTFIELDS),
3051 #endif
3052 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
3053 IMM(PROXY_TRANSFER_MODE),
3054 #else
3055 IMM_NO(PROXY_TRANSFER_MODE),
3056 #endif
3057 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
3058 MAP(SEEKFUNCTION),
3059 #else
3060 MAP_NO(SEEKFUNCTION),
3061 #endif
3062 #if HAVE_DECL_CURLOPT_AUTOREFERER
3063 IMM(AUTOREFERER),
3064 #else
3065 IMM_NO(AUTOREFERER),
3066 #endif
3067 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
3068 MAP(OPENSOCKETFUNCTION),
3069 #else
3070 MAP_NO(OPENSOCKETFUNCTION),
3071 #endif
3072 #if HAVE_DECL_CURLOPT_PROXYTYPE
3073 IMM(PROXYTYPE),
3074 #else
3075 IMM_NO(PROXYTYPE),
3076 #endif
3077 #if HAVE_DECL_CURLOPT_PROTOCOLS
3078 IMM(PROTOCOLS),
3079 #else
3080 IMM_NO(PROTOCOLS),
3081 #endif
3082 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3083 IMM(REDIR_PROTOCOLS),
3084 #else
3085 IMM_NO(REDIR_PROTOCOLS),
3086 #endif
3087 #if HAVE_DECL_CURLOPT_RESOLVE
3088 MAP(RESOLVE),
3089 #else
3090 MAP_NO(RESOLVE),
3091 #endif
3092 #if HAVE_DECL_CURLOPT_DNS_SERVERS
3093 MAP(DNS_SERVERS),
3094 #else
3095 MAP_NO(DNS_SERVERS),
3096 #endif
3097 #if HAVE_DECL_CURLOPT_MAIL_FROM
3098 MAP(MAIL_FROM),
3099 #else
3100 MAP_NO(MAIL_FROM),
3101 #endif
3102 #if HAVE_DECL_CURLOPT_MAIL_RCPT
3103 MAP(MAIL_RCPT),
3104 #else
3105 MAP_NO(MAIL_RCPT),
3106 #endif
3109 static Connection *duplicateConnection(Connection *original)
3111 Connection *connection = NULL;
3112 CURL* h = NULL;
3113 size_t i = 0;
3114 CURLOptionMapping* this = NULL;
3116 caml_enter_blocking_section();
3117 h = curl_easy_duphandle(original->connection);
3118 caml_leave_blocking_section();
3120 connection = allocConnection(h);
3122 for (i = 0; i < sizeof(implementedOptionMap)/sizeof(CURLOptionMapping); i++)
3124 this = &implementedOptionMap[i];
3125 if (-1 == this->ocamlValue) continue;
3126 if (this->optionHandler && (Field(original->ocamlValues, this->ocamlValue) != Val_unit))
3128 this->optionHandler(connection, Field(original->ocamlValues, this->ocamlValue));
3132 return connection;
3135 CAMLprim value helper_curl_easy_setopt(value conn, value option)
3137 CAMLparam2(conn, option);
3138 CAMLlocal1(data);
3139 Connection *connection = Connection_val(conn);
3140 CURLOptionMapping* thisOption = NULL;
3141 static value* exception = NULL;
3143 checkConnection(connection);
3145 if (!Is_block(option))
3146 failwith("Not a block");
3148 if (Wosize_val(option) < 1)
3149 failwith("Insufficient data in block");
3151 data = Field(option, 0);
3153 if (Tag_val(option) < sizeof(implementedOptionMap)/sizeof(CURLOptionMapping))
3155 thisOption = &implementedOptionMap[Tag_val(option)];
3156 if (thisOption->optionHandler)
3157 thisOption->optionHandler(connection, data);
3158 else
3160 if (NULL == exception)
3162 exception = caml_named_value("Curl.NotImplemented");
3163 if (NULL == exception) caml_invalid_argument("Curl.NotImplemented");
3166 caml_raise_with_string(*exception, thisOption->name);
3169 else
3170 failwith("Invalid CURLOPT Option");
3172 CAMLreturn(Val_unit);
3176 ** curl_easy_perform helper function
3179 CAMLprim value helper_curl_easy_perform(value conn)
3181 CAMLparam1(conn);
3182 CURLcode result = CURLE_OK;
3183 Connection *connection = Connection_val(conn);
3185 checkConnection(connection);
3187 enter_blocking_section();
3188 result = curl_easy_perform(connection->connection);
3189 leave_blocking_section();
3191 if (result != CURLE_OK)
3192 raiseError(connection, result);
3194 CAMLreturn(Val_unit);
3198 ** curl_easy_cleanup helper function
3201 CAMLprim value helper_curl_easy_cleanup(value conn)
3203 CAMLparam1(conn);
3204 Connection *connection = Connection_val(conn);
3206 checkConnection(connection);
3208 removeConnection(connection, 0);
3210 CAMLreturn(Val_unit);
3214 ** curl_easy_duphandle helper function
3217 CAMLprim value helper_curl_easy_duphandle(value conn)
3219 CAMLparam1(conn);
3220 CAMLlocal1(result);
3221 Connection *connection = Connection_val(conn);
3223 checkConnection(connection);
3225 result = caml_curl_alloc(duplicateConnection(connection));
3227 CAMLreturn(result);
3231 ** curl_easy_getinfo helper function
3234 enum GetInfoResultType {
3235 StringValue, LongValue, DoubleValue, StringListValue
3238 value convertStringList(struct curl_slist *slist)
3240 CAMLparam0();
3241 CAMLlocal3(result, current, next);
3242 struct curl_slist *p = slist;
3244 result = Val_int(0);
3245 current = Val_int(0);
3246 next = Val_int(0);
3248 while (p != NULL)
3250 next = alloc_tuple(2);
3251 Store_field(next, 0, copy_string(p->data));
3252 Store_field(next, 1, Val_int(0));
3254 if (result == Val_int(0))
3255 result = next;
3257 if (current != Val_int(0))
3258 Store_field(current, 1, next);
3260 current = next;
3262 p = p->next;
3265 curl_slist_free_all(slist);
3267 CAMLreturn(result);
3270 CAMLprim value helper_curl_easy_getinfo(value conn, value option)
3272 CAMLparam2(conn, option);
3273 CAMLlocal1(result);
3274 CURLcode curlResult;
3275 Connection *connection = Connection_val(conn);
3276 enum GetInfoResultType resultType;
3277 char *strValue = NULL;
3278 double doubleValue;
3279 long longValue;
3280 struct curl_slist *stringListValue = NULL;
3282 checkConnection(connection);
3284 switch(Long_val(option))
3286 #if HAVE_DECL_CURLINFO_EFFECTIVE_URL
3287 case 0: /* CURLINFO_EFFECTIVE_URL */
3288 resultType = StringValue;
3290 curlResult = curl_easy_getinfo(connection->connection,
3291 CURLINFO_EFFECTIVE_URL,
3292 &strValue);
3293 break;
3294 #else
3295 #pragma message("libcurl does not provide CURLINFO_EFFECTIVE_URL")
3296 #endif
3298 #if HAVE_DECL_CURLINFO_RESPONSE_CODE || HAVE_DECL_CURLINFO_HTTP_CODE
3299 case 1: /* CURLINFO_HTTP_CODE */
3300 case 2: /* CURLINFO_RESPONSE_CODE */
3301 #if HAVE_DECL_CURLINFO_RESPONSE_CODE
3302 resultType = LongValue;
3304 curlResult = curl_easy_getinfo(connection->connection,
3305 CURLINFO_RESPONSE_CODE,
3306 &longValue);
3307 #else
3308 resultType = LongValue;
3310 curlResult = curl_easy_getinfo(connection->connection,
3311 CURLINFO_HTTP_CODE,
3312 &longValue);
3313 #endif
3314 break;
3315 #endif
3317 #if HAVE_DECL_CURLINFO_TOTAL_TIME
3318 case 3: /* CURLINFO_TOTAL_TIME */
3319 resultType = DoubleValue;
3321 curlResult = curl_easy_getinfo(connection->connection,
3322 CURLINFO_TOTAL_TIME,
3323 &doubleValue);
3324 break;
3325 #endif
3327 #if HAVE_DECL_CURLINFO_NAMELOOKUP_TIME
3328 case 4: /* CURLINFO_NAMELOOKUP_TIME */
3329 resultType = DoubleValue;
3331 curlResult = curl_easy_getinfo(connection->connection,
3332 CURLINFO_NAMELOOKUP_TIME,
3333 &doubleValue);
3334 break;
3335 #endif
3337 #if HAVE_DECL_CURLINFO_CONNECT_TIME
3338 case 5: /* CURLINFO_CONNECT_TIME */
3339 resultType = DoubleValue;
3341 curlResult = curl_easy_getinfo(connection->connection,
3342 CURLINFO_CONNECT_TIME,
3343 &doubleValue);
3344 break;
3345 #endif
3347 #if HAVE_DECL_CURLINFO_PRETRANSFER_TIME
3348 case 6: /* CURLINFO_PRETRANSFER_TIME */
3349 resultType = DoubleValue;
3351 curlResult = curl_easy_getinfo(connection->connection,
3352 CURLINFO_PRETRANSFER_TIME,
3353 &doubleValue);
3354 break;
3355 #endif
3357 #if HAVE_DECL_CURLINFO_SIZE_UPLOAD
3358 case 7: /* CURLINFO_SIZE_UPLOAD */
3359 resultType = DoubleValue;
3361 curlResult = curl_easy_getinfo(connection->connection,
3362 CURLINFO_SIZE_UPLOAD,
3363 &doubleValue);
3364 break;
3365 #endif
3367 #if HAVE_DECL_CURLINFO_SIZE_DOWNLOAD
3368 case 8: /* CURLINFO_SIZE_DOWNLOAD */
3369 resultType = DoubleValue;
3371 curlResult = curl_easy_getinfo(connection->connection,
3372 CURLINFO_SIZE_DOWNLOAD,
3373 &doubleValue);
3374 break;
3375 #endif
3377 #if HAVE_DECL_CURLINFO_SPEED_DOWNLOAD
3378 case 9: /* CURLINFO_SPEED_DOWNLOAD */
3379 resultType = DoubleValue;
3381 curlResult = curl_easy_getinfo(connection->connection,
3382 CURLINFO_SPEED_DOWNLOAD,
3383 &doubleValue);
3384 break;
3385 #endif
3387 #if HAVE_DECL_CURLINFO_SPEED_UPLOAD
3388 case 10: /* CURLINFO_SPEED_UPLOAD */
3389 resultType = DoubleValue;
3391 curlResult = curl_easy_getinfo(connection->connection,
3392 CURLINFO_SPEED_UPLOAD,
3393 &doubleValue);
3394 break;
3396 #endif
3398 #if HAVE_DECL_CURLINFO_HEADER_SIZE
3399 case 11: /* CURLINFO_HEADER_SIZE */
3400 resultType = LongValue;
3402 curlResult = curl_easy_getinfo(connection->connection,
3403 CURLINFO_HEADER_SIZE,
3404 &longValue);
3405 break;
3406 #endif
3408 #if HAVE_DECL_CURLINFO_REQUEST_SIZE
3409 case 12: /* CURLINFO_REQUEST_SIZE */
3410 resultType = LongValue;
3412 curlResult = curl_easy_getinfo(connection->connection,
3413 CURLINFO_REQUEST_SIZE,
3414 &longValue);
3415 break;
3416 #endif
3418 #if HAVE_DECL_CURLINFO_SSL_VERIFYRESULT
3419 case 13: /* CURLINFO_SSL_VERIFYRESULT */
3420 resultType = LongValue;
3422 curlResult = curl_easy_getinfo(connection->connection,
3423 CURLINFO_SSL_VERIFYRESULT,
3424 &longValue);
3425 break;
3426 #endif
3428 #if HAVE_DECL_CURLINFO_FILETIME
3429 case 14: /* CURLINFO_FILETIME */
3430 resultType = DoubleValue;
3432 curlResult = curl_easy_getinfo(connection->connection,
3433 CURLINFO_FILETIME,
3434 &longValue);
3436 doubleValue = longValue;
3437 break;
3438 #endif
3440 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_DOWNLOAD
3441 case 15: /* CURLINFO_CONTENT_LENGTH_DOWNLOAD */
3442 resultType = DoubleValue;
3444 curlResult = curl_easy_getinfo(connection->connection,
3445 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
3446 &doubleValue);
3447 break;
3448 #endif
3450 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_UPLOAD
3451 case 16: /* CURLINFO_CONTENT_LENGTH_UPLOAD */
3452 resultType = DoubleValue;
3454 curlResult = curl_easy_getinfo(connection->connection,
3455 CURLINFO_CONTENT_LENGTH_UPLOAD,
3456 &doubleValue);
3457 break;
3458 #endif
3460 #if HAVE_DECL_CURLINFO_STARTTRANSFER_TIME
3461 case 17: /* CURLINFO_STARTTRANSFER_TIME */
3462 resultType = DoubleValue;
3464 curlResult = curl_easy_getinfo(connection->connection,
3465 CURLINFO_STARTTRANSFER_TIME,
3466 &doubleValue);
3467 break;
3468 #endif
3470 #if HAVE_DECL_CURLINFO_CONTENT_TYPE
3471 case 18: /* CURLINFO_CONTENT_TYPE */
3472 resultType = StringValue;
3474 curlResult = curl_easy_getinfo(connection->connection,
3475 CURLINFO_CONTENT_TYPE,
3476 &strValue);
3477 break;
3478 #endif
3480 #if HAVE_DECL_CURLINFO_REDIRECT_TIME
3481 case 19: /* CURLINFO_REDIRECT_TIME */
3482 resultType = DoubleValue;
3484 curlResult = curl_easy_getinfo(connection->connection,
3485 CURLINFO_REDIRECT_TIME,
3486 &doubleValue);
3487 break;
3488 #endif
3490 #if HAVE_DECL_CURLINFO_REDIRECT_COUNT
3491 case 20: /* CURLINFO_REDIRECT_COUNT */
3492 resultType = LongValue;
3494 curlResult = curl_easy_getinfo(connection->connection,
3495 CURLINFO_REDIRECT_COUNT,
3496 &longValue);
3497 break;
3498 #endif
3500 #if HAVE_DECL_CURLINFO_PRIVATE
3501 case 21: /* CURLINFO_PRIVATE */
3502 resultType = StringValue;
3504 curlResult = curl_easy_getinfo(connection->connection,
3505 CURLINFO_PRIVATE,
3506 &strValue);
3507 break;
3508 #endif
3510 #if HAVE_DECL_CURLINFO_HTTP_CONNECTCODE
3511 case 22: /* CURLINFO_HTTP_CONNECTCODE */
3512 resultType = LongValue;
3514 curlResult = curl_easy_getinfo(connection->connection,
3515 CURLINFO_HTTP_CONNECTCODE,
3516 &longValue);
3517 break;
3518 #endif
3520 #if HAVE_DECL_CURLINFO_HTTPAUTH_AVAIL
3521 case 23: /* CURLINFO_HTTPAUTH_AVAIL */
3522 resultType = LongValue;
3524 curlResult = curl_easy_getinfo(connection->connection,
3525 CURLINFO_HTTPAUTH_AVAIL,
3526 &longValue);
3527 break;
3528 #endif
3530 #if HAVE_DECL_CURLINFO_PROXYAUTH_AVAIL
3531 case 24: /* CURLINFO_PROXYAUTH_AVAIL */
3532 resultType = LongValue;
3534 curlResult = curl_easy_getinfo(connection->connection,
3535 CURLINFO_PROXYAUTH_AVAIL,
3536 &longValue);
3537 break;
3538 #endif
3540 #if HAVE_DECL_CURLINFO_OS_ERRNO
3541 case 25: /* CURLINFO_OS_ERRNO */
3542 resultType = LongValue;
3544 curlResult = curl_easy_getinfo(connection->connection,
3545 CURLINFO_OS_ERRNO,
3546 &longValue);
3547 break;
3548 #endif
3550 #if HAVE_DECL_CURLINFO_NUM_CONNECTS
3551 case 26: /* CURLINFO_NUM_CONNECTS */
3552 resultType = LongValue;
3554 curlResult = curl_easy_getinfo(connection->connection,
3555 CURLINFO_NUM_CONNECTS,
3556 &longValue);
3557 break;
3558 #endif
3560 #if HAVE_DECL_CURLINFO_SSL_ENGINES
3561 case 27: /* CURLINFO_SSL_ENGINES */
3562 resultType = StringListValue;
3564 curlResult = curl_easy_getinfo(connection->connection,
3565 CURLINFO_SSL_ENGINES,
3566 &stringListValue);
3567 break;
3568 #endif
3570 #if HAVE_DECL_CURLINFO_COOKIELIST
3571 case 28: /* CURLINFO_COOKIELIST */
3572 resultType = StringListValue;
3574 curlResult = curl_easy_getinfo(connection->connection,
3575 CURLINFO_COOKIELIST,
3576 &stringListValue);
3577 break;
3578 #endif
3580 #if HAVE_DECL_CURLINFO_LASTSOCKET
3581 case 29: /* CURLINFO_LASTSOCKET */
3582 resultType = LongValue;
3584 curlResult = curl_easy_getinfo(connection->connection,
3585 CURLINFO_LASTSOCKET,
3586 &longValue);
3587 break;
3588 #endif
3590 #if HAVE_DECL_CURLINFO_FTP_ENTRY_PATH
3591 case 30: /* CURLINFO_FTP_ENTRY_PATH */
3592 resultType = StringValue;
3594 curlResult = curl_easy_getinfo(connection->connection,
3595 CURLINFO_FTP_ENTRY_PATH,
3596 &strValue);
3597 break;
3598 #endif
3600 #if HAVE_DECL_CURLINFO_REDIRECT_URL
3601 case 31: /* CURLINFO_REDIRECT_URL */
3602 resultType = StringValue;
3604 curlResult = curl_easy_getinfo(connection->connection,
3605 CURLINFO_REDIRECT_URL,
3606 &strValue);
3607 break;
3608 #else
3609 #pragma message("libcurl does not provide CURLINFO_REDIRECT_URL")
3610 #endif
3612 #if HAVE_DECL_CURLINFO_PRIMARY_IP
3613 case 32: /* CURLINFO_PRIMARY_IP */
3614 resultType = StringValue;
3616 curlResult = curl_easy_getinfo(connection->connection,
3617 CURLINFO_PRIMARY_IP,
3618 &strValue);
3619 break;
3620 #else
3621 #pragma message("libcurl does not provide CURLINFO_PRIMARY_IP")
3622 #endif
3624 #if HAVE_DECL_CURLINFO_LOCAL_IP
3625 case 33: /* CURLINFO_LOCAL_IP */
3626 resultType = StringValue;
3628 curlResult = curl_easy_getinfo(connection->connection,
3629 CURLINFO_LOCAL_IP,
3630 &strValue);
3631 break;
3632 #else
3633 #pragma message("libcurl does not provide CURLINFO_LOCAL_IP")
3634 #endif
3636 #if HAVE_DECL_CURLINFO_LOCAL_PORT
3637 case 34: /* CURLINFO_LOCAL_PORT */
3638 resultType = LongValue;
3640 curlResult = curl_easy_getinfo(connection->connection,
3641 CURLINFO_LOCAL_PORT,
3642 &longValue);
3643 break;
3644 #else
3645 #pragma message("libcurl does not provide CURLINFO_LOCAL_PORT")
3646 #endif
3648 #if HAVE_DECL_CURLINFO_CONDITION_UNMET
3649 case 35: /* CURLINFO_CONDITION_UNMET */
3650 resultType = LongValue;
3652 curlResult = curl_easy_getinfo(connection->connection,
3653 CURLINFO_CONDITION_UNMET,
3654 &longValue);
3655 break;
3656 #else
3657 #pragma message("libcurl does not provide CURLINFO_CONDITION_UNMET")
3658 #endif
3660 default:
3661 failwith("Invalid CURLINFO Option");
3662 break;
3665 if (curlResult != CURLE_OK)
3666 raiseError(connection, curlResult);
3668 switch (resultType)
3670 case StringValue:
3671 result = alloc(1, StringValue);
3672 Store_field(result, 0, copy_string(strValue?strValue:""));
3673 break;
3675 case LongValue:
3676 result = alloc(1, LongValue);
3677 Store_field(result, 0, Val_long(longValue));
3678 break;
3680 case DoubleValue:
3681 result = alloc(1, DoubleValue);
3682 Store_field(result, 0, copy_double(doubleValue));
3683 break;
3685 case StringListValue:
3686 result = alloc(1, StringListValue);
3687 Store_field(result, 0, convertStringList(stringListValue));
3688 break;
3691 CAMLreturn(result);
3695 ** curl_escape helper function
3698 CAMLprim value helper_curl_escape(value str)
3700 CAMLparam1(str);
3701 CAMLlocal1(result);
3702 char *curlResult;
3704 curlResult = curl_escape(String_val(str), string_length(str));
3705 result = copy_string(curlResult);
3706 free(curlResult);
3708 CAMLreturn(result);
3712 ** curl_unescape helper function
3715 CAMLprim value helper_curl_unescape(value str)
3717 CAMLparam1(str);
3718 CAMLlocal1(result);
3719 char *curlResult;
3721 curlResult = curl_unescape(String_val(str), string_length(str));
3722 result = copy_string(curlResult);
3723 free(curlResult);
3725 CAMLreturn(result);
3729 ** curl_getdate helper function
3732 CAMLprim value helper_curl_getdate(value str, value now)
3734 CAMLparam2(str, now);
3735 CAMLlocal1(result);
3736 time_t curlResult;
3737 time_t curlNow;
3739 curlNow = (time_t)Double_val(now);
3740 curlResult = curl_getdate(String_val(str), &curlNow);
3741 result = copy_double((double)curlResult);
3743 CAMLreturn(result);
3747 ** curl_version helper function
3750 CAMLprim value helper_curl_version(void)
3752 CAMLparam0();
3753 CAMLlocal1(result);
3754 char *str;
3756 str = curl_version();
3757 result = copy_string(str);
3759 CAMLreturn(result);
3762 struct CURLVersionBitsMapping
3764 int code;
3765 char *name;
3768 struct CURLVersionBitsMapping versionBitsMap[] =
3770 {CURL_VERSION_IPV6, "ipv6"},
3771 {CURL_VERSION_KERBEROS4, "kerberos4"},
3772 {CURL_VERSION_SSL, "ssl"},
3773 {CURL_VERSION_LIBZ, "libz"},
3774 {CURL_VERSION_NTLM, "ntlm"},
3775 {CURL_VERSION_GSSNEGOTIATE, "gssnegotiate"},
3776 {CURL_VERSION_DEBUG, "debug"},
3777 {CURL_VERSION_CURLDEBUG, "curldebug"},
3778 {CURL_VERSION_ASYNCHDNS, "asynchdns"},
3779 {CURL_VERSION_SPNEGO, "spnego"},
3780 {CURL_VERSION_LARGEFILE, "largefile"},
3781 {CURL_VERSION_IDN, "idn"},
3782 {CURL_VERSION_SSPI, "sspi"},
3783 {CURL_VERSION_CONV, "conv"},
3784 #if HAVE_DECL_CURL_VERSION_TLSAUTH_SRP
3785 {CURL_VERSION_TLSAUTH_SRP, "srp"},
3786 #endif
3787 #if HAVE_DECL_CURL_VERSION_NTLM_WB
3788 {CURL_VERSION_NTLM_WB, "wb"},
3789 #endif
3792 CAMLprim value caml_curl_version_info(value unit)
3794 CAMLparam1(unit);
3795 CAMLlocal4(v, vlist, vnum, vfeatures);
3796 const char* const* p = NULL;
3797 size_t i = 0;
3799 curl_version_info_data* data = curl_version_info(CURLVERSION_NOW);
3800 if (NULL == data) caml_failwith("curl_version_info");
3802 vlist = Val_emptylist;
3803 for (p = data->protocols; NULL != *p; p++)
3805 vlist = Val_cons(vlist, caml_copy_string(*p));
3808 vfeatures = Val_emptylist;
3809 for (i = 0; i < sizeof(versionBitsMap)/sizeof(versionBitsMap[0]); i++)
3811 if (0 != (versionBitsMap[i].code & data->features))
3812 vfeatures = Val_cons(vfeatures, caml_copy_string(versionBitsMap[i].name));
3815 vnum = caml_alloc_tuple(3);
3816 Store_field(vnum,0,Val_int(0xFF & (data->version_num >> 16)));
3817 Store_field(vnum,1,Val_int(0xFF & (data->version_num >> 8)));
3818 Store_field(vnum,2,Val_int(0xFF & (data->version_num)));
3820 v = caml_alloc_tuple(12);
3821 Store_field(v,0,caml_copy_string(data->version));
3822 Store_field(v,1,vnum);
3823 Store_field(v,2,caml_copy_string(data->host));
3824 Store_field(v,3,vfeatures);
3825 Store_field(v,4,data->ssl_version ? Val_some(caml_copy_string(data->ssl_version)) : Val_none);
3826 Store_field(v,5,data->libz_version ? Val_some(caml_copy_string(data->libz_version)) : Val_none);
3827 Store_field(v,6,vlist);
3828 Store_field(v,7,caml_copy_string((data->age >= 1 && data->ares) ? data->ares : ""));
3829 Store_field(v,8,Val_int((data->age >= 1) ? data->ares_num : 0));
3830 Store_field(v,9,caml_copy_string((data->age >= 2 && data->libidn) ? data->libidn : ""));
3831 Store_field(v,10,Val_int((data->age >= 3) ? data->iconv_ver_num : 0));
3832 Store_field(v,11,caml_copy_string((data->age >= 3 && data->libssh_version) ? data->libssh_version : ""));
3834 CAMLreturn(v);
3837 CAMLprim value caml_curl_pause(value conn, value opts)
3839 CAMLparam2(conn, opts);
3840 CAMLlocal4(v, vlist, vnum, vfeatures);
3841 Connection *connection = Connection_val(conn);
3842 int bitmask = 0;
3843 CURLcode result;
3845 while (Val_emptylist != opts)
3847 switch (Int_val(Field(opts,0)))
3849 case 0: bitmask |= CURLPAUSE_SEND; break;
3850 case 1: bitmask |= CURLPAUSE_RECV; break;
3851 case 2: bitmask |= CURLPAUSE_ALL; break;
3852 default: caml_failwith("wrong pauseOption");
3854 opts = Field(opts,1);
3857 result = curl_easy_pause(connection->connection,bitmask);
3858 if (result != CURLE_OK)
3859 raiseError(connection, result);
3861 CAMLreturn(Val_unit);
3865 * Curl multi stack support
3867 * Exported thin wrappers for libcurl are prefixed with caml_curl_multi_.
3868 * Other exported functions are prefixed with caml_curlm_, some of them
3869 * can/should be decomposed into smaller parts.
3872 struct ml_multi_handle
3874 CURLM* handle;
3875 value values; /* callbacks */
3878 enum
3880 curlmopt_socket_function,
3881 curlmopt_timer_function,
3883 /* last, not used */
3884 multi_values_total
3887 typedef struct ml_multi_handle ml_multi_handle;
3889 #define Multi_val(v) (*(ml_multi_handle**)Data_custom_val(v))
3890 #define CURLM_val(v) (Multi_val(v)->handle)
3892 static struct custom_operations curl_multi_ops = {
3893 "ygrek.curl_multi",
3894 custom_finalize_default,
3895 custom_compare_default,
3896 custom_hash_default,
3897 custom_serialize_default,
3898 custom_deserialize_default,
3899 #if defined(custom_compare_ext_default)
3900 custom_compare_ext_default,
3901 #endif
3904 CAMLprim value caml_curl_multi_init(value unit)
3906 CAMLparam1(unit);
3907 CAMLlocal1(v);
3908 ml_multi_handle* multi = (ml_multi_handle*)caml_stat_alloc(sizeof(ml_multi_handle));
3909 CURLM* h = curl_multi_init();
3911 if (!h)
3913 caml_stat_free(multi);
3914 failwith("caml_curl_multi_init");
3917 multi->handle = h;
3918 multi->values = caml_alloc(multi_values_total, 0);
3919 caml_register_generational_global_root(&multi->values);
3921 v = caml_alloc_custom(&curl_multi_ops, sizeof(ml_multi_handle*), 0, 1);
3922 Multi_val(v) = multi;
3924 CAMLreturn(v);
3927 CAMLprim value caml_curl_multi_cleanup(value handle)
3929 CAMLparam1(handle);
3930 ml_multi_handle* h = Multi_val(handle);
3932 if (NULL == h)
3933 CAMLreturn(Val_unit);
3935 caml_remove_generational_global_root(&h->values);
3937 if (CURLM_OK != curl_multi_cleanup(h->handle))
3938 failwith("caml_curl_multi_cleanup");
3940 Multi_val(handle) = (ml_multi_handle*)NULL;
3942 CAMLreturn(Val_unit);
3945 static CURL* curlm_remove_finished(CURLM* multi_handle, CURLcode* result)
3947 int msgs_in_queue = 0;
3949 while (1)
3951 CURLMsg* msg = curl_multi_info_read(multi_handle, &msgs_in_queue);
3952 if (NULL == msg) return NULL;
3953 if (CURLMSG_DONE == msg->msg)
3955 CURL* easy_handle = msg->easy_handle;
3956 if (result) *result = msg->data.result;
3957 if (CURLM_OK != curl_multi_remove_handle(multi_handle, easy_handle))
3959 /*failwith("curlm_remove_finished");*/
3961 return easy_handle;
3966 CAMLprim value caml_curlm_remove_finished(value v_multi)
3968 CAMLparam1(v_multi);
3969 CAMLlocal2(v_easy, v_tuple);
3970 CURL* handle;
3971 CURLM* multi_handle;
3972 CURLcode result;
3973 Connection* conn = NULL;
3975 multi_handle = CURLM_val(v_multi);
3977 caml_enter_blocking_section();
3978 handle = curlm_remove_finished(multi_handle,&result);
3979 caml_leave_blocking_section();
3981 if (NULL == handle)
3983 CAMLreturn(Val_none);
3985 else
3987 conn = findConnection(handle);
3988 if (conn->curl_ERRORBUFFER != NULL)
3990 Store_field(Field(conn->ocamlValues, Ocaml_ERRORBUFFER), 0, caml_copy_string(conn->curl_ERRORBUFFER));
3992 conn->refcount--;
3993 /* NB: same handle, but different block */
3994 v_easy = caml_curl_alloc(conn);
3995 v_tuple = caml_alloc(2, 0);
3996 Store_field(v_tuple,0,v_easy);
3997 Store_field(v_tuple,1,Val_int(result)); /* CURLcode */
3998 CAMLreturn(Val_some(v_tuple));
4002 static int curlm_wait_data(CURLM* multi_handle)
4004 struct timeval timeout;
4005 CURLMcode ret;
4007 fd_set fdread;
4008 fd_set fdwrite;
4009 fd_set fdexcep;
4010 int maxfd = -1;
4012 FD_ZERO(&fdread);
4013 FD_ZERO(&fdwrite);
4014 FD_ZERO(&fdexcep);
4016 /* set a suitable timeout */
4017 timeout.tv_sec = 1;
4018 timeout.tv_usec = 0;
4020 /* get file descriptors from the transfers */
4021 ret = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
4023 if (ret == CURLM_OK && maxfd >= 0)
4025 int rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
4026 if (-1 != rc) return 0;
4028 return 1;
4031 CAMLprim value caml_curlm_wait_data(value v_multi)
4033 CAMLparam1(v_multi);
4034 int ret;
4035 CURLM* h = CURLM_val(v_multi);
4037 caml_enter_blocking_section();
4038 ret = curlm_wait_data(h);
4039 caml_leave_blocking_section();
4041 CAMLreturn(Val_bool(0 == ret));
4044 CAMLprim value caml_curl_multi_add_handle(value v_multi, value v_easy)
4046 CAMLparam2(v_multi,v_easy);
4047 CURLM* multi = CURLM_val(v_multi);
4048 Connection* conn = Connection_val(v_easy);
4050 /* prevent collection of OCaml value while the easy handle is used
4051 and may invoke callbacks registered on OCaml side */
4052 conn->refcount++;
4054 /* may invoke callbacks so need to be consistent with locks */
4055 caml_enter_blocking_section();
4056 if (CURLM_OK != curl_multi_add_handle(multi, conn->connection))
4058 conn->refcount--; /* not added, revert */
4059 caml_leave_blocking_section();
4060 failwith("caml_curl_multi_add_handle");
4062 caml_leave_blocking_section();
4064 CAMLreturn(Val_unit);
4067 CAMLprim value caml_curl_multi_remove_handle(value v_multi, value v_easy)
4069 CAMLparam2(v_multi,v_easy);
4070 CURLM* multi = CURLM_val(v_multi);
4071 Connection* conn = Connection_val(v_easy);
4073 /* may invoke callbacks so need to be consistent with locks */
4074 caml_enter_blocking_section();
4075 if (CURLM_OK != curl_multi_remove_handle(multi, conn->connection))
4077 caml_leave_blocking_section();
4078 failwith("caml_curl_multi_remove_handle");
4080 conn->refcount--;
4081 caml_leave_blocking_section();
4083 CAMLreturn(Val_unit);
4086 CAMLprim value caml_curl_multi_perform_all(value v_multi)
4088 CAMLparam1(v_multi);
4089 int still_running = 0;
4090 CURLM* h = CURLM_val(v_multi);
4092 caml_enter_blocking_section();
4093 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(h, &still_running));
4094 caml_leave_blocking_section();
4096 CAMLreturn(Val_int(still_running));
4099 CAMLprim value helper_curl_easy_strerror(value v_code)
4101 CAMLparam1(v_code);
4102 CAMLreturn(caml_copy_string(curl_easy_strerror(Int_val(v_code))));
4106 * Wrappers for the curl_multi_socket_action infrastructure
4107 * Based on curl hiperfifo.c example
4110 #ifdef _WIN32
4111 #ifndef Val_socket
4112 #define Val_socket(v) win_alloc_socket(v)
4113 #endif
4114 #ifndef Socket_val
4115 #error Socket_val not defined in unixsupport.h
4116 #endif
4117 #else /* _WIN32 */
4118 #ifndef Socket_val
4119 #define Socket_val(v) Long_val(v)
4120 #endif
4121 #ifndef Val_socket
4122 #define Val_socket(v) Val_int(v)
4123 #endif
4124 #endif /* _WIN32 */
4126 static void raise_multi_error(char const* msg)
4128 static value* exception = NULL;
4130 if (NULL == exception)
4132 exception = caml_named_value("Curl.Multi.Error");
4133 if (NULL == exception) caml_invalid_argument("Curl.Multi.Error");
4136 caml_raise_with_string(*exception, msg);
4139 static void check_mcode(CURLMcode code)
4141 char const *s = NULL;
4142 switch (code)
4144 case CURLM_OK : return;
4145 case CURLM_CALL_MULTI_PERFORM : s="CURLM_CALL_MULTI_PERFORM"; break;
4146 case CURLM_BAD_HANDLE : s="CURLM_BAD_HANDLE"; break;
4147 case CURLM_BAD_EASY_HANDLE : s="CURLM_BAD_EASY_HANDLE"; break;
4148 case CURLM_OUT_OF_MEMORY : s="CURLM_OUT_OF_MEMORY"; break;
4149 case CURLM_INTERNAL_ERROR : s="CURLM_INTERNAL_ERROR"; break;
4150 case CURLM_UNKNOWN_OPTION : s="CURLM_UNKNOWN_OPTION"; break;
4151 case CURLM_LAST : s="CURLM_LAST"; break;
4152 case CURLM_BAD_SOCKET : s="CURLM_BAD_SOCKET"; break;
4153 default : s="CURLM_unknown"; break;
4155 raise_multi_error(s);
4158 CAMLprim value caml_curl_multi_socket_action(value v_multi, value v_fd, value v_kind)
4160 CAMLparam3(v_multi, v_fd, v_kind);
4161 CURLM* h = CURLM_val(v_multi);
4162 int still_running = 0;
4163 CURLMcode rc = CURLM_OK;
4164 curl_socket_t socket;
4165 int kind = 0;
4167 if (Val_none == v_fd)
4169 socket = CURL_SOCKET_TIMEOUT;
4171 else
4173 socket = Socket_val(Field(v_fd, 0));
4176 switch (Int_val(v_kind))
4178 case 0 : break;
4179 case 1 : kind |= CURL_CSELECT_IN; break;
4180 case 2 : kind |= CURL_CSELECT_OUT; break;
4181 case 3 : kind |= CURL_CSELECT_IN | CURL_CSELECT_OUT; break;
4182 default:
4183 raise_multi_error("caml_curl_multi_socket_action");
4186 /* fprintf(stdout,"fd %u kind %u\n",socket, kind); fflush(stdout); */
4188 caml_enter_blocking_section();
4189 do {
4190 rc = curl_multi_socket_action(h, socket, kind, &still_running);
4191 } while (rc == CURLM_CALL_MULTI_PERFORM);
4192 caml_leave_blocking_section();
4194 check_mcode(rc);
4196 CAMLreturn(Val_int(still_running));
4199 CAMLprim value caml_curl_multi_socket_all(value v_multi)
4201 CAMLparam1(v_multi);
4202 int still_running = 0;
4203 CURLMcode rc = CURLM_OK;
4204 CURLM* h = CURLM_val(v_multi);
4206 caml_enter_blocking_section();
4207 do {
4208 rc = curl_multi_socket_all(h, &still_running);
4209 } while (rc == CURLM_CALL_MULTI_PERFORM);
4210 caml_leave_blocking_section();
4212 check_mcode(rc);
4214 CAMLreturn(Val_int(still_running));
4217 static int curlm_sock_cb_nolock(CURL *e, curl_socket_t sock, int what, ml_multi_handle* multi, void *sockp)
4219 CAMLparam0();
4220 CAMLlocal2(v_what,csock);
4221 (void)e;
4222 (void)sockp; /* not used */
4224 /* v_what = Val_int(what); */
4225 switch (what)
4227 case CURL_POLL_NONE : v_what = Val_int(0); break;
4228 case CURL_POLL_IN : v_what = Val_int(1); break;
4229 case CURL_POLL_OUT : v_what = Val_int(2); break;
4230 case CURL_POLL_INOUT : v_what = Val_int(3); break;
4231 case CURL_POLL_REMOVE : v_what = Val_int(4); break;
4232 default:
4233 fprintf(stderr, "curlm_sock_cb sock=%d what=%d\n", sock, what);
4234 fflush(stderr);
4235 raise_multi_error("curlm_sock_cb"); /* FIXME exception from callback */
4237 csock=Val_socket(sock);
4238 caml_callback2(Field(multi->values,curlmopt_socket_function),
4239 csock, v_what);
4241 CAMLreturn(0);
4244 static int curlm_sock_cb(CURL *e, curl_socket_t sock, int what, void *cbp, void *sockp)
4246 int ret;
4247 caml_leave_blocking_section();
4248 ret = curlm_sock_cb_nolock(e, sock, what, (ml_multi_handle*)cbp, sockp);
4249 caml_enter_blocking_section();
4250 return ret;
4253 CAMLprim value caml_curl_multi_socketfunction(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_socket_function, v_cb);
4260 curl_multi_setopt(multi->handle, CURLMOPT_SOCKETFUNCTION, curlm_sock_cb);
4261 curl_multi_setopt(multi->handle, CURLMOPT_SOCKETDATA, multi);
4263 CAMLreturn(Val_unit);
4266 static void curlm_timer_cb_nolock(ml_multi_handle *multi, long timeout_ms)
4268 CAMLparam0();
4269 caml_callback(Field(multi->values,curlmopt_timer_function), Val_long(timeout_ms));
4270 CAMLreturn0;
4273 static int curlm_timer_cb(CURLM *multi, long timeout_ms, void *userp)
4275 (void)multi;
4277 caml_leave_blocking_section();
4278 curlm_timer_cb_nolock((ml_multi_handle*)userp, timeout_ms);
4279 caml_enter_blocking_section();
4280 return 0;
4283 CAMLprim value caml_curl_multi_timerfunction(value v_multi, value v_cb)
4285 CAMLparam2(v_multi, v_cb);
4286 ml_multi_handle* multi = Multi_val(v_multi);
4288 Store_field(multi->values, curlmopt_timer_function, v_cb);
4290 curl_multi_setopt(multi->handle, CURLMOPT_TIMERFUNCTION, curlm_timer_cb);
4291 curl_multi_setopt(multi->handle, CURLMOPT_TIMERDATA, multi);
4293 CAMLreturn(Val_unit);
4296 CAMLprim value caml_curl_multi_timeout(value v_multi)
4298 CAMLparam1(v_multi);
4299 long ms = 0;
4300 CURLMcode rc = CURLM_OK;
4301 ml_multi_handle* multi = Multi_val(v_multi);
4303 rc = curl_multi_timeout(multi->handle, &ms);
4305 check_mcode(rc);
4307 CAMLreturn(Val_long(ms));