forked from http4s/http4s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Http1ServerStageSpec.scala
487 lines (405 loc) · 16.5 KB
/
Http1ServerStageSpec.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package org.http4s
package server
package blaze
import cats.data.Kleisli
import cats.effect._
import cats.effect.concurrent.Deferred
import cats.implicits._
import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets
import org.http4s.{headers => H}
import org.http4s.blaze._
import org.http4s.blaze.pipeline.Command.Connected
import org.http4s.blaze.util.TickWheelExecutor
import org.http4s.blazecore.{ResponseParser, SeqTestHead}
import org.http4s.dsl.io._
import org.http4s.headers.{Date, `Content-Length`, `Transfer-Encoding`}
import org.specs2.specification.AfterAll
import org.specs2.specification.core.Fragment
import scala.concurrent.duration._
import scala.concurrent.Await
class Http1ServerStageSpec extends Http4sSpec with AfterAll {
sequential
val tickWheel = new TickWheelExecutor()
def afterAll = tickWheel.shutdown()
def makeString(b: ByteBuffer): String = {
val p = b.position()
val a = new Array[Byte](b.remaining())
b.get(a).position(p)
new String(a)
}
def parseAndDropDate(buff: ByteBuffer): (Status, Set[Header], String) =
dropDate(ResponseParser.apply(buff))
def dropDate(resp: (Status, Set[Header], String)): (Status, Set[Header], String) = {
val hds = resp._2.filter(_.name != Date.name)
(resp._1, hds, resp._3)
}
def runRequest(
req: Seq[String],
httpApp: HttpApp[IO],
maxReqLine: Int = 4 * 1024,
maxHeaders: Int = 16 * 1024): SeqTestHead = {
val head = new SeqTestHead(
req.map(s => ByteBuffer.wrap(s.getBytes(StandardCharsets.ISO_8859_1))))
val httpStage = Http1ServerStage[IO](
httpApp,
() => AttributeMap.empty,
testExecutionContext,
enableWebSockets = true,
maxReqLine,
maxHeaders,
10 * 1024,
DefaultServiceErrorHandler,
30.seconds,
30.seconds,
tickWheel
)
pipeline.LeafBuilder(httpStage).base(head)
head.sendInboundCommand(Connected)
head
}
"Http1ServerStage: Invalid Lengths" should {
val req = "GET /foo HTTP/1.1\r\nheader: value\r\n\r\n"
val routes = HttpRoutes
.of[IO] {
case _ => Ok("foo!")
}
.orNotFound
"fail on too long of a request line" in {
val buff = Await.result(runRequest(Seq(req), routes, maxReqLine = 1).result, 5.seconds)
val str = StandardCharsets.ISO_8859_1.decode(buff.duplicate()).toString
// make sure we don't have signs of chunked encoding.
str.contains("400 Bad Request") must_== true
}
"fail on too long of a header" in {
val buff = Await.result(runRequest(Seq(req), routes, maxHeaders = 1).result, 5.seconds)
val str = StandardCharsets.ISO_8859_1.decode(buff.duplicate()).toString
// make sure we don't have signs of chunked encoding.
str.contains("400 Bad Request") must_== true
}
}
"Http1ServerStage: Common responses" should {
Fragment.foreach(ServerTestRoutes.testRequestResults.zipWithIndex) {
case ((req, (status, headers, resp)), i) =>
if (i == 7 || i == 8) // Awful temporary hack
s"Run request $i Run request: --------\n${req.split("\r\n\r\n")(0)}\n" in {
val result = Await.result(runRequest(Seq(req), ServerTestRoutes()).result, 5.seconds)
parseAndDropDate(result) must_== ((status, headers, resp))
} else
s"Run request $i Run request: --------\n${req.split("\r\n\r\n")(0)}\n" in {
val result = Await.result(runRequest(Seq(req), ServerTestRoutes()).result, 5.seconds)
parseAndDropDate(result) must_== ((status, headers, resp))
}
}
}
"Http1ServerStage: Errors" should {
val exceptionService = HttpRoutes
.of[IO] {
case GET -> Root / "sync" => sys.error("Synchronous error!")
case GET -> Root / "async" => IO.raiseError(new Exception("Asynchronous error!"))
case GET -> Root / "sync" / "422" =>
throw InvalidMessageBodyFailure("lol, I didn't even look")
case GET -> Root / "async" / "422" =>
IO.raiseError(InvalidMessageBodyFailure("lol, I didn't even look"))
}
.orNotFound
def runError(path: String) =
runRequest(List(path), exceptionService).result
.map(parseAndDropDate)
.map {
case (s, h, r) =>
val close = h.exists { h =>
h.toRaw.name == "connection".ci && h.toRaw.value == "close"
}
(s, close, r)
}
"Deal with synchronous errors" in {
val path = "GET /sync HTTP/1.1\r\nConnection:keep-alive\r\n\r\n"
val (s, c, _) = Await.result(runError(path), 10.seconds)
s must_== InternalServerError
c must_== true
}
"Call toHttpResponse on synchronous errors" in {
val path = "GET /sync/422 HTTP/1.1\r\nConnection:keep-alive\r\n\r\n"
val (s, c, _) = Await.result(runError(path), 10.seconds)
s must_== UnprocessableEntity
c must_== false
}
"Deal with asynchronous errors" in {
val path = "GET /async HTTP/1.1\r\nConnection:keep-alive\r\n\r\n"
val (s, c, _) = Await.result(runError(path), 10.seconds)
s must_== InternalServerError
c must_== true
}
"Call toHttpResponse on asynchronous errors" in {
val path = "GET /async/422 HTTP/1.1\r\nConnection:keep-alive\r\n\r\n"
val (s, c, _) = Await.result(runError(path), 10.seconds)
s must_== UnprocessableEntity
c must_== false
}
"Handle parse error" in {
val path = "THIS\u0000IS\u0000NOT\u0000HTTP"
val (s, c, _) = Await.result(runError(path), 10.seconds)
s must_== BadRequest
c must_== true
}
}
"Http1ServerStage: routes" should {
"Do not send `Transfer-Encoding: identity` response" in {
val routes = HttpRoutes
.of[IO] {
case _ =>
val headers = Headers(H.`Transfer-Encoding`(TransferCoding.identity))
IO.pure(Response[IO](headers = headers)
.withEntity("hello world"))
}
.orNotFound
// The first request will get split into two chunks, leaving the last byte off
val req = "GET /foo HTTP/1.1\r\n\r\n"
val buff = Await.result(runRequest(Seq(req), routes).result, 5.seconds)
val str = StandardCharsets.ISO_8859_1.decode(buff.duplicate()).toString
// make sure we don't have signs of chunked encoding.
str.contains("0\r\n\r\n") must_== false
str.contains("hello world") must_== true
val (_, hdrs, _) = ResponseParser.apply(buff)
hdrs.find(_.name == `Transfer-Encoding`.name) must_== None
}
"Do not send an entity or entity-headers for a status that doesn't permit it" in {
val routes: HttpApp[IO] = HttpRoutes
.of[IO] {
case _ =>
IO.pure(
Response[IO](status = Status.NotModified)
.putHeaders(`Transfer-Encoding`(TransferCoding.chunked))
.withEntity("Foo!"))
}
.orNotFound
val req = "GET /foo HTTP/1.1\r\n\r\n"
val buf = Await.result(runRequest(Seq(req), routes).result, 5.seconds)
val (status, hs, body) = ResponseParser.parseBuffer(buf)
val hss = Headers(hs.toList)
`Content-Length`.from(hss).isDefined must_== false
body must_== ""
status must_== Status.NotModified
}
"Add a date header" in {
val routes = HttpRoutes
.of[IO] {
case req => IO.pure(Response(body = req.body))
}
.orNotFound
// The first request will get split into two chunks, leaving the last byte off
val req1 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 4\r\n\r\ndone"
val buff = Await.result(runRequest(Seq(req1), routes).result, 5.seconds)
// Both responses must succeed
val (_, hdrs, _) = ResponseParser.apply(buff)
hdrs.find(_.name == Date.name) must beSome[Header]
}
"Honor an explicitly added date header" in {
val dateHeader = Date(HttpDate.Epoch)
val routes = HttpRoutes
.of[IO] {
case req => IO.pure(Response(body = req.body).withHeaders(dateHeader))
}
.orNotFound
// The first request will get split into two chunks, leaving the last byte off
val req1 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 4\r\n\r\ndone"
val buff = Await.result(runRequest(Seq(req1), routes).result, 5.seconds)
// Both responses must succeed
val (_, hdrs, _) = ResponseParser.apply(buff)
hdrs.find(_.name == Date.name) must_== Some(dateHeader)
}
"Handle routes that echos full request body for non-chunked" in {
val routes = HttpRoutes
.of[IO] {
case req => IO.pure(Response(body = req.body))
}
.orNotFound
// The first request will get split into two chunks, leaving the last byte off
val req1 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 4\r\n\r\ndone"
val (r11, r12) = req1.splitAt(req1.length - 1)
val buff = Await.result(runRequest(Seq(r11, r12), routes).result, 5.seconds)
// Both responses must succeed
parseAndDropDate(buff) must_== ((Ok, Set(H.`Content-Length`.unsafeFromLong(4)), "done"))
}
"Handle routes that consumes the full request body for non-chunked" in {
val routes = HttpRoutes
.of[IO] {
case req =>
req.as[String].map { s =>
Response().withEntity("Result: " + s)
}
}
.orNotFound
// The first request will get split into two chunks, leaving the last byte off
val req1 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 4\r\n\r\ndone"
val (r11, r12) = req1.splitAt(req1.length - 1)
val buff = Await.result(runRequest(Seq(r11, r12), routes).result, 5.seconds)
// Both responses must succeed
parseAndDropDate(buff) must_== (
(
Ok,
Set(
H.`Content-Length`.unsafeFromLong(8 + 4),
H.`Content-Type`(MediaType.text.plain, Charset.`UTF-8`)),
"Result: done"))
}
"Maintain the connection if the body is ignored but was already read to completion by the Http1Stage" in {
val routes = HttpRoutes
.of[IO] {
case _ => IO.pure(Response().withEntity("foo"))
}
.orNotFound
// The first request will get split into two chunks, leaving the last byte off
val req1 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 4\r\n\r\ndone"
val req2 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 5\r\n\r\ntotal"
val buff = Await.result(runRequest(Seq(req1, req2), routes).result, 5.seconds)
val hs = Set(
H.`Content-Type`(MediaType.text.plain, Charset.`UTF-8`),
H.`Content-Length`.unsafeFromLong(3))
// Both responses must succeed
dropDate(ResponseParser.parseBuffer(buff)) must_== ((Ok, hs, "foo"))
dropDate(ResponseParser.parseBuffer(buff)) must_== ((Ok, hs, "foo"))
}
"Drop the connection if the body is ignored and was not read to completion by the Http1Stage" in {
val routes = HttpRoutes
.of[IO] {
case _ => IO.pure(Response().withEntity("foo"))
}
.orNotFound
// The first request will get split into two chunks, leaving the last byte off
val req1 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 4\r\n\r\ndone"
val (r11, r12) = req1.splitAt(req1.length - 1)
val req2 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 5\r\n\r\ntotal"
val buff = Await.result(runRequest(Seq(r11, r12, req2), routes).result, 5.seconds)
val hs = Set(
H.`Content-Type`(MediaType.text.plain, Charset.`UTF-8`),
H.`Content-Length`.unsafeFromLong(3))
// Both responses must succeed
dropDate(ResponseParser.parseBuffer(buff)) must_== ((Ok, hs, "foo"))
buff.remaining() must_== 0
}
"Handle routes that runs the request body for non-chunked" in {
val routes = HttpRoutes
.of[IO] {
case req => req.body.compile.drain *> IO.pure(Response().withEntity("foo"))
}
.orNotFound
// The first request will get split into two chunks, leaving the last byte off
val req1 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 4\r\n\r\ndone"
val (r11, r12) = req1.splitAt(req1.length - 1)
val req2 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 5\r\n\r\ntotal"
val buff = Await.result(runRequest(Seq(r11, r12, req2), routes).result, 5.seconds)
val hs = Set(
H.`Content-Type`(MediaType.text.plain, Charset.`UTF-8`),
H.`Content-Length`.unsafeFromLong(3))
// Both responses must succeed
dropDate(ResponseParser.parseBuffer(buff)) must_== ((Ok, hs, "foo"))
dropDate(ResponseParser.parseBuffer(buff)) must_== ((Ok, hs, "foo"))
}
// Think of this as drunk HTTP pipelining
"Not die when two requests come in back to back" in {
val routes = HttpRoutes
.of[IO] {
case req =>
IO.pure(Response(body = req.body))
}
.orNotFound
// The first request will get split into two chunks, leaving the last byte off
val req1 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 4\r\n\r\ndone"
val req2 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 5\r\n\r\ntotal"
val buff = Await.result(runRequest(Seq(req1 + req2), routes).result, 5.seconds)
// Both responses must succeed
dropDate(ResponseParser.parseBuffer(buff)) must_== (
(
Ok,
Set(H.`Content-Length`.unsafeFromLong(4)),
"done"))
dropDate(ResponseParser.parseBuffer(buff)) must_== (
(
Ok,
Set(H.`Content-Length`.unsafeFromLong(5)),
"total"))
}
"Handle using the request body as the response body" in {
val routes = HttpRoutes
.of[IO] {
case req => IO.pure(Response(body = req.body))
}
.orNotFound
// The first request will get split into two chunks, leaving the last byte off
val req1 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 4\r\n\r\ndone"
val req2 = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 5\r\n\r\ntotal"
val buff = Await.result(runRequest(Seq(req1, req2), routes).result, 5.seconds)
// Both responses must succeed
dropDate(ResponseParser.parseBuffer(buff)) must_== (
(
Ok,
Set(H.`Content-Length`.unsafeFromLong(4)),
"done"))
dropDate(ResponseParser.parseBuffer(buff)) must_== (
(
Ok,
Set(H.`Content-Length`.unsafeFromLong(5)),
"total"))
}
{
def req(path: String) =
s"GET /$path HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n" +
"3\r\n" +
"foo\r\n" +
"0\r\n" +
"Foo:Bar\r\n\r\n"
val routes = HttpRoutes
.of[IO] {
case req if req.pathInfo == "/foo" =>
for {
_ <- req.body.compile.drain
hs <- req.trailerHeaders
resp <- Ok(hs.mkString)
} yield resp
case req if req.pathInfo == "/bar" =>
for {
// Don't run the body
hs <- req.trailerHeaders
resp <- Ok(hs.mkString)
} yield resp
}
.orNotFound
"Handle trailing headers" in {
val buff = Await.result(runRequest(Seq(req("foo")), routes).result, 5.seconds)
val results = dropDate(ResponseParser.parseBuffer(buff))
results._1 must_== Ok
results._3 must_== "Foo: Bar"
}
"Fail if you use the trailers before they have resolved" in {
val buff = Await.result(runRequest(Seq(req("bar")), routes).result, 5.seconds)
val results = dropDate(ResponseParser.parseBuffer(buff))
results._1 must_== InternalServerError
}
}
}
"cancels on stage shutdown" in {
Deferred[IO, Unit]
.flatMap { canceled =>
Deferred[IO, Unit].flatMap { gate =>
val req = "POST /sync HTTP/1.1\r\nConnection:keep-alive\r\nContent-Length: 4\r\n\r\ndone"
val app: HttpApp[IO] = HttpApp { req =>
gate.complete(()) >> IO.cancelable(_ => canceled.complete(()))
}
for {
head <- IO(runRequest(List(req), app))
_ <- gate.get
_ <- IO(head.closePipeline(None))
_ <- canceled.get
} yield ()
}
}
.unsafeRunTimed(3.seconds) must beSome(())
}
"Disconnect if we read an EOF" in {
val head = runRequest(Seq.empty, Kleisli.liftF(Ok("")))
Await.ready(head.result, 10.seconds)
head.closeCauses must_== Seq(None)
}
}