]> gitweb.factorcode.org Git - factor.git/blob - basis/urls/urls.factor
urls: simpler url-port.
[factor.git] / basis / urls / urls.factor
1 ! Copyright (C) 2008, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3
4 USING: accessors arrays ascii assocs combinators fry
5 io.pathnames io.sockets io.sockets.secure kernel lexer
6 linked-assocs make math.parser multiline namespaces peg.ebnf
7 present sequences splitting strings strings.parser urls.encoding
8 vocabs.loader ;
9
10 IN: urls
11
12 TUPLE: url protocol username password host port path query anchor ;
13
14 : <url> ( -- url ) url new ;
15
16 : query-param ( url key -- value )
17     swap query>> at ;
18
19 : set-or-delete ( value key query -- )
20     pick [ set-at ] [ delete-at drop ] if ;
21
22 : set-query-param ( url value key -- url )
23     pick query>> [ <linked-hash> ] unless* [ set-or-delete ] keep >>query ;
24
25 : set-query-params ( url params -- url )
26     [ swap set-query-param ] assoc-each ;
27
28 ERROR: malformed-port ;
29
30 : parse-host ( string -- host/f port/f )
31     [
32         ":" split1-last [ url-decode ]
33         [ dup [ string>number [ malformed-port ] unless* ] when ] bi*
34     ] [ f f ] if* ;
35
36 GENERIC: >url ( obj -- url )
37
38 M: f >url drop <url> ;
39
40 M: url >url ;
41
42 <PRIVATE
43
44 EBNF: parse-url [=[
45
46 protocol = [a-zA-Z0-9.+-]+          => [[ url-decode ]]
47 username = [^/:@#?]+                => [[ url-decode ]]
48 password = [^/:@#?]+                => [[ url-decode ]]
49 pathname = [^#?]+                   => [[ url-decode ]]
50 query    = [^#]+                    => [[ query>assoc ]]
51 anchor   = .+                       => [[ url-decode ]]
52
53 hostname = [^/#?]+                  => [[ url-decode ]]
54
55 hostname-spec = hostname ("/"|!(.)) => [[ first ]]
56
57 auth     = (username (":" password  => [[ second ]])? "@"
58                                     => [[ first2 2array ]])?
59
60 url      = (((protocol "://") => [[ first ]] auth hostname)
61                     | (("//") => [[ f ]] auth hostname))?
62            (pathname)?
63            ("?" query               => [[ second ]])?
64            ("#" anchor              => [[ second ]])?
65
66 ]=]
67
68 PRIVATE>
69
70 M: string >url
71     [ <url> ] dip
72     parse-url {
73         [
74             first [
75                 [ first >lower >>protocol ]
76                 [
77                     second
78                     [ first [ first2 [ >>username ] [ >>password ] bi* ] when* ]
79                     [ second parse-host [ >>host ] [ >>port ] bi* ] bi
80                 ] bi
81             ] when*
82         ]
83         [ second >>path ]
84         [ third >>query ]
85         [ fourth >>anchor ]
86     } cleave
87     dup host>> [ [ "/" or ] change-path ] when ;
88
89 M: pathname >url string>> >url ;
90
91 : relative-url ( url -- url' )
92     clone
93         f >>protocol
94         f >>host
95         f >>port ;
96
97 : relative-url? ( url -- ? ) protocol>> not ;
98
99 <PRIVATE
100
101 : unparse-username-password ( url -- )
102     dup username>> dup [
103         % password>> [ ":" % % ] when* "@" %
104     ] [ 2drop ] if ;
105
106 : url-port ( url -- port/f )
107     [ port>> ] [ protocol>> protocol-port ] bi over =
108     [ drop f ] when ;
109
110 : unparse-host-part ( url -- )
111     {
112         [ unparse-username-password ]
113         [ host>> url-encode % ]
114         [ url-port [ ":" % # ] when* ]
115         [ path>> "/" head? [ "/" % ] unless ]
116     } cleave ;
117
118 ! URL" //foo.com" takes on the protocol of the url it's derived from
119 : unparse-protocol ( url -- )
120     dup protocol>> [
121         % "://" % unparse-host-part
122     ] [
123         dup host>> [
124             "//" % unparse-host-part
125         ] [
126             drop
127         ] if
128     ] if* ;
129
130 M: url present
131     [
132         {
133             [ unparse-protocol ]
134             [ path>> url-encode % ]
135             [ query>> dup assoc-empty? [ drop ] [ "?" % assoc>query % ] if ]
136             [ anchor>> [ "#" % present url-encode % ] when* ]
137         } cleave
138     ] "" make ;
139
140 PRIVATE>
141
142 : url-append-path ( path1 path2 -- path )
143     {
144         { [ dup "/" head? ] [ nip ] }
145         { [ dup empty? ] [ drop ] }
146         { [ over "/" tail? ] [ append ] }
147         { [ "/" pick subseq-start not ] [ nip ] }
148         [ [ "/" split1-last drop "/" ] dip 3append ]
149     } cond ;
150
151 <PRIVATE
152
153 : derive-port ( url base -- url' )
154     over relative-url? [ [ port>> ] either? ] [ drop port>> ] if ;
155
156 : derive-path ( url base -- url' )
157     [ path>> ] bi@ swap url-append-path ;
158
159 PRIVATE>
160
161 : derive-url ( base url -- url' )
162     [ clone ] dip over {
163         [ [ protocol>>  ] either? >>protocol ]
164         [ [ username>>  ] either? >>username ]
165         [ [ password>>  ] either? >>password ]
166         [ [ host>>      ] either? >>host ]
167         [ derive-port             >>port ]
168         [ derive-path             >>path ]
169         [ [ query>>     ] either? >>query ]
170         [ [ anchor>>    ] either? >>anchor ]
171     } 2cleave ;
172
173 ! Half-baked stuff follows
174 : secure-protocol? ( protocol -- ? )
175     "https" = ;
176
177 : url-addr ( url -- addr )
178     [
179         [ host>> ]
180         [ port>> ]
181         [ protocol>> protocol-port ]
182         tri or <inet>
183     ] [
184         dup protocol>> secure-protocol?
185         [ host>> <secure> ] [ drop ] if
186     ] bi ;
187
188 : set-url-addr ( url addr -- url )
189     [ host>> >>host ] [ port>> >>port ] bi ;
190
191 : ensure-port ( url -- url' )
192     clone dup protocol>> '[ _ protocol-port or ] change-port ;
193
194 ! Literal syntax
195 SYNTAX: URL" lexer get skip-blank parse-string >url suffix! ;
196
197 { "urls" "prettyprint" } "urls.prettyprint" require-when