Why is there no NSHTTPURLProtocol class?
-
22-09-2019 - |
Question
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/CocoaTouch
. 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 ;)
Solution
The are several good reasons:
You never have access to the
NSURLProtocol
-subclass that is powering the connection; only theNSURLConnection
object itself. Therefore, even if such methods were added to a hypotheticalNSHTTPURLProtocol
class, how would you ever access them?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.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?