1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel ascii combinators combinators.short-circuit
4 sequences splitting fry namespaces make assocs arrays strings
5 io.sockets io.encodings.string io.encodings.utf8 math
6 math.parser accessors parser strings.parser lexer
7 hashtables present peg.ebnf urls.encoding ;
10 TUPLE: url protocol username password host port path query anchor ;
12 : <url> ( -- url ) url new ;
14 : query-param ( url key -- value )
17 : delete-query-param ( url key -- url )
18 over query>> delete-at ;
20 : set-query-param ( url value key -- url )
22 '[ [ _ _ ] dip ?set-at ] change-query
24 nip delete-query-param
27 : parse-host ( string -- host port )
28 ":" split1 [ url-decode ] [
31 dup [ "Invalid port" throw ] unless
35 GENERIC: >url ( obj -- url )
37 M: f >url drop <url> ;
45 protocol = [a-z]+ => [[ url-decode ]]
46 username = [^/:@#?]+ => [[ url-decode ]]
47 password = [^/:@#?]+ => [[ url-decode ]]
48 pathname = [^#?]+ => [[ url-decode ]]
49 query = [^#]+ => [[ query>assoc ]]
50 anchor = .+ => [[ url-decode ]]
52 hostname = [^/#?]+ => [[ url-decode ]]
54 hostname-spec = hostname ("/"|!(.)) => [[ first ]]
56 auth = (username (":" password => [[ second ]])? "@"
57 => [[ first2 2array ]])?
59 url = ((protocol "://") => [[ first ]] auth hostname)?
61 ("?" query => [[ second ]])?
62 ("#" anchor => [[ second ]])?
75 [ first [ first2 ] [ f f ] if* ] ! username, password
76 [ second parse-host ] ! host, port
85 dup host>> [ [ "/" or ] change-path ] when ;
87 : protocol-port ( protocol -- port )
97 : unparse-username-password ( url -- )
99 % password>> [ ":" % % ] when* "@" %
102 : url-port ( url -- port/f )
103 [ port>> ] [ port>> ] [ protocol>> protocol-port ] tri =
106 : unparse-host-part ( url protocol -- )
110 [ unparse-username-password ]
111 [ host>> url-encode % ]
112 [ url-port [ ":" % # ] when* ]
113 [ path>> "/" head? [ "/" % ] unless ]
121 [ dup protocol>> dup [ unparse-host-part ] [ 2drop ] if ]
122 [ path>> url-encode % ]
123 [ query>> dup assoc-empty? [ drop ] [ "?" % assoc>query % ] if ]
124 [ anchor>> [ "#" % present url-encode % ] when* ]
128 : url-append-path ( path1 path2 -- path )
130 { [ dup "/" head? ] [ nip ] }
131 { [ dup empty? ] [ drop ] }
132 { [ over "/" tail? ] [ append ] }
133 { [ "/" pick start not ] [ nip ] }
134 [ [ "/" split1-last drop "/" ] dip 3append ]
139 : derive-url ( base url -- url' )
141 [ [ protocol>> ] either? >>protocol ]
142 [ [ username>> ] either? >>username ]
143 [ [ password>> ] either? >>password ]
144 [ [ host>> ] either? >>host ]
145 [ [ port>> ] either? >>port ]
146 [ [ path>> ] bi@ swap url-append-path >>path ]
147 [ [ query>> ] either? >>query ]
148 [ [ anchor>> ] either? >>anchor ]
151 : relative-url ( url -- url' )
157 : relative-url? ( url -- ? ) protocol>> not ;
159 ! Half-baked stuff follows
160 : secure-protocol? ( protocol -- ? )
165 GENERIC: >secure-addr ( addrspec -- addrspec' )
169 : url-addr ( url -- addr )
173 [ protocol>> protocol-port ]
176 secure-protocol? [ >secure-addr ] when ;
178 : ensure-port ( url -- url' )
179 clone dup protocol>> '[ _ protocol-port or ] change-port ;
182 SYNTAX: URL" lexer get skip-blank parse-string >url parsed ;
184 USING: vocabs vocabs.loader ;
186 "prettyprint" vocab [
187 "urls.prettyprint" require