|
| 1 | +{0 Simple_httpd library to write web server and application} |
| 2 | + |
| 3 | +{1 Introduction} |
| 4 | + |
| 5 | +This library implements a HTTP/1.1 server for Linux using domains and |
| 6 | +algebraic effects. It uses [epoll] and [eventfd] to schedule the treatment of |
| 7 | +clients very efficiently, with one domain used to accept requests and several |
| 8 | +domains to handle these requests. [Simple_httpd] can listen to several |
| 9 | +addresses and ports and use this information together with the headers [Host] |
| 10 | +field to decide how to answer the request. |
| 11 | + |
| 12 | +It is fast and can handle several thousands of simultaneous connections. It |
| 13 | +may use memory cache, [sendfile] system call and other TCP options to reach an |
| 14 | +efficiency comparable or better than nginx on static files. |
| 15 | + |
| 16 | +Through a bunch of modules, described here {!Simple_httpd}, we provide: |
| 17 | + |
| 18 | +- Routing, by address, port, host and url path. |
| 19 | + |
| 20 | +- Handling of static files possibly using a memory cache or a virtual file |
| 21 | + system. More precisely, it can serves a directory dynamically (hence |
| 22 | + reflecting changes has they occur on the file system) or use |
| 23 | + {{:./vfs_pack.html}vfs_pack} to compile a static folder (In this can changed |
| 24 | + nee recompilation and restarts), see below. |
| 25 | + |
| 26 | +- Basic management of session and cookies. Simple_httpd sessions can for |
| 27 | + instance keep an open connection to a database server. Session are saved and |
| 28 | + restored on server restart and the {!Simple_httpd.Auth} module provide basic |
| 29 | + authentication. Its functorial interface allow for more complex |
| 30 | + authentication like CAS. |
| 31 | + |
| 32 | +- Support for ssl using {{:https://github.com/savonet/ocaml-ssl}ocaml-ssl}. This |
| 33 | + includes using ktls if your linux kernel supports it. |
| 34 | + |
| 35 | +- A {!Simple_httpd.Host} module dedicated to write single server handling |
| 36 | + several sites or applications. |
| 37 | + |
| 38 | +- A {{:./vfs_pack.html}web site compiler vfs_pack}, the will cache small files |
| 39 | + in memory and bigger files in a separate store. This allows not to store the |
| 40 | + source of your websites on the server. Files in store or memory may be |
| 41 | + pre-compressed with zip (a.k.a. deflate, gzip is planned). |
| 42 | + |
| 43 | +- {{:./vfs_pack.html}vfs_pack} includes an equivalent of php files named |
| 44 | + [chaml] that are compiled to OCaml, but much faster than php (and also |
| 45 | + safer). It will also parse [html] files allowing to detect error and minimizing them. |
| 46 | + Parsing and dynamic css [are] planned. |
| 47 | + |
| 48 | +- It is fast: Here are some curves showing the performance on static files |
| 49 | + compared to nginx and apache. Our 99% quantile is very good, meaning that |
| 50 | + {!Simple_httpd} handles very quickly most request. |
| 51 | + |
| 52 | +{1 Benchmarks} |
| 53 | + |
| 54 | +Here is a plot of the latency obtained using |
| 55 | +{{:https://github.com/tsenart/vegeta} [vegeta]} with 1000 requests per seconds |
| 56 | +for 15s, on a small static file. Simple_httpd uses |
| 57 | +{!Simple_httpd.Dir.add_vfs} using {{:./vfs_pack.html}[vfs_pack]} from the |
| 58 | +[example/echo.ml] file shown below: |
| 59 | + |
| 60 | +{%html: <p align="center"><img src="/simple_httpd/bench/static.svg" alt="Latencies for static files"/></p>%} |
| 61 | + |
| 62 | +Here is the same graphic for a small chaml/php dynamic file. |
| 63 | + |
| 64 | +{%html: <p align="center"><img src="/simple_httpd/bench/dynamic.svg" alt="Latencies for dynamic files"/></p>%} |
| 65 | + |
| 66 | +Here are some other graphs showing the maximum number of requests possible, using {{:https://nghttp2.org/documentation/h2load-howto.html}[h2load]} |
| 67 | + |
| 68 | +{%html: <p align="center"><img src="/simple_httpd/bench/bench_foo_1k.svg" alt="comparison for small files"/></p>%} |
| 69 | + |
| 70 | +{%html: <p align="center"><img src="/simple_httpd/bench/bench_foo_500k.svg" alt="comparison for large files"/></p>%} |
| 71 | + |
| 72 | +[apache] and [nginx] are the usual servers with [php-fpm] and their default |
| 73 | +configuration on debian 13 except for accepting more connections than the |
| 74 | +default. There is a (reproducible) problem with nginx and php without ssl with |
| 75 | +a few extreme value that impact the mean, and also some errors in rare |
| 76 | +cases. Comparatively, simple_httpd serves all requests. |
| 77 | + |
| 78 | +Here is a similar graph showing the difference between [.php] and [.chaml] on |
| 79 | +a similar file. It shows that simple_httpd can serve at least than 5 times |
| 80 | +more requests. |
| 81 | + |
| 82 | +{%html: <p align="center"><img src="/simple_httpd/bench/bench_chaml_bar.html.svg" alt="comparison chaml versus php"/></p>%} |
| 83 | + |
| 84 | +{1 Quick start} |
| 85 | + |
| 86 | +A good start is the template folder of the distribution, documented |
| 87 | +{{:./template.html}[here]}. It contains the skeleton for a server serving |
| 88 | +two sites with status report and statistics. |
| 89 | + |
| 90 | +See also [examples/echo.ml] below, that demonstrates some of the features by |
| 91 | +declaring a few endpoints, including one for uploading files and a virtual |
| 92 | +file system. |
| 93 | + |
| 94 | +To go further, you should start reading {!Simple_httpd} the main module of the library and |
| 95 | +look at {{:./template.html}[template]} that is provided. |
| 96 | + |
| 97 | +{[ |
| 98 | +@@ECHO@@ |
| 99 | +]} |
0 commit comments