예제 #1
0
        private byte[] ManipulateServerResponseBeforeForwarding(byte[] serverResponse)
        {
            //Chk for too short (invalid) server responses
            if (serverResponse.Length < 4)
            {
                if (Configuration.IsDebugModeOn)
                {
                    _CLI.Out("Error. Too short server response. Nothing to manipulate. (Client ID: " + _ClientIdCounter.ToString() + ")");
                }
                return(serverResponse);
            }

            ServerResponse SRep = null;

            //First, check if every response needs to be modified or only HTMLs
            if (Configuration.ProxyHtmlFilesOnly)
            {
                //Split the response to header and body if only HTML files need modification
                SRep = SplitServerResponse(serverResponse, Configuration.ProxyHtmlFilesOnly);
            }
            else
            {
                //If all response needs to be modified, then convert the response to string
                string asStr = Encoding.UTF8.GetString(serverResponse);

                // Replace hostname in every request
                string hostNameToReplace = Configuration.WebServerAddress.ToString();
                string newHostName       = Configuration.LocalAddress.ToString() + ":" + Configuration.ListenerPortNumber.ToString();;
                string strResult         = asStr.Replace(hostNameToReplace, newHostName);

                //Remove content length
                DeleteContentLength(ref strResult);

                //Convert the result back to bytes, and return it
                return(Encoding.UTF8.GetBytes(strResult));
            }

            //This runs when only HTMLs whould be modified.
            //If header exists, then manipulate it
            if (SRep.Header != null)
            {
                if (SRep.isHtmlTextContent == true)
                {
                    //If only HTML files should be modified an this is a html file in the response

                    //Remove content length section from the response
                    DeleteContentLength(ref SRep);

                    //Replace the server's address with proxy address in both header and body
                    ReplaceAddressInServerResponse(ref SRep);

                    //Add WeSOF Guid if not present
                    _Cookie.EnsureGUIDisPresent(ref SRep);

                    //Pass it to the MVT manager
                    PassToMVTManager(ref SRep);

                    //Check if conversion is triggered by the last url
                    //This may also result in a test completion, if it reaches significance
                    ConversionManager.CheckUrlAgainstConversionTriggers(_LastRequestedURL, SRep.Cookie.Wesof_GUID);

                    //Merge the modified header and body too
                    MergeModifiedBodyAndHeader(ref SRep);
                    return(SRep.MergedModified);
                }

                if (SRep.isHtmlTextContent == null || SRep.isHtmlTextContent == false)
                {
                    //Remove content length section from the response
                    DeleteContentLength(ref SRep);

                    //Merge the modified header and body too
                    MergeModifiedBodyAndHeader(ref SRep);
                    return(SRep.MergedModified);

                    //Return server response as is
                    //return serverResponse;
                }
            }
            else
            {
                //The response does not have a header, or the splitter method failed. :-/
                //Return the original message without any manipulation
                return(serverResponse);
            }


            //This should never run
            if (Configuration.IsDebugModeOn)
            {
                _CLI.Out("Error. Could not analyse the server response. Forwarding as-is. (Client ID: " + _ClientIdCounter.ToString() + ")");
            }
            return(serverResponse);
        }