Given the C Code
#include "http.h"
void on_request(http_s *request);
FIOBJ HTTP_HEADER_X_DATA;
int main(void) {
HTTP_HEADER_X_DATA = fiobj_str_new("X-Data", 6);
http_listen("3000", NULL, .on_request = on_request, .log = 1);
fio_start(.threads = 1);
fiobj_free(HTTP_HEADER_X_DATA);
}
void on_request(http_s *request) {
http_set_cookie(request, .name = "my_cookie", .name_len = 9, .value = "data",
.value_len = 4);
http_set_header(request, HTTP_HEADER_CONTENT_TYPE,
http_mimetype_find("txt", 3));
http_set_header(request, HTTP_HEADER_X_DATA, fiobj_str_new("my data", 7));
http_send_body(request, "Hello World!\r\n", 14);
}
I wrote it’s Julia’s ccall counterpart
# First, define the path to the shared library
const httplib = "./libf.so"
struct http_settings_s
on_request::Ptr{Cvoid}
log::Cint
# Add other fields as needed
end
function on_request(request::Ptr{Cvoid})
ccall((:http_send_body, httplib), Cvoid, (Ptr{Cvoid}, Cstring, Csize_t), request, "Hello World!\r\n", 14)
end
# Convert the Julia function to a C function pointer
const on_request_c = @cfunction(on_request, Cvoid, (Ptr{Cvoid},))
# Define the C struct
const settings = http_settings_s(on_request_c, 1)
println("before listen")
# Start listening on the specified port
ccall((:http_listen, httplib), Cintmax_t, (Cstring, Cstring, Ref{http_settings_s}), "3000", C_NULL, settings)
println("after listen")
# Start the server
ccall((:fio_start, httplib), Cvoid, (Cint,), 1)
println("after start")
and I get the output
julia http.jl
before listen
FATAL: No supported SSL/TLS library available.
the output of
# Start the server
ccall((:fio_start, httplib), Cvoid, (Cint,), 1)
looks like
❯ julia http.jl
INFO: Server is running 1 worker X 1 thread with server 0.7.6 (kqueue)
* Detected capacity: 131056 open file limit
* Root pid: 72503
* Press ^C to stop