示例#1
0
        private DataContentTypeEncoding DetermineServerResponseContentTypeEncoding(Dictionary <string, List <string> > headers)
        {
            DataContentTypeEncoding contentTypeEncoding = new DataContentTypeEncoding();

            if (headers == null)
            {
                throw new ProxyWarningException("The headers list is invalid");
            }

            if (!headers.ContainsKey("Content-Type"))
            {
                throw new ProxyWarningException("The content type headerByteArray is invalid(0)");
            }

            if (headers["Content-Type"].Count <= 0)
            {
                throw new ProxyWarningException("The content type headerByteArray is invalid(1)");
            }

            if (string.IsNullOrEmpty(headers["Content-Type"][0].ToString()))
            {
                throw new ProxyWarningException("The content type headerByteArray is invalid(2)");
            }

            // If there is no content type headerByteArray set the default values
            if (!headers.ContainsKey("Content-Type") ||
                string.IsNullOrEmpty(headers["Content-Type"][0].ToString()))
            {
                contentTypeEncoding.ContentType            = "text/html";
                contentTypeEncoding.ContentCharSet         = "UTF-8";
                contentTypeEncoding.ContentCharsetEncoding = Encoding.GetEncoding(contentTypeEncoding.ContentCharSet);

                Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientBase.DetermineClientRequestContentTypeEncoding(): No Content-Type header found: text/html, UTF-8");
                return(contentTypeEncoding);
            }

            // Parse the server response content type
            try
            {
                string contentType = headers["Content-Type"][0].ToString();

                if (contentType.Contains(";"))
                {
                    string[] splitter = contentType.Split(new char[] { ';' }, 2);
                    contentTypeEncoding.ContentType            = splitter[0];
                    contentTypeEncoding.ContentCharSet         = this.DetermineContentCharSet(splitter[1]);
                    contentTypeEncoding.ContentCharsetEncoding = Encoding.GetEncoding(contentTypeEncoding.ContentCharSet);
                    Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientBase.DetermineClientRequestContentTypeEncoding(): Content-Type/Charset header found: {0}, {1}", contentTypeEncoding.ContentType, contentTypeEncoding.ContentCharSet);
                }
                else
                {
                    contentTypeEncoding.ContentType            = contentType;
                    contentTypeEncoding.ContentCharSet         = "UTF-8";
                    contentTypeEncoding.ContentCharsetEncoding = Encoding.GetEncoding(contentTypeEncoding.ContentCharSet);
                    Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientBase.DetermineClientRequestContentTypeEncoding(): Content-Type (noCharset) header found: {0}, {1}", contentTypeEncoding.ContentType, contentTypeEncoding.ContentCharSet);
                }
            }
            catch (Exception ex)
            {
                contentTypeEncoding.ContentType            = "text/html";
                contentTypeEncoding.ContentCharSet         = "UTF-8";
                contentTypeEncoding.ContentCharsetEncoding = Encoding.GetEncoding(contentTypeEncoding.ContentCharSet);
                Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientBase.DetermineClientRequestContentTypeEncoding(Exception): text/html, UTF-8 {0}", ex.Message);
            }

            return(contentTypeEncoding);
        }
        private DataContentTypeEncoding DetermineClientRequestContentTypeEncoding(RequestObj requestObj)
        {
            DataContentTypeEncoding contentTypeEncoding = new DataContentTypeEncoding();

            if (requestObj == null)
            {
                throw new ProxyWarningException("Request object is invalid");
            }

            if (requestObj.ClientRequestObj == null)
            {
                throw new ProxyWarningException("Client request object is invalid");
            }

            if (requestObj.ClientRequestObj.ClientRequestHeaders == null)
            {
                throw new ProxyWarningException("The headers list is invalid");
            }

            // If there is no content type headerByteArray set the default values
            if (!requestObj.ClientRequestObj.ClientRequestHeaders.ContainsKey("Content-Type") ||
                string.IsNullOrEmpty(requestObj.ClientRequestObj.ClientRequestHeaders["Content-Type"][0]))
            {
                contentTypeEncoding.ContentType            = "text/html";
                contentTypeEncoding.ContentCharSet         = "UTF-8";
                contentTypeEncoding.ContentCharsetEncoding = Encoding.GetEncoding(contentTypeEncoding.ContentCharSet);

                Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "IncomingClientRequest.DetermineClientRequestContentTypeEncoding(): No Content-Type header found: text/html, UTF-8");
                return(contentTypeEncoding);
            }

            // Parse the server response content type
            try
            {
                string contentType = requestObj.ClientRequestObj.ClientRequestHeaders["Content-Type"][0];

                if (contentType.Contains(";"))
                {
                    string[] splitter = contentType.Split(new char[] { ';' }, 2);
                    contentTypeEncoding.ContentType            = splitter[0];
                    contentTypeEncoding.ContentCharSet         = this.DetermineContentCharSet(splitter[1]);
                    contentTypeEncoding.ContentCharsetEncoding = Encoding.GetEncoding(contentTypeEncoding.ContentCharSet);
                    Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "IncomingClientRequest.DetermineClientRequestContentTypeEncoding(): Content-Type/Charset header found: {0}, {1}", contentTypeEncoding.ContentType, contentTypeEncoding.ContentCharSet);
                }
                else
                {
                    contentTypeEncoding.ContentType            = contentType;
                    contentTypeEncoding.ContentCharSet         = "UTF-8";
                    contentTypeEncoding.ContentCharsetEncoding = Encoding.GetEncoding(contentTypeEncoding.ContentCharSet);
                    Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "IncomingClientRequest.DetermineClientRequestContentTypeEncoding(): Content-Type (noCharset) header found: {0}, {1}", contentTypeEncoding.ContentType, contentTypeEncoding.ContentCharSet);
                }
            }
            catch (Exception ex)
            {
                contentTypeEncoding.ContentType            = "text/html";
                contentTypeEncoding.ContentCharSet         = "UTF-8";
                contentTypeEncoding.ContentCharsetEncoding = Encoding.GetEncoding(contentTypeEncoding.ContentCharSet);
                Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "IncomingClientRequest.DetermineClientRequestContentTypeEncoding(Exception): text/html, UTF-8 {0}", ex.Message);
            }

            return(contentTypeEncoding);
        }