]> gitweb.factorcode.org Git - factor.git/blob - basis/urls/urls.factor
urls: allow pathnames to convert >url.
[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 [ 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            (pathname)?
62            ("?" query               => [[ second ]])?
63            ("#" anchor              => [[ second ]])?
64
65 ;EBNF
66
67 PRIVATE>
68
69 M: string >url
70     [ <url> ] dip
71     parse-url {
72         [
73             first [
74                 [ first >>protocol ]
75                 [
76                     second
77                     [ first [ first2 [ >>username ] [ >>password ] bi* ] when* ]
78                     [ second parse-host [ >>host ] [ >>port ] bi* ] bi
79                 ] bi
80             ] when*
81         ]
82         [ second >>path ]
83         [ third >>query ]
84         [ fourth >>anchor ]
85     } cleave
86     dup host>> [ [ "/" or ] change-path ] when ;
87
88 M: pathname >url string>> >url ;
89
90 : protocol-port ( protocol -- port )
91     {
92         { "http" [ 80 ] }
93         { "https" [ 443 ] }
94         { "ftp" [ 21 ] }
95         [ drop f ]
96     } case ;
97
98 : relative-url ( url -- url' )
99     clone
100         f >>protocol
101         f >>host
102         f >>port ;
103
104 : relative-url? ( url -- ? ) protocol>> not ;
105
106 <PRIVATE
107
108 : unparse-username-password ( url -- )
109     dup username>> dup [
110         % password>> [ ":" % % ] when* "@" %
111     ] [ 2drop ] if ;
112
113 : url-port ( url -- port/f )
114     [ port>> ] [ port>> ] [ protocol>> protocol-port ] tri =
115     [ drop f ] when ;
116
117 : unparse-host-part ( url protocol -- )
118     %
119     "://" %
120     {
121         [ unparse-username-password ]
122         [ host>> url-encode % ]
123         [ url-port [ ":" % # ] when* ]
124         [ path>> "/" head? [ "/" % ] unless ]
125     } cleave ;
126
127 M: url present
128     [
129         {
130             [ dup protocol>> dup [ unparse-host-part ] [ 2drop ] if ]
131             [ path>> url-encode % ]
132             [ query>> dup assoc-empty? [ drop ] [ "?" % assoc>query % ] if ]
133             [ anchor>> [ "#" % present url-encode % ] when* ]
134         } cleave
135     ] "" make ;
136
137 PRIVATE>
138
139 : url-append-path ( path1 path2 -- path )
140     {
141         { [ dup "/" head? ] [ nip ] }
142         { [ dup empty? ] [ drop ] }
143         { [ over "/" tail? ] [ append ] }
144         { [ "/" pick start not ] [ nip ] }
145         [ [ "/" split1-last drop "/" ] dip 3append ]
146     } cond ;
147
148 <PRIVATE
149
150 : derive-port ( url base -- url' )
151     over relative-url? [ [ port>> ] either? ] [ drop port>> ] if ;
152
153 : derive-path ( url base -- url' )
154     [ path>> ] bi@ swap url-append-path ;
155
156 PRIVATE>
157
158 : derive-url ( base url -- url' )
159     [ clone ] dip over {
160         [ [ protocol>>  ] either? >>protocol ]
161         [ [ username>>  ] either? >>username ]
162         [ [ password>>  ] either? >>password ]
163         [ [ host>>      ] either? >>host ]
164         [ derive-port             >>port ]
165         [ derive-path             >>path ]
166         [ [ query>>     ] either? >>query ]
167         [ [ anchor>>    ] either? >>anchor ]
168     } 2cleave ;
169
170 ! Half-baked stuff follows
171 : secure-protocol? ( protocol -- ? )
172     "https" = ;
173
174 <PRIVATE
175
176 GENERIC: >secure-addr ( addrspec -- addrspec' )
177
178 PRIVATE>
179
180 : url-addr ( url -- addr )
181     [
182         [ host>> ]
183         [ port>> ]
184         [ protocol>> protocol-port ]
185         tri or <inet>
186     ] [ protocol>> ] bi
187     secure-protocol? [ >secure-addr ] when ;
188
189 : set-url-addr ( url addr -- url )
190     [ host>> >>host ] [ port>> >>port ] bi ;
191
192 : ensure-port ( url -- url' )
193     clone dup protocol>> '[ _ protocol-port or ] change-port ;
194
195 ! Literal syntax
196 SYNTAX: URL" lexer get skip-blank parse-string >url suffix! ;
197
198 { "urls" "prettyprint" } "urls.prettyprint" require-when
199 { "urls" "io.sockets.secure" } "urls.secure" require-when