handler method
Request handler.
Invoke this method to handle the request.
Implementation
Future<Response> handler(Request req) async {
final method = req.method;
_logProxy.fine('[${req.id}] $method ${req.requestPath()}');
// Determine the target URI for the proxy request
final targetUrl = _targetUri(req);
_logProxyRequest.finer('[${req.id}] $method $targetUrl');
try {
// Determine headers and body for the proxy request
final r = await _proxyRequestHeaders(req, targetUrl);
// Perform the proxy request
http.Response targetResponse;
switch (method) {
case 'GET':
targetResponse = await http.get(targetUrl, headers: r.headers);
break;
case 'HEAD':
targetResponse = await http.head(targetUrl, headers: r.headers);
break;
case 'POST':
targetResponse =
await http.post(targetUrl, headers: r.headers, body: r.body);
break;
case 'PUT':
targetResponse =
await http.put(targetUrl, headers: r.headers, body: r.body);
break;
case 'PATCH':
targetResponse =
await http.patch(targetUrl, headers: r.headers, body: r.body);
break;
case 'DELETE':
targetResponse = await http.delete(targetUrl, headers: r.headers);
break;
default:
throw UnimplementedError('proxy unsupported method: $method');
}
// Produce the response from the response of the proxy request
return await _produceResponse(req, targetResponse);
} catch (e) {
_logProxy.warning('[${req.id}] proxy: $e');
final errorResponse = http.Response(
'An error has occurred.\n', HttpStatus.internalServerError,
headers: {
'content-type': 'text/text',
'date': HttpDate.format(DateTime.now()),
'server': _receivedBy
});
return await _produceResponse(req, errorResponse);
// final proxyException = ProxyHandlerException(targetUrl, e);
// _logProxy.fine('[${req.id}] $proxyException');
// throw proxyException;
}
}