1. 程式人生 > >http頭中的host欄位詳解

http頭中的host欄位詳解

Host:客戶端指定自己想訪問的http伺服器的域名/IP 地址和埠號。

今天同事問了一個問題引發了我對http頭中host欄位的詳細思考,總結了以下5條。

1.host欄位可以是域名,也可以是ip地址。host欄位域名/ip後可以跟埠號,如Host: www.6san.com:8080

2.host可以由程式自定義,某些程式為了防止運營商或防火牆攔截會定義虛假host

3.http1.1中host欄位可以是空值,http1.0中可以缺失host欄位

4.http響應頭中不包含host欄位,所以wireshark中http.host過濾到的都是請求包

5.因為http頭中的host欄位可以由程式自定義,所以host欄位的值就會有很多特殊情況,如包含多個’/’字元的HOST頭資訊,結尾含有”.”等

http頭中可以沒有host欄位嗎?

在http 1.1中不能缺失host欄位,如果缺失, 伺服器返回400 bad request,http1.1中不能缺失host欄位,但host欄位可以是空值

在http 1.0中可以缺失host欄位。

RFC 2616中關於host欄位的說明如下:

A client MUST include a Host header field in all HTTP/1.1 request
messages . If the requested URI does not include an Internet host
name for the service being requested, then the Host header field MUST
be given with an empty value. An HTTP/1.1 proxy MUST ensure that any
request message it forwards does contain an appropriate Host header
field that identifies the service being requested by the proxy. All
Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request)
status code to any HTTP/1.1 request message which lacks a Host header
field.