private static void HandleReferer(HttpRequest httpRequest) { if (httpRequest.Headers[HttpHeaderField.Referer.Name] == null) { return; } if (HttpsMapper.IsHttps(httpRequest.Headers[HttpHeaderField.Referer.Name])) { httpRequest.Headers[HttpHeaderField.Referer.Name] = "https" + httpRequest.Headers[HttpHeaderField.Referer.Name].Substring("http".Length); } }
private static void HandleHttpsRedirect(Http.HttpResponse httpResponse) { if (httpResponse.StatusCode != ((int)HttpStatusCode.Found).ToString(CultureInfo.InvariantCulture) && httpResponse.StatusCode != ((int)HttpStatusCode.MovedPermanently).ToString(CultureInfo.InvariantCulture)) { return; } var uriStr = httpResponse.Headers.Get(HttpHeaderField.Location.Name); if (uriStr.StartsWith("https:")) //httpResponse.Headers[HttpHeaderField.Location.Name] = HttpsMapper.ConvertHttpsToXHttpUri(uriStr); { httpResponse.Headers[HttpHeaderField.Location.Name] = "Http" + uriStr.Substring("https".Length); HttpsMapper.AddHttps(uriStr); } }
private static string HandleHttps(string content, Uri fromUri) { { StringBuilder contentBuilder = new StringBuilder(); const string pattern = @"https://[-.a-z0-9]+(/[-.a-z0-9_]*)*"; Regex r = new Regex(pattern, RegexOptions.IgnoreCase); Match m = r.Match(content); int offset = 0; while (m.Success) { Group group = m.Groups[0]; contentBuilder.Append(content.Substring(offset, group.Index - offset)); var doneUri = "http" + group.Value.Substring("https".Length); HttpsMapper.AddHttps(doneUri); contentBuilder.Append(doneUri); offset = group.Index + group.Length; m = m.NextMatch(); } contentBuilder.Append(content.Substring(offset)); content = contentBuilder.ToString(); } { StringBuilder contentBuilder = new StringBuilder(); const string pattern = @"https:\\/\\/[-.a-z0-9]+(\\/[-.a-z0-9_]*)*"; Regex r = new Regex(pattern, RegexOptions.IgnoreCase); Match m = r.Match(content); int offset = 0; while (m.Success) { Group group = m.Groups[0]; contentBuilder.Append(content.Substring(offset, group.Index - offset)); var doneUri = "http" + group.Value.Substring("https".Length); HttpsMapper.AddHttps(doneUri.Replace("\\/", "/")); contentBuilder.Append(doneUri); offset = group.Index + group.Length; m = m.NextMatch(); } contentBuilder.Append(content.Substring(offset)); content = contentBuilder.ToString(); } if (fromUri.Scheme == "https") { { const string pattern = "\"(/[-.a-z0-9]+(/[-.a-z0-9_]*)*)\""; Regex r = new Regex(pattern, RegexOptions.IgnoreCase); Match m = r.Match(content); while (m.Success) { Group group = m.Groups[1]; var doneUri = new Uri(fromUri, group.Value).ToString(); HttpsMapper.AddHttps(doneUri); m = m.NextMatch(); } } { const string pattern = "\'(/[-.a-z0-9]+(/[-.a-z0-9_]*)*)\'"; Regex r = new Regex(pattern, RegexOptions.IgnoreCase); Match m = r.Match(content); while (m.Success) { Group group = m.Groups[1]; var doneUri = new Uri(fromUri, group.Value).ToString(); HttpsMapper.AddHttps(doneUri); m = m.NextMatch(); } } { const string pattern = "(src|action|href|formaction)=\"(.+?)\""; Regex r = new Regex(pattern, RegexOptions.IgnoreCase); Match m = r.Match(content); while (m.Success) { Group group = m.Groups[2]; try { if (!group.Value.StartsWith("http:")) { var doneUri = new Uri(fromUri, group.Value).ToString(); HttpsMapper.AddHttps(doneUri); } } catch { } m = m.NextMatch(); } } } return(content); }