コード例 #1
0
        public async Task Invoke(DownstreamContext context)
        {
            var response = await _requester.GetResponse(context);

            if (response.IsError)
            {
                Logger.LogDebug("IHttpRequester returned an error, setting pipeline error");
                SetPipelineError(context, response.Errors);
                return;
            }
            else if ((int)response.Data.StatusCode >= (int)HttpStatusCode.BadRequest)//如果后端未处理异常,设置异常信息,统一输出,防止暴露敏感信息
            {
                var path = context.HttpContext.Request.Path.Value;
                if (IsFile(path))
                {
                    context.DownstreamResponse = new DownstreamResponse(response.Data);
                    return;
                }
                var result = await response.Data.Content.ReadAsStringAsync();

                SetPipelineError(context, GetError(response.Data.StatusCode, result, path));
                return;
            }
            context.DownstreamResponse = new DownstreamResponse(response.Data);
        }
コード例 #2
0
        public async Task Invoke(DownstreamContext context)
        {
            var response = await _requester.GetResponse(context);

            if (response.IsError)
            {
                Logger.LogDebug("IHttpRequester returned an error, setting pipeline error");

                SetPipelineError(context, response.Errors);
                return;
            }
            else if (response.Data.StatusCode != System.Net.HttpStatusCode.OK)
            {     //如果后端未处理异常,设置异常信息,统一输出,防止暴露敏感信息
                if (response.Data.StatusCode == System.Net.HttpStatusCode.BadRequest)
                { //提取Ids4相关的异常(400)
                    var result = await response.Data.Content.ReadAsStringAsync();

                    JObject jobj     = JObject.Parse(result);
                    var     errorMsg = jobj["error"]?.ToString();
                    var     error    = new IdentityServer4Error(errorMsg ?? "未知异常");
                    SetPipelineError(context, error);
                    return;
                }
                else
                {
                    var error = new InternalServerError($"请求服务异常");
                    Logger.LogWarning($"路由地址 {context.HttpContext.Request.Path} 请求服务异常. {error}");
                    SetPipelineError(context, error);
                    return;
                }
            }
            Logger.LogDebug("setting http response message");

            context.DownstreamResponse = new DownstreamResponse(response.Data);
        }
コード例 #3
0
        public async Task Invoke(HttpContext context,
                                 DiscoveryOptions discoveryOptions,
                                 INodeDiscoveryProvider discoveryProvider)
        {
            _discoveryProvider = discoveryProvider;

            var request      = context.Request;
            var pathMatch    = discoveryOptions.MatchPath;
            var isCapRequest = request.Path.StartsWithSegments(new PathString(pathMatch));

            if (!isCapRequest)
            {
                await _next.Invoke(context);
            }
            else
            {
                //For performance reasons, we need to put this functionality in the else
                var isSwitchNode  = request.Cookies.TryGetValue(NodeCookieName, out var requestNodeId);
                var isCurrentNode = discoveryOptions.NodeId.ToString() == requestNodeId;
                var isNodesPage   = request.Path.StartsWithSegments(new PathString(pathMatch + "/nodes"));

                if (!isSwitchNode || isCurrentNode || isNodesPage)
                {
                    await _next.Invoke(context);
                }
                else
                {
                    _logger.LogDebug("started calling gateway proxy middleware");

                    if (TryGetRemoteNode(requestNodeId, out var node))
                    {
                        try
                        {
                            DownstreamRequest = await _requestMapper.Map(request);

                            SetDownStreamRequestUri(node, request.Path.Value);

                            var response = await _requester.GetResponse(DownstreamRequest);

                            await SetResponseOnHttpContext(context, response);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError(ex.Message);
                        }
                    }
                    else
                    {
                        context.Response.Cookies.Delete(NodeCookieName);
                        await _next.Invoke(context);
                    }
                }
            }
        }
コード例 #4
0
        public async Task Invoke(DownstreamContext context)
        {
            var response = await _requester.GetResponse(context);

            if (response.IsError)
            {
                Logger.LogDebug("IHttpRequester returned an error, setting pipeline error");

                SetPipelineError(context, response.Errors);
                return;
            }

            Logger.LogDebug("setting http response message");

            context.DownstreamResponse = response.Data;
        }
コード例 #5
0
        public async Task Invoke(HttpContext context)
        {
            var response = await _requester.GetResponse(Request);

            if (response.IsError)
            {
                _logger.LogDebug("IHttpRequester returned an error, setting pipeline error");

                SetPipelineError(response.Errors);
                return;
            }

            _logger.LogDebug("setting http response message");

            SetHttpResponseMessageThisRequest(response.Data);
        }
コード例 #6
0
        private static void GetUserById(IHttpRequester httpRequester)
        {
            var getUserByIdRequest = WebRequest.Create("https://localhost/api/users/128");

            getUserByIdRequest.Method = "GET";
            var getUserByIdResponse = httpRequester.GetResponse(getUserByIdRequest);

            using (var stream = new StreamReader(getUserByIdResponse.GetResponseStream()))
            {
                var serializer = new JsonSerializer();
                var user       = (User)serializer.Deserialize(stream, typeof(User));

                Console.WriteLine("Data from GET https://localhost/api/users/128");
                LogUser(user);
                LogCookiesAndHeaders(getUserByIdResponse);
            }
        }
コード例 #7
0
        private static void PostUser(IHttpRequester httpRequester)
        {
            //Post user
            var postUserRequest = WebRequest.Create("https://localhost/api/users");

            postUserRequest.Method = "POST";
            var postUserResponse = httpRequester.GetResponse(postUserRequest);

            using (var stream = new StreamReader(postUserResponse.GetResponseStream()))
            {
                var serializer = new JsonSerializer();
                var user       = (User)serializer.Deserialize(stream, typeof(User));

                Console.WriteLine("Data from POST https://localhost/api/users : ");
                LogUser(user);
                LogCookiesAndHeaders(postUserResponse);
            }
        }
コード例 #8
0
        private static void SearchUser(IHttpRequester httpRequester)
        {
            var searchRequest = WebRequest.Create("https://localhost/api/users?pIndex=0&pSize=2");

            searchRequest.Method = "GET";
            var searchResponse = httpRequester.GetResponse(searchRequest);

            using (var stream = new StreamReader(searchResponse.GetResponseStream()))
            {
                var serializer  = new JsonSerializer();
                var searchUsers = (SearchUsers)serializer.Deserialize(stream, typeof(SearchUsers));
                Console.WriteLine("Data from GET https://localhost/api/users?pIndex=0&pSize=2");

                foreach (var user in searchUsers.Results)
                {
                    LogUser(user);
                }

                LogCookiesAndHeaders(searchResponse);
            }
        }
コード例 #9
0
        public async Task Invoke(HttpContext httpContext)
        {
            var downstreamRoute = httpContext.Items.DownstreamRoute();

            var response = await _requester.GetResponse(httpContext);

            CreateLogBasedOnResponse(response);

            if (response.IsError)
            {
                Logger.LogDebug("IHttpRequester returned an error, setting pipeline error");

                httpContext.Items.UpsertErrors(response.Errors);
                return;
            }

            Logger.LogDebug("setting http response message");

            httpContext.Items.UpsertDownstreamResponse(new DownstreamResponse(response.Data));

            await _next.Invoke(httpContext);
        }
コード例 #10
0
 public static Response <HttpResponseMessage> GetResponse(Request.Request request)
 {
     return(_requester.GetResponse(request).Result);
 }