Hello everybody,
I would like to extend the example given in section 7.3.10.2 of the
guile-manual. I am trying to write a web handler procedure which shall
inspect whether or not a request can be found in a list that represents
items of a navigation bar.
In order to understand what guile’s web module is actually doing I wrote
this code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-modules (sxml simple)
(web uri)
(web request)
(web response)
(web server))
(define *navigation* '("start"
"gallery"))
(define *uri-1* (build-uri 'http
#:host "localhost"
#:port 8080
#:path "/start"))
(define *uri-2* (build-uri 'http
#:host "localhost"
#:port 8080
#:path "/gallery"))
(define *uri-foo* (build-uri 'http
#:host "localhost"
#:port 8080
#:path "/foo"))
(define *request-1* (build-request *uri-1*))
(define *request-2* (build-request *uri-2*))
(define *request-foo* (build-request *uri-foo*))
(define (request-path-components request)
(split-and-decode-uri-path (uri-path (request-uri request))))
(define (request-included-in-nav-bar? request)
(if (member (car (request-path-components request))
*navigation*)
#t #f))
(define (page request)
(string->symbol
(string-append "*" (car (request-path-components request)) "*")))
(define (respond-test page)
(begin
(display "Appearing in browser:")
(newline)
(display page)
(newline)))
(define (not-found-test request)
(begin
(display "Appearing in browser:")
(newline)
(display "Page not found: ")
(display (car (request-path-components request)))
(newline)))
(define (handler-test request)
(if (request-included-in-nav-bar? request)
(respond-test (page request))
(not-found-test request)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
The definitons are intended to be a kind of a simulation of a web-server
and work fine so far, e.g.:
------
scheme@(guile-user)> (request-included-in-nav-bar? *request-1*)
$2 = #t
scheme@(guile-user)> (request-included-in-nav-bar? *request-foo*)
$3 = #f
scheme@(guile-user)> (handler-test *request-2*)
Appearing in browser:
*gallery*
scheme@(guile-user)> (handler-test *request-foo*)
Appearing in browser:
Page not found: foo
-----
In the real world things seem to be different of course... Consider the
following definitions to be used together with those above and the
procedures »templatize«, »not-found« and »respond« as defined in section
7.3.10.2/3:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define *start*
'((h1 "Homepage ...")))
(define *gallery*
'((h1 "Gallery ...")))
(define (homepage request body)
(if (request-included-in-nav-bar? request)
(respond (page request))
(not-found request)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Starting the web-server and pointing the browser to localhost:8080/start
yields an error:
------
scheme@(guile-user)> (run-server homepage)
<unnamed port>:36:14: In procedure request-included-in-nav-bar?:
<unnamed port>:36:14: In procedure car: Wrong type argument in position
1 (expecting pair): ()
Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]> ,bt
In current input:
142:6 1 (homepage #<<request> method: GET uri: #<<uri> scheme:
http us…> …)
36:14 0 (request-included-in-nav-bar? #<<request> method: GET uri:
#<<u…>)
scheme@(guile-user) [1]> ,q
;;; WARNING (Error handling request wrong-type-arg (car Wrong type
argument in position ~A (expecting ~A): ~S (1 pair ()) (())))
<unnamed port>:36:14: In procedure request-included-in-nav-bar?:
<unnamed port>:36:14: In procedure car: Wrong type argument in position
1 (expecting pair): ()
Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
--------
While working fine above, my predicate »request-included-in-nav-bar?«
now breaks: The expression (request-path-components request) seems to
evaluate to anything but ("start"). What am I doing wrong?
Thank you very much in advance for your help.
--
Dr. Ludwig Meier