Clojure Pedestal root 作为 application/octet-stream

Clojure Pedestal root serving as application/octet-stream

我正在尝试在 Pedestal 0.5.1. I am using the ::file-path to point to a directory to host the files. This works fine if I navigate directly to the file http://localhost:8888/index.html but if I go to the root of the site http://localhost:8888 it serves the files as application/octet-stream rather than text/html. I adapted the Hello World Sample 中托管静态资产和服务,它具有相同的行为。

src/hello_world/server.clj

(ns hello-world.server
  (:require [io.pedestal.http :as http]
            [io.pedestal.http.route :as route])
  (:gen-class))

(def routes
  (route/expand-routes [[]]))

(def service
  {:env                 :prod
   ::http/join? false
   ::http/routes routes
   ::http/file-path "/tmp/www"       
   ::http/type          :jetty
   ::http/allowed-origins {:creds true :allowed-origins (constantly true)}       
   ::http/port          8888})

(defonce runnable-service (http/create-server service))

(defn -main
  "The entry-point for 'lein run'"
  [& args]
  (println "\nCreating your server...")
  (http/start runnable-service))

开始lein run

$ curl -i localhost:8888
HTTP/1.1 200 OK
Date: Fri, 18 Nov 2016 16:02:56 GMT
Last-Modified: Fri, 18 Nov 2016 15:10:22 GMT
Content-Type: application/octet-stream
Content-Length: 12
Server: Jetty(9.3.8.v20160314)

hello world

$ curl -i localhost:8888/index.html
HTTP/1.1 200 OK
Date: Fri, 18 Nov 2016 16:03:02 GMT
Last-Modified: Fri, 18 Nov 2016 15:10:22 GMT
Content-Type: text/html
Content-Length: 12
Server: Jetty(9.3.8.v20160314)

hello world

有什么方法可以修复“/”路由以提供正确的内容类型吗?

要获得正确的 content-types 作为目录索引的文件, 将拦截器 io.pedestal.http.ring-middlewares/file-info 添加到 您的基座配置。

这需要您覆盖默认拦截器链 你自己的,所以你必须包括所有的默认拦截器 你的应用程序需要。

例如,您的服务可能如下所示:

(ns hello-world.service
  (:require
   [io.pedestal.http :as http]
   [io.pedestal.http.ring-middlewares :as middlewares]
   [io.pedestal.http.route :as route]
   [io.pedestal.http.route.definition :refer [defroutes]]))

(defroutes routes
  [[]])

(def service
  {::http/type :jetty
   ::http/port 8080
   ::http/interceptors [http/log-request
                        http/not-found
                        middlewares/session
                        route/query-params
                        (middlewares/file-info)  ; HERE
                        (middlewares/file "/tmp/www")
                        ;; ... insert other interceptors ...
                        (route/router #(deref #'routes) :map-tree)]})

有关您可能想要包含的其他默认拦截器的示例, 参见 default-interceptors

说明

这在实践中可能不会经常出现,因为许多网络 应用程序使用处理函数来生成主页 返回静态文件。

对于替代解决方案,您可以为 / 编写路由处理程序 路由里面returns的内容index.html用合适的 content-type.

Pedestal 的默认拦截器堆栈包括 io.pedestal.http.ring-middlewares/fileio.pedestal.http.ring-middlewares/content-type.

这些拦截器只是包装了Ring中间件的功能 file-requestcontent-type-response

file-requestreturns一个java.io.Fileobject作为HTTP响应。

content-type-response 检查 请求 URI 以确定 Content-Type header 的值。由于 URI 只是 / 它 默认为 application/octet-stream.

相比之下 ring.middleware.file-info(已弃用)检查 响应中实际 File object 的路径。 参见 file-info-response

io.pedestal.http.ring-middlewares/file-info是拦截器 ring.middleware.file-info/file-info-response 周围的包装器。