public void ShouldParseQueryString() { var req = new RequestFactory().Create(new string[0], null, "/file.ext?key1=value+1&key2=value+2"); Assert.AreEqual("value 1", req.QueryString["key1"]); Assert.AreEqual("value 2", req.QueryString["key2"]); Assert.AreEqual(2, req.QueryString.Count); }
public void ShouldParseHeaders() { var lines = new[] { "(doesn't matter)", "header1: value1", "header2: value2" }; var req = new RequestFactory().Create(lines, null, ""); Assert.AreEqual("value1", req["header1"]); Assert.AreEqual("value1", req["header1"]); Assert.AreEqual(2, req.Headers.Count); }
public void ShouldParseFormValues() { var lines = new[] { "(not important, the HTTP command", "Content-Type: application/x-www-form-urlencoded", "", //separate headers from posted values "key1=value+1&key2=value+2" }; var req = new RequestFactory().Create(lines, null, ""); Assert.AreEqual("value 1", req.Form["key1"]); Assert.AreEqual("value 2", req.Form["key2"]); Assert.AreEqual(2, req.Form.Count); }
public void ShouldReturnRequestInstance() { var req = new RequestFactory().Create(new string[0], null, ""); Assert.IsNotNull(req); }
public void WaitForConnection() { try { while (true) { //Accept a new connection using (var socket = _listener.AcceptSocket()) { if(socket.Connected) { Console.WriteLine("\nRequest from IP {0}\n", socket.RemoteEndPoint); string reqText = GetRequestText(socket); if (string.IsNullOrEmpty(reqText)) { Console.WriteLine("Empty request, canceling."); socket.Close(); continue; } string[] lines = reqText.Split(new[] {"\r\n"}, StringSplitOptions.None); string firstLine = lines[0]; //(starting n the next line is what a GET request looks like, line break = \r\n //GET /some/path/in/the/server.html HTTP/1.1 //Host: localhost:8081 //User-Agent: Mozilla/5.0 (blah blah blah) //Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 //Accept-Language: en-us,en;q=0.5 //Accept-Encoding: gzip,deflate //Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 //Keep-Alive: 300 //Connection: keep-alive //Cookie: cookie1=val1; cookie2=val2; string vpath = "/"; //IMountPointHandler handler; MountPoint mount = null; if (CheckIfHttpRequest(firstLine)) { string[] httpCommand = firstLine.Split(' '); //so this must be an HTTP request var httpVerb = httpCommand[0]; //a vpath must have been given in the command vpath = httpCommand[1]; Console.WriteLine("Requested path:" + vpath); if(ValidateHttpVerb(httpVerb)) { mount = FindMount(vpath); } } if(mount == null) { mount = CreateUnacceptableMountPoint(vpath); } Console.WriteLine("Request being handled at vpath: {0}, by handler: {1}", mount.VirtualPath, mount.Handler); var request = new RequestFactory().Create(lines, mount, vpath); var response = CreateResponse(HttpStatusCode.OK, "OK"); mount.Handler.Respond(request, response); SendResponse(response, socket); socket.Close(); } } Thread.Sleep(50); } } catch (SocketException) { } }