The entry server
A simple web server: TCP in the process, one thread, one table of connections, and records of fixed size for all its state. It speaks HTTP/1.1 (RFC 9110, RFC 9112) through src/library/modus/tcp, and allocates nothing.
What it can reach comes from its grants only, with no ambient access:
a segment endpoint with a listen grant: the TCP port 80, and the
remotes that send a SYN to it;
a private store in the storage service, as each process has: the pages
that it serves, by name;
a service ring: the console calls its exports server.put,
server.pages, server.remove and server.served, functions with
typed parameters.
No shared folder: the principal puts a page with server.put from the console. PUT and DELETE over HTTP are refused (405): the network is not authenticated.
What it is for. It shows a network service of the system against clients that the project did not write: a browser or curl on the host. TCP is a library in the process, so the network service keeps no state of a connection, and the bounds against a slow or hostile client are the bounds of this process.
Start: start("server") in the console, or the bar of the desktop. Then, from the console: connect("server"), server.put("hello.html", "<h1>hello</h1>").
Grants, as the root service gives them:
- Network: a listen grant of TCP on port 80.
- Serve: the exports for the console.
- Store: its private store, which holds the pages and their index.
What a reader sees: a window with the counts of the requests, of the connections and of the pages. The log has "server: N pages read from the private store, M refused", "server: listening on the TCP port 80, ...", one line for each change of a page and for each end of a connection (50 in one second at most), and "server: N requests answered in one second" for each second with 50 requests or more.
What it serves: / and /counts.json, its own counts, and the pages of its store by name, with the field Date when the date is known. It serves no TLS: the TLS of the system is the one of QUIC.
The design:.
main.odin: the TCP side and the loop. http.odin: the requests and the answers. pages.odin: the pages of the store, and the exports. draw.odin: the window.
See: The network: TCP as a library in the process: The HTTP server
server module with an entry
- Hash
8b25ceb837122434c5079ec5e695e3a249a7824bcebe67a53ecd6548b5484297- Size
374563 bytes- Exports
4- Imports
0- Imported by
0- License
- MIT
A simple web server: TCP in the process, one thread, one table of connections, and records of fixed size for all its state
| Fact | Value |
|---|---|
| Kind | Modules with an entry |
| Source | src/modules/demonstrations/server |
| Boot image | yes: in the boot image, 374563 bytes in this build |
| Build settings | none: the defaults of the source |
| Imports | none |
| Imported by | none |
Neighbours
It imports no module, and no module imports it. The module graph shows it with the other modules that have no edge.
Exports
| Export | What it does |
|---|---|
entry() | The entry of server: it checks its grant, reads its pages, then serves TCP and the calls of the console until the machine stops |
put(name: Text, text: Text) -> Integer | writes a page in the private store of the server, and serves it at /name from now on |
remove(name: Text) -> Integer | takes a page out of the private store of the server: /name gives 404 from now on |
pages() -> Table(Page_Row) | gives one row for each page that the server serves: its name, its bytes, the answers of it since the start, its content type |
served() -> Served | gives the counts of the server: the requests that it answered, by class of status, its connections, its bytes and its pages |
entry
entry()
The entry of server: it checks its grant, reads its pages, then serves TCP and the calls of the console until the machine stops.
The entry, in order:
- 1. takes its random generator; with no seed it stops with
Refused; - 2. opens its window and its service, then its endpoint and its datagram region; a missing part stops it (
fail); - 3. asks the network service for its grant; a grant that is not a listen grant of TCP stops it;
- 4. creates the table of 48 connections and their buffers, with a secret of the initial sequence numbers from its generator;
- 5. reads the pages of its private store into memory (
pages_load); - 6. writes "server: listening on the TCP port 80, ...".
Then one loop runs for ever. Each pass gives the segments that arrived to the TCP library, answers the requests, runs the calls of the console, and draws the window at most ten times in one second. It waits for a segment, a call, an event of the window, or the next timer of TCP.
- Form
- The entry of the module: one parameter, an address, fixed by the contract.
src/modules/demonstrations/server/main.odin:472
put
put(name: , text: ) ->
put writes a page in the private store of the server, and serves it at /name from now on.
It is the one way to change what the network sees: HTTP itself refuses PUT and DELETE (405), because the network is not authenticated. The order is the store first, then the memory of the server: the page that is served is always the page that the store keeps, and a refused write leaves the old page in place. A page that changes keeps its count of answers.
- name: 1 to 39 bytes of letters, digits, ".", "-" and "_". Its end gives the content type: .html, .txt, .css, .js, .json or .svg.
- text: the bytes of the page, 60 KiB at most (the call region of a connection has 64 KiB).
The caller must act for the principal (Acts_For_Principal). The result is the bytes of the page, or a negative number with an error record:
No_Grant(-1): the caller does not act for the principal.Empty,Too_LongorBad_Byte(-2): the name; forBad_Byte, the record holds the place of the byte.Too_Long(-3): the text has more than 60 KiB.Full(-4): 16 pages exist, and the name is new.Refused(-6): the server has no store, or the storage service refused the page or the index of the pages.
Example: server.put("hello.html", "<h1>hello</h1>"), then the host reads http://<the address>/hello.html.
name:text:
- When it fails
- a negative result, and the error record
src/modules/demonstrations/server/pages.odin:191
remove
remove(name: ) ->
remove takes a page out of the private store of the server: /name gives 404 from now on.
The store first: when the storage service refuses, the page stays in the store and stays served, so what the network sees never differs from what the store keeps.
- name: the name of a page, as put gave it.
The caller must act for the principal (Acts_For_Principal). The result is 0, or a negative number with an error record:
No_Grant(-1): the caller does not act for the principal.Absent(-5): no page has this name.Refused(-6): the storage service refused the removal; the page is still served.
name:
- When it fails
- a negative result, and the error record
src/modules/demonstrations/server/pages.odin:269
pages
pages() -> ()
pages gives one row for each page that the server serves: its name, its bytes, the answers of it since the start, its content type.
Each caller gets the rows, with no grant: the pages are public on the network already.
- result: a table of
Page_Rowrows; the call fills at most its length.
The result is the count of pages, 16 at most. It never fails.
- none
()- When it fails
- a negative result, and the error record
src/modules/demonstrations/server/pages.odin:307
served
served() ->
served gives the counts of the server: the requests that it answered, by class of status, its connections, its bytes and its pages.
These are facts of the server itself: each caller sees them. The bytes in count the data of connections that ended; calls counts the calls of the exports.
- result: one
Servedrecord.
It never fails.
Read from the source of the module, as the build reads it to describe the exports (tools/modustool/build/describe.odin).
What the root service gives it
| What | Value | As the root service says |
|---|---|---|
| Grants | A listen grant, its exports for the console, and its pages in its private store. | |
| Other terms of its start grant set | none | |
| Budget that it asks for |
| Its table of connections with their buffers (CAPACITY): 3490 pages. And the glyph memory of its own copy of the text rendering, which draws its text by hand: 354 pages (display.TEXT_MEMORY_PAGES). And the picture of its window, which it draws by hand at its logical size: 976 x 680 pixels of 4 bytes, 649 pages (interface.hand_picture_pages). 3490 + 354 + 649 = 4493. |
The start grant set is read from start_grant_sets in src/modules/system/root/start_grant_sets.odin. The stated budget is read from the constant MODULE_BUDGET of the source of the module (its line); the build adds the counted parts and writes the sum in the module (tools/modustool/build/budget_count.odin); the root service accepts it up to the largest budget of a process, and adds the limits of processor time and of log records (src/modules/system/root/budgets.odin).
Text wire form
A wire form of the module contract: one word of a call. The address of a slice record {address, count} of UTF-8 bytes.
Integer wire form
A wire form of the module contract: one word of a call. A signed integer of 64 bits, in a register.
Error_Record record of a module
A record of the module server: 56 bytes, aligned on 8 bytes, 0 bytes of padding.
kindi6408 bytesuniti6488 bytesoperationi64168 bytesargumenti64248 bytesvaluei64328 byteslimiti64408 bytesfoundi64488 bytes
Table wire form
A wire form of the module contract: one word of a call. Records of one description that follow each other (a table).
Page_Row record of a module
A record of the module server: 88 bytes, aligned on 8 bytes, 0 bytes of padding.
nameText(40)040 bytesbytesi64408 bytesservedi64488 bytestypeText(32)5632 bytes
Served record of a module
A record of the module server: 80 bytes, aligned on 8 bytes, 0 bytes of padding.
requestsi6408 bytesanswers_2xxi6488 bytesanswers_4xxi64168 bytesanswers_5xxi64248 bytesconnections_acceptedi64328 bytesconnections_openi64408 bytesbytes_ini64488 bytesbytes_outi64568 bytes
and 2 more
pages_load procedure of the package server
pages_load :: proc "contextless" (ctx: ^system.Context)
pages_load opens the private store and reads the pages into memory.
