kasketis / netfox

A lightweight, one line setup, iOS / OSX network debugging library! 🦊
MIT License
3.67k stars 374 forks source link

Netfox makes iOS go crazy when intercepting URLRequests when httpBodyStream is set #190

Open srmadden opened 4 years ago

srmadden commented 4 years ago

We have an app that uploads a file via a POST request by doing something like:

NSInputStream *inputStream = [[NSInputStream alloc] initWithFileAtPath:fileToSend];
[request setHTTPBodyStream:inputStream];

Executing code like this when netfox is active causes several bad things to happen:

1) The request never completes (times out) 2) iOS allocates a crazy amount of memory, with hundred of thousands of CTHTTPMessage and HTTP2StreamBridge objects being allocated by CFNetwork (see screenshot from the debug memory graph in Xcode)

(Of course, neither of these things happens when netfox is not active)

I've tracked this issue to the getNFXBody() method in NFXHelper.swift and the readfully() method; specifically, this line:

return httpBodyStream?.readfully() ?? URLProtocol.property(forKey: "NFXBodyData", in: self) as? Data ?? Data()

calls readfully(), which proceeds to read the contents of the httpBodyStream object. Note that calling read() on httpBodyStream is discouraged by Apple's docs on the httpBodyStream object, which say:

The stream is returned for examination only; it’s unsafe for the caller to manipulate the stream in any way.

Modifying readfully() to simply return an empty data object makes this issue go away.

Screen Shot 2020-01-22 at 8 38 38 PM
dhcdht commented 3 years ago

Did you resolve this? @srmadden