An open API service indexing awesome lists of open source software.

https://github.com/douban/mturlprotocol

Multiple NSURLProtocol subclasses alternative solution.
https://github.com/douban/mturlprotocol

Last synced: 2 months ago
JSON representation

Multiple NSURLProtocol subclasses alternative solution.

Awesome Lists containing this project

README

          

# MTURLProtocol

[![CI Status](https://img.shields.io/travis/duyu1010@gmail.com/MTURLProtocol.svg?style=flat)](https://travis-ci.org/duyu1010@gmail.com/MTURLProtocol)
[![Version](https://img.shields.io/cocoapods/v/MTURLProtocol.svg?style=flat)](https://cocoapods.org/pods/MTURLProtocol)
[![License](https://img.shields.io/cocoapods/l/MTURLProtocol.svg?style=flat)](https://cocoapods.org/pods/MTURLProtocol)
[![Platform](https://img.shields.io/cocoapods/p/MTURLProtocol.svg?style=flat)](https://cocoapods.org/pods/MTURLProtocol)

## 1. Introduction
MTURLProtocol is a subclass of NSURLProtocl and itself is **subclass restricted**. It is used to avoid implementing multiple NSURLProtocol subclass in one application because multiple NSURLProtocol subclass will make it difficult to track and debug network traffic. In addition, mutiple NSURLProtocol will affect network efficiency.

If you have implemented multiple NSURLProtocol subclass and find it inconvenient to maintain those code, it's time to consider migrating to use MTURLProtocol to implement the same function.

MTURLProtocol is protocol oriented. The main idea is providing 3 protocols: `MTRequestHandler`, `MTTaskHandler`, `MTResponseHandler` for decorating request, dataTask and response.

## 2. Structure
ecc48ed2-a92c-11e8-9084-9edb982ab8c6

### 2.1 Handle Request
A request will be handled by your (mutiple) MTRequestHandler protocol instance(s) in order of adding time. Before sending decorated request, you have chance to choose one of the MTTaskHandler protocol instance to decorate dataTask used in MTURLProtocl instance.

#### 2.1.1 Handle Local Request
You can implement MTLocalRequestHandler protocol to return response and response data instantly.

### 2.2 Handle Response
MTURLProtocol instance will choose **only one** MTResponseHandler protocol instance to handle the response regarding to the `original request`: the request before decorated and the `final request`: the request before sent.

Previously, you may have multiple NSURLProtocol instances to handle different response. If more than 2 NSURLProtocol instances was handling one request, you may need merge the logic into one MTResponseHandler protocol instance when migrating.

## 3. Example
See demo in `MTURLProtocl-Example` project.

- `MTTestRequestHandler` + `MTTestResponseHandler`: decorating remote request and response
- `MTTestLocalRequestHandler`: decorating local request and return response instantly
- There is no example for `MTTaskHandler` but it is also easy like implementing handlers above.

## 4. Installation

MTURLProtocol is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:

```ruby
pod 'MTURLProtocol'
```

## 5. Projects and Apps using MTURLProtocol
- [rexxar-iOS](https://github.com/douban/rexxar-ios/tree/dev/Rexxar/Classes/Core)
- [豆瓣App](https://www.douban.com/doubanapp/frodo?channel=top-nav&referer=https%3A%2F%2Fwww.douban.com%2F&wechat=0&os=Mac+OS+X)

## 6. Change Log
- 0.1.2: Minor fix of Podfile source.
- 0.1.1: Add demo.
- 0.1.0: The first version.

## 7. Author

duyu1010@gmail.com

## 8. License

MTURLProtocol is available under the MIT license. See the LICENSE file for more info.