Professional Documents
Culture Documents
HTTP
HTTP
HTTP HEADERS
HTTP headers are key-value pairs of metadata included in both HTTP requests and
responses. They provide additional information about the data being sent, received, or the
communication itself. HTTP headers are an essential part of the HTTP protocol, and they can
serve various purposes, including content negotiation, security, and caching. Here are some
common HTTP headers:
These headers allow clients and servers to communicate and negotiate various aspects of
the HTTP transaction, such as the content format, language, authentication, and caching
behavior. They play a vital role in enabling the flexibility and extensibility of the HTTP
protocol for different use cases and requirements.
Persistent and Non persistent
https://www.scaler.com/topics/persistent-connection-http/
Browsers have a built-in cachingn mechanism for a better user experience.How do websites indicate if a web resource
needs to be cached or not?show http messages in transit for both scenarios.
ChatGPT
Websites can control whether web resources should be cached or not by using HTTP headers. Two key HTTP headers
that control caching behavior are Cache-Control and Expires . These headers provide directives to the client
(typically a web browser) about how it should cache and manage the resource.
Let's explore how websites indicate caching behavior using these headers and provide examples of HTTP messages in
transit for both scenarios:
.
Caching Enabled: Using Cache-Control and Expires Headers
.
Cache-Control Header: The Cache-Control header provides various directives to control caching.
Two commonly used directives are:
max-age: Specifies the maximum amount of time (in seconds) a resource can be cached.
public: Indicates that the resource can be cached by both the client and intermediary caches
(like CDN servers).
private: Specifies that the resource is specific to the client and should not be cached by
intermediary caches.
Expires Header: The Expires header specifies an absolute date and time when the resource expires and
should no longer be considered fresh. It's used in combination with the Cache-Control header.
HTTP/1.1 200 OK
Server: ExampleServer
Content-Type: text/html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Hello, Caching!</h1>
</body>
</html>
In this example, the server sends an HTTP response with Cache-Control and Expires headers. The Cache-
Control header specifies that the resource can be cached by both the client and intermediary caches and is valid
for one hour ( max-age=3600 ). The Expires header sets the absolute expiration time.
.
Caching Disabled: Using No-Cache and Pragma Headers
.
Cache-Control Header: The Cache-Control header can include the no-cache directive, which
indicates that the resource should not be cached. Additionally, it can include no-store , which prevents
any caching (even in memory or on disk) of the resource.
Pragma Header: The Pragma header, although considered somewhat outdated, can be set to no-
cache to specify that the resource should not be cached.
HTTP Response with Caching Disabled:
HTTP/1.1 200 OK
Date: Wed, 02 Nov 2023 12:00:00 GMT
Server: ExampleServer
Content-Type: text/html
Cache-Control: no-cache, no-store
Pragma: no-cache
<!DOCTYPE html>
<html>
<head>
<title>No-Cache Web Page</title>
</head>
<body>
<h1>No Caching Allowed</h1>
</body>
</html>
In this example, the server sends an HTTP response with Cache-Control and Pragma headers that explicitly
specify that the resource should not be cached by the client or intermediary caches.
Web browsers respect these headers when receiving the HTTP response. If caching is allowed, browsers store the
resource in their cache, and for subsequent requests, they may use the cached version. If caching is disabled,
browsers bypass caching and fetch the resource directly from the server.
Please note that while these headers indicate caching preferences, the actual behavior may also depend on the
browser's implementation and configuration.