-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshortener.lisp
164 lines (140 loc) · 5.02 KB
/
shortener.lisp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
;;;; shortener.lisp
(in-package #:l1sp-org)
;;;; url shortener service
(defparameter *shortener-storage-path*
(merge-pathnames "shorturl/" *base-directory*))
(defparameter *shortener-counter*
(merge-pathnames "shorturl/counter.dat" *base-directory*))
(defparameter *counter-lock* (sb-thread:make-mutex :name "shorturl"))
(defparameter *shortener-password* "x")
(defun read-file-u32 (file)
(if (probe-file file)
(with-open-file (stream file
:direction :input
:element-type '(unsigned-byte 8))
(+ (ash (read-byte stream) 24)
(ash (read-byte stream) 16)
(ash (read-byte stream) 8)
(ash (read-byte stream) 0)))
(random 1000)))
(defun write-file-u32 (value file)
(ensure-directories-exist file)
(with-open-file (stream file
:direction :output
:if-exists :supersede
:if-does-not-exist :create
:element-type '(unsigned-byte 8))
(write-byte (ldb (byte 8 24) value) stream)
(write-byte (ldb (byte 8 16) value) stream)
(write-byte (ldb (byte 8 8) value) stream)
(write-byte (ldb (byte 8 0) value) stream)
value))
(defun next-counter ()
(block nil
(sb-thread:with-mutex (*counter-lock*)
(let ((new-value (+ (read-file-u32 *shortener-counter*)
(1+ (random 249)))))
(write-file-u32 new-value *shortener-counter*)))))
(defun key-file (key)
(flet ((endref (i)
(aref key (mod i (length key)))))
(let ((a (string (endref -1)))
(b (string (endref -2))))
(merge-pathnames (make-pathname :name key
:type "txt"
:directory (list :relative a b))
*shortener-storage-path*))))
(defun next-key ()
(string-downcase (write-to-string (next-counter) :base 36)))
(defun key-value (key)
(let ((file (key-file key)))
(with-open-file (stream file)
(read-line stream))))
(defun (setf key-value) (new-value key)
(let ((file (key-file key)))
(ensure-directories-exist file)
(with-open-file (stream file
:direction :output
:if-exists :error)
(write-line new-value stream))))
(defun url-path-key (path)
(unless (starts-with "/r/" path)
(error "Not a shortener url"))
(subseq path 3))
(defun key-url (key)
(or (ignore-errors (key-value key))
"http://l1sp.org/html/"))
(defun string-md5 (string)
(ironclad:byte-array-to-hex-string
(ironclad:digest-sequence
:md5
(babel:string-to-octets string :encoding :utf-8))))
(defun url-digest-file-name (url)
(let* ((digest (string-md5 url))
(a (string (aref digest 0)))
(b (string (aref digest 1))))
(merge-pathnames (make-pathname :name digest
:type "dat"
:directory (list :relative "url-keys" a b))
*shortener-storage-path*)))
(defun call-with-temporary-output-file (template fun)
(let* ((counter 0)
file
stream)
(tagbody
retry
(setf file (format nil "~A-tmp-~6,'0X"
(namestring template)
(incf counter)))
(unwind-protect
(progn
(setf stream (open file :direction :output :if-exists nil))
(unless stream
(go retry))
(funcall fun stream))
(when stream
(close stream))))
file))
(defun call-with-atomic-output (file fun)
(let (temp-file)
(unwind-protect
(progn
(setf temp-file (call-with-temporary-output-file file fun))
(rename-file temp-file file))
(when temp-file
(ignore-errors (delete-file temp-file))))))
(defun find-url-key (url)
(with-open-file (stream (url-digest-file-name url)
:if-does-not-exist nil)
(when stream
(read-line stream))))
(defun store-url-key (url)
(let ((key (next-key))
(pathname (url-digest-file-name url)))
(setf (key-value key) url)
(ensure-directories-exist pathname)
(call-with-atomic-output
pathname
(lambda (stream)
(write-line key stream)))
key))
(defun ensure-url-key (url)
(or (find-url-key url)
(store-url-key url)))
(defun handle-shortened ()
(case (tbnl:request-method*)
(:get
(let ((new-location (key-url (url-path-key (tbnl:script-name*)))))
(setf (tbnl:return-code*) 301)
(setf (tbnl:header-out "Location") new-location)
""))
(:post
(let ((url (tbnl:post-parameter "url"))
(password (tbnl:post-parameter "x")))
(when (equal password *shortener-password*)
(let ((key (ensure-url-key url)))
(setf (tbnl:content-type*) "text/plain")
(format nil "http://l1sp.org/r/~A" key)))))))
(defun shortener-dispatcher (request)
(when (starts-with "/r/" (tbnl:script-name request))
'handle-shortened))