source: project/wiki/eggref/4/call-with-query @ 27744

Last change on this file since 27744 was 27744, checked in by Peter Danenberg, 8 years ago

@return -> @to

File size: 5.9 KB
Line 
1== call-with-query
2
3A reasonable abstraction around e.g. fastcgi server-invocations: ports, environment, query
4[[toc:]]
5=== {{query}}
6<record>query</record>
7Data structure to hold the query.
8; server : Server variables, e.g. environment
9; client : Client variables, e.g. {get,post,cookie}-parameters
10<enscript highlight="scheme">(define-record-and-printer query server client)
11</enscript>
12=== {{query-client-any}}
13<procedure>(query-client-any query key) → object</procedure>
14<procedure>(query-client-any query key default) → object</procedure>
15Return the first client parameter (e.g. {get,post,cookie}-parameter)
16corresponding to the key.
17; key : The key whose value to extract
18; default : A default value if {{key}} doesn't exist
19<enscript highlight="scheme">(define query-client-any
20  (case-lambda
21    ((query key) (alist-any (query-client query) key))
22    ((query key default) (alist-any (query-client query) key default))))
23</enscript>
24=== {{query-client-all}}
25<procedure>(query-client-all query key) → list</procedure>
26<procedure>(query-client-all query key default) → list</procedure>
27Return a list of client parameters (e.g. {get,post,cookie}-parameters)
28corresponding to the key.
29; key : The key whose value to extract
30; default : A default value if {{key}} doesn't exist
31<enscript highlight="scheme">(define query-client-all
32  (case-lambda
33    ((query key) (alist-all (query-client query) key))
34    ((query key default) (alist-all (query-client query) key default))))
35</enscript>
36=== {{query-server-any}}
37<procedure>(query-server-any query key) → object</procedure>
38<procedure>(query-server-any query key default) → object</procedure>
39Return the first client parameter (e.g.
40environment-variable) corresponding to the key.
41; key : The key whose value to extract
42; default : A default value if {{key}} doesn't exist
43<enscript highlight="scheme">(define query-server-any
44  (case-lambda
45    ((query key) (alist-any (query-server query) key))
46    ((query key default) (alist-any (query-server query) key default))))
47</enscript>
48=== {{query-server-all}}
49<procedure>(query-server-all query key) → list</procedure>
50<procedure>(query-server-all query key default) → list</procedure>
51Return a list of client parameters (e.g.
52 environment-variables) corresponding to the key.
53; key : The key whose value to extract
54; default : A default value if {{key}} doesn't exist
55<enscript highlight="scheme">(define query-server-all
56  (case-lambda
57    ((query key) (alist-all (query-server query) key))
58    ((query key default) (alist-all (query-server query) key default))))
59</enscript>
60=== {{query-any}}
61<procedure>(query-any query key) → object</procedure>
62<procedure>(query-any query key default) → object</procedure>
63Return the first client or server parameter (see above) corresponding to the key.
64; key : The key whose value to extract
65; default : A default value if {{key}} doesn't exist
66<enscript highlight="scheme">(define query-any
67  (case-lambda
68    ((query key) (alist-any (query-promiscuous query) key))
69    ((query key default) (alist-any (query-promiscuous query) key default))))
70</enscript>
71=== {{query-all}}
72<procedure>(query-all query key) → list</procedure>
73<procedure>(query-all query key default) → list</procedure>
74Return a list of client or server parameters (see above) corresponding to the key.
75; key : The key whose value to extract
76; default : A default value if {{key}} doesn't exist
77<enscript highlight="scheme">(define query-all
78  (case-lambda
79    ((query key) (alist-all (query-promiscuous query) key))
80    ((query key default) (alist-all (query-promiscuous query) key default))))
81</enscript>
82=== {{call-with-dynamic-fastcgi-query}}
83<procedure>(call-with-dynamic-fastcgi-query quaerendum) → unspecified</procedure>
84Start a dynamic FastCGI server where output is bound to stdout;
85and where a monadic function taking a query-record is called for every
86request.
87; quaerendum : A monadic function receiving a query parameter
88<enscript highlight="scheme">(define (call-with-dynamic-fastcgi-query quaerendum)
89  (fcgi-dynamic-server-accept-loop
90    (lambda (in out err env)
91      (let ((environment
92              (map (match-lambda
93                     ((key . value) (cons (env-string->symbol key) value)))
94                   (env)))
95            (cookies
96              (form-urldecode
97                (let ((cookies
98                        (string-delete
99                          char-set:whitespace
100                          (env "HTTP_COOKIE" ""))))
101                  (and (not (string-null? cookies)) cookies))))
102            (cookies2
103              (form-urldecode
104                (let ((cookies
105                        (string-delete
106                          char-set:whitespace
107                          (env "HTTP_COOKIE2" ""))))
108                  (and (not (string-null? cookies)) cookies))))
109            (post-data (form-urldecode (fcgi-get-post-data in env)))
110            (query (form-urldecode
111                     (let ((query (env "QUERY_STRING")))
112                       (and (not (string-null? query)) query)))))
113        (parameterize
114          ((current-output-port
115             (make-output-port (lambda (scribendum) (out scribendum)) void))
116           (current-error-port
117             (make-output-port (lambda (errandum) (err errandum)) void)))
118          (quaerendum
119            (make-query
120              environment
121              (append cookies cookies2 post-data query))))))))
122</enscript>
123==== Examples
124An authorization server
125 (call-with-auth-db
126   (lambda (connection)
127     (call-with-dynamic-fastcgi-query
128       (lambda (query)
129         (let ((user (query-server-any query 'remote-user))
130               (password (query-server-any query 'remote-passwd)))
131           (let ((status
132                  (if (valid? connection user password "physician")
133                      status-ok
134                      status-unauthorized)))
135             (display-status-&c. status)))))))
136=== About this egg
137
138==== Author
139
140[[/users/klutometis|Peter Danenberg]]
141==== Colophon
142
143Documented by [[/egg/cock|cock]].
Note: See TracBrowser for help on using the repository browser.