Skip to content

Commit

Permalink
Fix clj-kondo warnings in src directories
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Oct 19, 2024
1 parent 3ef0481 commit d7d0378
Show file tree
Hide file tree
Showing 21 changed files with 72 additions and 69 deletions.
7 changes: 7 additions & 0 deletions .clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -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]}}}}}
2 changes: 1 addition & 1 deletion ring-core-protocols/src/ring/core/protocols.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion ring-core/src/ring/middleware/content_type.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
4 changes: 2 additions & 2 deletions ring-core/src/ring/middleware/file.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)))))))
Expand Down
10 changes: 5 additions & 5 deletions ring-core/src/ring/middleware/file_info.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))))

Expand Down
2 changes: 1 addition & 1 deletion ring-core/src/ring/middleware/flash.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ring-core/src/ring/middleware/keyword_params.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion ring-core/src/ring/middleware/nested_params.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions ring-core/src/ring/middleware/not_modified.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion ring-core/src/ring/middleware/resource.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))))
Expand Down
10 changes: 5 additions & 5 deletions ring-core/src/ring/middleware/session.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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})))

Expand All @@ -43,15 +43,15 @@

(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
(store/delete-session store session-key)
(->> (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
Expand All @@ -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)))))
Expand Down
11 changes: 5 additions & 6 deletions ring-core/src/ring/middleware/session/cookie.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand All @@ -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 [_ _]
Expand Down
2 changes: 0 additions & 2 deletions ring-core/src/ring/util/io.clj
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion ring-core/src/ring/util/mime_type.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions ring-core/src/ring/util/request.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
"://"
(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)))))

(defn content-length
"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
Expand All @@ -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
Expand Down
32 changes: 16 additions & 16 deletions ring-core/src/ring/util/response.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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]
Expand All @@ -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))
Expand Down Expand Up @@ -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))
Expand All @@ -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"))
Expand Down Expand Up @@ -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]
Expand All @@ -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)})))
Expand All @@ -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)))))
Expand Down Expand Up @@ -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)))))))
2 changes: 1 addition & 1 deletion ring-core/src/ring/util/time.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"))))

Expand Down
Loading

0 comments on commit d7d0378

Please sign in to comment.