Esempio n. 1
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);
        }
Esempio n. 2
0
        private HTTPRequest GetRequest(NetworkStream inputStream)
        {
            string firstLine = StreamUtils.ReadLine(inputStream);

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

            if (requestLine.Length < 3)
            {
                throw new ArgumentException("Missing data");
            }

            RequestMethod method = RequestMethod.GET;

            if (requestLine[0].ToLower() == "post")
            {
                method = RequestMethod.POST;
            }

            string url    = requestLine[1];
            Header header = new Header(HeaderType.HttpRequest);
            string line;

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

                string[] headerInfo = line.Split(':');
                string   name       = headerInfo[0];
                string   value      = headerInfo[1];

                switch (name)
                {
                case "Cookie":
                    Cookie cookie = new Cookie(name, value);
                    header.Coockies.Add(cookie);
                    break;

                case "Content-Length":
                    header.ContentLength = value;
                    break;

                default:
                    if (!header.OtherParameters.ContainsKey(name))
                    {
                        header.OtherParameters.Add(name, value);
                    }

                    header.OtherParameters[name] = value;
                    break;
                }
            }

            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);
            }

            HTTPRequest request = new HTTPRequest(method, url)
            {
                Content = content,
                Header  = header
            };

            return(request);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        private HttpRequest GetRequest(NetworkStream stream)
        {
            var requestArgs = StreamUtils
                              .ReadLine(stream)
                              .Split(' ');
            string requestMethod          = requestArgs[0];
            string requestUrl             = requestArgs[1];
            string requestProtocolVersion = requestArgs[2];

            if (requestMethod == null || requestUrl == null || requestProtocolVersion == null)
            {
                throw new NullReferenceException("Some of request elements is missing!");
            }

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

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

                var    headerParams = line.Split(new[] { ':' }, 2).Select(s => s.Trim()).ToArray();
                string name         = headerParams[0];
                string value        = headerParams[1];

                if (name.ToLower() == "cookie")
                {
                    string[] cookies = value.Split(';');
                    foreach (var cookie in cookies)
                    {
                        string[] cookieArgs  = cookie.Split('=');
                        var      cookieName  = cookieArgs[0];
                        var      cookieValue = cookieArgs[1];

                        Cookie cookieObject = new Cookie {
                            Name = cookieName, Value = cookieValue
                        };
                        header.Cookies.AddCookie(cookieObject);
                    }
                }
                else if (name.ToLower() == "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      = stream.Read(buffer, 0, buffer.Length);
                    buffer.CopyTo(bytes, totalBytes - bytesLeft);

                    bytesLeft -= n;
                }

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

            var request = new HttpRequest
            {
                Method  = (RequestMethod)Enum.Parse(typeof(RequestMethod), requestMethod),
                Url     = requestUrl,
                Header  = header,
                Content = content
            };

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

            return(request);
        }
Esempio n. 5
0
        private HttpRequest GetRequest(NetworkStream inputStream)
        {
            string firstLine = StreamUtils.ReadLine(inputStream);

            string[] requestLineInfo = firstLine.Split(' ');
            if (requestLineInfo.Length < 3)
            {
                throw new ArgumentException();
            }

            RequestMethod method = RequestMethod.GET;

            if (requestLineInfo[0].ToLower() == "post")
            {
                method = RequestMethod.POST;
            }

            string url = requestLineInfo[1];

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

            while ((line = StreamUtils.ReadLine(inputStream)) != null)
            {
                if (String.IsNullOrEmpty(line))
                {
                    break;
                }
                string[] lineInfo = line.Split(':');
                string   name     = lineInfo[0];
                string   value    = lineInfo[1];
                if (name == "Cookie")
                {
                    header.Cookies[name] = new Cookie(name, value);
                }
                else if (name == "Content-Length")
                {
                    header.ContentLength = value;
                }
                else
                {
                    header.OthersParameters[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, url)
            {
                Content = content,
                Header  = header
            };

            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);
        }