问题
in the HTTP protocol,
the first line of requsest is like for example
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?)
回答1:
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
and...
Request-URI = "*" | absoluteURI | abs_path | authority
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 = "<" | ">" | "#" | "%" | <">
回答2:
Yes, it needs to be URL-encoded, i.e.
GET /img/my%20house.jpg HTTP/1.1
where %20 represents the hex value of the character in question
P.S. Didn't you already ask this?
回答3:
yes.
The spec defines the request line as follows:Request-Line   = Method SP Request-URI SP HTTP-Version CRLF
Note that space is a delimiter in the request line.
回答4:
Yes.  %20.  See RFC 1738 for details.
You should bear in mind that the path component of an HTTP URI is not a "file path". All it is is a hierarchical path to a resource, it need not correspond to any file on the server.
回答5:
A space needs to be encoded as %20 , the rules for encoding is documented in RFC3986. You're usually best off having a library taking care of the url encoding.
来源:https://stackoverflow.com/questions/4673809/does-http-protocol-require-space-be-encoded-in-file-path