<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Snooda]]></title> 
<link>http://www.snooda.com/index</link> 
<description><![CDATA[Snooda's Blog]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Snooda]]></copyright>
<item>
<link>http://www.snooda.com/read/</link>
<title><![CDATA[HTTP1.0下HTTP_HOST允许为空]]></title> 
<author>snooda &lt;admin@snooda.com&gt;</author>
<category><![CDATA[默认分类]]></category>
<pubDate>Wed, 21 Sep 2011 14:04:43 +0000</pubDate> 
<guid>http://www.snooda.com/read/</guid> 
<description>
<![CDATA[ 
	    今天遇到lighttpd的一个奇异问题，幸亏高人指点，原来HTTP1.0下允许host为空，而1.1协议则要求host必须有值，否则返回400错误。<br />
<br />
    贴鸟哥帖子一篇：http://www.laruence.com/2008/08/28/483.html  写的很好，我就不重新轮一个帖子了。<br />
<br />
    昨天xuepeng师兄提出一个问题是 $_SERVER[&#039;HTTP_HOST&#039;]为空, 经过我翻看RFC文档以及测试,得出结论如下:<br />
在http 1.1中, host字段是不能为空的,如果为空, 服务器会认为是bad request<br />
<br />
但是在http 1.0中, host字段是可以为空的. 如:<br />
<br />
  &lt;?php<br />
$fp = fsockopen(&quot;localhost&quot;, 80, $errno, $errstr, 30);<br />
 <br />
$header =  &quot;GET /index.php&quot;;<br />
$header .= &quot; HTTP/1.0<br />
&quot;;<br />
 <br />
$header .= &quot;Connection:Close<br />
<br />
&quot;;<br />
fwrite($fp, $header);<br />
    echo fread($fp, 1024);<br />
fclose($fp);<br />
?&gt;<br />
其中,主机的index.php只是var_dump($_SERVER[&#039;HTTP_HOST&#039;]);<br />
<br />
可以看到,当你指明使用http 1.0协议的时候, 请求正常,返回结果是false;<br />
<br />
但是如果你指明协议是http 1.1 :<br />
<br />
&lt;?php<br />
$fp = fsockopen(&quot;localhost&quot;, 80, $errno, $errstr, 30);<br />
 <br />
$header =  &quot;GET /index.php&quot;;<br />
$header .= &quot; HTTP/1.1<br />
&quot;;<br />
 <br />
$header .= &quot;Connection:Close<br />
<br />
&quot;;<br />
fwrite($fp, $header);<br />
    echo fread($fp, 1024);<br />
fclose($fp);<br />
?&gt;<br />
则结果是400 bad request;<br />
<br />
究其原因是因为在HTTP1.0的时候, 并没有设想到现在有这么多服务器共用一个IP的情况(virtual host), 而在HTTP1.1的时候,加入了对多个HOST共用一个IP的支持.<br />
<br />
以下文字摘自RFC2616:<br />
<br />
14.23 Host<br />
<br />
The Host request-header field specifies the Internet host and port<br />
number of the resource being requested, as obtained from the original<br />
URI given by the user or referring resource (generally an HTTP URL,<br />
<br />
Fielding, et al. Standards Track [Page 128]<br />
<br />
RFC 2616 HTTP/1.1 June 1999<br />
<br />
as described in section 3.2.2). The Host field value MUST represent<br />
the naming authority of the origin server or gateway given by the<br />
original URL. This allows the origin server or gateway to<br />
differentiate between internally-ambiguous URLs, such as the root “/”<br />
URL of a server for multiple host names on a single IP address.<br />
<br />
Host = “Host” “:” host [ &quot;:&quot; port ] ; Section 3.2.2<br />
<br />
A “host” without any trailing port information implies the default<br />
port for the service requested (e.g., “80″ for an HTTP URL). For<br />
example, a request on the origin server for<br />
would properly include:<br />
<br />
GET /pub/WWW/ HTTP/1.1<br />
Host: www.w3.org<br />
<br />
A client MUST include a Host header field in all HTTP/1.1 request<br />
messages . If the requested URI does not include an Internet host<br />
name for the service being requested, then the Host header field MUST<br />
be given with an empty value. An HTTP/1.1 proxy MUST ensure that any<br />
request message it forwards does contain an appropriate Host header<br />
field that identifies the service being requested by the proxy. All<br />
Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request)<br />
status code to any HTTP/1.1 request message which lacks a Host header<br />
field.<br />
<br />
以下省略…..
]]>
</description>
</item><item>
<link>http://www.snooda.com/read/#blogcomment</link>
<title><![CDATA[[评论] HTTP1.0下HTTP_HOST允许为空]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.snooda.com/read/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>