Beispiel #1
0
        public Response GetResponseForRequest(Request request, string root)
        {
            Response response = new Response(request);

            response.StatusCode = 200;
            response.StatusMessage = "OK";
            bool dirBrowsing = false;

            if (!ParseValidUrl(request.Url))
            {
                request.Url = FixUrl(request.Url);
                string defPage = GetDefaultPage(request, root);
                dirBrowsing = (ServerConfig.DirectoryBrowsing && defPage == null && request.Port == ServerConfig.WebPort);
                if (!dirBrowsing)
                    request.Url = request.Url + defPage;
                Console.WriteLine("Dir browsing: " + dirBrowsing);
            }

            response.Path = root + request.Url;
            if (File.Exists(response.Path) || dirBrowsing)
            {
                if (!dirBrowsing)
                    response.Content = File.ReadAllBytes(response.Path);
                else
                    response.Content = LoadDirBrowsingPage(response.Path);
            }

            if (!response.SetDefaultHeaders())
                response.Content = GetErrorPage(response.StatusCode);

            return response;
        }
Beispiel #2
0
 public Response(Request request)
     : base()
 {
     HTTPVersion = (!String.IsNullOrEmpty(request.HTTPVersion)) ? request.HTTPVersion : "HTTP/1.1";
     Url = request.Url;
 }
Beispiel #3
0
 private void ParseRequestValues(Request request)
 {
     switch (request.Method)
     {
         case "GET":
             ParseGetValues(request);
             break;
         case "POST":
             ParseGetValues(request);
             ParsePostValues(request);
             break;
     }
 }
Beispiel #4
0
 private void ParsePostValues(Request request)
 {
     if (request.GetHeader("content-type") == _formDataContentType)
     {
         request.Values = ParseFormData(request.Body, request.Values);
     }
 }
Beispiel #5
0
 private void ParseGetValues(Request request)
 {
     string[] urlParts = request.Url.Split('?');
     request.Url = urlParts[0];
     if (urlParts.Length > 1)
     {
         request.Values = ParseFormData(urlParts[1], request.Values);
     }
 }
Beispiel #6
0
        public Request ParseStringToRequest(string requestHeaders)
        {
            string[] reqParts = requestHeaders.Split(Environment.NewLine.ToCharArray());
            if (reqParts.Length > 0 && !String.IsNullOrEmpty(reqParts[0]))
            {
                string firstLine = reqParts[0];
                reqParts[0] = null;
                string[] firstLineParts = firstLine.Split(' ');
                Request request = new Request();

                request.Method = firstLineParts[0];
                request.Url = firstLineParts[1];
                request.HTTPVersion = firstLineParts[2];

                foreach (string line in reqParts)
                {
                    if (!String.IsNullOrEmpty(line))
                    {
                        if (!line.Contains(":"))
                            break;

                        string[] headerParts = line.Split(new char[] { ':' }, 2);
                        request.AddHeader(headerParts[0].Trim().ToLower(), headerParts[1].Trim());
                    }
                }

                if (request.GetHeader("host") != null)
                {
                    string hostHeader = request.GetHeader("host");
                    if (hostHeader.Contains(":"))
                    {
                        string[] hostParts = hostHeader.Split(':');
                        request.Host = hostParts[0];
                        request.Port = Convert.ToInt32(hostParts[1]);
                    }
                    else
                    {
                        request.Host = hostHeader;
                        request.Port = 80;
                    }
                }

                return request;
            }
            return null;
        }
Beispiel #7
0
 public bool IsRequestValid(Request request)
 {
     return (request != null && (request.Method == "POST" || request.Method == "GET"));
 }
Beispiel #8
0
 public RouteHandler(Request request, Response response)
 {
     _request = request;
     _response = response;
 }
Beispiel #9
0
 private string GetDefaultPage(Request request, string root)
 {
     string[] defaultPages = ServerConfig.DefaultPage.Split(';');
     foreach (string defPage in defaultPages)
     {
         if (File.Exists(root + request.Url + defPage))
             return defPage;
     }
     return null;
 }