CustomHttpHeadersURLProtocol 2.1.0

CustomHttpHeadersURLProtocol 2.1.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Nov 2016
SPMSupports SPM

Maintained by Takahiro Ooishi.



CustomHttpHeadersURLProtocol

Sample

  • Swift 3.0
fileprivate func setupCustomHttpHeadersURLProtocol() {
  let setupCustomHeaders: CustomHttpHeadersConfig.SetupCustomHeaders = { (request: NSMutableURLRequest) in
    request.addValue("CustomHttpHeadersURLProtocolSample", forHTTPHeaderField: "X-App-Name")
    request.addValue("\(Date().timeIntervalSince1970)", forHTTPHeaderField: "X-Timestamp")
  }

  let canHandleRequest: CustomHttpHeadersConfig.CanHandleRequest = { (request: URLRequest) -> Bool in
    guard let scheme = request.url?.scheme else { return false }
    guard let host = request.url?.host else { return false }

    if !["http", "https"].contains(scheme) { return false }
    if host == "0.0.0.0" { return true }

    return false
  }

  let config = CustomHttpHeadersConfig(setupCustomHeaders: setupCustomHeaders, canHandleRequest: canHandleRequest)
  CustomHttpHeadersURLProtocol.start(config)
}
  • Swift 2.3
private func setupCustomHttpHeadersURLProtocol() {
  let setupCustomHeaders: CustomHttpHeadersConfig.SetupCustomHeaders = { (request: NSMutableURLRequest) in
    request.addValue("CustomHttpHeadersURLProtocolSample", forHTTPHeaderField: "X-App-Name")
    request.addValue("\(NSDate().timeIntervalSince1970)", forHTTPHeaderField: "X-Timestamp")
  }

  let canHandleRequest: CustomHttpHeadersConfig.CanHandleRequest = { (request: NSURLRequest) -> Bool in
    guard let scheme = request.URL?.scheme else { return false }
    guard let host = request.URL?.host else { return false }

    if !["http", "https"].contains(scheme) { return false }
    if host == "0.0.0.0" { return true }

    return false
  }

  let config = CustomHttpHeadersConfig(setupCustomHeaders: setupCustomHeaders, canHandleRequest: canHandleRequest)
  CustomHttpHeadersURLProtocol.start(config)
}

Author

Takahiro Ooishi, [email protected]

License

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