Proxy constructor

Proxy(
  1. String pattern,
  2. String proxy, {
  3. String receivedBy = _receivedByDefault,
  4. bool includeViaHeaderInResponse = true,
})

Create a request handler that proxies requests to another URL.

This request handler is used to handle requests that match the pattern. The pattern MUST end with a wildcard segment "" (e.g. "~/" or "~/foobar/"). The path that matches the "" is the sub-path.

When handling a request, it will make a proxy request to a URL that is made up of the proxy with the sub-path appended. The response from that proxy request is forwarded back as the response.

A "Via" header is always added to the proxy request, but is optional for the proxy response. The value of receivedBy is used to generate the value of the proxy's Via header. A default value is used if receivedBy is a blank or empty string.

The value of includeViaHeaderInResponse controls whether the proxy's Via header is added to the proxy response or not. It defaults to true.

Null-safety breaking change: this deprecated named parameters requestBlockHeaders and responseBlockHeaders have been removed. If you were using them, please submit an issue in GitHub.

Implementation

Proxy(String pattern, String proxy,
    {String receivedBy = _receivedByDefault,
    this.includeViaHeaderInResponse = true})
    : _receivedBy = (receivedBy.trim().isNotEmpty)
          ? receivedBy.trim()
          : _receivedByDefault,
      _pathPrefix = _removeSlashes(pattern.substring(2, pattern.length - 1)),
      _targetUriPrefix = _removeSlashes(proxy) {
  // Set _pathPrefix (leading and trailing slashes are removed)
  // e.g. "~/" -> empty string
  // "~/foo/bar/*" -> "foo/bar"
  // "~/strange//*" -> "strange"
  // "~////very-strange////*" -> "very-strange"

  if (!pattern.startsWith('~/')) {
    throw ArgumentError.value(pattern, 'pattern', 'does not start with "~/"');
  }
  if (!pattern.endsWith('/*')) {
    throw ArgumentError.value(pattern, 'pattern', 'does not end with "/*"');
  }

  // _targetUriPrefix is set to a value with any trailing slashes removed
  // e.g. "http://remote.example.com/" -> "http://remove.example.com"

  /*
  Previous versions allowed headers to block to be specified.
  Probably no longer needed.

  // Store lower case versions of headers to block.

  if (requestBlockHeaders != null) {
    for (final name in requestBlockHeaders) {
      this.requestBlockHeaders.add(name.toLowerCase());
    }
  }

  if (responseBlockHeaders != null) {
    for (final name in responseBlockHeaders) {
      this.responseBlockHeaders.add(name.toLowerCase());
    }
  }
   */
}