1 ! Copyright (C) 2008, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors ascii assocs combinators fry io.pathnames
5 io.sockets io.sockets.secure kernel lexer linked-assocs make
6 math.parser multiline namespaces peg.ebnf present sequences
7 sequences.generalizations splitting strings strings.parser
8 urls.encoding vocabs.loader ;
12 TUPLE: url protocol username password host port path query anchor ;
14 : <url> ( -- url ) url new ;
16 : query-param ( url key -- value )
19 : set-or-delete ( value key query -- )
20 pick [ set-at ] [ delete-at drop ] if ;
22 : set-query-param ( url value key -- url )
23 pick query>> [ <linked-hash> ] unless* [ set-or-delete ] keep >>query ;
25 : set-query-params ( url params -- url )
26 [ swap set-query-param ] assoc-each ;
28 ERROR: malformed-port ;
30 : parse-port ( string -- port/f )
31 [ f ] [ string>number [ malformed-port ] unless* ] if-empty ;
33 : parse-host ( string -- host/f port/f )
35 ":" split1-last [ url-decode ] [ parse-port ] bi*
38 GENERIC: >url ( obj -- url )
40 M: f >url drop <url> ;
48 protocol = [a-zA-Z0-9.+-]+ => [[ url-decode ]]
49 username = [^/:@#?]* => [[ url-decode ]]
50 password = [^/:@#?]* => [[ url-decode ]]
51 path = [^#?]+ => [[ url-decode ]]
52 query = [^#]+ => [[ query>assoc ]]
53 anchor = .+ => [[ url-decode ]]
54 hostname = [^/#?:]+ => [[ url-decode ]]
55 port = [^/#?]+ => [[ url-decode parse-port ]]
57 auth = username (":"~ password?)? "@"~
58 host = hostname (":"~ port?)?
60 url = (protocol ":"~)?
71 [ <url> ] dip parse-url 5 firstn {
75 [ first [ first2 [ >>username ] [ >>password ] bi* ] when* ]
76 [ second [ first2 [ >>host ] [ >>port ] bi* ] when* ] bi
82 } spread dup host>> [ [ "/" or ] change-path ] when ;
84 M: pathname >url string>> >url ;
86 : relative-url ( url -- url' )
92 : relative-url? ( url -- ? ) protocol>> not ;
96 : unparse-username-password ( url -- )
98 % password>> [ ":" % % ] when* "@" %
101 : url-port ( url -- port/f )
102 [ port>> ] [ protocol>> protocol-port ] bi over =
105 : unparse-host-part ( url -- )
107 [ unparse-username-password ]
108 [ host>> url-encode % ]
109 [ url-port [ ":" % # ] when* ]
110 [ path>> "/" head? [ "/" % ] unless ]
113 ! URL" //foo.com" takes on the protocol of the url it's derived from
114 : unparse-protocol ( url -- )
115 protocol>> [ % ":" % ] when* ;
117 : unparse-authority ( url -- )
118 dup host>> [ "//" % unparse-host-part ] [ drop ] if ;
124 [ unparse-authority ]
125 [ path>> url-encode % ]
126 [ query>> dup assoc-empty? [ drop ] [ "?" % assoc>query % ] if ]
127 [ anchor>> [ "#" % present url-encode % ] when* ]
133 : url-append-path ( path1 path2 -- path )
135 { [ dup "/" head? ] [ nip ] }
136 { [ dup empty? ] [ drop ] }
137 { [ over "/" tail? ] [ append ] }
138 { [ "/" pick subseq-start not ] [ nip ] }
139 [ [ "/" split1-last drop "/" ] dip 3append ]
144 : derive-port ( url base -- url' )
145 over relative-url? [ [ port>> ] either? ] [ drop port>> ] if ;
147 : derive-path ( url base -- url' )
148 [ path>> ] bi@ swap url-append-path ;
152 : derive-url ( base url -- url' )
154 [ [ protocol>> ] either? >>protocol ]
155 [ [ username>> ] either? >>username ]
156 [ [ password>> ] either? >>password ]
157 [ [ host>> ] either? >>host ]
158 [ derive-port >>port ]
159 [ derive-path >>path ]
160 [ [ query>> ] either? >>query ]
161 [ [ anchor>> ] either? >>anchor ]
164 : redacted-url ( url -- url' )
165 clone [ "xxxxx" and ] change-password ;
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