• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #ifndef LLHTTP__TEST
3 # include "llhttp.h"
4 #else
5 # define llhttp_t llparse_t
6 #endif  /* */
7 
8 int llhttp_message_needs_eof(const llhttp_t* parser);
9 int llhttp_should_keep_alive(const llhttp_t* parser);
10 
llhttp__before_headers_complete(llhttp_t * parser,const char * p,const char * endp)11 int llhttp__before_headers_complete(llhttp_t* parser, const char* p,
12                                     const char* endp) {
13   /* Set this here so that on_headers_complete() callbacks can see it */
14   if ((parser->flags & F_UPGRADE) &&
15       (parser->flags & F_CONNECTION_UPGRADE)) {
16     /* For responses, "Upgrade: foo" and "Connection: upgrade" are
17      * mandatory only when it is a 101 Switching Protocols response,
18      * otherwise it is purely informational, to announce support.
19      */
20     parser->upgrade =
21         (parser->type == HTTP_REQUEST || parser->status_code == 101);
22   } else {
23     parser->upgrade = (parser->method == HTTP_CONNECT);
24   }
25   return 0;
26 }
27 
28 
29 /* Return values:
30  * 0 - No body, `restart`, message_complete
31  * 1 - CONNECT request, `restart`, message_complete, and pause
32  * 2 - chunk_size_start
33  * 3 - body_identity
34  * 4 - body_identity_eof
35  * 5 - invalid transfer-encoding for request
36  */
llhttp__after_headers_complete(llhttp_t * parser,const char * p,const char * endp)37 int llhttp__after_headers_complete(llhttp_t* parser, const char* p,
38                                    const char* endp) {
39   int hasBody;
40 
41   hasBody = parser->flags & F_CHUNKED || parser->content_length > 0;
42   if (parser->upgrade && (parser->method == HTTP_CONNECT ||
43                           (parser->flags & F_SKIPBODY) || !hasBody)) {
44     /* Exit, the rest of the message is in a different protocol. */
45     return 1;
46   }
47 
48   if (parser->flags & F_SKIPBODY) {
49     return 0;
50   } else if (parser->flags & F_CHUNKED) {
51     /* chunked encoding - ignore Content-Length header, prepare for a chunk */
52     return 2;
53   } else if (parser->flags & F_TRANSFER_ENCODING) {
54     if (parser->type == HTTP_REQUEST &&
55         (parser->lenient_flags & LENIENT_CHUNKED_LENGTH) == 0 &&
56         (parser->lenient_flags & LENIENT_TRANSFER_ENCODING) == 0) {
57       /* RFC 7230 3.3.3 */
58 
59       /* If a Transfer-Encoding header field
60        * is present in a request and the chunked transfer coding is not
61        * the final encoding, the message body length cannot be determined
62        * reliably; the server MUST respond with the 400 (Bad Request)
63        * status code and then close the connection.
64        */
65       return 5;
66     } else {
67       /* RFC 7230 3.3.3 */
68 
69       /* If a Transfer-Encoding header field is present in a response and
70        * the chunked transfer coding is not the final encoding, the
71        * message body length is determined by reading the connection until
72        * it is closed by the server.
73        */
74       return 4;
75     }
76   } else {
77     if (!(parser->flags & F_CONTENT_LENGTH)) {
78       if (!llhttp_message_needs_eof(parser)) {
79         /* Assume content-length 0 - read the next */
80         return 0;
81       } else {
82         /* Read body until EOF */
83         return 4;
84       }
85     } else if (parser->content_length == 0) {
86       /* Content-Length header given but zero: Content-Length: 0\r\n */
87       return 0;
88     } else {
89       /* Content-Length header given and non-zero */
90       return 3;
91     }
92   }
93 }
94 
95 
llhttp__after_message_complete(llhttp_t * parser,const char * p,const char * endp)96 int llhttp__after_message_complete(llhttp_t* parser, const char* p,
97                                    const char* endp) {
98   int should_keep_alive;
99 
100   should_keep_alive = llhttp_should_keep_alive(parser);
101   parser->finish = HTTP_FINISH_SAFE;
102   parser->flags = 0;
103 
104   /* NOTE: this is ignored in loose parsing mode */
105   return should_keep_alive;
106 }
107 
108 
llhttp_message_needs_eof(const llhttp_t * parser)109 int llhttp_message_needs_eof(const llhttp_t* parser) {
110   if (parser->type == HTTP_REQUEST) {
111     return 0;
112   }
113 
114   /* See RFC 2616 section 4.4 */
115   if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */
116       parser->status_code == 204 ||     /* No Content */
117       parser->status_code == 304 ||     /* Not Modified */
118       (parser->flags & F_SKIPBODY)) {     /* response to a HEAD request */
119     return 0;
120   }
121 
122   /* RFC 7230 3.3.3, see `llhttp__after_headers_complete` */
123   if ((parser->flags & F_TRANSFER_ENCODING) &&
124       (parser->flags & F_CHUNKED) == 0) {
125     return 1;
126   }
127 
128   if (parser->flags & (F_CHUNKED | F_CONTENT_LENGTH)) {
129     return 0;
130   }
131 
132   return 1;
133 }
134 
135 
llhttp_should_keep_alive(const llhttp_t * parser)136 int llhttp_should_keep_alive(const llhttp_t* parser) {
137   if (parser->http_major > 0 && parser->http_minor > 0) {
138     /* HTTP/1.1 */
139     if (parser->flags & F_CONNECTION_CLOSE) {
140       return 0;
141     }
142   } else {
143     /* HTTP/1.0 or earlier */
144     if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) {
145       return 0;
146     }
147   }
148 
149   return !llhttp_message_needs_eof(parser);
150 }
151