Apply PIE to nghttpx
[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 && (parser->flags & F_LENIENT) == 0) {
55       /* RFC 7230 3.3.3 */
56
57       /* If a Transfer-Encoding header field
58        * is present in a request and the chunked transfer coding is not
59        * the final encoding, the message body length cannot be determined
60        * reliably; the server MUST respond with the 400 (Bad Request)
61        * status code and then close the connection.
62        */
63       return 5;
64     } else {
65       /* RFC 7230 3.3.3 */
66
67       /* If a Transfer-Encoding header field is present in a response and
68        * the chunked transfer coding is not the final encoding, the
69        * message body length is determined by reading the connection until
70        * it is closed by the server.
71        */
72       return 4;
73     }
74   } else {
75     if (!(parser->flags & F_CONTENT_LENGTH)) {
76       if (!llhttp_message_needs_eof(parser)) {
77         /* Assume content-length 0 - read the next */
78         return 0;
79       } else {
80         /* Read body until EOF */
81         return 4;
82       }
83     } else if (parser->content_length == 0) {
84       /* Content-Length header given but zero: Content-Length: 0\r\n */
85       return 0;
86     } else {
87       /* Content-Length header given and non-zero */
88       return 3;
89     }
90   }
91 }
92
93
94 int llhttp__after_message_complete(llhttp_t* parser, const char* p,
95                                    const char* endp) {
96   int should_keep_alive;
97
98   should_keep_alive = llhttp_should_keep_alive(parser);
99   parser->finish = HTTP_FINISH_SAFE;
100
101   /* Keep `F_LENIENT` flag between messages, but reset every other flag */
102   parser->flags &= F_LENIENT;
103
104   /* NOTE: this is ignored in loose parsing mode */
105   return should_keep_alive;
106 }
107
108
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
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 }