views:

104

answers:

1

Well, NSURLProtocol is pretty generic right? It is for defining all kinds of protocols, isn't it?

So why is there nothing like NSHTTPURLProtocol? Instead, there is an informal category on NSURLRequest with specific HTTP stuff. Why did they do that this way? Why no custom HTTP protocol instead?

What's the idea behind putting all this stuff into NSURLRequest rather than into a custom protocol for HTTP?

Sure HTTP is often used, but anyways: It's confusing for understanding the entire URL Loading System of Cocoa / Cocoa Touch. Now there are two documents. One says, "create a custom NSURLProtocol subclass for your own protocol". The other says: "create informal categories like the ones for the HTTP stuff on NSURLRequest".

Hope someone with a deep understanding can clear that up ;)

A: 

The are several good reasons:

  1. You never have access to the NSURLProtocol-subclass that is powering the connection; only the NSURLConnection object itself. Therefore, if even if such methods were added to a hypothetical NSHTTPURLProtocol class, how would you ever access them?

  2. NSURLConnection uses the request object to determine which protocol will handle it. Therefore, all properties of the request need to be known before the protocol object is created. For example, you could create a custom protocol that overrides the default HTTP implementation, but only for certain hosts.

  3. Providing all settings up-front is a lot neater. What if you could access the protocol object and set properties on it mid-load? How should it handle them – ignore, exception, or try to adjust?

Mike Abdullah