Skip to content

SR-12135: Data(contentsOf:) does not account for failed HTTP response codes. #2742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions Sources/FoundationNetworking/URLSession/NetworkingSpecific.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ class _NSNonfileURLContentLoader: _NSNonfileURLContentLoading {

@usableFromInline
func contentsOf(url: URL) throws -> (result: NSData, textEncodingNameIfAvailable: String?) {

func cocoaError(with error: Error? = nil) -> Error {
var userInfo: [String: Any] = [:]
if let error = error {
userInfo[NSUnderlyingErrorKey] = error
}
return CocoaError.error(.fileReadUnknown, userInfo: userInfo, url: url)
}

var urlResponse: URLResponse?
let session = URLSession(configuration: URLSessionConfiguration.default)
let cond = NSCondition()
Expand All @@ -63,10 +72,25 @@ class _NSNonfileURLContentLoader: _NSNonfileURLContentLoading {
cond.wait()
}
cond.unlock()


guard resError == nil else {
throw cocoaError(with: resError)
}

guard let data = resData else {
throw resError!
throw cocoaError()
}

if let statusCode = (urlResponse as? HTTPURLResponse)?.statusCode {
switch statusCode {
// These are the only valid response codes that data will be returned for, all other codes will be treated as error.
case 101, 200...399, 401, 407:
return (NSData(bytes: UnsafeMutableRawPointer(mutating: (data as NSData).bytes), length: data.count), urlResponse?.textEncodingName)

default:
break
}
}
return (NSData(bytes: UnsafeMutableRawPointer(mutating: (data as NSData).bytes), length: data.count), urlResponse?.textEncodingName)
throw cocoaError()
}
}
6 changes: 5 additions & 1 deletion Tests/Foundation/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,11 @@ public class TestURLSessionServer {
bodyData: helloWorld)
}

return _HTTPResponse(response: .OK, body: capitals[String(uri.dropFirst())]!)
guard let capital = capitals[String(uri.dropFirst())] else {
return _HTTPResponse(response: .NOTFOUND, body: "Not Found")
}
return _HTTPResponse(response: .OK, body: capital)

}
}

Expand Down
45 changes: 38 additions & 7 deletions Tests/Foundation/Tests/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1517,13 +1517,44 @@ extension TestNSData {
#endif
}

func test_contentsOfURL() {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
let url = URL(string: urlString)!
let contents = NSData(contentsOf: url)
XCTAssertNotNil(contents)
if let contents = contents {
XCTAssertTrue(contents.length > 0)
func test_contentsOfURL() throws {
do {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
let url = try XCTUnwrap(URL(string: urlString))
let contents = NSData(contentsOf: url)
XCTAssertNotNil(contents)
if let contents = contents {
XCTAssertTrue(contents.length > 0)
}
}

do {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/NotFound"
let url = try XCTUnwrap(URL(string: urlString))
XCTAssertNil(NSData(contentsOf: url))
do {
_ = try NSData(contentsOf: url, options: [])
XCTFail("NSData(contentsOf:options: did not throw")
} catch let error as NSError {
if let nserror = error as? NSError {
XCTAssertEqual(NSCocoaErrorDomain, nserror.domain)
XCTAssertEqual(CocoaError.fileReadUnknown.rawValue, nserror.code)
} else {
XCTFail("Not an NSError")
}
}

do {
_ = try Data(contentsOf: url)
XCTFail("Data(contentsOf:options: did not throw")
} catch let error as NSError {
if let nserror = error as? NSError {
XCTAssertEqual(NSCocoaErrorDomain, nserror.domain)
XCTAssertEqual(CocoaError.fileReadUnknown.rawValue, nserror.code)
} else {
XCTFail("Not an NSError")
}
}
}
}

Expand Down