Ejemplo n.º 1
0
        private async Task <Request> ReadHeaders(PipeReader input)
        {
            Request request = null;

            try
            {
                var requestMsg = string.Empty;
                while ((request = HttpMessageParsing.ParseRequest(requestMsg, false)) == null)
                {
                    var result = await input.ReadAsync();

                    foreach (var seg in result.Buffer)
                    {
                        requestMsg += MsgEncoding.GetString(seg.Span);
                    }
                    input.AdvanceTo(result.Buffer.End);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.GetType()}: {ex.Message}");
            }

            return(request);
        }
Ejemplo n.º 2
0
        public async Task <Response> Post(string server, int port, string content)
        {
            var message = MsgEncoding.GetBytes(content);
            var client  = new TcpClient(server, port);

            client.SendTimeout = client.ReceiveTimeout = 500;

            var request = new Request
            {
                Method      = "POST",
                OriginalUrl = "/",
                HttpVersion = new Version(1, 1)
            };

            request.Headers.AddHeader(KnownHeaders.Host, server);
            request.Headers.AddHeader(KnownHeaders.ContentLength, message.Length.ToString());
            request.Headers.AddHeader(KnownHeaders.Expect, KnownHeaders.Expect100Continue);

            var header = MsgEncoding.GetBytes(request.HeaderText);
            await client.GetStream().WriteAsync(header, 0, header.Length);

            var      buffer      = new byte[1024];
            var      responseMsg = string.Empty;
            Response response    = null;

            while ((response = HttpMessageParsing.ParseResponse(responseMsg)) == null)
            {
                var readTask = client.GetStream().ReadAsync(buffer, 0, 1024);
                if (!readTask.Wait(200))
                {
                    return(null);
                }
                responseMsg += MsgEncoding.GetString(buffer, 0, readTask.Result);
            }

            if (response.StatusCode == 100)
            {
                await client.GetStream().WriteAsync(message);

                responseMsg = string.Empty;

                while ((response = HttpMessageParsing.ParseResponse(responseMsg)) == null)
                {
                    var readTask = client.GetStream().ReadAsync(buffer, 0, 1024);
                    if (!readTask.Wait(200))
                    {
                        return(null);
                    }
                    responseMsg += MsgEncoding.GetString(buffer, 0, readTask.Result);
                }

                return(response);
            }
            else
            {
                return(response);
            }
        }
        private async Task <Request> ReadBody(Request request, PipeReader input)
        {
            var msg = request.HeaderText;

            try
            {
                while ((request = HttpMessageParsing.ParseRequest(msg, true)) == null)
                {
                    var result = await input.ReadAsync();

                    foreach (var seg in result.Buffer)
                    {
                        msg += MsgEncoding.GetString(seg.Span);
                    }
                    input.AdvanceTo(result.Buffer.End);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.GetType()}: {ex.Message}");
            }
            return(request);
        }