mbrandonw
(Brandon Williams)
January 2, 2018, 1:19am
1
Hello all and happy new year!
I just came across this bug on Linux Foundation:
opened 01:08AM - 02 Jan 18 UTC
bug
Foundation
Linux
| | |
|------------------|-----------------|…
|Previous ID | SR-6687 |
|Radar | None |
|Original Reporter | @mbrandonw |
|Type | Bug |
<details>
<summary>Environment</summary>
Swift 4, Linux
</details>
<details>
<summary>Additional Detail from JIRA</summary>
| | |
|------------------|-----------------|
|Votes | 1 |
|Component/s | Foundation |
|Labels | Bug, Linux |
|Assignee | None |
|Priority | Medium |
md5: db509a16eb34a0d3bc2241e1ef7b6a0d
</details>
**Issue Description:**
This bug only affects Linux foundation. It works properly on macOS.
If you create a copy of a request that has an \`httpBody\` set, it will get cleared (set to \`nil\`) when modifying unrelated fields on the copy of the request:
``` java
var originalRequest = URLRequest(url: URL(string: "https://www.pointfree.co")!)
originalRequest.httpMethod = "post"
originalRequest.httpBody = Data("hello world".utf8)
var request = originalRequest
XCTAssertNotNil(request.httpBody) // <- Passes
request.allHTTPHeaderFields = ["test": "something"]
XCTAssertNotNil(request.httpBody) // <- Fails
request.httpMethod = request.httpMethod?.uppercased()
XCTAssertNotNil(request.httpBody) // <- Also fails
```
This does not seem to happen when you modify the request directly:
``` java
var request = URLRequest(url: URL(string: "https://www.pointfree.co")!)
request.httpMethod = "post"
request.httpBody = Data("hello world".utf8)
XCTAssertNotNil(request.httpBody) // <- Passes
request.allHTTPHeaderFields = ["test": "something"]
XCTAssertNotNil(request.httpBody) // <- Passes
request.httpMethod = request.httpMethod?.uppercased()
XCTAssertNotNil(request.httpBody) // <- Passes
```
This behavior also does not occur if you set the \`httpBody\` on the copy and then modify fields.
It’s very strange. Does that report make sense?
mbrandonw
(Brandon Williams)
January 29, 2018, 6:32pm
2
Hello again! I just wanted to bring this bug up again ([SR-6687] [Linux] Request body clears after modifying unrelated fields · Issue #3749 · apple/swift-corelibs-foundation · GitHub ). We were just bitten by this hard. It essentially means you are not allowed to create copies of request, for then any edit to the request causes the body to be nil
out.
nnnnnnnn
(Nate Cook)
January 30, 2018, 7:12am
3
It looks like the copy-on-write step for URLRequest
misses that property (and maybe others?) — the setValues
method here is what replicates a URLRequest
into a fresh instance:
https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSURLRequest.swift#L141
rintaro
(Rintaro Ishizaki)
January 30, 2018, 8:10am
4