cgi: adding a vocab to support using Factor in CGI scripts.

db4
John Benediktsson 2012-09-22 12:46:13 -07:00
parent f998138268
commit 0c68baf6b5
5 changed files with 87 additions and 0 deletions

1
extra/cgi/authors.txt Normal file
View File

@ -0,0 +1 @@
John Benediktsson

12
extra/cgi/cgi-docs.factor Normal file
View File

@ -0,0 +1,12 @@
! Copyright (C) 2009-2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: assocs help.markup help.syntax ;
IN: cgi
HELP: <cgi-form>
{ $values { "assoc" assoc } }
{ $description "Parse a CGI request into an " { $link assoc } ". Multiple parameters are passed as a list for each key." } ;
HELP: <cgi-simple-form>
{ $values { "assoc" assoc } }
{ $description "Parse a CGI request into an " { $link assoc } ". Only the first parameter is kept, if multiple parameters are passed." } ;

View File

@ -0,0 +1,20 @@
! Copyright (C) 2009 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: cgi cgi.private kernel tools.test ;
[ t ] [ H{ } "" (query-string) = ] unit-test
[ t ] [ H{ { "a" { "1" } } { "b" { "2" } } }
"a=1&b=2" (query-string) = ] unit-test
[ t ] [ H{ { "a" { "1" } } { "b" { "2" "3" } } }
"a=1&b=2&b=3" (query-string) = ] unit-test
[ t ] [ "text/html" (content-type)
[ H{ } = ] [ "text/html" = ] bi* and ] unit-test
[ t ] [ "text/html; charset=utf-8" (content-type)
[ H{ { "charset" { "utf-8" } } } = ]
[ "text/html" = ] bi* and ] unit-test

53
extra/cgi/cgi.factor Normal file
View File

@ -0,0 +1,53 @@
! Copyright (C) 2009-2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: arrays assocs combinators environment io kernel
math.parser regexp sequences splitting strings unicode.case
urls.encoding ;
IN: cgi
<PRIVATE
: (query-string) ( string -- assoc )
query>assoc [ nip ] assoc-filter [
[ [ CHAR: \s = ] trim ]
[ dup string? [ 1array ] when ] bi*
] assoc-map ;
: parse-get ( -- assoc )
"QUERY_STRING" os-env "" or (query-string) ;
: (content-type) ( string -- params media/type )
";" split unclip [
[ H{ } clone ] [ first (query-string) ] if-empty
] dip ;
: (multipart) ( -- assoc )
"multipart unsupported" throw ;
: (urlencoded) ( -- assoc )
"CONTENT_LENGTH" os-env "0" or string>number
read [ "" ] [ "&" append ] if-empty
"QUERY_STRING" os-env [ append ] when* (query-string) ;
: parse-post ( -- assoc )
"CONTENT_TYPE" os-env "" or (content-type) {
{ "multipart/form-data" [ (multipart) ] }
{ "application/x-www-form-urlencoded" [ (urlencoded) ] }
[ drop parse-get ]
} case nip ;
PRIVATE>
: <cgi-form> ( -- assoc )
"REQUEST_METHOD" os-env "GET" or >upper {
{ "GET" [ parse-get ] }
{ "POST" [ parse-post ] }
[ "Unknown request method" throw ]
} case ;
: <cgi-simple-form> ( -- assoc )
<cgi-form> [ first ] assoc-map ;

1
extra/cgi/summary.txt Normal file
View File

@ -0,0 +1 @@
Support using Factor in CGI scripts