예제 #1
0
        public async Task <HttpResponseMessage> Post(string appKey, string sig)
        {
            if (HttpContext.Current.Request.InputStream.Length > 20000000)
            {
                return(await KillLargeReportAsync(appKey));
            }

            if (!_samplingCounter.CanAccept(appKey))
            {
                return(Request.CreateResponse(HttpStatusCode.OK));
            }


            try
            {
                var buffer = new byte[HttpContext.Current.Request.InputStream.Length];
                HttpContext.Current.Request.InputStream.Read(buffer, 0, buffer.Length);
                var handler = new SaveReportHandler(_queueProvider);
                await handler.BuildReportAsync(appKey, sig, Request.GetClientIpAddress(), buffer);

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception exception)
            {
                _logger.Error("Failed to handle request from " + appKey + " / " + Request.GetClientIpAddress(),
                              exception);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception));
            }
        }
예제 #2
0
        public async Task <IActionResult> Post(string appKey, string sig)
        {
            var contentLength = Request.ContentLength;

            if (contentLength > CompressedReportSizeLimit)
            {
                return(await KillLargeReportAsync(appKey));
            }
            if (contentLength == null || contentLength < 1)
            {
                return(BadRequest("Content required."));
            }

            try
            {
                var buffer    = new byte[contentLength.Value];
                var bytesRead = 0;
                while (bytesRead < contentLength.Value)
                {
                    bytesRead += await Request.Body.ReadAsync(buffer, bytesRead, buffer.Length - bytesRead);
                }

                var reportConfig = _configStore.Load <ReportConfig>();
                var config       = new ReportConfigWrapper(reportConfig);
                var handler      = new SaveReportHandler(_messageQueue, _unitOfWork, config);
                var principal    = CreateReporterPrincipal();
                var remoteIp     = Request.HttpContext.Connection.RemoteIpAddress.ToString();
                await handler.BuildReportAsync(principal, appKey, sig, remoteIp, buffer);

                return(NoContent());
            }
            catch (InvalidCredentialException)
            {
                return(BadRequest(new ErrorMessage("INVALID_APP_KEY")));
            }
            catch (HttpException ex)
            {
                _logger.InfoFormat(ex.Message);
                return(new ContentResult
                {
                    Content = ex.Message,
                    StatusCode = ex.HttpCode
                });
            }
            catch (Exception exception)
            {
                _logger.Error(
                    "Failed to handle request from " + appKey + " / " + Request.HttpContext.Connection.RemoteIpAddress,
                    exception);
                return(new ContentResult
                {
                    Content = exception.Message,
                    StatusCode = (int)HttpStatusCode.InternalServerError
                });
            }
        }
        public async Task <HttpResponseMessage> Post(string appKey, string sig)
        {
            if (HttpContext.Current.Request.InputStream.Length > 2000000)
            {
                return(await KillLargeReportAsync(appKey));
            }

            if (!_samplingCounter.CanAccept(appKey))
            {
                return(Request.CreateResponse(HttpStatusCode.OK));
            }


            try
            {
                var buffer = new byte[HttpContext.Current.Request.InputStream.Length];
                HttpContext.Current.Request.InputStream.Read(buffer, 0, buffer.Length);
                var handler   = new SaveReportHandler(_messageQueue, _unitOfWork, _configStore);
                var principal = CreateReporterPrincipal();
                await handler.BuildReportAsync(principal, appKey, sig, Request.GetClientIpAddress(), buffer);

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (InvalidCredentialException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "INVALID_APP_KEY", ex));
            }
            catch (HttpException ex)
            {
                _logger.InfoFormat(ex.Message);
                return(Request.CreateErrorResponse((HttpStatusCode)ex.GetHttpCode(), ex.Message));
            }
            catch (Exception exception)
            {
                _logger.Error("Failed to handle request from " + appKey + " / " + Request.GetClientIpAddress(),
                              exception);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception));
            }
        }
        public async Task <IActionResult> Post(string appKey, string sig)
        {
            var contentLength = Request.ContentLength;

            if (contentLength > CompressedReportSizeLimit)
            {
                return(await KillLargeReportAsync(appKey));
            }
            if (contentLength == null || contentLength < 1)
            {
                return(BadRequest("Content required."));
            }
            var remoteIp = Request.HttpContext.Connection.RemoteIpAddress;

            // Sig may be null for web applications
            // as I don't know how to protect the secretKey in web applications
            if (sig == null)
            {
                if (!await _whitelistService.Validate(appKey, remoteIp))
                {
                    return(BadRequest("Must sign error report with the sharedSecret"));
                }
            }

            try
            {
                var buffer    = new byte[contentLength.Value];
                var bytesRead = 0;
                while (bytesRead < contentLength.Value)
                {
                    bytesRead += await Request.Body.ReadAsync(buffer, bytesRead, buffer.Length - bytesRead);
                }

                var reportConfig = _configStore.Load <ReportConfig>();
                var config       = new ReportConfigWrapper(reportConfig);
                var handler      = new SaveReportHandler(_messageQueue, _unitOfWork, config);
                var principal    = CreateReporterPrincipal();

                await handler.BuildReportAsync(principal, appKey, sig, remoteIp.ToString(), buffer);

                return(NoContent());
            }
            catch (InvalidCredentialException)
            {
                return(BadRequest(new ErrorMessage("INVALID_APP_KEY")));
            }
            catch (HttpException ex)
            {
                _logger.InfoFormat(ex.Message);
                return(new ContentResult
                {
                    Content = ex.Message,
                    StatusCode = ex.HttpCode,
                    ContentType = "text/plain"
                });
            }
            catch (Exception exception)
            {
                _logger.Error(
                    "Failed to handle request from " + appKey + " / " + Request.HttpContext.Connection.RemoteIpAddress,
                    exception);
                return(new ContentResult
                {
                    Content = exception.Message,
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    ContentType = "text/plain"
                });
            }
        }