metosin / reitit

A fast data-driven routing library for Clojure/Script

Home Page:https://cljdoc.org/d/metosin/reitit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ring/create-file-handler doesn't accept URL-encoded filenames

aphyr opened this issue · comments

Hi there! It looks like create-resource-handler and create-file-handler don't understand URL-escaped characters, so requests for files like foo%20bar.png will return a 404. Is it possible that I'm doing something wrong, or this is an intentional design choice? If not, would it be appropriate to URL-decode (:uri req) in the resource handler?

This seems to be a problem specifically when the handlers are mounted outside of the router. I tried this with the following code:

(ns example.server
  (:require [reitit.ring :as ring]
            [ring.adapter.jetty :as jetty]))

(def app
  (ring/ring-handler
    (ring/router
      [["/ping" (constantly {:status 200, :body "pong"})]
       ["/internal/*" (ring/create-file-handler)]])
    (ring/routes
     (ring/create-file-handler {:path "/external"})
     (ring/create-default-handler))))

(defn start []
  (jetty/run-jetty #'app {:port 3000, :join? false})
  (println "server running in port 3000"))

I created a file public/foo bar.txt. Now http://localhost:3000/internal/foo%20bar.txt works as expected but http://localhost:3000/external/foo%20bar.txt returns 404. I'd say that they both should work and URL-decoding (:uri req) sounds about right.