计算机网络 02:应用层(2)

189 阅读15分钟

Web and HTTP

HTTP(HyperText Transfer Protocol)是万维网(World Wide Web)的基础协议。自 Tim Berners-Lee 博士和他的团队在 1989-1991 年间创造出它以来,HTTP 已经发生了太多的变化,在保持协议简单性的同时,不断扩展其灵活性。如今,HTTP 已经从一个只在实验室之间交换文件的早期协议进化到了可以传输图片,高分辨率视频和 3D 效果的现代复杂互联网协议。

万维网的发明

1989 年,当时在 CERN 工作的 Tim Berners-Lee 博士写了一份关于建立一个通过网络传输超文本系统的报告。这个系统起初被命名为 Mesh,在随后的 1990 年项目实施期间被更名为万维网(World Wide Web)。它在现有的 TCP 和 IP 协议基础之上建立,由四个部分组成:

  • 一个用来表示超文本文档的文本格式,超文本标记语言(HTML)。
  • 一个用来交换超文本文档的简单协议,超文本传输协议(HTTP)。
  • 一个显示(以及编辑)超文本文档的客户端,即网络浏览器。第一个网络浏览器被称为 WorldWideWeb。
  • 一个服务器用于提供可访问的文档,即 httpd 的前身。

HTTP 在应用的早期阶段非常简单,后来被称为 HTTP/0.9,有时也叫做单行(one-line)协议。

HTTP/0.9——单行协议

最初版本的 HTTP 协议并没有版本号,后来它的版本号被定位在 0.9 以区分后来的版本。HTTP/0.9 极其简单:请求由单行指令构成,以唯一可用方法 GET 开头,其后跟目标资源的路径(一旦连接到服务器,协议、服务器、端口号这些都不是必须的)。

 GET /mypage.html

响应也极其简单的:只包含响应文档本身。

 <HTML>
 这是一个非常简单的 HTML 页面
 </HTML>

跟后来的版本不同,HTTP/0.9 的响应内容并不包含 HTTP 头。这意味着只有 HTML 文件可以传送,无法传输其他类型的文件。也没有状态码或错误代码。一旦出现问题,一个特殊的包含问题描述信息的 HTML 文件将被发回,供人们查看。

HTTP/1.0——构建可扩展性

由于 HTTP/0.9 协议的应用十分有限,浏览器和服务器迅速扩展内容使其用途更广:

  • 协议版本信息现在会随着每个请求发送(HTTP/1.0 被追加到了 GET 行)。
  • 状态码会在响应开始时发送,使浏览器能了解请求执行成功或失败,并相应调整行为(如更新或使用本地缓存)。
  • 引入了 HTTP 标头的概念,无论是对于请求还是响应,允许传输元数据,使协议变得非常灵活,更具扩展性。
  • 在新 HTTP 标头的帮助下,具备了传输除纯文本 HTML 文件以外其他类型文档的能力(凭借 Content-Type 标头)。

一个典型的请求看起来就像这样:

 GET /mypage.html HTTP/1.0
 User-Agent: NCSA_Mosaic/2.0 (Windows 3.1)
 
 200 OK
 Date: Tue, 15 Nov 1994 08:12:31 GMT
 Server: CERN/3.0 libwww/2.17
 Content-Type: text/html
 <HTML>
 一个包含图片的页面
   <IMG SRC="/myimage.gif">
 </HTML>

接下来是第二个连接,请求获取图片(并具有相同的响应):

 GET /myimage.gif HTTP/1.0
 User-Agent: NCSA_Mosaic/2.0 (Windows 3.1)
 
 200 OK
 Date: Tue, 15 Nov 1994 08:12:32 GMT
 Server: CERN/3.0 libwww/2.17
 Content-Type: text/gif
 (这里是图片内容)

在 1991-1995 年,这些新扩展并没有被引入到标准中以促进协助工作,而仅仅作为一种尝试。服务器和浏览器添加这些新扩展功能,但出现了大量的互操作问题。直到 1996 年 11 月,为了解决这些问题,一份新文档(RFC 1945)被发表出来,用以描述如何操作实践这些新扩展功能。文档 RFC 1945 定义了 HTTP/1.0,但它是狭义的,并不是官方标准。

HTTP/1.1——标准化的协议

HTTP/1.0 多种不同的实现方式在实际运用中显得有些混乱。自 1995 年开始,即 HTTP/1.0 文档发布的下一年,就开始修订 HTTP 的第一个标准化版本。在 1997 年初,HTTP1.1 标准发布,就在 HTTP/1.0 发布的几个月后。

