Ejemplo n.º 1
0
        private static HttpResponseEntity ToHttpResponseEntity(HttpRequestException exception)
        {
            WebException webexception = exception.InnerException as WebException;

            /**********************************************************************
            * Response Message
            **********************************************************************/
            HttpResponseEntity result = new HttpResponseEntity()
            {
                ReasonPhrase = webexception.Status.ToString()
            };

            if (webexception.Response == null)
            {
                throw new HttpException(HttpUtilExceptionType.Dns);
            }

            result.ContentLength = webexception.Response.ContentLength;
            result.ContentType   = webexception.Response.ContentType;
            result.Headers       = new Dictionary <string, IEnumerable <string> >();
            foreach (string key in webexception.Response.Headers.AllKeys)
            {
                IEnumerable <string> value = new List <string>(1)
                {
                    webexception.Response.Headers[key]
                };
                result.Headers.Add(key, value);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <UserClaimDTO> AcquireUserClaim(string identityServerIp, string token)
        {
            HttpRequestEntity httpRequestEntity = new HttpRequestEntity()
            {
                Method = Enums.HttpMethod.GET
            };
            HttpResponseEntity responseEntity = HttpHelper.Get(identityServerIp + "/connect/userinfo", httpRequestEntity, token);

            return(await Task.FromResult(JsonHelper.Deserialize <UserClaimDTO>(responseEntity.Bodys.ToString())));
        }
Ejemplo n.º 3
0
        private static async Task <HttpResponseEntity> ToHttpResponseEntity(HttpResponseMessage response, string content)
        {
            /**********************************************************************
            * Response Message
            **********************************************************************/
            HttpResponseEntity result = new HttpResponseEntity()
            {
                Version             = response.Version.ToString(),
                StatusCode          = (int)response.StatusCode,
                ReasonPhrase        = response.ReasonPhrase,
                IsSuccessStatusCode = response.IsSuccessStatusCode,
            };

            result.Headers = new Dictionary <string, IEnumerable <string> >();
            foreach (var row in response.Headers)
            {
                result.Headers.Add(row.Key, row.Value);
            }

            using (Stream responseStream = await response.Content.ReadAsStreamAsync())
            {
                Encoding charset = Encoding.GetEncoding(response.Content.Headers.ContentType.CharSet);
                result.Content       = new StreamReader(responseStream, charset).ReadToEnd();
                result.ContentType   = response.Content.Headers.ContentType.ToString();
                result.ContentLength = (response.Content.Headers.ContentLength != null) ? (long)response.Content.Headers.ContentLength : 0;
            }

            /**********************************************************************
            * Request Message
            **********************************************************************/
            result.RequestMessage = new HttpRequestEntity()
            {
                Method     = response.RequestMessage.Method.ToString(),
                Version    = response.RequestMessage.Version.ToString(),
                Properties = response.RequestMessage.Properties,
                RequestUri = response.RequestMessage.RequestUri,
            };

            result.RequestMessage.Headers = new Dictionary <string, IEnumerable <string> >();
            foreach (var row in response.RequestMessage.Headers)
            {
                result.RequestMessage.Headers.Add(row.Key, row.Value);
            }

            if (response.RequestMessage.Content != null)
            {
                result.RequestMessage.Content       = content;
                result.RequestMessage.ContentType   = response.RequestMessage.Content.Headers.ContentType.ToString();
                result.RequestMessage.ContentLength = (response.RequestMessage.Content.Headers.ContentLength != null) ? (long)response.RequestMessage.Content.Headers.ContentLength : 0;
            }

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Creates a Stump data entity from a Stump contract.
        /// </summary>
        /// <param name="contract">The <see cref="StumpContract"/> used to create the entity.</param>
        /// <returns>
        ///     A <see cref="StumpEntity"/> created from the specified <paramref name="contract"/>.
        /// </returns>
        public static StumpEntity CreateEntityFromContract(StumpContract contract)
        {
            var originalRequest = new HttpRequestEntity
            {
                BodyResourceName = string.Empty,
                Headers          = CreateNameValuePairFromHeaders(contract.OriginalRequest.Headers),
                HttpMethod       = contract.OriginalRequest.HttpMethod,
                LocalEndPoint    = contract.OriginalRequest.LocalEndPoint.ToString(),
                ProtocolVersion  = contract.OriginalRequest.ProtocolVersion,
                RawUrl           = contract.OriginalRequest.RawUrl,
                RemoteEndPoint   = contract.OriginalRequest.RemoteEndPoint.ToString()
            };

            var originalResponse = new HttpResponseEntity
            {
                BodyResourceName    = string.Empty,
                Headers             = CreateNameValuePairFromHeaders(contract.OriginalResponse.Headers),
                RedirectAddress     = contract.OriginalResponse.RedirectAddress,
                ResponseDelay       = contract.OriginalResponse.ResponseDelay,
                StatusCode          = contract.OriginalResponse.StatusCode,
                StatusDescription   = contract.OriginalResponse.StatusDescription,
                TerminateConnection = contract.OriginalResponse.TerminateConnection
            };

            var response = new HttpResponseEntity
            {
                BodyResourceName    = string.Empty,
                Headers             = CreateNameValuePairFromHeaders(contract.Response.Headers),
                RedirectAddress     = contract.Response.RedirectAddress,
                ResponseDelay       = contract.Response.ResponseDelay,
                StatusCode          = contract.Response.StatusCode,
                StatusDescription   = contract.Response.StatusDescription,
                TerminateConnection = contract.Response.TerminateConnection
            };

            var entity = new StumpEntity
            {
                OriginalRequest  = originalRequest,
                OriginalResponse = originalResponse,
                Response         = response,
                Rules            = new List <RuleEntity>(),
                StumpCategory    = contract.StumpCategory,
                StumpId          = contract.StumpId,
                StumpName        = contract.StumpName,
            };

            foreach (var rule in contract.Rules)
            {
                var ruleEntity = new RuleEntity
                {
                    RuleName = rule.RuleName,
                    Settings = new List <NameValuePairEntity>()
                };

                var settings = rule.GetRuleSettings();
                foreach (var setting in settings)
                {
                    ruleEntity.Settings.Add(
                        new NameValuePairEntity
                    {
                        Name  = setting.Name,
                        Value = setting.Value
                    });
                }

                entity.Rules.Add(ruleEntity);
            }

            return(entity);
        }