1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
@code{(require 'html-form)}
@defvar *html:output-port*
Procedure names starting with @samp{html:} send their output
to the port @var{*html:output-port*}. @var{*html:output-port*} is initially the current output port.
@end defvar
@defun make-atval txt
Returns a string with character substitutions appropriate to
send @var{txt} as an @dfn{attribute-value}.
@cindex attribute-value
@end defun
@defun make-plain txt
Returns a string with character substitutions appropriate to
send @var{txt} as an @dfn{plain-text}.
@cindex plain-text
@end defun
@defun html:start-page title backlink @dots{}
Outputs headers for an HTML page named @var{title}.
@end defun
@defun html:end-page
Outputs HTML codes to end a page.
@end defun
@defun html:pre line1 line @dots{}
Writes (using @code{html:printf}) the strings @var{line1}, @var{lines} as
@dfn{PRE}formmated plain text (rendered in fixed-width font).
@cindex PRE
Newlines are inserted between @var{line1}, @var{lines}. HTML tags (@samp{<tag>})
within @var{lines} will be visible verbatim.
@end defun
@defun html:comment line1 line @dots{}
Writes (using @code{html:printf}) the strings @var{line1} as HTML
comments.
@end defun
@section HTML Tables
@defun html:start-table caption
@end defun
@defun html:end-table
@end defun
@defun html:heading columns
Outputs a heading row for the currently-started table.
@end defun
@defun html:href-heading columns urls
Outputs a heading row with column-names @var{columns} linked to URLs @var{urls}.
@end defun
@defun make-row-converter k foreigns
The positive integer @var{k} is the primary-key-limit (number of
primary-keys) of the table. @var{foreigns} is a list of the filenames of
foreign-key field pages and #f for non foreign-key fields.
@code{make-row-converter} returns a procedure taking a row for its single argument. This
returned procedure prints the table row to @var{*html:output-port*}.
@end defun
@defun table-name->filename table-name
Returns the symbol @var{table-name} converted to a filename.
@end defun
@defun table->html caption db table-name match-key1 @dots{}
Writes HTML for @var{db} table @var{table-name} to @var{*html:output-port*}.
The optional @var{match-key1} @dots{} arguments restrict actions to a subset of
the table. @xref{Table Operations, match-key}.
@end defun
@defun table->page db table-name index-filename
Writes a complete HTML page to @var{*html:output-port*}. The string
@var{index-filename} names the page which refers to this one.
@end defun
@defun catalog->html db caption
Writes HTML for the catalog table of @var{db} to @var{*html:output-port*}.
@end defun
@defun catalog->page db caption
Writes a complete HTML page for the catalog of @var{db} to
@var{*html:output-port*}.
@end defun
@section HTML Forms
@defun html:start-form method action
The symbol @var{method} is either @code{get}, @code{head}, @code{post},
@code{put}, or @code{delete}. @code{html:start-form} prints the header for an HTML
@dfn{form}.
@cindex form
@end defun
@defun html:end-form pname submit-label
@code{html:end-form} prints the footer for an HTML @dfn{form}. The string @var{submit-label}
@cindex form
appears on the button which submits the form.
@end defun
@defun command->html rdb command-table command method action
The symbol @var{command-table} names a command table in the @var{rdb} relational
database.
@code{command->html} writes an HTML-2.0 @dfn{form} for command @var{command} to the
@cindex form
current-output-port. The @samp{SUBMIT} button, which is labeled @var{command},
invokes the URI @var{action} with method @var{method} with a hidden attribute
@code{*command*} bound to the command symbol submitted.
An action may invoke a CGI script
(@samp{http://www.my-site.edu/cgi-bin/search.cgi}) or HTTP daemon
(@samp{http://www.my-site.edu:8001}).
This example demonstrates how to create a HTML-form for the @samp{build}
command.
@example
(require (in-vicinity (implementation-vicinity) "build.scm"))
(call-with-output-file "buildscm.html"
(lambda (port)
(fluid-let ((*html:output-port* port))
(html:start-page 'commands)
(command->html
build '*commands* 'build 'post
(or "/cgi-bin/build.cgi"
"http://localhost:8081/buildscm"))
html:end-page)))
@end example
@end defun
@c node HTTP and CGI service, Printing Scheme, HTML Forms, Textual Conversion Packages
@section HTTP and CGI service
@code{(require 'html-form)}
@defun cgi:serve-command rdb command-table
Reads a @samp{"POST"} or @samp{"GET"} query from
@code{(current-input-port)} and executes the encoded command from @var{command-table}
in relational-database @var{rdb}.
This example puts up a plain-text page in response to a CGI query.
@example
(display "Content-Type: text/plain") (newline) (newline)
(require 'html-form)
(load (in-vicinity (implementation-vicinity) "build.scm"))
(cgi:serve-command build '*commands*)
@end example
@end defun
@defun serve-urlencoded-command rdb command-table urlencoded
Reads attribute-value pairs from @var{urlencoded}, converts them to
parameters and invokes the @var{rdb} command named by the parameter
@code{*command*}.
@end defun
@defun http:serve-query input-port output-port serve-proc
reads the @dfn{query-string} from @var{input-port}. If this is a valid
@cindex query-string
@samp{"POST"} or @samp{"GET"} query, then @code{http:serve-query} calls @var{serve-proc} with two
arguments, the query-string and the header-alist.
Otherwise, @code{http:serve-query} replies (to @var{output-port}) with appropriate HTML describing the
problem.
@end defun
This example services HTTP queries from port 8081:
@example
(define socket (make-stream-socket AF_INET 0))
(socket:bind socket 8081)
(socket:listen socket 10)
(dynamic-wind
(lambda () #f)
(lambda ()
(do ((port (socket:accept socket)
(socket:accept socket)))
(#f)
(dynamic-wind
(lambda () #f)
(lambda ()
(fluid-let ((*html:output-port* port))
(http:serve-query
port port
(lambda (query-string header)
(http:send-header
'(("Content-Type" . "text/plain")))
(with-output-to-port port
(lambda ()
(serve-urlencoded-command
build '*commands* query-string)))))))
(lambda () (close-port port)))))
(lambda () (close-port socket)))
@end example
@defun http:read-request-line port
Reads the first non-blank line from @var{port} and, if successful,
returns a list of three itmes from the request-line:
@enumerate 0
@item Method
Either one of the symbols @code{options}, @code{get}, @code{head},
@code{post}, @code{put}, @code{delete}, or @code{trace}; Or a string.
@item Request-URI
A string. At the minimum, it will be the string @samp{"/"}.
@item HTTP-Version
A string. For example, @samp{HTTP/1.0}.
@end enumerate
@end defun
@defun cgi:read-query-string
Reads the @dfn{query-string} from @code{(current-input-port)}.
@cindex query-string
@code{cgi:read-query-string} reads a @samp{"POST"} or @samp{"GET"} queries, depending on the
value of @code{(getenv "REQUEST_METHOD")}.
@end defun
|