예제 #1
0
        public static IHttp2Headers ToHttp2Headers(HttpHeaders inHeaders, bool validateHeaders)
        {
            if (inHeaders.IsEmpty)
            {
                return(EmptyHttp2Headers.Instance);
            }

            var output = new DefaultHttp2Headers(validateHeaders, inHeaders.Size);

            ToHttp2Headers(inHeaders, output);
            return(output);
        }
예제 #2
0
        /// <summary>
        /// Converts the given HTTP/1.x headers into HTTP/2 headers.
        /// The following headers are only used if they can not be found in from the <c>HOST</c> header or the
        /// <c>Request-Line</c> as defined by <a href="https://tools.ietf.org/html/rfc7230">rfc7230</a>
        /// <para><see cref="ExtensionHeaderNames.Scheme"/></para>
        /// <see cref="ExtensionHeaderNames.Path"/> is ignored and instead extracted from the <c>Request-Line</c>.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="validateHeaders"></param>
        /// <returns></returns>
        public static IHttp2Headers ToHttp2Headers(IHttpMessage input, bool validateHeaders)
        {
            HttpHeaders   inHeaders = input.Headers;
            IHttp2Headers output    = new DefaultHttp2Headers(validateHeaders, inHeaders.Size);

            if (input is IHttpRequest request)
            {
                output.Method = request.Method.AsciiName;
                // TODO java.net.URI isOriginForm  isAsteriskForm
                if (Uri.TryCreate(request.Uri, UriKind.Absolute, out var requestTargetUri) && !requestTargetUri.IsFile) // .NETCore 非Windows系统,会把 '/' 打头的相对 url 默认为 UnixFileUri
                {
                    output.Path = ToHttp2Path(requestTargetUri);
                    SetHttp2Scheme(inHeaders, requestTargetUri, output);
                    _ = requestTargetUri.Authority;
                    // Attempt to take from HOST header before taking from the request-line
                    var host = inHeaders.GetAsString(HttpHeaderNames.Host);
                    SetHttp2Authority(string.IsNullOrEmpty(host) ? requestTargetUri.Authority : host, output);
                }
                else
                {
                    output.Path = new AsciiString(request.Uri);
                    // Consume the Scheme extension header if present
                    if (inHeaders.TryGet(ExtensionHeaderNames.Scheme, out var cValue))
                    {
                        output.Scheme = AsciiString.Of(cValue);
                    }
                    if (output.Scheme is null)
                    {
                        ThrowHelper.ThrowArgumentException_SchemeMustBeSpecified();
                    }
                    var host = inHeaders.GetAsString(HttpHeaderNames.Host);
                    SetHttp2Authority(host, output);
                }
            }
            else if (input is IHttpResponse response)
            {
                output.Status = response.Status.CodeAsText;
            }

            // Add the HTTP headers which have not been consumed above
            ToHttp2Headers(inHeaders, output);
            return(output);
        }