2005-05-02 00:18:34 -04:00
|
|
|
! Copyright (C) 2004,2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-07-16 02:26:21 -04:00
|
|
|
IN: file-responder
|
2005-08-31 21:06:13 -04:00
|
|
|
USING: html httpd kernel lists math namespaces parser sequences
|
2005-08-21 20:50:14 -04:00
|
|
|
io strings ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-08-29 23:30:54 -04:00
|
|
|
: serving-path ( filename -- filename )
|
2005-05-02 00:18:34 -04:00
|
|
|
[ "" ] unless* "doc-root" get swap append ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-09-02 16:40:34 -04:00
|
|
|
: file-response ( mime-type length -- )
|
2004-11-11 15:15:43 -05:00
|
|
|
[
|
2005-08-21 20:50:14 -04:00
|
|
|
number>string "Content-Length" swons ,
|
2004-09-02 16:40:34 -04:00
|
|
|
"Content-Type" swons ,
|
2005-08-25 15:27:38 -04:00
|
|
|
] [ ] make "200 OK" response terpri ;
|
2004-09-02 16:40:34 -04:00
|
|
|
|
2004-08-29 23:30:54 -04:00
|
|
|
: serve-static ( filename mime-type -- )
|
2004-09-02 16:40:34 -04:00
|
|
|
over file-length file-response "method" get "head" = [
|
|
|
|
drop
|
|
|
|
] [
|
2005-05-02 00:18:34 -04:00
|
|
|
<file-reader> stdio get stream-copy
|
2004-09-02 16:40:34 -04:00
|
|
|
] ifte ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-08-29 23:30:54 -04:00
|
|
|
: serve-file ( filename -- )
|
2004-08-28 22:25:59 -04:00
|
|
|
dup mime-type dup "application/x-factor-server-page" = [
|
2004-08-29 23:30:54 -04:00
|
|
|
drop run-file
|
2004-07-16 02:26:21 -04:00
|
|
|
] [
|
2004-08-28 22:25:59 -04:00
|
|
|
serve-static
|
2004-07-16 02:26:21 -04:00
|
|
|
] ifte ;
|
|
|
|
|
2004-08-30 20:24:19 -04:00
|
|
|
: list-directory ( directory -- )
|
2004-09-02 16:40:34 -04:00
|
|
|
serving-html
|
|
|
|
"method" get "head" = [
|
|
|
|
drop
|
|
|
|
] [
|
2004-10-28 23:58:23 -04:00
|
|
|
"request" get [ directory. ] simple-html-document
|
2004-09-02 16:40:34 -04:00
|
|
|
] ifte ;
|
2004-08-30 20:24:19 -04:00
|
|
|
|
2004-08-29 23:30:54 -04:00
|
|
|
: serve-directory ( filename -- )
|
2005-05-18 16:26:22 -04:00
|
|
|
"/" ?tail [
|
2005-05-02 00:18:34 -04:00
|
|
|
dup "/index.html" append dup exists? [
|
2005-06-18 21:15:07 -04:00
|
|
|
nip serve-file
|
2004-08-29 23:30:54 -04:00
|
|
|
] [
|
2004-08-30 20:24:19 -04:00
|
|
|
drop list-directory
|
2004-08-29 23:30:54 -04:00
|
|
|
] ifte
|
|
|
|
] [
|
2004-08-30 20:24:19 -04:00
|
|
|
drop directory-no/
|
2004-08-29 23:30:54 -04:00
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: serve-object ( filename -- )
|
|
|
|
dup directory? [ serve-directory ] [ serve-file ] ifte ;
|
|
|
|
|
2004-09-02 19:38:05 -04:00
|
|
|
: file-responder ( filename -- )
|
2004-07-16 02:26:21 -04:00
|
|
|
"doc-root" get [
|
2004-08-29 23:30:54 -04:00
|
|
|
serving-path dup exists? [
|
|
|
|
serve-object
|
2004-07-16 02:26:21 -04:00
|
|
|
] [
|
2004-09-02 19:38:05 -04:00
|
|
|
drop "404 not found" httpd-error
|
2004-07-16 02:26:21 -04:00
|
|
|
] ifte
|
|
|
|
] [
|
2004-09-02 19:38:05 -04:00
|
|
|
drop "404 doc-root not set" httpd-error
|
2004-07-16 02:26:21 -04:00
|
|
|
] ifte ;
|