网络浏览器如何请求和接收网页?
今天被问到这个有趣的面试问题。
详细解释客户端计算机从服务器请求文件(例如 file.php),然后接收所需文件及其必要的 JS/CSS/图像/视频文件的过程,并且它们出现在客户端的浏览器屏幕。
以下是我所知道的以及我所说的:
所以发送了一个请求,然后服务器看到正在请求file.php文件,并且因为它有.php扩展名,所以它首先使用PHP引擎解析文件内部的任何PHP代码,然后一次完成后,它将生成的 file.php 文件输出回客户端计算机(作为响应)。然后,浏览器获取该响应并解析 HTML 以及必要的 JS 和 CSS 代码,然后将其显示到浏览器。
我的回答非常基本,没有应有的那么详细。我思考了我的回答并提出了新问题:
从字面上看,什么是“请求”?它基本上只是发送到服务器的文本头文件吗?
“回应”怎么样?响应本身是发送回客户端计算机的已解析 file.php 文件吗?
如果 file.php 文件包含对 script.js 文件和 style.css 文件的引用怎么办?这些文件在哪个阶段返回到客户端计算机?它们是以单独的标头形式出现还是什么?
在我的回答中,我不确定我所说的是否正确:“...因为它具有 .php 扩展名,所以它首先使用 PHP 引擎来解析文件内的任何 PHP 代码。”这真的是服务器解析文件内代码的原因,还是服务器默认扫描所有类型的文件以检查它们可能包含的任何 PHP 代码?
Got asked this interesting interview question today.
Explain in detail the process by which a client machine requests a file (say file.php) from the server, and then receives that desired file along with its necessary JS/CSS/images/video files and they appear on the client's browser screen.
Here is what I do know and what I did say:
So a request is sent, then the server sees that the file.php file is being requested, and because it has a .php extension, it first uses the PHP engine to parse any PHP code inside the file, and then once it is done, it outputs back to the client machine the resulting file.php file (as a response). The browser then takes that response and parses the HTML and necessary JS and CSS code, then displays it to the browser.
My answer is pretty basic and not as detailed as it should be. I thought about my response and came up with new questions:
What, literally, is a "request"? Is it basically just the textual header file that gets sent to the server?
What about a "response"? Is the response itself the parsed file.php file that gets sent back to the client machine?
What if the file.php file contains a reference to a script.js file and a style.css file? At which stage do those files get served back to the client machine? Do they come in as separate headers or what?
Above in my answer, I'm not sure if I was correct when I said "...because it has a .php extension, it first uses the PHP engine to parse any PHP code inside the file." Is that really the reason why the server parses the code inside the file, or does the server scan ALL kinds of files by default to check for any PHP code they might contain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我觉得你的回答非常好。它明确描述了您被问及的基本过程。
是的,HTTP 请求是发送给服务器的文本消息,主要包括:请求的路径、该路径的任何参数、客户端信息(用户代理、会话、cookie 等)。
有点像。 HTTP 响应由标题文本组成,描述:响应状态(成功或错误,例如未找到文件、内部服务器错误等)、一些内容元数据(内容类型、编码...)和内容。
内容可以是 HTML 文档。它还可以是 CSS 或 Javascript 文件、PNG 图像或 Web 服务器提供的任何其他文件。标头中的元数据以浏览器(或任何客户端)可以弄清楚如何处理它的方式描述内容。
首先,您刚才描述的过程将会完成。意思是,发送了请求,然后返回了响应。假设响应是 HTML 文档,浏览器会解析该文档并查找外部内容:CSS 样式表、Javascript 文件、图像文件、Flash 嵌入等。
对于每个外部文件,浏览器都会使用完全相同的过程发送新请求。例如,在获取 CSS 文件后,浏览器知道将其应用到刚刚解析的文档中。
嗯,这取决于服务器配置,但大多数时候,是的;
服务器通常配置为以相同的方式处理所有
.php
文件,这意味着将它们传递给 PHP 解析器并等待其响应。顺便说一句,这对于不同的服务器端软件技术来说是不同的。虽然这是 PHP 的工作方式,但其他技术(例如 Ruby on Rails、某些 .NET 语言)以不同的方式处理。
很好的问题,对你表现出兴趣有好处!
有关更多信息,我建议您查看维基百科上的 HTTP。
First of all, I think your answer was quite good. It definitely describes the basic process you were asked about.
Yes, an HTTP request is a text message to a server including, mostly: the requested path, any parameters to that path, client info (user agent, session, cookies etc.).
Sort of. An HTTP response consists of a header text that describes: the response status (success or errors such as file not found, internal server error etc.), some content metadata (content type, encoding...) and the content.
The content could be an HTML document. It could also be a CSS or Javascript file, a PNG image or whatever other files the web server serves. The meta-data in the header describes the content in a way that the browser (or any client) can figure out how to handle it.
Firstly, the process you just described would finish. Meaning, a request was sent and then a response was returned. Assuming the response is an HTML document, the browser parses the document and looks for external content: CSS stylesheets, Javascript files, image files, flash embeds and the like.
For each of these external files, the browser sends a new request using the exact same process. After getting a CSS file, for example, the browser knows to apply it to the document it just parsed.
Well, it depends on the server configuration, but most of the times, yes;
The server is usually configured to handle all
.php
files the same, meaning pass them to the PHP parser and wait for its response.By the way, this differs for different server-side software technologies. While this is the way PHP works, other technologies (e.g. Ruby on Rails, some .NET languages) are handled in a different way.
Great question, and good for you for showing interest!
For additional information, I suggest you check out HTTP on Wikipedia.
HTTP 请求类似于
GET /index.html HTTP/1.1
。它以纯文本形式发送到 Web 服务器。简化的 HTTP 响应(删除了大部分标头内容)可能如下所示:
如果页面包含图像、样式表或其他外部文件,则 Web 浏览器会为它们发送新请求,每个文件一个请求。 Web 服务器返回它们的方式与返回 HTML 的方式几乎相同。当浏览器请求并接收到它想要的所有文件时,页面就完成了。
由 Web 服务器决定如何处理 PHP 等内容。浏览器不需要知道幕后发生的事情。从它的角度来看,它只是请求内容并(希望)接收它。
一个简单的 Web 服务器可能配置为完全按照您所说的方式执行。如果它收到对以“.php”结尾的文件的请求,它将首先通过 PHP 解释器运行它。但这完全由网络服务器所有者决定。
A HTTP request looks like
GET /index.html HTTP/1.1
. It is sent as plain text to the web server.A simplified HTTP response (with most header stuff removed) might look like this:
If the page contains images or stylesheets or other external files, the web browser sends new requests for them, one request per file. The web server returns them in pretty much the same way it returned the HTML. When the browser has requested and received all the files it wants, the page is complete.
It is up to the web server to decide how it wants to process things like PHP. The browser doesn't need to know what goes on behind the curtains. From its perspective, it simply asks for content and (hopefully) receives it.
A simple web server might be configured to do exactly as you said. If it receives a request for a file ending in ".php", it would first run it through the PHP interpreter. But this is totally up to the web server owner to decide.
我认为这就是您正在寻找的: 当您导航到某个 URL 时实际会发生什么
总结一下:
1. 您在浏览器中输入 URL:facebook.com
2.浏览器查找域名的IP地址
3.浏览器向Web服务器发送HTTP请求
4. Facebook 服务器以永久重定向进行响应
5. 浏览器遵循重定向
6. 服务器‘处理’请求
7. 服务器发回 HTML 响应
8. 浏览器开始渲染 HTML
9. 浏览器发送对嵌入在 HTML 中的对象的请求
10. 浏览器发送进一步的异步(AJAX)请求
I think this is what you are looking for: What really happens when you navigate to a URL
To summarize:
1. You enter a URL into the browser: facebook.com
2. The browser looks up the IP address for the domain name
3. The browser sends a HTTP request to the web server
4. The facebook server responds with a permanent redirect
5. The browser follows the redirect
6. The server ‘handles’ the request
7. The server sends back a HTML response
8. The browser begins rendering the HTML
9. The browser sends requests for objects embedded in HTML
10. The browser sends further asynchronous (AJAX) requests