Example #1
0
        private static void AddResponse(Table table, Response response)
        {
            if (response == null)
            {
                return;
            }
            Table subtable = CreateSubtable("response", table);

            AddTextRow("status", response.status, subtable);

            AddTextRow("statusText", response.statusText, subtable);
            AddTextRow("redirectURL", response.redirectURL, subtable);
            //AddTextRow("httpVersion", response.httpVersion, subtable);

            //AddTextRow("headersSize", response.headersSize, subtable);
            AddTextRow("comment", response.comment, subtable);
            //AddTextRow("bodySize", response.bodySize, subtable);


            AddCookies(subtable, response.cookies);
            AddNameValueTable(subtable, response.headers, "headers");

            Content content = response.content;

            Table cntable = CreateSubtable("content", subtable);

            AddTextRow("mimeType", content.mimeType, cntable);
            AddTextRow("size", content.size, cntable);
            AddTextRow("encoding", content.encoding, cntable);
            AddTextRow("compression", content.compression, cntable);
            AddTextRow("text", content.text, cntable);
            //AddTextRow("comment", content.comment, cntable);
        }
        private static Content GetBodyInfo(Session oS)
        {
            var content = new Content();

            int num;
            int num2;

            GetDecompressedSize(oS, out num, out num2);
            content.size = num;
            content.compression = num2;
            content.mimeType = oS.oResponse["Content-Type"];

            string mImeType = oS.oResponse.MIMEType;
            bool isMimeTypeTextEquivalent = Utility.IsMimeTypeTextEquivalent(mImeType);
            if (((isMimeTypeTextEquivalent && ("text/plain" == mImeType)) && (oS.responseBodyBytes.Length > 3)) &&
                ((((oS.responseBodyBytes[0] == 0x43) && (oS.responseBodyBytes[1] == 0x57)) &&
                  (oS.responseBodyBytes[2] == 0x53)) ||
                 (((oS.responseBodyBytes[0] == 70) && (oS.responseBodyBytes[1] == 0x4c)) &&
                  (oS.responseBodyBytes[2] == 0x56))))
            {
                isMimeTypeTextEquivalent = false;
            }
            if (isMimeTypeTextEquivalent)
            {
                content.text = oS.GetResponseBodyAsString();
                return content;
            }
            if (oS.responseBodyBytes.Length < MaxBinaryBodyLength)
            {
                content.encoding = "base64";
                content.text = Convert.ToBase64String(oS.responseBodyBytes);
                return content;
            }

            content.comment =
                "Body length exceeded Mocument.Transcoders.HttpArchiveTranscoder.MaxBinaryBodyLength , so body was omitted.";
            return content;
        }
        private static byte[] GetBodyArrayFromContent(Content content, string headers)
        {
            var writeData = new byte[0];
            if (content != null)
            {
                if (content.text == null)
                {
                    return writeData;
                }

                if (content.encoding != null && ("base64" == content.encoding))
                {
                    // ReSharper disable AssignNullToNotNullAttribute
                    return Convert.FromBase64String(content.text);
                    // ReSharper restore AssignNullToNotNullAttribute
                }

                Encoding encoding = Encoding.UTF8;
                if (content.mimeType != null && (content.mimeType.IndexOf("charset", StringComparison.Ordinal) > -1))
                {
                    Match match = new Regex("charset\\s?=\\s?[\"]?(?<TokenValue>[^\";]*)").Match(content.mimeType);
                    if (match.Success && (match.Groups["TokenValue"] != null))
                    {
                        try
                        {
                            encoding = Encoding.GetEncoding(match.Groups["TokenValue"].Value);
                        }
                        // ReSharper disable RedundantCatchClause
                        catch
                        {
                            throw;
                        }
                        // ReSharper restore RedundantCatchClause
                    }
                }
                // ReSharper disable AssignNullToNotNullAttribute
                writeData = encoding.GetBytes(content.text);
                // ReSharper restore AssignNullToNotNullAttribute
                if (headers.Contains("Content-Encoding") && headers.Contains("gzip"))
                {
                    writeData = Utilities.GzipCompress(writeData);
                }
                if (headers.Contains("Content-Encoding") && headers.Contains("deflate"))
                {
                    writeData = Utilities.DeflaterCompress(writeData);
                }
                if (headers.Contains("Transfer-Encoding") && headers.Contains("chunked"))
                {
                    writeData = Utilities.doChunk(writeData, 2);
                }
            }
            return writeData;
        }