Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 69 lines (61 sloc) 2.5 kb
87c06a1 @ztellman everything seems to be in working order
authored
1 ;; Copyright (c) Zachary Tellman. All rights reserved.
2 ;; The use and distribution terms for this software are covered by the
3 ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4 ;; which can be found in the file epl-v10.html at the root of this distribution.
5 ;; By using this software in any fashion, you are agreeing to be bound by
6 ;; the terms of this license.
7 ;; You must not remove this notice, or any other, from this software.
8
9 (ns aloha.requests
10 (:use
11 [potemkin]
12 [aloha netty utils])
13 (:require
14 [clojure.string :as str]
15 [clojure.tools.logging :as log])
16 (:import
17 [org.jboss.netty.channel
18 Channel]
19 [org.jboss.netty.handler.codec.http
20 HttpRequest]
21 [java.nio.channels
22 Pipe
23 Channels]
24 [org.jboss.netty.buffer
25 ChannelBufferInputStream]))
26
27 ;;;
28
29 (def-custom-map LazyMap
30 :get
31 (fn [_ data _ key default-value]
32 `(if-not (contains? ~data ~key)
33 ~default-value
34 (let [val# (get ~data ~key)]
35 (if (delay? val#)
36 @val#
37 val#)))))
38
39 (defn lazy-map [& {:as m}]
40 (LazyMap. m))
41
42 (defn assoc-request-body [request ^HttpRequest netty-request]
43 (if-not (.isChunked netty-request)
44 (let [body (.getContent netty-request)]
45 (assoc request
46 :body (when-not (= 0 (.readableBytes body))
47 (ChannelBufferInputStream. body))))
48 (let [pipe (Pipe/open)]
49 (with-meta
50 (assoc request
51 :body (Channels/newInputStream (.source pipe)))
52 {::output-stream (Channels/newOutputStream (.sink pipe))}))))
53
54 (defn transform-netty-request [^Channel channel ^HttpRequest netty-request]
55 (let [request (lazy-map
56 :scheme :http
57 :remote-addr (delay (channel-remote-host-address channel))
58 :server-name (delay (channel-local-host-address channel))
59 :server-port (delay (channel-local-port channel))
60 :request-method (delay (request-method netty-request))
61 :headers (delay (http-headers netty-request))
62 :content-type (delay (http-content-type netty-request))
63 :character-encoding (delay (http-character-encoding netty-request))
64 :uri (delay (request-uri netty-request))
65 :query-string (delay (request-query-string netty-request))
66 :content-length (delay (http-content-length netty-request)))]
67 (assoc-request-body request netty-request)))
68
Something went wrong with that request. Please try again.