/// <summary> /// Sends request to remote server as specified in options /// </summary> /// <param name="app"></param> /// <param name="options">Options for setting port, host, and scheme</param> public static void RunProxy(this IApplicationBuilder app, ProxyOptions options) { if (app == null) { throw new ArgumentNullException(nameof(app)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } app.UseMiddleware <ProxyMiddleware>(Options.Create(options)); }
public ProxyMiddleware(RequestDelegate next, IOptions <ProxyOptions> options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } _next = next ?? throw new ArgumentNullException(nameof(next)); _options = options.Value; if (string.IsNullOrEmpty(_options.Host)) { throw new ArgumentException("Options parameter must specify host.", nameof(options)); } // Setting default Port and Scheme if not specified if (string.IsNullOrEmpty(_options.Port)) { if (string.Equals(_options.Scheme, "https", StringComparison.OrdinalIgnoreCase)) { _options.Port = "443"; } else { _options.Port = "80"; } } if (string.IsNullOrEmpty(_options.Scheme)) { _options.Scheme = "http"; } _hostWithPort = _options.Host + ":" + _options.Port; _httpClient = new HttpClient(new HttpClientHandler()); }