private bool InspectStatusCode(HttpContext context)
 {
     StatusCodeAction action;
     int statusCode = context.Response.StatusCode;
     if (options.StatusCodeActions.TryGetValue(statusCode, out action)
         && action == StatusCodeAction.ReplaceResponse)
     {
         context.GetFeature<BackupStreamFeature>().Replace = true;
         return action == StatusCodeAction.ReplaceResponse;
     }
     context.GetFeature<BackupStreamFeature>().Replace = false;
     return false;
 }
        public Task Invoke(HttpContext context)
        {
            // Detect if an opaque upgrade is available. If so, add a websocket upgrade.
            var upgradeFeature = context.GetFeature<IHttpUpgradeFeature>();
            if (upgradeFeature != null)
            {
                if (_options.ReplaceFeature || context.GetFeature<IHttpWebSocketFeature>() == null)
                {
                    context.SetFeature<IHttpWebSocketFeature>(new UpgradeHandshake(context, upgradeFeature, _options));
                }
            }

            return _next(context);
        }
        public Task Invoke(HttpContext context)
        {
            // Check if there is a SendFile feature already present
            if (context.GetFeature<IHttpSendFileFeature>() == null)
            {
                context.SetFeature<IHttpSendFileFeature>(new SendFileWrapper(context.Response.Body, _logger));
            }

            return _next(context);
        }
 private Task SendImageLink(HttpContext context, string link)
 {
     context.Response.Body = context.GetFeature<BackupStreamFeature>().Body;
     context.Response.ContentType = "text/html";
     string body = string.Format("<html><body><img src=\"{0}\" /></body></html>",
         WebUtility.HtmlEncode(link));
     body += new string(' ', 512); // Padding to bypass IE's friendly error pages.
     context.Response.ContentLength = body.Length;
     return context.Response.WriteAsync(body);
 }
        public async Task Invoke(HttpContext context)
        {
            var environment = (IApplicationEnvironment)context.RequestServices.GetService(typeof(IApplicationEnvironment));

            if (context.GetFeature<IHttpSendFileFeature>() == null)
            {
                var sendFile = new SendFileFallBack(context.Response.Body, environment.ApplicationBasePath);
                context.SetFeature<IHttpSendFileFeature>(sendFile);
            }

            await _next(context);
        }
Beispiel #6
0
        public async Task Invoke(HttpContext context)
        {
            var errorFeature = context.GetFeature<IErrorHandlerFeature>();

            if(errorFeature.Error != null)
            {
                _logger.LogCritical("A critical exception occurred.", errorFeature.Error);
            } else
            {
                _logger.LogCritical("There was an error...");
            }

            //IErrorHandlerFeature
            context.Response.StatusCode = 200;
            
            // we're going to "short-circuit" the pipeline
            await context.Response.WriteAsync("There was an error . But it was logged.");            
        }
        private RequestIdentifier(HttpContext context)
        {
            _context = context;
            _feature = context.GetFeature<IHttpRequestIdentifierFeature>();

            if (_feature == null)
            {
                _feature = new HttpRequestIdentifierFeature()
                {
                    TraceIdentifier = Guid.NewGuid().ToString()
                };
                context.SetFeature(_feature);
                _addedFeature = true;
            }
            else if (string.IsNullOrEmpty(_feature.TraceIdentifier))
            {
                _originalIdentifierValue = _feature.TraceIdentifier;
                _feature.TraceIdentifier = Guid.NewGuid().ToString();
                _updatedIdentifier = true;
            }
        }
 public async Task Invoke(HttpContext context)
 {
     context.SetFeature<BackupStreamFeature>(new BackupStreamFeature() { Body = context.Response.Body });
     context.Response.Body = new StreamWrapper(context.Response.Body, InspectStatusCode, context);
     await _next.Invoke(context);
     
     StatusCodeAction action;
     int statusCode = context.Response.StatusCode;
     bool? replace = context.GetFeature<BackupStreamFeature>().Replace;
     if (!replace.HasValue)
     {
         // Never evaluated, no response sent yet.
         if (options.StatusCodeActions.TryGetValue(statusCode, out action)
             && action != StatusCodeAction.Ignore)
         {
             await options.ResponseGenerator(context);
         }
     }
     else if (replace.Value == true)
     {
         await options.ResponseGenerator(context);
     }
 }
Beispiel #9
0
        private string GetRequestIdentifier(HttpContext httpContext)
        {
            var requestIdentifierFeature = httpContext.GetFeature<IHttpRequestIdentifierFeature>();
            if (requestIdentifierFeature == null)
            {
                requestIdentifierFeature = new HttpRequestIdentifierFeature()
                {
                    TraceIdentifier = Guid.NewGuid().ToString()
                };
                httpContext.SetFeature(requestIdentifierFeature);
            }

            return requestIdentifierFeature.TraceIdentifier;
        }
 public async Task Invoke(HttpContext context)
 {
     var feature = context.GetFeature<IErrorHandlerFeature>();
     _logger.LogError("Error in Application", feature.Error);
     await WriteErrorResponseAsync(context, feature.Error);
 }
 private static bool IsSessionEnabled(HttpContext context)
 {
     return context.GetFeature<ISessionFeature>() != null;
 }
        private Task ProcessNegotiationRequest(HttpContext context)
        {
            // Total amount of time without a keep alive before the client should attempt to reconnect in seconds.
            var keepAliveTimeout = _options.Transports.KeepAliveTimeout();
            string connectionId = Guid.NewGuid().ToString("d");
            string connectionToken = connectionId + ':' + GetUserIdentity(context);

            var payload = new
            {
                Url = context.Request.LocalPath().Replace("/negotiate", ""),
                ConnectionToken = ProtectedData.Protect(connectionToken, Purposes.ConnectionToken),
                ConnectionId = connectionId,
                KeepAliveTimeout = keepAliveTimeout != null ? keepAliveTimeout.Value.TotalSeconds : (double?)null,
                DisconnectTimeout = _options.Transports.DisconnectTimeout.TotalSeconds,
                ConnectionTimeout = _options.Transports.LongPolling.PollTimeout.TotalSeconds,
                // TODO: Supports websockets
                TryWebSockets = _transportManager.SupportsTransport(WebSocketsTransportName) && context.GetFeature<IHttpWebSocketFeature>() != null,
                ProtocolVersion = _protocolResolver.Resolve(context.Request).ToString(),
                TransportConnectTimeout = _options.Transports.TransportConnectTimeout.TotalSeconds,
                LongPollDelay = _options.Transports.LongPolling.PollDelay.TotalSeconds
            };

            return SendJsonResponse(context, JsonSerializer.Stringify(payload));
        }
        protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
        {
            if (!string.IsNullOrEmpty(telemetry.Context.Location.Ip))
            {
                //already populated
                return;
            }

            if (string.IsNullOrEmpty(requestTelemetry.Context.Location.Ip))
            {
                string resultIp = null;
                foreach (var name in this.HeaderNames)
                {
                    var headerValue = platformContext.Request.Headers[name];
                    if (!string.IsNullOrEmpty(headerValue))
                    {
                        var ip = GetIpFromHeader(headerValue);
                        ip = CutPort(ip);
                        if (IsCorrectIpAddress(ip))
                        {
                            resultIp = ip;
                            break;
                        }
                    }
                }

                if (string.IsNullOrEmpty(resultIp))
                {
                    var connectionFeature = platformContext.GetFeature<IHttpConnectionFeature>();

                    if (connectionFeature != null)
                    {
                        resultIp = connectionFeature.RemoteIpAddress.ToString();
                    }
                }

                requestTelemetry.Context.Location.Ip = resultIp;
            }
            telemetry.Context.Location.Ip = requestTelemetry.Context.Location.Ip;
        }