1 ! Copyright (C) 2008, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors arrays assocs combinators fry hashtables
5 io.pathnames io.sockets kernel lexer make math.parser
6 namespaces peg.ebnf present sequences splitting strings
7 strings.parser urls.encoding vocabs.loader ;
11 TUPLE: url protocol username password host port path query anchor ;
13 : <url> ( -- url ) url new ;
15 : query-param ( url key -- value )
18 : delete-query-param ( url key -- url )
19 over query>> delete-at ;
21 : set-query-param ( url value key -- url )
23 '[ [ _ _ ] dip ?set-at ] change-query
25 nip delete-query-param
28 ERROR: malformed-port ;
30 : parse-host ( string -- host/f port/f )
32 ":" split1-last [ url-decode ]
33 [ dup [ string>number [ malformed-port ] unless* ] when ] bi*
36 GENERIC: >url ( obj -- url )
38 M: f >url drop <url> ;
46 protocol = [a-z]+ => [[ url-decode ]]
47 username = [^/:@#?]+ => [[ url-decode ]]
48 password = [^/:@#?]+ => [[ url-decode ]]
49 pathname = [^#?]+ => [[ url-decode ]]
50 query = [^#]+ => [[ query>assoc ]]
51 anchor = .+ => [[ url-decode ]]
53 hostname = [^/#?]+ => [[ url-decode ]]
55 hostname-spec = hostname ("/"|!(.)) => [[ first ]]
57 auth = (username (":" password => [[ second ]])? "@"
58 => [[ first2 2array ]])?
60 url = ((protocol "://") => [[ first ]] auth hostname)?
62 ("?" query => [[ second ]])?
63 ("#" anchor => [[ second ]])?
77 [ first [ first2 [ >>username ] [ >>password ] bi* ] when* ]
78 [ second parse-host [ >>host ] [ >>port ] bi* ] bi
86 dup host>> [ [ "/" or ] change-path ] when ;
88 M: pathname >url string>> >url ;
90 : relative-url ( url -- url' )
96 : relative-url? ( url -- ? ) protocol>> not ;
100 : unparse-username-password ( url -- )
102 % password>> [ ":" % % ] when* "@" %
105 : url-port ( url -- port/f )
106 [ port>> ] [ port>> ] [ protocol>> protocol-port ] tri =
109 : unparse-host-part ( url protocol -- )
113 [ unparse-username-password ]
114 [ host>> url-encode % ]
115 [ url-port [ ":" % # ] when* ]
116 [ path>> "/" head? [ "/" % ] unless ]
122 [ dup protocol>> dup [ unparse-host-part ] [ 2drop ] if ]
123 [ path>> url-encode % ]
124 [ query>> dup assoc-empty? [ drop ] [ "?" % assoc>query % ] if ]
125 [ anchor>> [ "#" % present url-encode % ] when* ]
131 : url-append-path ( path1 path2 -- path )
133 { [ dup "/" head? ] [ nip ] }
134 { [ dup empty? ] [ drop ] }
135 { [ over "/" tail? ] [ append ] }
136 { [ "/" pick start not ] [ nip ] }
137 [ [ "/" split1-last drop "/" ] dip 3append ]
142 : derive-port ( url base -- url' )
143 over relative-url? [ [ port>> ] either? ] [ drop port>> ] if ;
145 : derive-path ( url base -- url' )
146 [ path>> ] bi@ swap url-append-path ;
150 : derive-url ( base url -- url' )
152 [ [ protocol>> ] either? >>protocol ]
153 [ [ username>> ] either? >>username ]
154 [ [ password>> ] either? >>password ]
155 [ [ host>> ] either? >>host ]
156 [ derive-port >>port ]
157 [ derive-path >>path ]
158 [ [ query>> ] either? >>query ]
159 [ [ anchor>> ] either? >>anchor ]
162 ! Half-baked stuff follows
163 : secure-protocol? ( protocol -- ? )
168 GENERIC: >secure-addr ( addrspec -- addrspec' )
172 : url-addr ( url -- addr )
176 [ protocol>> protocol-port ]
179 secure-protocol? [ >secure-addr ] when ;
181 : set-url-addr ( url addr -- url )
182 [ host>> >>host ] [ port>> >>port ] bi ;
184 : ensure-port ( url -- url' )
185 clone dup protocol>> '[ _ protocol-port or ] change-port ;
188 SYNTAX: URL" lexer get skip-blank parse-string >url suffix! ;
190 { "urls" "prettyprint" } "urls.prettyprint" require-when
191 { "urls" "io.sockets.secure" } "urls.secure" require-when