예제 #1
0
        public static HttpResponse ReadFile(HttpRequest request)
        {
            if (programPath == "")
            {
                programPath = Environment.CurrentDirectory;
                programPath = Path.Combine(programPath + "/data/Web");
            }

            //Gets Local Request File
            string path = request.path.LocalPath;

            //Removes The First Symbol for Better Path Combining
            string trimmedPath = path.Remove(0, 1);

            //Combines Program And Request Paths Into One
            string filePath = Path.Combine(programPath, trimmedPath);

            //If File Is Not Found Returns 404
            if (!File.Exists(filePath))
            {
                return(HttpErrors.GenerateHttpError(404));
            }

            //If File Access Is Restricted And Not Authorized Returns 401
            if (AccessManager.IsFileRestricted(filePath, out JSON.FileAccessess authorizationFile))
            {
                if (!IsAuthorized(request, filePath, authorizationFile))
                {
                    return(HttpErrors.GenerateHttpError(401));
                }
            }

            //Creates New Http Response
            HttpResponse response = new HttpResponse("test", 200, "text/html");

            //Adds File As Http Response Body
            response.AddFile(filePath);

            return(response);
        }