diff options
| author | eltrac <hi@eltr.ac> | 2026-03-10 10:58:02 +0800 |
|---|---|---|
| committer | eltrac <hi@eltr.ac> | 2026-03-10 10:58:02 +0800 |
| commit | 2d2e1cb7b5661cdc6dc6548b665a9f52235f328b (patch) | |
| tree | 86da63ee7bdabd802fb98e5f3602bb72b3883df1 | |
| parent | ee601e565bbb6aba393f41ed7be7cf6d66b1b7a6 (diff) | |
refactor: move validation utilities to utils.clj
| -rw-r--r-- | src/weepinbell/receiver.clj | 21 | ||||
| -rw-r--r-- | src/weepinbell/utils.clj | 18 |
2 files changed, 20 insertions, 19 deletions
diff --git a/src/weepinbell/receiver.clj b/src/weepinbell/receiver.clj index cf2fe91..83ed4b3 100644 --- a/src/weepinbell/receiver.clj +++ b/src/weepinbell/receiver.clj @@ -1,24 +1,7 @@ (ns weepinbell.receiver "Receiver receives Webmention request (a source and a target); verify the request; return error message if any; push Webmention into queue of processor." - (:import [org.apache.commons.validator.routines UrlValidator] - [java.net URI]) - (:require [weepinbell.config :refer [get-config]] - [clojure.core.async :refer [put!]])) - -;; validation utilities -(defn valid-http-url? [url] - (let [schemes (into-array String ["http" "https"]) - validator (UrlValidator. schemes)] - (.isValid validator url))) - -(defn validate-urls [s t] - (and (valid-http-url? s) (valid-http-url? t))) - -(defn validate-target [t] - (let [allow-list (get-config :allowed-target-domains) - target-domain (-> (URI/create t) .getHost)] - (some #{target-domain} allow-list))) -;; validation utilities end + (:require [clojure.core.async :refer [put!]] + [weepinbell.utils :refer [validate-urls validate-target]])) (defn verify-request "Receive source and target of webmention; performs a series of checks; return a list of error responses or nil values. nil means the check has passed. The caller needs to return the first errror response if any." diff --git a/src/weepinbell/utils.clj b/src/weepinbell/utils.clj new file mode 100644 index 0000000..f81be4d --- /dev/null +++ b/src/weepinbell/utils.clj @@ -0,0 +1,18 @@ +(ns weepinbell.utils + (:import [org.apache.commons.validator.routines UrlValidator] + [java.net URI]) + (:require [weepinbell.config :refer [get-config]])) + +(defn valid-http-url? [url] + (let [schemes (into-array String ["http" "https"]) + validator (UrlValidator. schemes)] + (.isValid validator url))) + +(defn validate-urls [s t] + (and (valid-http-url? s) (valid-http-url? t))) + +(defn validate-target [t] + (let [allow-list (get-config :allowed-target-domains) + target-domain (-> (URI/create t) .getHost)] + (some #{target-domain} allow-list))) + |
