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