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

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

Docs for call-with-query

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