Exemplo n.º 1
0
        protected virtual void TransformRequestHeaders(NameValueCollection leftSideHeaders, WebHeaderCollection rightSideHeaders)
        {
            foreach (string headerName in leftSideHeaders)
            {
                var h = headerName.ToLowerInvariant();

                string headerValue = leftSideHeaders[headerName];

                if (HandlePvpHeader(headerName, headerValue, rightSideHeaders))
                {
                    continue;
                }

                if (s_headersToRemove.Contains(h))
                {
                    TraceScope.Current.TraceEvent(System.Diagnostics.TraceEventType.Verbose,
                                                  (int)Event.TransformRequest,
                                                  "Removing Header {0}.", headerName);
                    continue;
                }

                switch (h)
                {
                case "connection":
                case "date":
                case "host":
                    //Restricted Header
                    TraceScope.Current.TraceEvent(System.Diagnostics.TraceEventType.Verbose,
                                                  (int)Event.TransformRequest,
                                                  "Ignoring Header {0}.", headerName);
                    break;

                case "authorization":
                    bool remove = false;
                    if (s_authorizationToRemove != null)
                    {
                        foreach (string authorizationType in s_authorizationToRemove)
                        {
                            if (!String.IsNullOrEmpty(authorizationType) &&
                                headerValue.StartsWith(authorizationType))
                            {
                                remove = true;
                                break;
                            }
                        }
                    }

                    if (!remove)
                    {
                        rightSideHeaders.Add(headerName, headerValue);
                    }

                    break;

                case "keep-alive":
                    // true is default _rightSideRequest.KeepAlive = true;
                    break;

                case "transfer-encoding":
                    if (String.Equals(headerValue, "chunked", StringComparison.InvariantCultureIgnoreCase))
                    {
                        _rightSideRequest.SendChunked = true;
                    }
                    else
                    {
                        _rightSideRequest.TransferEncoding = headerValue;
                    }
                    break;

                case "content-length":
                    _rightSideRequest.ContentLength = _leftSideRequest.ContentLength;
                    break;

                case "content-type":
                    _rightSideRequest.ContentType = _leftSideRequest.ContentType;
                    break;

                case "accept":
                    _rightSideRequest.Accept = headerValue;
                    break;

                case "cookie":
                    TraceScope.Current.TraceEvent(System.Diagnostics.TraceEventType.Verbose,
                                                  (int)Event.TransformRequest, "Transforming Cookies");
                    CookieTransformer cookieTransformer = new CookieTransformer(_isolateCookies, _targetRootUrl);
                    _rightSideRequest.CookieContainer.Add(cookieTransformer.GetRightSideRequestCookies(_leftSideRequest));
                    break;

                case "expect":
                    string val = headerValue;
                    if (val.ToLowerInvariant() == "100-continue")
                    {
                        _rightSideRequest.ServicePoint.Expect100Continue = true;
                    }
                    else
                    {
                        _rightSideRequest.Expect = headerValue;
                    }
                    break;

                case "if-modified-since":
                    _rightSideRequest.IfModifiedSince = DateTime.Parse(headerValue);
                    break;

                case "referer":
                    _rightSideRequest.Referer = headerValue;
                    break;

                case "range":
                    string   rangeValue = headerValue;
                    string[] rangeParts = rangeValue.Split('=');
                    string   rangeType;
                    if (rangeParts.Length >= 2)
                    {
                        rangeType = rangeParts[0];
                        foreach (string range in rangeParts[1].Split(','))
                        {
                            string[] limits = range.Split('-');
                            if (limits.Length >= 2)
                            {
                                TraceScope.Current.TraceEvent(System.Diagnostics.TraceEventType.Verbose, (int)Event.TransformRequest,
                                                              "Transforming Range Header {0}.", range);
                                if (limits[0].Length > 0 && limits[1].Length > 0)
                                {
                                    _rightSideRequest.AddRange(rangeType, Int32.Parse(limits[0]), Int32.Parse(limits[1]));
                                }
                                else if (limits[0].Length > 0 && limits[1].Length == 0)
                                {
                                    _rightSideRequest.AddRange(Int32.Parse(limits[0]) * (-1));
                                }
                                else if (limits[0].Length == 0 && limits[1].Length > 0)
                                {
                                    _rightSideRequest.AddRange(Int32.Parse(limits[1]));
                                }
                                else
                                {
                                    TraceScope.Current.TraceEvent(System.Diagnostics.TraceEventType.Verbose,
                                                                  (int)Event.TransformRequest, "Error Transforming Range Header {0}.",
                                                                  range);
                                    throw new HttpParseException("range header not understood: " + rangeValue);
                                }
                            }
                            else
                            {
                                TraceScope.Current.TraceEvent(System.Diagnostics.TraceEventType.Verbose,
                                                              (int)Event.TransformRequest, "Error Transforming Range Header {0}.", range);
                                throw new HttpParseException("range header not understood: " + rangeValue);
                            }
                        }
                    }
                    break;

                case "user-agent":
                    _rightSideRequest.UserAgent = _leftSideRequest.UserAgent;
                    break;

                case "from":
                    if (!Settings.Default.UseFromHeader)
                    {
                        rightSideHeaders.Add(headerName, headerValue);
                    }
                    break;

                // all other Headers not handled yet are copied 1:1
                default:
                    rightSideHeaders.Add(headerName, headerValue);
                    break;
                }
            }
        }
