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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
import NIOHTTP1
import NIOTestUtils
private final class ReadRecorder<T: Equatable>: ChannelInboundHandler, RemovableChannelHandler {
typealias InboundIn = T
enum Event: Equatable {
case channelRead(InboundIn)
case httpFrameTooLongEvent
case httpExpectationFailedEvent
static func ==(lhs: Event, rhs: Event) -> Bool {
switch (lhs, rhs) {
case (.channelRead(let b1), .channelRead(let b2)):
return b1 == b2
case (.httpFrameTooLongEvent, .httpFrameTooLongEvent):
return true
case (.httpExpectationFailedEvent, .httpExpectationFailedEvent):
return true
default:
return false
}
}
}
public var reads: [Event] = []
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
self.reads.append(.channelRead(self.unwrapInboundIn(data)))
context.fireChannelRead(data)
}
func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
switch event {
case let evt as NIOHTTPObjectAggregatorEvent where evt == NIOHTTPObjectAggregatorEvent.httpFrameTooLong:
self.reads.append(.httpFrameTooLongEvent)
case let evt as NIOHTTPObjectAggregatorEvent where evt == NIOHTTPObjectAggregatorEvent.httpExpectationFailed:
self.reads.append(.httpExpectationFailedEvent)
default:
context.fireUserInboundEventTriggered(event)
}
}
func clear() {
self.reads.removeAll(keepingCapacity: true)
}
}
private final class WriteRecorder: ChannelOutboundHandler, RemovableChannelHandler {
typealias OutboundIn = HTTPServerResponsePart
public var writes: [HTTPServerResponsePart] = []
func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
self.writes.append(self.unwrapOutboundIn(data))
context.write(data, promise: promise)
}
func clear() {
self.writes.removeAll(keepingCapacity: true)
}
}
private extension ByteBuffer {
func assertContainsOnly(_ string: String) {
let innerData = self.getString(at: self.readerIndex, length: self.readableBytes)!
XCTAssertEqual(innerData, string)
}
}
private func asHTTPResponseHead(_ response: HTTPServerResponsePart) -> HTTPResponseHead? {
switch response {
case .head(let resHead):
return resHead
default:
return nil
}
}
class NIOHTTPServerRequestAggregatorTest: XCTestCase {
var channel: EmbeddedChannel! = nil
var requestHead: HTTPRequestHead! = nil
var responseHead: HTTPResponseHead! = nil
fileprivate var readRecorder: ReadRecorder<NIOHTTPServerRequestFull>! = nil
fileprivate var writeRecorder: WriteRecorder! = nil
fileprivate var aggregatorHandler: NIOHTTPServerRequestAggregator! = nil
override func setUp() {
self.channel = EmbeddedChannel()
self.readRecorder = ReadRecorder()
self.writeRecorder = WriteRecorder()
self.aggregatorHandler = NIOHTTPServerRequestAggregator(maxContentLength: 1024 * 1024)
XCTAssertNoThrow(try channel.pipeline.addHandler(HTTPResponseEncoder()).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(self.writeRecorder).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(self.aggregatorHandler).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(self.readRecorder).wait())
self.requestHead = HTTPRequestHead(version: .http1_1, method: .PUT, uri: "/path")
self.requestHead.headers.add(name: "Host", value: "example.com")
self.requestHead.headers.add(name: "X-Test", value: "True")
self.responseHead = HTTPResponseHead(version: .http1_1, status: .ok)
self.responseHead.headers.add(name: "Server", value: "SwiftNIO")
// this activates the channel
XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 1)).wait())
}
/// Modify pipeline setup to use aggregator with a smaller `maxContentLength`
private func resetSmallHandler(maxContentLength: Int) {
XCTAssertNoThrow(try self.channel.pipeline.removeHandler(self.readRecorder!).wait())
XCTAssertNoThrow(try self.channel.pipeline.removeHandler(self.aggregatorHandler!).wait())
self.aggregatorHandler = NIOHTTPServerRequestAggregator(maxContentLength: maxContentLength)
XCTAssertNoThrow(try self.channel.pipeline.addHandler(self.aggregatorHandler).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(self.readRecorder).wait())
}
override func tearDown() {
if let channel = self.channel {
XCTAssertNoThrow(try channel.finish(acceptAlreadyClosed: true))
self.channel = nil
}
self.requestHead = nil
self.responseHead = nil
self.readRecorder = nil
self.writeRecorder = nil
self.aggregatorHandler = nil
}
func testAggregateNoBody() {
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
// Only one request should have made it through.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(NIOHTTPServerRequestFull(head: self.requestHead, body: nil))])
}
func testAggregateWithBody() {
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.body(
channel.allocator.buffer(string: "hello"))))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
// Only one request should have made it through.
XCTAssertEqual(self.readRecorder.reads, [
.channelRead(NIOHTTPServerRequestFull(
head: self.requestHead,
body: channel.allocator.buffer(string: "hello")))])
}
func testAggregateChunkedBody() {
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.body(
channel.allocator.buffer(string: "hello"))))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.body(
channel.allocator.buffer(string: "world"))))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
// Only one request should have made it through.
XCTAssertEqual(self.readRecorder.reads, [
.channelRead(NIOHTTPServerRequestFull(
head: self.requestHead,
body: channel.allocator.buffer(string: "helloworld")))])
}
func testAggregateWithTrailer() {
var reqWithChunking: HTTPRequestHead = self.requestHead
reqWithChunking.headers.add(name: "transfer-encoding", value: "chunked")
reqWithChunking.headers.add(name: "Trailer", value: "X-Trailer")
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(reqWithChunking)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.body(
channel.allocator.buffer(string: "hello"))))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.body(
channel.allocator.buffer(string: "world"))))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(
HTTPHeaders.init([("X-Trailer", "true")]))))
reqWithChunking.headers.remove(name: "Trailer")
reqWithChunking.headers.add(name: "X-Trailer", value: "true")
// Trailer headers should get moved to normal ones
XCTAssertEqual(self.readRecorder.reads, [
.channelRead(NIOHTTPServerRequestFull(
head: reqWithChunking,
body: channel.allocator.buffer(string: "helloworld")))])
}
func testOversizeRequest() {
resetSmallHandler(maxContentLength: 4)
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertTrue(channel.isActive)
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.body(
channel.allocator.buffer(string: "he"))))
XCTAssertEqual(self.writeRecorder.writes, [])
XCTAssertThrowsError(try self.channel.writeInbound(HTTPServerRequestPart.body(
channel.allocator.buffer(string: "llo")))) { error in
XCTAssertEqual(NIOHTTPObjectAggregatorError.frameTooLong, error as? NIOHTTPObjectAggregatorError)
}
let resTooLarge = HTTPResponseHead(
version: .http1_1,
status: .payloadTooLarge,
headers: HTTPHeaders([("Content-Length", "0"), ("connection", "close")]))
XCTAssertEqual(self.writeRecorder.writes, [
HTTPServerResponsePart.head(resTooLarge),
HTTPServerResponsePart.end(nil)])
XCTAssertFalse(channel.isActive)
XCTAssertThrowsError(try self.channel.writeInbound(HTTPServerRequestPart.end(nil))) { error in
XCTAssertEqual(NIOHTTPObjectAggregatorError.connectionClosed, error as? NIOHTTPObjectAggregatorError)
}
}
func testOversizedRequestWithoutKeepAlive() {
resetSmallHandler(maxContentLength: 4)
// send an HTTP/1.0 request with no keep-alive header
let requestHead: HTTPRequestHead = HTTPRequestHead(
version: .http1_0,
method: .PUT, uri: "/path",
headers: HTTPHeaders(
[("Host", "example.com"), ("X-Test", "True"), ("content-length", "5")]))
XCTAssertThrowsError(try self.channel.writeInbound(HTTPServerRequestPart.head(requestHead)))
let resTooLarge = HTTPResponseHead(
version: .http1_0,
status: .payloadTooLarge,
headers: HTTPHeaders([("Content-Length", "0"), ("connection", "close")]))
XCTAssertEqual(self.writeRecorder.writes, [
HTTPServerResponsePart.head(resTooLarge),
HTTPServerResponsePart.end(nil)])
// Connection should be closed right away
XCTAssertFalse(channel.isActive)
XCTAssertThrowsError(try self.channel.writeInbound(HTTPServerRequestPart.end(nil))) { error in
XCTAssertEqual(NIOHTTPObjectAggregatorError.connectionClosed, error as? NIOHTTPObjectAggregatorError)
}
}
func testOversizedRequestWithContentLength() {
resetSmallHandler(maxContentLength: 4)
// HTTP/1.1 uses Keep-Alive unless told otherwise
let requestHead: HTTPRequestHead = HTTPRequestHead(
version: .http1_1,
method: .PUT, uri: "/path",
headers: HTTPHeaders(
[("Host", "example.com"), ("X-Test", "True"), ("content-length", "8")]))
resetSmallHandler(maxContentLength: 4)
XCTAssertThrowsError(try self.channel.writeInbound(HTTPServerRequestPart.head(requestHead)))
let response = asHTTPResponseHead(self.writeRecorder.writes.first!)!
XCTAssertEqual(response.status, .payloadTooLarge)
XCTAssertEqual(response.headers[canonicalForm: "content-length"], ["0"])
XCTAssertEqual(response.version, requestHead.version)
// Connection should be kept open
XCTAssertTrue(channel.isActive)
// An ill-behaved client may continue writing the request
let requestParts = [
HTTPServerRequestPart.body(channel.allocator.buffer(bytes: [1, 2, 3, 4])),
HTTPServerRequestPart.body(channel.allocator.buffer(bytes: [5,6])),
HTTPServerRequestPart.body(channel.allocator.buffer(bytes: [7,8]))
]
for requestPart in requestParts {
XCTAssertThrowsError(try self.channel.writeInbound(requestPart))
}
// The aggregated message should not get passed up as it is too large.
// There should only be one "frame too long" event despite multiple writes
XCTAssertEqual(self.readRecorder.reads, [.httpFrameTooLongEvent])
XCTAssertThrowsError(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertEqual(self.readRecorder.reads, [.httpFrameTooLongEvent])
// Write another request that is small enough
var secondReqWithContentLength: HTTPRequestHead = self.requestHead
secondReqWithContentLength.headers.replaceOrAdd(name: "content-length", value: "2")
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(secondReqWithContentLength)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.body(
channel.allocator.buffer(bytes: [1]))))
XCTAssertEqual(self.readRecorder.reads, [.httpFrameTooLongEvent])
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.body(
channel.allocator.buffer(bytes: [2]))))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertEqual(self.readRecorder.reads, [
.httpFrameTooLongEvent,
.channelRead(NIOHTTPServerRequestFull(
head: secondReqWithContentLength,
body: channel.allocator.buffer(bytes: [1, 2])))])
}
}
class NIOHTTPClientResponseAggregatorTest: XCTestCase {
var channel: EmbeddedChannel! = nil
var requestHead: HTTPRequestHead! = nil
var responseHead: HTTPResponseHead! = nil
fileprivate var readRecorder: ReadRecorder<NIOHTTPClientResponseFull>! = nil
fileprivate var aggregatorHandler: NIOHTTPClientResponseAggregator! = nil
override func setUp() {
self.channel = EmbeddedChannel()
self.readRecorder = ReadRecorder()
self.aggregatorHandler = NIOHTTPClientResponseAggregator(maxContentLength: 1024 * 1024)
XCTAssertNoThrow(try channel.pipeline.addHandler(HTTPRequestEncoder()).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(self.aggregatorHandler).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(self.readRecorder).wait())
self.requestHead = HTTPRequestHead(version: .http1_1, method: .PUT, uri: "/path")
self.requestHead.headers.add(name: "Host", value: "example.com")
self.requestHead.headers.add(name: "X-Test", value: "True")
self.responseHead = HTTPResponseHead(version: .http1_1, status: .ok)
self.responseHead.headers.add(name: "Server", value: "SwiftNIO")
// this activates the channel
XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 1)).wait())
}
/// Modify pipeline setup to use aggregator with a smaller `maxContentLength`
private func resetSmallHandler(maxContentLength: Int) {
XCTAssertNoThrow(try self.channel.pipeline.removeHandler(self.readRecorder!).wait())
XCTAssertNoThrow(try self.channel.pipeline.removeHandler(self.aggregatorHandler!).wait())
self.aggregatorHandler = NIOHTTPClientResponseAggregator(maxContentLength: maxContentLength)
XCTAssertNoThrow(try self.channel.pipeline.addHandler(self.aggregatorHandler).wait())
XCTAssertNoThrow(try self.channel.pipeline.addHandler(self.readRecorder!).wait())
}
override func tearDown() {
if let channel = self.channel {
XCTAssertNoThrow(try channel.finish(acceptAlreadyClosed: true))
self.channel = nil
}
self.requestHead = nil
self.responseHead = nil
self.readRecorder = nil
self.aggregatorHandler = nil
}
func testOversizeResponseHead() {
resetSmallHandler(maxContentLength: 5)
var resHead: HTTPResponseHead = self.responseHead
resHead.headers.replaceOrAdd(name: "content-length", value: "10")
XCTAssertThrowsError(try self.channel.writeInbound(HTTPClientResponsePart.head(resHead)))
XCTAssertThrowsError(try self.channel.writeInbound(HTTPClientResponsePart.end(nil)))
// User event triggered
XCTAssertEqual(self.readRecorder.reads, [.httpFrameTooLongEvent])
}
func testOversizeResponse() {
resetSmallHandler(maxContentLength: 5)
XCTAssertNoThrow(try self.channel.writeInbound(HTTPClientResponsePart.head(self.responseHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPClientResponsePart.body(
self.channel.allocator.buffer(string: "hello"))))
XCTAssertThrowsError(try self.channel.writeInbound(
HTTPClientResponsePart.body(
self.channel.allocator.buffer(string: "world"))))
XCTAssertThrowsError(try self.channel.writeInbound(HTTPClientResponsePart.end(nil)))
// User event triggered
XCTAssertEqual(self.readRecorder.reads, [.httpFrameTooLongEvent])
}
func testAggregatedResponse() {
XCTAssertNoThrow(try self.channel.writeInbound(HTTPClientResponsePart.head(self.responseHead)))
XCTAssertNoThrow(try self.channel.writeInbound(
HTTPClientResponsePart.body(
self.channel.allocator.buffer(string: "hello"))))
XCTAssertNoThrow(try self.channel.writeInbound(
HTTPClientResponsePart.body(
self.channel.allocator.buffer(string: "world"))))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPClientResponsePart.end(HTTPHeaders([("X-Trail", "true")]))))
var aggregatedHead: HTTPResponseHead = self.responseHead
aggregatedHead.headers.add(name: "X-Trail", value: "true")
XCTAssertEqual(self.readRecorder.reads, [
.channelRead(NIOHTTPClientResponseFull(
head: aggregatedHead,
body: self.channel.allocator.buffer(string: "helloworld")))])
}
func testOkAfterOversized() {
resetSmallHandler(maxContentLength: 4)
XCTAssertNoThrow(try self.channel.writeInbound(HTTPClientResponsePart.head(self.responseHead)))
XCTAssertNoThrow(try self.channel.writeInbound(
HTTPClientResponsePart.body(
self.channel.allocator.buffer(string: "hell"))))
XCTAssertThrowsError(try self.channel.writeInbound(
HTTPClientResponsePart.body(
self.channel.allocator.buffer(string: "owor"))))
XCTAssertThrowsError(try self.channel.writeInbound(
HTTPClientResponsePart.body(
self.channel.allocator.buffer(string: "ld"))))
XCTAssertThrowsError(try self.channel.writeInbound(HTTPClientResponsePart.end(nil)))
// User event triggered
XCTAssertEqual(self.readRecorder.reads, [.httpFrameTooLongEvent])
XCTAssertNoThrow(try self.channel.writeInbound(HTTPClientResponsePart.head(self.responseHead)))
XCTAssertNoThrow(try self.channel.writeInbound(
HTTPClientResponsePart.body(
self.channel.allocator.buffer(string: "test"))))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPClientResponsePart.end(nil)))
XCTAssertEqual(self.readRecorder.reads, [
.httpFrameTooLongEvent,
.channelRead(NIOHTTPClientResponseFull(
head: self.responseHead,
body: self.channel.allocator.buffer(string: "test")))])
}
}
|