예제 #1
0
        public static string GetResponseCodeInfo(EHTTPResponse responseCode, HTTPHeaderData requestHeaders, VirtualHost host = null)
        {
            switch (responseCode)
            {
            default: return("");

            case EHTTPResponse.R404_NotFound: return("The requested URL " + requestHeaders.GetRequestedResource() + " was not found on this server.");
            }
        }
예제 #2
0
        public string GetFinalResourceLocation(HTTPHeaderData headers)
        {
            string resourceLoc = LocalRootDirectory + headers.GetRequestedResource();

            if (File.Exists(resourceLoc))
            {
                return resourceLoc;
            }
            else if (resourceLoc.EndsWith("/") || resourceLoc.EndsWith("\\"))
            {
                for (int i = 0; i < DirectoryIndexes.Count; i++)
                {
                    if (File.Exists(resourceLoc + DirectoryIndexes[i]))
                    {
                        return resourceLoc + DirectoryIndexes[i];
                    }
                }

                //Return generated directory info
            }

            return null;
        }
예제 #3
0
        public string GetFinalResourceLocation(HTTPHeaderData headers)
        {
            string resourceLoc = LocalRootDirectory + headers.GetRequestedResource();

            if (File.Exists(resourceLoc))
            {
                return(resourceLoc);
            }
            else if (resourceLoc.EndsWith("/") || resourceLoc.EndsWith("\\"))
            {
                for (int i = 0; i < DirectoryIndexes.Count; i++)
                {
                    if (File.Exists(resourceLoc + DirectoryIndexes[i]))
                    {
                        return(resourceLoc + DirectoryIndexes[i]);
                    }
                }

                //Return generated directory info
            }

            return(null);
        }
예제 #4
0
 public static string GetResponseCodeInfo(EHTTPResponse responseCode, HTTPHeaderData requestHeaders, VirtualHost host = null)
 {
     switch (responseCode)
     {
         default: return "";
         case EHTTPResponse.R404_NotFound: return "The requested URL " + requestHeaders.GetRequestedResource() + " was not found on this server.";
     }
 }
예제 #5
0
        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();
        }