public void InvokeHook(FireHTTPServerHookType currentHook, dynamic executionScope, out bool handled,
            FireHTTPHookParameters hookParameters, dynamic currentServer)
        {
            handled = false; //This plugin doesn't override completely, it just logs
            var response = (ClientHttpResponse) executionScope.response;
            var responseHeaders = JsonConvert.SerializeObject(response.RequestHttpHeaders);
            var remoteIp = "<unknown>";
            try
            {
                remoteIp = ((IPEndPoint) response.BaseSocket.Client.RemoteEndPoint).Address.ToString();
            }
            catch
            {
                //Couldn't get remote IP.
            }
            var clientData = string.Format("{0} - details: {1}{2}{2}", remoteIp, responseHeaders, Environment.NewLine);
            try
            {
                switch (currentHook)
                {
                    case FireHTTPServerHookType.GetPreHook:
                        File.AppendAllText(_requestLogFile, clientData);
                        break;

                    case FireHTTPServerHookType.PostPreHook:
                        File.AppendAllText(_requestLogFile, clientData);
                        break;
                }
            }
            catch (IOException)
            {
                //File Can't be written to right now
            }
        }
        public void InvokeHook(FireHTTPServerHookType currentHook, dynamic executionScope, out bool handled,
            FireHTTPHookParameters hookParameters, dynamic currentServer)
        {
            handled = false;
            if (!_enable) return; //Don't do anything if disabled
            if (!_supportedMimeTypes.Contains(hookParameters.RequestMimeType)) return;
            var response = (ClientHttpResponse) executionScope.response;
            if (response == null) return;
            handled = true; //We are sending a modified stream
            var requestedFileNameOnDisk = hookParameters.RequestedDocumentFullPath;
            var originalFileStream = hookParameters.RequestFileContentStream;
            Stream processedStream = null;

            Logger.WriteLine($"Started HtmlScrambler on {response.RequestPath} with MIME {hookParameters.RequestMimeType}");

            switch (hookParameters.RequestMimeType)
            {
                case "text/html":
                    if (_useHashCaching && _hashCache.ContainsKey(requestedFileNameOnDisk))
                    {
                        //Cache is enabled and hash is cached, so no need to recompute
                        processedStream =
                            CommonUtilities.GenerateStreamFromString(
                                new StringBuilder(_processedFileCache[_hashCache[requestedFileNameOnDisk]]));
                    }
                    else
                    {
                        //Not cached or caching is disabled
                        StringBuilder htmlContent;
                        using (var sr = new StreamReader(originalFileStream))
                        {
                            htmlContent = new StringBuilder(sr.ReadToEnd());
                        }
                        ulong hashForCache = 0;
                        if (_useHashCaching)
                        {
                            //Caching enabled, and this file has not been cached
                            hashForCache = CommonUtilities.CalculateMiniHash(htmlContent.ToString());
                            _hashCache.Add(requestedFileNameOnDisk, hashForCache);
                        }
                        //Scramble the file
                        HtmlScrambler.ScrambleHtmlFile(ref htmlContent, 1);
                        if (_useHashCaching)
                        {
                            //Save processed file in cache
                            _processedFileCache[hashForCache] = htmlContent.ToString();
                        }
                        processedStream = CommonUtilities.GenerateStreamFromString(htmlContent);
                            //Convert processed data to stream
                    }
                    break;

                case "application/x-javascript":
                case "application/javascript":
                    if (_useHashCaching && _hashCache.ContainsKey(requestedFileNameOnDisk))
                    {
                        //Cache is enabled and hash is cached, so no need to recompute
                        processedStream =
                            CommonUtilities.GenerateStreamFromString(
                                new StringBuilder(_processedFileCache[_hashCache[requestedFileNameOnDisk]]));
                    }
                    else
                    {
                        //Not cached or caching is disabled
                        StringBuilder jsContent;
                        using (var sr = new StreamReader(originalFileStream))
                        {
                            jsContent = new StringBuilder(sr.ReadToEnd());
                        }
                        ulong hashForCache = 0;
                        if (_useHashCaching)
                        {
                            //Caching enabled, and this file has not been cached
                            hashForCache = CommonUtilities.CalculateMiniHash(jsContent.ToString());
                            _hashCache.Add(requestedFileNameOnDisk, hashForCache);
                        }
                        //Scramble the file
                        HtmlScrambler.ScrambleJavascriptFile(ref jsContent, 1);
                        if (_useHashCaching)
                        {
                            //Save processed file in cache
                            _processedFileCache[hashForCache] = jsContent.ToString();
                        }
                        processedStream = CommonUtilities.GenerateStreamFromString(jsContent);
                            //Convert processed data to stream
                    }
                    break;

                default: //for MIME types not specifically implemented
                    processedStream = originalFileStream;
                    break;
            }
            (currentServer as DynamicScriptHttpServer)?.SendStreamWithMimeType(processedStream, hookParameters.RequestMimeType, response);
                //Send the processed stream
            Logger.WriteLine("Sent scrambled file.");
        }
 private bool InvokeHook(FireHTTPServerHookType currentHook, dynamic executionScope,
     FireHTTPHookParameters hookParameters = null)
 {
     hookParameters = hookParameters ?? new FireHTTPHookParameters();
     //Use empty instance if not used (null by default)
     var handled = false;
     foreach (var plugin in _loadedHookPlugins)
     {
         if (!handled && plugin.Instance.PluginHooks.Select(hook => hook.HookType).ToList().Contains(currentHook))
         {
             //Select hook types
             plugin.Instance.InvokeHook(currentHook, executionScope, out handled, hookParameters, this);
         }
     }
     return handled;
 }
 public void InvokeHook(FireHTTPServerHookType currentHook, dynamic executionScope, out bool handled,
     FireHTTPHookParameters hookParameters, dynamic currentServer)
 {
     handled = false;
 }
 public override void SendFileWithMimeType(string path, string wwwroot, ClientHttpResponse response,
     Dictionary<string, string> requestParameters, HTTPMethod method, string postData)
 {
     path = path.Substring(1); //Remove slash at beginning
     var fullPath = Path.Combine(wwwroot, path);
     var fileInfo = new FileInfo(fullPath);
     var containingDir = fileInfo.Directory;
     var configFile = new FileInfo(containingDir?.FullName + _dirSep + CommonVariables.ConfigFileName);
     var faccess = _defaultFAccess;
     var fAccessFound = FindFAccess(ref configFile, wwwroot);
     if (fAccessFound)
     {
         var faccessJson = File.ReadAllText(configFile.FullName);
         faccess = JsonConvert.DeserializeObject<FAccessConfig>(faccessJson);
     }
     var requestHandled = HandleFAccessConfig(fileInfo, containingDir, faccess, response); //Process the FACCESS
     if (requestHandled)
         return;
     var extension = fileInfo.Extension;
     var mimeType = "application/octet-stream";
     if (CommonVariables.MimeTypeMappings.ContainsKey(extension))
     {
         mimeType = CommonVariables.MimeTypeMappings[extension];
     }
     if (!_executableFileTypes.Contains(extension))
     {
         var staticFileHookParameters = new FireHTTPHookParameters
         {
             RequestedDocumentFullPath = fullPath,
             RequestFileName = path,
             RequestMimeType = mimeType,
             RequestedFileExtension = extension
         };
         //Static file; if not executable, send normally
         using (var fs = File.OpenRead(fullPath))
         {
             staticFileHookParameters.RequestFileContentStream = fs;
             bool staticFileHookHandled = InvokeHook(FireHTTPServerHookType.StaticFileSendHook,
                 _currentHookExecutionScope, staticFileHookParameters);
             if (!staticFileHookHandled)
             {
                 SendStreamWithMimeType(fs, mimeType, response);
             }
         }
     }
     else
     {
         //Execute Script
         var executionParameters = new ScriptExecutionParameters();
         ExecuteScript(fullPath, requestParameters, response, extension, mimeType, method, postData, wwwroot,
             executionParameters);
     }
 }