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 io.pathnames
5 io.sockets kernel lexer linked-assocs make math.parser namespaces
6 peg.ebnf present sequences splitting strings strings.parser
7 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 : set-or-delete ( val key query -- )
19 pick [ set-at ] [ delete-at drop ] if ;
21 : set-query-param ( url value key -- url )
22 pick query>> [ <linked-hash> ] unless* [ set-or-delete ] keep >>query ;
24 ERROR: malformed-port ;
26 : parse-host ( string -- host/f port/f )
28 ":" split1-last [ url-decode ]
29 [ dup [ string>number [ malformed-port ] unless* ] when ] bi*
32 GENERIC: >url ( obj -- url )
34 M: f >url drop <url> ;
42 protocol = [a-z+]+ => [[ url-decode ]]
43 username = [^/:@#?]+ => [[ url-decode ]]
44 password = [^/:@#?]+ => [[ url-decode ]]
45 pathname = [^#?]+ => [[ url-decode ]]
46 query = [^#]+ => [[ query>assoc ]]
47 anchor = .+ => [[ url-decode ]]
49 hostname = [^/#?]+ => [[ url-decode ]]
51 hostname-spec = hostname ("/"|!(.)) => [[ first ]]
53 auth = (username (":" password => [[ second ]])? "@"
54 => [[ first2 2array ]])?
56 url = (((protocol "://") => [[ first ]] auth hostname)
57 | (("//") => [[ f ]] auth hostname))?
59 ("?" query => [[ second ]])?
60 ("#" anchor => [[ second ]])?
74 [ first [ first2 [ >>username ] [ >>password ] bi* ] when* ]
75 [ second parse-host [ >>host ] [ >>port ] bi* ] bi
83 dup host>> [ [ "/" or ] change-path ] when ;
85 M: pathname >url string>> >url ;
87 : relative-url ( url -- url' )
93 : relative-url? ( url -- ? ) protocol>> not ;
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 -- )
108 [ unparse-username-password ]
109 [ host>> url-encode % ]
110 [ url-port [ ":" % # ] when* ]
111 [ path>> "/" head? [ "/" % ] unless ]
114 ! URL" //foo.com" takes on the protocol of the url it's derived from
115 : unparse-protocol ( url -- )
117 % "://" % unparse-host-part
120 "//" % unparse-host-part
130 [ path>> url-encode % ]
131 [ query>> dup assoc-empty? [ drop ] [ "?" % assoc>query % ] if ]
132 [ anchor>> [ "#" % present url-encode % ] when* ]
138 : url-append-path ( path1 path2 -- path )
140 { [ dup "/" head? ] [ nip ] }
141 { [ dup empty? ] [ drop ] }
142 { [ over "/" tail? ] [ append ] }
143 { [ "/" pick start not ] [ nip ] }
144 [ [ "/" split1-last drop "/" ] dip 3append ]
149 : derive-port ( url base -- url' )
150 over relative-url? [ [ port>> ] either? ] [ drop port>> ] if ;
152 : derive-path ( url base -- url' )
153 [ path>> ] bi@ swap url-append-path ;
157 : derive-url ( base url -- url' )
159 [ [ protocol>> ] either? >>protocol ]
160 [ [ username>> ] either? >>username ]
161 [ [ password>> ] either? >>password ]
162 [ [ host>> ] either? >>host ]
163 [ derive-port >>port ]
164 [ derive-path >>path ]
165 [ [ query>> ] either? >>query ]
166 [ [ anchor>> ] either? >>anchor ]
169 ! Half-baked stuff follows
170 : secure-protocol? ( protocol -- ? )
175 GENERIC: >secure-addr ( addrspec -- addrspec' )
179 : url-addr ( url -- addr )
183 [ protocol>> protocol-port ]
186 secure-protocol? [ >secure-addr ] when ;
188 : set-url-addr ( url addr -- url )
189 [ host>> >>host ] [ port>> >>port ] bi ;
191 : ensure-port ( url -- url' )
192 clone dup protocol>> '[ _ protocol-port or ] change-port ;
195 SYNTAX: URL" lexer get skip-blank parse-short-string >url suffix! ;
197 { "urls" "prettyprint" } "urls.prettyprint" require-when
198 { "urls" "io.sockets.secure" } "urls.secure" require-when