<commit_before>(ns pc.http.handlers.custom-domain
  (:require [cemerick.url :as url]
            [pc.datomic :as pcd]
            [pc.models.team :as team-model]
            [pc.profile :as profile]))

(def blacklist #{"www" "admin"})

;; starts with a letter, only contains letters, numbers, and hyphens
;; must be more than 3 characters
(def subdomain-pattern "[a-zA-Z]{1}[a-zA-Z0-9\\-]{3,}")

(defn parse-subdomain [req]
  (last (re-find (re-pattern (format "(%s)\\.%s" subdomain-pattern (profile/hostname)))
                 (:server-name req))))

(defn redirect-to-main [req]
  {:status 302
   :headers {"Location" (str (url/map->URL {:host (profile/hostname)
                                            :protocol (name (:scheme req))
                                            :port (:server-port req)
                                            :path (:uri req)
                                            :query (:query-string req)}))}
   :body ""})

(defn handle-custom-domains [handler req]
  (let [subdomain (parse-subdomain req)]
    (if-not subdomain
      (if (not= (:server-name req)
                (profile/hostname))
        (redirect-to-main req)
        (handler req))
      (if (contains? blacklist subdomain)
        (redirect-to-main subdomain)
        (handler (assoc req
                        :subdomain subdomain
                        :team (team-model/find-by-subdomain (pcd/default-db) subdomain)))))))

(defn wrap-custom-domains [handler]
  (fn [req]
    (handle-custom-domains handler req)))<commit_msg>protect against nil server names<commit_after>(ns pc.http.handlers.custom-domain
  (:require [cemerick.url :as url]
            [pc.datomic :as pcd]
            [pc.models.team :as team-model]
            [pc.profile :as profile]))

(def blacklist #{"www" "admin"})

;; starts with a letter, only contains letters, numbers, and hyphens
;; must be more than 3 characters
(def subdomain-pattern "[a-zA-Z]{1}[a-zA-Z0-9\\-]{3,}")

(defn parse-subdomain [req]
  (last (re-find (re-pattern (format "(%s)\\.%s" subdomain-pattern (profile/hostname)))
                 (:server-name req))))

(defn redirect-to-main [req]
  {:status 302
   :headers {"Location" (str (url/map->URL {:host (profile/hostname)
                                            :protocol (name (:scheme req))
                                            :port (:server-port req)
                                            :path (:uri req)
                                            :query (:query-string req)}))}
   :body ""})

(defn handle-custom-domains [handler req]
  (if-not (:server-name req)
    (handler req)
    (let [subdomain (parse-subdomain req)]
      (if-not subdomain
        (if (not= (:server-name req)
                  (profile/hostname))
          (redirect-to-main req)
          (handler req))
        (if (contains? blacklist subdomain)
          (redirect-to-main subdomain)
          (handler (assoc req
                          :subdomain subdomain
                          :team (team-model/find-by-subdomain (pcd/default-db) subdomain))))))))

(defn wrap-custom-domains [handler]
  (fn [req]
    (handle-custom-domains handler req)))
{'fraction_non_alphanumeric': 0.14375987361769352, 'fraction_numerical': 0.005055292259083728, 'mean_word_length': 1.82174688057041, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2478096', 'n_tokens_mistral': 945, 'n_tokens_neox': 908, 'n_words': 242}