forked from twitter/finagle
/
Http.scala
414 lines (348 loc) · 16.1 KB
/
Http.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package com.twitter.finagle
import com.twitter.conversions.storage._
import com.twitter.finagle.Http.param.HttpImpl
import com.twitter.finagle.client._
import com.twitter.finagle.dispatch.GenSerialClientDispatcher
import com.twitter.finagle.filter.PayloadSizeFilter
import com.twitter.finagle.http._
import com.twitter.finagle.http.exp.{StreamTransport, HttpTransport => ExpHttpTransport,
HttpClientDispatcher => ExpHttpClientDispatcher, HttpServerDispatcher => ExpHttpServerDispatcher}
import com.twitter.finagle.http.filter.{ClientContextFilter, DtabFilter, HttpNackFilter,
ServerContextFilter}
import com.twitter.finagle.http.netty.{Netty3ClientStreamTransport, Netty3ServerStreamTransport, Netty3HttpTransporter, Netty3HttpListener}
import com.twitter.finagle.netty3._
import com.twitter.finagle.param.{Monitor => _, ResponseClassifier => _, ExceptionStatsHandler => _,
Tracer => _, _}
import com.twitter.finagle.server._
import com.twitter.finagle.service.RetryBudget
import com.twitter.finagle.stats.{ExceptionStatsHandler, StatsReceiver}
import com.twitter.finagle.tracing._
import com.twitter.finagle.transport.Transport
import com.twitter.util.{Duration, Future, StorageUnit, Monitor}
import java.net.SocketAddress
/**
* A rich client with a *very* basic URL fetcher. (It does not handle
* redirects, does not have a cookie jar, etc.)
*/
trait HttpRichClient { self: Client[Request, Response] =>
def fetchUrl(url: String): Future[Response] = fetchUrl(new java.net.URL(url))
def fetchUrl(url: java.net.URL): Future[Response] = {
val addr = {
val port = if (url.getPort < 0) url.getDefaultPort else url.getPort
Address(url.getHost, port)
}
val req = http.RequestBuilder().url(url).buildGet()
val service = newService(Name.bound(addr), "")
service(req) ensure {
service.close()
}
}
}
/**
* Http protocol support, including client and server.
*/
object Http extends Client[Request, Response] with HttpRichClient
with Server[Request, Response] {
object param {
/**
* the transporter, useful for changing underlying http implementations
*/
private[finagle] case class ParameterizableTransporter(
transporterFn: Stack.Params => Transporter[Any, Any])
/**
* configure alternative http 1.1 implementations
*
* note: the listener and transporter don't strictly need to be
* coupled but we do so for ease of configuration.
*/
private[finagle] case class HttpImpl(
clientTransport: Transport[Any, Any] => StreamTransport[Request, Response],
serverTransport: Transport[Any, Any] => StreamTransport[Response, Request],
transporter: Stack.Params => Transporter[Any, Any],
listener: Stack.Params => Listener[Any, Any]
)
private[finagle] implicit object HttpImpl extends Stack.Param[HttpImpl] {
val default = Netty3Impl
}
private[finagle] val Netty3Impl: HttpImpl = HttpImpl(
new Netty3ClientStreamTransport(_),
new Netty3ServerStreamTransport(_),
Netty3HttpTransporter,
Netty3HttpListener
)
/**
* when streaming, the maximum size of http chunks.
*/
case class MaxChunkSize(size: StorageUnit)
implicit object MaxChunkSize extends Stack.Param[MaxChunkSize] {
val default = MaxChunkSize(8.kilobytes)
}
/**
* the maximum size of all headers.
*/
case class MaxHeaderSize(size: StorageUnit)
implicit object MaxHeaderSize extends Stack.Param[MaxHeaderSize] {
val default = MaxHeaderSize(8.kilobytes)
}
/**
* the maximum size of the initial line.
*/
case class MaxInitialLineSize(size: StorageUnit)
implicit object MaxInitialLineSize extends Stack.Param[MaxInitialLineSize] {
val default = MaxInitialLineSize(4.kilobytes)
}
case class MaxRequestSize(size: StorageUnit) {
require(size < 2.gigabytes,
s"MaxRequestSize should be less than 2 Gb, but was $size")
}
implicit object MaxRequestSize extends Stack.Param[MaxRequestSize] {
val default = MaxRequestSize(5.megabytes)
}
case class MaxResponseSize(size: StorageUnit) {
require(size < 2.gigabytes,
s"MaxResponseSize should be less than 2 Gb, but was $size")
}
implicit object MaxResponseSize extends Stack.Param[MaxResponseSize] {
val default = MaxResponseSize(5.megabytes)
}
case class Streaming(enabled: Boolean)
implicit object Streaming extends Stack.Param[Streaming] {
val default = Streaming(false)
}
case class Decompression(enabled: Boolean)
implicit object Decompression extends Stack.Param[Decompression] {
val default = Decompression(enabled = true)
}
case class CompressionLevel(level: Int)
implicit object CompressionLevel extends Stack.Param[CompressionLevel] {
val default = CompressionLevel(-1)
}
}
// Only record payload sizes when streaming is disabled.
private[finagle] val nonChunkedPayloadSize: Stackable[ServiceFactory[Request, Response]] =
new Stack.Module2[param.Streaming, Stats, ServiceFactory[Request, Response]] {
override def role: Stack.Role = PayloadSizeFilter.Role
override def description: String = PayloadSizeFilter.Description
override def make(
streaming: param.Streaming,
stats: Stats,
next: ServiceFactory[Request, Response]
): ServiceFactory[Request, Response] = {
if (!streaming.enabled)
new PayloadSizeFilter[Request, Response](
stats.statsReceiver, _.content.length, _.content.length).andThen(next)
else next
}
}
object Client {
val stack: Stack[ServiceFactory[Request, Response]] =
StackClient.newStack
.replace(StackClient.Role.prepConn, DelayedRelease.module)
.replace(StackClient.Role.prepFactory, DelayedRelease.module)
.replace(TraceInitializerFilter.role, new HttpClientTraceInitializer[Request, Response])
.prepend(http.TlsFilter.module)
.prepend(nonChunkedPayloadSize)
}
case class Client(
stack: Stack[ServiceFactory[Request, Response]] = Client.stack,
params: Stack.Params = StackClient.defaultParams + ProtocolLibrary("http"))
extends StdStackClient[Request, Response, Client]
with WithSessionPool[Client]
with WithDefaultLoadBalancer[Client] {
protected type In = Any
protected type Out = Any
protected def newStreamTransport(
transport: Transport[Any, Any]
): StreamTransport[Request, Response] =
new ExpHttpTransport(params[HttpImpl].clientTransport(transport))
protected def newTransporter(): Transporter[Any, Any] =
params[param.HttpImpl].transporter(params)
protected def copy1(
stack: Stack[ServiceFactory[Request, Response]] = this.stack,
params: Stack.Params = this.params
): Client = copy(stack, params)
protected def newDispatcher(transport: Transport[Any, Any]): Service[Request, Response] = {
val dispatcher = new ExpHttpClientDispatcher(
newStreamTransport(transport),
params[Stats].statsReceiver.scope(GenSerialClientDispatcher.StatsScope)
)
new ClientContextFilter[Request, Response].andThen(dispatcher)
}
def withTls(cfg: Netty3TransporterTLSConfig): Client =
configured(Transport.TLSClientEngine(Some(cfg.newEngine)))
.configured(Transporter.TLSHostname(cfg.verifyHost))
def withTls(hostname: String): Client = withTransport.tls(hostname)
def withTlsWithoutValidation: Client = withTransport.tlsWithoutValidation
def withMaxHeaderSize(size: StorageUnit): Client =
configured(param.MaxHeaderSize(size))
/**
* Configures the maximum initial line length the client can
* receive from a server.
*/
def withMaxInitialLineSize(size: StorageUnit): Client =
configured(param.MaxInitialLineSize(size))
/**
* Configures the maximum request size that the client can send.
*/
def withMaxRequestSize(size: StorageUnit): Client =
configured(param.MaxRequestSize(size))
/**
* Configures the maximum response size that client can receive.
*/
def withMaxResponseSize(size: StorageUnit): Client =
configured(param.MaxResponseSize(size))
/**
* Streaming allows applications to work with HTTP messages that have large
* (or infinite) content bodies. When this set to `true`, the message content is
* available through a [[com.twitter.io.Reader]], which gives the application a
* handle to the byte stream. If `false`, the entire message content is buffered
* into a [[com.twitter.io.Buf]].
*/
def withStreaming(enabled: Boolean): Client =
configured(param.Streaming(enabled))
/**
* Enables decompression of http content bodies.
*/
def withDecompression(enabled: Boolean): Client =
configured(param.Decompression(enabled))
/**
* The compression level to use. If passed the default value (-1) then it will use
* [[com.twitter.finagle.http.codec.TextualContentCompressor TextualContentCompressor]]
* which will compress text-like content-types with the default compression level (6).
* Otherwise, use [[org.jboss.netty.handler.codec.http.HttpContentCompressor HttpContentCompressor]]
* for all content-types with specified compression level.
*/
def withCompressionLevel(level: Int): Client =
configured(param.CompressionLevel(level))
// Java-friendly forwarders
// See https://issues.scala-lang.org/browse/SI-8905
override val withSessionPool: SessionPoolingParams[Client] =
new SessionPoolingParams(this)
override val withLoadBalancer: DefaultLoadBalancingParams[Client] =
new DefaultLoadBalancingParams(this)
override val withSessionQualifier: SessionQualificationParams[Client] =
new SessionQualificationParams(this)
override val withAdmissionControl: ClientAdmissionControlParams[Client] =
new ClientAdmissionControlParams(this)
override val withSession: SessionParams[Client] =
new SessionParams(this)
override val withTransport: ClientTransportParams[Client] =
new ClientTransportParams(this)
override def withResponseClassifier(responseClassifier: service.ResponseClassifier): Client =
super.withResponseClassifier(responseClassifier)
override def withRetryBudget(budget: RetryBudget): Client = super.withRetryBudget(budget)
override def withRetryBackoff(backoff: Stream[Duration]): Client = super.withRetryBackoff(backoff)
override def withLabel(label: String): Client = super.withLabel(label)
override def withStatsReceiver(statsReceiver: StatsReceiver): Client =
super.withStatsReceiver(statsReceiver)
override def withMonitor(monitor: Monitor): Client = super.withMonitor(monitor)
override def withTracer(tracer: Tracer): Client = super.withTracer(tracer)
override def withExceptionStatsHandler(exceptionStatsHandler: ExceptionStatsHandler): Client =
super.withExceptionStatsHandler(exceptionStatsHandler)
override def withRequestTimeout(timeout: Duration): Client = super.withRequestTimeout(timeout)
override def configured[P](psp: (P, Stack.Param[P])): Client = super.configured(psp)
override def filtered(filter: Filter[Request, Response, Request, Response]): Client =
super.filtered(filter)
}
val client: Http.Client = Client()
def newService(dest: Name, label: String): Service[Request, Response] =
client.newService(dest, label)
def newClient(dest: Name, label: String): ServiceFactory[Request, Response] =
client.newClient(dest, label)
object Server {
val stack: Stack[ServiceFactory[Request, Response]] =
StackServer.newStack
.replace(TraceInitializerFilter.role, new HttpServerTraceInitializer[Request, Response])
.replace(StackServer.Role.preparer, HttpNackFilter.module)
.prepend(nonChunkedPayloadSize)
}
case class Server(
stack: Stack[ServiceFactory[Request, Response]] = Server.stack,
params: Stack.Params = StackServer.defaultParams + ProtocolLibrary("http"))
extends StdStackServer[Request, Response, Server] {
protected type In = Any
protected type Out = Any
protected def newListener(): Listener[Any, Any] =
params[param.HttpImpl].listener(params)
protected def newStreamTransport(
transport: Transport[Any, Any]
): StreamTransport[Response, Request] =
new ExpHttpTransport(params[HttpImpl].serverTransport(transport))
protected def newDispatcher(transport: Transport[In, Out],
service: Service[Request, Response]) = {
val dtab = new DtabFilter.Finagle[Request]
val context = new ServerContextFilter[Request, Response]
val Stats(stats) = params[Stats]
val endpoint = dtab.andThen(context).andThen(service)
new ExpHttpServerDispatcher(
newStreamTransport(transport),
endpoint,
stats.scope("dispatch"))
}
protected def copy1(
stack: Stack[ServiceFactory[Request, Response]] = this.stack,
params: Stack.Params = this.params
): Server = copy(stack, params)
def withTls(cfg: Netty3ListenerTLSConfig): Server =
configured(Transport.TLSServerEngine(Some(cfg.newEngine)))
/**
* Configures the maximum request size this server can receive.
*/
def withMaxRequestSize(size: StorageUnit): Server =
configured(param.MaxRequestSize(size))
/**
* Configures the maximum response size this server can send.
*/
def withMaxResponseSize(size: StorageUnit): Server =
configured(param.MaxResponseSize(size))
/**
* Streaming allows applications to work with HTTP messages that have large
* (or infinite) content bodies. When this set to `true`, the message content is
* available through a [[com.twitter.io.Reader]], which gives the application a
* handle to the byte stream. If `false`, the entire message content is buffered
* into a [[com.twitter.io.Buf]].
*/
def withStreaming(enabled: Boolean): Server =
configured(param.Streaming(enabled))
/**
* Enables decompression of http content bodies.
*/
def withDecompression(enabled: Boolean): Server =
configured(param.Decompression(enabled))
/**
* The compression level to use. If passed the default value (-1) then it will use
* [[com.twitter.finagle.http.codec.TextualContentCompressor TextualContentCompressor]]
* which will compress text-like content-types with the default compression level (6).
* Otherwise, use [[org.jboss.netty.handler.codec.http.HttpContentCompressor HttpContentCompressor]]
* for all content-types with specified compression level.
*/
def withCompressionLevel(level: Int): Server =
configured(param.CompressionLevel(level))
/**
* Configures the maximum initial http line length the server is
* willing to accept.
*/
def withMaxInitialLineSize(size: StorageUnit): Server =
configured(param.MaxInitialLineSize(size))
// Java-friendly forwarders
// See https://issues.scala-lang.org/browse/SI-8905
override val withAdmissionControl: ServerAdmissionControlParams[Server] =
new ServerAdmissionControlParams(this)
override val withTransport: ServerTransportParams[Server] =
new ServerTransportParams[Server](this)
override def withResponseClassifier(responseClassifier: service.ResponseClassifier): Server =
super.withResponseClassifier(responseClassifier)
override def withLabel(label: String): Server = super.withLabel(label)
override def withStatsReceiver(statsReceiver: StatsReceiver): Server =
super.withStatsReceiver(statsReceiver)
override def withMonitor(monitor: Monitor): Server = super.withMonitor(monitor)
override def withTracer(tracer: Tracer): Server = super.withTracer(tracer)
override def withExceptionStatsHandler(exceptionStatsHandler: ExceptionStatsHandler): Server =
super.withExceptionStatsHandler(exceptionStatsHandler)
override def withRequestTimeout(timeout: Duration): Server = super.withRequestTimeout(timeout)
override def configured[P](psp: (P, Stack.Param[P])): Server = super.configured(psp)
}
val server: Http.Server = Server()
def serve(addr: SocketAddress, service: ServiceFactory[Request, Response]): ListeningServer =
server.serve(addr, service)
}