コード例 #1
0
ファイル: DNurl.cs プロジェクト: jamesrep/dnurl
        /// <summary>
        /// Read message sent in stream. If an output file is specified; received message is stored in that file.
        /// </summary>
        /// <param name="streamFromServer"></param>
        /// <returns>The string that has been read</returns>
        string readMessage(Stream streamFromServer)
        {
            StringBuilder messageData = new StringBuilder();
            BinaryLineStream sr = new BinaryLineStream(streamFromServer);

            // First read the header. If the encoding is gzip we need to decompress the result
            string strHeaderLine = null;

            try
            {
                strHeaderLine = sr.ReadLine();
            }
            catch (System.IO.IOException ex)
            {
                if(this.bDebug)
                {
                    Console.WriteLine(ex.Message);
                }

                return string.Empty;
            }

            byte[] btsLF = ASCIIEncoding.ASCII.GetBytes(strLinefeed); // Create proper linefeed
            int contentLength = -1;

            // Get Headers.
            while (strHeaderLine != null && strHeaderLine != string.Empty)
            {
                byte[] bts = ASCIIEncoding.ASCII.GetBytes(strHeaderLine); // We assume all headers are in ascii

                // Output headers and linefeed to the listening output streams.
                echoWrite(bts, this.outputStreams, false);
                echoWrite(btsLF, this.outputStreams, false);

                // If we should output (to stdout) the headers then we add them here.
                if (!bSkipHeaders)
                {
                    messageData.Append(strHeaderLine);
                    messageData.Append(strLinefeed);
                }

                // Check if we need to decompress the server result
                if (this.bDecodeGzip && isGzipLine(strHeaderLine))
                {
                    bIsGzip = true;
                }

                // Check for utf-8 (important for example swedish pages).
                if (isContainedHeaderStatement(strHeaderLine, "content-type:", "charset=utf-8"))
                {
                    this.decoder = System.Text.UTF8Encoding.UTF8.GetDecoder();
                }

                // Check for chunked format
                if (!bIsChunked && bDecodeChunked)
                {
                    bIsChunked = isChunkedLine(strHeaderLine);
                }

                // Parse content length
                if (this.bUseContentLength && contentLength == -1)
                {
                    contentLength = getContentLength(strHeaderLine);
                }

                try
                {
                    strHeaderLine = sr.ReadLine();
                }
                catch (System.IO.IOException)
                {
                    break;
                }
            }

            // Output linefeed
            if (strHeaderLine == string.Empty)
            {
                this.echoWrite(btsLF, outputStreams, false);    // For listening streams
                messageData.Append(strLinefeed);                // For user interface output
            }

            // Check content length
            if (contentLength != 0)
            {
                Stream streamToUse = streamFromServer;

                // Determine which streams to use upon the current format (none, gzip+chunked, gzip, chunked)
                if (bIsChunked)
                {
                    if (bIsGzip)
                    {
                        streamToUse = new GZipStream(new ChunkedStream(streamFromServer), CompressionMode.Decompress);
                    }
                    else
                    {
                        streamToUse = new ChunkedStream(streamFromServer);
                    }
                }
                else if(bIsGzip)
                {
                    streamToUse = new GZipStream(streamFromServer, CompressionMode.Decompress);
                }

                // Now we are finished with headers so we read the rest of the response
                fillMessageData(streamToUse, messageData, streamFromServer);
            }

            return messageData.ToString();
        }
コード例 #2
0
ファイル: ChunkedStream.cs プロジェクト: jamesrep/dnurl
        uint getChunkSize(Stream inputStream)
        {
            if(binStream==null) binStream = new BinaryLineStream(inputStream);

            string hexString = binStream.ReadLine();

            uint num = 0;

            try
            {
                num = (uint)Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
            }
            catch
            {
                // TODO: Warning for bad chunking format?
                return 0;
            }

            int btRead = inputStream.ReadByte();

            while (btRead != -1 && btRead != ((int)'\n'))
            {
                btRead = inputStream.ReadByte();
            }

            return num;
        }