factor/contrib/httpd/http-common.factor

57 lines
1.3 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2005 Slava Pestov
IN: http
USING: errors kernel lists math namespaces parser sequences
io strings ;
2005-11-29 23:49:59 -05:00
: header-line ( line -- )
": " split1 dup [ swap set ] [ 2drop ] if ;
2005-11-29 23:49:59 -05:00
: (read-header) ( hash -- hash )
readln dup
2005-09-24 15:21:17 -04:00
empty? [ drop ] [ header-line (read-header) ] if ;
2004-07-16 02:26:21 -04:00
2005-11-29 23:49:59 -05:00
: read-header ( -- hash )
[ (read-header) ] make-hash ;
2004-07-16 02:26:21 -04:00
: url-encode ( str -- str )
[
[
dup url-quotable? [
,
] [
2005-06-15 23:27:28 -04:00
CHAR: % , >hex 2 CHAR: 0 pad-left %
2005-09-24 15:21:17 -04:00
] if
] each
2005-08-25 15:27:38 -04:00
] "" make ;
2005-09-21 01:12:16 -04:00
: catch-hex> ( str -- n/f )
#! Push f if string is not a valid hex literal.
[ hex> ] catch [ drop f ] when ;
: url-decode-hex ( index str -- )
2dup length 2 - >= [
2drop
] [
2005-09-16 22:47:28 -04:00
>r 1+ dup 2 + r> subseq catch-hex> [ , ] when*
2005-09-24 15:21:17 -04:00
] if ;
: url-decode-% ( index str -- index str )
2dup url-decode-hex >r 3 + r> ;
: url-decode-+-or-other ( index str ch -- index str )
2005-09-16 22:47:28 -04:00
dup CHAR: + = [ drop CHAR: \s ] when , >r 1+ r> ;
: url-decode-iter ( index str -- )
2dup length >= [
2drop
] [
2dup nth dup CHAR: % = [
drop url-decode-%
] [
url-decode-+-or-other
2005-09-24 15:21:17 -04:00
] if url-decode-iter
] if ;
: url-decode ( str -- str )
2005-08-25 15:27:38 -04:00
[ 0 swap url-decode-iter ] "" make ;