This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
/
HttpServerConnection.scala
207 lines (193 loc) · 11.2 KB
/
HttpServerConnection.scala
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
/*
* Copyright (C) 2011-2013 spray.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package spray.can.server
import java.net.InetSocketAddress
import scala.concurrent.duration.Duration
import akka.actor.{ SupervisorStrategy, Terminated, ReceiveTimeout, ActorRef }
import akka.io.Tcp
import spray.can.server.StatsSupport.StatsHolder
import spray.io._
import spray.can.Http
import spray.http.{ SetTimeoutTimeout, SetRequestTimeout }
private[can] class HttpServerConnection(tcpConnection: ActorRef,
userLevelListener: ActorRef,
pipelineStage: RawPipelineStage[ServerFrontend.Context with SslTlsContext],
remoteAddress: InetSocketAddress,
localAddress: InetSocketAddress,
settings: ServerSettings)(implicit val sslEngineProvider: ServerSSLEngineProvider)
extends ConnectionHandler { actor ⇒
userLevelListener ! Http.Connected(remoteAddress, localAddress)
context.setReceiveTimeout(settings.registrationTimeout)
// we cannot sensibly recover from crashes
override def supervisorStrategy = SupervisorStrategy.stoppingStrategy
def receive: Receive = {
case Tcp.Register(handler, keepOpenOnPeerClosed, _) ⇒ register(handler, keepOpenOnPeerClosed)
case Http.Register(handler, keepOpenOnPeerClosed, fastPath) ⇒ register(handler, keepOpenOnPeerClosed, fastPath)
case ReceiveTimeout ⇒
log.warning("Configured registration timeout of {} expired, stopping", settings.registrationTimeout)
tcpConnection ! Http.Close
context.watch(tcpConnection)
context.become {
case _: Http.ConnectionClosed ⇒ // ignore
case Terminated(`tcpConnection`) ⇒ context.stop(self)
case ReceiveTimeout ⇒ context.stop(self)
}
}
def register(handler: ActorRef, keepOpenOnPeerClosed: Boolean, fastPath: Http.FastPath = Http.EmptyFastPath): Unit = {
context.setReceiveTimeout(Duration.Undefined)
tcpConnection ! Tcp.Register(self, keepOpenOnPeerClosed)
context.watch(tcpConnection)
context.watch(handler)
context.become(running(tcpConnection, pipelineStage, pipelineContext(handler, fastPath)))
}
def pipelineContext(_handler: ActorRef, _fastPath: Http.FastPath) = new SslTlsContext with ServerFrontend.Context {
def handler = _handler
def fastPath = _fastPath
def actorContext = context
def remoteAddress = actor.remoteAddress
def localAddress = actor.localAddress
def log = actor.log
def sslEngine = sslEngineProvider(this)
}
override def running(tcpConnection: ActorRef, pipelines: Pipelines): Receive =
super.running(tcpConnection, pipelines) orElse {
case x: SetRequestTimeout ⇒ pipelines.commandPipeline(CommandWrapper(x))
case x: SetTimeoutTimeout ⇒ pipelines.commandPipeline(CommandWrapper(x))
}
}
private[can] object HttpServerConnection {
/**
* The HttpServerConnection pipeline setup:
*
* |------------------------------------------------------------------------------------------
* | ServerFrontend: converts HttpMessagePart, Closed and SendCompleted events to
* | MessageHandlerDispatch.DispatchCommand,
* | generates HttpResponsePartRenderingContext
* |------------------------------------------------------------------------------------------
* /\ |
* | HttpMessagePart | HttpResponsePartRenderingContext
* | IOServer.Closed | IOServer.Tell
* | IOServer.SentOk |
* | TickGenerator.Tick |
* | \/
* |------------------------------------------------------------------------------------------
* | RequestChunkAggregation: listens to HttpMessagePart events, generates HttpRequest events
* |------------------------------------------------------------------------------------------
* /\ |
* | HttpMessagePart | HttpResponsePartRenderingContext
* | IOServer.Closed | IOServer.Tell
* | IOServer.SentOk |
* | TickGenerator.Tick |
* | \/
* |------------------------------------------------------------------------------------------
* | PipeliningLimiter: throttles incoming requests according to the PipeliningLimit, listens
* | to HttpResponsePartRenderingContext commands and HttpRequestPart events,
* | generates StopReading and ResumeReading commands
* |------------------------------------------------------------------------------------------
* /\ |
* | HttpMessagePart | HttpResponsePartRenderingContext
* | IOServer.Closed | IOServer.Tell
* | IOServer.SentOk | IOServer.StopReading
* | TickGenerator.Tick | IOServer.ResumeReading
* | \/
* |------------------------------------------------------------------------------------------
* | StatsSupport: listens to most commands and events to collect statistics
* |------------------------------------------------------------------------------------------
* /\ |
* | HttpMessagePart | HttpResponsePartRenderingContext
* | IOServer.Closed | IOServer.Tell
* | IOServer.SentOk | IOServer.StopReading
* | TickGenerator.Tick | IOServer.ResumeReading
* | \/
* |------------------------------------------------------------------------------------------
* | RemoteAddressHeaderSupport: add `Remote-Address` headers to incoming requests
* |------------------------------------------------------------------------------------------
* /\ |
* | HttpMessagePart | HttpResponsePartRenderingContext
* | IOServer.Closed | IOServer.Tell
* | IOServer.SentOk | IOServer.StopReading
* | TickGenerator.Tick | IOServer.ResumeReading
* | \/
* |------------------------------------------------------------------------------------------
* | RequestParsing: converts Received events to HttpMessagePart,
* | generates HttpResponsePartRenderingContext (in case of errors)
* |------------------------------------------------------------------------------------------
* /\ |
* | IOServer.Closed | HttpResponsePartRenderingContext
* | IOServer.SentOk | IOServer.Tell
* | IOServer.Received | IOServer.StopReading
* | TickGenerator.Tick | IOServer.ResumeReading
* | \/
* |------------------------------------------------------------------------------------------
* | ResponseRendering: converts HttpResponsePartRenderingContext
* | to Send and Close commands
* |------------------------------------------------------------------------------------------
* /\ |
* | IOServer.Closed | IOServer.Send
* | IOServer.SentOk | IOServer.Close
* | IOServer.Received | IOServer.Tell
* | TickGenerator.Tick | IOServer.StopReading
* | | IOServer.ResumeReading
* | \/
* |------------------------------------------------------------------------------------------
* | ConnectionTimeouts: listens to Received events and Send commands and
* | TickGenerator.Tick, generates Close commands
* |------------------------------------------------------------------------------------------
* /\ |
* | IOServer.Closed | IOServer.Send
* | IOServer.SentOk | IOServer.Close
* | IOServer.Received | IOServer.Tell
* | TickGenerator.Tick | IOServer.StopReading
* | | IOServer.ResumeReading
* | \/
* |------------------------------------------------------------------------------------------
* | SslTlsSupport: listens to event Send and Close commands and Received events,
* | provides transparent encryption/decryption in both directions
* |------------------------------------------------------------------------------------------
* /\ |
* | IOServer.Closed | IOServer.Send
* | IOServer.SentOk | IOServer.Close
* | IOServer.Received | IOServer.Tell
* | TickGenerator.Tick | IOServer.StopReading
* | | IOServer.ResumeReading
* | \/
* |------------------------------------------------------------------------------------------
* | TickGenerator: listens to Closed events,
* | dispatches TickGenerator.Tick events to the head of the event PL
* |------------------------------------------------------------------------------------------
* /\ |
* | IOServer.Closed | IOServer.Send
* | IOServer.SentOk | IOServer.Close
* | IOServer.Received | IOServer.Tell
* | TickGenerator.Tick | IOServer.StopReading
* | | IOServer.ResumeReading
* | \/
*/
def pipelineStage(settings: ServerSettings, statsHolder: Option[StatsHolder]) = {
import settings._
ServerFrontend(settings) >>
RequestChunkAggregation(requestChunkAggregationLimit) ? (requestChunkAggregationLimit > 0) >>
PipeliningLimiter(pipeliningLimit) ? (pipeliningLimit > 0) >>
StatsSupport(statsHolder.get) ? statsSupport >>
RemoteAddressHeaderSupport ? remoteAddressHeader >>
RequestParsing(settings) >>
ResponseRendering(settings) >>
ConnectionTimeouts(idleTimeout) ? (reapingCycle.isFinite && idleTimeout.isFinite) >>
SslTlsSupport(false) ? sslEncryption >>
TickGenerator(reapingCycle) ? (reapingCycle.isFinite && (idleTimeout.isFinite || requestTimeout.isFinite)) >>
BackPressureHandling(backpressureSettings.get.noAckRate, backpressureSettings.get.readingLowWatermark) ? backpressureSettings.isDefined
}
}