Exemplo n.º 1
0
        private static void Handle(Http.HttpResponse httpResponse, string from)
        {
            string contentTypeStr = httpResponse.Headers.Get(HttpHeaderField.ContentType.Name);

            if (contentTypeStr == null)
            {
                return;
            }

            ContentType contentType = new ContentType(contentTypeStr);

            if (httpResponse.Content.Length == 0)
            {
                return;
            }

            var contentBytes = httpResponse.Content;

            Encoding httpHeaderEncoding = null;

            if (contentType.CharSet != null)
            {
                httpHeaderEncoding = Encoding.GetEncoding(contentType.CharSet);
            }

            Encoding docEncoding = httpHeaderEncoding ?? Encoding.UTF8;
            string   content     = docEncoding.GetString(contentBytes);

            httpResponse.Content = docEncoding.GetBytes(HandleHttps(content, new Uri(from)));
            httpResponse.Headers.Set(HttpHeaderField.ContentLength.Name, httpResponse.Content.Length.ToString(CultureInfo.InvariantCulture));
        }
Exemplo n.º 2
0
        private static void HandleSecureCookies(Http.HttpResponse httpResponse)
        {
            List <string> setCookies = new List <string>();

            foreach (var cookie in httpResponse.Cookies)
            {
                setCookies.Add(cookie.Replace("; Secure", "").Replace(";Secure", "")
                               .Replace("; secure", "").Replace(";secure", ""));
            }
            httpResponse.Cookies = setCookies;
        }
Exemplo n.º 3
0
        public static void Process(Http.HttpResponse httpResponse, string from)
        {
            if (ShouldProcess(httpResponse))
            {
                DecodeContent(httpResponse);
                Handle(httpResponse, from);
            }

            HandleSecureCookies(httpResponse);

            HandleHttpsRedirect(httpResponse);
        }
Exemplo n.º 4
0
        private static bool ShouldProcess(Http.HttpResponse httpResponse)
        {
            string contentTypeStr = httpResponse.Headers.Get(HttpHeaderField.ContentType.Name);

            if (contentTypeStr != null)
            {
                ContentType contentType = new ContentType(contentTypeStr);
                Logger.LogSetValue("content_type", contentType.MediaType);
                if (!(IsJavaType(httpResponse) || IsTextCssType(httpResponse) || IsTextHtmlType(httpResponse)))
                {
                    return(false);
                }
                return(true);
            }
            return(true);
        }
Exemplo n.º 5
0
        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);
            }
        }
Exemplo n.º 6
0
        public static bool IsJavaType(Http.HttpResponse httpResponse)
        {
            string contentTypeStr = httpResponse.Headers.Get(HttpHeaderField.ContentType.Name);

            if (contentTypeStr == null)
            {
                return(false);
            }

            ContentType contentType = new ContentType(contentTypeStr);

            if (contentType.MediaType != "application/javascript" && contentType.MediaType != "application/x-javascript" && contentType.MediaType != "text/javascript" && contentType.MediaType != "text/plain")
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 7
0
        public static bool IsTextCssType(Http.HttpResponse httpResponse)
        {
            string contentTypeStr = httpResponse.Headers.Get(HttpHeaderField.ContentType.Name);

            if (contentTypeStr == null)
            {
                return(false);
            }

            ContentType contentType = new ContentType(contentTypeStr);

            if (contentType.MediaType != "text/css")
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 8
0
        private static void DecodeContent(Http.HttpResponse httpResponse)
        {
            if (httpResponse.Content == null)
            {
                return;
            }

            var contentEncodingStr = httpResponse.Headers.Get(HttpHeaderField.ContentEncoding.Name);

            if (contentEncodingStr == null)
            {
                return;
            }

            byte[] decodedContent;
            if (contentEncodingStr == ContentCoding.GZip.Name)
            {
                using (MemoryStream memoryStream = new MemoryStream())
                    using (GZipStream gZipStream = new GZipStream(new MemoryStream(httpResponse.Content),
                                                                  CompressionMode.Decompress))
                    {
                        byte[] bufferBytes = new byte[1024];
                        int    readCount   = gZipStream.Read(bufferBytes, 0, bufferBytes.Length);
                        while (readCount > 0)
                        {
                            memoryStream.Write(bufferBytes, 0, readCount);
                            readCount = gZipStream.Read(bufferBytes, 0, bufferBytes.Length);
                        }
                        decodedContent = memoryStream.ToArray();
                    }
            }
            else
            {
                throw new NotImplementedException("Not implemented content-coding.");
            }

            httpResponse.Content = decodedContent;
            httpResponse.Headers.Remove(HttpHeaderField.ContentEncoding.Name);
            httpResponse.Headers.Set(HttpHeaderField.ContentLength.Name, httpResponse.Content.Length.ToString(CultureInfo.InvariantCulture));
        }