Content-length and other HTTP headers?

前端 未结 6 1400
时光取名叫无心
时光取名叫无心 2020-12-05 10:45

Does it give me any advantage if I set this header when generating normal HTML pages?

I see that some frameworks out there will set this header property and I was wo

相关标签:
6条回答
  • 2020-12-05 11:24

    Does it give me any advantage if I set this header when generating normal html pages?

    Does browser load the site faster or smoother?

    For dynamically generated pages, usually not. Content-Length lets the client know how large the file is. However, if Content-Length isn't specified, the transfer is sent in chunks (with a special chunk that signifies the end of the file). The former method can result in faster transmission as the overhead of splitting into chunks is eliminated. However, this is usually best reserved for static files as the resources required to buffer the entire contents in order to determine the length may outweigh any advantages gained by setting Content-Length.

    0 讨论(0)
  • 2020-12-05 11:25

    One supposedly good use for setting the content-length is when you like to send your response to the client even though you still like to perform more time-consuming operations afterwards.

    See this answer that explains that you'd set the content length, write your output and then can perform further operations in your script without making the client wait any longer.

    0 讨论(0)
  • 2020-12-05 11:25

    The best way (that is working for me) of getting size of a remote file:

    $head = array_change_key_case(get_headers($file, TRUE));
    $filesize = $head['content-length'];
    
    0 讨论(0)
  • 2020-12-05 11:26

    I think its only because of the HTTP Spec says to do this in every case possible.

    Applications SHOULD use this field to indicate the transfer-length of the message-body, unless this is prohibited by the rules in section 4.4.

    You can also look at Pauls Answer on the Question of Deaomon.

    I think this will answer yours too.

    Also you should use Content-Length if you want someone download a file with another header: e.g.

    <?php
    $file = "original.pdf"
    $size = filesize($file);
    header('Content-type: application/pdf');
    header("Content-length: $size");
    header('Content-Disposition: attachment; filename="downloaded.pdf"');
    readfile($file);
    ?>
    
    0 讨论(0)
  • 2020-12-05 11:29

    The main motivation behind it is re-using an existing TCP connection in HTTP 1.1. Content-Length can demarcate where the response ends for the recipient. As for the other headers, like Content-Type which specifies the MIME-type, they are for the recipient so that she would know what to do with the result depending on the type of the content For example, in Firefox you can specify what action to perform with different MIME-types. If you can pop-up the browser's save dialog or open a PDF in viewer with Content-Type of application/pdf

    0 讨论(0)
  • 2020-12-05 11:39

    Where content-type: text/html is concerned, it's mainly to tell the client what to expect and usually how to handle the file. In the case of bots, it can be useful for SEO purposes as it tells the bot what type of file it's looking at which can change how it's parsed/ranked.

    In the case of content-length, it's just to let the client know how large a file to expect and thus where the response ends.

    0 讨论(0)
提交回复
热议问题