Пример #1
0
        private HttpRequest GetRequest(Stream inputStream)
        {
            string requestLine = StreamUtils.ReadLine(inputStream);

            string[] tokens = requestLine.Split(' ');

            while (tokens.Length != 3)
            {
                requestLine = StreamUtils.ReadLine(inputStream);
                tokens      = requestLine.Split(' ');
            }

            RequestMethod method          = (RequestMethod)Enum.Parse(typeof(RequestMethod), tokens[0].ToUpper());
            string        url             = tokens[1];
            string        protocolVersion = tokens[2];

            Header header = new Header(HeaderType.HttpRequest);
            string line;

            while ((line = StreamUtils.ReadLine(inputStream)) != null)
            {
                if (line.Equals(""))
                {
                    break;
                }

                int separator = line.IndexOf(':');
                if (separator == -1)
                {
                    throw new Exception("invalid http header line: " + line);
                }
                string name = line.Substring(0, separator);
                int    pos  = separator + 1;
                while ((pos < line.Length) && (line[pos] == ' '))
                {
                    pos++;
                }

                string value = line.Substring(pos, line.Length - pos);
                if (name == "Cookie")
                {
                    string[] cookieSaves = value.Split(';');
                    foreach (var cookieSave in cookieSaves)
                    {
                        string[] cookiePair = cookieSave.Split('=').Select(x => x.Trim()).ToArray();
                        var      cookie     = new Cookie(cookiePair[0], cookiePair[1]);
                        header.AddCookie(cookie);
                    }
                }
                else if (name == "Location")
                {
                    header.Location = value;
                }
                else if (name == "Content-Length")
                {
                    header.ContentLength = value;
                }
                else
                {
                    header.OtherParameters.Add(name, value);
                }
            }



            string content = null;

            if (header.ContentLength != null)
            {
                int    totalBytes = int.Parse(header.ContentLength);
                int    bytesLeft  = totalBytes;
                byte[] bytes      = new byte[totalBytes];

                while (bytesLeft > 0)
                {
                    int bufferSize = 1024;
                    if (bytesLeft < 1024)
                    {
                        bufferSize = bytesLeft;
                    }
                    byte[] buffer = new byte[bufferSize];
                    int    n      = inputStream.Read(buffer, 0, buffer.Length);
                    int    index  = 0;
                    for (int i = totalBytes - bytesLeft; i < buffer.Length; i++)
                    {
                        bytes[i] = buffer[index];
                        index++;
                    }
                    bytesLeft -= n;
                }

                content = Encoding.ASCII.GetString(bytes);
            }



            var request = new HttpRequest()
            {
                Method  = method,
                Url     = url,
                Header  = header,
                Content = content
            };

            if (request.Header.Cookies.Contains("sessionId"))
            {
                var sessionId = request.Header.Cookies["sessionId"].Value;
                request.Session = new HttpSession(sessionId);
                if (!this.sessions.ContainsKey(sessionId))
                {
                    this.sessions.Add(sessionId, request.Session);
                }
            }

            Console.WriteLine("-REQUEST-----------------------------");
            Console.WriteLine(request);
            Console.WriteLine("------------------------------");

            return(request);
        }
 public void HandleClient(Stream stream)
 {
     Request  = GetRequest(stream);
     Response = RouteRequest();
     StreamUtils.WriteResponse(stream, Response);
 }
