source: project/wiki/stream-cgi @ 12879

Last change on this file since 12879 was 12879, checked in by azul, 11 years ago

Changes applied for azul (193.142.125.1) through svnwiki:

Another example

File size: 3.4 KB
Line 
1[[tags:eggs web cgi streams eggspublicdomain]]
2[[toc:]]
3
4== Introduction
5
6The stream-cgi egg is useful for creating applications that run under Common Gateway Interface.
7
8You might find the [[html-stream]] egg useful.
9
10== Examples
11
12The following is a trivial CGI application.
13You can call it using {{?name=alejo}} as its QUERY_STRING.
14
15<enscript highlight=scheme>
16(use stream-cgi html-stream)
17
18(cgi-main
19  (lambda (get post cookies)
20    (stream-append
21      (string->stream "Content-type: text/html\n\n")
22      (let ((name (get 'name)))
23        (if (null? name)
24          (html-stream (html (body (p "Hello, stranger!"))))
25          (html-stream
26            (html
27              (body (p "Hello, " (b (car name)))))))))))
28</enscript>
29
30This is another way to do the same:
31
32<enscript highlight=scheme>
33(use stream-cgi stream-ext)
34
35(cgi-main
36  (lambda (get post cookies)
37    (with-output-to-stream
38      (lambda ()
39        (format #t "Content-type: text/html~%~%<html><body><p>")
40        (let ((name (get 'name)))
41          (if (null? name)
42            (format #t "Hello, stranger!")
43            (format #t "Hello, <b>~A</b>" (stream->string (car name)))))
44        (format #t "</p></body></html>")))))
45</enscript>
46
47
48== Authors
49
50The stream-cgi egg was created by [[Alejandro Forero Cuervo]] <azul@freaks-unidos.net>.
51
52== License
53
54The CGI egg for Chicken Scheme is in the public domain and may be reproduced or copied without
55permission from its author.  Citation of the source is appreciated.
56
57== Requirements
58
59This egg depends on the following:
60
61* [[srfi-40]]
62* [[stream-ext]]
63
64== Creating a CGI application
65
66 [procedure] (cgi-main proc)
67
68Initializes internal structures, calls the
69{{proc}} procedure and, after it returns, cleans things up.
70
71{{proc}} is passed three argument:
72procedures that can used to obtain information received by the CGI from, respectively:
73
74# The {{QUERY_STRING}} (or {{<input>}} objects in HTML forms using method {{GET}})
75# The contents uploaded through HTTP post requests.
76# HTTP cookies.
77
78The functions passed to {{proc}} receive one argument,
79a symbol, and return a list with all its associated values as found on the inputs.
80If no values are found, the empty list
81is returned.  The values are returned as srfi-40 streams of characters;
82values shorter than a given threshold are kept in memory while others
83are stored in temporary files.  This allows the library to use a constant
84amount of memory, regardless of the size of the contents received
85through HTTP.
86
87The functions passed to {{proc}} can not be called after
88{{proc}} returns.
89This is because they may depend on files that get removed
90when {{proc}} returns.
91
92{{proc}} must return a srfi-40 stream with the contents
93to send to the browser, including HTTP headers;
94the actual headers in the HTTP response are generated
95by your web server.
96
97Temporary files are created by the library by means of
98{{(create-temporary-file "chicken-cgi-input")}}.
99You probably want to make sure that the
100{{TMPDIR}}, {{TEMP}} or {{TMP}}
101environment variables are set to a meaningful directory,
102such as {{/tmp}}, where the user your application
103runs as (usually {{nobody}}) has write permissions.
104
105== Version history
106
107=== 1.4
108
109* Implemented parsing of multipart/form-data.
110
111=== 1.3
112
113* Fixed bug in invocation of <code>hash-table-walk</code> [Thanks to Michal Dybizbanski]
114
115=== 1.2
116
117* Adapted to SRFI-69-compatible hash-tables
118
119=== 1.1
120
121* Simple bug fixes
122
123=== 1.0
124
125* First public release
126
Note: See TracBrowser for help on using the repository browser.