1 ! Copyright (C) 2003, 2005 Slava Pestov
3 USING: errors hashtables io kernel math namespaces parser
6 : header-line ( line -- )
7 ": " split1 dup [ swap set ] [ 2drop ] if ;
9 : (read-header) ( hash -- hash )
11 empty? [ drop ] [ header-line (read-header) ] if ;
13 : read-header ( -- hash )
14 [ (read-header) ] make-hash ;
16 : url-quotable? ( ch -- ? )
17 #! In a URL, can this character be used without
22 swap "/_?." member? or ; foldable
24 : url-encode ( str -- str )
30 CHAR: % , >hex 2 CHAR: 0 pad-left %
35 : catch-hex> ( str -- n/f )
36 #! Push f if string is not a valid hex literal.
37 [ hex> ] catch [ drop f ] when ;
39 : url-decode-hex ( index str -- )
43 >r 1+ dup 2 + r> subseq catch-hex> [ , ] when*
46 : url-decode-% ( index str -- index str )
47 2dup url-decode-hex >r 3 + r> ;
49 : url-decode-+-or-other ( index str ch -- index str )
50 dup CHAR: + = [ drop CHAR: \s ] when , >r 1+ r> ;
52 : url-decode-iter ( index str -- )
56 2dup nth dup CHAR: % = [
63 : url-decode ( str -- str )
64 [ 0 swap url-decode-iter ] "" make ;
66 : build-url ( path query-params -- str )
68 swap % dup hash-empty? [
71 [ [ url-encode ] map "=" join ] map "&" join %