forked from akka/akka-http
/
RequestParserSpec.scala
676 lines (597 loc) · 24.9 KB
/
RequestParserSpec.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.http.impl.engine.parsing
import akka.NotUsed
import scala.concurrent.Future
import scala.concurrent.duration._
import com.typesafe.config.{ Config, ConfigFactory }
import akka.util.ByteString
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl._
import akka.stream.TLSProtocol._
import org.scalatest.matchers.Matcher
import org.scalatest.{ BeforeAndAfterAll, FreeSpec, Matchers }
import akka.http.scaladsl.settings.ParserSettings
import akka.http.impl.engine.parsing.ParserOutput._
import akka.http.impl.util._
import akka.http.scaladsl.model.HttpEntity._
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model.HttpProtocols._
import akka.http.scaladsl.model.MediaType.{ WithFixedCharset, WithOpenCharset }
import akka.http.scaladsl.model.MediaTypes._
import akka.http.scaladsl.model.RequestEntityAcceptance.Expected
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers._
import akka.http.scaladsl.util.FastFuture
import akka.http.scaladsl.util.FastFuture._
import akka.testkit._
abstract class RequestParserSpec(mode: String, newLine: String) extends FreeSpec with Matchers with BeforeAndAfterAll {
val testConf: Config = ConfigFactory.parseString("""
akka.event-handlers = ["akka.testkit.TestEventListener"]
akka.loglevel = WARNING
akka.http.parsing.max-header-value-length = 32
akka.http.parsing.max-uri-length = 40
akka.http.parsing.max-content-length = infinite""")
implicit val system = ActorSystem(getClass.getSimpleName, testConf)
import system.dispatcher
val BOLT = HttpMethod.custom("BOLT", safe = false, idempotent = true, requestEntityAcceptance = Expected)
implicit val materializer = ActorMaterializer()
s"The request parsing logic should (mode: $mode)" - {
"properly parse a request" - {
"with no headers and no body" in new Test {
"""GET / HTTP/1.0
|
|""" should parseTo(HttpRequest(protocol = `HTTP/1.0`))
closeAfterResponseCompletion shouldEqual Seq(true)
}
"with no headers and no body but remaining content" in new Test {
Seq("""GET / HTTP/1.0
|
|POST /foo HTTP/1.0
|
|TRA""") /* beginning of TRACE request */ should generalMultiParseTo(
Right(HttpRequest(GET, "/", protocol = `HTTP/1.0`)),
Right(HttpRequest(POST, "/foo", protocol = `HTTP/1.0`)),
Left(MessageStartError(StatusCodes.BadRequest, ErrorInfo("Illegal HTTP message start"))))
closeAfterResponseCompletion shouldEqual Seq(true, true)
}
"with one header" in new Test {
"""GET / HTTP/1.1
|Host: example.com
|
|""" should parseTo(HttpRequest(headers = List(Host("example.com"))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"with absolute uri in request-target" in new Test {
"""GET http://127.0.0.1:8080/hello HTTP/1.1
|Host: 127.0.0.1:8080
|
|""" should parseTo(HttpRequest(uri = "http://127.0.0.1:8080/hello", headers = List(Host("127.0.0.1", 8080))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"with 3 headers and a body" in new Test {
"""POST /resource/yes HTTP/1.0
|User-Agent: curl/7.19.7 xyz
|Connection:keep-alive
|Content-Type: text/plain; charset=UTF-8
|Content-length: 17
|
|Shake your BOODY!""" should parseTo {
HttpRequest(POST, "/resource/yes", List(`User-Agent`("curl/7.19.7 xyz"), Connection("keep-alive")),
"Shake your BOODY!", `HTTP/1.0`)
}
closeAfterResponseCompletion shouldEqual Seq(false)
}
"with 3 headers, a body and remaining content" in new Test {
"""POST /resource/yes HTTP/1.0
|User-Agent: curl/7.19.7 xyz
|Connection:keep-alive
|Content-length: 17
|
|Shake your BOODY!GET / HTTP/1.0
|
|""" should parseTo(
HttpRequest(POST, "/resource/yes", List(`User-Agent`("curl/7.19.7 xyz"), Connection("keep-alive")),
"Shake your BOODY!".getBytes, `HTTP/1.0`),
HttpRequest(protocol = `HTTP/1.0`))
closeAfterResponseCompletion shouldEqual Seq(false, true)
}
"with multi-line headers" in new Test {
"""DELETE /abc HTTP/1.0
|User-Agent: curl/7.19.7
| abc
| xyz
|Accept: */*
|Connection: close,
| fancy
|
|""" should parseTo {
HttpRequest(DELETE, "/abc", List(`User-Agent`("curl/7.19.7 abc xyz"), Accept(MediaRanges.`*/*`),
Connection("close", "fancy")), protocol = `HTTP/1.0`)
}
closeAfterResponseCompletion shouldEqual Seq(true)
}
"byte-by-byte" in new Test {
prep {
"""PUT /resource/yes HTTP/1.1
|Content-length: 4
|Host: x
|
|ABCDPATCH"""
}.toCharArray.map(_.toString).toSeq should generalRawMultiParseTo(
Right(HttpRequest(PUT, "/resource/yes", List(Host("x")), "ABCD".getBytes)),
Left(MessageStartError(400, ErrorInfo("Illegal HTTP message start"))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"with a custom HTTP method" in new Test {
override protected def parserSettings: ParserSettings =
super.parserSettings.withCustomMethods(BOLT)
"""BOLT / HTTP/1.0
|
|""" should parseTo(HttpRequest(BOLT, "/", protocol = `HTTP/1.0`))
closeAfterResponseCompletion shouldEqual Seq(true)
}
"with a funky `Transfer-Encoding` header" in new Test {
"""PUT / HTTP/1.1
|Transfer-Encoding: foo, chunked, bar
|Host: x
|
|""" should parseTo(HttpRequest(PUT, "/", List(`Transfer-Encoding`(
TransferEncodings.Extension("foo"),
TransferEncodings.chunked, TransferEncodings.Extension("bar")), Host("x"))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"with several identical `Content-Type` headers" in new Test {
"""GET /data HTTP/1.1
|Host: x
|Content-Type: application/pdf
|Content-Type: application/pdf
|Content-Length: 0
|
|""" should parseTo(HttpRequest(GET, "/data", List(Host("x")), HttpEntity.empty(`application/pdf`)))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"with a request target starting with a double-slash" in new Test {
"""GET //foo HTTP/1.0
|
|""" should parseTo(HttpRequest(GET, Uri("http://x//foo").toHttpRequestTargetOriginForm, protocol = `HTTP/1.0`))
closeAfterResponseCompletion shouldEqual Seq(true)
}
"with additional fields in Strict-Transport-Security header" in new Test {
"""GET /hsts HTTP/1.1
|Host: x
|Strict-Transport-Security: max-age=1; preload; dummy
|
|""" should parseTo(HttpRequest(
GET,
"/hsts",
headers = List(Host("x"), `Strict-Transport-Security`(1, None)),
protocol = `HTTP/1.1`))
"""GET /hsts HTTP/1.1
|Host: x
|Strict-Transport-Security: max-age=1; dummy; preload
|
|""" should parseTo(HttpRequest(
GET,
"/hsts",
headers = List(Host("x"), `Strict-Transport-Security`(1, None)),
protocol = `HTTP/1.1`))
}
}
"properly parse a chunked request" - {
val start =
"""PATCH /data HTTP/1.1
|Transfer-Encoding: chunked
|Connection: lalelu
|Content-Type: application/pdf
|Host: ping
|
|"""
val baseRequest = HttpRequest(PATCH, "/data", List(Connection("lalelu"), Host("ping")))
"request start" in new Test {
Seq(start, "rest") should generalMultiParseTo(
Right(baseRequest.withEntity(HttpEntity.Chunked(`application/pdf`, source()))),
Left(EntityStreamError(ErrorInfo("Illegal character 'r' in chunk start"))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"message chunk with and without extension" in new Test {
Seq(
start +
"""3
|abc
|10;some=stuff;bla
|0123456789ABCDEF
|""",
"10;foo=",
"""bar
|0123456789ABCDEF
|A
|0123456789""",
"""
|0
|
|""") should generalMultiParseTo(
Right(baseRequest.withEntity(Chunked(`application/pdf`, source(
Chunk(ByteString("abc")),
Chunk(ByteString("0123456789ABCDEF"), "some=stuff;bla"),
Chunk(ByteString("0123456789ABCDEF"), "foo=bar"),
Chunk(ByteString("0123456789"), ""),
LastChunk)))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"message end" in new Test {
Seq(
start,
"""0
|
|""") should generalMultiParseTo(
Right(baseRequest.withEntity(Chunked(`application/pdf`, source(LastChunk)))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"with incorrect but harmless whitespace after chunk size" in new Test {
Seq(
start,
"""|0\u0020\u0020
|
|""") should generalMultiParseTo(
Right(baseRequest.withEntity(Chunked(`application/pdf`, source(LastChunk)))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"message end with extension and trailer" in new Test {
Seq(
start,
"""000;nice=true
|Foo: pip
| apo
|Bar: xyz
|
|""") should generalMultiParseTo(
Right(baseRequest.withEntity(Chunked(
`application/pdf`,
source(LastChunk("nice=true", List(RawHeader("Foo", "pip apo"), RawHeader("Bar", "xyz"))))))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"don't overflow the stack for large buffers of chunks" in new Test {
override val awaitAtMost = 10000.millis.dilated
val numChunks = 12000 // failed starting from 4000 with sbt started with `-Xss2m`
val oneChunk = s"1${newLine}z\n"
val manyChunks = (oneChunk * numChunks) + s"0${newLine}"
val result = multiParse(newParser)(Seq(prep(start + manyChunks)))
val HttpEntity.Chunked(_, chunks) = result.head.right.get.req.entity
val strictChunks = chunks.limit(100000).runWith(Sink.seq).awaitResult(awaitAtMost)
strictChunks.size shouldEqual numChunks
}
}
"properly parse a chunked request with additional transfer encodings" in new Test {
"""PATCH /data HTTP/1.1
|Transfer-Encoding: fancy, chunked
|Content-Type: application/pdf
|Host: ping
|
|0
|
|""" should parseTo(HttpRequest(PATCH, "/data", List(
`Transfer-Encoding`(TransferEncodings.Extension("fancy")),
Host("ping")), HttpEntity.Chunked(`application/pdf`, source(LastChunk))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"support `rawRequestUriHeader` setting" in new Test {
override protected def newParser: HttpRequestParser =
new HttpRequestParser(parserSettings, rawRequestUriHeader = true, headerParser = HttpHeaderParser(parserSettings, system.log))
"""GET /f%6f%6fbar?q=b%61z HTTP/1.1
|Host: ping
|Content-Type: application/pdf
|
|""" should parseTo(
HttpRequest(
GET,
"/foobar?q=b%61z",
List(
`Raw-Request-URI`("/f%6f%6fbar?q=b%61z"),
Host("ping")),
HttpEntity.empty(`application/pdf`)))
}
"support custom media type parsing" in new Test {
val `application/custom`: WithFixedCharset =
MediaType.customWithFixedCharset("application", "custom", HttpCharsets.`UTF-8`)
override protected def parserSettings: ParserSettings =
super.parserSettings.withCustomMediaTypes(`application/custom`)
"""POST / HTTP/1.1
|Host: ping
|Content-Type: application/custom
|Content-Length: 0
|
|""" should parseTo(
HttpRequest(
POST,
"/",
List(Host("ping")),
HttpEntity.empty(`application/custom`)))
"""POST / HTTP/1.1
|Host: ping
|Content-Type: application/json
|Content-Length: 3
|
|123""" should parseTo(
HttpRequest(
POST,
"/",
List(Host("ping")),
HttpEntity(ContentTypes.`application/json`, "123")))
"""POST / HTTP/1.1
|Host: ping
|Content-Type: text/plain; charset=UTF-8
|Content-Length: 8
|
|abcdefgh""" should parseTo(
HttpRequest(
POST,
"/",
List(Host("ping")),
HttpEntity(ContentTypes.`text/plain(UTF-8)`, "abcdefgh")))
}
"support custom media types that override existing media types" in new Test {
// Override the application/json media type and give it an open instead of fixed charset.
// This allows us to support various third-party agents which use an explicit charset.
val openJson: WithOpenCharset =
MediaType.customWithOpenCharset("application", "json")
override protected def parserSettings: ParserSettings =
super.parserSettings.withCustomMediaTypes(openJson).withMaxHeaderValueLength(64)
"""POST /abc HTTP/1.1
|Host: ping
|Content-Type: application/json
|Content-Length: 0
|
|""" should parseTo(
HttpRequest(
POST,
"/abc",
List(Host("ping")),
HttpEntity.empty(ContentType.WithMissingCharset(openJson))))
"""POST /def HTTP/1.1
|Host: ping
|Content-Type: application/json; charset=utf-8
|Content-Length: 3
|
|123""" should parseTo(
HttpRequest(
POST,
"/def",
List(Host("ping")),
HttpEntity(ContentType(openJson, HttpCharsets.`UTF-8`), "123")))
"""POST /ghi HTTP/1.1
|Host: ping
|Content-Type: application/json; charset=us-ascii
|Content-Length: 8
|
|abcdefgh""" should parseTo(
HttpRequest(
POST,
"/ghi",
List(Host("ping")),
HttpEntity(ContentType(openJson, HttpCharsets.`US-ASCII`), "abcdefgh")))
}
"reject a message chunk with" - {
val start =
"""PATCH /data HTTP/1.1
|Transfer-Encoding: chunked
|Connection: lalelu
|Host: ping
|
|"""
val baseRequest = HttpRequest(PATCH, "/data", List(Connection("lalelu"), Host("ping")),
HttpEntity.Chunked(`application/octet-stream`, source()))
"an illegal char after chunk size" in new Test {
Seq(
start,
"""15_;
|""") should generalMultiParseTo(
Right(baseRequest),
Left(EntityStreamError(ErrorInfo("Illegal character '_' in chunk start"))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"an illegal char in chunk size" in new Test {
Seq(start, "bla") should generalMultiParseTo(
Right(baseRequest),
Left(EntityStreamError(ErrorInfo("Illegal character 'l' in chunk start"))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"too-long chunk extension" in new Test {
Seq(start, "3;" + ("x" * 257)) should generalMultiParseTo(
Right(baseRequest),
Left(EntityStreamError(ErrorInfo("HTTP chunk extension length exceeds configured limit of 256 characters"))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"too-large chunk size" in new Test {
Seq(
start,
"""1a2b3c4d5e
|""") should generalMultiParseTo(
Right(baseRequest),
Left(EntityStreamError(ErrorInfo("HTTP chunk size exceeds the configured limit of 1048576 bytes"))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"an illegal chunk termination" in new Test {
Seq(
start,
"""3
|abcde""") should generalMultiParseTo(
Right(baseRequest),
Left(EntityStreamError(ErrorInfo("Illegal chunk termination"))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
"an illegal header in the trailer" in new Test {
Seq(
start,
"""0
|F@oo: pip""") should generalMultiParseTo(
Right(baseRequest),
Left(EntityStreamError(ErrorInfo("Illegal character '@' in header name"))))
closeAfterResponseCompletion shouldEqual Seq(false)
}
}
"reject a request with" - {
"an illegal HTTP method" in new Test {
"get " should parseToError(NotImplemented, ErrorInfo("Unsupported HTTP method", "get"))
"GETX " should parseToError(NotImplemented, ErrorInfo("Unsupported HTTP method", "GETX"))
}
"a too long HTTP method" in new Test {
"ABCDEFGHIJKLMNOPQ " should
parseToError(
BadRequest,
ErrorInfo(
"Unsupported HTTP method",
"HTTP method too long (started with 'ABCDEFGHIJKLMNOP'). Increase `akka.http.server.parsing.max-method-length` to support HTTP methods with more characters."))
}
"two Content-Length headers" in new Test {
"""GET / HTTP/1.1
|Content-Length: 3
|Content-Length: 4
|
|foo""" should parseToError(
BadRequest,
ErrorInfo("HTTP message must not contain more than one Content-Length header"))
}
"a too-long URI" in new Test {
"GET /2345678901234567890123456789012345678901 HTTP/1.1" should parseToError(
RequestUriTooLong,
ErrorInfo("URI length exceeds the configured limit of 40 characters"))
}
"HTTP version 1.2" in new Test {
"""GET / HTTP/1.2
|""" should parseToError(
HTTPVersionNotSupported,
ErrorInfo("The server does not support the HTTP protocol version used in the request."))
}
"with an illegal char in a header name" in new Test {
"""GET / HTTP/1.1
|User@Agent: curl/7.19.7""" should parseToError(BadRequest, ErrorInfo("Illegal character '@' in header name"))
}
"with a too-long header name" in new Test {
"""|GET / HTTP/1.1
|UserxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAgent: curl/7.19.7""" should parseToError(
RequestHeaderFieldsTooLarge, ErrorInfo("HTTP header name exceeds the configured limit of 64 characters"))
}
"with a too-long header-value" in new Test {
"""|GET / HTTP/1.1
|Fancy: 123456789012345678901234567890123""" should parseToError(
RequestHeaderFieldsTooLarge,
ErrorInfo("HTTP header value exceeds the configured limit of 32 characters"))
}
"with an invalid Content-Length header value" in new Test {
"""GET / HTTP/1.0
|Content-Length: 1.5
|
|abc""" should parseToError(BadRequest, ErrorInfo("Illegal `Content-Length` header value"))
}
"with Content-Length > Long.MaxSize" in new Test {
// content-length = (Long.MaxValue + 1) * 10, which is 0 when calculated overflow
"""PUT /resource/yes HTTP/1.1
|Content-length: 92233720368547758080
|Host: x
|
|""" should parseToError(400: StatusCode, ErrorInfo("`Content-Length` header value must not exceed 63-bit integer range"))
}
"with an illegal entity using CONNECT" in new Test {
"""CONNECT /resource/yes HTTP/1.1
|Transfer-Encoding: chunked
|Host: x
|
|""" should parseToError(422: StatusCode, ErrorInfo("CONNECT requests must not have an entity"))
}
"with an illegal entity using HEAD" in new Test {
"""HEAD /resource/yes HTTP/1.1
|Content-length: 3
|Host: x
|
|foo""" should parseToError(422: StatusCode, ErrorInfo("HEAD requests must not have an entity"))
}
"with an illegal entity using TRACE" in new Test {
"""TRACE /resource/yes HTTP/1.1
|Transfer-Encoding: chunked
|Host: x
|
|""" should parseToError(422: StatusCode, ErrorInfo("TRACE requests must not have an entity"))
}
}
}
override def afterAll() = TestKit.shutdownActorSystem(system)
private class Test {
def awaitAtMost: FiniteDuration = 3.seconds.dilated
var closeAfterResponseCompletion = Seq.empty[Boolean]
class StrictEqualHttpRequest(val req: HttpRequest) {
override def equals(other: scala.Any): Boolean = other match {
case other: StrictEqualHttpRequest ⇒
this.req.copy(entity = HttpEntity.Empty) == other.req.copy(entity = HttpEntity.Empty) &&
this.req.entity.toStrict(awaitAtMost).awaitResult(awaitAtMost) ==
other.req.entity.toStrict(awaitAtMost).awaitResult(awaitAtMost)
}
override def toString = req.toString
}
def strictEqualify[T](x: Either[T, HttpRequest]): Either[T, StrictEqualHttpRequest] =
x.right.map(new StrictEqualHttpRequest(_))
def parseTo(expected: HttpRequest*): Matcher[String] =
multiParseTo(expected: _*).compose(_ :: Nil)
def multiParseTo(expected: HttpRequest*): Matcher[Seq[String]] = multiParseTo(newParser, expected: _*)
def multiParseTo(parser: HttpRequestParser, expected: HttpRequest*): Matcher[Seq[String]] =
rawMultiParseTo(parser, expected: _*).compose(_ map prep)
def rawMultiParseTo(expected: HttpRequest*): Matcher[Seq[String]] =
rawMultiParseTo(newParser, expected: _*)
def rawMultiParseTo(parser: HttpRequestParser, expected: HttpRequest*): Matcher[Seq[String]] =
generalRawMultiParseTo(parser, expected.map(Right(_)): _*)
def parseToError(status: StatusCode, info: ErrorInfo): Matcher[String] =
generalMultiParseTo(Left(MessageStartError(status, info))).compose(_ :: Nil)
def generalMultiParseTo(expected: Either[RequestOutput, HttpRequest]*): Matcher[Seq[String]] =
generalRawMultiParseTo(expected: _*).compose(_ map prep)
def generalRawMultiParseTo(expected: Either[RequestOutput, HttpRequest]*): Matcher[Seq[String]] =
generalRawMultiParseTo(newParser, expected: _*)
def generalRawMultiParseTo(
parser: HttpRequestParser,
expected: Either[RequestOutput, HttpRequest]*): Matcher[Seq[String]] =
equal(expected.map(strictEqualify))
.matcher[Seq[Either[RequestOutput, StrictEqualHttpRequest]]] compose multiParse(parser)
def multiParse(parser: HttpRequestParser)(input: Seq[String]): Seq[Either[RequestOutput, StrictEqualHttpRequest]] =
Source(input.toList)
.map(bytes ⇒ SessionBytes(TLSPlacebo.dummySession, ByteString(bytes)))
.via(parser).named("parser")
.splitWhen(x ⇒ x.isInstanceOf[MessageStart] || x.isInstanceOf[EntityStreamError])
.prefixAndTail(1)
.collect {
case (Seq(RequestStart(method, uri, protocol, headers, createEntity, _, close)), entityParts) ⇒
closeAfterResponseCompletion :+= close
Right(HttpRequest(method, uri, headers, createEntity(entityParts), protocol))
case (Seq(x @ (MessageStartError(_, _) | EntityStreamError(_))), rest) ⇒
rest.runWith(Sink.cancelled)
Left(x)
}
.concatSubstreams
.flatMapConcat { x ⇒
Source.fromFuture {
x match {
case Right(request) ⇒ compactEntity(request.entity).fast.map(x ⇒ Right(request.withEntity(x)))
case Left(error) ⇒ FastFuture.successful(Left(error))
}
}
}
.map(strictEqualify)
.limit(100000).runWith(Sink.seq)
.awaitResult(awaitAtMost)
protected def parserSettings: ParserSettings = ParserSettings(system)
protected def newParser = new HttpRequestParser(parserSettings, false, HttpHeaderParser(parserSettings, system.log))
private def compactEntity(entity: RequestEntity): Future[RequestEntity] =
entity match {
case x: Chunked ⇒ compactEntityChunks(x.chunks).fast.map(compacted ⇒ x.copy(chunks = source(compacted: _*)))
case _ ⇒ entity.toStrict(awaitAtMost)
}
private def compactEntityChunks(data: Source[ChunkStreamPart, Any]): Future[Seq[ChunkStreamPart]] =
data.limit(100000).runWith(Sink.seq)
.fast.recover { case _: NoSuchElementException ⇒ Nil }
def prep(response: String) = response.stripMarginWithNewline(newLine)
}
def source[T](elems: T*): Source[T, NotUsed] = Source(elems.toList)
}
class RequestParserCRLFSpec extends RequestParserSpec("CRLF", "\r\n")
class RequestParserLFSpec extends RequestParserSpec("LF", "\n")