// METHOD : ValidateAndSetRequest // DESCRIPTION : This method does the initial validation of the // HTTP request to see if its structure is valid. // // PARAMETERS : // string request : contains the request line from the client // string serverInfo : contains the ip address and port of the server // to validate against the URI portion of request // // RETURNS : // Boolean : returns true if request is valid, false otherwise // public Boolean ValidateAndSetRequest(string request, string serverInfo) { Boolean isValid = false; //Split request string by spaces. GET | HTTP://.... | HTTP/Version string[] split = request.Split(' '); //Validate request method to ensure GET was used if (ValidateRequestMethod(split[0]) != true) { StatusCode = NOT_IMPLEMENTED; } else if (ValidateHTTPStructure(split[1], serverInfo) != true) { StatusCode = BAD_REQUEST; } else if (ValidateHTTPVersion(split[2]) != true) { StatusCode = HTTP_VERSION_NOT_SUPPORTED; } else if (ValidateFileType(split[1]) != true) { StatusCode = NOT_IMPLEMENTED; } else if (ValidateFileExists(split[1], serverInfo) != true) { StatusCode = NOT_FOUND; } else { Method = split[0]; RequestURI = split[1]; HTTPVersion = split[2]; StatusCode = ALL_OKAY; isValid = true; } //Strip host info from request URI string resource = RequestURI.Replace("http://", ""); resource = resource.Replace(serverInfo, ""); //Log incoming request string message = "[REQUEST] " + Method + resource; Logger.Log(message); //Build Response somewhere? return(isValid); }
public string CreateResponse() { string response = HTTPVersion + StatusCode + "\n"; string contentPath = RequestURI.Replace(Host, ""); if (!StatusCode.ToString().StartsWith("4") || !StatusCode.ToString().StartsWith("5")) { if (ContentType.StartsWith("text")) { //string data = File.ReadAllText(ServerRoot + contentPath); } else { //byte[] data = File.ReadAllBytes(ServerRoot + contentPath); } response += "Content-Type: " + ContentType + "\n"; response += "Content-Length: "; } return(response); }