-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.clj
226 lines (210 loc) · 7.8 KB
/
handler.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
(ns iny.http1.handler
(:require [clojure.tools.logging :as log]
[potemkin :refer [def-derived-map]]
[iny.http.date :refer [schedule-date-value-update]]
[iny.http.method :refer [http-methods get?]]
[iny.http.status :refer [->status]]
[iny.http.body :refer [->buffer release]]
[iny.http1.headers :refer [->headers headers->map headers-with-date]])
(:import [java.io
IOException]
[java.net
InetSocketAddress]
[io.netty.buffer
ByteBuf
ByteBufInputStream]
[io.netty.channel
ChannelFuture
ChannelFutureListener
ChannelHandlerContext
ChannelInboundHandler]
[io.netty.handler.codec.http
HttpUtil
HttpContent
LastHttpContent
HttpRequest
HttpResponse
HttpVersion
HttpHeaderNames
HttpResponseStatus
DefaultHttpContent
DefaultHttpResponse]
[io.netty.handler.codec.http.multipart
DefaultHttpDataFactory
HttpPostRequestDecoder
InterfaceHttpData
InterfaceHttpData$HttpDataType
Attribute
FileUpload]))
(defmacro cond-let
[& clauses]
(when clauses
(list 'if-let (first clauses)
(if (next clauses)
(second clauses)
(throw (IllegalArgumentException.
"cond-let requires an even number of forms")))
(cons 'iny.http1.handler/cond-let (next (next clauses))))))
(defn ^ChannelFuture write-response
[^ChannelHandlerContext ctx
^HttpResponse head
^HttpContent body]
(.write ctx head (.voidPromise ctx))
;; want to use some-> here but then pos? gets boxed...
(when (and body (-> body (.content) (.readableBytes) (pos?)))
(.write ctx body (.voidPromise ctx)))
(.writeAndFlush ctx LastHttpContent/EMPTY_LAST_CONTENT))
(defn ^ChannelFuture respond-500
[^ChannelHandlerContext ctx
^Throwable ex]
(let [error-head (doto (DefaultHttpResponse.
HttpVersion/HTTP_1_1
HttpResponseStatus/INTERNAL_SERVER_ERROR
(headers-with-date))
(HttpUtil/setContentLength 0))]
(write-response ctx error-head nil)))
(defn ^ChannelFuture respond
[^ChannelHandlerContext ctx
{:keys [body headers status]}]
(let [status (->status status)
buffer (->buffer body ctx)
headers (->headers headers)
response (DefaultHttpResponse.
HttpVersion/HTTP_1_1
status
headers)
response-body (DefaultHttpContent. buffer)]
(HttpUtil/setContentLength response (.readableBytes buffer))
(write-response ctx response response-body)))
(defn request-method
[^HttpRequest req]
(->> req (.method) (.name) (.get http-methods)))
(def-derived-map RingRequest
[^ChannelHandlerContext ctx
^HttpRequest req
^ByteBuf body
q-at]
:uri (if (not (neg? ^int q-at))
(.substring (.uri req) 0 q-at)
(.uri req))
:query-string (if (not (neg? ^int q-at))
(.substring (.uri req) q-at))
:headers (headers->map (.headers req))
:request-method (request-method req)
:scheme :http
:body (ByteBufInputStream. body false)
:server-name (some-> ctx (.channel) ^InetSocketAddress (.localAddress) (.getHostName))
:server-port (some-> ctx (.channel) ^InetSocketAddress (.localAddress) (.getPort))
:remote-addr (some-> ctx (.channel) ^InetSocketAddress (.remoteAddress) (.getAddress) (.getHostAddress))
:iny/keep-alive (HttpUtil/isKeepAlive req))
(defn netty->ring-request
[^ChannelHandlerContext ctx
^ByteBuf body
^HttpRequest req]
(->RingRequest ctx req body (.indexOf (.uri req) (int 63))))
(defn content-length'
[^HttpRequest req]
(when-let [header-value (-> req
(.headers)
(.get HttpHeaderNames/CONTENT_LENGTH))]
(try
(Long/parseLong header-value)
(catch Throwable e
(log/debug "Wrong content length header value" e)
nil))))
(def content-length (memoize content-length'))
(defn content-known-empty?
[^HttpRequest req]
(= (content-length req) 0))
(def data-factory
(DefaultHttpDataFactory. DefaultHttpDataFactory/MINSIZE))
(defprotocol LogUpload
(log-it [_]))
(extend-protocol LogUpload
Attribute
(log-it [^Attribute attr]
(log/info {(.getName attr) (.getValue attr)}))
FileUpload
(log-it [^FileUpload upload]
(log/info
{:field-name (.getName upload)
:filename (.getFilename upload)
:content-type (.getContentType upload)
:contents (if (.isInMemory upload)
(.getByteBuf upload)
(.getFile upload))
:size (.definedLength upload)})))
(defn ^ChannelInboundHandler http-handler
[user-handler]
(let [body-buf (atom nil)
request (atom nil)
body-decoder (atom nil)]
(reify
ChannelInboundHandler
(handlerAdded [_ ctx])
(handlerRemoved [_ ctx])
(exceptionCaught [_ ctx ex]
(log/error ex)
(when-not (instance? IOException ex)
(respond-500 ctx ex))
(.close ctx))
(channelRegistered [_ ctx]
(schedule-date-value-update ctx))
(channelUnregistered [_ ctx])
(channelActive [_ ctx])
(channelInactive [_ ctx])
(channelRead [_ ctx msg]
(cond
(instance? HttpRequest msg)
(cond
;; request without body
(or (get? msg) (content-known-empty? msg))
(let [ftr (->> msg
(netty->ring-request ctx (->buffer nil))
(user-handler)
(respond ctx))]
(when-not (HttpUtil/isKeepAlive msg)
(.addListener ftr ChannelFutureListener/CLOSE)))
;; multipart
(HttpPostRequestDecoder/isMultipart msg)
(let [decoder-instance (HttpPostRequestDecoder. data-factory msg)]
(reset! request (netty->ring-request ctx (->buffer nil) msg))
(reset! body-decoder decoder-instance))
;; non-multipart request with body
:else
(let [allocator (.alloc ctx)
buffer (if-let [len (content-length msg)]
(.buffer allocator len)
(.buffer allocator))]
(reset! request (netty->ring-request ctx buffer msg))
(reset! body-buf buffer)))
(instance? HttpContent msg)
(cond-let
[^ByteBuf buffer @body-buf]
(do
(.writeBytes buffer (.content ^HttpContent msg))
(when (instance? LastHttpContent msg)
(->> @request
(user-handler)
(respond ctx))
(release buffer)
(reset! request nil)
(reset! body-buf nil)))
[^HttpPostRequestDecoder decoder @body-decoder]
(do
(.offer decoder ^HttpContent msg)
(when (instance? LastHttpContent msg)
(->> @request
(user-handler)
(respond ctx))
(doseq [data (.getBodyHttpDatas decoder)]
(log-it data))
(.destroy decoder)
(reset! body-decoder nil))))
; :else
; (log/info (class msg))
)
(release msg))
(channelReadComplete [_ ctx])
(userEventTriggered [_ ctx event])
(channelWritabilityChanged [_ ctx]))))