Upgrade to 1.46.0
[platform/upstream/nghttp2.git] / third-party / llhttp / src / http.c
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
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  */
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       /* RFC 7230 3.3.3 */
57
58       /* If a Transfer-Encoding header field
59        * is present in a request and the chunked transfer coding is not
60        * the final encoding, the message body length cannot be determined
61        * reliably; the server MUST respond with the 400 (Bad Request)
62        * status code and then close the connection.
63        */
64       return 5;
65     } else {
66       /* RFC 7230 3.3.3 */
67
68       /* If a Transfer-Encoding header field is present in a response and
69        * the chunked transfer coding is not the final encoding, the
70        * message body length is determined by reading the connection until
71        * it is closed by the server.
72        */
73       return 4;
74     }
75   } else {
76     if (!(parser->flags & F_CONTENT_LENGTH)) {
77       if (!llhttp_message_needs_eof(parser)) {
78         /* Assume content-length 0 - read the next */
79         return 0;
80       } else {
81         /* Read body until EOF */
82         return 4;
83       }
84     } else if (parser->content_length == 0) {
85       /* Content-Length header given but zero: Content-Length: 0\r\n */
86       return 0;
87     } else {
88       /* Content-Length header given and non-zero */
89       return 3;
90     }
91   }
92 }
93
94
95 int llhttp__after_message_complete(llhttp_t* parser, const char* p,
96                                    const char* endp) {
97   int should_keep_alive;
98
99   should_keep_alive = llhttp_should_keep_alive(parser);
100   parser->finish = HTTP_FINISH_SAFE;
101   parser->flags = 0;
102
103   /* NOTE: this is ignored in loose parsing mode */
104   return should_keep_alive;
105 }
106
107
108 int llhttp_message_needs_eof(const llhttp_t* parser) {
109   if (parser->type == HTTP_REQUEST) {
110     return 0;
111   }
112
113   /* See RFC 2616 section 4.4 */
114   if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */
115       parser->status_code == 204 ||     /* No Content */
116       parser->status_code == 304 ||     /* Not Modified */
117       (parser->flags & F_SKIPBODY)) {     /* response to a HEAD request */
118     return 0;
119   }
120
121   /* RFC 7230 3.3.3, see `llhttp__after_headers_complete` */
122   if ((parser->flags & F_TRANSFER_ENCODING) &&
123       (parser->flags & F_CHUNKED) == 0) {
124     return 1;
125   }
126
127   if (parser->flags & (F_CHUNKED | F_CONTENT_LENGTH)) {
128     return 0;
129   }
130
131   return 1;
132 }
133
134
135 int llhttp_should_keep_alive(const llhttp_t* parser) {
136   if (parser->http_major > 0 && parser->http_minor > 0) {
137     /* HTTP/1.1 */
138     if (parser->flags & F_CONNECTION_CLOSE) {
139       return 0;
140     }
141   } else {
142     /* HTTP/1.0 or earlier */
143     if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) {
144       return 0;
145     }
146   }
147
148   return !llhttp_message_needs_eof(parser);
149 }