From acb7cd20b5526c7609845f861838e2a05aeced25 Mon Sep 17 00:00:00 2001 From: "carpentry-heartbeat[bot]" Date: Thu, 11 Jun 2026 02:10:49 +0200 Subject: [PATCH 1/2] Extract shared header-lookup helper from duplicate implementations Request.header and Response.header had identical case-insensitive lookup logic. Extract to a private top-level helper that both call. --- http.carp | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/http.carp b/http.carp index cbe70be..115d2e3 100644 --- a/http.carp +++ b/http.carp @@ -167,6 +167,20 @@ the type yourself, instead you can [parse](#parse) it.") (parse (Array.unsafe-first &splt)) &(Array.suffix &splt 1))))) +(private header-lookup) +(hidden header-lookup) +(defn header-lookup [hdrs name] + (let [lower-name &(String.ascii-to-lower name)] + (Map.kv-reduce + &(fn [acc k v] + (if (Maybe.just? &acc) + acc + (if (= &(String.ascii-to-lower k) lower-name) + (Maybe.Just @(Array.unsafe-first v)) + acc))) + (the (Maybe String) (Maybe.Nothing)) + hdrs))) + (doc Request "is a request data type. It holds the `verb` of the request, the `version`, the `uri`, the `cookies`, the `headers`, and the `body`.") (deftype Request @@ -332,17 +346,7 @@ it will return a `(Success Request)`.") (doc header "gets the first value of a header by name (case-insensitive). Returns `(Maybe String)`.") - (defn header [r name] - (let [lower-name &(String.ascii-to-lower name)] - (Map.kv-reduce - &(fn [acc k v] - (cond - (Maybe.just? &acc) acc - (= &(String.ascii-to-lower k) lower-name) - (Maybe.Just @(Array.unsafe-first v)) - acc)) - (the (Maybe String) (Maybe.Nothing)) - (headers r))))) + (defn header [r name] (header-lookup (headers r) name))) (doc Response "is a response data type. It holds the `code` of the request, the `version`, the `message`, the `cookies`, the `headers`, and the `body`.") @@ -488,17 +492,7 @@ it will return a `(Success Response)`.") (doc header "gets the first value of a header by name (case-insensitive). Returns `(Maybe String)`.") - (defn header [r name] - (let [lower-name &(String.ascii-to-lower name)] - (Map.kv-reduce - &(fn [acc k v] - (cond - (Maybe.just? &acc) acc - (= &(String.ascii-to-lower k) lower-name) - (Maybe.Just @(Array.unsafe-first v)) - acc)) - (the (Maybe String) (Maybe.Nothing)) - (headers r))))) + (defn header [r name] (header-lookup (headers r) name))) (doc Status "provides HTTP status code constants and reason phrases.") (defmodule Status From ff46e8ab9303e59fad7a8be8cc263b9f36289f25 Mon Sep 17 00:00:00 2001 From: "carpentry-heartbeat[bot]" Date: Thu, 11 Jun 2026 16:41:03 +0200 Subject: [PATCH 2/2] fix: use cond instead of nested if in header-lookup --- http.carp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/http.carp b/http.carp index 115d2e3..8c32e43 100644 --- a/http.carp +++ b/http.carp @@ -173,11 +173,11 @@ the type yourself, instead you can [parse](#parse) it.") (let [lower-name &(String.ascii-to-lower name)] (Map.kv-reduce &(fn [acc k v] - (if (Maybe.just? &acc) - acc - (if (= &(String.ascii-to-lower k) lower-name) + (cond + (Maybe.just? &acc) acc + (= &(String.ascii-to-lower k) lower-name) (Maybe.Just @(Array.unsafe-first v)) - acc))) + acc)) (the (Maybe String) (Maybe.Nothing)) hdrs)))