* (web server): Trying to define a predicate »request-included-in-nav-bar?«
@ 2012-09-21 1:47 Dr. Ludwig Meier
2012-09-23 18:32 ` Mark H Weaver
0 siblings, 1 reply; 2+ messages in thread
From: Dr. Ludwig Meier @ 2012-09-21 1:47 UTC (permalink / raw)
To: guile-user
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: (web server): Trying to define a predicate »request-included-in-nav-bar?«
2012-09-21 1:47 (web server): Trying to define a predicate »request-included-in-nav-bar?« Dr. Ludwig Meier
@ 2012-09-23 18:32 ` Mark H Weaver
0 siblings, 0 replies; 2+ messages in thread
From: Mark H Weaver @ 2012-09-23 18:32 UTC (permalink / raw)
To: Dr. Ludwig Meier; +Cc: guile-user
"Dr. Ludwig Meier" <ludwig.meier@ymail.com> writes:
> 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.
[...]
> (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))
Here, and in multiple other places, you evaluate:
(car (request-path-components request))
which will raise an exception unless there is at least one non-empty
path component. Somehow you need to cope with the possibility of an
empty path, e.g.:
(define (request-included-in-nav-bar? request)
(let ((components (request-path-components request)))
(and (pair? components)
(member (car components) *navigation*)
#t)))
> 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): ()
[...]
> 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?
In this case, (request-path-components request) returned the empty list,
which implies that the URI path contains no non-empty path components.
This suggests that perhaps "http://localhost:8080/" was requested for
some reason.
Even if you don't expect such requests, I recommend that you fix your
code to properly handle them. Modern web browsers typically generate
additional requests that the user did not ask for. For example, they
will typically request the "favicon" for display beside the URL in the
navigation bar.
You might also want to add some debugging output to your program that
shows what URLs are being requested. If you suspect that Guile's web
server is doing something wrong, then you could try using a simpler web
browser such as "lynx" or "w3m", or even telnetting directly to port
8080 and performing the HTTP request manually.
Mark
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2012-09-23 18:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-21 1:47 (web server): Trying to define a predicate »request-included-in-nav-bar?« Dr. Ludwig Meier
2012-09-23 18:32 ` Mark H Weaver
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).