Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running Tiny_httpd.run_exn as a background task. #91

Open
zoj613 opened this issue Nov 26, 2024 · 5 comments
Open

Running Tiny_httpd.run_exn as a background task. #91

zoj613 opened this issue Nov 26, 2024 · 5 comments

Comments

@zoj613
Copy link

zoj613 commented Nov 26, 2024

Hi, I am trying to programmically create and serve a local directory via http using this library as a way to unit test an http client. However when I call Tiny_httpd.run_exn dune runtest freezes. Is there a way I can call run_exn as background task so that the program can continue to use the http server to perform unit tests? Here is what I wrote:

         let dir_behavior = Tiny_httpd.Dir.Lists
         and download = true and delete = true and upload = true in
         let config = Tiny_httpd.Dir.config ~dir_behavior ~delete ~upload ~download ()
         and addr = "127.0.0.1" and port = 8080 and timeout = 30000. in
         let server = Tiny_httpd.create ~max_connections:4 ~addr ~port ~timeout () in
         and suffix = (string_of_int @@ Random.int 5000) ^ ".zarr" in
		 let dir = Filename.(concat (get_temp_dir_name ()) (rand_num ^ ".zarr")) in
         Tiny_httpd_unix.Dir.add_dir_path ~config ~dir ~prefix:"" server;
         Tiny_httpd.run_exn ~after_init:(fun () ->
            perform_unit_tests_for_client;
            Tiny_httpd.stop server) server

If I use http_of_dir <dir_name> -p 8080 in another shell session the client unit tests run just fine. Am I using this library's serving capabilities incorrectly? How can I achieve what i'm looking for?

As an aside it appears POST/PUT methods don't actually create a resource under the directory even after the client (it uses Ezcurl internally) returns a 200/201 status code.

@c-cube
Copy link
Owner

c-cube commented Nov 26, 2024

I think you might want to start a thread to run Tiny_httpd.run_exn in the background.

About the upload, I find this a bit surprising, there are some tests for that. At least with http_of_dir . -p 8085 --upload alongside curl -X PUT -vv http://localhost:8085/foo --upload-file foo I do get a 201 and the file is uploaded properly?

@zoj613
Copy link
Author

zoj613 commented Nov 26, 2024

Using Thread.create Tiny_httpd.run_exn server seems to do the job, thanks alot!

Regarding the http_of_dir call, I mistakenly forgot to add the --upload flag so I'm happy to confirm it works as expected. However solving this let me to discover that the HEAD method returns a 404 Not found response for the same object I successfully uploaded using PUT. Is HEAD not supported or must I do something extra to make it work?

@c-cube
Copy link
Owner

c-cube commented Nov 26, 2024

ah yes, I don't think the directory module supports HEAD at all. It'd be a welcome addition!

@zoj613
Copy link
Author

zoj613 commented Nov 28, 2024

ah yes, I don't think the directory module supports HEAD at all. It'd be a welcome addition!

I was able to bolt-on a HEAD router handler which works as expected. I then discovered a bug in the Virtual File System's create function. When a resource path contains a directory name, the function raises a Sys_error "<resource path inside the VFS>: No such file or directory which then causes the PUT or POST methods to fail with a 403 error code. See:

tiny_httpd/src/unix/dir.ml

Lines 106 to 110 in b80c5f9

let create f =
let oc = open_out_bin (top // f) in
let write = output oc in
let close () = close_out oc in
write, close
. I believe the create function needs to include a way to create intermediate directories if they exist when creating a resource.

Do you want this to be a seperate issue or are you fine discussing it here?

@c-cube
Copy link
Owner

c-cube commented Nov 29, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants