1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: classes help.markup help.syntax io.streams.string ;
4 IN: http.server.dispatchers
7 { $values { "class" class } { "dispatcher" dispatcher } }
8 { $description "Creates a new instance of a subclass of " { $link dispatcher } "." } ;
11 { $description "The class of dispatchers. May be subclassed, in which case subclasses should be constructed by calling " { $link new-dispatcher } "." } ;
14 { $values { "dispatcher" dispatcher } }
15 { $description "Creates a new pathname dispatcher." } ;
17 HELP: vhost-dispatcher
18 { $description "The class of virtual host dispatchers." } ;
20 HELP: <vhost-dispatcher>
21 { $values { "dispatcher" vhost-dispatcher } }
22 { $description "Creates a new virtual host dispatcher." } ;
26 { "dispatcher" dispatcher } { "responder" "a responder" } { "path" "a pathname string or hostname" } }
27 { $description "Adds a responder to a dispatcher." }
28 { $notes "The " { $snippet "path" } " parameter is interpreted differently depending on the dispatcher type." }
29 { $side-effects "dispatcher" } ;
31 ARTICLE: "http.server.dispatchers.example" "HTTP dispatcher examples"
32 { $heading "Simple pathname dispatcher" }
35 <new-action> "new" add-responder
36 <edit-action> "edit" add-responder
37 <delete-action> "delete" add-responder
38 <list-action> "" add-responder
39 main-responder set-global"""
41 "In the above example, visiting any URL other than " { $snippet "/new" } ", " { $snippet "/edit" } ", " { $snippet "/delete" } ", or " { $snippet "/" } " will result in a 404 error."
42 { $heading "Another pathname dispatcher" }
43 "On the other hand, suppose we wanted to route all unrecognized paths to a “view” action:"
46 <new-action> "new" add-responder
47 <edit-action> "edit" add-responder
48 <delete-action> "delete" add-responder
49 <view-action> >>default
50 main-responder set-global"""
52 "The " { $slot "default" } " slot holds a responder to which all unrecognized paths are sent to."
53 { $heading "Dispatcher subclassing example" }
55 """TUPLE: golf-courses < dispatcher ;
57 : <golf-courses> ( -- golf-courses )
58 golf-courses new-dispatcher ;
61 <new-action> "new" add-responder
62 <edit-action> "edit" add-responder
63 <delete-action> "delete" add-responder
64 <list-action> "" add-responder
65 main-responder set-global"""
67 "The action templates can now emit links to responder-relative URLs prefixed by " { $snippet "$golf-courses/" } "."
68 { $heading "Virtual hosting example" }
71 <casino> "concatenative-casino.com" add-responder
72 <dating> "raptor-dating.com" add-responder
73 main-responder set-global"""
75 "Note that the virtual host dispatcher strips off a " { $snippet "www." } " prefix, so " { $snippet "www.concatenative-casino.com" } " would be routed to the " { $snippet "<casino>" } " responder instead of receiving a 404." ;
77 ARTICLE: "http.server.dispatchers" "HTTP dispatchers and virtual hosting"
78 "The " { $vocab-link "http.server.dispatchers" } " vocabulary implements two responders which route HTTP requests to one or more child responders."
79 { $subsections "http.server.dispatchers.example" }
80 "Pathname dispatchers implement a directory hierarchy where each subdirectory is its own responder:"
85 "Virtual host dispatchers dispatch each virtual host to a different responder:"
90 "Adding responders to dispatchers:"
91 { $subsections add-responder }
92 "The " { $slot "default" } " slot holds a responder which receives all unrecognized URLs. By default, it responds with 404 messages." ;
94 ABOUT: "http.server.dispatchers"