Skip to content

Commit 9e1c352

Browse files
committed
doc + reexporting correctly session_data from Async to Session modules
1 parent d02f555 commit 9e1c352

7 files changed

Lines changed: 60 additions & 67 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ doc:
2020

2121
.PHONY: install_doc
2222
install_doc: doc
23-
rsync -r _build/default/_doc/_html/ ~/WWW2/main/simple_httpd
23+
rsync -r _build/default/_doc/_html/ ~/WWW2/main/simple_httpd/
2424

2525
.PHONY: install
2626
install: build

doc/vfs_pack.mld

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{0 Vfs_pack web site compiler}
22

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

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

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

@@ -30,49 +31,49 @@ Here is an example of `.chaml` included in the distribution:
3031
<li> item 2 </li>
3132
<li> item 3 </li>
3233
<li> item 4 </li>
33-
<li> views: <ML>
34+
<li> views: <script type="ml">
3435
let _ = echo (string_of_int count);;
3536
let _ = echo (if count mod 2 = 0 then
3637
<p>even (<ML> ^ string_of_int count ^ </ML>)</p>
3738
else <p>odd</p>)
38-
</ML>
39+
</script>
3940
</li>
4041
</ul>
4142
<a href="."> escape from this file </a>
4243
<br/>
43-
request: <ML>
44+
request: <script type="ml">
4445
let _ = printf "%a" Request.pp request
45-
</ML>
46+
</script>
4647
</body>
4748
</html>
4849
]}
4950

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

5253
There are four ways to insert ocaml code:
5354

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

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

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

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

7879
{[
@@ -104,7 +105,7 @@ There are four ways to insert ocaml code:
104105
- Inside OCaml sections, html sections are interpreted as ocaml
105106
strings.
106107

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

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

119-
is compiled as
120+
is transformed to
120121

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

127-
Here is the documentation of the `vfs_pack` command line:
128+
Here is the documentation of the [vfs_pack] command line:
128129

129130
{[
130131
vfs-pack [opt]+
131132

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

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

138139
-v verbose mode
139-
-o set output file
140+
-o <file> set output file
140141
--file <name,file> adds name=file to the VFS
141142
--url <name,url> adds name=url to the VFS
142-
--mirror <prefix,dir> copies directory dir into the VFS under prefix
143-
--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.
144-
--destination set the destination folder to use with mirror
145-
--perm set the permission of created folder
146-
-F <file> reads entries from the file, on per line written using this command line option syntax.
143+
--mirror <prefix,dir> adds prefix=dir to the vfs, copying all files in directory dir
144+
--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.
145+
--destination <dir> set the destination folder to use with mirror
146+
--perm <int> set the permission of created folder
147+
-F <file> reads entries from the file, written using this command line option syntax.
147148
-help Display this list of options
148149
--help Display this list of options
149150
]}

examples/files/tyr.chaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<script type="ml/global">
5-
type Async.session_data += Count of int
5+
type Session.session_data += Count of int
66
</script>
77
<script type="ml/prelude">
88
let cookies, session = Session.check ~init:(fun _ -> Count 0) request

src/Session.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
open Response_code
22

33
type t = Async.session
4-
type session_data = Async.session_data
4+
5+
type session_data = Async.session_data = ..
6+
type session_data += NoData = Async.NoData
57

68
module LinkedList = Util.LinkedList
79

src/Session.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
type t
2-
type session_data = Async.session_data
2+
type session_data = Async.session_data = ..
3+
type session_data += NoData
34

45
(** Managment of sessions using cookies *)
56

src/Simple_httpd.mli

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ module Async : sig
8989
be identified as one session using session cookies.
9090
*)
9191

92-
type session_data = ..
93-
type session_data += NoData
94-
9592
(** Connection status. Holds the number of clients per domain. *)
9693
type status = {
9794
nb_connections : int Atomic.t array
@@ -756,7 +753,8 @@ module Session : sig
756753
type t
757754
(** type for session *)
758755

759-
type session_data = Async.session_data
756+
type session_data = ..
757+
type session_data += NoData
760758
(** This type is an extensible variant that you can extend to hold some data
761759
which resides in the server memory. These data will be lost if the
762760
server reboots.

src/bin/vfs_pack.ml

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ type entry =
99
| Url of string * string
1010
| Path of bool * string * string * string
1111
| Mirror of string * string
12-
| Source_file of string
1312
| MlHtml of string * string
1413

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

213-
| Source_file f ->
214-
log (fun k -> k "read source file %S" f);
215-
216-
let lines =
217-
read_file f |> String.split_on_char '\n'
218-
|> List.map String.trim
219-
|> List.filter ((<>) "")
220-
in
221-
222-
let process_line line =
223-
let vfs_path, path = split_comma line in
224-
if is_url path then add_entry (Url(vfs_path, path))
225-
else add_entry (Mirror (vfs_path, path))
226-
in
227-
228-
List.iter process_line lines
229-
230212
in
231213
List.iter add_entry l;
232214

@@ -261,28 +243,37 @@ let () =
261243
let vfs_path, path = split_comma s in
262244
let vfs_path, path = if path="" then "", vfs_path else vfs_path, path in
263245
add_entry (Mirror (vfs_path, path))
264-
and add_source f = add_entry (Source_file f)
265246
and add_url s =
266247
let vfs_path, path = split_comma s in
267248
if is_url path then add_entry (Url(vfs_path, path))
268249
else invalid_arg (spf "--url: invalid URL %S" path)
269250
in
270251

271-
let opts = [
252+
let rec opts = lazy ([
272253
"-v", Arg.Set verbose, " verbose mode";
273-
"-o", Arg.Set_string out, " set output file";
254+
"-o", Arg.Set_string out, " <file> set output file";
274255
"--file", Arg.String add_file, " <name,file> adds name=file to the VFS";
275256
"--url", Arg.String add_url, " <name,url> adds name=url to the VFS";
276-
"--mirror", Arg.String add_mirror, " <prefix,dir> copies directory dir into the VFS under prefix";
277-
"--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.";
257+
"--mirror", Arg.String add_mirror, " <prefix,dir> adds prefix=dir to the vfs, copying all files in directory dir";
258+
"--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.";
278259
("--destination", Arg.String (fun s -> destination := Some s),
279-
" set the destination folder to use with mirror");
260+
" <dir> set the destination folder to use with mirror (default the same as dir)");
280261
("--perm", Arg.Set_int perm,
281-
" set the permission of created folder");
282-
("-F", Arg.String add_source,
283-
" <file> reads entries from the file, on per line written using this command line option syntax.");
284-
] |> Arg.align in
285-
Arg.parse opts (fun _ -> raise (Arg.Help "no positional arg")) help;
262+
" <int>set the permission of created folder");
263+
("-F", Arg.String parse_source,
264+
" <file> reads entries from the file, written using this command line option syntax.");
265+
] |> Arg.align)
266+
267+
and parse args =
268+
let opts = Lazy.force opts in
269+
Arg.parse_argv ~current:(ref 0) args opts (fun _ -> raise (Arg.Help "no positional arg")) help
270+
271+
and parse_source f =
272+
log (fun k -> k "read source file %S" f);
273+
let args = Arg.read_arg f in
274+
parse args
275+
in
276+
parse Sys.argv;
286277

287278
let out, close =
288279
if !out="" then stdout,ignore

0 commit comments

Comments
 (0)