Skip to content

Commit 2455e3f

Browse files
committed
Reimplemented http_client using the ocurl library
This commit reimplements the http_client module by using the ocurl library. This should add support for the https protocol.
1 parent 0d44635 commit 2455e3f

File tree

9 files changed

+431
-449
lines changed

9 files changed

+431
-449
lines changed

config/Makefile.config.in

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ OCAMLDOC=@OCAMLDOC@
2626
OCAMLMKTOP=@OCAMLMKTOP@
2727
OCAMLVERSION_MAJOR=@OCAMLVERSION_MAJOR@
2828
NUMS_INCLUDE=@NUMS_INCLUDE@
29+
CURL_INCLUDE=@CURL_INCLUDE@
30+
CURL_PATH=@CURL_PATH@
2931

3032
LABLGL_CMA=@LABLGL_CMA@
3133
LABLGL_CMXA=@LABLGL_CMXA@

config/Makefile.in

+6-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ SRC_FILETP=src/networks/fileTP
9393
SUBDIRS=$(EXTLIB) $(CDK) $(BITSTRING) $(LIB) $(RSS) $(XML) $(NET) tools \
9494
$(COMMON) $(DRIVER) $(MP3) src/config/$(OS_FILES)
9595

96-
INCLUDES += $(foreach file, $(SUBDIRS), -I $(file)) -I +camlp4 $(NUMS_INCLUDE)
96+
INCLUDES += $(foreach file, $(SUBDIRS), -I $(file)) -I +camlp4 $(NUMS_INCLUDE) -I +threads $(CURL_INCLUDE)
9797

9898
CFLAGS:=$(CFLAGS) $(CONFIG_INCLUDES) $(GTKCFLAGS) $(GD_CFLAGS)
9999

@@ -197,7 +197,8 @@ LIB_SRCS= \
197197
$(LIB)/gettext.ml4 $(LIB)/md5_c.c $(LIB)/sha1_c.c \
198198
$(LIB)/tiger.c \
199199
$(LIB)/stubs_c.c $(LIB)/set2.ml $(LIB)/queues.ml \
200-
$(LIB)/verificationBitmap.ml
200+
$(LIB)/verificationBitmap.ml \
201+
$(LIB)/threadPool.ml
201202

202203
ifeq ("$(MAGIC)", "yes")
203204
MAGIC_LIBS_flags += -cclib -lmagic
@@ -580,6 +581,9 @@ DRIVER_SRCS+= \
580581

581582
ICONS_CMXA=icons.cmxa
582583

584+
LIBS_opt += $(OCAMLLIB)/threads/threads.cmxa
585+
LIBS_opt += $(CURL_PATH)/curl.cmxa -cclib "-L$(CURL_PATH) -lcurl-helper"
586+
583587
CDK_CMXA=cdk.cmxa
584588
BITSTRING_CMXA=
585589
BITSTRING_CMA=

config/configure

+31
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,8 @@ ac_header_c_list=
650650
ac_subst_vars='LTLIBOBJS
651651
LIBOBJS
652652
NUMS_INCLUDE
653+
CURL_INCLUDE
654+
CURL_PATH
653655
OCAMLVERSION_MAJOR
654656
DEVFLAGS
655657
CONFIGURE_RUN
@@ -6100,6 +6102,35 @@ else
61006102
printf "%s\n" "found $NUMS_INCLUDE" >&6; }
61016103
fi
61026104

6105+
CURL_INCLUDE=
6106+
CURL=yes
6107+
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking ocaml curl library" >&5
6108+
printf %s "checking ocaml curl library... " >&6; }
6109+
CURL_PATH="`ocamlfind query curl 2> /dev/null`"
6110+
if test "$CURL_PATH" != ""; then
6111+
CURL_INCLUDE="-I $CURL_PATH"
6112+
else
6113+
CURL=no
6114+
fi
6115+
if ! test -f $OCAMLLIB/curl.$OCAMLLIB_EXT; then
6116+
if test -f $OCAMLLIB/curl/curl.$OCAMLLIB_EXT; then
6117+
CURL_INCLUDE="-I +curl"
6118+
fi
6119+
fi
6120+
if ! test -f "$CURL_PATH/curl.$OCAMLLIB_EXT"; then
6121+
CURL=no
6122+
fi
6123+
6124+
if test "$CURL" = "no"; then
6125+
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
6126+
printf "%s\n" "no" >&6; }
6127+
echo "ERROR: ocurl is missing"
6128+
exit 1
6129+
else
6130+
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: found $CURL_INCLUDE" >&5
6131+
printf "%s\n" "found $CURL_INCLUDE" >&6; }
6132+
fi
6133+
61036134
echo ""
61046135
echo "----------------------------------"
61056136
echo " Checking system headers."

config/configure.in

+22
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,28 @@ else
563563
AC_MSG_RESULT([found $NUMS_INCLUDE])
564564
fi
565565

