question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Date" header not changing between requests

See original GitHub issue

Discussed in https://github.com/encode/uvicorn/discussions/1610

<div type='discussions-op-text'>

Originally posted by arjwilliams August 22, 2022 I am testing out some cache-control headers in my web app, but the auto-generated “Date” header seems to only be changing when a new connection is established, rather than with each new request, which is preventing the browser from caching new responses, as it sees the response as being older than it actually is.

I would expect that the “Date” header to change between requests, even when using an existing connection.

I am testing on Python 3.10.5, with Uvicorn 0.18.2.

Possible test case, which is currently failing for me:

import asyncio

from email.utils import parsedate_to_datetime
from contextlib import asynccontextmanager

import httpx

from uvicorn import Config, Server

async def test_default_date_header_different():
    config = Config(app=app, loop="asyncio", limit_max_requests=2)
    async with run_server(config):
        async with httpx.AsyncClient() as client:
            response1 = await client.get("http://127.0.0.1:8000")
            await asyncio.sleep(2)
            response2 = await client.get("http://127.0.0.1:8000")

            response1_date = parsedate_to_datetime(response1.headers["date"])
            response2_date = parsedate_to_datetime(response2.headers["date"])
            assert(response2_date > response1_date)

async def app(scope, receive, send):
    assert scope["type"] == "http"
    await send({"type": "http.response.start", "status": 200, "headers": []})
    await send({"type": "http.response.body", "body": b"", "more_body": False})

@asynccontextmanager
async def run_server(config: Config):
    server = Server(config=config)
    cancel_handle = asyncio.ensure_future(server.serve())
    await asyncio.sleep(0.1)
    try:
        yield server
    finally:
        await server.shutdown()
        cancel_handle.cancel()

asyncio.run(test_date_header_different())
```</div>

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Kludexcommented, Oct 19, 2022

This will be available in uvicorn 0.19.0.

1reaction
Kludexcommented, Sep 11, 2022

not sure about that, isn’t that the keep-alive of 5s is for, the tests pass fine if for instance we set

    config = Config(app=app, timeout_keep_alive=1.1)
    server = Server(config=config)

Correct. This is not a bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Date" header not changing between requests #1610 - GitHub
I am testing out some cache-control headers in my web app, but the auto-generated "Date" header seems to only be changing when a...
Read more >
Why is the Date header not set when I make a WebRequest in ...
My requests to Google are failing though due to a missing "Date" header. Fiddler confirms that a Date header is not being sent...
Read more >
Using The Date Header Field In HTTP Requests - IETF
Most HTTP clients have no need to use the Date header field in requests. This only changes if it is important that the...
Read more >
Date - HTTP - MDN Web Docs - Mozilla
The Date general HTTP header contains the date and time at which the message originated.
Read more >
HTTP/1.1: Header Field Definitions
Note: If the request does not include an Accept-Encoding field, and if the ... or 101 (Switching Protocols), the response MAY include a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found