views:

953

answers:

2

I am working with a wrapper class for CFHTTPMessage, which contains a CFHTTPMessageRef object to which is added the method (GET), the URL of the web application server, and a few custom headers containing the date and an authentication nonce.

I'm having some problems getting the method and URL to return certain data. I think I've worked out the authentication nonce.

I'd like to troubleshoot this by looking at the raw request going to the web application, and making sure everything is formatted properly.

My question is: If I have a CFHTTPMessageRef object (e.g. messageRef), is there a way to log the raw HTTP request that comes out of this message?

I've tried the following but I get a EXC_BAD_ACCESS signal when I try to access its bytes:

CFDataRef messageData = CFHTTPMessageCopyBody(messageRef);

Thanks for any advice.

As an alternative, is it possible to use a packet sniffer on a switched network? I can run ettercap on a laptop device, but don't know how to sniff what my iPhone is doing on the local wireless network.

A: 

The only reason why you should be getting an EXC_BAD_ACCESS when accessing the bytes is if the messageData is NULL (no HTTP body) and you're dereferencing it.

Point to remember: the HTTP body isn't the "raw request". It doesn't include the headers or the actual HTTP instruction (GET/POST/ETC). If you haven't actually set body content, it will be nil.

It is possible (but less likely) that your CFHTTPMessageRef value isn't properly initialized. Check this in the debugger by setting a breakpoint on your CFHTTPMessageCopyBody line, going to the Debugger Console window, setting the text input cursor to the last line in this window and typing "po messageRef". It should give you a CFTypeID message if validly initialized.

Matt Gallagher
+1  A: 

The following worked well:

NSData *d = (NSData *)CFHTTPMessageCopySerializedMessage(messageRef);
NSLog([[[NSString alloc] initWithBytes:[d bytes] length:[d length] encoding:NSUTF8StringEncoding] autorelease]);

Hope this is helpful to others.

Alex Reynolds
Thanks for the code snippet, it works for what I needed too.
Hector Ramos