1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: namespaces make assocs sequences kernel classes splitting
4 words vocabs.loader accessors strings combinators arrays
5 continuations present fry urls http http.server xml.syntax xml.writer
6 http.server.redirection http.server.remapping ;
9 : word>string ( word -- string )
10 [ vocabulary>> ] [ name>> ] bi ":" glue ;
12 : words>strings ( seq -- seq' )
15 ERROR: no-such-word name vocab ;
17 : string>word ( string -- word )
18 ":" split1 swap 2dup lookup dup
19 [ 2nip ] [ drop no-such-word ] if ;
21 : strings>words ( seq -- seq' )
24 : nested-responders ( -- seq )
25 responder-nesting get values ;
27 : each-responder ( quot -- )
28 nested-responders swap each ; inline
30 : base-path ( string -- pair )
31 dup responder-nesting get
32 [ second class superclasses [ name>> = ] with any? ] with find nip
33 [ first ] [ "No such responder: " swap append throw ] ?if ;
35 : resolve-base-path ( string -- string' )
38 "/" split1 [ base-path [ "/" % % ] each "/" % ] dip %
42 : vocab-path ( vocab -- path )
43 dup vocab-dir vocab-append-path ;
45 : resolve-template-path ( pair -- path )
47 first2 [ vocabulary>> vocab-path % ] [ "/" % % ] bi*
50 GENERIC: modify-query ( query responder -- query' )
52 M: object modify-query drop ;
54 GENERIC: modify-redirect-query ( query responder -- query' )
56 M: object modify-redirect-query drop ;
58 GENERIC: adjust-url ( url -- url' )
62 [ [ modify-query ] each-responder ] change-query
63 [ resolve-base-path ] change-path
66 M: string adjust-url ;
68 GENERIC: adjust-redirect-url ( url -- url' )
70 M: url adjust-redirect-url
72 [ [ modify-redirect-query ] each-responder ] change-query ;
74 M: string adjust-redirect-url ;
76 GENERIC: link-attr ( tag responder -- )
78 M: object link-attr 2drop ;
80 GENERIC: modify-form ( responder -- xml/f )
82 M: object modify-form drop f ;
84 : form-modifications ( -- xml )
85 [ [ modify-form [ , ] when* ] each-responder ] { } make ;
87 : hidden-form-field ( value name -- xml )
89 [XML <input type="hidden" value=<-> name=<->/> XML]
92 CONSTANT: nested-forms-key "__n"
94 : referrer ( -- referrer/f )
95 #! Typo is intentional, it's in the HTTP spec!
96 "referer" request get header>> at
97 dup [ >url ensure-port [ remap-port ] change-port ] when ;
99 : user-agent ( -- user-agent )
100 "user-agent" request get header>> at "" or ;
102 : same-host? ( url -- ? )
107 [ port>> remap-port ]
112 : cookie-client-state ( key request -- value/f )
113 swap get-cookie dup [ value>> ] when ;
115 : post-client-state ( key request -- value/f )
118 : client-state ( key -- value/f )
119 request get dup method>> {
120 { "GET" [ cookie-client-state ] }
121 { "HEAD" [ cookie-client-state ] }
122 { "POST" [ post-client-state ] }
125 SYMBOL: exit-continuation
127 : exit-with ( value -- * )
128 exit-continuation get continue-with ;
130 : with-exit-continuation ( quot -- value )
131 '[ exit-continuation set @ ] callcc1 exit-continuation off ; inline