]> gitweb.factorcode.org Git - factor.git/blob - basis/urls/urls.factor
basis: ERROR: changes.
[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 assocs combinators fry hashtables
5 io.pathnames io.sockets kernel lexer make math.parser
6 namespaces peg.ebnf present sequences splitting strings
7 strings.parser urls.encoding vocabs.loader ;
8
9 IN: urls
10
11 TUPLE: url protocol username password host port path query anchor ;
12
13 : <url> ( -- url ) url new ;
14
15 : query-param ( url key -- value )
16     swap query>> at ;
17
18 : delete-query-param ( url key -- url )
19     over query>> delete-at ;
20
21 : set-query-param ( url value key -- url )
22     over [
23         '[ [ _ _ ] dip ?set-at ] change-query
24     ] [
25         nip delete-query-param
26     ] if ;
27
28 ERROR: malformed-port ;
29
30 : parse-host ( string -- host/f port/f )
31     [
32         ":" split1-last [ url-decode ]
33         [ dup [ string>number [ throw-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-z+]+                   => [[ 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 ;EBNF
67
68 PRIVATE>
69
70 M: string >url
71     [ <url> ] dip
72     parse-url {
73         [
74             first [
75                 [ first >>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>> ] [ port>> ] [ protocol>> protocol-port ] tri =
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 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 <PRIVATE
178
179 GENERIC: >secure-addr ( addrspec -- addrspec' )
180
181 PRIVATE>
182
183 : url-addr ( url -- addr )
184     [
185         [ host>> ]
186         [ port>> ]
187         [ protocol>> protocol-port ]
188         tri or <inet>
189     ] [ protocol>> ] bi
190     secure-protocol? [ >secure-addr ] when ;
191
192 : set-url-addr ( url addr -- url )
193     [ host>> >>host ] [ port>> >>port ] bi ;
194
195 : ensure-port ( url -- url' )
196     clone dup protocol>> '[ _ protocol-port or ] change-port ;
197
198 ! Literal syntax
199 SYNTAX: URL" lexer get skip-blank parse-string >url suffix! ;
200
201 { "urls" "prettyprint" } "urls.prettyprint" require-when
202 { "urls" "io.sockets.secure" } "urls.secure" require-when