PipelineContinuation RewriteAppBase(ICommunicationContext ctx)
        {
            if (!ctx.Request.Headers.TryGetValue("forwarded", out var headerValue))
            {
                return(PipelineContinuation.Continue);
            }

            var header = ForwardedHeader.Parse(headerValue).FirstOrDefault();

            if (header == null)
            {
                return(PipelineContinuation.Continue);
            }
            var appBaseUri = new UriBuilder(ctx.Request.Uri);

            if (header.TryGetValue("host", out var host))
            {
                appBaseUri.Host = host;
            }

            var hasPort = header.TryGetValue("port", out var sport);

            if (hasPort)
            {
                if (int.TryParse(sport, out var port))
                {
                    appBaseUri.Port = port;
                }
            }

            if (header.TryGetValue("proto", out var proto))
            {
                appBaseUri.Scheme = proto;
                if (!hasPort)
                {
                    appBaseUri.Port = -1;
                }
            }

            if (header.TryGetValue("base", out var baseUri) && appBaseUri.Path.StartsWith(baseUri))
            {
                appBaseUri.Path = appBaseUri.Path.Substring(baseUri.Length);
            }

            ctx.Request.Uri = appBaseUri.Uri;

            return(PipelineContinuation.Continue);
        }
Example #2
0
        bool TryReadHeader(out ForwardedHeader header)
        {
            header = null;
            while (TryReadPair(out var pair))
            {
                if (header == null)
                {
                    header = new ForwardedHeader();
                }
                header[pair.key] = pair.value;
                if (TryPeekChar(';'))
                {
                    _pos++;
                }
                else
                {
                    break;
                }
            }

            return(header != null);
        }