Пример #3
0
        private HttpRequest GetRequest(Stream inputStream)
        {
            //Read Request Line
            string requestLine = StreamUtils.ReadLine(inputStream);

            string[] tokens = requestLine.Split(' ');
            if (tokens.Length != 3)
            {
                throw new Exception("invalid http request line");
            }
            RequestMethod method          = (RequestMethod)Enum.Parse(typeof(RequestMethod), tokens[0].ToUpper());
            string        url             = tokens[1];
            string        protocolVersion = tokens[2];

            //Read Headers
            Header header = new Header(HeaderType.HttpRequest);
            string line;

            while ((line = StreamUtils.ReadLine(inputStream)) != null)
            {
                if (line.Equals(""))
                {
                    break;
                }

                int separator = line.IndexOf(':');
                if (separator == -1)
                {
                    throw new Exception("invalid http header line: " + line);
                }
                string name = line.Substring(0, separator);
                int    pos  = separator + 1;
                while ((pos < line.Length) && (line[pos] == ' '))
                {
                    pos++;
                }

                string value = line.Substring(pos, line.Length - pos);
                if (name == "Cookie")
                {
                    string[] cookieSaves = value.Split(';');
                    foreach (var cookieSave in cookieSaves)
                    {
                        string[] cookiePair = cookieSave.Split('=').Select(x => x.Trim()).ToArray();
                        var      cookie     = new Cookie(cookiePair[0], cookiePair[1]);
                        header.AddCookie(cookie);
                    }
                }
                else if (name == "Content-Length")
                {
                    header.ContentLength = value;
                }
                else
                {
                    header.OtherParameters.Add(name, value);
                }
            }



            string content = null;

            if (header.ContentLength != null)
            {
                int    totalBytes = Convert.ToInt32(header.ContentLength);
                int    bytesLeft  = totalBytes;
                byte[] bytes      = new byte[totalBytes];

                while (bytesLeft > 0)
                {
                    byte[] buffer = new byte[bytesLeft > 1024 ? 1024 : bytesLeft];
                    int    n      = inputStream.Read(buffer, 0, buffer.Length);
                    buffer.CopyTo(bytes, totalBytes - bytesLeft);

                    bytesLeft -= n;
                }

                content = Encoding.ASCII.GetString(bytes);
            }



            var request = new HttpRequest()
            {
                Method  = method,
                Url     = url,
                Header  = header,
                Content = content
            };

            Console.WriteLine("-REQUEST-----------------------------");
            Console.WriteLine(request);
            Console.WriteLine("------------------------------");

            return(request);
        }
        private HttpRequest GetRequest(Stream stream)
        {
            //get method, url and protocol version
            string requestLine = StreamUtils.ReadLine(stream);

            string[] tokens = requestLine.Split(' ');
            if (tokens.Length != 3)
            {
                throw new Exception("Invalid Http request line");
            }

            RequestMethod method = (RequestMethod)Enum.Parse(typeof(RequestMethod), tokens[0]);
            string        url    = tokens[1];

            // read header
            Header header = new Header(HeaderType.HttpRequest);
            string line;

            while ((line = StreamUtils.ReadLine(stream)) != null)
            {
                if (line == "")
                {
                    break;
                }

                int separator = line.IndexOf(':');
                if (separator == -1)
                {
                    throw new Exception("Invalid HTTP Header line: " + line);
                }
                string name = line.Substring(0, separator);
                int    pos  = separator + 1;
                while ((pos < line.Length) && (line[pos] == ' '))
                {
                    pos++;
                }

                string value = line.Substring(pos, line.Length - pos);

                if (name == "Cookie")
                {
                    string[] cookies = value.Split(';');
                    foreach (string cookie in cookies)
                    {
                        string[] cookieTokens = cookie.TrimStart().Split('=');
                        header.Cookies.AddCookie(new Cookie(cookieTokens[0], cookieTokens[1]));
                    }
                }
                else if (name == "Content-Length")
                {
                    header.ContentLenght = value;
                }
                else
                {
                    header.OtherParameters.Add(name, value);
                }
            }

            string content = null;

            if (header.ContentLenght != null)
            {
                int    totalBytes = Convert.ToInt32(header.ContentLenght);
                int    bytesLeft  = totalBytes;
                byte[] bytes      = new byte[totalBytes];

                while (bytesLeft > 0)
                {
                    byte[] buffer = new byte[bytesLeft > 1024 ? 1024 : bytesLeft];
                    int    n      = stream.Read(buffer, 0, buffer.Length);
                    buffer.CopyTo(bytes, totalBytes - bytesLeft);

                    bytesLeft -= n;
                }

                content = Encoding.ASCII.GetString(bytes);
            }

            HttpRequest request = new HttpRequest()
            {
                Method  = method,
                Url     = url,
                Header  = header,
                Content = content
            };

            Console.WriteLine("-REQUEST-----------------------------");
            Console.WriteLine(request);
            Console.WriteLine("------------------------------");

            return(request);
        }