From 4bb609fffc45aaf303e00f339cf635c4b529abf0 Mon Sep 17 00:00:00 2001 From: eltrac Date: Fri, 11 Sep 2026 17:04:36 +0800 Subject: refactor(receiver, webhook, utils): replace commons validator used java.net to create a small validator that passes the test refactor: rename validator to url, and move other url funcs here --- src/weepinbell/feed.clj | 2 +- src/weepinbell/receiver.clj | 2 +- src/weepinbell/retriever.clj | 2 +- src/weepinbell/storage/edn_storage.clj | 2 +- src/weepinbell/utils.clj | 18 ------------------ src/weepinbell/utils/url.clj | 26 ++++++++++++++++++++++++++ src/weepinbell/webhook.clj | 2 +- 7 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 src/weepinbell/utils/url.clj (limited to 'src') diff --git a/src/weepinbell/feed.clj b/src/weepinbell/feed.clj index c28e0f9..7f1963d 100644 --- a/src/weepinbell/feed.clj +++ b/src/weepinbell/feed.clj @@ -1,6 +1,6 @@ (ns weepinbell.feed (:require [clj-rss.core :as rss] - [weepinbell.utils :refer [get-domain]] + [weepinbell.utils.url :refer [get-domain]] [weepinbell.utils.digest :refer [md5-hash]]) (:import [java.time Instant])) diff --git a/src/weepinbell/receiver.clj b/src/weepinbell/receiver.clj index 21433ee..bf4bce7 100644 --- a/src/weepinbell/receiver.clj +++ b/src/weepinbell/receiver.clj @@ -2,7 +2,7 @@ "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 [valid-http-url? get-domain]] + [weepinbell.utils.url :refer [get-domain valid-http-url?]] [weepinbell.utils.digest :refer [md5-hash]])) ;; utilities diff --git a/src/weepinbell/retriever.clj b/src/weepinbell/retriever.clj index ce9fcfd..1bcd959 100644 --- a/src/weepinbell/retriever.clj +++ b/src/weepinbell/retriever.clj @@ -1,5 +1,5 @@ (ns weepinbell.retriever - (:require [weepinbell.utils :refer [get-domain]] + (:require [weepinbell.utils.url :refer [get-domain]] [weepinbell.microformats-wrapper :as mf-wrapper])) (def default-page-size 12) diff --git a/src/weepinbell/storage/edn_storage.clj b/src/weepinbell/storage/edn_storage.clj index 436d2be..ca2d9b7 100644 --- a/src/weepinbell/storage/edn_storage.clj +++ b/src/weepinbell/storage/edn_storage.clj @@ -1,6 +1,6 @@ (ns weepinbell.storage.edn-storage (:require [clojure.edn :as edn] - [weepinbell.utils :refer [get-domain get-target get-path]] + [weepinbell.utils.url :refer [get-domain get-target get-path]] [clojure.java.io :as io] [clojure.string :refer [join]])) diff --git a/src/weepinbell/utils.clj b/src/weepinbell/utils.clj index 78068ef..9c279b7 100644 --- a/src/weepinbell/utils.clj +++ b/src/weepinbell/utils.clj @@ -1,28 +1,10 @@ (ns weepinbell.utils - (:import [org.apache.commons.validator.routines UrlValidator] - [java.net URI]) (:require [org.httpkit.client :as hk] [clojure.string :as string] [clojure.walk :as walk] [clojure.java.io :as io] [taoensso.timbre :as timbre])) -(defn get-domain [url] - (when (some? url) - (-> url URI/create .getHost))) - -(defn get-path [url] - (when (some? url) - (-> url URI/create .getPath))) - -(defn get-target [wm] - (get-in wm [:webmention :target])) - -(defn valid-http-url? [url] - (let [schemes (into-array String ["http" "https"]) - validator (UrlValidator. schemes)] - (.isValid validator url))) - (defn javamap->map [map] (->> (seq map) (into {}) walk/keywordize-keys)) diff --git a/src/weepinbell/utils/url.clj b/src/weepinbell/utils/url.clj new file mode 100644 index 0000000..1e47b82 --- /dev/null +++ b/src/weepinbell/utils/url.clj @@ -0,0 +1,26 @@ +(ns weepinbell.utils.url + (:import [java.net URL URI MalformedURLException URISyntaxException IDN]) + (:require [clojure.string :as string])) + +(defn valid-http-url? [url-string] + (try + (let [url (URL. url-string) + uri (.toURI url) + scheme (.getScheme uri) + host (. IDN toASCII (-> url .getHost (string/replace "www." "")))] + (and (string/includes? host ".") ;; do not accept localhost and other non-TLD names + (or (= scheme "http") (= scheme "https")))) + (catch MalformedURLException _ false) + (catch URISyntaxException _ false))) + +(defn get-domain [url] + (when (some? url) + (-> url URI/create .getHost))) + +(defn get-path [url] + (when (some? url) + (-> url URI/create .getPath))) + +(defn get-target [wm] + (get-in wm [:webmention :target])) + diff --git a/src/weepinbell/webhook.clj b/src/weepinbell/webhook.clj index 0090243..58583b0 100644 --- a/src/weepinbell/webhook.clj +++ b/src/weepinbell/webhook.clj @@ -1,7 +1,7 @@ (ns weepinbell.webhook (:require [org.httpkit.client :as hk] [taoensso.timbre :as timbre] - [weepinbell.utils :refer [valid-http-url?]])) + [weepinbell.utils.url :refer [valid-http-url?]])) (defn trigger "Send requests to a list of webhooks." -- cgit v1.2.3