Exemplo n.º 2
0
        protected virtual void TransformResponseHeaders()
        {
            var cookiesWithEmptyPath = new List <string>();

            for (int i = 0; i < _rightSideResponse.Headers.Count; i++)
            //foreach (string headerName in _rightSideResponse.Headers)
            {
                var headerName   = _rightSideResponse.Headers.GetKey(i);
                var headerValues = _rightSideResponse.Headers.GetValues(i);
                foreach (var headerValue in headerValues.Length > 0 ? headerValues : new string[] { null })
                {
                    switch (headerName.ToLowerInvariant())
                    {
                    // Do nothing
                    case "keep-alive":
                    case "content-length":
                    case "range":
                        TraceScope.Current.TraceEvent(System.Diagnostics.TraceEventType.Verbose,
                                                      (int)Event.TransformResponse,
                                                      "Ignoring Header {0} with value {1}.", headerName,
                                                      headerValue);
                        break;

                    case "transfer-encoding":
                        //if (String.Equals(headerValue, "chunked", StringComparison.InvariantCultureIgnoreCase))
                        //    _leftSideResponse.SendChunked = true;
                        // else if (String.Equals(headerValue, "compress, deflate, gzip, identity", StringComparison.InvariantCultureIgnoreCase))
                        // else do nothing
                        break;

                    // Cookies
                    case "set-cookie":
                    case "set-cookie2":
                        TraceScope.Current.TraceEvent(System.Diagnostics.TraceEventType.Verbose,
                                                      (int)Event.TransformResponse,
                                                      "Transforming Cookies.");
                        var cookieName = GetCookieName(headerValue);
                        if (cookieName == null)
                        {
                            break;
                        }
                        var hasPath = headerValue.IndexOf("path=", StringComparison.InvariantCultureIgnoreCase);
                        if (hasPath == -1)
                        {
                            cookiesWithEmptyPath.Add(cookieName);
                        }
                        break;

                    //Location
                    case "location":
                        string location = LocationTransformer.AdjustPath(headerValue);
                        TraceScope.Current.TraceEvent(System.Diagnostics.TraceEventType.Verbose,
                                                      (int)Event.TransformResponse,
                                                      "Transforming Location Header from {0} to {1}.",
                                                      headerValue, location);
                        _leftSideResponse.AddHeader(headerName, location);
                        break;

                    // all other Headers not handled yet are copied 1:1
                    default:
                        TraceScope.Current.TraceEvent(System.Diagnostics.TraceEventType.Verbose,
                                                      (int)Event.TransformResponse,
                                                      "Copy Header {0} with value {1}.", headerName,
                                                      headerValue);
                        _leftSideResponse.AddHeader(headerName, headerValue);
                        break;
                    }
                }
            }
            CookieTransformer cookieTransformer = new CookieTransformer(_isolateCookies, _targetRootUrl, _rightSideResponse.ResponseUri);

            // https://github.com/dotnet/corefx/issues/19166
            // wenn Cookie Path empty, dann sollte current Path herangezogen werden.
            // Beispiel Url = https://pamgate2.portal.at/at.gv.bmvit.uhs_p/uhs_c/f?p=blablabla
            // Path sollte sein https://pamgate2.portal.at/at.gv.bmvit.uhs_p/uhs_c
            // Path ist aber https://pamgate2.portal.at/at.gv.bmvit.uhs_p/uhs_c/f
            // daher wird Cookie nicht zurückgeliefert
            foreach (HttpCookie cookie in cookieTransformer.GetLeftSideResponseCookies(_rightSideResponse.Cookies, cookiesWithEmptyPath))
            {
                _leftSideResponse.Cookies.Add(cookie);
            }
        }