public HTTPHeaderData(EHTTPVersion httpVersion, EHTTPResponse httpResponseCode) { HTTPVersion = httpVersion; HTTPResponseCode = httpResponseCode; InvalidHeader = false; }
public HTTPHeaderData(string headerString) { InvalidHeader = true; int doubleCRLFIndex = headerString.IndexOf("\r\n\r\n"); if (doubleCRLFIndex >= 0) headerString = headerString.Substring(0, doubleCRLFIndex); string[] headerRows = headerString.Split(new string[] { "\r\n" }, StringSplitOptions.None); if (headerRows[0].EndsWith("HTTP/1.1")) HTTPVersion = EHTTPVersion.HTTP11; else if (headerRows[0].EndsWith("HTTP/1.0")) HTTPVersion = EHTTPVersion.HTTP11; else HTTPVersion = EHTTPVersion.HTTP09; int spaceIndex = headerRows[0].IndexOf(' '); if (spaceIndex < 3 || spaceIndex > 7) return; string method = headerRows[0].Substring(0, spaceIndex); switch(method) { case "GET": HTTPMethod = EHTTPMethod.GET; break; case "POST": HTTPMethod = EHTTPMethod.POST; break; case "PUT": HTTPMethod = EHTTPMethod.PUT; break; case "DELETE": HTTPMethod = EHTTPMethod.DELETE; break; case "HEAD": HTTPMethod = EHTTPMethod.HEAD; break; case "OPTIONS": HTTPMethod = EHTTPMethod.OPTIONS; break; case "CONNECT": HTTPMethod = EHTTPMethod.CONNECT; break; default: return; //Invalid } requestedResource = headerRows[0].Substring(headerRows[0].IndexOf(' ') + 1); requestedResource = requestedResource.Substring(0, requestedResource.IndexOfAny(new char[] { ' ', '\r' })); for (int i = 1; i < headerRows.Length; i++) { if (headerRows[i] == "") break; string field = headerRows[i].Substring(0, headerRows[i].IndexOf(':')); string value = headerRows[i].Substring(headerRows[i].IndexOf(':') + 1).TrimStart(null); //Check for a malformed field headerData.Add(field, value); } InvalidHeader = false; }
public HTTPHeaderData(string headerString) { InvalidHeader = true; int doubleCRLFIndex = headerString.IndexOf("\r\n\r\n"); if (doubleCRLFIndex >= 0) { headerString = headerString.Substring(0, doubleCRLFIndex); } string[] headerRows = headerString.Split(new string[] { "\r\n" }, StringSplitOptions.None); if (headerRows[0].EndsWith("HTTP/1.1")) { HTTPVersion = EHTTPVersion.HTTP11; } else if (headerRows[0].EndsWith("HTTP/1.0")) { HTTPVersion = EHTTPVersion.HTTP11; } else { HTTPVersion = EHTTPVersion.HTTP09; } int spaceIndex = headerRows[0].IndexOf(' '); if (spaceIndex < 3 || spaceIndex > 7) { return; } string method = headerRows[0].Substring(0, spaceIndex); switch (method) { case "GET": HTTPMethod = EHTTPMethod.GET; break; case "POST": HTTPMethod = EHTTPMethod.POST; break; case "PUT": HTTPMethod = EHTTPMethod.PUT; break; case "DELETE": HTTPMethod = EHTTPMethod.DELETE; break; case "HEAD": HTTPMethod = EHTTPMethod.HEAD; break; case "OPTIONS": HTTPMethod = EHTTPMethod.OPTIONS; break; case "CONNECT": HTTPMethod = EHTTPMethod.CONNECT; break; default: return; //Invalid } requestedResource = headerRows[0].Substring(headerRows[0].IndexOf(' ') + 1); requestedResource = requestedResource.Substring(0, requestedResource.IndexOfAny(new char[] { ' ', '\r' })); for (int i = 1; i < headerRows.Length; i++) { if (headerRows[i] == "") { break; } string field = headerRows[i].Substring(0, headerRows[i].IndexOf(':')); string value = headerRows[i].Substring(headerRows[i].IndexOf(':') + 1).TrimStart(null); //Check for a malformed field headerData.Add(field, value); } InvalidHeader = false; }
public HTTPResponse(EHTTPVersion httpVersion, EHTTPResponse httpResponse) { Header = new HTTPHeaderData(httpVersion, httpResponse); }
private static void HandleClient(TcpClient client) { NetworkStream stream = client.GetStream(); bool closingConnection = false; EHTTPVersion lastHTTPVersion = EHTTPVersion.HTTP10; LogManager.Log(client, "Opening connection - Thread Started"); Stopwatch timeSinceLastRequest = new Stopwatch(); while (!closingConnection) { timeSinceLastRequest.Restart(); while (client.Connected && client.Available == 0) { if (timeSinceLastRequest.ElapsedMilliseconds > ConnectionTTL) { closingConnection = true; break; } } if (closingConnection || !client.Connected) { break; } MemoryStream ms = new MemoryStream(); while (client.Available > 0) { byte[] pdata = new byte[client.Available]; stream.Read(pdata, 0, pdata.Length); ms.Write(pdata, 0, pdata.Length); } byte[] requestData = ms.ToArray(); //Hand data to plugins if necessary //HTTP parse string request = Encoding.ASCII.GetString(requestData, 0, requestData.Length); HTTPHeaderData requestHeader = new HTTPHeaderData(request); if (requestHeader.InvalidHeader) { //Maybe it's SSL? Can't handle that LogManager.Error(client, "Recieved SSL - Cannot Handle (Yet)"); HTTPResponse sslErrorResponse = SimpleResponseManager.PrepareSimpleResponse(EHTTPResponse.R501_NotImplemented, null); sslErrorResponse.WriteToStream(stream, requestHeader, true); break; } if (!requestHeader.HasHeaderField("Connection") || !requestHeader.GetHeaderField("Connection").Contains("keep-alive")) { closingConnection = true; } lastHTTPVersion = requestHeader.HTTPVersion; //TODO: Plugin parse headers string requestLogStr = "Requested " + (requestHeader.HasHeaderField("Host") ? requestHeader.GetHeaderField("Host") : "") + requestHeader.GetRequestedResource(); VirtualHost vhost = VirtualHostManager.GetVirtualHost(requestHeader.GetHeaderField("Host")); HTTPResponse response = null; if (vhost == null) { response = SimpleResponseManager.PrepareSimpleResponse(EHTTPResponse.R404_NotFound, requestHeader); requestLogStr += " - 404"; } else { string resourceLoc = vhost.GetFinalResourceLocation(requestHeader); string clientETag = HTTPResponse.MakeETag(resourceLoc); string IFNONEMATCH = requestHeader.GetHeaderField("If-None-Match"); bool TEST = clientETag == IFNONEMATCH; if (clientETag != null && ( (requestHeader.HasHeaderField("If-Modified-Since") && vhost.HasBeenModifiedSince(resourceLoc, DateTime.Parse(requestHeader.GetHeaderField("If-Modified-Since")))) || (requestHeader.HasHeaderField("If-None-Match") && requestHeader.GetHeaderField("If-None-Match") == clientETag) )) { response = SimpleResponseManager.PrepareSimpleResponse(EHTTPResponse.R304_NotModified, requestHeader); requestLogStr += " - 304"; } else { response = vhost.GetResource(requestHeader); } } if (response == null) { response = SimpleResponseManager.PrepareSimpleResponse(EHTTPResponse.R500_InternalServerError, requestHeader); requestLogStr += " - 500"; } LogManager.Log(client, requestLogStr); response.WriteToStream(stream, requestHeader, closingConnection); } LogManager.Log(client, "Closing connection"); client.Close(); }