private void HandleRequest(SplunkApiConfiguration configuration, HttpContext context, string rawUrl) { string redirect = null; using (var handler = CreateHandler()) { using (var proxy = new ProxyClient(handler, context, configuration)) { try { ProxyUtilities.MapHeaders(context.Request, proxy, _excludedHeaders); ProxyUtilities.MapCookies(context.Request, handler, configuration); Log.Debug($"Partner KPI: new request for: '{rawUrl}'."); var response = proxy.HandleRequest(rawUrl, context.Request.HttpMethod); Log.Debug($"Partner KPI: response received: {(int)response.StatusCode} {response.StatusCode}."); var cookies = ProxyUtilities.GetAllCookies(handler.CookieContainer); if (((int)response.StatusCode).ToString().StartsWith("3")) { // HTTP Status: 300 - 399 redirect = response.Headers.Location?.PathAndQuery; } ProxyUtilities.MapHeaders(response, context.Response, _excludedHeaders); ProxyUtilities.MapCookies(cookies, context.Response); var contentType = response.Content.Headers?.ContentType?.MediaType ?? context.Request.ContentType; var content = _dashboardRegex.IsMatch(rawUrl) || string.Equals(contentType, "text/html", StringComparison.OrdinalIgnoreCase) ? Encoding.UTF8.GetBytes(_headerRegex.Replace(response.Content.ReadAsStringAsync().Result, string.Empty)) : response.Content.ReadAsByteArrayAsync().Result; context.Response.BinaryWrite(content); context.Response.ContentType = string.IsNullOrWhiteSpace(contentType) ? "text/html" : contentType; context.Response.StatusCode = (int)response.StatusCode; if (_loginUrlRegex.IsMatch(redirect ?? string.Empty)) { Log.Debug("Partner KPI: auto logging in user with configured credentials."); var query = HttpUtility.ParseQueryString(response.Headers.Location?.Query ?? string.Empty); var returnTo = query.Get("return_to"); var loginUrl = $"{configuration.LoginPath}?username={configuration.UserName}&password={configuration.Password}{(string.IsNullOrWhiteSpace(returnTo) ? string.Empty : $"&return_to={returnTo}")}"; context.Response.RedirectLocation = loginUrl; } else { if (!string.IsNullOrWhiteSpace(redirect)) { Log.Debug($"Partner KPI: sending redirect response to: '{redirect}'."); } context.Response.RedirectLocation = redirect; } }
private void HandleRequest(object sender, EventArgs e) { string redirect = null; var configuration = new SplunkApiConfiguration(); var application = (HttpApplication)sender; var context = application.Context; var rawUrl = _userNameUrlRegex.Replace(_applicationRegex2.Replace(_applicationRegex.Replace(context.Request.RawUrl, $"/{configuration.UserName}/launcher/"), "/launcher/"), $"/{configuration.UserName}/"); var repository = new ScaleOutRepositoryFactory("SplunkSoContext2") .AddToContext <SplunkUhdWifiInformation>() .CreateRepository <SplunkUhdWifiInformation>(); var t = string.Empty; if (_splunkReportRequestRegex.IsMatch(rawUrl)) { t = context.Request.QueryString["reportInfoId"]; } else { t = context.Request.Cookies[ReportInfoCookieName]?.Value; } var reportInfoId = _splunkReportRequestRegex.IsMatch(rawUrl) ? context.Request.QueryString["reportInfoId"] : context.Request.Cookies[ReportInfoCookieName]?.Value; var reportInfo = repository.GetByKey(reportInfoId); var p1 = !_splunkFileRequest.IsMatch(rawUrl) && reportInfo == null; var p2 = (_splunkReportRequestRegex.IsMatch(rawUrl) && !Regex.IsMatch(rawUrl, $".*{Regex.Escape(reportInfo.Key)}.*", RegexOptions.IgnoreCase)); //if (!_splunkFileRequest.IsMatch(rawUrl) && reportInfo == null || (_splunkReportRequestRegex.IsMatch(rawUrl) && !Regex.IsMatch(rawUrl, $".*{Regex.Escape(reportInfo.Key)}.*", RegexOptions.IgnoreCase))) //{ // return; //} if (!_splunkFileRequest.IsMatch(rawUrl) && reportInfo == null || (_splunkDeviceRequestRegex.IsMatch(rawUrl) && !Regex.IsMatch(rawUrl, $".*{Regex.Escape(reportInfo.DeviceAddr)}.*", RegexOptions.IgnoreCase))) { return; } using (var handler = CreateHandler()) { using (var proxy = new ProxyClient(handler, context, configuration)) { try { ProxyUtilities.MapHeaders(context.Request, proxy, _excludedHeaders); ProxyUtilities.MapCookies(context.Request, handler, configuration); Log.Debug($"Partner KPI: new request for: '{rawUrl}'."); var response = proxy.HandleRequest(rawUrl, context.Request.HttpMethod); Log.Debug($"Partner KPI: response received: {(int)response.StatusCode} {response.StatusCode}."); var cookies = ProxyUtilities.GetAllCookies(handler.CookieContainer); if (((int)response.StatusCode).ToString().StartsWith("3")) { // HTTP Status: 300 - 399 redirect = response.Headers.Location?.PathAndQuery; } ProxyUtilities.MapHeaders(response, context.Response, _excludedHeaders); ProxyUtilities.MapCookies(cookies, context.Response); var contentType = response.Content.Headers?.ContentType?.MediaType ?? context.Request.ContentType; var content = _dashboardRegex.IsMatch(rawUrl) || string.Equals(contentType, "text/html", StringComparison.OrdinalIgnoreCase) ? Encoding.UTF8.GetBytes(_headerRegex.Replace(response.Content.ReadAsStringAsync().Result, string.Empty)) : response.Content.ReadAsByteArrayAsync().Result; context.Response.BinaryWrite(content); context.Response.ContentType = string.IsNullOrWhiteSpace(contentType) ? "text/html" : contentType; context.Response.StatusCode = (int)response.StatusCode; if (_loginUrlRegex.IsMatch(redirect ?? string.Empty)) { Log.Debug("Partner KPI: auto logging in user with configured credentials."); var query = HttpUtility.ParseQueryString(response.Headers.Location?.Query ?? string.Empty); var returnTo = query.Get("return_to"); var loginUrl = $"{configuration.LoginPath}?username={configuration.UserName}&password={configuration.Password}{(string.IsNullOrWhiteSpace(returnTo) ? string.Empty : $"&return_to={returnTo}")}"; context.Response.RedirectLocation = loginUrl; } else { if (!string.IsNullOrWhiteSpace(redirect)) { Log.Debug($"Partner KPI: sending redirect response to: '{redirect}'."); } context.Response.RedirectLocation = redirect; } }