Skip to content

Commit

Permalink
doc + reexporting correctly session_data from Async to Session modules
Browse files Browse the repository at this point in the history
  • Loading branch information
craff committed Jul 5, 2023
1 parent d02f555 commit 9e1c352
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ doc:

.PHONY: install_doc
install_doc: doc
rsync -r _build/default/_doc/_html/ ~/WWW2/main/simple_httpd
rsync -r _build/default/_doc/_html/ ~/WWW2/main/simple_httpd/

.PHONY: install
install: build
Expand Down
61 changes: 31 additions & 30 deletions doc/vfs_pack.mld
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{0 Vfs_pack web site compiler}

vfs_pack is a binary too that will compile your website to a ML site that
can ve used with {!Simple_httpd.Dir.add_vfs}. It will
The binary [vfs_pack], provided with [Simple_httpd], will compile your website to
a ML module of type {!Simple_httpd.Dir.VFS} that can be used with
{!Simple_httpd.Dir.add_vfs}. It will

- compile a directory structure to a list of Route (see {!Simple_httpd.Route},
- It can be configured to allow directory listing or redirection to `index.html`,
- Large files are not compiled into memory and may be installed in a separate
directory
- precompiled version of large file (with deflate a.k.a zlib) can be stored too.
- dynamic `.chaml` can be used to produce dynamic pages.
- compile a directory structure to a list of Route (see {!Simple_httpd.Route}),
- it can be configured to allow directory listing or redirection to [index.html],
- large files are not stored into memory but may be installed in a separate
directory,
- precompressed version of file (with deflate a.k.a zlib) can be stored too.
- dynamic [.chaml] can be used to produce dynamic pages.

Here is an example of `.chaml` included in the distribution:

Expand All @@ -30,49 +31,49 @@ Here is an example of `.chaml` included in the distribution:
<li> item 2 </li>
<li> item 3 </li>
<li> item 4 </li>
<li> views: <ML>
<li> views: <script type="ml">
let _ = echo (string_of_int count);;
let _ = echo (if count mod 2 = 0 then
<p>even (<ML> ^ string_of_int count ^ </ML>)</p>
else <p>odd</p>)
</ML>
</script>
</li>
</ul>
<a href="."> escape from this file </a>
<br/>
request: <ML>
request: <script type="ml">
let _ = printf "%a" Request.pp request
</ML>
</script>
</body>
</html>
]}

As you can see, some defect of php are avoided: no quote/double every where.
As you can see, some defect of php are avoided: no quote everywhere.

There are four ways to insert ocaml code:

- In all [<ML>...</ML>] or [<script type="ml">...</script>] sections
- In all [<script type="ml">...</script>] sections
correspond to OCaml code. In this code [Simple_httpd] is open, the value
[request :] {!Simple_httpd.Request.t} is accessible.

- OCaml sections which are not inside others are compiled as toplevel
expressions. You may define type, values, ... In these sections and you can
use [echo] and [printf] provided automatically by opening a module of type
{!Simple_httpd.Input.Output} if they are not "prelude" section.
use [echo] and [printf] provided because a module of type
{!Simple_httpd.Input.Output} is open if they are not "prelude" section.

- Some [<script type="ml/prelude">...</script>] section can be added inside
the [<head>] tag but before any other OCaml sections. they are executed
before any output is done to the server and may shadow two values [headers
:] {!Simple_httpd.Headers.t} initialy empty and [cookies :]
{!Simple_httpd.Cookies.t} that contain an initical
:] {!Simple_httpd.Headers.t} initially empty and [cookies :]
{!Simple_httpd.Cookies.t} that contain an initial
{!Simple_httpd.Headers.Cache_Control} or {!Simple_httpd.Headers.ETag}. For
instance, this allows to read and modify cookies or start a
{!Simple_httpd.Session}. Values defined in this section are part of a
`Prelude` module which is opened in the rest of the file.

- Some [<script type="ml/global">...</script>] section can alse be added inside
- Some [<script type="ml/global">...</script>] section can also be added inside
inside the [<head>] tag, they are placed at the top level of the generated ml
file. This is usefull for instance to extend the [Async.session_data] type.
file. This is useful for instance to extend the [Async.session_data] type.
Here is an example using session:

