Support multiple sessions

This commit is contained in:
Michiel Borkent 2020-03-29 14:01:30 +02:00
parent 313971d0d7
commit 34d36d56ba

View file

@ -13,9 +13,14 @@
(def notes-file (io/file (System/getProperty "user.home") ".notes" "notes.txt")) (def notes-file (io/file (System/getProperty "user.home") ".notes" "notes.txt"))
(io/make-parents notes-file) (io/make-parents notes-file)
(def file-lock (Object.))
;; ensure notes file exists ;; ensure notes file exists
(spit notes-file "" :append true) (spit notes-file "" :append true)
(defn write-note! [note]
(locking file-lock
(spit notes-file (str note "\n") :append true)))
;; hiccup-like ;; hiccup-like
(defn html [v] (defn html [v]
(cond (vector? v) (cond (vector? v)
@ -101,38 +106,40 @@
;; run the server ;; run the server
(with-open [server-socket (let [s (new ServerSocket 8080)] (with-open [server-socket (let [s (new ServerSocket 8080)]
(println "Server started on port 8080.") (println "Server started on port 8080.")
s) s)]
client-socket (.accept server-socket)]
(loop [] (loop []
(let [out (io/writer (.getOutputStream client-socket)) (let [client-socket (.accept server-socket)]
is (.getInputStream client-socket) (future
in (io/reader is) (with-open [conn client-socket]
[_req & headers :as response] (let [out (io/writer (.getOutputStream conn))
(loop [headers []] is (.getInputStream conn)
(let [line (.readLine in)] in (io/reader is)
(if (str/blank? line) [_req & headers :as response]
headers (loop [headers []]
(recur (conj headers line))))) (let [line (.readLine in)]
session-id (get-session-id headers) (if (str/blank? line)
form-data (let [sb (StringBuilder.)] headers
(loop [] (recur (conj headers line)))))
(when (.ready in) session-id (get-session-id headers)
(.append sb (char (.read in))) form-data (let [sb (StringBuilder.)]
(recur))) (loop []
(-> (str sb) (when (.ready in)
(java.net.URLDecoder/decode))) (.append sb (char (.read in)))
_ (when debug? (println (str/join "\n" response))) (recur)))
_ (when-not (str/blank? form-data) (-> (str sb)
(when debug? (println form-data)) (java.net.URLDecoder/decode)))
(let [note (str/replace form-data "note=" "")] _ (when debug? (println (str/join "\n" response)))
(spit notes-file (str note "\n") :append true))) _ (when-not (str/blank? form-data)
_ (when debug? (println))] (when debug? (println form-data))
(cond (let [note (str/replace form-data "note=" "")]
;; if we didn't see this session before, we want the user to re-authenticate (write-note! note)))
(not (contains? @known-sessions session-id)) _ (when debug? (println))]
(let [uuid (new-session!)] (cond
(basic-auth-response out uuid)) ;; if we didn't see this session before, we want the user to re-authenticate
(not (authenticate! session-id headers)) (not (contains? @known-sessions session-id))
(basic-auth-response out session-id) (let [uuid (new-session!)]
:else (home-response out session-id))) (basic-auth-response out uuid))
(not (authenticate! session-id headers))
(basic-auth-response out session-id)
:else (home-response out session-id))))))
(recur))) (recur)))