diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn new file mode 100644 index 00000000..9fe3d39e --- /dev/null +++ b/.clj-kondo/config.edn @@ -0,0 +1,7 @@ +{:lint-as {hiccup.def/defhtml clojure.core/defn} + :linters + {:deprecated-var + {:exclude + {ring.middleware.file-info/file-info-response + {:defs [ring.middleware.file-info/wrap-file-info] + :namespaces [ring.middleware.test.file-info]}}}}} diff --git a/ring-core-protocols/src/ring/core/protocols.clj b/ring-core-protocols/src/ring/core/protocols.clj index 0c30b7e4..6de7c5bb 100644 --- a/ring-core-protocols/src/ring/core/protocols.clj +++ b/ring-core-protocols/src/ring/core/protocols.clj @@ -37,7 +37,7 @@ (io/writer output-stream))) (extend-protocol StreamableResponseBody - (Class/forName "[B") + #_{:clj-kondo/ignore [:syntax]} (Class/forName "[B") (write-body-to-stream [body _ ^OutputStream output-stream] (.write output-stream ^bytes body) (.close output-stream)) diff --git a/ring-core/src/ring/middleware/content_type.clj b/ring-core/src/ring/middleware/content_type.clj index b093e59a..10ebaa27 100644 --- a/ring-core/src/ring/middleware/content_type.clj +++ b/ring-core/src/ring/middleware/content_type.clj @@ -14,7 +14,7 @@ ([response request] (content-type-response response request {})) ([response request options] - (if response + (when response (if (get-header response "Content-Type") response (let [mime-type (guess-mime-type request response (:mime-types options))] diff --git a/ring-core/src/ring/middleware/file.clj b/ring-core/src/ring/middleware/file.clj index b46eaab3..d8db451b 100644 --- a/ring-core/src/ring/middleware/file.clj +++ b/ring-core/src/ring/middleware/file.clj @@ -13,7 +13,7 @@ "Ensures that a directory exists at the given path, throwing if one does not." [dir-path] (let [dir (io/as-file dir-path)] - (if-not (.exists dir) + (when-not (.exists dir) (throw (Exception. (format "Directory does not exist: %s" dir-path)))))) (defn file-request @@ -27,7 +27,7 @@ :index-files? true :allow-symlinks? false} options)] - (if (#{:get :head} (:request-method request)) + (when (#{:get :head} (:request-method request)) (let [path (subs (codec/url-decode (request/path-info request)) 1)] (-> (response/file-response path options) (head/head-response request))))))) diff --git a/ring-core/src/ring/middleware/file_info.clj b/ring-core/src/ring/middleware/file_info.clj index 070cf664..46361e11 100644 --- a/ring-core/src/ring/middleware/file_info.clj +++ b/ring-core/src/ring/middleware/file_info.clj @@ -7,7 +7,7 @@ [ring.util.mime-type :refer [ext-mime-type]] [ring.util.io :refer [last-modified-date]]) (:import [java.io File] - [java.util Date Locale TimeZone] + [java.util Locale TimeZone] [java.text SimpleDateFormat])) (defn- guess-mime-type @@ -17,17 +17,17 @@ (or (ext-mime-type (.getPath file) mime-types) "application/octet-stream")) -(defn- ^SimpleDateFormat make-http-format +(defn- make-http-format "Formats or parses dates into HTTP date format (RFC 822/1123)." - [] + ^SimpleDateFormat [] ;; SimpleDateFormat is not threadsafe, so return a new instance each time (doto (SimpleDateFormat. "EEE, dd MMM yyyy HH:mm:ss ZZZ" Locale/US) (.setTimeZone (TimeZone/getTimeZone "UTC")))) (defn- not-modified-since? "Has the file been modified since the last request from the client?" - [{headers :headers :as req} last-modified] - (if-let [modified-since (headers "if-modified-since")] + [{headers :headers} last-modified] + (when-let [modified-since (headers "if-modified-since")] (not (.before (.parse (make-http-format) modified-since) last-modified)))) diff --git a/ring-core/src/ring/middleware/flash.clj b/ring-core/src/ring/middleware/flash.clj index ca2118dd..54d5c11c 100644 --- a/ring-core/src/ring/middleware/flash.clj +++ b/ring-core/src/ring/middleware/flash.clj @@ -17,7 +17,7 @@ {:added "1.2"} [response request] (let [{:keys [session flash]} request] - (if response + (when response (let [session (if (contains? response :session) (response :session) session) diff --git a/ring-core/src/ring/middleware/keyword_params.clj b/ring-core/src/ring/middleware/keyword_params.clj index 6c776b70..13f25f48 100644 --- a/ring-core/src/ring/middleware/keyword_params.clj +++ b/ring-core/src/ring/middleware/keyword_params.clj @@ -10,7 +10,7 @@ (defn- keyword-syntax? [s parse-namespaces?] (or (re-matches re-plain-keyword s) - (if parse-namespaces? (re-matches re-namespaced-keyword s)))) + (when parse-namespaces? (re-matches re-namespaced-keyword s)))) (defn- keyify-params [target parse-namespaces?] (cond diff --git a/ring-core/src/ring/middleware/multipart_params/temp_file.clj b/ring-core/src/ring/middleware/multipart_params/temp_file.clj index bb6b3791..bdc592c8 100644 --- a/ring-core/src/ring/middleware/multipart_params/temp_file.clj +++ b/ring-core/src/ring/middleware/multipart_params/temp_file.clj @@ -26,7 +26,7 @@ (.delete file) (swap! file-set disj file)))) -(defn- ^File make-temp-file [file-set] +(defn- make-temp-file ^File [file-set] (let [temp-file (File/createTempFile "ring-multipart-" nil)] (swap! file-set conj temp-file) temp-file)) diff --git a/ring-core/src/ring/middleware/nested_params.clj b/ring-core/src/ring/middleware/nested_params.clj index 7b176d92..0a8776e1 100644 --- a/ring-core/src/ring/middleware/nested_params.clj +++ b/ring-core/src/ring/middleware/nested_params.clj @@ -12,7 +12,7 @@ => [\"foo\" \"bar\" \"\" \"baz\"]" [param-name] (let [[_ k ks] (re-matches #"(?s)(.*?)((?:\[.*?\])*)" (name param-name)) - keys (if ks (map second (re-seq #"\[(.*?)\]" ks)))] + keys (when ks (map second (re-seq #"\[(.*?)\]" ks)))] (cons k keys))) (defn- assoc-vec [m k v] diff --git a/ring-core/src/ring/middleware/not_modified.clj b/ring-core/src/ring/middleware/not_modified.clj index 3249d9d5..99a1d09e 100644 --- a/ring-core/src/ring/middleware/not_modified.clj +++ b/ring-core/src/ring/middleware/not_modified.clj @@ -2,15 +2,15 @@ "Middleware that returns a 304 Not Modified response for responses with Last-Modified headers." (:require [ring.util.time :refer [parse-date]] - [ring.util.response :refer [status get-header header]] + [ring.util.response :refer [get-header header]] [ring.util.io :refer [close!]])) (defn- etag-match? [request response] - (if-let [etag (get-header response "ETag")] + (when-let [etag (get-header response "ETag")] (= etag (get-header request "if-none-match")))) -(defn- ^java.util.Date date-header [response header] - (if-let [http-date (get-header response header)] +(defn- date-header ^java.util.Date [response header] + (when-let [http-date (get-header response header)] (parse-date http-date))) (defn- not-modified-since? [request response] diff --git a/ring-core/src/ring/middleware/resource.clj b/ring-core/src/ring/middleware/resource.clj index fb205546..28619f24 100644 --- a/ring-core/src/ring/middleware/resource.clj +++ b/ring-core/src/ring/middleware/resource.clj @@ -13,7 +13,7 @@ ([request root-path] (resource-request request root-path {})) ([request root-path options] - (if (#{:head :get} (:request-method request)) + (when (#{:head :get} (:request-method request)) (let [path (subs (codec/url-decode (request/path-info request)) 1)] (-> (response/resource-response path (assoc options :root root-path)) (head/head-response request)))))) diff --git a/ring-core/src/ring/middleware/session.clj b/ring-core/src/ring/middleware/session.clj index 12d30b0a..7bfbe636 100644 --- a/ring-core/src/ring/middleware/session.clj +++ b/ring-core/src/ring/middleware/session.clj @@ -19,14 +19,14 @@ :cookie-attrs (merge {:path "/" :http-only true} (options :cookie-attrs) - (if-let [root (options :root)] + (when-let [root (options :root)] {:path root}))}) (defn- bare-session-request [request {:keys [store cookie-name]}] (let [req-key (get-in request [:cookies cookie-name :value]) session (store/read-session store req-key) - session-key (if session req-key)] + session-key (when session req-key)] (merge request {:session (or session {}) :session/key session-key}))) @@ -43,7 +43,7 @@ (defn- bare-session-response [response {session-key :session/key} {:keys [store cookie-name cookie-attrs]}] - (let [new-session-key (if (contains? response :session) + (let [new-session-key (when (contains? response :session) (if-let [session (response :session)] (if (:recreate (meta session)) (do @@ -51,7 +51,7 @@ (->> (vary-meta session dissoc :recreate) (store/write-session store nil))) (store/write-session store session-key session)) - (if session-key + (when session-key (store/delete-session store session-key)))) session-attrs (:session-cookie-attrs response) cookie {cookie-name @@ -70,7 +70,7 @@ ([response request] (session-response response request {})) ([response request options] - (if response + (when response (-> response (bare-session-response request options) (cond-> (:set-cookies? options true) cookies/cookies-response))))) diff --git a/ring-core/src/ring/middleware/session/cookie.clj b/ring-core/src/ring/middleware/session/cookie.clj index 2917acd4..7043af90 100644 --- a/ring-core/src/ring/middleware/session/cookie.clj +++ b/ring-core/src/ring/middleware/session/cookie.clj @@ -5,8 +5,7 @@ [clojure.edn :as edn] [crypto.random :as random] [crypto.equality :as crypto]) - (:import [java.security SecureRandom] - [javax.crypto Cipher Mac] + (:import [javax.crypto Cipher Mac] [javax.crypto.spec SecretKeySpec IvParameterSpec])) (def ^{:private true @@ -74,7 +73,7 @@ (defn- deserialize [x options] (edn/read-string (select-keys options [:readers]) x)) -(defn- ^String serialize [x options] +(defn- serialize ^String [x options] {:post [(= x (deserialize % options))]} (pr-str x)) @@ -88,15 +87,15 @@ "Retrieve a sealed Clojure data structure from a string" [key ^String string options] (let [[data mac] (.split string "--")] - (if-let [data (try (codec/base64-decode data) + (when-let [data (try (codec/base64-decode data) (catch IllegalArgumentException _ nil))] - (if (crypto/eq? mac (hmac key data)) + (when (crypto/eq? mac (hmac key data)) (deserialize (decrypt key data) options))))) (deftype CookieStore [secret-key options] SessionStore (read-session [_ data] - (if data (unseal secret-key data options))) + (when data (unseal secret-key data options))) (write-session [_ _ data] (seal secret-key data options)) (delete-session [_ _] diff --git a/ring-core/src/ring/util/io.clj b/ring-core/src/ring/util/io.clj index 0584ea08..d43e48a9 100644 --- a/ring-core/src/ring/util/io.clj +++ b/ring-core/src/ring/util/io.clj @@ -1,11 +1,9 @@ (ns ring.util.io "Utility functions for handling I/O." - (:require [clojure.java.io :as io]) (:import [java.io PipedInputStream PipedOutputStream ByteArrayInputStream File - Closeable IOException])) (defn piped-input-stream diff --git a/ring-core/src/ring/util/mime_type.clj b/ring-core/src/ring/util/mime_type.clj index acf4c2f0..52d81fce 100644 --- a/ring-core/src/ring/util/mime_type.clj +++ b/ring-core/src/ring/util/mime_type.clj @@ -104,7 +104,7 @@ (defn- filename-ext "Returns the file extension of a filename or filepath." [filename] - (if-let [ext (second (re-find #"\.([^./\\]+)$" filename))] + (when-let [ext (second (re-find #"\.([^./\\]+)$" filename))] (str/lower-case ext))) (defn ext-mime-type diff --git a/ring-core/src/ring/util/request.clj b/ring-core/src/ring/util/request.clj index 8c00be5d..ff33fe4f 100644 --- a/ring-core/src/ring/util/request.clj +++ b/ring-core/src/ring/util/request.clj @@ -10,14 +10,14 @@ "://" (get-in request [:headers "host"]) (:uri request) - (if-let [query (:query-string request)] + (when-let [query (:query-string request)] (str "?" query)))) (defn content-type "Return the content-type of the request, or nil if no content-type is set." {:added "1.3"} [request] - (if-let [type ^String (get (:headers request) "content-type")] + (when-let [type ^String (get (:headers request) "content-type")] (let [i (.indexOf type ";")] (if (neg? i) type (subs type 0 i))))) @@ -25,7 +25,7 @@ "Return the content-length of the request, or nil no content-length is set." {:added "1.3"} [request] - (if-let [^String length (get-in request [:headers "content-length"])] + (when-let [^String length (get-in request [:headers "content-length"])] (Long/valueOf length))) (defn character-encoding @@ -39,7 +39,7 @@ "True if a request contains a urlencoded form in the body." {:added "1.3"} [request] - (if-let [^String type (content-type request)] + (when-let [^String type (content-type request)] (.startsWith type "application/x-www-form-urlencoded"))) (defmulti ^String body-string diff --git a/ring-core/src/ring/util/response.clj b/ring-core/src/ring/util/response.clj index 35c96e18..8686e27f 100644 --- a/ring-core/src/ring/util/response.clj +++ b/ring-core/src/ring/util/response.clj @@ -84,7 +84,7 @@ (defn- canonical-path ^String [^File file] (str (.getCanonicalPath file) - (if (.isDirectory file) File/separatorChar))) + (when (.isDirectory file) File/separatorChar))) (defn- safe-path? [^String root ^String path] (.startsWith (canonical-path (File. root path)) @@ -99,7 +99,7 @@ (defn- find-file-named [^File dir ^String filename] (let [path (File. dir filename)] - (if (.isFile path) + (when (.isFile path) path))) (defn- find-file-starting-with [^File dir ^String prefix] @@ -117,13 +117,13 @@ (defn- safely-find-file [^String path opts] (if-let [^String root (:root opts)] - (if (or (safe-path? root path) + (when (or (safe-path? root path) (and (:allow-symlinks? opts) (not (directory-transversal? path)))) (File. root path)) (File. path))) (defn- find-file [^String path opts] - (if-let [^File file (safely-find-file path opts)] + (when-let [^File file (safely-find-file path opts)] (cond (.isDirectory file) (and (:index-files? opts true) (find-index-file file)) @@ -156,7 +156,7 @@ ([filepath] (file-response filepath {})) ([filepath options] - (if-let [file (find-file filepath options)] + (when-let [file (find-file filepath options)] (let [data (file-data file)] (-> (response (:content data)) (content-length (:content-length data)) @@ -170,7 +170,7 @@ ;; As a work-around, we'll backport the fix from CLJ-1177 into ;; url-as-file. -(defn- ^File url-as-file [^java.net.URL u] +(defn- url-as-file ^File [^java.net.URL u] (-> (.getFile u) (str/replace \/ File/separatorChar) (str/replace "+" (URLEncoder/encode "+" "UTF-8")) @@ -265,8 +265,8 @@ (defmethod resource-data :file [url] - (if-let [file (url-as-file url)] - (if-not (.isDirectory file) + (when-let [file (url-as-file url)] + (when-not (.isDirectory file) (file-data file)))) (defn- add-ending-slash [^String path] @@ -282,17 +282,17 @@ (defn- connection-content-length [^java.net.URLConnection conn] (let [len (.getContentLength conn)] - (if (<= 0 len) len))) + (when (<= 0 len) len))) (defn- connection-last-modified [^java.net.URLConnection conn] (let [last-mod (.getLastModified conn)] - (if-not (zero? last-mod) + (when-not (zero? last-mod) (Date. last-mod)))) (defmethod resource-data :jar [^java.net.URL url] (let [conn (.openConnection url)] - (if-not (jar-directory? conn) + (when-not (jar-directory? conn) {:content (.getInputStream conn) :content-length (connection-content-length conn) :last-modified (connection-last-modified conn)}))) @@ -301,7 +301,7 @@ "Return a response for the supplied URL." {:added "1.2"} [^URL url] - (if-let [data (resource-data url)] + (when-let [data (resource-data url)] (-> (response (:content data)) (content-length (:content-length data)) (last-modified (:last-modified data))))) @@ -337,9 +337,9 @@ load #(if-let [loader (:loader options)] (io/resource % loader) (io/resource %))] - (if-not (directory-transversal? root+path) - (if-let [resource (load root+path)] + (when-not (directory-transversal? root+path) + (when-let [resource (load root+path)] (let [response (url-response resource)] - (if (or (not (instance? File (:body response))) - (safe-file-resource? response options)) + (when (or (not (instance? File (:body response))) + (safe-file-resource? response options)) response))))))) diff --git a/ring-core/src/ring/util/time.clj b/ring-core/src/ring/util/time.clj index a346e93b..072b24a4 100644 --- a/ring-core/src/ring/util/time.clj +++ b/ring-core/src/ring/util/time.clj @@ -9,7 +9,7 @@ :rfc1036 "EEEE, dd-MMM-yy HH:mm:ss zzz" :asctime "EEE MMM d HH:mm:ss yyyy"}) -(defn- ^SimpleDateFormat formatter [format] +(defn- formatter ^SimpleDateFormat [format] (doto (SimpleDateFormat. ^String (http-date-formats format) Locale/US) (.setTimeZone (TimeZone/getTimeZone "GMT")))) diff --git a/ring-devel/src/ring/handler/dump.clj b/ring-devel/src/ring/handler/dump.clj index 9e617260..f2ef3d4c 100644 --- a/ring-devel/src/ring/handler/dump.clj +++ b/ring-devel/src/ring/handler/dump.clj @@ -38,7 +38,7 @@ [:tbody (for [key ring-keys] (req-pair key req))]] - (if-let [user-keys (set/difference (set (keys req)) (set ring-keys))] + (when-let [user-keys (set/difference (set (keys req)) (set ring-keys))] (html [:br] [:table.request.user @@ -54,5 +54,5 @@ (println) (-> (response (template request)) (content-type "text/html"))) - ([request respond raise] + ([request respond _raise] (respond (handle-dump request)))) diff --git a/ring-devel/src/ring/middleware/lint.clj b/ring-devel/src/ring/middleware/lint.clj index 3d20c90d..3ed94f2f 100644 --- a/ring-devel/src/ring/middleware/lint.clj +++ b/ring-devel/src/ring/middleware/lint.clj @@ -1,7 +1,6 @@ (ns ring.middleware.lint "Middleware that checks Ring requests and responses for correctness." - (:require [clojure.set :as set] - [clojure.string :as str]) + (:require [clojure.string :as str]) (:import [java.io File InputStream] [java.security.cert X509Certificate])) @@ -11,7 +10,7 @@ argument and a printing of an invalid val." [val spec message] (try - (if-not (spec val) + (when-not (spec val) (throw (Exception. (format "Ring lint error: specified %s, but %s was not" message (pr-str val))))) (catch Exception e diff --git a/ring-devel/src/ring/middleware/stacktrace.clj b/ring-devel/src/ring/middleware/stacktrace.clj index d0ac396d..f1f5cf7d 100644 --- a/ring-devel/src/ring/middleware/stacktrace.clj +++ b/ring-devel/src/ring/middleware/stacktrace.clj @@ -7,8 +7,8 @@ (:require [clojure.java.io :as io] [hiccup.core :refer [html h]] [hiccup.page :refer [html5]] - [clj-stacktrace.core :refer :all] - [clj-stacktrace.repl :refer :all] + [clj-stacktrace.core :as st] + [clj-stacktrace.repl :as repl] [ring.util.response :refer [content-type response status]])) (defn wrap-stacktrace-log @@ -26,13 +26,13 @@ (try (handler request) (catch Throwable ex - (pst-on *err* color? ex) + (repl/pst-on *err* color? ex) (throw ex)))) ([request respond raise] (try - (handler request respond (fn [ex] (pst-on *err* color? ex) (raise ex))) + (handler request respond (fn [ex] (repl/pst-on *err* color? ex) (raise ex))) (catch Throwable ex - (pst-on *err* color? ex) + (repl/pst-on *err* color? ex) (throw ex)))))))) (defn- style-resource [path] @@ -41,14 +41,14 @@ (defn- elem-partial [elem] (if (:clojure elem) [:tr.clojure - [:td.source (h (source-str elem))] - [:td.method (h (clojure-method-str elem))]] + [:td.source (h (repl/source-str elem))] + [:td.method (h (repl/clojure-method-str elem))]] [:tr.java - [:td.source (h (source-str elem))] - [:td.method (h (java-method-str elem))]])) + [:td.source (h (repl/source-str elem))] + [:td.method (h (repl/java-method-str elem))]])) (defn- html-exception [ex] - (let [[ex & causes] (iterate :cause (parse-exception ex))] + (let [[ex & causes] (iterate :cause (st/parse-exception ex))] (html5 [:head [:title "Ring: Stacktrace"] @@ -69,7 +69,7 @@ [:tbody (map elem-partial (:trace-elems cause))]]]])]]))) (defn- text-ex-response [e] - (-> (response (with-out-str (pst e))) + (-> (response (with-out-str (repl/pst e))) (status 500) (content-type "text/plain")))