{[
Expand Down Expand Up @@ -104,7 +105,7 @@ There are four ways to insert ocaml code:
- Inside OCaml sections, html sections are interpreted as ocaml
strings.

- Nested OCaml are compiled as normal ocaml code with no modification.
- Nested OCaml sections are emitted as ocaml code with no modification.
For instance, in the above example,

{[
Expand All @@ -116,34 +117,34 @@ There are four ways to insert ocaml code:
</ML>
]}

is compiled as
is transformed to

{[
let _ = echo (if n mod 2 = 0 then
"<li>even (" ^ string_of_int n ^ ")</li>"
else "<li>odd</li>")
]}

Here is the documentation of the `vfs_pack` command line:
Here is the documentation of the [vfs_pack] command line:

{[
vfs-pack [opt]+

Builds an OCaml module containing a `Simple_httpd.Dir.Embedded_fs.t`
Builds an OCaml module containing a {!Simple_httpd.Dir.Embedded_fs.t}
virtual file system. This is useful to pack assets into an OCaml binary,
for example.

Each entry in the VFS can be added from the command line.

-v verbose mode
-o set output file
-o <file> set output file
--file <name,file> adds name=file to the VFS
--url <name,url> adds name=url to the VFS
--mirror <prefix,dir> copies directory dir into the VFS under prefix
--max-size <size>, max size to hold file in memory (default: infinite). Bigger filed are copie to the folder given by --desination. A compressed version .zlib is also produced.
--destination set the destination folder to use with mirror
--perm set the permission of created folder
-F <file> reads entries from the file, on per line written using this command line option syntax.
--mirror <prefix,dir> adds prefix=dir to the vfs, copying all files in directory dir
--max-size <size>, max size to hold file in memory (default: infinite). Bigger filed are copied to the folder given by --destination. A compressed version .zlib is also produced if it is at least 10% smaller.
--destination <dir> set the destination folder to use with mirror
--perm <int> set the permission of created folder
-F <file> reads entries from the file, written using this command line option syntax.
-help Display this list of options
--help Display this list of options
]}
2 changes: 1 addition & 1 deletion examples/files/tyr.chaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<script type="ml/global">
type Async.session_data += Count of int
type Session.session_data += Count of int
</script>
<script type="ml/prelude">
let cookies, session = Session.check ~init:(fun _ -> Count 0) request
Expand Down
4 changes: 3 additions & 1 deletion src/Session.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
open Response_code

type t = Async.session
type session_data = Async.session_data

type session_data = Async.session_data = ..
type session_data += NoData = Async.NoData

module LinkedList = Util.LinkedList

Expand Down
3 changes: 2 additions & 1 deletion src/Session.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type t
type session_data = Async.session_data
type session_data = Async.session_data = ..
type session_data += NoData

(** Managment of sessions using cookies *)

Expand Down
6 changes: 2 additions & 4 deletions src/Simple_httpd.mli
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ module Async : sig
be identified as one session using session cookies.
*)

type session_data = ..
type session_data += NoData

(** Connection status. Holds the number of clients per domain. *)
type status = {
nb_connections : int Atomic.t array
Expand Down Expand Up @@ -756,7 +753,8 @@ module Session : sig
type t
(** type for session *)

type session_data = Async.session_data
type session_data = ..
type session_data += NoData
(** This type is an extensible variant that you can extend to hold some data
which resides in the server memory. These data will be lost if the
server reboots.
Expand Down
49 changes: 20 additions & 29 deletions src/bin/vfs_pack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type entry =
| Url of string * string
| Path of bool * string * string * string
| Mirror of string * string
| Source_file of string
| MlHtml of string * string

let read_file filename =
Expand Down Expand Up @@ -210,23 +209,6 @@ let emit ~perm ?max_size ?destination oc (l:entry list) : unit =
in
traverse ""

| Source_file f ->
log (fun k -> k "read source file %S" f);

let lines =
read_file f |> String.split_on_char '\n'
|> List.map String.trim
|> List.filter ((<>) "")
in

let process_line line =
let vfs_path, path = split_comma line in
if is_url path then add_entry (Url(vfs_path, path))
else add_entry (Mirror (vfs_path, path))
in

List.iter process_line lines

in
List.iter add_entry l;

Expand Down Expand Up @@ -261,28 +243,37 @@ let () =
let vfs_path, path = split_comma s in
let vfs_path, path = if path="" then "", vfs_path else vfs_path, path in
add_entry (Mirror (vfs_path, path))
and add_source f = add_entry (Source_file f)
and add_url s =
let vfs_path, path = split_comma s in
if is_url path then add_entry (Url(vfs_path, path))
else invalid_arg (spf "--url: invalid URL %S" path)
in

let opts = [
let rec opts = lazy ([
"-v", Arg.Set verbose, " verbose mode";
"-o", Arg.Set_string out, " set output file";
"-o", Arg.Set_string out, " <file> set output file";
"--file", Arg.String add_file, " <name,file> adds name=file to the VFS";
"--url", Arg.String add_url, " <name,url> adds name=url to the VFS";
"--mirror", Arg.String add_mirror, " <prefix,dir> copies directory dir into the VFS under prefix";
"--max-size", Arg.Set_int max_size, " <size>, max size to hold file in memory (default: infinite). Bigger filed are copie to the folder given by --desination. A compressed version .zlib is also produced.";
"--mirror", Arg.String add_mirror, " <prefix,dir> adds prefix=dir to the vfs, copying all files in directory dir";
"--max-size", Arg.Set_int max_size, " <size>, max size to hold file in memory (default: infinite). Bigger filed are copied to the folder given by --destination. A compressed version .zlib is also produced if it is at least 10% smaller.";
("--destination", Arg.String (fun s -> destination := Some s),
" set the destination folder to use with mirror");
" <dir> set the destination folder to use with mirror (default the same as dir)");
("--perm", Arg.Set_int perm,
" set the permission of created folder");
("-F", Arg.String add_source,
" <file> reads entries from the file, on per line written using this command line option syntax.");
] |> Arg.align in
Arg.parse opts (fun _ -> raise (Arg.Help "no positional arg")) help;
" <int>set the permission of created folder");
("-F", Arg.String parse_source,
" <file> reads entries from the file, written using this command line option syntax.");
] |> Arg.align)

and parse args =
let opts = Lazy.force opts in
Arg.parse_argv ~current:(ref 0) args opts (fun _ -> raise (Arg.Help "no positional arg")) help

and parse_source f =
log (fun k -> k "read source file %S" f);
let args = Arg.read_arg f in
parse args
in
parse Sys.argv;

let out, close =
if !out="" then stdout,ignore
Expand Down

0 comments on commit 9e1c352

Please sign in to comment.