566+
CURL_INCLUDE=
567+
CURL=yes
568+
AC_MSG_CHECKING([ocaml curl bindings])
569+
if ! test -f $OCAMLLIB/curl.$OCAMLLIB_EXT; then
570+
if test -f $OCAMLLIB/curl/curl.$OCAMLLIB_EXT; then
571+
CURL_INCLUDE="-I +curl"
572+
else
573+
CURL_PATH="`ocamlfind query curl 2> /dev/null`"
574+
if test "$CURL_PATH" != ""; then
575+
CURL_INCLUDE="-I $CURL_PATH"
576+
else
577+
CURL=no
578+
fi
579+
fi
580+
fi
581+
582+
if test "$CURL" = "no"; then
583+
AC_MSG_RESULT([no])
584+
else
585+
AC_MSG_RESULT([found $CURL_INCLUDE])
586+
fi
587+
566588
echo ""
567589
echo "----------------------------------"
568590
echo " Checking system headers."

src/daemon/common/commonOptions.ml

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ let min_connections = 50
129129

130130
let () =
131131
lprintf_nl "Starting MLDonkey %s ... " Autoconf.current_version;
132+
133+
Curl.global_init Curl.CURLINIT_GLOBALALL;
134+
lprintf_nl "Curl initialized. Version: %s" (Curl.version ());
135+
132136
let ulof_old = Unix2.c_getdtablesize () in
133137
lprintf_nl "Language %s, locale %s, ulimit for open files %d"
134138
Charset.Locale.default_language Charset.Locale.locale_string ulof_old;

src/utils/lib/threadPool.ml

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
(* Copyright 2025 Luca Carlon *)
2+
(*
3+
This file is part of mldonkey.
4+
5+
mldonkey is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation; either version 2 of the License, or
8+
(at your option) any later version.
9+
10+
mldonkey is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with mldonkey; if not, write to the Free Software
17+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
*)
19+
20+
module TaskQueue = struct
21+
type 'a t = {
22+
queue : 'a Queue.t;
23+
mutex : Mutex.t;
24+
cond : Condition.t;
25+
}
26+
27+
(* Create a new task queue *)
28+
let create () = {
29+
queue = Queue.create ();
30+
mutex = Mutex.create ();
31+
cond = Condition.create ();
32+
}
33+
34+
(* Add a task to the queue *)
35+
let add t task =
36+
Mutex.lock t.mutex;
37+
Queue.add task t.queue;
38+
Condition.signal t.cond;
39+
Mutex.unlock t.mutex
40+
41+
(* Retrieve a task from the queue (blocking if empty) *)
42+
let take t =
43+
Mutex.lock t.mutex;
44+
while Queue.is_empty t.queue do
45+
Condition.wait t.cond t.mutex
46+
done;
47+
let task = Queue.pop t.queue in
48+
Mutex.unlock t.mutex;
49+
task
50+
end
51+
52+
(* Thread pool *)
53+
type t = {
54+
threads : Thread.t list;
55+
tasks : (unit -> unit) TaskQueue.t;
56+
stop_flag : bool ref;
57+
}
58+
59+
(* Worker thread function *)
60+
let rec worker_loop tasks stop_flag =
61+
if !stop_flag then ()
62+
else
63+
let task = TaskQueue.take tasks in
64+
(try task () with _ -> ());
65+
worker_loop tasks stop_flag
66+
67+
(* Create a thread pool with a fixed number of threads *)
68+
let create num_threads =
69+
let tasks = TaskQueue.create () in
70+
let stop_flag = ref false in
71+
let threads = List.init num_threads (fun _ ->
72+
Thread.create (fun () -> worker_loop tasks stop_flag) ()
73+
) in
74+
{ threads; tasks; stop_flag }
75+
76+
(* Add a task to the thread pool *)
77+
let add_task pool task =
78+
TaskQueue.add pool.tasks task
79+
80+
(* Stop the thread pool and wait for all threads to finish *)
81+
let stop pool =
82+
pool.stop_flag := true;
83+
List.iter Thread.join pool.threads

src/utils/lib/threadPool.mli

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(* Copyright 2025 Luca Carlon *)
2+
(*
3+
This file is part of mldonkey.
4+
5+
mldonkey is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation; either version 2 of the License, or
8+
(at your option) any later version.
9+
10+
mldonkey is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with mldonkey; if not, write to the Free Software
17+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
*)
19+
20+
(** The type of a thread pool. *)
21+
type t
22+
23+
(** [create num_threads] creates a thread pool with [num_threads] worker threads. *)
24+
val create : int -> t
25+
26+
(** [add_task pool task] adds [task] to the task queue of the thread pool [pool].
27+
The task is a function that takes no arguments and returns [unit]. *)
28+
val add_task : t -> (unit -> unit) -> unit
29+
30+
(** [stop pool] stops all worker threads in the thread pool [pool] and waits for them to finish. *)
31+
val stop : t -> unit

0 commit comments

Comments
 (0)