diff options
| author | eltrac <hi@eltr.ac> | 2026-05-03 10:21:29 +0800 |
|---|---|---|
| committer | eltrac <hi@eltr.ac> | 2026-05-03 10:21:29 +0800 |
| commit | 4a652fffff1172ba6fc18413ceb5cbe1538c7a16 (patch) | |
| tree | bdbe77bfea98d2fd009382a4a0ec4e17bef4f474 /src | |
| parent | 586fcbf2644d554ea31c8c383189a9ef2ec37945 (diff) | |
refactor(receiver): (verify-request) returns only msg string, not entire response
HTTP-layer reponse should be handled by receive function (which acts as
a controller that does HTTP things), verify-request function should only
focus on error detection.
Code within a function should be within the same layer of abstraction.
Diffstat (limited to 'src')
| -rw-r--r-- | src/weepinbell/receiver.clj | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/src/weepinbell/receiver.clj b/src/weepinbell/receiver.clj index 6e0327e..dabf3c3 100644 --- a/src/weepinbell/receiver.clj +++ b/src/weepinbell/receiver.clj @@ -17,19 +17,20 @@ (some #{target-domain} allow-list))) (defn- verify-request + "Verify source and target based on URLs, return error message (string) if any. Return nil if none." [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. - (not (validate-urls source target)) "Invalid URLs. Source and target have to be valid http(s) urls." - ;; The receiver MUST reject the request - ;; if the source URL is the same as the target URL. - (= source target) "Source and target are identical." - (not (validate-target target)) "Domain of target is not allowed. This receiver does not receive webmention on its behalf.")] - {:status 400 - :headers {"Content-Type" "application/json; charset=utf-8"} - :body {:status 400 - :message 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. + (not (validate-urls source target)) + "Invalid URLs. Source and target have to be valid http(s) urls." + ;; The receiver MUST reject the request + ;; if the source URL is the same as the target URL. + (= source target) + "Source and target are identical." + ;; The receiver only accetps webmentions on behalf of configured domains. + (not (validate-target target opts)) + "Domain of target is not allowed. This receiver does not receive webmention on its behalf.")) ;; receiving logic @@ -41,9 +42,12 @@ source (some-> s (string/replace #"#.*$" "")) target (some-> t (string/replace #"#.*$" ""))] ;; Request verification - (if-let [errror-response (verify-request source target opts)] + (if-let [error-msg (verify-request source target opts)] ;; respond if request verification failed - errror-response + {:status 400 + :headers {"Content-Type" "application/json; charset=utf-8"} + :body {:status 400 + :message error-msg}} ;; proceed if request is ok (do ;; The receiver SHOULD check that target is a valid |
