factor/basis/http/server/responses/responses.factor

52 lines
1.4 KiB
Factor
Raw Normal View History

! Copyright (C) 2008, 2010 Slava Pestov.
2008-06-02 16:00:03 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors fry http io io.encodings.utf8 io.files
io.streams.string kernel math math.parser parser sequences
splitting unicode words xml.syntax xml.writer ;
2008-06-02 16:00:03 -04:00
IN: http.server.responses
: <content> ( body content-type -- response )
<response>
200 >>code
"Document follows" >>message
utf8 >>content-encoding
2008-06-02 16:00:03 -04:00
swap >>content-type
swap >>body ;
: <text-content> ( body -- response )
"text/plain" <content> ;
: <html-content> ( body -- response )
"text/html" <content> ;
2008-06-02 16:00:03 -04:00
: trivial-response-body ( code message -- )
2009-01-30 20:28:16 -05:00
<XML
<html>
<body>
<h1><-> <-></h1>
</body>
</html>
XML> write-xml ;
2008-06-02 16:00:03 -04:00
: <trivial-response> ( code message -- response )
2dup [ trivial-response-body ] with-string-writer
<html-content>
2008-06-02 16:00:03 -04:00
swap >>message
swap >>code ;
<<
"vocab:http/server/responses/http-status-codes.txt"
utf8 file-lines [ [ blank? ] trim ] map
dup [ "Value" head? ] find drop 1 + tail
[ "Unassigned" swap subseq? ] reject
[
"[RFC" over start head " " split1 [ blank? ] trim
[
[
"<" ">" surround create-word-in dup reset-generic
] keep string>number
] dip '[ _ _ <trivial-response> ] ( -- response )
define-declared
] each
>>