1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel ascii combinators combinators.short-circuit
4 sequences splitting fry namespaces make assocs arrays strings
5 io.encodings.string io.encodings.utf8 math math.parser accessors
9 : url-quotable? ( ch -- ? )
21 [ CHAR: % , >hex 2 CHAR: 0 pad-left % ] each ;
25 : url-encode ( str -- encoded )
27 [ dup url-quotable? [ , ] [ push-utf8 ] if ] each
32 : url-decode-hex ( index str -- )
36 [ 1+ dup 2 + ] dip subseq hex> [ , ] when*
39 : url-decode-% ( index str -- index str )
42 : url-decode-iter ( index str -- )
46 2dup nth dup CHAR: % = [
47 drop url-decode-% [ 3 + ] dip
55 : url-decode ( str -- decoded )
56 [ 0 swap url-decode-iter ] "" make utf8 decode ;
58 : query-decode ( str -- decoded )
59 [ dup CHAR: + = [ drop "%20" ] [ 1string ] if ] { } map-as
64 : add-query-param ( value key assoc -- )
68 { [ dup string? ] [ swap 2array ] }
69 { [ dup array? ] [ swap suffix ] }
70 { [ dup not ] [ drop ] }
77 : query>assoc ( query -- assoc )
79 "&;" split H{ } clone [
81 [ "=" split1 [ dup [ query-decode ] when ] bi@ swap ] dip
87 : assoc>query ( assoc -- str )
89 dup array? [ [ present ] map ] [ present 1array ] if
94 [ url-encode "=" swap 3append , ] with each