This repository has been archived by the owner on Nov 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Honor &stopProcessingBody flag, with tests (#51)
Issue #48
- Loading branch information
Showing
6 changed files
with
180 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// This source file is part of the Swift.org Server APIs open source project | ||
// | ||
// Copyright (c) 2017 Swift Server API project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// | ||
|
||
import Foundation | ||
import HTTP | ||
|
||
/// Simple `HTTPRequestHandler` that prints "Hello, World" as per K&R | ||
class AbortAndSendHelloHandler: HTTPRequestHandling { | ||
|
||
var chunkCalledCount=0 | ||
var chunkLength=0 | ||
|
||
func handle(request: HTTPRequest, response: HTTPResponseWriter ) -> HTTPBodyProcessing { | ||
//Assume the router gave us the right request - at least for now | ||
response.writeHeader(status: .ok, headers: [.transferEncoding: "chunked", "X-foo": "bar"]) | ||
return .processBody { (chunk, stop) in | ||
switch chunk { | ||
case .chunk(let data, let finishedProcessing): | ||
stop = true | ||
self.chunkCalledCount += 1 | ||
self.chunkLength += data.count | ||
finishedProcessing() | ||
case .end: | ||
response.writeBody("Hello, World!") | ||
response.done() | ||
default: | ||
stop = true /* don't call us anymore */ | ||
response.abort() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// This source file is part of the Swift.org Server APIs open source project | ||
// | ||
// Copyright (c) 2017 Swift Server API project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// | ||
|
||
import Foundation | ||
import HTTP | ||
|
||
/// Simple `HTTPRequestHandler` that prints "Hello, World" as per K&R | ||
class UnchunkedHelloWorldHandler: HTTPRequestHandling { | ||
func handle(request: HTTPRequest, response: HTTPResponseWriter ) -> HTTPBodyProcessing { | ||
//Assume the router gave us the right request - at least for now | ||
let responseString = "Hello, World!" | ||
response.writeHeader(status: .ok, headers: [.contentLength: "\(responseString.lengthOfBytes(using: .utf8))"]) | ||
response.writeBody(responseString) | ||
response.done() | ||
return .discardBody | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters