Exemplo n.º 1
0
        // no me gusta
        // smell?
        public HttpResponseMessage Execute(HttpRequestMessage request, string[] urlSegments)
        {
            var apiLog = new CreateApiLogItem
            {
                SessionId = _apiTransactionUtility.ExtractSessionId(request.RequestUri),
                ApiRequest = _apiTransactionUtility.BuildApiRequest(request)
            };

            // HttpClient and WebRequest objs both bark when GET w/ body content
            if (request.Method.Method.ToUpper() == "GET")
                request.Content = null;

            // reset the request URI to the decoded path
            var uri = _apiTransactionUtility.BuildDestinationUri(request.RequestUri);
            request.RequestUri = uri;

            using (var client = new HttpClient())
            {
                ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

                var response = client.SendAsync(request).Result;
                apiLog.ApiResponse = _apiTransactionUtility.BuildApiResponse(response);

                // why we have to reset this? (comes in as localhost if not)
                apiLog.ApiResponse.UriAccessed = uri.OriginalString;
                apiLog.ApiResponse.IsSuccessStatusCode = response.IsSuccessStatusCode;

                _commandHandler.Handle(apiLog); // async could be nice

                return response;
            }
        }
Exemplo n.º 2
0
        // no me gusta
        // smell?
        public HttpResponseMessage Execute(HttpRequestMessage request, string[] urlSegments)
        {
            var apiLog = new CreateApiLogItem
            {
                Id = CombGuid.Generate(),
                SessionId = _apiTransactionUtility.ExtractSessionId(request.RequestUri),
                ApiRequest = _apiTransactionUtility.BuildApiRequest(request)
            };

            // HttpClient and WebRequest objs both bark when GET w/ body content
            if (request.Method.Method.ToUpper() == "GET")
                request.Content = null;

            // reset the request URI to the decoded path
            Uri uri;
            try
            {
                uri = _apiTransactionUtility.BuildDestinationUri(request.RequestUri);
            }
            catch (Exception ex)
            {
                //CannotParseUriException may be a good candidate for a 400 responce: "The request cannot be fulfilled due to bad syntax."
                if (ex is CannotParseUriException || ex is ConfigurationErrorsException)
                    return new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = ex.Message };
                throw;
            }

            request.RequestUri = uri;

            using (var client = new HttpClient())
            {
                ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

                var response = client.SendAsync(request).Result;
                apiLog.ApiResponse = _apiTransactionUtility.BuildApiResponse(response);

                // why we have to reset this? (comes in as localhost if not)
                apiLog.ApiResponse.UriAccessed = uri.OriginalString;
                apiLog.ApiResponse.IsSuccessStatusCode = response.IsSuccessStatusCode;

                _commandHandler.Handle(apiLog); // async could be nice

                return response;
            }
        }
Exemplo n.º 3
0
        private void LogRequestLoggingInfo(HttpRequestMessage request)
        {
            var sessionId = ExtractSessionId(request.RequestUri);

            var info = new CreateApiLogItem
            {
                SessionId = sessionId,
                MessageType = HttpMessageType.Request,
                HttpMethod = request.Method.Method,
                UriAccessed = request.RequestUri.AbsoluteUri,
                IpAddress = HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "0.0.0.0",
                Headers = ExtractHeaders(request.Headers.ToList()),

                BodyContent = ExtractBody(request.Content)
            };

            _commandHandler.Handle(info);
        }
Exemplo n.º 4
0
        private void LogResponseLoggingInfo(HttpResponseMessage response, string sessionId)
        {
            var info = new CreateApiLogItem
            {
                SessionId = sessionId,
                MessageType = HttpMessageType.Response,
                HttpMethod = response.RequestMessage.Method.ToString(),
                UriAccessed = response.RequestMessage.RequestUri.AbsoluteUri,
                IpAddress = HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "0.0.0.0",
                Headers = ExtractHeaders(response.Headers.ToList()),

                BodyContent = ExtractBody(response.Content),

                ResponseStatusCode = response.StatusCode,
                ResponseStatusMessage = response.ReasonPhrase
            };

            _commandHandler.Handle(info);
        }