示例#1
0
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            MakoHttpOptions.UseHttpScheme(request);
            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

            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(
                       MakoHttpOptions.BypassRequiredHost.IsMatch(host) && MakoClient.Configuration.Bypass
                    ? typeof(PixivApiNameResolver)
                    : typeof(LocalMachineNameResolver)
                       ).SendAsync(request, cancellationToken));
        }
示例#2
0
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            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,