forked from babashka/babashka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.clj
executable file
·147 lines (132 loc) · 5.22 KB
/
notes.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env bb
(import (java.net ServerSocket))
(require '[clojure.java.io :as io]
'[clojure.string :as str])
(def debug? true)
(def user "admin")
(def password "admin")
(def base64 (-> (.getEncoder java.util.Base64)
(.encodeToString (.getBytes (str user ":" password)))))
(def notes-file (io/file (System/getProperty "user.home") ".notes" "notes.txt"))
(def file-lock (Object.))
(defn write-note! [note]
(locking file-lock
(io/make-parents notes-file)
(spit notes-file (str note "\n") :append true)))
;; hiccup-like
(defn html [v]
(cond (vector? v)
(let [tag (first v)
attrs (second v)
attrs (when (map? attrs) attrs)
elts (if attrs (nnext v) (next v))
tag-name (name tag)]
(format "<%s%s>%s</%s>\n" tag-name (html attrs) (html elts) tag-name))
(map? v)
(str/join ""
(map (fn [[k v]]
(format " %s=\"%s\"" (name k) v)) v))
(seq? v)
(str/join " " (map html v))
:else (str v)))
(defn write-response [out session-id status headers content]
(let [cookie-header (str "Set-Cookie: notes-id=" session-id)
headers (str/join "\r\n" (conj headers cookie-header))
response (str "HTTP/1.1 " status "\r\n"
(str headers "\r\n")
"Content-Length: " (if content (count content)
0)
"\r\n\r\n"
(when content
(str content)))]
(when debug? (println response))
(binding [*out* out]
(print response)
(flush))))
;; the home page
(defn home-response [out session-id]
(let [body (str
"<!DOCTYPE html>\n"
(html
[:html
[:head
[:title "Notes"]]
[:body
[:h1 "Notes"]
[:pre (when (.exists notes-file)
(slurp notes-file))]
[:form {:action "/" :method "post"}
[:input {:type "text" :name "note"}]
[:input {:type "submit" :value "Submit"}]]]]))]
(write-response out session-id "200 OK" nil body)))
(defn basic-auth-response [out session-id]
(write-response out session-id
"401 Unauthorized"
["WWW-Authenticate: Basic realm=\"notes\""]
nil))
(def known-sessions
(atom #{}))
(defn new-session! []
(let [uuid (str (java.util.UUID/randomUUID))]
(swap! known-sessions conj uuid)
uuid))
(defn get-session-id [headers]
(if-let [cookie-header (first (filter #(str/starts-with? % "Cookie: ") headers))]
(let [parts (str/split cookie-header #"; ")]
(if-let [notes-id (first (filter #(str/starts-with? % "notes-id") parts))]
(str/replace notes-id "notes-id=" "")
(new-session!)))
(new-session!)))
(defn basic-auth-header [headers]
(some #(str/starts-with? % "Basic-Auth: ") headers))
(def authenticated-sessions
(atom #{}))
(defn authenticate! [session-id headers]
(or (contains? @authenticated-sessions session-id)
(when (some #(= % (str "Authorization: Basic " base64)) headers)
(swap! authenticated-sessions conj session-id)
true)))
;; run the server
(with-open [server-socket (let [s (new ServerSocket 8080)]
(println "Server started on port 8080.")
s)]
(loop []
(let [client-socket (.accept server-socket)]
(future
(with-open [conn client-socket]
(try
(let [out (io/writer (.getOutputStream conn))
is (.getInputStream conn)
in (io/reader is)
[_req & headers :as response]
(loop [headers []]
(let [line (.readLine in)]
(if (str/blank? line)
headers
(recur (conj headers line)))))
session-id (get-session-id headers)
form-data (let [sb (StringBuilder.)]
(loop []
(when (.ready in)
(.append sb (char (.read in)))
(recur)))
(-> (str sb)
(java.net.URLDecoder/decode)))
_ (when debug? (println (str/join "\n" response)))
_ (when-not (str/blank? form-data)
(when debug? (println form-data))
(let [note (str/replace form-data "note=" "")]
(write-note! note)))
_ (when debug? (println))]
(cond
;; if we didn't see this session before, we want the user to re-authenticate
(not (contains? @known-sessions session-id))
(let [uuid (new-session!)]
(basic-auth-response out uuid))
(not (authenticate! session-id headers))
(basic-auth-response out session-id)
:else (home-response out session-id)))
(catch Throwable t
(binding [*err* *out*]
(println t)))))))
(recur)))