1 ! Copyright (C) 2008, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays ascii assocs combinators fry io.pathnames
4 io.sockets io.sockets.secure kernel lexer linked-assocs make
5 math.parser namespaces peg.ebnf present sequences splitting
6 strings strings.parser urls.encoding vocabs.loader ;
9 TUPLE: url protocol username password host port path query anchor ;
11 : <url> ( -- url ) url new ;
13 : query-param ( url key -- value )
16 : set-or-delete ( val key query -- )
17 pick [ set-at ] [ delete-at drop ] if ;
19 : set-query-param ( url value key -- url )
20 pick query>> [ <linked-hash> ] unless* [ set-or-delete ] keep >>query ;
22 ERROR: malformed-port ;
24 : parse-host ( string -- host/f port/f )
26 ":" split1-last [ url-decode ]
27 [ dup [ string>number [ malformed-port ] unless* ] when ] bi*
30 GENERIC: >url ( obj -- url )
32 M: f >url drop <url> ;
40 protocol = [a-zA-Z0-9.+-]+ => [[ url-decode ]]
41 username = [^/:@#?]+ => [[ url-decode ]]
42 password = [^/:@#?]+ => [[ url-decode ]]
43 pathname = [^#?]+ => [[ url-decode ]]
44 query = [^#]+ => [[ query>assoc ]]
45 anchor = .+ => [[ url-decode ]]
47 hostname = [^/#?]+ => [[ url-decode ]]
49 hostname-spec = hostname ("/"|!(.)) => [[ first ]]
51 auth = (username (":" password => [[ second ]])? "@"
52 => [[ first2 2array ]])?
54 url = (((protocol "://") => [[ first ]] auth hostname)
55 | (("//") => [[ f ]] auth hostname))?
57 ("?" query => [[ second ]])?
58 ("#" anchor => [[ second ]])?
69 [ first >lower >>protocol ]
72 [ first [ first2 [ >>username ] [ >>password ] bi* ] when* ]
73 [ second parse-host [ >>host ] [ >>port ] bi* ] bi
81 dup host>> [ [ "/" or ] change-path ] when ;
83 M: pathname >url string>> >url ;
85 : relative-url ( url -- url' )
91 : relative-url? ( url -- ? ) protocol>> not ;
95 : unparse-username-password ( url -- )
97 % password>> [ ":" % % ] when* "@" %
100 : url-port ( url -- port/f )
101 [ port>> ] [ port>> ] [ protocol>> protocol-port ] tri =
104 : unparse-host-part ( url -- )
106 [ unparse-username-password ]
107 [ host>> url-encode % ]
108 [ url-port [ ":" % # ] when* ]
109 [ path>> "/" head? [ "/" % ] unless ]
112 ! URL" //foo.com" takes on the protocol of the url it's derived from
113 : unparse-protocol ( url -- )
115 % "://" % unparse-host-part
118 "//" % unparse-host-part
128 [ path>> url-encode % ]
129 [ query>> dup assoc-empty? [ drop ] [ "?" % assoc>query % ] if ]
130 [ anchor>> [ "#" % present url-encode % ] when* ]
136 : url-append-path ( path1 path2 -- path )
138 { [ dup "/" head? ] [ nip ] }
139 { [ dup empty? ] [ drop ] }
140 { [ over "/" tail? ] [ append ] }
141 { [ "/" pick start not ] [ nip ] }
142 [ [ "/" split1-last drop "/" ] dip 3append ]
147 : derive-port ( url base -- url' )
148 over relative-url? [ [ port>> ] either? ] [ drop port>> ] if ;
150 : derive-path ( url base -- url' )
151 [ path>> ] bi@ swap url-append-path ;
155 : derive-url ( base url -- url' )
157 [ [ protocol>> ] either? >>protocol ]
158 [ [ username>> ] either? >>username ]
159 [ [ password>> ] either? >>password ]
160 [ [ host>> ] either? >>host ]
161 [ derive-port >>port ]
162 [ derive-path >>path ]
163 [ [ query>> ] either? >>query ]
164 [ [ anchor>> ] either? >>anchor ]
167 ! Half-baked stuff follows
168 : secure-protocol? ( protocol -- ? )
171 : url-addr ( url -- addr )
175 [ protocol>> protocol-port ]
178 dup protocol>> secure-protocol?
179 [ host>> <secure> ] [ drop ] if
182 : set-url-addr ( url addr -- url )
183 [ host>> >>host ] [ port>> >>port ] bi ;
185 : ensure-port ( url -- url' )
186 clone dup protocol>> '[ _ protocol-port or ] change-port ;
189 SYNTAX: URL" lexer get skip-blank parse-string >url suffix! ;
191 { "urls" "prettyprint" } "urls.prettyprint" require-when