public void Limiter_ResponseLength() { using (GetResetResponseLimiterSwindler()) { Test((builder) => { builder.UseResponseLimiter(10, 10); }, () => { var responses = new string[3]; for (int i = 0; i < 3; i++) { try { ResponseLimiter.AssertResponseLength(i + 9); responses[i] = "Ok."; } catch (ApplicationException e) { responses[i] = e.Message; } } var actual = string.Join(" ", responses); Assert.AreEqual("Ok. Ok. Response length limit exceeded.", actual); } ); } }
public void Limiter_ResponseLength_AssertMore() { using (GetResetResponseLimiterSwindler()) { Test((builder) => { builder.UseResponseLimiter(29, 10); }, () => { var responses = new string[4]; var httpContext = CreateHttpContext(); var response = httpContext.Response; try { ResponseLimiter.AssertResponseLength(response, 10); responses[0] = ResponseLimiter.GetCurrentResponseLength(response).ToString(); ResponseLimiter.AssertResponseLength(response, 10); responses[1] = ResponseLimiter.GetCurrentResponseLength(response).ToString(); ResponseLimiter.AssertResponseLength(httpContext.Response, 10); Assert.Fail("ApplicationException was not thrown"); } catch (ApplicationException e) { responses[2] = ResponseLimiter.GetCurrentResponseLength(response).ToString(); responses[3] = e.Message; } var actual = string.Join(" ", responses); Assert.AreEqual("10 20 20 Response length limit exceeded.", actual); } ); } }
/// <summary> /// Writes an object to the webresponse. /// </summary> /// <param name="response">The object that will be written.</param> /// <param name="httpContext">The current <see cref="HttpContext"/> instance containing the current web-response.</param> protected virtual async Task WriteRawAsync(object response, HttpContext httpContext) { var text = response.ToString(); ResponseLimiter.AssertResponseLength(httpContext.Response, text.Length); await httpContext.Response.WriteAsync(text, httpContext.RequestAborted) .ConfigureAwait(false); }
internal async Task ProcessRequestAsync(HttpContext context, bool calledFromTest) { var wopiRequest = WopiRequest.Parse(context); // set current user based on the access token if (!calledFromTest) { var user = await GetCurrentUserAsync(wopiRequest, context.RequestAborted).ConfigureAwait(false); if (user != null) { User.Current = user; } } var webResponse = context.Response; var wopiResponse = await GetResponseAsync(wopiRequest, context.RequestAborted).ConfigureAwait(false); // Set content type if it is known. if (!string.IsNullOrEmpty(wopiResponse.ContentType)) { webResponse.ContentType = wopiResponse.ContentType; } // Set response headers. foreach (var item in wopiResponse.Headers) { webResponse.Headers.Add(item.Key, item.Value); } // Set HTTP Status code. webResponse.StatusCode = (int)wopiResponse.StatusCode; // Write binary content if (wopiResponse is IWopiBinaryResponse wopiBinaryResponse) { //TODO: provide custom binary field name if available var binaryHandler = new BinaryHandler(context, wopiBinaryResponse.File); await binaryHandler.ProcessRequestCore().ConfigureAwait(false); return; } // Write JSON body if (wopiResponse is IWopiObjectResponse) { var settings = new JsonSerializerSettings { Formatting = Formatting.Indented }; var output = JsonConvert.SerializeObject(wopiResponse, settings); var buffer = Encoding.UTF8.GetBytes(output); ResponseLimiter.AssertResponseLength(webResponse, buffer.Length); await webResponse.Body.WriteAsync(buffer, 0, buffer.Length) .ConfigureAwait(false); } }
/// <summary> /// Writes an object to the webresponse. /// </summary> /// <param name="response">The object that will be written.</param> /// <param name="odataRequest">The OData request.</param> /// <param name="httpContext">The current <see cref="HttpContext"/> instance containing the current web-response.</param> protected virtual async Task WriteRawAsync(object response, HttpContext httpContext, ODataRequest odataRequest) { var text = response.ToString(); if (odataRequest != null) { odataRequest.ResponseSize = text.Length; } ResponseLimiter.AssertResponseLength(httpContext.Response, text.Length); await httpContext.Response.WriteAsync(text, httpContext.RequestAborted) .ConfigureAwait(false); }
protected async Task WriteAsync(object response, HttpContext httpContext, ODataRequest odataRequest) { var resp = httpContext.Response; switch (response) { case null: resp.StatusCode = 204; return; case string _: await WriteRawAsync(response, httpContext, odataRequest).ConfigureAwait(false); return; } var settings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat, Formatting = Formatting.Indented, Converters = ODataMiddleware.JsonConverters }; var serializer = JsonSerializer.Create(settings); string text; using (var writer = new StringWriter()) { serializer.Serialize(writer, response); text = writer.GetStringBuilder().ToString(); } odataRequest.ResponseSize = text.Length; ResponseLimiter.AssertResponseLength(resp, text.Length); resp.ContentType = "application/json;odata=verbose;charset=utf-8"; await resp.WriteAsync(text).ConfigureAwait(false); }