public string Decode()
        {
            int          bytesRead;
            int          totalBytesRead = 0;
            MemoryStream mw             = new MemoryStream();
            var          buffer         = ClientStreamReader.ReadBytes(RequestLength);

            while (totalBytesRead < RequestLength && (bytesRead = buffer.Length) > 0)
            {
                totalBytesRead += bytesRead;
                mw.Write(buffer, 0, bytesRead);
            }

            mw.Close();
            return(Encoding.Default.GetString(mw.ToArray()));
        }
Exemple #2
0
        public string GetRequestHtmlBody()
        {
            if (RequestHtmlBody == null)
            {
                int          bytesRead;
                int          totalBytesRead = 0;
                MemoryStream mw             = new MemoryStream();
                var          buffer         = ClientStreamReader.ReadBytes(RequestLength);
                while (totalBytesRead < RequestLength && (bytesRead = buffer.Length) > 0)
                {
                    totalBytesRead += bytesRead;
                    mw.Write(buffer, 0, bytesRead);
                }

                mw.Close();
                RequestHtmlBody = Encoding.Default.GetString(mw.ToArray());
            }
            RequestWasModified = true;
            return(RequestHtmlBody);
        }
Exemple #3
0
        private void ReadRequestBody()
        {
            if ((ProxyRequest.Method.ToUpper() != "POST" && ProxyRequest.Method.ToUpper() != "PUT"))
            {
                throw new BodyNotFoundException("Request don't have a body." +
                                                "Please verify that this request is a Http POST/PUT and request content length is greater than zero before accessing the body.");
            }

            if (RequestBody == null)
            {
                var    isChunked = false;
                string requestContentEncoding = null;


                if (RequestHeaders.Any(x => x.Name.ToLower() == "content-encoding"))
                {
                    requestContentEncoding = RequestHeaders.First(x => x.Name.ToLower() == "content-encoding").Value;
                }

                if (RequestHeaders.Any(x => x.Name.ToLower() == "transfer-encoding"))
                {
                    var transferEncoding =
                        RequestHeaders.First(x => x.Name.ToLower() == "transfer-encoding").Value.ToLower();
                    if (transferEncoding.Contains("chunked"))
                    {
                        isChunked = true;
                    }
                }


                if (requestContentEncoding == null && !isChunked)
                {
                    RequestBody = ClientStreamReader.ReadBytes(RequestContentLength);
                }
                else
                {
                    using (var requestBodyStream = new MemoryStream())
                    {
                        if (isChunked)
                        {
                            while (true)
                            {
                                var chuchkHead = ClientStreamReader.ReadLine();
                                var chunkSize  = int.Parse(chuchkHead, NumberStyles.HexNumber);

                                if (chunkSize != 0)
                                {
                                    var buffer = ClientStreamReader.ReadBytes(chunkSize);
                                    requestBodyStream.Write(buffer, 0, buffer.Length);
                                    //chunk trail
                                    ClientStreamReader.ReadLine();
                                }
                                else
                                {
                                    ClientStreamReader.ReadLine();
                                    break;
                                }
                            }
                        }
                        try
                        {
                            switch (requestContentEncoding)
                            {
                            case "gzip":
                                RequestBody = CompressionHelper.DecompressGzip(requestBodyStream);
                                break;

                            case "deflate":
                                RequestBody = CompressionHelper.DecompressDeflate(requestBodyStream);
                                break;

                            case "zlib":
                                RequestBody = CompressionHelper.DecompressGzip(requestBodyStream);
                                break;

                            default:
                                RequestBody = requestBodyStream.ToArray();
                                break;
                            }
                        }
                        catch
                        {
                            RequestBody = requestBodyStream.ToArray();
                        }
                    }
                }
            }
            RequestBodyRead = true;
        }