diff --git a/collects/web-server/dispatchers/dispatch-files.ss b/collects/web-server/dispatchers/dispatch-files.ss index bd7a6cb682..8cf553add1 100644 --- a/collects/web-server/dispatchers/dispatch-files.ss +++ b/collects/web-server/dispatchers/dispatch-files.ss @@ -50,12 +50,12 @@ ; XXX: Unhandled range: r (output-file conn path method (get-mime-type path) 0 +inf.0)])])] - [(directory-exists? path) + [(and (directory-exists? path) + (looks-like-directory? (url-path->string (url-path uri)))) (let/ec esc (for-each (lambda (dir-default) (define full-name (build-path path dir-default)) - (when (and (file-exists? full-name) - (looks-like-directory? (url-path->string (url-path uri)))) + (when (file-exists? full-name) (esc (output-file conn full-name method (get-mime-type full-name) 0 +inf.0)))) indices) diff --git a/collects/web-server/private/request-structs.ss b/collects/web-server/private/request-structs.ss index af2c128a8a..71cc94def1 100644 --- a/collects/web-server/private/request-structs.ss +++ b/collects/web-server/private/request-structs.ss @@ -54,9 +54,10 @@ (define-serializable-struct request (method uri headers/raw bindings/raw post-data/raw host-ip host-port client-ip)) (provide/contract - [struct request ([method symbol?] [uri url?] - [headers/raw (listof header?)] - [bindings/raw (listof binding?)] - [post-data/raw (or/c false/c bytes?)] - [host-ip string?] [host-port number?] - [client-ip string?])])) \ No newline at end of file + [struct request ([method symbol?] + [uri url?] + [headers/raw (listof header?)] + [bindings/raw (listof binding?)] + [post-data/raw (or/c false/c bytes?)] + [host-ip string?] [host-port number?] + [client-ip string?])])) \ No newline at end of file diff --git a/collects/web-server/private/response.ss b/collects/web-server/private/response.ss index 0483c84d66..bf9bede2e6 100644 --- a/collects/web-server/private/response.ss +++ b/collects/web-server/private/response.ss @@ -201,6 +201,7 @@ (define bresp (make-response/basic 206 "Okay" (file-or-directory-modify-seconds file-path) mime-type (list (cons 'Content-Length (number->string len)) + ; XXX Remove on non-gets? (cons 'Content-Range (format "bytes ~a-~a/~a" start end total-len))))) (output-headers+response/basic conn bresp) (when (eq? method 'get) diff --git a/collects/web-server/tests/dispatchers/dispatch-files-test.ss b/collects/web-server/tests/dispatchers/dispatch-files-test.ss index 6200926487..57c60a6bc7 100644 --- a/collects/web-server/tests/dispatchers/dispatch-files-test.ss +++ b/collects/web-server/tests/dispatchers/dispatch-files-test.ss @@ -1,8 +1,80 @@ (module dispatch-files-test mzscheme - (require (planet "test.ss" ("schematics" "schemeunit.plt" 2))) + (require (planet "test.ss" ("schematics" "schemeunit.plt" 2)) + (lib "file.ss") + (lib "url.ss" "net") + (lib "list.ss") + (lib "xml.ss" "xml") + (lib "request-structs.ss" "web-server" "private") + (lib "util.ss" "web-server" "private") + (lib "dispatch.ss" "web-server" "dispatchers") + (prefix files: (lib "dispatch-files.ss" "web-server" "dispatchers")) + "../util.ss") (provide dispatch-files-tests) - ; XXX + (define tmp-file (make-temporary-file)) + (with-output-to-file tmp-file + (lambda () + (display + (xexpr->string + `(html (head (title "A title")) + (body "Here's some content!"))))) + 'truncate/replace) + + (define a-dir (directory-part tmp-file)) + (define not-there (build-path "I/probably/do/not/exist")) + + (define (dispatch i? . paths) + (define b (box 0)) + (files:make #:url->path + (lambda (url) + (begin0 (values (list-ref paths (min (unbox b) (sub1 (length paths)))) empty) + (set-box! b (add1 (unbox b))))) + #:mime-types-path (build-path "/etc/httpd/mime.types") + #:indices (list (if i? (file-name-from-path tmp-file) not-there)))) + + (define (collect d req) + (define-values (c i o) (make-mock-connection #"")) + (d c req) + (redact (get-output-bytes o))) + + (define file-url (string->url "http://test.com/foo")) + (define dir-url (string->url "http://test.com/foo/")) + (define (req d? meth heads) + (make-request meth (if d? dir-url file-url) heads empty #"" "host" 80 "client")) + (define dispatch-files-tests (test-suite - "Files"))) \ No newline at end of file + "Files" + + (test-equal? "file, exists, whole, get" + (collect (dispatch #t tmp-file) (req #f 'get empty)) + #"HTTP/1.1 206 Okay\r\nDate: REDACTED GMT\r\nLast-Modified: REDACTED GMT\r\nServer: PLT Scheme\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: 81\r\nContent-Range: bytes 0-81/81\r\n\r\n