首页 > 解决方案 > 仅在应用层级别支持 URL 路径的概念?

问题描述

我正在编写自己的客户端和服务器,它们实现了我自己的应用程序级协议。传输层使用 TCP。对于互联网层,使用 IPv4。TCP 标头和 IPv4 标头都不包含有关 URL 路径的任何信息。TCP头包含端口,IPv4包含IP地址。谁应该包含路径?

所以,考虑这个 URL - 127.0.0.1:4444/location-path127.0.0.1应该是 IPv4 的一部分。4444应该是TCP的一部分。例如,在 Golang 中,使用标准net.Dial()net.Listen(),我只能使用127.0.0.1:4444. 使用 of127.0.0.1:4444/location-path会抛出一个错误,这是意料之中的,因为它只支持 TCP/IP/域名解析,而/location-path不是这三者的一部分。

根据RFC 1738

url-path
    The rest of the locator consists of data specific to the
    scheme, and is known as the "url-path". It supplies the
    details of how the specified resource can be accessed. Note
    that the "/" between the host (or port) and the url-path is
    NOT part of the url-path.

The url-path syntax depends on the scheme being used, as does the
manner in which it is interpreted.

那么,我可以实现自己的规则url-path以及客户端和服务器将如何解释它?要传输它,我可以将其作为数据发送吗?

例如,我可以只定义我自己的应用程序级协议的规则proto——将url-path在 TCP 有效负载的前四个字节处放置?所以,proto://127.0.0.1:4444/path会变成127.0.0.1:4444与 TCP 有效载荷[112 97 116 104]

标签: httpurlnetwork-protocolsurl-scheme

解决方案


最简单的比较方法是 HTTP。

http url 可能类似于 http://example:1234/bar

http 客户端理解该 URL。HTTP 客户端(在后台)仍然会建立标准的 TCP 连接以到达那里。

它通过抓取主机和端口部分并(暂时)丢弃其他所有内容来做到这一点。所以只有exampleand1234用在那个阶段。

TCP连接建立后,会使用路径部分作为第一行发送。

TCP 客户端不知道 URL,他们知道主机和端口。你对路径部分做什么取决于


推荐阅读