-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new property serverRequiresContentLength
This property turns off body streaming and causes us to encode JSON and multipart bodies synchronously instead, so we can provide a Content-Length. As usual, there is a corresponding `defaultServerRequiresContentLength` property on the `HTTPManager`, which only applies to requests contained within the environment. Add a method `HTTPManagerRequest.setDefaultEnvironmentalProperties()`. This sets `auth` and `serverRequiresContentLength` to the `HTTPManager` defaults regardless of the environment.
- Loading branch information
Kevin Ballard
committed
Aug 18, 2017
1 parent
4c2ab1b
commit 3792555
Showing
11 changed files
with
515 additions
and
177 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
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,65 @@ | ||
// | ||
// InputStream+ReadAll.swift | ||
// PMHTTP | ||
// | ||
// Created by Kevin Ballard on 8/18/17. | ||
// Copyright © 2017 Postmates. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
internal extension InputStream { | ||
/// An error thrown from `readAll()` if the stream indicates that an error occurred but | ||
/// `streamError` is `nil`. | ||
struct UnknownError: Error {} | ||
|
||
/// Reads the entire stream and returns the contents as a `Data`. | ||
/// | ||
/// - Requires: The stream must be opened before invoking this method. | ||
/// | ||
/// - Note: This method automatically calls `close()` on the stream when it's done. | ||
/// | ||
/// - Throws: An error if the stream returns an error while reading. | ||
@nonobjc | ||
func readAll() throws -> Data { | ||
let cap = 64 * 1024 | ||
var len = 0 | ||
var buf = UnsafeMutablePointer<UInt8>.allocate(capacity: cap) | ||
var data = DispatchData.empty | ||
while true { | ||
switch read(buf, maxLength: cap - len) { | ||
case 0: // EOF | ||
if len > 0 { | ||
data.append(DispatchData(bytesNoCopy: UnsafeBufferPointer(start: buf, count: len), deallocator: DispatchData.Deallocator.custom(nil, { [buf] in | ||
buf.deallocate(capacity: cap) | ||
}))) | ||
} else { | ||
buf.deallocate(capacity: cap) | ||
} | ||
// This is an ugly hack, but DispatchData doesn't have any way to go to Data directly. | ||
// This relies on the fact that OS_dispatch_data is toll-free bridged to NSData, even though Swift doesn't understand that. | ||
let nsdata = unsafeDowncast(data as AnyObject, to: NSData.self) | ||
return Data(referencing: nsdata) | ||
case let n where n > 0: | ||
let overflow: Bool | ||
(len, overflow) = Int.addWithOverflow(len, n) | ||
guard len <= cap && !overflow else { | ||
// The stream claims to have written more bytes than is available. We don't know | ||
// how to handle this. | ||
buf.deallocate(capacity: cap) | ||
throw UnknownError() | ||
} | ||
if len == cap { | ||
data.append(DispatchData(bytesNoCopy: UnsafeBufferPointer(start: buf, count: len), deallocator: DispatchData.Deallocator.custom(nil, { [buf] in | ||
buf.deallocate(capacity: cap) | ||
}))) | ||
buf = UnsafeMutablePointer<UInt8>.allocate(capacity: cap) | ||
len = 0 | ||
} | ||
default: // -1 | ||
buf.deallocate(capacity: cap) | ||
throw streamError ?? UnknownError() | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
3792555
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, you're fast! On the surface, this looks good.
With the change, the POSTs are working well with the broken server I'm using.
Thanks!
3792555
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad to hear it! I'll go ahead and publish a release with this.
3792555
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Released as v3.0.3.