1 ! Copyright (C) 2003, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: hashtables io kernel math namespaces math.parser assocs
4 sequences strings splitting ;
7 : header-line ( line -- )
8 ": " split1 dup [ swap set ] [ 2drop ] if ;
10 : (read-header) ( -- )
12 empty? [ drop ] [ header-line (read-header) ] if ;
14 : read-header ( -- hash )
15 [ (read-header) ] H{ } make-assoc ;
17 : url-quotable? ( ch -- ? )
18 #! In a URL, can this character be used without
23 swap "/_-?." member? or ; foldable
25 : url-encode ( str -- str )
31 CHAR: % , >hex 2 CHAR: 0 pad-left %
36 : url-decode-hex ( index str -- )
40 >r 1+ dup 2 + r> subseq hex> [ , ] when*
43 : url-decode-% ( index str -- index str )
44 2dup url-decode-hex >r 3 + r> ;
46 : url-decode-+-or-other ( index str ch -- index str )
47 dup CHAR: + = [ drop CHAR: \s ] when , >r 1+ r> ;
49 : url-decode-iter ( index str -- )
53 2dup nth dup CHAR: % = [
60 : url-decode ( str -- str )
61 [ 0 swap url-decode-iter ] "" make ;
63 : hash>query ( hash -- str )
64 [ [ url-encode ] 2apply "=" swap 3append ] { } assoc>map
67 : build-url ( str query-params -- newstr )
73 CHAR: ? rot member? "&" "?" ? %