Exemplo n.º 1
0
 public void Limiter_FileLength_Upgrade()
 {
     using (GetResetResponseLimiterSwindler())
     {
         Test((builder) => { builder.UseResponseLimiter(10, 10); }, () =>
         {
             ResponseLimiter.ModifyFileLengthLimit(20);
             var responses = new string[3];
             for (int i = 0; i < 3; i++)
             {
                 try
                 {
                     ResponseLimiter.AssertFileLength(i + 19);
                     responses[i] = "Ok.";
                 }
                 catch (ApplicationException e)
                 {
                     responses[i] = e.Message;
                 }
             }
             var actual = string.Join(" ", responses);
             Assert.AreEqual("Ok. Ok. File length limit exceeded.", actual);
         }
              );
     }
 }
Exemplo n.º 2
0
        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);
                }
                     );
            }
        }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        public async Task ProcessRequestCore()
        {
            if (!CheckPermissions())
            {
                _context.Response.StatusCode = 404;
                return;
            }

            var httpHeaderTools = new HttpHeaderTools(_context);
            var endResponse     = HandleResponseForClientCache(httpHeaderTools);

            if (endResponse)
            {
                return;
            }

            //TODO: CheckExecutableType feature is not ported yet from .net framework Services
            // It was designed to handle executable files like aspx or cshtml.
            // See the Repository.ExecutableExtensions property for the full list.

            //TODO: the ImgResizeApplication feature is not ported yet from .net framework Services

            await InitializeRequestedNodeAsync();

            if (RequestedNode == null)
            {
                _context.Response.StatusCode = 404;
                return;
            }

            using (var binaryStream = GetConvertedStream(out var contentType, out var fileName))
            {
                if (binaryStream == null)
                {
                    return;
                }

                _context.Response.ContentType = contentType;
                _context.Response.Headers.Append(HeaderNames.ContentLength, binaryStream.Length.ToString());

                httpHeaderTools.SetContentDispositionHeader(fileName);
                httpHeaderTools.SetCacheControlHeaders(lastModified: RequestedNode.ModificationDate, maxAge: MaxAge);

                _context.Response.StatusCode = 200;

                binaryStream.Position = 0;

                // Let ASP.NET handle sending bytes to the client.
                ResponseLimiter.AssertFileLength(binaryStream.Length);
                await binaryStream.CopyToAsync(_context.Response.Body);
            }

            // Let the client code log file downloads
            if (RequestedNode is File file)
            {
                File.Downloaded(file.Id);
            }
        }
Exemplo n.º 5
0
        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);
            }
        }
Exemplo n.º 6
0
        /// <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);
        }
Exemplo n.º 7
0
        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);
        }