javascript - HTTP 中的数据传输

标签 javascript html sockets http server-side

我正在尝试了解 HTTP 服务器和客户端内部的基础知识,了解它们如何传输数据。我已经阅读了很多关于 HTTP 工作原理的文章,但我还没有找到任何可以回答我的一些问题的文章。 我想按照我的理解来完成加载网页的过程,如果你让我注意到我哪里错了,我将不胜感激。

  1. 当我访问一个站点时,我的浏览器向服务器请求一个 HTML 文件,为此我的浏览器创建了一个套接字,将其绑定(bind)到我的 ip 地址,并将其连接到站点服务器的监听套接字我正在参观。为了将我的浏览器套接字连接到服务器,我需要一个端口号和一个主机名,端口号是 80 因为这是 HTTP 并且主机名是通过 DNS 解析获得的。既然套接字之间存在连接,我的浏览器就会发送一个 GET 请求。该请求是一个 ASCII 文件,其内容与 HTTP 请求相对应。我的浏览器将 ASCII 原始字节写入套接字,然后写入服务器的套接字。

  2. 服务器将我请求的 HTML 文件写回套接字。服务器发送的 HTML 只是一个 ASCII 文件,服务器将逐字节写入套接字。

  3. 我的浏览器收到 ASCII 文件并解析它。让我们在这里假设它找到了一个图像标签。浏览器发送该图像文件的 HTTP 请求。这里有一些我不明白的东西。服务器如何响应?据我所知,服务器必须发回一个 ASCII 文件,该文件由一组 header 组成,后跟 CRLF,然后是消息正文。在这种情况下,假设我的浏览器请求 .jpeg,服务器是否将 header 作为 ASCII 纯文本写入套接字,然后将图像的原始字节写入套接字?

  4. 如果 HTML 文件有多个图像,我们是否为每个图像(每个请求)打开一个套接字?

  5. 假设我的浏览器现在找到了一个 javascript 标签。当服务器响应我对该脚本的请求时,服务器是否将脚本源的 ASCII 字节写入套接字? js库会发生什么?服务器是否必须为每一个发送所有源代码?

  6. 关于将数据写入套接字:write(2) 是在套接字之间执行所有这些写入的正确方法吗?

  7. 关于大文件的传输:如果我点击网站上的一个按钮让我下载一个大的 PDF,服务器是如何完成的?我假设服务器试图分段传输。据我所知,有一个分块编码选项。是这样吗?如果是,是否将文件分成 block ,并将这些 block 附加到 ASCII 响应并逐字节写入套接字?

最后,视频是如何传输的?我知道视频编码和传输需要整本书才能详细解释,但如果您能谈谈视频传输的一般性(例如在 youtube 中),我将不胜感激。

如果您能就套接字级别的 HTTP 发表任何看法,我们将不胜感激。谢谢。

最佳答案

我下面的所有回答都与 HTTP/1.1 有关,而不是 HTTP/2:

3.-My browser recieves the ASCII file and parses it. Lets assume here that it finds an image tag. The browser sends an HTTP request for that image file. Here comes something I don't understand. How does the server respond? As far as I can tell the server must send back an ASCII file formed by a set of headers followed by a CRLF and then the body of the message. In this case, assuming my browser asked for a .jpeg, does the server write the headers as ASCII plaintext to the socket and then writes the raw bytes of the image to the socket?

是的,通常会。它可能以不同的格式(gzip、brotli)编码,或者如果未设置 Content-Length,它可能会被分块。

4.- If the HTML file has several images do we open a socket per image (per request)?

在 HTTP/1 中,现代浏览器将为每个主机打开最多 6 个套接字,但不会更多。如果有超过 6 个请求发往同一主机,它将等待直到收到其他响应。

5.- Lets asume that my browser now finds a javascript tag. When the server answers to my request for that script does the server writes the ASCII bytes of the source of the script to the socket? What happens with js libraries? Does the server have to send all the source code for each one?

通常是的,每个 javascript 文件需要 1 个 http 请求。有一些服务器端工具将 javascript 源及其依赖项组合在一个 javascript"file"中。请注意,javascript 源通常是 UTF-8,而不是 ASCII。

6.- On writing data to the sockets: is write(2) the correct way to do all this writing between sockets?

不知道!不是C哥

7.- On the transmition of large files: if I click a button on the site that lets me download a large PDF, how is this accomplished by the server? I assume that the server tries to transmit this in pieces. As far as I can tell there is an option for chunked encoding. Is this the way? If it is, is the file divided into chunks, and these are appended to the ASCII response and written byte by byte into the socket?

不,chunked 用于内容长度事先未知的 HTTP 响应。您所说的“拆分”是在 IP/TCP 级别完成的,而不是在 HTTP 协议(protocol)级别完成的。从 HTTP 的 Angular 来看,它只是一个连续的流。

Finally, how is video transmited? I know video encoding and transmition would require entire books for a detailed explanaition but if you could say something about the generalities of video transmition (for example in youtube) I would appreciate it.

范围太广,我无法回答。

关于javascript - HTTP 中的数据传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44446011/

相关文章:

javascript - 检查数组 onchange 中的重复值

javascript - 类 ='col-md-12' 在 Firefox 中无法正常工作但在 Chrome 中工作?

html - 将背景图像带到前台。可能与否?

javascript - Socket.io:如何让每个人获得不同的 channel ?

c# - 如何将 C++ 套接字编程代码转换为 C#?

c++ - TCP Socket block 函数段错误,由移动 if 语句引起

Javascript 随机数?

Javascript 在子窗口的就绪回调中给出了错误的值

javascript - 如何在网页 View Android 中的网页上运行 javascript

php - 如果表格不为空则隐藏按钮