HTTP/1.1 消除了大量歧义内容并引入了多项改进:

  • 连接可以复用,节省了多次打开 TCP 连接加载网页文档资源的时间。
  • 增加管线化技术,允许在第一个应答被完全发送之前就发送第二个请求,以降低通信延迟。
  • 支持响应分块。
  • 引入额外的缓存控制机制。
  • 引入内容协商机制,包括语言、编码、类型等。并允许客户端和服务器之间约定以最合适的内容进行交换。
  • 凭借 Host 标头,能够使不同域名配置在同一个 IP 地址的服务器上。

一个典型的请求流程,所有请求都通过一个连接实现,看起来就像这样:

 GET /en-US/docs/Glossary/Simple_header HTTP/1.1
 Host: developer.mozilla.org
 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0
 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
 Accept-Language: en-US,en;q=0.5
 Accept-Encoding: gzip, deflate, br
 Referer: https://developer.mozilla.org/en-US/docs/Glossary/Simple_header
 
 200 OK
 Connection: Keep-Alive
 Content-Encoding: gzip
 Content-Type: text/html; charset=utf-8
 Date: Wed, 20 Jul 2016 10:55:30 GMT
 Etag: "547fa7e369ef56031dd3bff2ace9fc0832eb251a"
 Keep-Alive: timeout=5, max=1000
 Last-Modified: Tue, 19 Jul 2016 00:59:33 GMT
 Server: Apache
 Transfer-Encoding: chunked
 Vary: Cookie, Accept-Encoding
 
 (content)
 
 
 GET /static/img/header-background.png HTTP/1.1
 Host: developer.mozilla.org
 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0
 Accept: */*
 Accept-Language: en-US,en;q=0.5
 Accept-Encoding: gzip, deflate, br
 Referer: https://developer.mozilla.org/en-US/docs/Glossary/Simple_header
 
 200 OK
 Age: 9578461
 Cache-Control: public, max-age=315360000
 Connection: keep-alive
 Content-Length: 3077
 Content-Type: image/png
 Date: Thu, 31 Mar 2016 13:34:46 GMT
 Last-Modified: Wed, 21 Oct 2015 18:27:50 GMT
 Server: Apache
 
 (image content of 3077 bytes)

HTTP/1.1 在 1997 年 1 月以 RFC 2068 文件发布。

由于 HTTP 协议的可扩展性使得创建新的头部和方法是很容易的。即使 HTTP/1.1 协议进行过两次修订,RFC 2616 发布于 1999 年 6 月,而另外两个文档 RFC 7230-RFC 7235 发布于 2014 年 6 月(在 HTTP/2 发布之前)。HTTP/1.1 协议已经稳定使用超过 15 年了。

HTTP overview

HTTP: hypertext(超文本) transfer protocol

  • Web’s application layer protocol

  • client/server model

    • client: browser that requests, receives, (using HTTP protocol) and “displays” Web objects
    • server: Web server sends (using HTTP protocol) objects in response to requests

HTTP request message: ASCII (human-readable format)

Uploading form input

POST method:

  • web page often includes form input
  • input is uploaded to server in entity(实体) body

URL method:

  • uses GET method
  • input is uploaded in URL field of request line:如 www.somesite.com/animalsearch?monkeys&banana

Method types

HTTP/1.0:

  • GET
  • POST
  • HEAD:asks server to leave requested object out of response

HTTP/1.1:

  • GET
  • POST
  • HEAD
  • PUT:uploads file in entity body to path specified in URL field
  • DELETE:deletes file specified in the URL field

HTTP response status codes(HTTP 响应状态码)

status code appears in 1st line in server-to-client response message.

HTTP 响应状态码 - HTTP | MDN (mozilla.org)

HTTP 响应状态码用来表明特定 HTTP 请求是否成功完成。 响应被归为以下五大类:

  1. 信息响应 (100199)
  2. 成功响应 (200299)
  3. 重定向消息 (300399)
  4. 客户端错误响应 (400499)
  5. 服务端错误响应 (500599)

备注: 如果您收到的响应不在 此列表 中,则它为非标准响应,可能是服务器软件的自定义响应。

HTTP response status codes - PlayFab | Microsoft Learn

常见的相应状态码:

HTTP status codeGeneral description
100Continue: Returned on HEAD requests
200OK: Returned for all successful requests. May indicate partial success for bulk APIs.
201Created: Request was successful and resource was created.
202Accepted: Request was successful, processing will continue asynchronously.
204No Content: API successful, but there is no response to be returned from the API.
301Moved Permanently: requested object moved, new location specified later in this msg (Location:)
400Bad Request: Parameters in request where invalid or request payload structure was invalid. Do not retry.
401Unauthorized: Caller is not authorized to either call the specific API or perform the action requested. Do not retry.
403Forbidden: Caller is not allowed access. Do not retry.
404Not Found: API does not exist. Do not retry.
408Request Timeout: The request took too long to be sent to the server. Okay to retry using exponential backoff pattern.
409Conflict: A concurrency error occurred between two API calls. Okay to retry using exponential backoff pattern.
413Payload Too Large: The request is larger than the server is allowed to handle. Do not retry. If unexpected, contact support.
414URI Too Long: The URI in the request is longer than the server is allowed to handle. Do not retry.
429Too Many Requests: API calls are being rate limited. Pause and then retry request, check if API returned “Retry-After” header or retryAfter in JSON response for delay needed.
500Internal Server Error: An error occurred on the PlayFab server. Okay to retry, contact support if problem persists.
501Not Implemented: The API called has not been implemented yet. Do not retry.
502Bad Gateway: PlayFab API servers are not available to process the request. Pause and then retry request using exponential backoff pattern.
503Service Unavailable: PlayFab API servers are not available to process the request. Pause and then retry request using exponential backoff pattern.
504Gateway Timeout: PlayFab API servers are not available to process the request. Pause and then retry request.
505HTTP Version Not Supported

User-server state: cookies

many Web sites use cookies

four components:

  1. cookie header line of HTTP response message
  2. cookie header line in next HTTP request message
  3. cookie file kept on user’s host, managed by user’s browser
  4. back-end database at Web site

what cookies can be used for:

  • authorization
  • shopping carts(购物车,也代指其他需要存储的东西)
  • recommendations
  • user session state (Web e-mail)

Web caches (proxy server)

  • user sets browser: Web accesses via cache

  • browser sends all HTTP requests to cache

    object in cache: cache returns object else cache requests object from origin server, then returns object to client

Web 缓存(也称为代理服务器)是广泛用于提高 Web 页面加载速度和减轻网络流量压力的技术。它的工作方式是在用户和 Web 服务器之间放置一个位于网络边缘的服务器,该服务器存储先前获取的 Web 页面的副本,以便当后续用户请求相同页面时,可以直接返回本地存储的副本,而不必重新从源服务器下载。这样,用户就可以更快地加载页面,并且减轻了源服务器的压力。

Web 缓存可以根据不同的应用场景进行配置,例如在企业内部网络中设置私有 Web 缓存来提高内部网站的访问速度,或在 Internet 网络边缘设置公共 Web 缓存来减轻互联网上的流量压力。此外,Web 缓存还可以通过缓存一些常用的 JavaScript 和 CSS 文件等静态资源,来进一步提高页面加载速度。

需要注意的是,Web 缓存不适用于动态生成的 Web 页面或需要个性化处理的页面,因为这些页面的内容是根据用户请求动态生成的,所以不可能事先缓存下来。此外,一些受保护的 Web 页面(如登录页)也不能缓存,因为每个用户的页面内容都是不同的,不适合缓存下来供其他用户使用。

Conditional GET

Conditional GET 是一个 HTTP 协议的特性,可以在客户端像服务器发起请求时,通过发送 HTTP 头部中的条件标识,让服务器判断资源是否发生了变化,如果资源未发生变化,服务器可以选择不返回资源实体,而是返回 304 Not Modified 状态码,从而节省网络带宽和服务器资源。

使用条件 GET 时,客户端发送的请求中会包含 If-Modified-Since、If-Unmodified-Since、If-Match、If-None-Match 等条件标识,这些条件标识指定了客户端请求的资源应该满足的条件。当服务器对客户端的请求进行处理时,会根据这些条件是否满足来决定是否返回实际的资源,或者只返回 304 状态码。

条件 GET 可以提高网络传输效率和响应速度,因为服务器避免了重复发送已经有缓存的资源,从而降低了网络带宽的消耗。

Electronic Mail

Introduction to Electronic Mail - GeeksforGeeks

Electronic mail, commonly known as email, is a method of exchanging messages over the internet.

Here are the basics of email:

  1. An email address: This is a unique identifier for each user, typically in the format of name@domain.com.
  2. An email client: This is a software program used to send, receive and manage emails, such as Gmail, Outlook, or Apple Mail.
  3. An email server: This is a computer system responsible for storing and forwarding emails to their intended recipients.

SMTP (Simple Mail Transfer Protocol)、POP3 (Post Office Protocol Version 3) 和 IMAP (Internet Message Access Protocol) 都是在互联网上传输邮件的协议。

SMTP

SMTP (Simple Mail Transfer Protocol [RFC 2821]): 协议不负责接收邮件,在邮件传递过程中,它只负责将邮件从发送者的邮件服务器传递到接收者的邮件服务器。SMTP 协议发送邮件的过程通常分为以下步骤:

  1. 发送方邮件客户端(如 Outlook 等)向发送方邮件服务器发送邮件。
  2. 发送方邮件服务器将邮件发送给接收方邮件服务器,通过 SMTP 协议与接收方邮件服务器通信,询问是否可以接收该邮件。
  3. 接收方邮件服务器回复确认,发送方邮件服务器将邮件发送给接收方邮件服务器。
  4. 接收方邮件服务器接收到邮件并存储下来,接收方在自己的邮件客户端中查看邮件。

SMTP 协议的工作原理如下:

  1. 连接建立(SMTP connection opened):发送方邮件服务器与接收方邮件服务器建立 TCP 连接(25 号端口);握手:发送方发送 EHLO 命令告诉接收方客户端自己的域名和所支持的命令,接收方回复 250 OK 表示可以通信。

  2. 发送邮件数据(Email data transferred):发送方向接收方发送邮件数据,包括邮件头和邮件正文。

  3. Mail Transfer Agent (MTA): 在邮件传输过程中实际传送邮件的程序或服务。MTA 会负责从发件人的 SMTP 客户端接收邮件并将其传递给相应的收件人的 SMTP 服务器。MTA 还可以处理邮件发送过程中的错误或问题,例如无法传递邮件或邮件被拒绝等情况。常见的 MTA 软件包括 Sendmail、Postfix 和 Qmail 等。

    What is the Simple Mail Transfer Protocol (SMTP)? | Cloudflare

    The server runs a program called a Mail Transfer Agent (MTA). The MTA checks the domain of the recipient's email address, and if it differs from the sender's, it queries the Domain Name System (DNS) to find the recipient's IP address. This is like a post office looking up a mail recipient's zip code.

  4. 断开连接(Connection closed):发送方发送 QUIT 命令告诉接收方已经完成发送,接收方回复 221 表示连接已断开。

POP3 是一种用于接收电子邮件的协议。当您使用 POP3 协议从邮件服务器下载邮件时,邮件服务器就会将邮件传送到您的邮件客户端中,并从服务器上删除邮件。

IMAP 是另一种用于接收电子邮件的协议,它与 POP3 不同的是,它允许您在邮件服务器上保留邮件的副本,可以在多个设备之间同步邮件。这样,当您在一个设备上修改或删除邮件时,在其他设备上也可以看到这些修改。

What is the Simple Mail Transfer Protocol (SMTP)? | Cloudflare

What are SMTP commands?

SMTP commands are predefined text-based instructions that tell a client or server what to do and how to handle any accompanying data. Think of them as buttons the client can press to get the server to accept data correctly.

  • HELO/EHLO: These commands say "Hello" and start off the SMTP connection between client and server. "HELO" is the basic version of this command; "EHLO" is for a specialized type of SMTP.

  • MAIL FROM: This tells the server who is sending the email. If Alice were trying to email her friend Bob, a client might send "MAIL FROM:alice@example.com".

  • RCPT TO: This command is for listing the email's recipients. A client can send this command multiple times if there are multiple recipients. In the example above, Alice's email client would send "RCPT TO:bob@example.com".

  • DATA: This precedes the content of the email.

    It obeys RFC 822: standard for text message format. Like:

 DATA
 Date: Mon, 4 April 2022
 From: Alice alice@example.com
 Subject: Eggs benedict casserole
 To: Bob bob@example.com
 
 Hi Bob,
 I will bring the eggs benedict casserole recipe on Friday.
 -Alice
 .

Pay attention to line 6 and line 10.

  • RSET: This command resets the connection, removing all previously transferred information without closing the SMTP connection. RSET is used if the client sent incorrect information.
  • QUIT: This ends the connection.
  • SMTP uses persistent(持续的,可持续的) connections
  • SMTP requires message (header & body) to be in 7-bit ASCII
  • SMTP server uses CRLF.CRLF(就是指一个点) to determine end of message

comparison with HTTP:

  • HTTP: pull
  • SMTP: push

POP

POP (Post Office Protocol [RFC 1939]): authorization, download.

而 POP3 是一种用于接收电子邮件的协议。当您使用 POP3 协议从邮件服务器下载邮件时,邮件服务器就会将邮件传送到您的邮件客户端中,并从服务器上删除邮件。

IMAP

IMAP: Internet Mail Access Protocol [RFC 1730]: more features, including manipulation of stored messages on server.

IMAP 是另一种用于接收电子邮件的协议,它与 POP3 不同的是,它允许您在邮件服务器上保留邮件的副本(§keeps all messages in one place: at server),可以在多个设备之间同步邮件。这样,当您在一个设备上修改或删除邮件时,在其他设备上也可以看到这些修改。此外还有 allows user to organize messages in folders。