`
hideto
  • 浏览: 2649598 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

HTTP1.1 Notes 4, Request & Response & Entity

阅读更多
Request
        Request       = Request-Line             
                        *(( general-header       
                         | request-header        
                         | entity-header ) CRLF) 
                        CRLF
                        [ message-body ]
Request-Line
        Request-Line   = Method SP Request-URI SP HTTP-Version CRLF

An example Request-Line would be:

       GET http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1

The Method token indicates the method to be performed on the resource identified by the Request-URI
The method is case-sensitive

       Method         = "OPTIONS"               
                      | "GET"                   
                      | "HEAD"                  
                      | "POST"                  
                      | "PUT"                   
                      | "DELETE"                
                      | "TRACE"                 
                      | "CONNECT"               
                      | extension-method
       extension-method = token

The methods GET and HEAD MUST be supported by all general-purpose servers. All other methods are OPTIONAL
An origin server SHOULD return the status code 405 (Method Not Allowed) if the method is known by the origin server but not allowed for the requested resource, and 501 (Not Implemented) if the method is unrecognized or not implemented by the origin server

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

The asterisk "*" means that the request does not apply to a particular resource, but to the server itself, and is only allowed when the method used does not necessarily apply to a resource

The Resource Identified by a Request
The exact resource identified by an Internet request is determined by examining both the Request-URI and the Host header field
1. If Request-URI is an absoluteURI, the host is part of the Request-URI. Any Host header field value in the request MUST be ignored.

2. If the Request-URI is not an absoluteURI, and the request includes a Host header field, the host is determined by the Host header field value.

3. If the host as determined by rule 1 or 2 is not a valid host on the server, the response MUST be a 400 (Bad Request) error message.

Request Header Fields
       request-header = Accept                  
                      | Accept-Charset          
                      | Accept-Encoding         
                      | Accept-Language         
                      | Authorization           
                      | Expect                  
                      | From                    
                      | Host                    
                      | If-Match                

                      | If-Modified-Since       
                      | If-None-Match           
                      | If-Range                
                      | If-Unmodified-Since     
                      | Max-Forwards            
                      | Proxy-Authorization     
                      | Range                   
                      | Referer                 
                      | TE                      
                      | User-Agent          

                      | Range                   
                      | Referer                 
                      | TE                      
                      | User-Agent              
New or experimental header fields MAY be given the semantics of request- header fields if all parties in the communication recognize them to be request-header fields
Unrecognized header fields are treated as entity-header fields

Response
       Response      = Status-Line              
                       *(( general-header       
                        | response-header       
                        | entity-header ) CRLF) 
                       CRLF
                       [ message-body ]

Status-Line
       Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF

The first digit of the Status-Code defines the class of response
There are 5 values for the first digit:
      - 1xx: Informational - Request received, continuing process

      - 2xx: Success - The action was successfully received,
        understood, and accepted

      - 3xx: Redirection - Further action must be taken in order to
        complete the request

      - 4xx: Client Error - The request contains bad syntax or cannot
        be fulfilled

      - 5xx: Server Error - The server failed to fulfill an apparently
        valid request

      Status-Code    =
            "100"  ; Continue
          | "101"  ; Switching Protocols

          | "200"  ; OK
          | "201"  ; Created
          | "202"  ; Accepted
          | "203"  ; Non-Authoritative Information
          | "204"  ; No Content
          | "205"  ; Reset Content
          | "206"  ; Partial Content

          | "300"  ; Multiple Choices
          | "301"  ; Moved Permanently
          | "302"  ; Found
          | "303"  ; See Other
          | "304"  ; Not Modified
          | "305"  ; Use Proxy
          | "307"  ; Temporary Redirect

          | "400"  ; Bad Request
          | "401"  ; Unauthorized
          | "402"  ; Payment Required
          | "403"  ; Forbidden
          | "404"  ; Not Found
          | "405"  ; Method Not Allowed
          | "406"  ; Not Acceptable
          | "407"  ; Proxy Authentication Required
          | "408"  ; Request Time-out
          | "409"  ; Conflict
          | "410"  ; Gone
          | "411"  ; Length Required
          | "412"  ; Precondition Failed
          | "413"  ; Request Entity Too Large
          | "414"  ; Request-URI Too Large
          | "415"  ; Unsupported Media Type
          | "416"  ; Requested range not satisfiable
          | "417"  ; Expectation Failed

          | "500"  ; Internal Server Error
          | "501"  ; Not Implemented
          | "502"  ; Bad Gateway
          | "503"  ; Service Unavailable
          | "504"  ; Gateway Time-out
          | "505"  ; HTTP Version not supported
          | extension-code

      extension-code = 3DIGIT
      Reason-Phrase  = *<TEXT, excluding CR, LF>

pplications MUST understand the class of any status code, as indicated by the first digit, and treat any unrecognized response as being equivalent to the x00 status code of that class, with the exception that an unrecognized response MUST NOT be cached

Response Header Fields
       response-header = Accept-Ranges          
                       | Age                    
                       | ETag                   
                       | Location               
                       | Proxy-Authenticate     
                       | Retry-After            
                       | Server                 
                       | Vary                   
                       | WWW-Authenticate

Entity
Request and Response messages MAY transfer an entity if not otherwise restricted by the request method or response status code
An entity consists of entity-header fields and an entity-body, although some responses will only include the entity-headers

Entity Header Fields
Entity-header fields define metainformation about the entity-body or, if no body is present, about the resource identified by the request

       entity-header  = Allow                   
                      | Content-Encoding        
                      | Content-Language        
                      | Content-Length          
                      | Content-Location        
                      | Content-MD5             
                      | Content-Range           
                      | Content-Type            
                      | Expires                 
                      | Last-Modified           
                      | extension-header

       extension-header = message-header

Entity Body
The entity-body (if any) sent with an HTTP request or response is in a format and encoding defined by the entity-header fields

       entity-body    = *OCTET

An entity-body is only present in a message when a message-body is present
When an entity-body is included with a message, the data type of that body is determined via the header fields Content-Type and Content- Encoding
These define a two-layer, ordered encoding model:

       entity-body := Content-Encoding( Content-Type( data ) )

Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body
If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource
If the media type remains unknown, the recipient SHOULD treat it as type "application/octet-stream"

The entity-length of a message is the length of the message-body before any transfer-codings have been applied

Examples
D:\projects\maui>curl -I www.g.cn
HTTP/1.1 301 Moved Permanently
Location: http://www.google.cn/webhp?source=g_cn
Content-Type: text/html; charset=UTF-8
Date: Fri, 26 Dec 2008 05:07:21 GMT
Expires: Sun, 25 Jan 2009 05:07:20 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 0

D:\projects\maui>curl -I www.google.cn
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Date: Fri, 26 Dec 2008 05:07:28 GMT
Expires: -1
Content-Type: text/html; charset=GB2312
Set-Cookie: PREF=ID=fc9dd9293e07dfbd:NW=1:TM=1230268048:LM=1230268048:S=GSJ0ovAC
pSDvYdtq; expires=Sun, 26-Dec-2010 05:07:28 GMT; path=/; domain=.google.cn
Server: gws
Content-Length: 0

D:\projects\maui>curl -I 192.168.0.5
HTTP/1.1 302 Found
Content-Type: text/html; charset=utf-8
Set-Cookie: _freewheel_session_id=6f190d42bf361e23336c7b646a565c2d; path=/; Http
Only
Location: http://192.168.0.5/system/account/login
Cache-Control: no-cache
Content-Length: 105
Date: Fri, 26 Dec 2008 05:27:16 GMT
Server: lighttpd/1.4.19
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics