0

I'm using a custom NSURLCache to intercept calls for certain web pages in order to modify them dynamically. Inside cachedResponseForRequest: I modify the request, then send it out using sendSynchronousRequest. This works very well until you attempt to submit a form. If there is post data it fails.

If i remove the HTTPBody and set the request to GET, it succeeds. But i need it to work with POST. If i send the request asynchronously it also works which is why i can't figure out why the synchronous request fails.

Here's my stack trace:

Date/Time: 2010-01-17 12:37:55.416 -0800
OS Version: iPhone OS 3.1.2 (7D11)
Report Version: 104

Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000001c
Crashed Thread: 2

Thread 2 Crashed:
0 CFNetwork 0x0000b4e4 HTTPMessage::copyHeaderFieldValue(__CFString const*) + 18
1 CFNetwork 0x0000b4c8 CFHTTPMessageCopyHeaderFieldValue + 16
2 CFNetwork 0x0000e022 HTTPProtocol::createStream() + 328
3 CFNetwork 0x0000de8a HTTPProtocol::createAndOpenStream() + 458
4 CFNetwork 0x0000cba2 HTTPProtocol::startLoad() + 278
5 CFNetwork 0x0000c8da URLConnectionLoader::loaderScheduleOriginLoad(_CFURLRequest const*) + 216
6 CFNetwork 0x0000c77c URLConnectionLoader::loaderScheduleLoad(_CFURLRequest const*) + 280
7 CFNetwork 0x0000c5e6 URLConnectionLoader::LoaderConnectionEventQueue::processAllEventsAndConsumePayload(XConnectionEventInfo, long) + 134
8 CFNetwork 0x0000c53a URLConnectionLoader::processEvents() + 60
9 CFNetwork 0x0000a892 URLConnection::multiplexerClientPerform(RunLoopMultiplexer
) + 30
10 CFNetwork 0x0000a812 MultiplexerSource::perform() + 86
11 CFNetwork 0x0000a7b2 MultiplexerSource::_perform(void*) + 2
12 CoreFoundation 0x000573a0 CFRunLoopRunSpecific + 1908
13 CoreFoundation 0x00056c18 CFRunLoopRunInMode + 44
14 Foundation 0x0005a998 +[NSURLConnection(NSURLConnectionReallyInternal) _resourceLoadLoop:] + 172
15 Foundation 0x00053ac6 -[NSThread main] + 42
16 Foundation 0x00001d0e __NSThread__main__ + 852
17 libSystem.B.dylib 0x0002b7b0 _pthread_body + 20

As you can see, copyHeaderFieldValue is causing the crash. What I don't understand is why.

Arlen Anderson
  • 2,486
  • 2
  • 25
  • 36

2 Answers2

0

EXC_BAD_ACCESS is link to a Bad Release somewhere... put retain everywhere just to see if it works.

Arnaud
  • 1
0

Did you end up solving this yet?

The problem seems to be when cachedResponseForRequest returns a new instance of NSCachedURLResponse. Looking at your other question, even trying to mimic NSHTTPURLResponse does not seem to solve the crash issue.

A solution to is not to create your own response but rather return one from an object created in storeCachedResponse. Take a look at how SDURLCache does this by serializing/deserializing that Object.

Community
  • 1
  • 1
Pooya
  • 21
  • 2
  • Yes i solved it... I'll have to look at what i did and report back as I've now forgotten. – Arlen Anderson Apr 07 '11 at 08:31
  • Yeah, if you got a chance to check it out that would be awesome. – Pooya Apr 08 '11 at 00:39
  • In the UIWebView delegate's `shouldStartLoadWithRequest:` the request that's sent is actually mutable. Just cast it to NSMutableURLRequest and you can modify it. Add any post data using `setHTTPBody:` but make sure to set the method to GET as POST fails with the webview for some reason. Then, in your custom URLCache, check for your modified URLRequest. Make a new NSMutableURLRequest, and set all it's properties to match the request with the only exception of setting the method to POST. – Arlen Anderson Apr 08 '11 at 21:44
  • It's been awhile, so i don't remember why so many hoops were needed. I think the webview makes an immutable copy of the request before sending it to the URLCache. That's probably where it fails. Since the request that's sent to the cache is no longer mutable, you need to make a new mutable request. – Arlen Anderson Apr 08 '11 at 21:45