HTTP 协议是否需要在文件路径中编码空间?

问题描述:

在HTTP协议中,

请求的第一行例如

GET /img/my house.jpg HTTP/1.1

我的问题是,如果文件名有空格,协议是否要求以某种方式对其进行编码?(如果协议不清楚,实际情况是什么?)

my question is, if the file name has a space, does the protocol require it to be encoded in some way? (and if the protocol isn't clear, what's the practical situation?)

您必须将其编码为 %20,这就是原因.首先,RFC 2616第 5.1.2 节...

You'll have to encode it as %20 and here is why. First, RFC 2616 Section 5.1.2...

Request-Line = Method SP Request-URI SP HTTP-Version CRLF

Request-Line = Method SP Request-URI SP HTTP-Version CRLF

还有……

请求-URI = "*" |绝对URI |abs_path |权威

Request-URI = "*" | absoluteURI | abs_path | authority

现在让我们跳到 RFC 1808,您会注意到缺少空格在 URL 和路径规范中...

Now let's hop over to RFC 1808 and you'll note the lack of spaces in the URL and path spec...

   URL         = ( absoluteURL | relativeURL ) [ "#" fragment ]

   absoluteURL = generic-RL | ( scheme ":" *( uchar | reserved ) )

   generic-RL  = scheme ":" relativeURL

   relativeURL = net_path | abs_path | rel_path

   net_path    = "//" net_loc [ abs_path ]    
   abs_path    = "/"  rel_path   
   rel_path    = [ path ] [ ";" params ] [ "?" query ]

   path        = fsegment *( "/" segment )    
   fsegment    = 1*pchar    
   segment     =  *pchar

   params      = param *( ";" param )  
   param       = *( pchar | "/" )

   scheme      = 1*( alpha | digit | "+" | "-" | "." )    
   net_loc     =  *( pchar | ";" | "?" )    
   query       =  *( uchar | reserved )    
   fragment    =  *( uchar | reserved )

   pchar       = uchar | ":" | "@" | "&" | "="    
   uchar       = unreserved | escape    
   unreserved  = alpha | digit | safe | extra

   escape      = "%" hex hex    
   hex         = digit | "A" | "B" | "C" | "D" | "E" | "F" |
                         "a" | "b" | "c" | "d" | "e" | "f"

   alpha       = lowalpha | hialpha    
   lowalpha    = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
                 "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
                 "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"    
   hialpha     = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
                 "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
                 "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"

   digit       = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
                 "8" | "9"

   safe        = "$" | "-" | "_" | "." | "+"    
   extra       = "!" | "*" | "'" | "(" | ")" | ","    
   national    = "{" | "}" | "|" | "\" | "^" | "~" | "[" | "]" | "`"    
   reserved    = ";" | "/" | "?" | ":" | "@" | "&" | "="    
   punctuation = "<" | ">" | "#" | "%" | <">