views:

148

answers:

1

I want to write a MobileSubstrate (http://www.iphonedevwiki.net/index.php/MobileSubstrate) addon that takes every HTTP request and modifies the URL. (The reason is very complicated and would take a while to explain, so just trust me that there's not an alternative). Basically, I need to know what's the best Objective-C message or C function that I should overwrite to intercept any HTTP request, modify the URL, and send the request back to the original function (this shouldn't be just the first request sent by an application, but any redirects too). I think it may be in NSURL or CFNetwork, but am not sure where exactly. This function should, when replaced, work with any apps and the OS itself.

EDIT: Ok, here's two sample scenarios of what I want to do, note that the method to override should be one that is called by any HTTP request anywhere from an app to the OS:
1) User in MobileSafari types in aaa.com/a.html
2) aaa.com/a.html redirects to aaa.com/b.html
3) aaa.com/b.html loads bbb.com/c.png
I want to change the request so everything has ?i=j added.
EX: Change all aaa.com/a.html to aaa.com/a.html?i=j, aaa.com/b.html?i=j, bbb.com/c.png?i=j?

And another one:
1) User uses Maps.app
2) Google Maps tries to load google.com/map1.png (not actual image, I know)
Change google.com/map1.png to google.com/map1.png?i=j?

+1  A: 

Hook the following methods and swap out the request with a mutableCopy that you've edited.

  • -[NSURLConnection sendWillSendRequest:redirectResponse:]
  • -[NSURLConnection initWithRequest:delegate:]
  • –[NSURLConnection initWithRequest:delegate:startImmediately:]

This will not redirect raw TCP socket connections, only the HTTP requests that are funnelled through the standard API.

Note: be very wary of doing this. Redirecting all http requests is a violation of user trust in my opinion

rpetrich
Ok, here's a sample scenario:1) User in MobileSafari types in aaa.com/a.html2) aaa.com/a.html redirects to aaa.com/b.html3) aaa.com/b.html loads bbb.com/c.pngWhen I replace "-[NSURLConnection sendWillSendRequest:redirectResponse:]" with my code to add ?i=j to all html requests, does it change all aaa.com/a.html to aaa.com/a.html?i=j, aaa.com/b.html?i=j, bbb.com/c.png?i=j?
Yifan
And another one:1) User uses Maps.app2) Google Maps tries to load google.com/map1.png (not actual image, I know)Does replacing that method change google.com/map1.png to google.com/map1.png?i=j?Also, this mobile substrate plugin I'm writing is for internal use only.
Yifan
Also, thanks for the response.
Yifan
You can change the URL to any location you want. How that works is up to you. Also, you may need additional hooks that I have missed, but this should get you started :)
rpetrich
But if, say, the user is using ASIHTTPRequest, which is based off of CFNetwork (as is NSURLConnection and pals) then this will not work. You can go one level lower and hook CFNetwork to do the same thing if you want a more 'complete' hook.
ckrames1234
ckrames1234: correct. or you could go lower than that and hook all sockets :)
rpetrich