Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Zero Content-Length for static file on Ubuntu #2082

Closed
rustamkulenov opened this issue Sep 22, 2017 · 4 comments
Closed

Zero Content-Length for static file on Ubuntu #2082

rustamkulenov opened this issue Sep 22, 2017 · 4 comments

Comments

@rustamkulenov
Copy link

I've developed SPA application (ASP core,MVC,Angular) on Windows but can not host it on Linux. When I reload webpage in browser by F5 Kestrel prints out error and webbrowser receives HTTP code 500. This happens every second reload.

fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HL81HU49MSPA", Request id "0HL81HU49MSPA:00000002": An unhandled exception was thrown by the application.
System.InvalidOperationException: Response Content-Length mismatch: too few bytes written (0 of 42).

Environment:

#dotnet --info

.NET Command Line Tools (2.0.0)

Product Information:
Version: 2.0.0
Commit SHA-1 hash: cdcd1928c9

Runtime Environment:
OS Name: ubuntu
OS Version: 16.04
OS Platform: Linux
RID: ubuntu.16.04-x64
Base Path: /usr/share/dotnet/sdk/2.0.0/

Microsoft .NET Core Shared Framework Host

Version : 2.0.0
Build : e8b8861ac7faf042c87a5c2f9f2d04c98b69f28d

Steps to reproduce:

  1. Create new MVC application:
    #dotnet new mvc
  2. Place file named 1.txt with any content into wwwroot
  3. Change Home/Index controller method to:

public IActionResult Index()
{
return File("~/1.txt", "text/plain");
}

  1. #dotnet run

Now, If you try to open localhost:5000 browser will show content of the 1.txt, if you press F5 it'll show error 500. Press F5 again and it'll display content of 1.txt again, etc...

I tried to change Startup.cs to (as stated in #1289 ):

app.UseStaticFiles(new StaticFileOptions(){
OnPrepareResponse=context=>{
context.Context.Response.Headers.Remove("Content-Length");
}
});

, but this does not help.

If I set breakpoint in Home/Index controller method it is hit every time (i.e. on every reload). But it returns content-length=0 on every even reload. On every odd reload it returns correct value.

@Tratcher
Copy link
Member

Can you share a Fiddler trace?

Note there's no functional link between the File result in MVC and UseStaticFiles, they operate independently.

I've seen this behavior before. The odd requests are normal request but the even requests have conditional headers like if-modified-since. The result from these even requests is supposed to be a 304 not-modified with no body. There's a disconnect where something sets the content-length but then does not write the body, likely based on these conditional headers.

I'll try to track down the old bug for you. I don't think #1289 is the same issue, that was an overwrite rather than an underwrite.

@Tratcher
Copy link
Member

I'm not finding the matching bug.

I assume you're hitting this new code path:
https://github.com/aspnet/Mvc/blob/9aff0a67c189fe25d48379f94f6180e577d55d5c/src/Microsoft.AspNetCore.Mvc.Core/Internal/FileResultExecutorBase.cs#L88-L92

But I don't see it setting the Content-Length in this case. In fact, I don't see it setting the ContentLength for the 200 responses either. @jbagga

@jbagga
Copy link

jbagga commented Sep 22, 2017

I think this might be the bug https://github.com/aspnet/Mvc/blob/rel/2.0.0/src/Microsoft.AspNetCore.Mvc.Core/Internal/FileResultExecutorBase.cs#L85-L91

If the status is 304 or 412, Content-Length should not be set because an empty body is being returned. @Tratcher

@Tratcher
Copy link
Member

This issue was moved to aspnet/Mvc#6875

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants