private HttpRequest GetRequest(Stream inputStream) { //Processing and validating input stream 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) { Header header = new Header(HeaderType.HttpRequest); var line = StreamUtils.ReadLine(stream); string[] requestLine = line.Split(' '); RequestMethod method; if (requestLine.Length != 3) { throw new Exception("Invalid request line."); } if (requestLine[0].ToLower() == "get") { method = RequestMethod.Get; } else { method = RequestMethod.Post; } string url = requestLine[1]; while ((line = StreamUtils.ReadLine(stream)) != null) { requestLine = line.Split(':'); string name = requestLine[0]; string value = requestLine[1].Trim(); if (name == "Cookie") { header.AddCookie(new Cookie(name, value)); } else if (name == "Content-Length") { header.ContentLength = value; } else { header.OtherParameters.Add(name, value); } } 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; } line = Encoding.ASCII.GetString(bytes); } var request = new HttpRequest() { Method = method, Url = url, Header = header, Content = line }; return(Request); }