diff options
| author | eltrac <hi@eltr.ac> | 2026-05-03 10:11:27 +0800 |
|---|---|---|
| committer | eltrac <hi@eltr.ac> | 2026-05-03 10:11:27 +0800 |
| commit | 586fcbf2644d554ea31c8c383189a9ef2ec37945 (patch) | |
| tree | 3f62001048b9c0cb52ddd35e4822b6401b9300f4 /src | |
| parent | eedd9b5ce9fa3de2dc29d12319f14ea69d7466fb (diff) | |
refactor(receiver): move validator functions from utils to receiver
receiver.clj is the only component that uses and logically relies on
these functions, so they should be its private functions in the first
place
Diffstat (limited to 'src')
| -rw-r--r-- | src/weepinbell/receiver.clj | 22 | ||||
| -rw-r--r-- | src/weepinbell/utils.clj | 14 |
2 files changed, 19 insertions, 17 deletions
diff --git a/src/weepinbell/receiver.clj b/src/weepinbell/receiver.clj index 5adb535..6e0327e 100644 --- a/src/weepinbell/receiver.clj +++ b/src/weepinbell/receiver.clj @@ -2,12 +2,22 @@ "Receiver receives Webmention request (a source and a target); verify the request; return error message if any; push Webmention into queue of processor." (:require [clojure.string :as string] [clojure.core.async :refer [put!]] - [weepinbell.utils :refer [validate-urls validate-target]] + [weepinbell.utils :refer [valid-http-url? get-domain]] [weepinbell.config :refer [get-config]] [md5.core :refer [string->md5-hex]])) -(defn verify-request - [source target] +;; validation and verification + +(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 (get-domain t)] + (some #{target-domain} allow-list))) + +(defn- verify-request + [source target opts] (when-let [error-msg (cond ;; The receiver MUST check that source and target are valid URLs [URL] ;; and are of schemes that are supported by the receiver. @@ -21,15 +31,17 @@ :body {:status 400 :message error-msg}})) +;; receiving logic + (defn receive "Receive webmention; call verify-request; if verification passed, push webmention into queue." - [wm wmc] + [wm wmc opts] (let [{:keys [s t]} wm ;; remove anchor, #... source (some-> s (string/replace #"#.*$" "")) target (some-> t (string/replace #"#.*$" ""))] ;; Request verification - (if-let [errror-response (verify-request source target)] + (if-let [errror-response (verify-request source target opts)] ;; respond if request verification failed errror-response ;; proceed if request is ok diff --git a/src/weepinbell/utils.clj b/src/weepinbell/utils.clj index 84a3d09..9afcc49 100644 --- a/src/weepinbell/utils.clj +++ b/src/weepinbell/utils.clj @@ -1,13 +1,11 @@ (ns weepinbell.utils (:import [org.apache.commons.validator.routines UrlValidator] [java.net URI]) - (:require [weepinbell.config :refer [get-config]] - [clj-http.client :as client] + (:require [clj-http.client :as client] [clojure.string :as string] [clojure.walk :as walk] [clojure.java.io :as io] - [taoensso.timbre :as timbre] - [cheshire.core :as json])) + [taoensso.timbre :as timbre])) (defn get-domain [url] (when (some? url) @@ -25,14 +23,6 @@ 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 (get-domain t)] - (some #{target-domain} allow-list))) - (defn get-content-type [url] (try (let [response (client/head url)] |
