public HttpClientResponse(string status, HttpHeaders headers, BufferContext bufferContext)
        {
            HttpUtility.ParseStatus(status, out protocol, out statusCode, out statusDescription);
            Headers = headers;
            if (Headers.ContainsKey("cookie"))
            {
                Cookies = HttpUtility.ParseCookie(Headers["cookie"]);
            }
            else
            {
                Cookies = new Dictionary <string, string>();
            }

            if (Headers.ContainsKey("content-length"))
            {
                long contentLength = Convert.ToInt64(Headers["content-length"]);
                InputStream = new LengthLimitedStream(bufferContext, contentLength);
            }
            else if (Headers.ContainsKey("transfer-encoding") && (Headers["transfer-encoding"].ToLower() == "chunked"))
            {
                InputStream = new InputChunkedStream(bufferContext);
            }
            else
            {
                InputStream = new LengthLimitedStream(bufferContext, 0);
            }
        }
Exemple #2
0
 public InputChunkedStream(BufferContext bufferContext)
 {
     this.bufferContext = bufferContext;
     position           = 0;
     end         = false;
     chunkLength = 0;
     chunkPos    = 0;
 }
Exemple #3
0
 HttpClient()
 {
     bufferContext             = new BufferContext();
     bufferContext.Offset      = 0;
     bufferContext.Count       = 0;
     bufferContext.ReadCounter = 0;
     bufferContext.Buffer      = new byte[4096];
 }
 public LengthLimitedStream(Stream stream, long length)
 {
     bufferContext        = new BufferContext();
     bufferContext.Buffer = new byte[4096];
     bufferContext.Stream = stream;
     bufferContext.Count  = 0;
     bufferContext.Offset = 0;
     this.length          = length;
     position             = 0;
 }
        public static async Task <bool> ReadHeadersAsync(BufferContext bufferContext, MemoryStream stringBuffer, Dictionary <string, string> headers)
        {
            bool   cr   = false;
            string line = null;

            while (true)
            {
                // if needed, fill the buffer
                if (bufferContext.Count <= 0)
                {
                    await bufferContext.Fill();
                }
                if (bufferContext.Count <= 0)
                {
                    return(false);
                }
                // read a line
                if (ReadLine(bufferContext, stringBuffer, ref cr))
                {
                    string line2 = Encoding.UTF8.GetString(stringBuffer.GetBuffer(), 0, (int)stringBuffer.Length);
                    stringBuffer.SetLength(0);
                    // multi lines header
                    if ((line != null) && (line2.StartsWith(" ") || line2.StartsWith("\t")))
                    {
                        line += line2;
                    }
                    // new header
                    else
                    {
                        // flush previous line
                        if (line != null)
                        {
                            int pos = line.IndexOf(':');
                            if ((pos == -1) || (pos == 0))
                            {
                                throw new Exception("Invalid mime header line. Missing ':'");
                            }
                            string key     = line.Substring(0, pos).ToLower();
                            string content = line.Substring(pos + 2, line.Length - (pos + 2));
                            headers[key] = HttpUtility.QuotedPrintableDecode(content);
                        }
                        line = line2;
                        if (line == String.Empty)
                        {
                            return(true);
                        }
                    }
                }
            }
        }
        public static async Task <string> ReadLineAsync(BufferContext bufferContext, MemoryStream stringBuffer)
        {
            bool cr = false;

            while (!ReadLine(bufferContext, stringBuffer, ref cr))
            {
                await bufferContext.Fill();

                if (bufferContext.Count == 0)
                {
                    return(null);
                }
            }
            return(Encoding.UTF8.GetString(stringBuffer.GetBuffer(), 0, (int)stringBuffer.Length));
        }
 public static bool ReadLine(BufferContext bufferContext, MemoryStream stringBuffer, ref bool cr)
 {
     while (bufferContext.Count > 0)
     {
         bufferContext.Count--;
         int offset = bufferContext.Offset++;
         if (cr)
         {
             // LF
             if (bufferContext.Buffer[offset] == 10)
             {
                 cr = false;
                 return(true);
             }
             else
             {
                 throw new Exception("Invalid line format");
             }
         }
         else
         {
             // CR
             if (bufferContext.Buffer[offset] == 13)
             {
                 cr = true;
             }
             else if (bufferContext.Buffer[offset] == 10)
             {
                 throw new Exception("Invalid line format");
             }
             else
             {
                 stringBuffer.WriteByte(bufferContext.Buffer[offset]);
             }
         }
     }
     return(false);
 }
Exemple #8
0
        public HttpServerClient(HttpServer server, Socket socket)
        {
            this.server         = server;
            this.socket         = socket;
            this.socket.NoDelay = true;
            this.socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);

            requestCounter     = 0;
            stream             = new NetStream(socket, true);
            localEndPoint      = socket.LocalEndPoint;
            remoteEndPoint     = socket.RemoteEndPoint;
            KeepAliveCountdown = 100;
            KeepAliveTimeout   = 10;

            // handle HTTPS
            if (server.ServerCertificate != null)
            {
                SslStream sslStream = new SslStream(stream, true);
                sslStream.ReadTimeout  = 0;
                sslStream.WriteTimeout = 0;
                sslStream.AuthenticateAsServer(
                    server.ServerCertificate, false, SslProtocols.Tls, false);
                dataStream = sslStream;
            }
            else
            {
                dataStream = stream;
            }

            bufferContext             = new BufferContext();
            bufferContext.Offset      = 0;
            bufferContext.Count       = 0;
            bufferContext.ReadCounter = 0;
            bufferContext.Buffer      = new byte[4096];
            bufferContext.Stream      = dataStream;
        }
 public LengthLimitedStream(BufferContext bufferContext, long length)
 {
     this.bufferContext = bufferContext;
     this.length        = length;
     position           = 0;
 }
 public static Task <string> ReadLineAsync(BufferContext bufferContext)
 {
     return(ReadLineAsync(bufferContext, new MemoryStream(128)));
 }