Ejemplo n.º 1
0
 public Request(Methods.Base method, Uri uri)
 {
     RequestLine = new RequestLine()
     {
         HttpVersion = "HTTP/1.1", Method = method, RequestUri = uri
     };
 }
Ejemplo n.º 2
0
 public Request(System.Web.HttpRequest request)
 {
     Headers     = new Message.HeaderCollection(request.Headers);
     RequestLine = new RequestLine()
     {
         HttpVersion = "HTTP/1.1",
         Method      = Methods.Base.Parse(request.HttpMethod),
         RequestUri  = request.Url
     };
     Body = new Message.Body()
     {
         IsChunked     = false,
         ReceiveStream = request.InputStream
     };
 }
Ejemplo n.º 3
0
 public Request(System.Web.HttpRequest request)
 {
     Headers = new Message.HeaderCollection(request.Headers);
     RequestLine = new RequestLine()
     {
         HttpVersion = "HTTP/1.1",
         Method = Methods.Base.Parse(request.HttpMethod),
         RequestUri = request.Url
     };
     Body = new Message.Body() 
     { 
         IsChunked = false, 
         ReceiveStream = request.InputStream 
     };
 }
Ejemplo n.º 4
0
        public static RequestLine Parse(string line)
        {
            RequestLine rl = new RequestLine();
            string      method, requestUri, httpVersion;
            int         loc;

            line        = line.Trim();
            loc         = line.IndexOf(" ");
            method      = line.Substring(0, loc).Trim();
            line        = line.Substring(loc).Trim();
            loc         = line.IndexOf(" ");
            requestUri  = line.Substring(0, loc).Trim();
            line        = line.Substring(loc).Trim();
            httpVersion = line;

            switch (method)
            {
            case "DELETE":
                rl.Method = new Methods.Delete();
                break;

            case "GET":
                rl.Method = new Methods.Get();
                break;

            case "HEAD":
                rl.Method = new Methods.Head();
                break;

            case "POST":
                rl.Method = new Methods.Post();
                break;

            case "PUT":
                rl.Method = new Methods.Put();
                break;

            default:
                throw new MethodParseException();
            }

            rl.RequestUri  = new Uri(requestUri);
            rl.HttpVersion = httpVersion;

            return(rl);
        }
Ejemplo n.º 5
0
        public static RequestLine Parse(string line)
        {
            RequestLine rl = new RequestLine();
            string method, requestUri, httpVersion;
            int loc;

            line = line.Trim();
            loc = line.IndexOf(" ");
            method = line.Substring(0, loc).Trim();
            line = line.Substring(loc).Trim();
            loc = line.IndexOf(" ");
            requestUri = line.Substring(0, loc).Trim();
            line = line.Substring(loc).Trim();
            httpVersion = line;

            switch (method)
            {
                case "DELETE":
                    rl.Method = new Methods.Delete();
                    break;
                case "GET":
                    rl.Method = new Methods.Get();
                    break;
                case "HEAD":
                    rl.Method = new Methods.Head();
                    break;
                case "POST":
                    rl.Method = new Methods.Post();
                    break;
                case "PUT":
                    rl.Method = new Methods.Put();
                    break;
                default:
                    throw new MethodParseException();
            }

            rl.RequestUri = new Uri(requestUri);
            rl.HttpVersion = httpVersion;

            return rl;
        }
Ejemplo n.º 6
0
 public Request(Methods.Base method, Uri uri)
 {
     RequestLine = new RequestLine() { HttpVersion = "HTTP/1.1", Method = method, RequestUri = uri };
 }
        public override void Parse()
        {
            // A status line will always be first, its possible another status line will follow
            // (consider 100 continue then a 404)

            int    index;
            string newpacket;


            newpacket = BytesToString(_remainingBuffer, 0, _remainingBufferAppendPosition);

            if ((index = newpacket.IndexOf("\r\n\r\n")) < 0)
            {
                // Ending sequence is not found, we need to append to our string and wait for
                // Parse to get called again.
                _firstLineAndHeaders += newpacket;

                // Clear _remainingBuffer since it was all used
                _remainingBuffer = null;
                _remainingBufferAppendPosition = 0;


                if (!string.IsNullOrEmpty(_firstLineAndHeaders) &&
                    (newpacket.StartsWith("GET") || newpacket.StartsWith("DELETE") ||
                     newpacket.StartsWith("HEAD") || newpacket.StartsWith("POST") ||
                     newpacket.StartsWith("PUT")))
                {
                    string      temp = newpacket.Substring(0, newpacket.IndexOf("\r\n")).Trim();
                    RequestLine rl   = RequestLine.Parse(temp);
                    Request = new Request(rl.Method, rl.RequestUri);
                }
            }
            else
            {
                string[]      parts;
                List <string> lines;

                // index + 4 = how many bytes to skip in _remainingBuffer then tie the stream to the
                // Response.Body

                // Possible index placements (index < 0 is handled and index >= newpacket.length-4 is impossible
                // 1) index == 0
                // 2) index between 0 and newpacket.length-4

                // Append the headers from newpacket to the other status and headers
                _firstLineAndHeaders += newpacket.Substring(0, index);
                // Reduce the buffer by the removed bytes
                _remainingBuffer = TrimStartBuffer(_remainingBuffer, index + 4);
                // Reduce the append position by the number of removed bytes
                _remainingBufferAppendPosition -= index + 4;

                lines = GetLines(_firstLineAndHeaders);

                if (Request == null || Request.RequestLine == null)
                {
                    // We have no request line yet, need to parse one
                    RequestLine rl = RequestLine.Parse(lines[0]);
                    Request = new Request(rl.Method, rl.RequestUri);
                }

                Request.Headers.Clear();
                for (int i = 1; i < lines.Count; i++)
                {
                    parts    = new string[2];
                    parts[0] = lines[i].Substring(0, lines[i].IndexOf(':')).Trim();
                    parts[1] = lines[i].Substring(lines[i].IndexOf(':') + 1).Trim();

                    Response.Headers.Add(new Message.Token(parts[0]), parts[1]);
                }

                AllHeadersReceived = true;
            }
        }