protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var headers = request.Headers;
            var host    = request.RequestUri !.Host; // the 'RequestUri' is guaranteed to be nonnull here, because the 'HttpClient' will set it to 'BaseAddress' if its null

            var bypass = MakoHttpOptions.BypassRequiredHost.IsMatch(host) && MakoClient.Configuration.Bypass || host == MakoHttpOptions.OAuthHost;

            if (bypass)
            {
                MakoHttpOptions.UseHttpScheme(request);
            }

            headers.TryAddWithoutValidation("Accept-Language", MakoClient.Configuration.CultureInfo.Name);

            var session = MakoClient.Session;

            switch (host)
            {
            case MakoHttpOptions.WebApiHost:
                headers.TryAddWithoutValidation("Cookie", session.Cookie);
                break;

            case MakoHttpOptions.AppApiHost:
                headers.Authorization = new AuthenticationHeaderValue("Bearer", session.AccessToken);
                break;
            }

            return(MakoClient.GetHttpMessageInvoker(bypass
                ? typeof(PixivApiNameResolver)
                : typeof(LocalMachineNameResolver)).SendAsync(request, cancellationToken));
        }
Ejemplo n.º 2
0
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (MakoClient.Configuration.Bypass)
            {
                MakoHttpOptions.UseHttpScheme(request);
            }
            var requestUri = request.RequestUri !;

            if (requestUri.Host == MakoHttpOptions.ImageHost && MakoClient.Configuration.MirrorHost is { } mirror&& mirror.IsNotNullOrBlank())
            {
                request.RequestUri = mirror switch
                {
                    _ when Uri.CheckHostName(mirror) is not UriHostNameType.Unknown => new UriBuilder(requestUri)
                    {
                        Host = mirror
                    }.Uri,
                      _ when Uri.IsWellFormedUriString(mirror, UriKind.Absolute) => new Uri(mirror).Let(mirrorUri => new UriBuilder(requestUri)
                    {
                        Host = mirrorUri !.Host, Scheme = mirrorUri.Scheme
                    }) !.Uri,