xref: /unit/src/nxt_h1proto.c (revision 2505:450bf52c3a2c)
1431Sigor@sysoev.ru 
2431Sigor@sysoev.ru /*
3431Sigor@sysoev.ru  * Copyright (C) Igor Sysoev
4431Sigor@sysoev.ru  * Copyright (C) NGINX, Inc.
5431Sigor@sysoev.ru  */
6431Sigor@sysoev.ru 
7431Sigor@sysoev.ru #include <nxt_router.h>
8431Sigor@sysoev.ru #include <nxt_http.h>
91394Sigor@sysoev.ru #include <nxt_upstream.h>
101131Smax.romanov@nginx.com #include <nxt_h1proto.h>
111131Smax.romanov@nginx.com #include <nxt_websocket.h>
121131Smax.romanov@nginx.com #include <nxt_websocket_header.h>
131113Sigor@sysoev.ru 
141113Sigor@sysoev.ru 
15624Sigor@sysoev.ru /*
16771Sigor@sysoev.ru  * nxt_http_conn_ and nxt_h1p_conn_ prefixes are used for connection handlers.
17771Sigor@sysoev.ru  * nxt_h1p_idle_ prefix is used for idle connection handlers.
18624Sigor@sysoev.ru  * nxt_h1p_request_ prefix is used for HTTP/1 protocol request methods.
19624Sigor@sysoev.ru  */
20624Sigor@sysoev.ru 
21771Sigor@sysoev.ru #if (NXT_TLS)
221268Sigor@sysoev.ru static ssize_t nxt_http_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c);
23771Sigor@sysoev.ru static void nxt_http_conn_test(nxt_task_t *task, void *obj, void *data);
24771Sigor@sysoev.ru #endif
251268Sigor@sysoev.ru static ssize_t nxt_h1p_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c);
26624Sigor@sysoev.ru static void nxt_h1p_conn_proto_init(nxt_task_t *task, void *obj, void *data);
27624Sigor@sysoev.ru static void nxt_h1p_conn_request_init(nxt_task_t *task, void *obj, void *data);
28683Sigor@sysoev.ru static void nxt_h1p_conn_request_header_parse(nxt_task_t *task, void *obj,
29683Sigor@sysoev.ru     void *data);
301131Smax.romanov@nginx.com static nxt_int_t nxt_h1p_header_process(nxt_task_t *task, nxt_h1proto_t *h1p,
31620Svbart@nginx.com     nxt_http_request_t *r);
32625Sigor@sysoev.ru static nxt_int_t nxt_h1p_header_buffer_test(nxt_task_t *task,
33625Sigor@sysoev.ru     nxt_h1proto_t *h1p, nxt_conn_t *c, nxt_socket_conf_t *skcf);
34431Sigor@sysoev.ru static nxt_int_t nxt_h1p_connection(void *ctx, nxt_http_field_t *field,
35431Sigor@sysoev.ru     uintptr_t data);
361131Smax.romanov@nginx.com static nxt_int_t nxt_h1p_upgrade(void *ctx, nxt_http_field_t *field,
371131Smax.romanov@nginx.com     uintptr_t data);
381131Smax.romanov@nginx.com static nxt_int_t nxt_h1p_websocket_key(void *ctx, nxt_http_field_t *field,
391131Smax.romanov@nginx.com     uintptr_t data);
401131Smax.romanov@nginx.com static nxt_int_t nxt_h1p_websocket_version(void *ctx, nxt_http_field_t *field,
411131Smax.romanov@nginx.com     uintptr_t data);
42431Sigor@sysoev.ru static nxt_int_t nxt_h1p_transfer_encoding(void *ctx, nxt_http_field_t *field,
43431Sigor@sysoev.ru     uintptr_t data);
44431Sigor@sysoev.ru static void nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r);
45637Sigor@sysoev.ru static void nxt_h1p_conn_request_body_read(nxt_task_t *task, void *obj,
46637Sigor@sysoev.ru     void *data);
47431Sigor@sysoev.ru static void nxt_h1p_request_local_addr(nxt_task_t *task, nxt_http_request_t *r);
48431Sigor@sysoev.ru static void nxt_h1p_request_header_send(nxt_task_t *task,
491270Sigor@sysoev.ru     nxt_http_request_t *r, nxt_work_handler_t body_handler, void *data);
50431Sigor@sysoev.ru static void nxt_h1p_request_send(nxt_task_t *task, nxt_http_request_t *r,
51431Sigor@sysoev.ru     nxt_buf_t *out);
52431Sigor@sysoev.ru static nxt_buf_t *nxt_h1p_chunk_create(nxt_task_t *task, nxt_http_request_t *r,
53431Sigor@sysoev.ru     nxt_buf_t *out);
54630Svbart@nginx.com static nxt_off_t nxt_h1p_request_body_bytes_sent(nxt_task_t *task,
55630Svbart@nginx.com     nxt_http_proto_t proto);
56608Sigor@sysoev.ru static void nxt_h1p_request_discard(nxt_task_t *task, nxt_http_request_t *r,
57608Sigor@sysoev.ru     nxt_buf_t *last);
58624Sigor@sysoev.ru static void nxt_h1p_conn_request_error(nxt_task_t *task, void *obj, void *data);
59624Sigor@sysoev.ru static void nxt_h1p_conn_request_timeout(nxt_task_t *task, void *obj,
60624Sigor@sysoev.ru     void *data);
61626Sigor@sysoev.ru static void nxt_h1p_conn_request_send_timeout(nxt_task_t *task, void *obj,
62626Sigor@sysoev.ru     void *data);
63725Sigor@sysoev.ru nxt_inline void nxt_h1p_request_error(nxt_task_t *task, nxt_h1proto_t *h1p,
64725Sigor@sysoev.ru     nxt_http_request_t *r);
65771Sigor@sysoev.ru static void nxt_h1p_request_close(nxt_task_t *task, nxt_http_proto_t proto,
66771Sigor@sysoev.ru     nxt_socket_conf_joint_t *joint);
67771Sigor@sysoev.ru static void nxt_h1p_conn_sent(nxt_task_t *task, void *obj, void *data);
68771Sigor@sysoev.ru static void nxt_h1p_conn_close(nxt_task_t *task, void *obj, void *data);
69771Sigor@sysoev.ru static void nxt_h1p_conn_error(nxt_task_t *task, void *obj, void *data);
70771Sigor@sysoev.ru static nxt_msec_t nxt_h1p_conn_timer_value(nxt_conn_t *c, uintptr_t data);
71771Sigor@sysoev.ru static void nxt_h1p_keepalive(nxt_task_t *task, nxt_h1proto_t *h1p,
72771Sigor@sysoev.ru     nxt_conn_t *c);
73771Sigor@sysoev.ru static void nxt_h1p_idle_close(nxt_task_t *task, void *obj, void *data);
74771Sigor@sysoev.ru static void nxt_h1p_idle_timeout(nxt_task_t *task, void *obj, void *data);
75771Sigor@sysoev.ru static void nxt_h1p_idle_response(nxt_task_t *task, nxt_conn_t *c);
76771Sigor@sysoev.ru static void nxt_h1p_idle_response_sent(nxt_task_t *task, void *obj, void *data);
771457Sigor@sysoev.ru static void nxt_h1p_idle_response_error(nxt_task_t *task, void *obj,
781457Sigor@sysoev.ru     void *data);
79771Sigor@sysoev.ru static void nxt_h1p_idle_response_timeout(nxt_task_t *task, void *obj,
80771Sigor@sysoev.ru     void *data);
81771Sigor@sysoev.ru static nxt_msec_t nxt_h1p_idle_response_timer_value(nxt_conn_t *c,
82771Sigor@sysoev.ru     uintptr_t data);
83771Sigor@sysoev.ru static void nxt_h1p_shutdown(nxt_task_t *task, nxt_conn_t *c);
841270Sigor@sysoev.ru static void nxt_h1p_closing(nxt_task_t *task, nxt_conn_t *c);
851131Smax.romanov@nginx.com static void nxt_h1p_conn_ws_shutdown(nxt_task_t *task, void *obj, void *data);
86771Sigor@sysoev.ru static void nxt_h1p_conn_closing(nxt_task_t *task, void *obj, void *data);
87771Sigor@sysoev.ru static void nxt_h1p_conn_free(nxt_task_t *task, void *obj, void *data);
88431Sigor@sysoev.ru 
891270Sigor@sysoev.ru static void nxt_h1p_peer_connect(nxt_task_t *task, nxt_http_peer_t *peer);
901270Sigor@sysoev.ru static void nxt_h1p_peer_connected(nxt_task_t *task, void *obj, void *data);
911270Sigor@sysoev.ru static void nxt_h1p_peer_refused(nxt_task_t *task, void *obj, void *data);
921270Sigor@sysoev.ru static void nxt_h1p_peer_header_send(nxt_task_t *task, nxt_http_peer_t *peer);
931270Sigor@sysoev.ru static void nxt_h1p_peer_header_sent(nxt_task_t *task, void *obj, void *data);
941270Sigor@sysoev.ru static void nxt_h1p_peer_header_read(nxt_task_t *task, nxt_http_peer_t *peer);
951270Sigor@sysoev.ru static ssize_t nxt_h1p_peer_io_read_handler(nxt_task_t *task, nxt_conn_t *c);
961270Sigor@sysoev.ru static void nxt_h1p_peer_header_read_done(nxt_task_t *task, void *obj,
971270Sigor@sysoev.ru     void *data);
981270Sigor@sysoev.ru static nxt_int_t nxt_h1p_peer_header_parse(nxt_http_peer_t *peer,
991270Sigor@sysoev.ru     nxt_buf_mem_t *bm);
1001270Sigor@sysoev.ru static void nxt_h1p_peer_read(nxt_task_t *task, nxt_http_peer_t *peer);
1011270Sigor@sysoev.ru static void nxt_h1p_peer_read_done(nxt_task_t *task, void *obj, void *data);
1021505Sigor@sysoev.ru static void nxt_h1p_peer_body_process(nxt_task_t *task, nxt_http_peer_t *peer, nxt_buf_t *out);
1031270Sigor@sysoev.ru static void nxt_h1p_peer_closed(nxt_task_t *task, void *obj, void *data);
1041270Sigor@sysoev.ru static void nxt_h1p_peer_error(nxt_task_t *task, void *obj, void *data);
1051270Sigor@sysoev.ru static void nxt_h1p_peer_send_timeout(nxt_task_t *task, void *obj, void *data);
1061270Sigor@sysoev.ru static void nxt_h1p_peer_read_timeout(nxt_task_t *task, void *obj, void *data);
1071270Sigor@sysoev.ru static nxt_msec_t nxt_h1p_peer_timer_value(nxt_conn_t *c, uintptr_t data);
1081270Sigor@sysoev.ru static void nxt_h1p_peer_close(nxt_task_t *task, nxt_http_peer_t *peer);
1091270Sigor@sysoev.ru static void nxt_h1p_peer_free(nxt_task_t *task, void *obj, void *data);
1101505Sigor@sysoev.ru static nxt_int_t nxt_h1p_peer_transfer_encoding(void *ctx,
1111505Sigor@sysoev.ru     nxt_http_field_t *field, uintptr_t data);
1121270Sigor@sysoev.ru 
113771Sigor@sysoev.ru #if (NXT_TLS)
114771Sigor@sysoev.ru static const nxt_conn_state_t  nxt_http_idle_state;
115771Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_shutdown_state;
116771Sigor@sysoev.ru #endif
117431Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_idle_state;
118624Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_header_parse_state;
119431Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_read_body_state;
120740Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_request_send_state;
121771Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_timeout_response_state;
122624Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_keepalive_state;
123683Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_close_state;
1241270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_connect_state;
1251270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_header_send_state;
1261270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_header_body_send_state;
1271270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_header_read_state;
1281270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_header_read_timer_state;
1291270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_read_state;
1301270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_close_state;
131431Sigor@sysoev.ru 
132431Sigor@sysoev.ru 
1331112Sigor@sysoev.ru const nxt_http_proto_table_t  nxt_http_proto[3] = {
1341112Sigor@sysoev.ru     /* NXT_HTTP_PROTO_H1 */
1351112Sigor@sysoev.ru     {
1361112Sigor@sysoev.ru         .body_read        = nxt_h1p_request_body_read,
1371112Sigor@sysoev.ru         .local_addr       = nxt_h1p_request_local_addr,
1381112Sigor@sysoev.ru         .header_send      = nxt_h1p_request_header_send,
1391112Sigor@sysoev.ru         .send             = nxt_h1p_request_send,
1401112Sigor@sysoev.ru         .body_bytes_sent  = nxt_h1p_request_body_bytes_sent,
1411112Sigor@sysoev.ru         .discard          = nxt_h1p_request_discard,
1421112Sigor@sysoev.ru         .close            = nxt_h1p_request_close,
1431270Sigor@sysoev.ru 
1441270Sigor@sysoev.ru         .peer_connect     = nxt_h1p_peer_connect,
1451270Sigor@sysoev.ru         .peer_header_send = nxt_h1p_peer_header_send,
1461270Sigor@sysoev.ru         .peer_header_read = nxt_h1p_peer_header_read,
1471270Sigor@sysoev.ru         .peer_read        = nxt_h1p_peer_read,
1481270Sigor@sysoev.ru         .peer_close       = nxt_h1p_peer_close,
1491270Sigor@sysoev.ru 
1501131Smax.romanov@nginx.com         .ws_frame_start   = nxt_h1p_websocket_frame_start,
1511112Sigor@sysoev.ru     },
1521112Sigor@sysoev.ru     /* NXT_HTTP_PROTO_H2      */
1531112Sigor@sysoev.ru     /* NXT_HTTP_PROTO_DEVNULL */
154431Sigor@sysoev.ru };
155431Sigor@sysoev.ru 
156431Sigor@sysoev.ru 
1571270Sigor@sysoev.ru static nxt_lvlhsh_t                    nxt_h1p_fields_hash;
1581270Sigor@sysoev.ru 
1591270Sigor@sysoev.ru static nxt_http_field_proc_t           nxt_h1p_fields[] = {
160431Sigor@sysoev.ru     { nxt_string("Connection"),        &nxt_h1p_connection, 0 },
1611131Smax.romanov@nginx.com     { nxt_string("Upgrade"),           &nxt_h1p_upgrade, 0 },
1621131Smax.romanov@nginx.com     { nxt_string("Sec-WebSocket-Key"), &nxt_h1p_websocket_key, 0 },
1631131Smax.romanov@nginx.com     { nxt_string("Sec-WebSocket-Version"),
1641131Smax.romanov@nginx.com                                        &nxt_h1p_websocket_version, 0 },
165431Sigor@sysoev.ru     { nxt_string("Transfer-Encoding"), &nxt_h1p_transfer_encoding, 0 },
166431Sigor@sysoev.ru 
167431Sigor@sysoev.ru     { nxt_string("Host"),              &nxt_http_request_host, 0 },
168431Sigor@sysoev.ru     { nxt_string("Cookie"),            &nxt_http_request_field,
169431Sigor@sysoev.ru         offsetof(nxt_http_request_t, cookie) },
170630Svbart@nginx.com     { nxt_string("Referer"),           &nxt_http_request_field,
171630Svbart@nginx.com         offsetof(nxt_http_request_t, referer) },
172630Svbart@nginx.com     { nxt_string("User-Agent"),        &nxt_http_request_field,
173630Svbart@nginx.com         offsetof(nxt_http_request_t, user_agent) },
174431Sigor@sysoev.ru     { nxt_string("Content-Type"),      &nxt_http_request_field,
175431Sigor@sysoev.ru         offsetof(nxt_http_request_t, content_type) },
176431Sigor@sysoev.ru     { nxt_string("Content-Length"),    &nxt_http_request_content_length, 0 },
1771733Svbart@nginx.com     { nxt_string("Authorization"),     &nxt_http_request_field,
1781733Svbart@nginx.com         offsetof(nxt_http_request_t, authorization) },
179431Sigor@sysoev.ru };
180431Sigor@sysoev.ru 
181431Sigor@sysoev.ru 
1821270Sigor@sysoev.ru static nxt_lvlhsh_t                    nxt_h1p_peer_fields_hash;
1831270Sigor@sysoev.ru 
1841270Sigor@sysoev.ru static nxt_http_field_proc_t           nxt_h1p_peer_fields[] = {
1851270Sigor@sysoev.ru     { nxt_string("Connection"),        &nxt_http_proxy_skip, 0 },
1861505Sigor@sysoev.ru     { nxt_string("Transfer-Encoding"), &nxt_h1p_peer_transfer_encoding, 0 },
1871270Sigor@sysoev.ru     { nxt_string("Server"),            &nxt_http_proxy_skip, 0 },
1881270Sigor@sysoev.ru     { nxt_string("Date"),              &nxt_http_proxy_date, 0 },
1891270Sigor@sysoev.ru     { nxt_string("Content-Length"),    &nxt_http_proxy_content_length, 0 },
1901270Sigor@sysoev.ru };
1911270Sigor@sysoev.ru 
1921270Sigor@sysoev.ru 
193431Sigor@sysoev.ru nxt_int_t
nxt_h1p_init(nxt_task_t * task)1941459Smax.romanov@nginx.com nxt_h1p_init(nxt_task_t *task)
195431Sigor@sysoev.ru {
1961270Sigor@sysoev.ru     nxt_int_t  ret;
1971270Sigor@sysoev.ru 
1981459Smax.romanov@nginx.com     ret = nxt_http_fields_hash(&nxt_h1p_fields_hash,
1991270Sigor@sysoev.ru                                nxt_h1p_fields, nxt_nitems(nxt_h1p_fields));
2001270Sigor@sysoev.ru 
2011270Sigor@sysoev.ru     if (nxt_fast_path(ret == NXT_OK)) {
2021270Sigor@sysoev.ru         ret = nxt_http_fields_hash(&nxt_h1p_peer_fields_hash,
2031459Smax.romanov@nginx.com                                    nxt_h1p_peer_fields,
2041270Sigor@sysoev.ru                                    nxt_nitems(nxt_h1p_peer_fields));
2051270Sigor@sysoev.ru     }
2061270Sigor@sysoev.ru 
2071270Sigor@sysoev.ru     return ret;
208431Sigor@sysoev.ru }
209431Sigor@sysoev.ru 
210431Sigor@sysoev.ru 
211431Sigor@sysoev.ru void
nxt_http_conn_init(nxt_task_t * task,void * obj,void * data)212431Sigor@sysoev.ru nxt_http_conn_init(nxt_task_t *task, void *obj, void *data)
213431Sigor@sysoev.ru {
214431Sigor@sysoev.ru     nxt_conn_t               *c;
215431Sigor@sysoev.ru     nxt_socket_conf_t        *skcf;
216431Sigor@sysoev.ru     nxt_event_engine_t       *engine;
217683Sigor@sysoev.ru     nxt_listen_event_t       *lev;
218431Sigor@sysoev.ru     nxt_socket_conf_joint_t  *joint;
219431Sigor@sysoev.ru 
220431Sigor@sysoev.ru     c = obj;
221683Sigor@sysoev.ru     lev = data;
222431Sigor@sysoev.ru 
223431Sigor@sysoev.ru     nxt_debug(task, "http conn init");
224431Sigor@sysoev.ru 
225683Sigor@sysoev.ru     joint = lev->socket.data;
226431Sigor@sysoev.ru     skcf = joint->socket_conf;
227431Sigor@sysoev.ru     c->local = skcf->sockaddr;
228431Sigor@sysoev.ru 
229431Sigor@sysoev.ru     engine = task->thread->engine;
230431Sigor@sysoev.ru     c->read_work_queue = &engine->fast_work_queue;
231431Sigor@sysoev.ru     c->write_work_queue = &engine->fast_work_queue;
232431Sigor@sysoev.ru 
233431Sigor@sysoev.ru     c->read_state = &nxt_h1p_idle_state;
234431Sigor@sysoev.ru 
235771Sigor@sysoev.ru #if (NXT_TLS)
236771Sigor@sysoev.ru     if (skcf->tls != NULL) {
237771Sigor@sysoev.ru         c->read_state = &nxt_http_idle_state;
238771Sigor@sysoev.ru     }
239771Sigor@sysoev.ru #endif
240771Sigor@sysoev.ru 
241637Sigor@sysoev.ru     nxt_conn_read(engine, c);
242431Sigor@sysoev.ru }
243431Sigor@sysoev.ru 
244431Sigor@sysoev.ru 
245771Sigor@sysoev.ru #if (NXT_TLS)
246771Sigor@sysoev.ru 
247771Sigor@sysoev.ru static const nxt_conn_state_t  nxt_http_idle_state
248431Sigor@sysoev.ru     nxt_aligned(64) =
249431Sigor@sysoev.ru {
250771Sigor@sysoev.ru     .ready_handler = nxt_http_conn_test,
251771Sigor@sysoev.ru     .close_handler = nxt_h1p_conn_close,
252431Sigor@sysoev.ru     .error_handler = nxt_h1p_conn_error,
253431Sigor@sysoev.ru 
254771Sigor@sysoev.ru     .io_read_handler = nxt_http_idle_io_read_handler,
255431Sigor@sysoev.ru 
256771Sigor@sysoev.ru     .timer_handler = nxt_h1p_idle_timeout,
257771Sigor@sysoev.ru     .timer_value = nxt_h1p_conn_timer_value,
258635Sigor@sysoev.ru     .timer_data = offsetof(nxt_socket_conf_t, idle_timeout),
259431Sigor@sysoev.ru };
260431Sigor@sysoev.ru 
261431Sigor@sysoev.ru 
262629Sigor@sysoev.ru static ssize_t
nxt_http_idle_io_read_handler(nxt_task_t * task,nxt_conn_t * c)2631268Sigor@sysoev.ru nxt_http_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c)
264629Sigor@sysoev.ru {
265629Sigor@sysoev.ru     size_t                   size;
266629Sigor@sysoev.ru     ssize_t                  n;
267629Sigor@sysoev.ru     nxt_buf_t                *b;
268629Sigor@sysoev.ru     nxt_socket_conf_joint_t  *joint;
269629Sigor@sysoev.ru 
270683Sigor@sysoev.ru     joint = c->listen->socket.data;
271741Sigor@sysoev.ru 
272771Sigor@sysoev.ru     if (nxt_slow_path(joint == NULL)) {
273741Sigor@sysoev.ru         /*
274741Sigor@sysoev.ru          * Listening socket had been closed while
275741Sigor@sysoev.ru          * connection was in keep-alive state.
276741Sigor@sysoev.ru          */
277741Sigor@sysoev.ru         c->read_state = &nxt_h1p_idle_close_state;
278741Sigor@sysoev.ru         return 0;
279741Sigor@sysoev.ru     }
280741Sigor@sysoev.ru 
281629Sigor@sysoev.ru     size = joint->socket_conf->header_buffer_size;
282629Sigor@sysoev.ru 
2831268Sigor@sysoev.ru     b = nxt_event_engine_buf_mem_alloc(task->thread->engine, size);
284629Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
285629Sigor@sysoev.ru         c->socket.error = NXT_ENOMEM;
286629Sigor@sysoev.ru         return NXT_ERROR;
287629Sigor@sysoev.ru     }
288629Sigor@sysoev.ru 
289771Sigor@sysoev.ru     /*
290771Sigor@sysoev.ru      * 1 byte is enough to distinguish between SSLv3/TLS and plain HTTP.
291771Sigor@sysoev.ru      * 11 bytes are enough to log supported SSLv3/TLS version.
292771Sigor@sysoev.ru      * 16 bytes are just for more optimized kernel copy-out operation.
293771Sigor@sysoev.ru      */
294771Sigor@sysoev.ru     n = c->io->recv(c, b->mem.pos, 16, MSG_PEEK);
295629Sigor@sysoev.ru 
296629Sigor@sysoev.ru     if (n > 0) {
297629Sigor@sysoev.ru         c->read = b;
298629Sigor@sysoev.ru 
299629Sigor@sysoev.ru     } else {
300771Sigor@sysoev.ru         c->read = NULL;
3011268Sigor@sysoev.ru         nxt_event_engine_buf_mem_free(task->thread->engine, b);
302629Sigor@sysoev.ru     }
303629Sigor@sysoev.ru 
304629Sigor@sysoev.ru     return n;
305629Sigor@sysoev.ru }
306629Sigor@sysoev.ru 
307629Sigor@sysoev.ru 
308771Sigor@sysoev.ru static void
nxt_http_conn_test(nxt_task_t * task,void * obj,void * data)309771Sigor@sysoev.ru nxt_http_conn_test(nxt_task_t *task, void *obj, void *data)
310771Sigor@sysoev.ru {
311771Sigor@sysoev.ru     u_char                   *p;
312771Sigor@sysoev.ru     nxt_buf_t                *b;
313771Sigor@sysoev.ru     nxt_conn_t               *c;
314771Sigor@sysoev.ru     nxt_tls_conf_t           *tls;
3151268Sigor@sysoev.ru     nxt_event_engine_t       *engine;
316771Sigor@sysoev.ru     nxt_socket_conf_joint_t  *joint;
317771Sigor@sysoev.ru 
318771Sigor@sysoev.ru     c = obj;
319771Sigor@sysoev.ru 
320771Sigor@sysoev.ru     nxt_debug(task, "h1p conn https test");
321771Sigor@sysoev.ru 
3221268Sigor@sysoev.ru     engine = task->thread->engine;
323771Sigor@sysoev.ru     b = c->read;
324771Sigor@sysoev.ru     p = b->mem.pos;
325771Sigor@sysoev.ru 
326771Sigor@sysoev.ru     c->read_state = &nxt_h1p_idle_state;
327771Sigor@sysoev.ru 
328771Sigor@sysoev.ru     if (p[0] != 0x16) {
329771Sigor@sysoev.ru         b->mem.free = b->mem.pos;
330771Sigor@sysoev.ru 
3311268Sigor@sysoev.ru         nxt_conn_read(engine, c);
332771Sigor@sysoev.ru         return;
333771Sigor@sysoev.ru     }
334771Sigor@sysoev.ru 
335771Sigor@sysoev.ru     /* SSLv3/TLS ClientHello message. */
336771Sigor@sysoev.ru 
337771Sigor@sysoev.ru #if (NXT_DEBUG)
338771Sigor@sysoev.ru     if (nxt_buf_mem_used_size(&b->mem) >= 11) {
339771Sigor@sysoev.ru         u_char      major, minor;
340771Sigor@sysoev.ru         const char  *protocol;
341771Sigor@sysoev.ru 
342771Sigor@sysoev.ru         major = p[9];
343771Sigor@sysoev.ru         minor = p[10];
344771Sigor@sysoev.ru 
345771Sigor@sysoev.ru         if (major == 3) {
346771Sigor@sysoev.ru             if (minor == 0) {
347771Sigor@sysoev.ru                 protocol = "SSLv";
348771Sigor@sysoev.ru 
349771Sigor@sysoev.ru             } else {
350771Sigor@sysoev.ru                 protocol = "TLSv";
351771Sigor@sysoev.ru                 major -= 2;
352771Sigor@sysoev.ru                 minor -= 1;
353771Sigor@sysoev.ru             }
354771Sigor@sysoev.ru 
355771Sigor@sysoev.ru             nxt_debug(task, "SSL/TLS: %s%ud.%ud", protocol, major, minor);
356771Sigor@sysoev.ru         }
357771Sigor@sysoev.ru     }
358771Sigor@sysoev.ru #endif
359771Sigor@sysoev.ru 
360771Sigor@sysoev.ru     c->read = NULL;
3611268Sigor@sysoev.ru     nxt_event_engine_buf_mem_free(engine, b);
362771Sigor@sysoev.ru 
363771Sigor@sysoev.ru     joint = c->listen->socket.data;
364771Sigor@sysoev.ru 
365771Sigor@sysoev.ru     if (nxt_slow_path(joint == NULL)) {
366771Sigor@sysoev.ru         /*
367771Sigor@sysoev.ru          * Listening socket had been closed while
368771Sigor@sysoev.ru          * connection was in keep-alive state.
369771Sigor@sysoev.ru          */
3701270Sigor@sysoev.ru         nxt_h1p_closing(task, c);
371771Sigor@sysoev.ru         return;
372771Sigor@sysoev.ru     }
373771Sigor@sysoev.ru 
374771Sigor@sysoev.ru     tls = joint->socket_conf->tls;
375771Sigor@sysoev.ru 
376771Sigor@sysoev.ru     tls->conn_init(task, tls, c);
377771Sigor@sysoev.ru }
378771Sigor@sysoev.ru 
379771Sigor@sysoev.ru #endif
380771Sigor@sysoev.ru 
381771Sigor@sysoev.ru 
382771Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_idle_state
383741Sigor@sysoev.ru     nxt_aligned(64) =
384741Sigor@sysoev.ru {
385771Sigor@sysoev.ru     .ready_handler = nxt_h1p_conn_proto_init,
386741Sigor@sysoev.ru     .close_handler = nxt_h1p_conn_close,
387771Sigor@sysoev.ru     .error_handler = nxt_h1p_conn_error,
388771Sigor@sysoev.ru 
389771Sigor@sysoev.ru     .io_read_handler = nxt_h1p_idle_io_read_handler,
390771Sigor@sysoev.ru 
391771Sigor@sysoev.ru     .timer_handler = nxt_h1p_idle_timeout,
392771Sigor@sysoev.ru     .timer_value = nxt_h1p_conn_timer_value,
393771Sigor@sysoev.ru     .timer_data = offsetof(nxt_socket_conf_t, idle_timeout),
394771Sigor@sysoev.ru     .timer_autoreset = 1,
395741Sigor@sysoev.ru };
396741Sigor@sysoev.ru 
397741Sigor@sysoev.ru 
398771Sigor@sysoev.ru static ssize_t
nxt_h1p_idle_io_read_handler(nxt_task_t * task,nxt_conn_t * c)3991268Sigor@sysoev.ru nxt_h1p_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c)
400771Sigor@sysoev.ru {
401771Sigor@sysoev.ru     size_t                   size;
402771Sigor@sysoev.ru     ssize_t                  n;
403771Sigor@sysoev.ru     nxt_buf_t                *b;
404771Sigor@sysoev.ru     nxt_socket_conf_joint_t  *joint;
405771Sigor@sysoev.ru 
406771Sigor@sysoev.ru     joint = c->listen->socket.data;
407771Sigor@sysoev.ru 
408771Sigor@sysoev.ru     if (nxt_slow_path(joint == NULL)) {
409771Sigor@sysoev.ru         /*
410771Sigor@sysoev.ru          * Listening socket had been closed while
411771Sigor@sysoev.ru          * connection was in keep-alive state.
412771Sigor@sysoev.ru          */
413771Sigor@sysoev.ru         c->read_state = &nxt_h1p_idle_close_state;
414771Sigor@sysoev.ru         return 0;
415771Sigor@sysoev.ru     }
416771Sigor@sysoev.ru 
417771Sigor@sysoev.ru     b = c->read;
418771Sigor@sysoev.ru 
419771Sigor@sysoev.ru     if (b == NULL) {
420771Sigor@sysoev.ru         size = joint->socket_conf->header_buffer_size;
421771Sigor@sysoev.ru 
4221268Sigor@sysoev.ru         b = nxt_event_engine_buf_mem_alloc(task->thread->engine, size);
423771Sigor@sysoev.ru         if (nxt_slow_path(b == NULL)) {
424771Sigor@sysoev.ru             c->socket.error = NXT_ENOMEM;
425771Sigor@sysoev.ru             return NXT_ERROR;
426771Sigor@sysoev.ru         }
427771Sigor@sysoev.ru     }
428771Sigor@sysoev.ru 
429771Sigor@sysoev.ru     n = c->io->recvbuf(c, b);
430771Sigor@sysoev.ru 
431771Sigor@sysoev.ru     if (n > 0) {
432771Sigor@sysoev.ru         c->read = b;
433771Sigor@sysoev.ru 
434771Sigor@sysoev.ru     } else {
435771Sigor@sysoev.ru         c->read = NULL;
4361268Sigor@sysoev.ru         nxt_event_engine_buf_mem_free(task->thread->engine, b);
437771Sigor@sysoev.ru     }
438771Sigor@sysoev.ru 
439771Sigor@sysoev.ru     return n;
440771Sigor@sysoev.ru }
441771Sigor@sysoev.ru 
442771Sigor@sysoev.ru 
443431Sigor@sysoev.ru static void
nxt_h1p_conn_proto_init(nxt_task_t * task,void * obj,void * data)444624Sigor@sysoev.ru nxt_h1p_conn_proto_init(nxt_task_t *task, void *obj, void *data)
445431Sigor@sysoev.ru {
446624Sigor@sysoev.ru     nxt_conn_t     *c;
447624Sigor@sysoev.ru     nxt_h1proto_t  *h1p;
448624Sigor@sysoev.ru 
449624Sigor@sysoev.ru     c = obj;
450624Sigor@sysoev.ru 
451624Sigor@sysoev.ru     nxt_debug(task, "h1p conn proto init");
452624Sigor@sysoev.ru 
453624Sigor@sysoev.ru     h1p = nxt_mp_zget(c->mem_pool, sizeof(nxt_h1proto_t));
454624Sigor@sysoev.ru     if (nxt_slow_path(h1p == NULL)) {
4551270Sigor@sysoev.ru         nxt_h1p_closing(task, c);
456624Sigor@sysoev.ru         return;
457624Sigor@sysoev.ru     }
458624Sigor@sysoev.ru 
459624Sigor@sysoev.ru     c->socket.data = h1p;
460624Sigor@sysoev.ru     h1p->conn = c;
461624Sigor@sysoev.ru 
462624Sigor@sysoev.ru     nxt_h1p_conn_request_init(task, c, h1p);
463624Sigor@sysoev.ru }
464624Sigor@sysoev.ru 
465624Sigor@sysoev.ru 
466624Sigor@sysoev.ru static void
nxt_h1p_conn_request_init(nxt_task_t * task,void * obj,void * data)467624Sigor@sysoev.ru nxt_h1p_conn_request_init(nxt_task_t *task, void *obj, void *data)
468624Sigor@sysoev.ru {
469431Sigor@sysoev.ru     nxt_int_t                ret;
470431Sigor@sysoev.ru     nxt_conn_t               *c;
471431Sigor@sysoev.ru     nxt_h1proto_t            *h1p;
4721709Svbart@nginx.com     nxt_socket_conf_t        *skcf;
473431Sigor@sysoev.ru     nxt_http_request_t       *r;
474431Sigor@sysoev.ru     nxt_socket_conf_joint_t  *joint;
475431Sigor@sysoev.ru 
476431Sigor@sysoev.ru     c = obj;
477431Sigor@sysoev.ru     h1p = data;
478431Sigor@sysoev.ru 
479624Sigor@sysoev.ru     nxt_debug(task, "h1p conn request init");
480431Sigor@sysoev.ru 
4812185Svbart@nginx.com     nxt_conn_active(task->thread->engine, c);
4821457Sigor@sysoev.ru 
483624Sigor@sysoev.ru     r = nxt_http_request_create(task);
484431Sigor@sysoev.ru 
485624Sigor@sysoev.ru     if (nxt_fast_path(r != NULL)) {
486431Sigor@sysoev.ru         h1p->request = r;
487431Sigor@sysoev.ru         r->proto.h1 = h1p;
488624Sigor@sysoev.ru 
4891112Sigor@sysoev.ru         /* r->protocol = NXT_HTTP_PROTO_H1 is done by zeroing. */
490431Sigor@sysoev.ru         r->remote = c->remote;
491431Sigor@sysoev.ru 
4921110Saxel.duch@nginx.com #if (NXT_TLS)
4932133Sz.hong@f5.com         r->tls = (c->u.tls != NULL);
4941110Saxel.duch@nginx.com #endif
4951110Saxel.duch@nginx.com 
4961265Sigor@sysoev.ru         r->task = c->task;
4971265Sigor@sysoev.ru         task = &r->task;
4981265Sigor@sysoev.ru         c->socket.task = task;
4991265Sigor@sysoev.ru         c->read_timer.task = task;
5001265Sigor@sysoev.ru         c->write_timer.task = task;
5011265Sigor@sysoev.ru 
502431Sigor@sysoev.ru         ret = nxt_http_parse_request_init(&h1p->parser, r->mem_pool);
503624Sigor@sysoev.ru 
504624Sigor@sysoev.ru         if (nxt_fast_path(ret == NXT_OK)) {
505683Sigor@sysoev.ru             joint = c->listen->socket.data;
506683Sigor@sysoev.ru             joint->count++;
507683Sigor@sysoev.ru 
508683Sigor@sysoev.ru             r->conf = joint;
5091709Svbart@nginx.com             skcf = joint->socket_conf;
5102381Salx@nginx.com             r->log_route = skcf->log_route;
5111619Smax.romanov@nginx.com 
5121619Smax.romanov@nginx.com             if (c->local == NULL) {
5131709Svbart@nginx.com                 c->local = skcf->sockaddr;
5141619Smax.romanov@nginx.com             }
515683Sigor@sysoev.ru 
5161709Svbart@nginx.com             h1p->parser.discard_unsafe_fields = skcf->discard_unsafe_fields;
5171709Svbart@nginx.com 
518683Sigor@sysoev.ru             nxt_h1p_conn_request_header_parse(task, c, h1p);
519624Sigor@sysoev.ru             return;
520431Sigor@sysoev.ru         }
521624Sigor@sysoev.ru 
522624Sigor@sysoev.ru         /*
523624Sigor@sysoev.ru          * The request is very incomplete here,
524624Sigor@sysoev.ru          * so "internal server error" useless here.
525624Sigor@sysoev.ru          */
526624Sigor@sysoev.ru         nxt_mp_release(r->mem_pool);
527431Sigor@sysoev.ru     }
528431Sigor@sysoev.ru 
5291270Sigor@sysoev.ru     nxt_h1p_closing(task, c);
530624Sigor@sysoev.ru }
531624Sigor@sysoev.ru 
532624Sigor@sysoev.ru 
533624Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_header_parse_state
534624Sigor@sysoev.ru     nxt_aligned(64) =
535624Sigor@sysoev.ru {
536683Sigor@sysoev.ru     .ready_handler = nxt_h1p_conn_request_header_parse,
537624Sigor@sysoev.ru     .close_handler = nxt_h1p_conn_request_error,
538624Sigor@sysoev.ru     .error_handler = nxt_h1p_conn_request_error,
539624Sigor@sysoev.ru 
540624Sigor@sysoev.ru     .timer_handler = nxt_h1p_conn_request_timeout,
541771Sigor@sysoev.ru     .timer_value = nxt_h1p_conn_request_timer_value,
542624Sigor@sysoev.ru     .timer_data = offsetof(nxt_socket_conf_t, header_read_timeout),
543624Sigor@sysoev.ru };
544624Sigor@sysoev.ru 
545624Sigor@sysoev.ru 
546624Sigor@sysoev.ru static void
nxt_h1p_conn_request_header_parse(nxt_task_t * task,void * obj,void * data)547683Sigor@sysoev.ru nxt_h1p_conn_request_header_parse(nxt_task_t *task, void *obj, void *data)
548624Sigor@sysoev.ru {
549624Sigor@sysoev.ru     nxt_int_t           ret;
550624Sigor@sysoev.ru     nxt_conn_t          *c;
551624Sigor@sysoev.ru     nxt_h1proto_t       *h1p;
552624Sigor@sysoev.ru     nxt_http_status_t   status;
553624Sigor@sysoev.ru     nxt_http_request_t  *r;
554624Sigor@sysoev.ru 
555624Sigor@sysoev.ru     c = obj;
556624Sigor@sysoev.ru     h1p = data;
557624Sigor@sysoev.ru 
558624Sigor@sysoev.ru     nxt_debug(task, "h1p conn header parse");
559624Sigor@sysoev.ru 
560431Sigor@sysoev.ru     ret = nxt_http_parse_request(&h1p->parser, &c->read->mem);
561431Sigor@sysoev.ru 
562636Sigor@sysoev.ru     ret = nxt_expect(NXT_DONE, ret);
563624Sigor@sysoev.ru 
564636Sigor@sysoev.ru     if (ret != NXT_AGAIN) {
565636Sigor@sysoev.ru         nxt_timer_disable(task->thread->engine, &c->read_timer);
566636Sigor@sysoev.ru     }
567636Sigor@sysoev.ru 
568636Sigor@sysoev.ru     r = h1p->request;
569625Sigor@sysoev.ru 
570625Sigor@sysoev.ru     switch (ret) {
571625Sigor@sysoev.ru 
572625Sigor@sysoev.ru     case NXT_DONE:
573431Sigor@sysoev.ru         /*
574431Sigor@sysoev.ru          * By default the keepalive mode is disabled in HTTP/1.0 and
575431Sigor@sysoev.ru          * enabled in HTTP/1.1.  The mode can be overridden later by
576431Sigor@sysoev.ru          * the "Connection" field processed in nxt_h1p_connection().
577431Sigor@sysoev.ru          */
578481Svbart@nginx.com         h1p->keepalive = (h1p->parser.version.s.minor != '0');
579431Sigor@sysoev.ru 
5802430Salx@nginx.com         r->request_line.start = h1p->parser.method.start;
5812430Salx@nginx.com         r->request_line.length = h1p->parser.request_line_end
5822430Salx@nginx.com                                  - r->request_line.start;
5832430Salx@nginx.com 
5842430Salx@nginx.com         if (nxt_slow_path(r->log_route)) {
5852430Salx@nginx.com             nxt_log(task, NXT_LOG_NOTICE, "http request line \"%V\"",
5862430Salx@nginx.com                     &r->request_line);
5872430Salx@nginx.com         }
5882430Salx@nginx.com 
5891131Smax.romanov@nginx.com         ret = nxt_h1p_header_process(task, h1p, r);
590431Sigor@sysoev.ru 
591431Sigor@sysoev.ru         if (nxt_fast_path(ret == NXT_OK)) {
592772Sigor@sysoev.ru 
593772Sigor@sysoev.ru #if (NXT_TLS)
594772Sigor@sysoev.ru             if (c->u.tls == NULL && r->conf->socket_conf->tls != NULL) {
595772Sigor@sysoev.ru                 status = NXT_HTTP_TO_HTTPS;
596772Sigor@sysoev.ru                 goto error;
597772Sigor@sysoev.ru             }
598772Sigor@sysoev.ru #endif
599772Sigor@sysoev.ru 
600431Sigor@sysoev.ru             r->state->ready_handler(task, r, NULL);
601431Sigor@sysoev.ru             return;
602431Sigor@sysoev.ru         }
603431Sigor@sysoev.ru 
604945Svbart@nginx.com         status = ret;
605625Sigor@sysoev.ru         goto error;
606431Sigor@sysoev.ru 
607625Sigor@sysoev.ru     case NXT_AGAIN:
608683Sigor@sysoev.ru         status = nxt_h1p_header_buffer_test(task, h1p, c, r->conf->socket_conf);
609431Sigor@sysoev.ru 
610625Sigor@sysoev.ru         if (nxt_fast_path(status == NXT_OK)) {
611625Sigor@sysoev.ru             c->read_state = &nxt_h1p_header_parse_state;
612431Sigor@sysoev.ru 
613625Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
614625Sigor@sysoev.ru             return;
615431Sigor@sysoev.ru         }
616431Sigor@sysoev.ru 
617625Sigor@sysoev.ru         break;
618480Svbart@nginx.com 
619480Svbart@nginx.com     case NXT_HTTP_PARSE_INVALID:
620480Svbart@nginx.com         status = NXT_HTTP_BAD_REQUEST;
621480Svbart@nginx.com         break;
622431Sigor@sysoev.ru 
623482Svbart@nginx.com     case NXT_HTTP_PARSE_UNSUPPORTED_VERSION:
624482Svbart@nginx.com         status = NXT_HTTP_VERSION_NOT_SUPPORTED;
625482Svbart@nginx.com         break;
626482Svbart@nginx.com 
627480Svbart@nginx.com     case NXT_HTTP_PARSE_TOO_LARGE_FIELD:
628480Svbart@nginx.com         status = NXT_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE;
629480Svbart@nginx.com         break;
630480Svbart@nginx.com 
631480Svbart@nginx.com     default:
632480Svbart@nginx.com     case NXT_ERROR:
633480Svbart@nginx.com         status = NXT_HTTP_INTERNAL_SERVER_ERROR;
634480Svbart@nginx.com         break;
635480Svbart@nginx.com     }
636480Svbart@nginx.com 
6371131Smax.romanov@nginx.com     (void) nxt_h1p_header_process(task, h1p, r);
638625Sigor@sysoev.ru 
639625Sigor@sysoev.ru error:
640625Sigor@sysoev.ru 
641960Sigor@sysoev.ru     h1p->keepalive = 0;
642960Sigor@sysoev.ru 
643480Svbart@nginx.com     nxt_http_request_error(task, r, status);
644431Sigor@sysoev.ru }
645431Sigor@sysoev.ru 
646431Sigor@sysoev.ru 
647431Sigor@sysoev.ru static nxt_int_t
nxt_h1p_header_process(nxt_task_t * task,nxt_h1proto_t * h1p,nxt_http_request_t * r)6481131Smax.romanov@nginx.com nxt_h1p_header_process(nxt_task_t *task, nxt_h1proto_t *h1p,
6491131Smax.romanov@nginx.com     nxt_http_request_t *r)
650620Svbart@nginx.com {
6511131Smax.romanov@nginx.com     u_char     *m;
6521131Smax.romanov@nginx.com     nxt_int_t  ret;
6531131Smax.romanov@nginx.com 
654620Svbart@nginx.com     r->target.start = h1p->parser.target_start;
655620Svbart@nginx.com     r->target.length = h1p->parser.target_end - h1p->parser.target_start;
656620Svbart@nginx.com 
657620Svbart@nginx.com     if (h1p->parser.version.ui64 != 0) {
658620Svbart@nginx.com         r->version.start = h1p->parser.version.str;
659620Svbart@nginx.com         r->version.length = sizeof(h1p->parser.version.str);
660620Svbart@nginx.com     }
661620Svbart@nginx.com 
662620Svbart@nginx.com     r->method = &h1p->parser.method;
663620Svbart@nginx.com     r->path = &h1p->parser.path;
664620Svbart@nginx.com     r->args = &h1p->parser.args;
665620Svbart@nginx.com 
666620Svbart@nginx.com     r->fields = h1p->parser.fields;
667620Svbart@nginx.com 
6681131Smax.romanov@nginx.com     ret = nxt_http_fields_process(r->fields, &nxt_h1p_fields_hash, r);
6691131Smax.romanov@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
6701131Smax.romanov@nginx.com         return ret;
6711131Smax.romanov@nginx.com     }
6721131Smax.romanov@nginx.com 
6731131Smax.romanov@nginx.com     if (h1p->connection_upgrade && h1p->upgrade_websocket) {
6741131Smax.romanov@nginx.com         m = h1p->parser.method.start;
6751131Smax.romanov@nginx.com 
6761131Smax.romanov@nginx.com         if (nxt_slow_path(h1p->parser.method.length != 3
6771131Smax.romanov@nginx.com                           || m[0] != 'G'
6781131Smax.romanov@nginx.com                           || m[1] != 'E'
6791131Smax.romanov@nginx.com                           || m[2] != 'T'))
6801131Smax.romanov@nginx.com         {
6811131Smax.romanov@nginx.com             nxt_log(task, NXT_LOG_INFO, "h1p upgrade: bad method");
6821131Smax.romanov@nginx.com 
6831131Smax.romanov@nginx.com             return NXT_HTTP_BAD_REQUEST;
6841131Smax.romanov@nginx.com         }
6851131Smax.romanov@nginx.com 
6861131Smax.romanov@nginx.com         if (nxt_slow_path(h1p->parser.version.s.minor != '1')) {
6871131Smax.romanov@nginx.com             nxt_log(task, NXT_LOG_INFO, "h1p upgrade: bad protocol version");
6881131Smax.romanov@nginx.com 
6891131Smax.romanov@nginx.com             return NXT_HTTP_BAD_REQUEST;
6901131Smax.romanov@nginx.com         }
6911131Smax.romanov@nginx.com 
6921131Smax.romanov@nginx.com         if (nxt_slow_path(h1p->websocket_key == NULL)) {
6931235Sigor@sysoev.ru             nxt_log(task, NXT_LOG_INFO,
6941235Sigor@sysoev.ru                     "h1p upgrade: bad or absent websocket key");
6951131Smax.romanov@nginx.com 
6961131Smax.romanov@nginx.com             return NXT_HTTP_BAD_REQUEST;
6971131Smax.romanov@nginx.com         }
6981131Smax.romanov@nginx.com 
6991131Smax.romanov@nginx.com         if (nxt_slow_path(h1p->websocket_version_ok == 0)) {
7001235Sigor@sysoev.ru             nxt_log(task, NXT_LOG_INFO,
7011235Sigor@sysoev.ru                     "h1p upgrade: bad or absent websocket version");
7021131Smax.romanov@nginx.com 
7031131Smax.romanov@nginx.com             return NXT_HTTP_UPGRADE_REQUIRED;
7041131Smax.romanov@nginx.com         }
7051131Smax.romanov@nginx.com 
7061131Smax.romanov@nginx.com         r->websocket_handshake = 1;
7071131Smax.romanov@nginx.com     }
7081131Smax.romanov@nginx.com 
7091131Smax.romanov@nginx.com     return ret;
710620Svbart@nginx.com }
711620Svbart@nginx.com 
712620Svbart@nginx.com 
713620Svbart@nginx.com static nxt_int_t
nxt_h1p_header_buffer_test(nxt_task_t * task,nxt_h1proto_t * h1p,nxt_conn_t * c,nxt_socket_conf_t * skcf)714625Sigor@sysoev.ru nxt_h1p_header_buffer_test(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_conn_t *c,
715625Sigor@sysoev.ru     nxt_socket_conf_t *skcf)
716625Sigor@sysoev.ru {
717625Sigor@sysoev.ru     size_t     size, used;
718625Sigor@sysoev.ru     nxt_buf_t  *in, *b;
719625Sigor@sysoev.ru 
720625Sigor@sysoev.ru     in = c->read;
721625Sigor@sysoev.ru 
722625Sigor@sysoev.ru     if (nxt_buf_mem_free_size(&in->mem) == 0) {
723625Sigor@sysoev.ru         size = skcf->large_header_buffer_size;
724625Sigor@sysoev.ru         used = nxt_buf_mem_used_size(&in->mem);
725625Sigor@sysoev.ru 
726625Sigor@sysoev.ru         if (size <= used || h1p->nbuffers >= skcf->large_header_buffers) {
727625Sigor@sysoev.ru             return NXT_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE;
728625Sigor@sysoev.ru         }
729625Sigor@sysoev.ru 
730625Sigor@sysoev.ru         b = nxt_buf_mem_alloc(c->mem_pool, size, 0);
731625Sigor@sysoev.ru         if (nxt_slow_path(b == NULL)) {
732625Sigor@sysoev.ru             return NXT_HTTP_INTERNAL_SERVER_ERROR;
733625Sigor@sysoev.ru         }
734625Sigor@sysoev.ru 
735625Sigor@sysoev.ru         b->mem.free = nxt_cpymem(b->mem.pos, in->mem.pos, used);
736625Sigor@sysoev.ru 
737625Sigor@sysoev.ru         in->next = h1p->buffers;
738625Sigor@sysoev.ru         h1p->buffers = in;
739625Sigor@sysoev.ru         h1p->nbuffers++;
740625Sigor@sysoev.ru 
741625Sigor@sysoev.ru         c->read = b;
742625Sigor@sysoev.ru     }
743625Sigor@sysoev.ru 
744625Sigor@sysoev.ru     return NXT_OK;
745625Sigor@sysoev.ru }
746625Sigor@sysoev.ru 
747625Sigor@sysoev.ru 
748625Sigor@sysoev.ru static nxt_int_t
nxt_h1p_connection(void * ctx,nxt_http_field_t * field,uintptr_t data)749431Sigor@sysoev.ru nxt_h1p_connection(void *ctx, nxt_http_field_t *field, uintptr_t data)
750431Sigor@sysoev.ru {
7511234Sigor@sysoev.ru     nxt_http_request_t  *r;
752431Sigor@sysoev.ru 
753431Sigor@sysoev.ru     r = ctx;
7541270Sigor@sysoev.ru     field->hopbyhop = 1;
755431Sigor@sysoev.ru 
7561614Smax.romanov@nginx.com     if (field->value_length == 5
7571614Smax.romanov@nginx.com         && nxt_memcasecmp(field->value, "close", 5) == 0)
7581614Smax.romanov@nginx.com     {
759431Sigor@sysoev.ru         r->proto.h1->keepalive = 0;
7601131Smax.romanov@nginx.com 
7611614Smax.romanov@nginx.com     } else if (field->value_length == 10
7621614Smax.romanov@nginx.com                && nxt_memcasecmp(field->value, "keep-alive", 10) == 0)
7631614Smax.romanov@nginx.com     {
7641614Smax.romanov@nginx.com         r->proto.h1->keepalive = 1;
7651614Smax.romanov@nginx.com 
7661131Smax.romanov@nginx.com     } else if (field->value_length == 7
7671234Sigor@sysoev.ru                && nxt_memcasecmp(field->value, "upgrade", 7) == 0)
7681131Smax.romanov@nginx.com     {
7691131Smax.romanov@nginx.com         r->proto.h1->connection_upgrade = 1;
7701131Smax.romanov@nginx.com     }
7711131Smax.romanov@nginx.com 
7721131Smax.romanov@nginx.com     return NXT_OK;
7731131Smax.romanov@nginx.com }
7741131Smax.romanov@nginx.com 
7751131Smax.romanov@nginx.com 
7761131Smax.romanov@nginx.com static nxt_int_t
nxt_h1p_upgrade(void * ctx,nxt_http_field_t * field,uintptr_t data)7771131Smax.romanov@nginx.com nxt_h1p_upgrade(void *ctx, nxt_http_field_t *field, uintptr_t data)
7781131Smax.romanov@nginx.com {
7791234Sigor@sysoev.ru     nxt_http_request_t  *r;
7801131Smax.romanov@nginx.com 
7811131Smax.romanov@nginx.com     r = ctx;
7821131Smax.romanov@nginx.com 
7831131Smax.romanov@nginx.com     if (field->value_length == 9
7841234Sigor@sysoev.ru         && nxt_memcasecmp(field->value, "websocket", 9) == 0)
7851131Smax.romanov@nginx.com     {
7861131Smax.romanov@nginx.com         r->proto.h1->upgrade_websocket = 1;
7871131Smax.romanov@nginx.com     }
7881131Smax.romanov@nginx.com 
7891131Smax.romanov@nginx.com     return NXT_OK;
7901131Smax.romanov@nginx.com }
7911131Smax.romanov@nginx.com 
7921131Smax.romanov@nginx.com 
7931131Smax.romanov@nginx.com static nxt_int_t
nxt_h1p_websocket_key(void * ctx,nxt_http_field_t * field,uintptr_t data)7941131Smax.romanov@nginx.com nxt_h1p_websocket_key(void *ctx, nxt_http_field_t *field, uintptr_t data)
7951131Smax.romanov@nginx.com {
7961131Smax.romanov@nginx.com     nxt_http_request_t  *r;
7971131Smax.romanov@nginx.com 
7981131Smax.romanov@nginx.com     r = ctx;
7991131Smax.romanov@nginx.com 
8001131Smax.romanov@nginx.com     if (field->value_length == 24) {
8011131Smax.romanov@nginx.com         r->proto.h1->websocket_key = field;
8021131Smax.romanov@nginx.com     }
8031131Smax.romanov@nginx.com 
8041131Smax.romanov@nginx.com     return NXT_OK;
8051131Smax.romanov@nginx.com }
8061131Smax.romanov@nginx.com 
8071131Smax.romanov@nginx.com 
8081131Smax.romanov@nginx.com static nxt_int_t
nxt_h1p_websocket_version(void * ctx,nxt_http_field_t * field,uintptr_t data)8091131Smax.romanov@nginx.com nxt_h1p_websocket_version(void *ctx, nxt_http_field_t *field, uintptr_t data)
8101131Smax.romanov@nginx.com {
8111131Smax.romanov@nginx.com     nxt_http_request_t  *r;
8121131Smax.romanov@nginx.com 
8131131Smax.romanov@nginx.com     r = ctx;
8141131Smax.romanov@nginx.com 
8151131Smax.romanov@nginx.com     if (field->value_length == 2
8161131Smax.romanov@nginx.com         && field->value[0] == '1' && field->value[1] == '3')
8171131Smax.romanov@nginx.com     {
8181131Smax.romanov@nginx.com         r->proto.h1->websocket_version_ok = 1;
819431Sigor@sysoev.ru     }
820431Sigor@sysoev.ru 
821431Sigor@sysoev.ru     return NXT_OK;
822431Sigor@sysoev.ru }
823431Sigor@sysoev.ru 
824431Sigor@sysoev.ru 
825431Sigor@sysoev.ru static nxt_int_t
nxt_h1p_transfer_encoding(void * ctx,nxt_http_field_t * field,uintptr_t data)826431Sigor@sysoev.ru nxt_h1p_transfer_encoding(void *ctx, nxt_http_field_t *field, uintptr_t data)
827431Sigor@sysoev.ru {
828431Sigor@sysoev.ru     nxt_http_te_t       te;
829431Sigor@sysoev.ru     nxt_http_request_t  *r;
830431Sigor@sysoev.ru 
831431Sigor@sysoev.ru     r = ctx;
8321270Sigor@sysoev.ru     field->skip = 1;
8331270Sigor@sysoev.ru     field->hopbyhop = 1;
834431Sigor@sysoev.ru 
835431Sigor@sysoev.ru     if (field->value_length == 7
8362231Salx@nginx.com         && memcmp(field->value, "chunked", 7) == 0)
837431Sigor@sysoev.ru     {
838431Sigor@sysoev.ru         te = NXT_HTTP_TE_CHUNKED;
839431Sigor@sysoev.ru 
840431Sigor@sysoev.ru     } else {
841431Sigor@sysoev.ru         te = NXT_HTTP_TE_UNSUPPORTED;
842431Sigor@sysoev.ru     }
843431Sigor@sysoev.ru 
844431Sigor@sysoev.ru     r->proto.h1->transfer_encoding = te;
845431Sigor@sysoev.ru 
846431Sigor@sysoev.ru     return NXT_OK;
847431Sigor@sysoev.ru }
848431Sigor@sysoev.ru 
849431Sigor@sysoev.ru 
850431Sigor@sysoev.ru static void
nxt_h1p_request_body_read(nxt_task_t * task,nxt_http_request_t * r)851431Sigor@sysoev.ru nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r)
852431Sigor@sysoev.ru {
8531403Smax.romanov@nginx.com     size_t             size, body_length, body_buffer_size, body_rest;
8541403Smax.romanov@nginx.com     ssize_t            res;
8551403Smax.romanov@nginx.com     nxt_str_t          *tmp_path, tmp_name;
856459Sigor@sysoev.ru     nxt_buf_t          *in, *b;
857431Sigor@sysoev.ru     nxt_conn_t         *c;
858459Sigor@sysoev.ru     nxt_h1proto_t      *h1p;
859431Sigor@sysoev.ru     nxt_http_status_t  status;
860431Sigor@sysoev.ru 
8611403Smax.romanov@nginx.com     static const nxt_str_t tmp_name_pattern = nxt_string("/req-XXXXXXXX");
8621403Smax.romanov@nginx.com 
863459Sigor@sysoev.ru     h1p = r->proto.h1;
864459Sigor@sysoev.ru 
865624Sigor@sysoev.ru     nxt_debug(task, "h1p request body read %O te:%d",
866459Sigor@sysoev.ru               r->content_length_n, h1p->transfer_encoding);
867431Sigor@sysoev.ru 
868459Sigor@sysoev.ru     switch (h1p->transfer_encoding) {
869431Sigor@sysoev.ru 
870431Sigor@sysoev.ru     case NXT_HTTP_TE_CHUNKED:
871431Sigor@sysoev.ru         status = NXT_HTTP_LENGTH_REQUIRED;
872431Sigor@sysoev.ru         goto error;
873431Sigor@sysoev.ru 
874431Sigor@sysoev.ru     case NXT_HTTP_TE_UNSUPPORTED:
875431Sigor@sysoev.ru         status = NXT_HTTP_NOT_IMPLEMENTED;
876431Sigor@sysoev.ru         goto error;
877431Sigor@sysoev.ru 
878431Sigor@sysoev.ru     default:
879431Sigor@sysoev.ru     case NXT_HTTP_TE_NONE:
880431Sigor@sysoev.ru         break;
881431Sigor@sysoev.ru     }
882431Sigor@sysoev.ru 
883431Sigor@sysoev.ru     if (r->content_length_n == -1 || r->content_length_n == 0) {
884431Sigor@sysoev.ru         goto ready;
885431Sigor@sysoev.ru     }
886431Sigor@sysoev.ru 
887527Svbart@nginx.com     body_length = (size_t) r->content_length_n;
888431Sigor@sysoev.ru 
8891403Smax.romanov@nginx.com     body_buffer_size = nxt_min(r->conf->socket_conf->body_buffer_size,
8901403Smax.romanov@nginx.com                                body_length);
8911403Smax.romanov@nginx.com 
8921403Smax.romanov@nginx.com     if (body_length > body_buffer_size) {
8931403Smax.romanov@nginx.com         tmp_path = &r->conf->socket_conf->body_temp_path;
8941403Smax.romanov@nginx.com 
8951403Smax.romanov@nginx.com         tmp_name.length = tmp_path->length + tmp_name_pattern.length;
8961403Smax.romanov@nginx.com 
8971403Smax.romanov@nginx.com         b = nxt_buf_file_alloc(r->mem_pool,
8981403Smax.romanov@nginx.com                                body_buffer_size + sizeof(nxt_file_t)
8991403Smax.romanov@nginx.com                                + tmp_name.length + 1, 0);
9001403Smax.romanov@nginx.com 
9011403Smax.romanov@nginx.com     } else {
9021403Smax.romanov@nginx.com         /* This initialization required for CentOS 6, gcc 4.4.7. */
9031403Smax.romanov@nginx.com         tmp_path = NULL;
9041403Smax.romanov@nginx.com         tmp_name.length = 0;
9051403Smax.romanov@nginx.com 
9061403Smax.romanov@nginx.com         b = nxt_buf_mem_alloc(r->mem_pool, body_buffer_size, 0);
9071403Smax.romanov@nginx.com     }
9081403Smax.romanov@nginx.com 
9091403Smax.romanov@nginx.com     if (nxt_slow_path(b == NULL)) {
9101403Smax.romanov@nginx.com         status = NXT_HTTP_INTERNAL_SERVER_ERROR;
9111403Smax.romanov@nginx.com         goto error;
9121403Smax.romanov@nginx.com     }
9131403Smax.romanov@nginx.com 
9141403Smax.romanov@nginx.com     r->body = b;
9151403Smax.romanov@nginx.com 
9161403Smax.romanov@nginx.com     if (body_length > body_buffer_size) {
9171403Smax.romanov@nginx.com         tmp_name.start = nxt_pointer_to(b->mem.start, sizeof(nxt_file_t));
9181403Smax.romanov@nginx.com 
9191403Smax.romanov@nginx.com         memcpy(tmp_name.start, tmp_path->start, tmp_path->length);
9201403Smax.romanov@nginx.com         memcpy(tmp_name.start + tmp_path->length, tmp_name_pattern.start,
9211403Smax.romanov@nginx.com                tmp_name_pattern.length);
9221403Smax.romanov@nginx.com         tmp_name.start[tmp_name.length] = '\0';
9231403Smax.romanov@nginx.com 
9241403Smax.romanov@nginx.com         b->file = (nxt_file_t *) b->mem.start;
9251403Smax.romanov@nginx.com         nxt_memzero(b->file, sizeof(nxt_file_t));
9261403Smax.romanov@nginx.com         b->file->fd = -1;
9271403Smax.romanov@nginx.com         b->file->size = body_length;
9281403Smax.romanov@nginx.com 
9291403Smax.romanov@nginx.com         b->mem.start += sizeof(nxt_file_t) + tmp_name.length + 1;
9301403Smax.romanov@nginx.com         b->mem.pos = b->mem.start;
9311403Smax.romanov@nginx.com         b->mem.free = b->mem.start;
9321403Smax.romanov@nginx.com 
9331403Smax.romanov@nginx.com         b->file->fd = mkstemp((char *) tmp_name.start);
9341403Smax.romanov@nginx.com         if (nxt_slow_path(b->file->fd == -1)) {
9351534Svbart@nginx.com             nxt_alert(task, "mkstemp(%s) failed %E", tmp_name.start, nxt_errno);
9361403Smax.romanov@nginx.com 
937431Sigor@sysoev.ru             status = NXT_HTTP_INTERNAL_SERVER_ERROR;
938431Sigor@sysoev.ru             goto error;
939431Sigor@sysoev.ru         }
940431Sigor@sysoev.ru 
9411403Smax.romanov@nginx.com         nxt_debug(task, "create body tmp file \"%V\", %d",
9421403Smax.romanov@nginx.com                   &tmp_name, b->file->fd);
9431403Smax.romanov@nginx.com 
9441403Smax.romanov@nginx.com         unlink((char *) tmp_name.start);
945431Sigor@sysoev.ru     }
946431Sigor@sysoev.ru 
9471403Smax.romanov@nginx.com     body_rest = body_length;
9481403Smax.romanov@nginx.com 
949459Sigor@sysoev.ru     in = h1p->conn->read;
950431Sigor@sysoev.ru 
951459Sigor@sysoev.ru     size = nxt_buf_mem_used_size(&in->mem);
952431Sigor@sysoev.ru 
953431Sigor@sysoev.ru     if (size != 0) {
9541403Smax.romanov@nginx.com         size = nxt_min(size, body_length);
9551403Smax.romanov@nginx.com 
9561403Smax.romanov@nginx.com         if (nxt_buf_is_file(b)) {
9571403Smax.romanov@nginx.com             res = nxt_fd_write(b->file->fd, in->mem.pos, size);
9581403Smax.romanov@nginx.com             if (nxt_slow_path(res < (ssize_t) size)) {
9591403Smax.romanov@nginx.com                 status = NXT_HTTP_INTERNAL_SERVER_ERROR;
9601403Smax.romanov@nginx.com                 goto error;
9611403Smax.romanov@nginx.com             }
9621403Smax.romanov@nginx.com 
9631403Smax.romanov@nginx.com             b->file_end += size;
9641403Smax.romanov@nginx.com 
9651403Smax.romanov@nginx.com         } else {
9661403Smax.romanov@nginx.com             size = nxt_min(body_buffer_size, size);
9671403Smax.romanov@nginx.com             b->mem.free = nxt_cpymem(b->mem.free, in->mem.pos, size);
968431Sigor@sysoev.ru         }
969431Sigor@sysoev.ru 
970459Sigor@sysoev.ru         in->mem.pos += size;
9711403Smax.romanov@nginx.com         body_rest -= size;
972431Sigor@sysoev.ru     }
973431Sigor@sysoev.ru 
9741403Smax.romanov@nginx.com     nxt_debug(task, "h1p body rest: %uz", body_rest);
9751403Smax.romanov@nginx.com 
9761403Smax.romanov@nginx.com     if (body_rest != 0) {
977459Sigor@sysoev.ru         in->next = h1p->buffers;
978459Sigor@sysoev.ru         h1p->buffers = in;
9791131Smax.romanov@nginx.com         h1p->nbuffers++;
980459Sigor@sysoev.ru 
981459Sigor@sysoev.ru         c = h1p->conn;
982431Sigor@sysoev.ru         c->read = b;
983431Sigor@sysoev.ru         c->read_state = &nxt_h1p_read_body_state;
984431Sigor@sysoev.ru 
985431Sigor@sysoev.ru         nxt_conn_read(task->thread->engine, c);
986431Sigor@sysoev.ru         return;
987431Sigor@sysoev.ru     }
988431Sigor@sysoev.ru 
9891403Smax.romanov@nginx.com     if (nxt_buf_is_file(b)) {
9901403Smax.romanov@nginx.com         b->mem.start = NULL;
9911403Smax.romanov@nginx.com         b->mem.end = NULL;
9921403Smax.romanov@nginx.com         b->mem.pos = NULL;
9931403Smax.romanov@nginx.com         b->mem.free = NULL;
9941403Smax.romanov@nginx.com     }
9951403Smax.romanov@nginx.com 
996431Sigor@sysoev.ru ready:
997431Sigor@sysoev.ru 
9981155Smax.romanov@nginx.com     r->state->ready_handler(task, r, NULL);
999431Sigor@sysoev.ru 
1000431Sigor@sysoev.ru     return;
1001431Sigor@sysoev.ru 
1002431Sigor@sysoev.ru error:
1003431Sigor@sysoev.ru 
1004459Sigor@sysoev.ru     h1p->keepalive = 0;
1005431Sigor@sysoev.ru 
1006431Sigor@sysoev.ru     nxt_http_request_error(task, r, status);
1007431Sigor@sysoev.ru }
1008431Sigor@sysoev.ru 
1009431Sigor@sysoev.ru 
1010431Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_read_body_state
1011431Sigor@sysoev.ru     nxt_aligned(64) =
1012431Sigor@sysoev.ru {
1013637Sigor@sysoev.ru     .ready_handler = nxt_h1p_conn_request_body_read,
1014624Sigor@sysoev.ru     .close_handler = nxt_h1p_conn_request_error,
1015624Sigor@sysoev.ru     .error_handler = nxt_h1p_conn_request_error,
1016431Sigor@sysoev.ru 
1017624Sigor@sysoev.ru     .timer_handler = nxt_h1p_conn_request_timeout,
1018771Sigor@sysoev.ru     .timer_value = nxt_h1p_conn_request_timer_value,
1019431Sigor@sysoev.ru     .timer_data = offsetof(nxt_socket_conf_t, body_read_timeout),
1020431Sigor@sysoev.ru     .timer_autoreset = 1,
1021431Sigor@sysoev.ru };
1022431Sigor@sysoev.ru 
1023431Sigor@sysoev.ru 
1024431Sigor@sysoev.ru static void
nxt_h1p_conn_request_body_read(nxt_task_t * task,void * obj,void * data)1025637Sigor@sysoev.ru nxt_h1p_conn_request_body_read(nxt_task_t *task, void *obj, void *data)
1026431Sigor@sysoev.ru {
10271403Smax.romanov@nginx.com     size_t              size, body_rest;
10281403Smax.romanov@nginx.com     ssize_t             res;
10291403Smax.romanov@nginx.com     nxt_buf_t           *b;
1030431Sigor@sysoev.ru     nxt_conn_t          *c;
1031431Sigor@sysoev.ru     nxt_h1proto_t       *h1p;
1032431Sigor@sysoev.ru     nxt_http_request_t  *r;
1033637Sigor@sysoev.ru     nxt_event_engine_t  *engine;
1034431Sigor@sysoev.ru 
1035431Sigor@sysoev.ru     c = obj;
1036431Sigor@sysoev.ru     h1p = data;
1037431Sigor@sysoev.ru 
1038637Sigor@sysoev.ru     nxt_debug(task, "h1p conn request body read");
1039431Sigor@sysoev.ru 
10401403Smax.romanov@nginx.com     r = h1p->request;
1041431Sigor@sysoev.ru 
1042637Sigor@sysoev.ru     engine = task->thread->engine;
1043637Sigor@sysoev.ru 
10441403Smax.romanov@nginx.com     b = c->read;
10451403Smax.romanov@nginx.com 
10461403Smax.romanov@nginx.com     if (nxt_buf_is_file(b)) {
10471403Smax.romanov@nginx.com         body_rest = b->file->size - b->file_end;
10481403Smax.romanov@nginx.com 
10491403Smax.romanov@nginx.com         size = nxt_buf_mem_used_size(&b->mem);
10501403Smax.romanov@nginx.com         size = nxt_min(size, body_rest);
10511403Smax.romanov@nginx.com 
10521403Smax.romanov@nginx.com         res = nxt_fd_write(b->file->fd, b->mem.pos, size);
10531403Smax.romanov@nginx.com         if (nxt_slow_path(res < (ssize_t) size)) {
10541403Smax.romanov@nginx.com             nxt_h1p_request_error(task, h1p, r);
10551403Smax.romanov@nginx.com             return;
10561403Smax.romanov@nginx.com         }
10571403Smax.romanov@nginx.com 
10581403Smax.romanov@nginx.com         b->file_end += size;
10591403Smax.romanov@nginx.com         body_rest -= res;
10601403Smax.romanov@nginx.com 
10611403Smax.romanov@nginx.com         b->mem.pos += size;
10621403Smax.romanov@nginx.com 
10631403Smax.romanov@nginx.com         if (b->mem.pos == b->mem.free) {
10641403Smax.romanov@nginx.com             if (body_rest >= (size_t) nxt_buf_mem_size(&b->mem)) {
10651403Smax.romanov@nginx.com                 b->mem.free = b->mem.start;
10661403Smax.romanov@nginx.com 
10671403Smax.romanov@nginx.com             } else {
10681403Smax.romanov@nginx.com                 /* This required to avoid reading next request. */
10691403Smax.romanov@nginx.com                 b->mem.free = b->mem.end - body_rest;
10701403Smax.romanov@nginx.com             }
10711403Smax.romanov@nginx.com 
10721403Smax.romanov@nginx.com             b->mem.pos = b->mem.free;
10731403Smax.romanov@nginx.com         }
10741403Smax.romanov@nginx.com 
10751403Smax.romanov@nginx.com     } else {
10761403Smax.romanov@nginx.com         body_rest = nxt_buf_mem_free_size(&c->read->mem);
10771403Smax.romanov@nginx.com     }
10781403Smax.romanov@nginx.com 
10791403Smax.romanov@nginx.com     nxt_debug(task, "h1p body rest: %uz", body_rest);
10801403Smax.romanov@nginx.com 
10811403Smax.romanov@nginx.com     if (body_rest != 0) {
1082637Sigor@sysoev.ru         nxt_conn_read(engine, c);
1083431Sigor@sysoev.ru 
1084431Sigor@sysoev.ru     } else {
10851403Smax.romanov@nginx.com         if (nxt_buf_is_file(b)) {
10861403Smax.romanov@nginx.com             b->mem.start = NULL;
10871403Smax.romanov@nginx.com             b->mem.end = NULL;
10881403Smax.romanov@nginx.com             b->mem.pos = NULL;
10891403Smax.romanov@nginx.com             b->mem.free = NULL;
10901403Smax.romanov@nginx.com         }
10911403Smax.romanov@nginx.com 
1092637Sigor@sysoev.ru         c->read = NULL;
1093637Sigor@sysoev.ru 
10941155Smax.romanov@nginx.com         r->state->ready_handler(task, r, NULL);
1095431Sigor@sysoev.ru     }
1096431Sigor@sysoev.ru }
1097431Sigor@sysoev.ru 
1098431Sigor@sysoev.ru 
1099431Sigor@sysoev.ru static void
nxt_h1p_request_local_addr(nxt_task_t * task,nxt_http_request_t * r)1100431Sigor@sysoev.ru nxt_h1p_request_local_addr(nxt_task_t *task, nxt_http_request_t *r)
1101431Sigor@sysoev.ru {
1102431Sigor@sysoev.ru     r->local = nxt_conn_local_addr(task, r->proto.h1->conn);
1103431Sigor@sysoev.ru }
1104431Sigor@sysoev.ru 
1105431Sigor@sysoev.ru 
11061131Smax.romanov@nginx.com #define NXT_HTTP_LAST_INFORMATIONAL                                           \
11071131Smax.romanov@nginx.com     (NXT_HTTP_CONTINUE + nxt_nitems(nxt_http_informational) - 1)
11081131Smax.romanov@nginx.com 
11091131Smax.romanov@nginx.com static const nxt_str_t  nxt_http_informational[] = {
11101131Smax.romanov@nginx.com     nxt_string("HTTP/1.1 100 Continue\r\n"),
11111131Smax.romanov@nginx.com     nxt_string("HTTP/1.1 101 Switching Protocols\r\n"),
11121131Smax.romanov@nginx.com };
11131131Smax.romanov@nginx.com 
11141131Smax.romanov@nginx.com 
1115431Sigor@sysoev.ru #define NXT_HTTP_LAST_SUCCESS                                                 \
1116431Sigor@sysoev.ru     (NXT_HTTP_OK + nxt_nitems(nxt_http_success) - 1)
1117431Sigor@sysoev.ru 
1118431Sigor@sysoev.ru static const nxt_str_t  nxt_http_success[] = {
1119431Sigor@sysoev.ru     nxt_string("HTTP/1.1 200 OK\r\n"),
1120431Sigor@sysoev.ru     nxt_string("HTTP/1.1 201 Created\r\n"),
1121431Sigor@sysoev.ru     nxt_string("HTTP/1.1 202 Accepted\r\n"),
1122431Sigor@sysoev.ru     nxt_string("HTTP/1.1 203 Non-Authoritative Information\r\n"),
1123431Sigor@sysoev.ru     nxt_string("HTTP/1.1 204 No Content\r\n"),
1124431Sigor@sysoev.ru     nxt_string("HTTP/1.1 205 Reset Content\r\n"),
1125431Sigor@sysoev.ru     nxt_string("HTTP/1.1 206 Partial Content\r\n"),
1126431Sigor@sysoev.ru };
1127431Sigor@sysoev.ru 
1128431Sigor@sysoev.ru 
1129431Sigor@sysoev.ru #define NXT_HTTP_LAST_REDIRECTION                                             \
1130431Sigor@sysoev.ru     (NXT_HTTP_MULTIPLE_CHOICES + nxt_nitems(nxt_http_redirection) - 1)
1131431Sigor@sysoev.ru 
1132431Sigor@sysoev.ru static const nxt_str_t  nxt_http_redirection[] = {
1133431Sigor@sysoev.ru     nxt_string("HTTP/1.1 300 Multiple Choices\r\n"),
1134431Sigor@sysoev.ru     nxt_string("HTTP/1.1 301 Moved Permanently\r\n"),
1135431Sigor@sysoev.ru     nxt_string("HTTP/1.1 302 Found\r\n"),
1136431Sigor@sysoev.ru     nxt_string("HTTP/1.1 303 See Other\r\n"),
1137431Sigor@sysoev.ru     nxt_string("HTTP/1.1 304 Not Modified\r\n"),
11381432Svbart@nginx.com     nxt_string("HTTP/1.1 307 Temporary Redirect\r\n"),
11391432Svbart@nginx.com     nxt_string("HTTP/1.1 308 Permanent Redirect\r\n"),
1140431Sigor@sysoev.ru };
1141431Sigor@sysoev.ru 
1142431Sigor@sysoev.ru 
1143431Sigor@sysoev.ru #define NXT_HTTP_LAST_CLIENT_ERROR                                            \
1144431Sigor@sysoev.ru     (NXT_HTTP_BAD_REQUEST + nxt_nitems(nxt_http_client_error) - 1)
1145431Sigor@sysoev.ru 
1146431Sigor@sysoev.ru static const nxt_str_t  nxt_http_client_error[] = {
1147431Sigor@sysoev.ru     nxt_string("HTTP/1.1 400 Bad Request\r\n"),
1148431Sigor@sysoev.ru     nxt_string("HTTP/1.1 401 Unauthorized\r\n"),
1149431Sigor@sysoev.ru     nxt_string("HTTP/1.1 402 Payment Required\r\n"),
1150431Sigor@sysoev.ru     nxt_string("HTTP/1.1 403 Forbidden\r\n"),
1151431Sigor@sysoev.ru     nxt_string("HTTP/1.1 404 Not Found\r\n"),
1152431Sigor@sysoev.ru     nxt_string("HTTP/1.1 405 Method Not Allowed\r\n"),
1153431Sigor@sysoev.ru     nxt_string("HTTP/1.1 406 Not Acceptable\r\n"),
1154431Sigor@sysoev.ru     nxt_string("HTTP/1.1 407 Proxy Authentication Required\r\n"),
1155431Sigor@sysoev.ru     nxt_string("HTTP/1.1 408 Request Timeout\r\n"),
1156431Sigor@sysoev.ru     nxt_string("HTTP/1.1 409 Conflict\r\n"),
1157431Sigor@sysoev.ru     nxt_string("HTTP/1.1 410 Gone\r\n"),
1158431Sigor@sysoev.ru     nxt_string("HTTP/1.1 411 Length Required\r\n"),
1159431Sigor@sysoev.ru     nxt_string("HTTP/1.1 412 Precondition Failed\r\n"),
1160431Sigor@sysoev.ru     nxt_string("HTTP/1.1 413 Payload Too Large\r\n"),
1161431Sigor@sysoev.ru     nxt_string("HTTP/1.1 414 URI Too Long\r\n"),
1162431Sigor@sysoev.ru     nxt_string("HTTP/1.1 415 Unsupported Media Type\r\n"),
1163431Sigor@sysoev.ru     nxt_string("HTTP/1.1 416 Range Not Satisfiable\r\n"),
1164431Sigor@sysoev.ru     nxt_string("HTTP/1.1 417 Expectation Failed\r\n"),
11651732Svbart@nginx.com     nxt_string("HTTP/1.1 418 I'm a teapot\r\n"),
11661732Svbart@nginx.com     nxt_string("HTTP/1.1 419 \r\n"),
11671732Svbart@nginx.com     nxt_string("HTTP/1.1 420 \r\n"),
11681732Svbart@nginx.com     nxt_string("HTTP/1.1 421 Misdirected Request\r\n"),
11691732Svbart@nginx.com     nxt_string("HTTP/1.1 422 Unprocessable Entity\r\n"),
11701732Svbart@nginx.com     nxt_string("HTTP/1.1 423 Locked\r\n"),
11711732Svbart@nginx.com     nxt_string("HTTP/1.1 424 Failed Dependency\r\n"),
11721732Svbart@nginx.com     nxt_string("HTTP/1.1 425 \r\n"),
11731131Smax.romanov@nginx.com     nxt_string("HTTP/1.1 426 Upgrade Required\r\n"),
11741732Svbart@nginx.com     nxt_string("HTTP/1.1 427 \r\n"),
11751732Svbart@nginx.com     nxt_string("HTTP/1.1 428 \r\n"),
11761732Svbart@nginx.com     nxt_string("HTTP/1.1 429 \r\n"),
11771732Svbart@nginx.com     nxt_string("HTTP/1.1 430 \r\n"),
1178431Sigor@sysoev.ru     nxt_string("HTTP/1.1 431 Request Header Fields Too Large\r\n"),
1179431Sigor@sysoev.ru };
1180431Sigor@sysoev.ru 
1181431Sigor@sysoev.ru 
1182772Sigor@sysoev.ru #define NXT_HTTP_LAST_NGINX_ERROR                                             \
1183772Sigor@sysoev.ru     (NXT_HTTP_TO_HTTPS + nxt_nitems(nxt_http_nginx_error) - 1)
1184772Sigor@sysoev.ru 
1185772Sigor@sysoev.ru static const nxt_str_t  nxt_http_nginx_error[] = {
1186772Sigor@sysoev.ru     nxt_string("HTTP/1.1 400 "
1187772Sigor@sysoev.ru                "The plain HTTP request was sent to HTTPS port\r\n"),
1188772Sigor@sysoev.ru };
1189772Sigor@sysoev.ru 
1190772Sigor@sysoev.ru 
1191431Sigor@sysoev.ru #define NXT_HTTP_LAST_SERVER_ERROR                                            \
1192431Sigor@sysoev.ru     (NXT_HTTP_INTERNAL_SERVER_ERROR + nxt_nitems(nxt_http_server_error) - 1)
1193431Sigor@sysoev.ru 
1194431Sigor@sysoev.ru static const nxt_str_t  nxt_http_server_error[] = {
1195431Sigor@sysoev.ru     nxt_string("HTTP/1.1 500 Internal Server Error\r\n"),
1196431Sigor@sysoev.ru     nxt_string("HTTP/1.1 501 Not Implemented\r\n"),
1197431Sigor@sysoev.ru     nxt_string("HTTP/1.1 502 Bad Gateway\r\n"),
1198431Sigor@sysoev.ru     nxt_string("HTTP/1.1 503 Service Unavailable\r\n"),
1199431Sigor@sysoev.ru     nxt_string("HTTP/1.1 504 Gateway Timeout\r\n"),
1200482Svbart@nginx.com     nxt_string("HTTP/1.1 505 HTTP Version Not Supported\r\n"),
1201431Sigor@sysoev.ru };
1202431Sigor@sysoev.ru 
1203431Sigor@sysoev.ru 
12041732Svbart@nginx.com #define UNKNOWN_STATUS_LENGTH  nxt_length("HTTP/1.1 999 \r\n")
1205431Sigor@sysoev.ru 
1206431Sigor@sysoev.ru static void
nxt_h1p_request_header_send(nxt_task_t * task,nxt_http_request_t * r,nxt_work_handler_t body_handler,void * data)12071148Sigor@sysoev.ru nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r,
12081270Sigor@sysoev.ru     nxt_work_handler_t body_handler, void *data)
1209431Sigor@sysoev.ru {
1210431Sigor@sysoev.ru     u_char              *p;
1211431Sigor@sysoev.ru     size_t              size;
1212431Sigor@sysoev.ru     nxt_buf_t           *header;
1213431Sigor@sysoev.ru     nxt_str_t           unknown_status;
1214431Sigor@sysoev.ru     nxt_int_t           conn;
1215431Sigor@sysoev.ru     nxt_uint_t          n;
1216431Sigor@sysoev.ru     nxt_bool_t          http11;
1217431Sigor@sysoev.ru     nxt_conn_t          *c;
1218431Sigor@sysoev.ru     nxt_h1proto_t       *h1p;
1219431Sigor@sysoev.ru     const nxt_str_t     *status;
1220431Sigor@sysoev.ru     nxt_http_field_t    *field;
1221431Sigor@sysoev.ru     u_char              buf[UNKNOWN_STATUS_LENGTH];
1222431Sigor@sysoev.ru 
1223431Sigor@sysoev.ru     static const char   chunked[] = "Transfer-Encoding: chunked\r\n";
12241131Smax.romanov@nginx.com     static const char   websocket_version[] = "Sec-WebSocket-Version: 13\r\n";
1225431Sigor@sysoev.ru 
12261131Smax.romanov@nginx.com     static const nxt_str_t  connection[3] = {
1227431Sigor@sysoev.ru         nxt_string("Connection: close\r\n"),
1228431Sigor@sysoev.ru         nxt_string("Connection: keep-alive\r\n"),
12291131Smax.romanov@nginx.com         nxt_string("Upgrade: websocket\r\n"
12301131Smax.romanov@nginx.com                    "Connection: Upgrade\r\n"
12311131Smax.romanov@nginx.com                    "Sec-WebSocket-Accept: "),
1232431Sigor@sysoev.ru     };
1233431Sigor@sysoev.ru 
1234431Sigor@sysoev.ru     nxt_debug(task, "h1p request header send");
1235431Sigor@sysoev.ru 
1236431Sigor@sysoev.ru     r->header_sent = 1;
1237431Sigor@sysoev.ru     h1p = r->proto.h1;
1238431Sigor@sysoev.ru     n = r->status;
1239431Sigor@sysoev.ru 
12401131Smax.romanov@nginx.com     if (n >= NXT_HTTP_CONTINUE && n <= NXT_HTTP_LAST_INFORMATIONAL) {
12411131Smax.romanov@nginx.com         status = &nxt_http_informational[n - NXT_HTTP_CONTINUE];
12421131Smax.romanov@nginx.com 
12431131Smax.romanov@nginx.com     } else if (n >= NXT_HTTP_OK && n <= NXT_HTTP_LAST_SUCCESS) {
1244431Sigor@sysoev.ru         status = &nxt_http_success[n - NXT_HTTP_OK];
1245431Sigor@sysoev.ru 
1246431Sigor@sysoev.ru     } else if (n >= NXT_HTTP_MULTIPLE_CHOICES
1247431Sigor@sysoev.ru                && n <= NXT_HTTP_LAST_REDIRECTION)
1248431Sigor@sysoev.ru     {
1249431Sigor@sysoev.ru         status = &nxt_http_redirection[n - NXT_HTTP_MULTIPLE_CHOICES];
1250431Sigor@sysoev.ru 
1251431Sigor@sysoev.ru     } else if (n >= NXT_HTTP_BAD_REQUEST && n <= NXT_HTTP_LAST_CLIENT_ERROR) {
1252431Sigor@sysoev.ru         status = &nxt_http_client_error[n - NXT_HTTP_BAD_REQUEST];
1253431Sigor@sysoev.ru 
1254772Sigor@sysoev.ru     } else if (n >= NXT_HTTP_TO_HTTPS && n <= NXT_HTTP_LAST_NGINX_ERROR) {
1255772Sigor@sysoev.ru         status = &nxt_http_nginx_error[n - NXT_HTTP_TO_HTTPS];
1256772Sigor@sysoev.ru 
1257431Sigor@sysoev.ru     } else if (n >= NXT_HTTP_INTERNAL_SERVER_ERROR
1258431Sigor@sysoev.ru                && n <= NXT_HTTP_LAST_SERVER_ERROR)
1259431Sigor@sysoev.ru     {
1260431Sigor@sysoev.ru         status = &nxt_http_server_error[n - NXT_HTTP_INTERNAL_SERVER_ERROR];
1261431Sigor@sysoev.ru 
12621732Svbart@nginx.com     } else if (n <= NXT_HTTP_STATUS_MAX) {
12631732Svbart@nginx.com         (void) nxt_sprintf(buf, buf + UNKNOWN_STATUS_LENGTH,
12641732Svbart@nginx.com                            "HTTP/1.1 %03d \r\n", n);
12651732Svbart@nginx.com 
12661732Svbart@nginx.com         unknown_status.length = UNKNOWN_STATUS_LENGTH;
1267431Sigor@sysoev.ru         unknown_status.start = buf;
1268431Sigor@sysoev.ru         status = &unknown_status;
12691732Svbart@nginx.com 
12701732Svbart@nginx.com     } else {
12711732Svbart@nginx.com         status = &nxt_http_server_error[0];
1272431Sigor@sysoev.ru     }
1273431Sigor@sysoev.ru 
1274450Sigor@sysoev.ru     size = status->length;
1275450Sigor@sysoev.ru     /* Trailing CRLF at the end of header. */
1276703Svbart@nginx.com     size += nxt_length("\r\n");
1277431Sigor@sysoev.ru 
12781131Smax.romanov@nginx.com     conn = -1;
1279431Sigor@sysoev.ru 
12801131Smax.romanov@nginx.com     if (r->websocket_handshake && n == NXT_HTTP_SWITCHING_PROTOCOLS) {
12811131Smax.romanov@nginx.com         h1p->websocket = 1;
12821131Smax.romanov@nginx.com         h1p->keepalive = 0;
12831131Smax.romanov@nginx.com         conn = 2;
12841131Smax.romanov@nginx.com         size += NXT_WEBSOCKET_ACCEPT_SIZE + 2;
12851131Smax.romanov@nginx.com 
12861131Smax.romanov@nginx.com     } else {
1287*2505Sz.hong@f5.com         http11 = nxt_h1p_is_http11(h1p);
12881131Smax.romanov@nginx.com 
12891131Smax.romanov@nginx.com         if (r->resp.content_length == NULL || r->resp.content_length->skip) {
1290793Sigor@sysoev.ru 
12911131Smax.romanov@nginx.com             if (http11) {
12921131Smax.romanov@nginx.com                 if (n != NXT_HTTP_NOT_MODIFIED
12931131Smax.romanov@nginx.com                     && n != NXT_HTTP_NO_CONTENT
12941148Sigor@sysoev.ru                     && body_handler != NULL
12951131Smax.romanov@nginx.com                     && !h1p->websocket)
12961131Smax.romanov@nginx.com                 {
12971131Smax.romanov@nginx.com                     h1p->chunked = 1;
12981131Smax.romanov@nginx.com                     size += nxt_length(chunked);
12991131Smax.romanov@nginx.com                     /* Trailing CRLF will be added by the first chunk header. */
13001131Smax.romanov@nginx.com                     size -= nxt_length("\r\n");
13011131Smax.romanov@nginx.com                 }
13021131Smax.romanov@nginx.com 
13031131Smax.romanov@nginx.com             } else {
13041131Smax.romanov@nginx.com                 h1p->keepalive = 0;
1305797Svbart@nginx.com             }
13061131Smax.romanov@nginx.com         }
1307431Sigor@sysoev.ru 
13081131Smax.romanov@nginx.com         if (http11 ^ h1p->keepalive) {
13091131Smax.romanov@nginx.com             conn = h1p->keepalive;
1310431Sigor@sysoev.ru         }
1311431Sigor@sysoev.ru     }
1312431Sigor@sysoev.ru 
13131131Smax.romanov@nginx.com     if (conn >= 0) {
1314431Sigor@sysoev.ru         size += connection[conn].length;
1315431Sigor@sysoev.ru     }
1316431Sigor@sysoev.ru 
1317431Sigor@sysoev.ru     nxt_list_each(field, r->resp.fields) {
1318431Sigor@sysoev.ru 
1319431Sigor@sysoev.ru         if (!field->skip) {
1320431Sigor@sysoev.ru             size += field->name_length + field->value_length;
1321703Svbart@nginx.com             size += nxt_length(": \r\n");
1322431Sigor@sysoev.ru         }
1323431Sigor@sysoev.ru 
1324431Sigor@sysoev.ru     } nxt_list_loop;
1325431Sigor@sysoev.ru 
13261131Smax.romanov@nginx.com     if (nxt_slow_path(n == NXT_HTTP_UPGRADE_REQUIRED)) {
13271131Smax.romanov@nginx.com         size += nxt_length(websocket_version);
13281131Smax.romanov@nginx.com     }
13291131Smax.romanov@nginx.com 
1330608Sigor@sysoev.ru     header = nxt_http_buf_mem(task, r, size);
1331431Sigor@sysoev.ru     if (nxt_slow_path(header == NULL)) {
1332725Sigor@sysoev.ru         nxt_h1p_request_error(task, h1p, r);
1333431Sigor@sysoev.ru         return;
1334431Sigor@sysoev.ru     }
1335431Sigor@sysoev.ru 
13361131Smax.romanov@nginx.com     p = nxt_cpymem(header->mem.free, status->start, status->length);
1337431Sigor@sysoev.ru 
1338431Sigor@sysoev.ru     nxt_list_each(field, r->resp.fields) {
1339431Sigor@sysoev.ru 
1340431Sigor@sysoev.ru         if (!field->skip) {
1341431Sigor@sysoev.ru             p = nxt_cpymem(p, field->name, field->name_length);
1342431Sigor@sysoev.ru             *p++ = ':'; *p++ = ' ';
1343431Sigor@sysoev.ru             p = nxt_cpymem(p, field->value, field->value_length);
1344431Sigor@sysoev.ru             *p++ = '\r'; *p++ = '\n';
1345431Sigor@sysoev.ru         }
1346431Sigor@sysoev.ru 
1347431Sigor@sysoev.ru     } nxt_list_loop;
1348431Sigor@sysoev.ru 
1349431Sigor@sysoev.ru     if (conn >= 0) {
1350431Sigor@sysoev.ru         p = nxt_cpymem(p, connection[conn].start, connection[conn].length);
1351431Sigor@sysoev.ru     }
1352431Sigor@sysoev.ru 
13531131Smax.romanov@nginx.com     if (h1p->websocket) {
13541131Smax.romanov@nginx.com         nxt_websocket_accept(p, h1p->websocket_key->value);
13551131Smax.romanov@nginx.com         p += NXT_WEBSOCKET_ACCEPT_SIZE;
13561131Smax.romanov@nginx.com 
13571131Smax.romanov@nginx.com         *p++ = '\r'; *p++ = '\n';
13581131Smax.romanov@nginx.com     }
13591131Smax.romanov@nginx.com 
13601131Smax.romanov@nginx.com     if (nxt_slow_path(n == NXT_HTTP_UPGRADE_REQUIRED)) {
13611131Smax.romanov@nginx.com         p = nxt_cpymem(p, websocket_version, nxt_length(websocket_version));
13621131Smax.romanov@nginx.com     }
13631131Smax.romanov@nginx.com 
1364431Sigor@sysoev.ru     if (h1p->chunked) {
1365703Svbart@nginx.com         p = nxt_cpymem(p, chunked, nxt_length(chunked));
1366450Sigor@sysoev.ru         /* Trailing CRLF will be added by the first chunk header. */
1367431Sigor@sysoev.ru 
1368431Sigor@sysoev.ru     } else {
1369431Sigor@sysoev.ru         *p++ = '\r'; *p++ = '\n';
1370431Sigor@sysoev.ru     }
1371431Sigor@sysoev.ru 
1372431Sigor@sysoev.ru     header->mem.free = p;
1373431Sigor@sysoev.ru 
1374630Svbart@nginx.com     h1p->header_size = nxt_buf_mem_used_size(&header->mem);
1375630Svbart@nginx.com 
1376431Sigor@sysoev.ru     c = h1p->conn;
1377431Sigor@sysoev.ru 
1378431Sigor@sysoev.ru     c->write = header;
13791318Smax.romanov@nginx.com     h1p->conn_write_tail = &header->next;
1380740Sigor@sysoev.ru     c->write_state = &nxt_h1p_request_send_state;
1381431Sigor@sysoev.ru 
13821148Sigor@sysoev.ru     if (body_handler != NULL) {
13831148Sigor@sysoev.ru         /*
13841148Sigor@sysoev.ru          * The body handler will run before c->io->write() handler,
13851148Sigor@sysoev.ru          * because the latter was inqueued by nxt_conn_write()
13861148Sigor@sysoev.ru          * in engine->write_work_queue.
13871148Sigor@sysoev.ru          */
13881148Sigor@sysoev.ru         nxt_work_queue_add(&task->thread->engine->fast_work_queue,
13891270Sigor@sysoev.ru                            body_handler, task, r, data);
13901148Sigor@sysoev.ru 
13911148Sigor@sysoev.ru     } else {
13921148Sigor@sysoev.ru         header->next = nxt_http_buf_last(r);
13931148Sigor@sysoev.ru     }
13941148Sigor@sysoev.ru 
13951127Smax.romanov@nginx.com     nxt_conn_write(task->thread->engine, c);
13961131Smax.romanov@nginx.com 
13971131Smax.romanov@nginx.com     if (h1p->websocket) {
13981131Smax.romanov@nginx.com         nxt_h1p_websocket_first_frame_start(task, r, c->read);
13991131Smax.romanov@nginx.com     }
14001131Smax.romanov@nginx.com }
14011131Smax.romanov@nginx.com 
14021131Smax.romanov@nginx.com 
14031131Smax.romanov@nginx.com void
nxt_h1p_complete_buffers(nxt_task_t * task,nxt_h1proto_t * h1p,nxt_bool_t all)14041417Smax.romanov@nginx.com nxt_h1p_complete_buffers(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_bool_t all)
14051131Smax.romanov@nginx.com {
14061417Smax.romanov@nginx.com     size_t            size;
14071417Smax.romanov@nginx.com     nxt_buf_t         *b, *in, *next;
14081417Smax.romanov@nginx.com     nxt_conn_t        *c;
14091131Smax.romanov@nginx.com 
14101131Smax.romanov@nginx.com     nxt_debug(task, "h1p complete buffers");
14111131Smax.romanov@nginx.com 
14121131Smax.romanov@nginx.com     b = h1p->buffers;
14131131Smax.romanov@nginx.com     c = h1p->conn;
14141131Smax.romanov@nginx.com     in = c->read;
14151131Smax.romanov@nginx.com 
14161131Smax.romanov@nginx.com     if (b != NULL) {
14171131Smax.romanov@nginx.com         if (in == NULL) {
14181131Smax.romanov@nginx.com             /* A request with large body. */
14191131Smax.romanov@nginx.com             in = b;
14201131Smax.romanov@nginx.com             c->read = in;
14211131Smax.romanov@nginx.com 
14221131Smax.romanov@nginx.com             b = in->next;
14231131Smax.romanov@nginx.com             in->next = NULL;
14241131Smax.romanov@nginx.com         }
14251131Smax.romanov@nginx.com 
14261131Smax.romanov@nginx.com         while (b != NULL) {
14271131Smax.romanov@nginx.com             next = b->next;
14281269Sigor@sysoev.ru             b->next = NULL;
14291131Smax.romanov@nginx.com 
14301418Smax.romanov@nginx.com             b->completion_handler(task, b, b->parent);
14311131Smax.romanov@nginx.com 
14321131Smax.romanov@nginx.com             b = next;
14331131Smax.romanov@nginx.com         }
14341131Smax.romanov@nginx.com 
14351131Smax.romanov@nginx.com         h1p->buffers = NULL;
14361131Smax.romanov@nginx.com         h1p->nbuffers = 0;
14371131Smax.romanov@nginx.com     }
14381131Smax.romanov@nginx.com 
14391131Smax.romanov@nginx.com     if (in != NULL) {
14401131Smax.romanov@nginx.com         size = nxt_buf_mem_used_size(&in->mem);
14411131Smax.romanov@nginx.com 
14421417Smax.romanov@nginx.com         if (size == 0 || all) {
14431418Smax.romanov@nginx.com             in->completion_handler(task, in, in->parent);
14441131Smax.romanov@nginx.com 
14451131Smax.romanov@nginx.com             c->read = NULL;
14461131Smax.romanov@nginx.com         }
14471131Smax.romanov@nginx.com     }
1448431Sigor@sysoev.ru }
1449431Sigor@sysoev.ru 
1450431Sigor@sysoev.ru 
1451740Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_request_send_state
1452431Sigor@sysoev.ru     nxt_aligned(64) =
1453431Sigor@sysoev.ru {
1454740Sigor@sysoev.ru     .ready_handler = nxt_h1p_conn_sent,
1455624Sigor@sysoev.ru     .error_handler = nxt_h1p_conn_request_error,
1456431Sigor@sysoev.ru 
1457626Sigor@sysoev.ru     .timer_handler = nxt_h1p_conn_request_send_timeout,
1458771Sigor@sysoev.ru     .timer_value = nxt_h1p_conn_request_timer_value,
1459431Sigor@sysoev.ru     .timer_data = offsetof(nxt_socket_conf_t, send_timeout),
1460431Sigor@sysoev.ru     .timer_autoreset = 1,
1461431Sigor@sysoev.ru };
1462431Sigor@sysoev.ru 
1463431Sigor@sysoev.ru 
1464431Sigor@sysoev.ru static void
nxt_h1p_request_send(nxt_task_t * task,nxt_http_request_t * r,nxt_buf_t * out)1465431Sigor@sysoev.ru nxt_h1p_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out)
1466431Sigor@sysoev.ru {
1467725Sigor@sysoev.ru     nxt_conn_t     *c;
1468725Sigor@sysoev.ru     nxt_h1proto_t  *h1p;
1469431Sigor@sysoev.ru 
1470431Sigor@sysoev.ru     nxt_debug(task, "h1p request send");
1471431Sigor@sysoev.ru 
1472725Sigor@sysoev.ru     h1p = r->proto.h1;
1473725Sigor@sysoev.ru     c = h1p->conn;
1474431Sigor@sysoev.ru 
1475725Sigor@sysoev.ru     if (h1p->chunked) {
1476431Sigor@sysoev.ru         out = nxt_h1p_chunk_create(task, r, out);
1477431Sigor@sysoev.ru         if (nxt_slow_path(out == NULL)) {
1478725Sigor@sysoev.ru             nxt_h1p_request_error(task, h1p, r);
1479431Sigor@sysoev.ru             return;
1480431Sigor@sysoev.ru         }
1481431Sigor@sysoev.ru     }
1482431Sigor@sysoev.ru 
1483431Sigor@sysoev.ru     if (c->write == NULL) {
1484431Sigor@sysoev.ru         c->write = out;
1485740Sigor@sysoev.ru         c->write_state = &nxt_h1p_request_send_state;
1486431Sigor@sysoev.ru 
1487431Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
1488431Sigor@sysoev.ru 
1489431Sigor@sysoev.ru     } else {
14901318Smax.romanov@nginx.com         *h1p->conn_write_tail = out;
1491431Sigor@sysoev.ru     }
14921318Smax.romanov@nginx.com 
14931318Smax.romanov@nginx.com     while (out->next != NULL) {
14941318Smax.romanov@nginx.com         out = out->next;
14951318Smax.romanov@nginx.com     }
14961318Smax.romanov@nginx.com 
14971318Smax.romanov@nginx.com     h1p->conn_write_tail = &out->next;
1498431Sigor@sysoev.ru }
1499431Sigor@sysoev.ru 
1500431Sigor@sysoev.ru 
1501431Sigor@sysoev.ru static nxt_buf_t *
nxt_h1p_chunk_create(nxt_task_t * task,nxt_http_request_t * r,nxt_buf_t * out)1502431Sigor@sysoev.ru nxt_h1p_chunk_create(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out)
1503431Sigor@sysoev.ru {
1504483Sigor@sysoev.ru     nxt_off_t          size;
1505431Sigor@sysoev.ru     nxt_buf_t          *b, **prev, *header, *tail;
1506431Sigor@sysoev.ru 
1507703Svbart@nginx.com     const size_t       chunk_size = 2 * nxt_length("\r\n") + NXT_OFF_T_HEXLEN;
1508431Sigor@sysoev.ru     static const char  tail_chunk[] = "\r\n0\r\n\r\n";
1509431Sigor@sysoev.ru 
1510431Sigor@sysoev.ru     size = 0;
1511431Sigor@sysoev.ru     prev = &out;
1512431Sigor@sysoev.ru 
1513431Sigor@sysoev.ru     for (b = out; b != NULL; b = b->next) {
1514431Sigor@sysoev.ru 
1515431Sigor@sysoev.ru         if (nxt_buf_is_last(b)) {
15161215Svbart@nginx.com             tail = nxt_http_buf_mem(task, r, sizeof(tail_chunk));
1517431Sigor@sysoev.ru             if (nxt_slow_path(tail == NULL)) {
1518431Sigor@sysoev.ru                 return NULL;
1519431Sigor@sysoev.ru             }
1520431Sigor@sysoev.ru 
1521431Sigor@sysoev.ru             *prev = tail;
1522431Sigor@sysoev.ru             tail->next = b;
1523431Sigor@sysoev.ru             /*
1524431Sigor@sysoev.ru              * The tail_chunk size with trailing zero is 8 bytes, so
1525431Sigor@sysoev.ru              * memcpy may be inlined with just single 8 byte move operation.
1526431Sigor@sysoev.ru              */
1527431Sigor@sysoev.ru             nxt_memcpy(tail->mem.free, tail_chunk, sizeof(tail_chunk));
1528703Svbart@nginx.com             tail->mem.free += nxt_length(tail_chunk);
1529431Sigor@sysoev.ru 
1530431Sigor@sysoev.ru             break;
1531431Sigor@sysoev.ru         }
1532431Sigor@sysoev.ru 
1533431Sigor@sysoev.ru         size += nxt_buf_used_size(b);
1534431Sigor@sysoev.ru         prev = &b->next;
1535431Sigor@sysoev.ru     }
1536431Sigor@sysoev.ru 
1537431Sigor@sysoev.ru     if (size == 0) {
1538431Sigor@sysoev.ru         return out;
1539431Sigor@sysoev.ru     }
1540431Sigor@sysoev.ru 
1541608Sigor@sysoev.ru     header = nxt_http_buf_mem(task, r, chunk_size);
1542431Sigor@sysoev.ru     if (nxt_slow_path(header == NULL)) {
1543431Sigor@sysoev.ru         return NULL;
1544431Sigor@sysoev.ru     }
1545431Sigor@sysoev.ru 
1546431Sigor@sysoev.ru     header->next = out;
1547431Sigor@sysoev.ru     header->mem.free = nxt_sprintf(header->mem.free, header->mem.end,
1548431Sigor@sysoev.ru                                    "\r\n%xO\r\n", size);
1549431Sigor@sysoev.ru     return header;
1550431Sigor@sysoev.ru }
1551431Sigor@sysoev.ru 
1552431Sigor@sysoev.ru 
1553630Svbart@nginx.com static nxt_off_t
nxt_h1p_request_body_bytes_sent(nxt_task_t * task,nxt_http_proto_t proto)1554630Svbart@nginx.com nxt_h1p_request_body_bytes_sent(nxt_task_t *task, nxt_http_proto_t proto)
1555630Svbart@nginx.com {
1556630Svbart@nginx.com     nxt_off_t      sent;
1557630Svbart@nginx.com     nxt_h1proto_t  *h1p;
1558630Svbart@nginx.com 
1559630Svbart@nginx.com     h1p = proto.h1;
1560630Svbart@nginx.com 
1561630Svbart@nginx.com     sent = h1p->conn->sent - h1p->header_size;
1562630Svbart@nginx.com 
1563630Svbart@nginx.com     return (sent > 0) ? sent : 0;
1564630Svbart@nginx.com }
1565630Svbart@nginx.com 
1566630Svbart@nginx.com 
1567431Sigor@sysoev.ru static void
nxt_h1p_request_discard(nxt_task_t * task,nxt_http_request_t * r,nxt_buf_t * last)1568608Sigor@sysoev.ru nxt_h1p_request_discard(nxt_task_t *task, nxt_http_request_t *r,
1569608Sigor@sysoev.ru     nxt_buf_t *last)
1570608Sigor@sysoev.ru {
1571608Sigor@sysoev.ru     nxt_buf_t         *b;
1572608Sigor@sysoev.ru     nxt_conn_t        *c;
1573608Sigor@sysoev.ru     nxt_h1proto_t     *h1p;
1574608Sigor@sysoev.ru     nxt_work_queue_t  *wq;
1575608Sigor@sysoev.ru 
1576608Sigor@sysoev.ru     nxt_debug(task, "h1p request discard");
1577608Sigor@sysoev.ru 
1578608Sigor@sysoev.ru     h1p = r->proto.h1;
1579608Sigor@sysoev.ru     h1p->keepalive = 0;
1580608Sigor@sysoev.ru 
1581608Sigor@sysoev.ru     c = h1p->conn;
1582608Sigor@sysoev.ru     b = c->write;
1583608Sigor@sysoev.ru     c->write = NULL;
1584608Sigor@sysoev.ru 
1585608Sigor@sysoev.ru     wq = &task->thread->engine->fast_work_queue;
1586608Sigor@sysoev.ru 
1587608Sigor@sysoev.ru     nxt_sendbuf_drain(task, wq, b);
1588608Sigor@sysoev.ru     nxt_sendbuf_drain(task, wq, last);
1589608Sigor@sysoev.ru }
1590608Sigor@sysoev.ru 
1591608Sigor@sysoev.ru 
1592608Sigor@sysoev.ru static void
nxt_h1p_conn_request_error(nxt_task_t * task,void * obj,void * data)1593624Sigor@sysoev.ru nxt_h1p_conn_request_error(nxt_task_t *task, void *obj, void *data)
1594624Sigor@sysoev.ru {
1595624Sigor@sysoev.ru     nxt_h1proto_t       *h1p;
1596624Sigor@sysoev.ru     nxt_http_request_t  *r;
1597624Sigor@sysoev.ru 
1598624Sigor@sysoev.ru     h1p = data;
1599624Sigor@sysoev.ru 
1600624Sigor@sysoev.ru     nxt_debug(task, "h1p conn request error");
1601624Sigor@sysoev.ru 
1602624Sigor@sysoev.ru     r = h1p->request;
1603624Sigor@sysoev.ru 
16041131Smax.romanov@nginx.com     if (nxt_slow_path(r == NULL)) {
16051131Smax.romanov@nginx.com         nxt_h1p_shutdown(task, h1p->conn);
16061131Smax.romanov@nginx.com         return;
16071131Smax.romanov@nginx.com     }
16081131Smax.romanov@nginx.com 
1609624Sigor@sysoev.ru     if (r->fields == NULL) {
16101131Smax.romanov@nginx.com         (void) nxt_h1p_header_process(task, h1p, r);
1611624Sigor@sysoev.ru     }
1612624Sigor@sysoev.ru 
1613624Sigor@sysoev.ru     if (r->status == 0) {
1614624Sigor@sysoev.ru         r->status = NXT_HTTP_BAD_REQUEST;
1615624Sigor@sysoev.ru     }
1616624Sigor@sysoev.ru 
1617725Sigor@sysoev.ru     nxt_h1p_request_error(task, h1p, r);
1618624Sigor@sysoev.ru }
1619624Sigor@sysoev.ru 
1620624Sigor@sysoev.ru 
1621624Sigor@sysoev.ru static void
nxt_h1p_conn_request_timeout(nxt_task_t * task,void * obj,void * data)1622624Sigor@sysoev.ru nxt_h1p_conn_request_timeout(nxt_task_t *task, void *obj, void *data)
1623624Sigor@sysoev.ru {
1624624Sigor@sysoev.ru     nxt_conn_t          *c;
1625624Sigor@sysoev.ru     nxt_timer_t         *timer;
1626624Sigor@sysoev.ru     nxt_h1proto_t       *h1p;
1627624Sigor@sysoev.ru     nxt_http_request_t  *r;
1628624Sigor@sysoev.ru 
1629624Sigor@sysoev.ru     timer = obj;
1630624Sigor@sysoev.ru 
1631624Sigor@sysoev.ru     nxt_debug(task, "h1p conn request timeout");
1632624Sigor@sysoev.ru 
1633624Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
1634979Sigor@sysoev.ru     c->block_read = 1;
1635626Sigor@sysoev.ru     /*
1636626Sigor@sysoev.ru      * Disable SO_LINGER off during socket closing
1637626Sigor@sysoev.ru      * to send "408 Request Timeout" error response.
1638626Sigor@sysoev.ru      */
1639626Sigor@sysoev.ru     c->socket.timedout = 0;
1640626Sigor@sysoev.ru 
1641624Sigor@sysoev.ru     h1p = c->socket.data;
1642725Sigor@sysoev.ru     h1p->keepalive = 0;
1643624Sigor@sysoev.ru     r = h1p->request;
1644624Sigor@sysoev.ru 
1645624Sigor@sysoev.ru     if (r->fields == NULL) {
16461131Smax.romanov@nginx.com         (void) nxt_h1p_header_process(task, h1p, r);
1647624Sigor@sysoev.ru     }
1648624Sigor@sysoev.ru 
1649626Sigor@sysoev.ru     nxt_http_request_error(task, r, NXT_HTTP_REQUEST_TIMEOUT);
1650626Sigor@sysoev.ru }
1651626Sigor@sysoev.ru 
1652624Sigor@sysoev.ru 
1653626Sigor@sysoev.ru static void
nxt_h1p_conn_request_send_timeout(nxt_task_t * task,void * obj,void * data)1654626Sigor@sysoev.ru nxt_h1p_conn_request_send_timeout(nxt_task_t *task, void *obj, void *data)
1655626Sigor@sysoev.ru {
1656626Sigor@sysoev.ru     nxt_conn_t     *c;
1657626Sigor@sysoev.ru     nxt_timer_t    *timer;
1658626Sigor@sysoev.ru     nxt_h1proto_t  *h1p;
1659626Sigor@sysoev.ru 
1660626Sigor@sysoev.ru     timer = obj;
1661626Sigor@sysoev.ru 
1662626Sigor@sysoev.ru     nxt_debug(task, "h1p conn request send timeout");
1663626Sigor@sysoev.ru 
1664724Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
1665979Sigor@sysoev.ru     c->block_write = 1;
1666626Sigor@sysoev.ru     h1p = c->socket.data;
1667626Sigor@sysoev.ru 
1668725Sigor@sysoev.ru     nxt_h1p_request_error(task, h1p, h1p->request);
1669624Sigor@sysoev.ru }
1670624Sigor@sysoev.ru 
1671624Sigor@sysoev.ru 
16721131Smax.romanov@nginx.com nxt_msec_t
nxt_h1p_conn_request_timer_value(nxt_conn_t * c,uintptr_t data)1673771Sigor@sysoev.ru nxt_h1p_conn_request_timer_value(nxt_conn_t *c, uintptr_t data)
1674683Sigor@sysoev.ru {
1675683Sigor@sysoev.ru     nxt_h1proto_t  *h1p;
1676683Sigor@sysoev.ru 
1677683Sigor@sysoev.ru     h1p = c->socket.data;
1678683Sigor@sysoev.ru 
1679683Sigor@sysoev.ru     return nxt_value_at(nxt_msec_t, h1p->request->conf->socket_conf, data);
1680683Sigor@sysoev.ru }
1681683Sigor@sysoev.ru 
1682683Sigor@sysoev.ru 
1683624Sigor@sysoev.ru nxt_inline void
nxt_h1p_request_error(nxt_task_t * task,nxt_h1proto_t * h1p,nxt_http_request_t * r)1684725Sigor@sysoev.ru nxt_h1p_request_error(nxt_task_t *task, nxt_h1proto_t *h1p,
1685725Sigor@sysoev.ru     nxt_http_request_t *r)
1686624Sigor@sysoev.ru {
1687725Sigor@sysoev.ru     h1p->keepalive = 0;
1688725Sigor@sysoev.ru 
1689725Sigor@sysoev.ru     r->state->error_handler(task, r, h1p);
1690431Sigor@sysoev.ru }
1691771Sigor@sysoev.ru 
1692771Sigor@sysoev.ru 
1693771Sigor@sysoev.ru static void
nxt_h1p_request_close(nxt_task_t * task,nxt_http_proto_t proto,nxt_socket_conf_joint_t * joint)1694771Sigor@sysoev.ru nxt_h1p_request_close(nxt_task_t *task, nxt_http_proto_t proto,
1695771Sigor@sysoev.ru     nxt_socket_conf_joint_t *joint)
1696771Sigor@sysoev.ru {
1697771Sigor@sysoev.ru     nxt_conn_t     *c;
1698771Sigor@sysoev.ru     nxt_h1proto_t  *h1p;
1699771Sigor@sysoev.ru 
1700771Sigor@sysoev.ru     nxt_debug(task, "h1p request close");
1701771Sigor@sysoev.ru 
1702771Sigor@sysoev.ru     h1p = proto.h1;
17031271Sigor@sysoev.ru     h1p->keepalive &= !h1p->request->inconsistent;
1704771Sigor@sysoev.ru     h1p->request = NULL;
1705771Sigor@sysoev.ru 
1706771Sigor@sysoev.ru     nxt_router_conf_release(task, joint);
1707771Sigor@sysoev.ru 
1708771Sigor@sysoev.ru     c = h1p->conn;
17091265Sigor@sysoev.ru     task = &c->task;
17101265Sigor@sysoev.ru     c->socket.task = task;
17111265Sigor@sysoev.ru     c->read_timer.task = task;
17121265Sigor@sysoev.ru     c->write_timer.task = task;
1713771Sigor@sysoev.ru 
1714771Sigor@sysoev.ru     if (h1p->keepalive) {
1715771Sigor@sysoev.ru         nxt_h1p_keepalive(task, h1p, c);
1716771Sigor@sysoev.ru 
1717771Sigor@sysoev.ru     } else {
1718771Sigor@sysoev.ru         nxt_h1p_shutdown(task, c);
1719771Sigor@sysoev.ru     }
1720771Sigor@sysoev.ru }
1721771Sigor@sysoev.ru 
1722771Sigor@sysoev.ru 
1723771Sigor@sysoev.ru static void
nxt_h1p_conn_sent(nxt_task_t * task,void * obj,void * data)1724771Sigor@sysoev.ru nxt_h1p_conn_sent(nxt_task_t *task, void *obj, void *data)
1725771Sigor@sysoev.ru {
1726771Sigor@sysoev.ru     nxt_conn_t          *c;
1727771Sigor@sysoev.ru     nxt_event_engine_t  *engine;
1728771Sigor@sysoev.ru 
1729771Sigor@sysoev.ru     c = obj;
1730771Sigor@sysoev.ru 
1731771Sigor@sysoev.ru     nxt_debug(task, "h1p conn sent");
1732771Sigor@sysoev.ru 
1733771Sigor@sysoev.ru     engine = task->thread->engine;
1734771Sigor@sysoev.ru 
1735771Sigor@sysoev.ru     c->write = nxt_sendbuf_completion(task, &engine->fast_work_queue, c->write);
1736771Sigor@sysoev.ru 
1737771Sigor@sysoev.ru     if (c->write != NULL) {
1738771Sigor@sysoev.ru         nxt_conn_write(engine, c);
1739771Sigor@sysoev.ru     }
1740771Sigor@sysoev.ru }
1741771Sigor@sysoev.ru 
1742771Sigor@sysoev.ru 
1743771Sigor@sysoev.ru static void
nxt_h1p_conn_close(nxt_task_t * task,void * obj,void * data)1744771Sigor@sysoev.ru nxt_h1p_conn_close(nxt_task_t *task, void *obj, void *data)
1745771Sigor@sysoev.ru {
1746771Sigor@sysoev.ru     nxt_conn_t  *c;
1747771Sigor@sysoev.ru 
1748771Sigor@sysoev.ru     c = obj;
1749771Sigor@sysoev.ru 
1750771Sigor@sysoev.ru     nxt_debug(task, "h1p conn close");
1751771Sigor@sysoev.ru 
17522185Svbart@nginx.com     nxt_conn_active(task->thread->engine, c);
17531457Sigor@sysoev.ru 
1754771Sigor@sysoev.ru     nxt_h1p_shutdown(task, c);
1755771Sigor@sysoev.ru }
1756771Sigor@sysoev.ru 
1757771Sigor@sysoev.ru 
1758771Sigor@sysoev.ru static void
nxt_h1p_conn_error(nxt_task_t * task,void * obj,void * data)1759771Sigor@sysoev.ru nxt_h1p_conn_error(nxt_task_t *task, void *obj, void *data)
1760771Sigor@sysoev.ru {
1761771Sigor@sysoev.ru     nxt_conn_t  *c;
1762771Sigor@sysoev.ru 
1763771Sigor@sysoev.ru     c = obj;
1764771Sigor@sysoev.ru 
1765771Sigor@sysoev.ru     nxt_debug(task, "h1p conn error");
1766771Sigor@sysoev.ru 
17672185Svbart@nginx.com     nxt_conn_active(task->thread->engine, c);
17681457Sigor@sysoev.ru 
1769771Sigor@sysoev.ru     nxt_h1p_shutdown(task, c);
1770771Sigor@sysoev.ru }
1771771Sigor@sysoev.ru 
1772771Sigor@sysoev.ru 
1773771Sigor@sysoev.ru static nxt_msec_t
nxt_h1p_conn_timer_value(nxt_conn_t * c,uintptr_t data)1774771Sigor@sysoev.ru nxt_h1p_conn_timer_value(nxt_conn_t *c, uintptr_t data)
1775771Sigor@sysoev.ru {
1776771Sigor@sysoev.ru     nxt_socket_conf_joint_t  *joint;
1777771Sigor@sysoev.ru 
1778771Sigor@sysoev.ru     joint = c->listen->socket.data;
1779771Sigor@sysoev.ru 
17801599Sigor@sysoev.ru     if (nxt_fast_path(joint != NULL)) {
17811599Sigor@sysoev.ru         return nxt_value_at(nxt_msec_t, joint->socket_conf, data);
17821599Sigor@sysoev.ru     }
17831599Sigor@sysoev.ru 
17841599Sigor@sysoev.ru     /*
17851599Sigor@sysoev.ru      * Listening socket had been closed while
17861599Sigor@sysoev.ru      * connection was in keep-alive state.
17871599Sigor@sysoev.ru      */
17881599Sigor@sysoev.ru     return 1;
1789771Sigor@sysoev.ru }
1790771Sigor@sysoev.ru 
1791771Sigor@sysoev.ru 
1792771Sigor@sysoev.ru static void
nxt_h1p_keepalive(nxt_task_t * task,nxt_h1proto_t * h1p,nxt_conn_t * c)1793771Sigor@sysoev.ru nxt_h1p_keepalive(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_conn_t *c)
1794771Sigor@sysoev.ru {
17951457Sigor@sysoev.ru     size_t              size;
17961457Sigor@sysoev.ru     nxt_buf_t           *in;
17971457Sigor@sysoev.ru     nxt_event_engine_t  *engine;
1798771Sigor@sysoev.ru 
1799771Sigor@sysoev.ru     nxt_debug(task, "h1p keepalive");
1800771Sigor@sysoev.ru 
1801771Sigor@sysoev.ru     if (!c->tcp_nodelay) {
1802771Sigor@sysoev.ru         nxt_conn_tcp_nodelay_on(task, c);
1803771Sigor@sysoev.ru     }
1804771Sigor@sysoev.ru 
18051417Smax.romanov@nginx.com     nxt_h1p_complete_buffers(task, h1p, 0);
18061131Smax.romanov@nginx.com 
18071131Smax.romanov@nginx.com     in = c->read;
1808771Sigor@sysoev.ru 
1809771Sigor@sysoev.ru     nxt_memzero(h1p, offsetof(nxt_h1proto_t, conn));
1810771Sigor@sysoev.ru 
1811771Sigor@sysoev.ru     c->sent = 0;
1812771Sigor@sysoev.ru 
18131457Sigor@sysoev.ru     engine = task->thread->engine;
18142185Svbart@nginx.com 
18152185Svbart@nginx.com     nxt_conn_idle(engine, c);
18161457Sigor@sysoev.ru 
1817771Sigor@sysoev.ru     if (in == NULL) {
1818771Sigor@sysoev.ru         c->read_state = &nxt_h1p_keepalive_state;
1819771Sigor@sysoev.ru 
18201457Sigor@sysoev.ru         nxt_conn_read(engine, c);
1821771Sigor@sysoev.ru 
1822771Sigor@sysoev.ru     } else {
18231131Smax.romanov@nginx.com         size = nxt_buf_mem_used_size(&in->mem);
18241131Smax.romanov@nginx.com 
1825771Sigor@sysoev.ru         nxt_debug(task, "h1p pipelining");
1826771Sigor@sysoev.ru 
1827771Sigor@sysoev.ru         nxt_memmove(in->mem.start, in->mem.pos, size);
1828771Sigor@sysoev.ru 
1829771Sigor@sysoev.ru         in->mem.pos = in->mem.start;
1830771Sigor@sysoev.ru         in->mem.free = in->mem.start + size;
1831771Sigor@sysoev.ru 
1832771Sigor@sysoev.ru         nxt_h1p_conn_request_init(task, c, c->socket.data);
1833771Sigor@sysoev.ru     }
1834771Sigor@sysoev.ru }
1835771Sigor@sysoev.ru 
1836771Sigor@sysoev.ru 
1837771Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_keepalive_state
1838771Sigor@sysoev.ru     nxt_aligned(64) =
1839771Sigor@sysoev.ru {
1840771Sigor@sysoev.ru     .ready_handler = nxt_h1p_conn_request_init,
1841771Sigor@sysoev.ru     .close_handler = nxt_h1p_conn_close,
1842771Sigor@sysoev.ru     .error_handler = nxt_h1p_conn_error,
1843771Sigor@sysoev.ru 
1844771Sigor@sysoev.ru     .io_read_handler = nxt_h1p_idle_io_read_handler,
1845771Sigor@sysoev.ru 
1846771Sigor@sysoev.ru     .timer_handler = nxt_h1p_idle_timeout,
1847771Sigor@sysoev.ru     .timer_value = nxt_h1p_conn_timer_value,
1848771Sigor@sysoev.ru     .timer_data = offsetof(nxt_socket_conf_t, idle_timeout),
1849771Sigor@sysoev.ru     .timer_autoreset = 1,
1850771Sigor@sysoev.ru };
1851771Sigor@sysoev.ru 
1852771Sigor@sysoev.ru 
18531131Smax.romanov@nginx.com const nxt_conn_state_t  nxt_h1p_idle_close_state
1854771Sigor@sysoev.ru     nxt_aligned(64) =
1855771Sigor@sysoev.ru {
1856771Sigor@sysoev.ru     .close_handler = nxt_h1p_idle_close,
1857771Sigor@sysoev.ru };
1858771Sigor@sysoev.ru 
1859771Sigor@sysoev.ru 
1860771Sigor@sysoev.ru static void
nxt_h1p_idle_close(nxt_task_t * task,void * obj,void * data)1861771Sigor@sysoev.ru nxt_h1p_idle_close(nxt_task_t *task, void *obj, void *data)
1862771Sigor@sysoev.ru {
1863771Sigor@sysoev.ru     nxt_conn_t  *c;
1864771Sigor@sysoev.ru 
1865771Sigor@sysoev.ru     c = obj;
1866771Sigor@sysoev.ru 
1867771Sigor@sysoev.ru     nxt_debug(task, "h1p idle close");
1868771Sigor@sysoev.ru 
18692185Svbart@nginx.com     nxt_conn_active(task->thread->engine, c);
18701598Sigor@sysoev.ru 
1871771Sigor@sysoev.ru     nxt_h1p_idle_response(task, c);
1872771Sigor@sysoev.ru }
1873771Sigor@sysoev.ru 
1874771Sigor@sysoev.ru 
1875771Sigor@sysoev.ru static void
nxt_h1p_idle_timeout(nxt_task_t * task,void * obj,void * data)1876771Sigor@sysoev.ru nxt_h1p_idle_timeout(nxt_task_t *task, void *obj, void *data)
1877771Sigor@sysoev.ru {
1878771Sigor@sysoev.ru     nxt_conn_t   *c;
1879771Sigor@sysoev.ru     nxt_timer_t  *timer;
1880771Sigor@sysoev.ru 
1881771Sigor@sysoev.ru     timer = obj;
1882771Sigor@sysoev.ru 
1883771Sigor@sysoev.ru     nxt_debug(task, "h1p idle timeout");
1884771Sigor@sysoev.ru 
1885771Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
1886979Sigor@sysoev.ru     c->block_read = 1;
1887771Sigor@sysoev.ru 
18882185Svbart@nginx.com     nxt_conn_active(task->thread->engine, c);
18891457Sigor@sysoev.ru 
1890771Sigor@sysoev.ru     nxt_h1p_idle_response(task, c);
1891771Sigor@sysoev.ru }
1892771Sigor@sysoev.ru 
1893771Sigor@sysoev.ru 
1894771Sigor@sysoev.ru #define NXT_H1P_IDLE_TIMEOUT                                                  \
18952078Salx.manpages@gmail.com     "HTTP/1.1 408 Request Timeout\r\n"                                        \
18962078Salx.manpages@gmail.com     "Server: " NXT_SERVER "\r\n"                                              \
18972078Salx.manpages@gmail.com     "Connection: close\r\n"                                                   \
18982078Salx.manpages@gmail.com     "Content-Length: 0\r\n"                                                   \
18992078Salx.manpages@gmail.com     "Date: "
1900771Sigor@sysoev.ru 
1901771Sigor@sysoev.ru 
1902771Sigor@sysoev.ru static void
nxt_h1p_idle_response(nxt_task_t * task,nxt_conn_t * c)1903771Sigor@sysoev.ru nxt_h1p_idle_response(nxt_task_t *task, nxt_conn_t *c)
1904771Sigor@sysoev.ru {
19051600Sigor@sysoev.ru     u_char     *p;
19061600Sigor@sysoev.ru     size_t     size;
19071600Sigor@sysoev.ru     nxt_buf_t  *out, *last;
1908771Sigor@sysoev.ru 
1909771Sigor@sysoev.ru     size = nxt_length(NXT_H1P_IDLE_TIMEOUT)
1910771Sigor@sysoev.ru            + nxt_http_date_cache.size
1911771Sigor@sysoev.ru            + nxt_length("\r\n\r\n");
1912771Sigor@sysoev.ru 
1913771Sigor@sysoev.ru     out = nxt_buf_mem_alloc(c->mem_pool, size, 0);
1914771Sigor@sysoev.ru     if (nxt_slow_path(out == NULL)) {
1915771Sigor@sysoev.ru         goto fail;
1916771Sigor@sysoev.ru     }
1917771Sigor@sysoev.ru 
1918771Sigor@sysoev.ru     p = nxt_cpymem(out->mem.free, NXT_H1P_IDLE_TIMEOUT,
1919771Sigor@sysoev.ru                    nxt_length(NXT_H1P_IDLE_TIMEOUT));
1920771Sigor@sysoev.ru 
1921771Sigor@sysoev.ru     p = nxt_thread_time_string(task->thread, &nxt_http_date_cache, p);
1922771Sigor@sysoev.ru 
1923771Sigor@sysoev.ru     out->mem.free = nxt_cpymem(p, "\r\n\r\n", 4);
1924771Sigor@sysoev.ru 
1925771Sigor@sysoev.ru     last = nxt_mp_zget(c->mem_pool, NXT_BUF_SYNC_SIZE);
1926771Sigor@sysoev.ru     if (nxt_slow_path(last == NULL)) {
1927771Sigor@sysoev.ru         goto fail;
1928771Sigor@sysoev.ru     }
1929771Sigor@sysoev.ru 
1930771Sigor@sysoev.ru     out->next = last;
1931771Sigor@sysoev.ru     nxt_buf_set_sync(last);
1932771Sigor@sysoev.ru     nxt_buf_set_last(last);
1933771Sigor@sysoev.ru 
1934771Sigor@sysoev.ru     last->completion_handler = nxt_h1p_idle_response_sent;
1935771Sigor@sysoev.ru     last->parent = c;
1936771Sigor@sysoev.ru 
1937771Sigor@sysoev.ru     c->write = out;
1938771Sigor@sysoev.ru     c->write_state = &nxt_h1p_timeout_response_state;
1939771Sigor@sysoev.ru 
1940771Sigor@sysoev.ru     nxt_conn_write(task->thread->engine, c);
1941771Sigor@sysoev.ru     return;
1942771Sigor@sysoev.ru 
1943771Sigor@sysoev.ru fail:
1944771Sigor@sysoev.ru 
1945771Sigor@sysoev.ru     nxt_h1p_shutdown(task, c);
1946771Sigor@sysoev.ru }
1947771Sigor@sysoev.ru 
1948771Sigor@sysoev.ru 
1949771Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_timeout_response_state
1950771Sigor@sysoev.ru     nxt_aligned(64) =
1951771Sigor@sysoev.ru {
1952771Sigor@sysoev.ru     .ready_handler = nxt_h1p_conn_sent,
19531457Sigor@sysoev.ru     .error_handler = nxt_h1p_idle_response_error,
1954771Sigor@sysoev.ru 
1955771Sigor@sysoev.ru     .timer_handler = nxt_h1p_idle_response_timeout,
1956771Sigor@sysoev.ru     .timer_value = nxt_h1p_idle_response_timer_value,
1957771Sigor@sysoev.ru };
1958771Sigor@sysoev.ru 
1959771Sigor@sysoev.ru 
1960771Sigor@sysoev.ru static void
nxt_h1p_idle_response_sent(nxt_task_t * task,void * obj,void * data)1961771Sigor@sysoev.ru nxt_h1p_idle_response_sent(nxt_task_t *task, void *obj, void *data)
1962771Sigor@sysoev.ru {
1963771Sigor@sysoev.ru     nxt_conn_t  *c;
1964771Sigor@sysoev.ru 
1965771Sigor@sysoev.ru     c = data;
1966771Sigor@sysoev.ru 
1967771Sigor@sysoev.ru     nxt_debug(task, "h1p idle timeout response sent");
1968771Sigor@sysoev.ru 
1969771Sigor@sysoev.ru     nxt_h1p_shutdown(task, c);
1970771Sigor@sysoev.ru }
1971771Sigor@sysoev.ru 
1972771Sigor@sysoev.ru 
1973771Sigor@sysoev.ru static void
nxt_h1p_idle_response_error(nxt_task_t * task,void * obj,void * data)19741457Sigor@sysoev.ru nxt_h1p_idle_response_error(nxt_task_t *task, void *obj, void *data)
19751457Sigor@sysoev.ru {
19761457Sigor@sysoev.ru     nxt_conn_t  *c;
19771457Sigor@sysoev.ru 
19781457Sigor@sysoev.ru     c = obj;
19791457Sigor@sysoev.ru 
19801457Sigor@sysoev.ru     nxt_debug(task, "h1p response error");
19811457Sigor@sysoev.ru 
19821457Sigor@sysoev.ru     nxt_h1p_shutdown(task, c);
19831457Sigor@sysoev.ru }
19841457Sigor@sysoev.ru 
19851457Sigor@sysoev.ru 
19861457Sigor@sysoev.ru static void
nxt_h1p_idle_response_timeout(nxt_task_t * task,void * obj,void * data)1987771Sigor@sysoev.ru nxt_h1p_idle_response_timeout(nxt_task_t *task, void *obj, void *data)
1988771Sigor@sysoev.ru {
1989771Sigor@sysoev.ru     nxt_conn_t   *c;
1990771Sigor@sysoev.ru     nxt_timer_t  *timer;
1991771Sigor@sysoev.ru 
1992771Sigor@sysoev.ru     timer = obj;
1993771Sigor@sysoev.ru 
1994771Sigor@sysoev.ru     nxt_debug(task, "h1p idle timeout response timeout");
1995771Sigor@sysoev.ru 
1996771Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
1997979Sigor@sysoev.ru     c->block_write = 1;
1998771Sigor@sysoev.ru 
1999771Sigor@sysoev.ru     nxt_h1p_shutdown(task, c);
2000771Sigor@sysoev.ru }
2001771Sigor@sysoev.ru 
2002771Sigor@sysoev.ru 
2003771Sigor@sysoev.ru static nxt_msec_t
nxt_h1p_idle_response_timer_value(nxt_conn_t * c,uintptr_t data)2004771Sigor@sysoev.ru nxt_h1p_idle_response_timer_value(nxt_conn_t *c, uintptr_t data)
2005771Sigor@sysoev.ru {
2006771Sigor@sysoev.ru     return 10 * 1000;
2007771Sigor@sysoev.ru }
2008771Sigor@sysoev.ru 
2009771Sigor@sysoev.ru 
2010771Sigor@sysoev.ru static void
nxt_h1p_shutdown(nxt_task_t * task,nxt_conn_t * c)2011771Sigor@sysoev.ru nxt_h1p_shutdown(nxt_task_t *task, nxt_conn_t *c)
2012771Sigor@sysoev.ru {
20131131Smax.romanov@nginx.com     nxt_timer_t    *timer;
20141131Smax.romanov@nginx.com     nxt_h1proto_t  *h1p;
20151131Smax.romanov@nginx.com 
2016771Sigor@sysoev.ru     nxt_debug(task, "h1p shutdown");
2017771Sigor@sysoev.ru 
20181131Smax.romanov@nginx.com     h1p = c->socket.data;
20191131Smax.romanov@nginx.com 
20201417Smax.romanov@nginx.com     if (h1p != NULL) {
20211417Smax.romanov@nginx.com         nxt_h1p_complete_buffers(task, h1p, 1);
20221417Smax.romanov@nginx.com 
20231417Smax.romanov@nginx.com         if (nxt_slow_path(h1p->websocket_timer != NULL)) {
20241417Smax.romanov@nginx.com             timer = &h1p->websocket_timer->timer;
20251417Smax.romanov@nginx.com 
20261417Smax.romanov@nginx.com             if (timer->handler != nxt_h1p_conn_ws_shutdown) {
20271417Smax.romanov@nginx.com                 timer->handler = nxt_h1p_conn_ws_shutdown;
20281417Smax.romanov@nginx.com                 nxt_timer_add(task->thread->engine, timer, 0);
20291417Smax.romanov@nginx.com 
20301417Smax.romanov@nginx.com             } else {
20311417Smax.romanov@nginx.com                 nxt_debug(task, "h1p already scheduled ws shutdown");
20321417Smax.romanov@nginx.com             }
20331417Smax.romanov@nginx.com 
20341417Smax.romanov@nginx.com             return;
20351131Smax.romanov@nginx.com         }
20361131Smax.romanov@nginx.com     }
20371417Smax.romanov@nginx.com 
20381417Smax.romanov@nginx.com     nxt_h1p_closing(task, c);
20391131Smax.romanov@nginx.com }
20401131Smax.romanov@nginx.com 
20411131Smax.romanov@nginx.com 
20421131Smax.romanov@nginx.com static void
nxt_h1p_conn_ws_shutdown(nxt_task_t * task,void * obj,void * data)20431270Sigor@sysoev.ru nxt_h1p_conn_ws_shutdown(nxt_task_t *task, void *obj, void *data)
20441131Smax.romanov@nginx.com {
20451270Sigor@sysoev.ru     nxt_timer_t                *timer;
20461270Sigor@sysoev.ru     nxt_h1p_websocket_timer_t  *ws_timer;
20471270Sigor@sysoev.ru 
20481270Sigor@sysoev.ru     nxt_debug(task, "h1p conn ws shutdown");
20491270Sigor@sysoev.ru 
20501270Sigor@sysoev.ru     timer = obj;
20511270Sigor@sysoev.ru     ws_timer = nxt_timer_data(timer, nxt_h1p_websocket_timer_t, timer);
20521270Sigor@sysoev.ru 
20531270Sigor@sysoev.ru     nxt_h1p_closing(task, ws_timer->h1p->conn);
20541270Sigor@sysoev.ru }
20551270Sigor@sysoev.ru 
20561270Sigor@sysoev.ru 
20571270Sigor@sysoev.ru static void
nxt_h1p_closing(nxt_task_t * task,nxt_conn_t * c)20581270Sigor@sysoev.ru nxt_h1p_closing(nxt_task_t *task, nxt_conn_t *c)
20591270Sigor@sysoev.ru {
20601270Sigor@sysoev.ru     nxt_debug(task, "h1p closing");
20611270Sigor@sysoev.ru 
2062771Sigor@sysoev.ru     c->socket.data = NULL;
2063771Sigor@sysoev.ru 
2064771Sigor@sysoev.ru #if (NXT_TLS)
2065771Sigor@sysoev.ru 
2066771Sigor@sysoev.ru     if (c->u.tls != NULL) {
2067771Sigor@sysoev.ru         c->write_state = &nxt_h1p_shutdown_state;
2068771Sigor@sysoev.ru 
2069771Sigor@sysoev.ru         c->io->shutdown(task, c, NULL);
2070771Sigor@sysoev.ru         return;
2071771Sigor@sysoev.ru     }
2072771Sigor@sysoev.ru 
2073771Sigor@sysoev.ru #endif
2074771Sigor@sysoev.ru 
2075771Sigor@sysoev.ru     nxt_h1p_conn_closing(task, c, NULL);
2076771Sigor@sysoev.ru }
2077771Sigor@sysoev.ru 
2078771Sigor@sysoev.ru 
2079771Sigor@sysoev.ru #if (NXT_TLS)
2080771Sigor@sysoev.ru 
2081771Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_shutdown_state
2082771Sigor@sysoev.ru     nxt_aligned(64) =
2083771Sigor@sysoev.ru {
2084771Sigor@sysoev.ru     .ready_handler = nxt_h1p_conn_closing,
2085771Sigor@sysoev.ru     .close_handler = nxt_h1p_conn_closing,
2086771Sigor@sysoev.ru     .error_handler = nxt_h1p_conn_closing,
2087771Sigor@sysoev.ru };
2088771Sigor@sysoev.ru 
2089771Sigor@sysoev.ru #endif
2090771Sigor@sysoev.ru 
2091771Sigor@sysoev.ru 
2092771Sigor@sysoev.ru static void
nxt_h1p_conn_closing(nxt_task_t * task,void * obj,void * data)2093771Sigor@sysoev.ru nxt_h1p_conn_closing(nxt_task_t *task, void *obj, void *data)
2094771Sigor@sysoev.ru {
2095771Sigor@sysoev.ru     nxt_conn_t  *c;
2096771Sigor@sysoev.ru 
2097771Sigor@sysoev.ru     c = obj;
2098771Sigor@sysoev.ru 
2099771Sigor@sysoev.ru     nxt_debug(task, "h1p conn closing");
2100771Sigor@sysoev.ru 
2101771Sigor@sysoev.ru     c->write_state = &nxt_h1p_close_state;
2102771Sigor@sysoev.ru 
2103771Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
2104771Sigor@sysoev.ru }
2105771Sigor@sysoev.ru 
2106771Sigor@sysoev.ru 
2107771Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_close_state
2108771Sigor@sysoev.ru     nxt_aligned(64) =
2109771Sigor@sysoev.ru {
2110771Sigor@sysoev.ru     .ready_handler = nxt_h1p_conn_free,
2111771Sigor@sysoev.ru };
2112771Sigor@sysoev.ru 
2113771Sigor@sysoev.ru 
2114771Sigor@sysoev.ru static void
nxt_h1p_conn_free(nxt_task_t * task,void * obj,void * data)2115771Sigor@sysoev.ru nxt_h1p_conn_free(nxt_task_t *task, void *obj, void *data)
2116771Sigor@sysoev.ru {
2117771Sigor@sysoev.ru     nxt_conn_t          *c;
2118771Sigor@sysoev.ru     nxt_listen_event_t  *lev;
2119771Sigor@sysoev.ru     nxt_event_engine_t  *engine;
2120771Sigor@sysoev.ru 
2121771Sigor@sysoev.ru     c = obj;
2122771Sigor@sysoev.ru 
2123771Sigor@sysoev.ru     nxt_debug(task, "h1p conn free");
2124771Sigor@sysoev.ru 
2125771Sigor@sysoev.ru     engine = task->thread->engine;
2126771Sigor@sysoev.ru 
2127771Sigor@sysoev.ru     nxt_sockaddr_cache_free(engine, c);
2128771Sigor@sysoev.ru 
2129771Sigor@sysoev.ru     lev = c->listen;
2130771Sigor@sysoev.ru 
2131771Sigor@sysoev.ru     nxt_conn_free(task, c);
2132771Sigor@sysoev.ru 
2133771Sigor@sysoev.ru     nxt_router_listen_event_release(&engine->task, lev, NULL);
2134771Sigor@sysoev.ru }
21351270Sigor@sysoev.ru 
21361270Sigor@sysoev.ru 
21371270Sigor@sysoev.ru static void
nxt_h1p_peer_connect(nxt_task_t * task,nxt_http_peer_t * peer)21381270Sigor@sysoev.ru nxt_h1p_peer_connect(nxt_task_t *task, nxt_http_peer_t *peer)
21391270Sigor@sysoev.ru {
21401270Sigor@sysoev.ru     nxt_mp_t            *mp;
21411270Sigor@sysoev.ru     nxt_int_t           ret;
21421270Sigor@sysoev.ru     nxt_conn_t          *c, *client;
21431270Sigor@sysoev.ru     nxt_h1proto_t       *h1p;
21441270Sigor@sysoev.ru     nxt_fd_event_t      *socket;
21451270Sigor@sysoev.ru     nxt_work_queue_t    *wq;
21461270Sigor@sysoev.ru     nxt_http_request_t  *r;
21471270Sigor@sysoev.ru 
21481270Sigor@sysoev.ru     nxt_debug(task, "h1p peer connect");
21491270Sigor@sysoev.ru 
21501270Sigor@sysoev.ru     peer->status = NXT_HTTP_UNSET;
21511270Sigor@sysoev.ru     r = peer->request;
21521270Sigor@sysoev.ru 
21531270Sigor@sysoev.ru     mp = nxt_mp_create(1024, 128, 256, 32);
21541270Sigor@sysoev.ru 
21551270Sigor@sysoev.ru     if (nxt_slow_path(mp == NULL)) {
21561270Sigor@sysoev.ru         goto fail;
21571270Sigor@sysoev.ru     }
21581270Sigor@sysoev.ru 
21591270Sigor@sysoev.ru     h1p = nxt_mp_zalloc(mp, sizeof(nxt_h1proto_t));
21601270Sigor@sysoev.ru     if (nxt_slow_path(h1p == NULL)) {
21611270Sigor@sysoev.ru         goto fail;
21621270Sigor@sysoev.ru     }
21631270Sigor@sysoev.ru 
21641270Sigor@sysoev.ru     ret = nxt_http_parse_request_init(&h1p->parser, r->mem_pool);
21651270Sigor@sysoev.ru     if (nxt_slow_path(ret != NXT_OK)) {
21661270Sigor@sysoev.ru         goto fail;
21671270Sigor@sysoev.ru     }
21681270Sigor@sysoev.ru 
21691270Sigor@sysoev.ru     c = nxt_conn_create(mp, task);
21701270Sigor@sysoev.ru     if (nxt_slow_path(c == NULL)) {
21711270Sigor@sysoev.ru         goto fail;
21721270Sigor@sysoev.ru     }
21731270Sigor@sysoev.ru 
21741270Sigor@sysoev.ru     c->mem_pool = mp;
21751270Sigor@sysoev.ru     h1p->conn = c;
21761270Sigor@sysoev.ru 
21771270Sigor@sysoev.ru     peer->proto.h1 = h1p;
21781270Sigor@sysoev.ru     h1p->request = r;
21791270Sigor@sysoev.ru 
21801270Sigor@sysoev.ru     c->socket.data = peer;
21811394Sigor@sysoev.ru     c->remote = peer->server->sockaddr;
21821270Sigor@sysoev.ru 
21831270Sigor@sysoev.ru     c->socket.write_ready = 1;
21841270Sigor@sysoev.ru     c->write_state = &nxt_h1p_peer_connect_state;
21851270Sigor@sysoev.ru 
21861270Sigor@sysoev.ru     /*
21871270Sigor@sysoev.ru      * TODO: queues should be implemented via client proto interface.
21881270Sigor@sysoev.ru      */
21891270Sigor@sysoev.ru     client = r->proto.h1->conn;
21901270Sigor@sysoev.ru 
21911270Sigor@sysoev.ru     socket = &client->socket;
21921270Sigor@sysoev.ru     wq = socket->read_work_queue;
21931270Sigor@sysoev.ru     c->read_work_queue = wq;
21941270Sigor@sysoev.ru     c->socket.read_work_queue = wq;
21951270Sigor@sysoev.ru     c->read_timer.work_queue = wq;
21961270Sigor@sysoev.ru 
21971270Sigor@sysoev.ru     wq = socket->write_work_queue;
21981270Sigor@sysoev.ru     c->write_work_queue = wq;
21991270Sigor@sysoev.ru     c->socket.write_work_queue = wq;
22001270Sigor@sysoev.ru     c->write_timer.work_queue = wq;
22011270Sigor@sysoev.ru     /* TODO END */
22021270Sigor@sysoev.ru 
22031270Sigor@sysoev.ru     nxt_conn_connect(task->thread->engine, c);
22041270Sigor@sysoev.ru 
22051270Sigor@sysoev.ru     return;
22061270Sigor@sysoev.ru 
22071270Sigor@sysoev.ru fail:
22081270Sigor@sysoev.ru 
22091270Sigor@sysoev.ru     peer->status = NXT_HTTP_INTERNAL_SERVER_ERROR;
22101270Sigor@sysoev.ru 
22111270Sigor@sysoev.ru     r->state->error_handler(task, r, peer);
22121270Sigor@sysoev.ru }
22131270Sigor@sysoev.ru 
22141270Sigor@sysoev.ru 
22151270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_connect_state
22161270Sigor@sysoev.ru     nxt_aligned(64) =
22171270Sigor@sysoev.ru {
22181270Sigor@sysoev.ru     .ready_handler = nxt_h1p_peer_connected,
22191270Sigor@sysoev.ru     .close_handler = nxt_h1p_peer_refused,
22201270Sigor@sysoev.ru     .error_handler = nxt_h1p_peer_error,
22211270Sigor@sysoev.ru 
22221270Sigor@sysoev.ru     .timer_handler = nxt_h1p_peer_send_timeout,
22231270Sigor@sysoev.ru     .timer_value = nxt_h1p_peer_timer_value,
22241270Sigor@sysoev.ru     .timer_data = offsetof(nxt_socket_conf_t, proxy_timeout),
22251270Sigor@sysoev.ru };
22261270Sigor@sysoev.ru 
22271270Sigor@sysoev.ru 
22281270Sigor@sysoev.ru static void
nxt_h1p_peer_connected(nxt_task_t * task,void * obj,void * data)22291270Sigor@sysoev.ru nxt_h1p_peer_connected(nxt_task_t *task, void *obj, void *data)
22301270Sigor@sysoev.ru {
22311270Sigor@sysoev.ru     nxt_http_peer_t     *peer;
22321270Sigor@sysoev.ru     nxt_http_request_t  *r;
22331270Sigor@sysoev.ru 
22341270Sigor@sysoev.ru     peer = data;
22351270Sigor@sysoev.ru 
22361270Sigor@sysoev.ru     nxt_debug(task, "h1p peer connected");
22371270Sigor@sysoev.ru 
22381270Sigor@sysoev.ru     r = peer->request;
22391270Sigor@sysoev.ru     r->state->ready_handler(task, r, peer);
22401270Sigor@sysoev.ru }
22411270Sigor@sysoev.ru 
22421270Sigor@sysoev.ru 
22431270Sigor@sysoev.ru static void
nxt_h1p_peer_refused(nxt_task_t * task,void * obj,void * data)22441270Sigor@sysoev.ru nxt_h1p_peer_refused(nxt_task_t *task, void *obj, void *data)
22451270Sigor@sysoev.ru {
22461270Sigor@sysoev.ru     nxt_http_peer_t     *peer;
22471270Sigor@sysoev.ru     nxt_http_request_t  *r;
22481270Sigor@sysoev.ru 
22491270Sigor@sysoev.ru     peer = data;
22501270Sigor@sysoev.ru 
22511270Sigor@sysoev.ru     nxt_debug(task, "h1p peer refused");
22521270Sigor@sysoev.ru 
22531270Sigor@sysoev.ru     //peer->status = NXT_HTTP_SERVICE_UNAVAILABLE;
22541270Sigor@sysoev.ru     peer->status = NXT_HTTP_BAD_GATEWAY;
22551270Sigor@sysoev.ru 
22561270Sigor@sysoev.ru     r = peer->request;
22571270Sigor@sysoev.ru     r->state->error_handler(task, r, peer);
22581270Sigor@sysoev.ru }
22591270Sigor@sysoev.ru 
22601270Sigor@sysoev.ru 
22611270Sigor@sysoev.ru static void
nxt_h1p_peer_header_send(nxt_task_t * task,nxt_http_peer_t * peer)22621270Sigor@sysoev.ru nxt_h1p_peer_header_send(nxt_task_t *task, nxt_http_peer_t *peer)
22631270Sigor@sysoev.ru {
22641270Sigor@sysoev.ru     u_char              *p;
22651270Sigor@sysoev.ru     size_t              size;
22661270Sigor@sysoev.ru     nxt_buf_t           *header, *body;
22671270Sigor@sysoev.ru     nxt_conn_t          *c;
22681270Sigor@sysoev.ru     nxt_http_field_t    *field;
22691270Sigor@sysoev.ru     nxt_http_request_t  *r;
22701270Sigor@sysoev.ru 
22711270Sigor@sysoev.ru     nxt_debug(task, "h1p peer header send");
22721270Sigor@sysoev.ru 
22731270Sigor@sysoev.ru     r = peer->request;
22741270Sigor@sysoev.ru 
22751270Sigor@sysoev.ru     size = r->method->length + sizeof(" ") + r->target.length
22761505Sigor@sysoev.ru            + sizeof(" HTTP/1.1\r\n")
22771505Sigor@sysoev.ru            + sizeof("Connection: close\r\n")
22781270Sigor@sysoev.ru            + sizeof("\r\n");
22791270Sigor@sysoev.ru 
22801270Sigor@sysoev.ru     nxt_list_each(field, r->fields) {
22811270Sigor@sysoev.ru 
22821270Sigor@sysoev.ru         if (!field->hopbyhop) {
22831270Sigor@sysoev.ru             size += field->name_length + field->value_length;
22841270Sigor@sysoev.ru             size += nxt_length(": \r\n");
22851270Sigor@sysoev.ru         }
22861270Sigor@sysoev.ru 
22871270Sigor@sysoev.ru     } nxt_list_loop;
22881270Sigor@sysoev.ru 
22891270Sigor@sysoev.ru     header = nxt_http_buf_mem(task, r, size);
22901270Sigor@sysoev.ru     if (nxt_slow_path(header == NULL)) {
22911270Sigor@sysoev.ru         r->state->error_handler(task, r, peer);
22921270Sigor@sysoev.ru         return;
22931270Sigor@sysoev.ru     }
22941270Sigor@sysoev.ru 
22951270Sigor@sysoev.ru     p = header->mem.free;
22961270Sigor@sysoev.ru 
22971270Sigor@sysoev.ru     p = nxt_cpymem(p, r->method->start, r->method->length);
22981270Sigor@sysoev.ru     *p++ = ' ';
22991270Sigor@sysoev.ru     p = nxt_cpymem(p, r->target.start, r->target.length);
23001505Sigor@sysoev.ru     p = nxt_cpymem(p, " HTTP/1.1\r\n", 11);
23011505Sigor@sysoev.ru     p = nxt_cpymem(p, "Connection: close\r\n", 19);
23021270Sigor@sysoev.ru 
23031270Sigor@sysoev.ru     nxt_list_each(field, r->fields) {
23041270Sigor@sysoev.ru 
23051270Sigor@sysoev.ru         if (!field->hopbyhop) {
23061270Sigor@sysoev.ru             p = nxt_cpymem(p, field->name, field->name_length);
23071270Sigor@sysoev.ru             *p++ = ':'; *p++ = ' ';
23081270Sigor@sysoev.ru             p = nxt_cpymem(p, field->value, field->value_length);
23091270Sigor@sysoev.ru             *p++ = '\r'; *p++ = '\n';
23101270Sigor@sysoev.ru         }
23111270Sigor@sysoev.ru 
23121270Sigor@sysoev.ru     } nxt_list_loop;
23131270Sigor@sysoev.ru 
23141270Sigor@sysoev.ru     *p++ = '\r'; *p++ = '\n';
23151270Sigor@sysoev.ru     header->mem.free = p;
23161270Sigor@sysoev.ru     size = p - header->mem.pos;
23171270Sigor@sysoev.ru 
23181270Sigor@sysoev.ru     c = peer->proto.h1->conn;
23191270Sigor@sysoev.ru     c->write = header;
23201270Sigor@sysoev.ru     c->write_state = &nxt_h1p_peer_header_send_state;
23211270Sigor@sysoev.ru 
23221270Sigor@sysoev.ru     if (r->body != NULL) {
23231403Smax.romanov@nginx.com         if (nxt_buf_is_file(r->body)) {
23241403Smax.romanov@nginx.com             body = nxt_buf_file_alloc(r->mem_pool, 0, 0);
23251403Smax.romanov@nginx.com 
23261403Smax.romanov@nginx.com         } else {
23271403Smax.romanov@nginx.com             body = nxt_buf_mem_alloc(r->mem_pool, 0, 0);
23281403Smax.romanov@nginx.com         }
23291403Smax.romanov@nginx.com 
23301270Sigor@sysoev.ru         if (nxt_slow_path(body == NULL)) {
23311270Sigor@sysoev.ru             r->state->error_handler(task, r, peer);
23321270Sigor@sysoev.ru             return;
23331270Sigor@sysoev.ru         }
23341270Sigor@sysoev.ru 
23351270Sigor@sysoev.ru         header->next = body;
23361270Sigor@sysoev.ru 
23371403Smax.romanov@nginx.com         if (nxt_buf_is_file(r->body)) {
23381403Smax.romanov@nginx.com             body->file = r->body->file;
23391403Smax.romanov@nginx.com             body->file_end = r->body->file_end;
23401403Smax.romanov@nginx.com 
23411403Smax.romanov@nginx.com         } else {
23421403Smax.romanov@nginx.com             body->mem = r->body->mem;
23431403Smax.romanov@nginx.com         }
23441403Smax.romanov@nginx.com 
23451403Smax.romanov@nginx.com         size += nxt_buf_used_size(body);
23461270Sigor@sysoev.ru 
23471270Sigor@sysoev.ru //        nxt_mp_retain(r->mem_pool);
23481270Sigor@sysoev.ru     }
23491270Sigor@sysoev.ru 
23501270Sigor@sysoev.ru     if (size > 16384) {
23511270Sigor@sysoev.ru         /* Use proxy_send_timeout instead of proxy_timeout. */
23521270Sigor@sysoev.ru         c->write_state = &nxt_h1p_peer_header_body_send_state;
23531270Sigor@sysoev.ru     }
23541270Sigor@sysoev.ru 
23551270Sigor@sysoev.ru     nxt_conn_write(task->thread->engine, c);
23561270Sigor@sysoev.ru }
23571270Sigor@sysoev.ru 
23581270Sigor@sysoev.ru 
23591270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_header_send_state
23601270Sigor@sysoev.ru     nxt_aligned(64) =
23611270Sigor@sysoev.ru {
23621270Sigor@sysoev.ru     .ready_handler = nxt_h1p_peer_header_sent,
23631270Sigor@sysoev.ru     .error_handler = nxt_h1p_peer_error,
23641270Sigor@sysoev.ru 
23651270Sigor@sysoev.ru     .timer_handler = nxt_h1p_peer_send_timeout,
23661270Sigor@sysoev.ru     .timer_value = nxt_h1p_peer_timer_value,
23671270Sigor@sysoev.ru     .timer_data = offsetof(nxt_socket_conf_t, proxy_timeout),
23681270Sigor@sysoev.ru };
23691270Sigor@sysoev.ru 
23701270Sigor@sysoev.ru 
23711270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_header_body_send_state
23721270Sigor@sysoev.ru     nxt_aligned(64) =
23731270Sigor@sysoev.ru {
23741270Sigor@sysoev.ru     .ready_handler = nxt_h1p_peer_header_sent,
23751270Sigor@sysoev.ru     .error_handler = nxt_h1p_peer_error,
23761270Sigor@sysoev.ru 
23771270Sigor@sysoev.ru     .timer_handler = nxt_h1p_peer_send_timeout,
23781270Sigor@sysoev.ru     .timer_value = nxt_h1p_peer_timer_value,
23791270Sigor@sysoev.ru     .timer_data = offsetof(nxt_socket_conf_t, proxy_send_timeout),
23801270Sigor@sysoev.ru     .timer_autoreset = 1,
23811270Sigor@sysoev.ru };
23821270Sigor@sysoev.ru 
23831270Sigor@sysoev.ru 
23841270Sigor@sysoev.ru static void
nxt_h1p_peer_header_sent(nxt_task_t * task,void * obj,void * data)23851270Sigor@sysoev.ru nxt_h1p_peer_header_sent(nxt_task_t *task, void *obj, void *data)
23861270Sigor@sysoev.ru {
23871270Sigor@sysoev.ru     nxt_conn_t          *c;
23881270Sigor@sysoev.ru     nxt_http_peer_t     *peer;
23891270Sigor@sysoev.ru     nxt_http_request_t  *r;
23901270Sigor@sysoev.ru     nxt_event_engine_t  *engine;
23911270Sigor@sysoev.ru 
23921270Sigor@sysoev.ru     c = obj;
23931270Sigor@sysoev.ru     peer = data;
23941270Sigor@sysoev.ru 
23951270Sigor@sysoev.ru     nxt_debug(task, "h1p peer header sent");
23961270Sigor@sysoev.ru 
23971270Sigor@sysoev.ru     engine = task->thread->engine;
23981270Sigor@sysoev.ru 
23991270Sigor@sysoev.ru     c->write = nxt_sendbuf_completion(task, &engine->fast_work_queue, c->write);
24001270Sigor@sysoev.ru 
24011403Smax.romanov@nginx.com     if (c->write != NULL) {
24021403Smax.romanov@nginx.com         nxt_conn_write(engine, c);
24031270Sigor@sysoev.ru         return;
24041270Sigor@sysoev.ru     }
24051270Sigor@sysoev.ru 
24061403Smax.romanov@nginx.com     r = peer->request;
24071403Smax.romanov@nginx.com     r->state->ready_handler(task, r, peer);
24081270Sigor@sysoev.ru }
24091270Sigor@sysoev.ru 
24101270Sigor@sysoev.ru 
24111270Sigor@sysoev.ru static void
nxt_h1p_peer_header_read(nxt_task_t * task,nxt_http_peer_t * peer)24121270Sigor@sysoev.ru nxt_h1p_peer_header_read(nxt_task_t *task, nxt_http_peer_t *peer)
24131270Sigor@sysoev.ru {
24141270Sigor@sysoev.ru     nxt_conn_t  *c;
24151270Sigor@sysoev.ru 
24161270Sigor@sysoev.ru     nxt_debug(task, "h1p peer header read");
24171270Sigor@sysoev.ru 
24181270Sigor@sysoev.ru     c = peer->proto.h1->conn;
24191270Sigor@sysoev.ru 
24201270Sigor@sysoev.ru     if (c->write_timer.enabled) {
24211270Sigor@sysoev.ru         c->read_state = &nxt_h1p_peer_header_read_state;
24221270Sigor@sysoev.ru 
24231270Sigor@sysoev.ru     } else {
24241270Sigor@sysoev.ru         c->read_state = &nxt_h1p_peer_header_read_timer_state;
24251270Sigor@sysoev.ru     }
24261270Sigor@sysoev.ru 
24271270Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
24281270Sigor@sysoev.ru }
24291270Sigor@sysoev.ru 
24301270Sigor@sysoev.ru 
24311270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_header_read_state
24321270Sigor@sysoev.ru     nxt_aligned(64) =
24331270Sigor@sysoev.ru {
24341270Sigor@sysoev.ru     .ready_handler = nxt_h1p_peer_header_read_done,
24351270Sigor@sysoev.ru     .close_handler = nxt_h1p_peer_closed,
24361270Sigor@sysoev.ru     .error_handler = nxt_h1p_peer_error,
24371270Sigor@sysoev.ru 
24381270Sigor@sysoev.ru     .io_read_handler = nxt_h1p_peer_io_read_handler,
24391270Sigor@sysoev.ru };
24401270Sigor@sysoev.ru 
24411270Sigor@sysoev.ru 
24421270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_header_read_timer_state
24431270Sigor@sysoev.ru     nxt_aligned(64) =
24441270Sigor@sysoev.ru {
24451270Sigor@sysoev.ru     .ready_handler = nxt_h1p_peer_header_read_done,
24461270Sigor@sysoev.ru     .close_handler = nxt_h1p_peer_closed,
24471270Sigor@sysoev.ru     .error_handler = nxt_h1p_peer_error,
24481270Sigor@sysoev.ru 
24491270Sigor@sysoev.ru     .io_read_handler = nxt_h1p_peer_io_read_handler,
24501270Sigor@sysoev.ru 
24511270Sigor@sysoev.ru     .timer_handler = nxt_h1p_peer_read_timeout,
24521270Sigor@sysoev.ru     .timer_value = nxt_h1p_peer_timer_value,
24531270Sigor@sysoev.ru     .timer_data = offsetof(nxt_socket_conf_t, proxy_timeout),
24541270Sigor@sysoev.ru };
24551270Sigor@sysoev.ru 
24561270Sigor@sysoev.ru 
24571270Sigor@sysoev.ru static ssize_t
nxt_h1p_peer_io_read_handler(nxt_task_t * task,nxt_conn_t * c)24581270Sigor@sysoev.ru nxt_h1p_peer_io_read_handler(nxt_task_t *task, nxt_conn_t *c)
24591270Sigor@sysoev.ru {
24601270Sigor@sysoev.ru     size_t              size;
24611270Sigor@sysoev.ru     ssize_t             n;
24621270Sigor@sysoev.ru     nxt_buf_t           *b;
24631270Sigor@sysoev.ru     nxt_http_peer_t     *peer;
24641270Sigor@sysoev.ru     nxt_socket_conf_t   *skcf;
24651270Sigor@sysoev.ru     nxt_http_request_t  *r;
24661270Sigor@sysoev.ru 
24671270Sigor@sysoev.ru     peer = c->socket.data;
24681270Sigor@sysoev.ru     r = peer->request;
24691270Sigor@sysoev.ru     b = c->read;
24701270Sigor@sysoev.ru 
24711270Sigor@sysoev.ru     if (b == NULL) {
24721270Sigor@sysoev.ru         skcf = r->conf->socket_conf;
24731270Sigor@sysoev.ru 
24741270Sigor@sysoev.ru         size = (peer->header_received) ? skcf->proxy_buffer_size
24751270Sigor@sysoev.ru                                        : skcf->proxy_header_buffer_size;
24761270Sigor@sysoev.ru 
24771270Sigor@sysoev.ru         nxt_debug(task, "h1p peer io read: %z", size);
24781270Sigor@sysoev.ru 
24791270Sigor@sysoev.ru         b = nxt_http_proxy_buf_mem_alloc(task, r, size);
24801270Sigor@sysoev.ru         if (nxt_slow_path(b == NULL)) {
24811270Sigor@sysoev.ru             c->socket.error = NXT_ENOMEM;
24821270Sigor@sysoev.ru             return NXT_ERROR;
24831270Sigor@sysoev.ru         }
24841270Sigor@sysoev.ru     }
24851270Sigor@sysoev.ru 
24861270Sigor@sysoev.ru     n = c->io->recvbuf(c, b);
24871270Sigor@sysoev.ru 
24881270Sigor@sysoev.ru     if (n > 0) {
24891270Sigor@sysoev.ru         c->read = b;
24901270Sigor@sysoev.ru 
24911270Sigor@sysoev.ru     } else {
24921270Sigor@sysoev.ru         c->read = NULL;
24931270Sigor@sysoev.ru         nxt_http_proxy_buf_mem_free(task, r, b);
24941270Sigor@sysoev.ru     }
24951270Sigor@sysoev.ru 
24961270Sigor@sysoev.ru     return n;
24971270Sigor@sysoev.ru }
24981270Sigor@sysoev.ru 
24991270Sigor@sysoev.ru 
25001270Sigor@sysoev.ru static void
nxt_h1p_peer_header_read_done(nxt_task_t * task,void * obj,void * data)25011270Sigor@sysoev.ru nxt_h1p_peer_header_read_done(nxt_task_t *task, void *obj, void *data)
25021270Sigor@sysoev.ru {
25031270Sigor@sysoev.ru     nxt_int_t           ret;
25041270Sigor@sysoev.ru     nxt_buf_t           *b;
25051270Sigor@sysoev.ru     nxt_conn_t          *c;
25061505Sigor@sysoev.ru     nxt_h1proto_t       *h1p;
25071270Sigor@sysoev.ru     nxt_http_peer_t     *peer;
25081270Sigor@sysoev.ru     nxt_http_request_t  *r;
25091270Sigor@sysoev.ru     nxt_event_engine_t  *engine;
25101270Sigor@sysoev.ru 
25111270Sigor@sysoev.ru     c = obj;
25121270Sigor@sysoev.ru     peer = data;
25131270Sigor@sysoev.ru 
25141270Sigor@sysoev.ru     nxt_debug(task, "h1p peer header read done");
25151270Sigor@sysoev.ru 
25161270Sigor@sysoev.ru     b = c->read;
25171270Sigor@sysoev.ru 
25181270Sigor@sysoev.ru     ret = nxt_h1p_peer_header_parse(peer, &b->mem);
25191270Sigor@sysoev.ru 
25201270Sigor@sysoev.ru     r = peer->request;
25211270Sigor@sysoev.ru 
25221270Sigor@sysoev.ru     ret = nxt_expect(NXT_DONE, ret);
25231270Sigor@sysoev.ru 
25241270Sigor@sysoev.ru     if (ret != NXT_AGAIN) {
25251270Sigor@sysoev.ru         engine = task->thread->engine;
25261270Sigor@sysoev.ru         nxt_timer_disable(engine, &c->write_timer);
25271270Sigor@sysoev.ru         nxt_timer_disable(engine, &c->read_timer);
25281270Sigor@sysoev.ru     }
25291270Sigor@sysoev.ru 
25301270Sigor@sysoev.ru     switch (ret) {
25311270Sigor@sysoev.ru 
25321270Sigor@sysoev.ru     case NXT_DONE:
25331270Sigor@sysoev.ru         peer->fields = peer->proto.h1->parser.fields;
25341270Sigor@sysoev.ru 
25351270Sigor@sysoev.ru         ret = nxt_http_fields_process(peer->fields,
25361270Sigor@sysoev.ru                                       &nxt_h1p_peer_fields_hash, r);
25371270Sigor@sysoev.ru         if (nxt_slow_path(ret != NXT_OK)) {
25381270Sigor@sysoev.ru             peer->status = NXT_HTTP_INTERNAL_SERVER_ERROR;
25391270Sigor@sysoev.ru             break;
25401270Sigor@sysoev.ru         }
25411270Sigor@sysoev.ru 
25421270Sigor@sysoev.ru         c->read = NULL;
25431270Sigor@sysoev.ru 
25441505Sigor@sysoev.ru         peer->header_received = 1;
25451505Sigor@sysoev.ru 
25461505Sigor@sysoev.ru         h1p = peer->proto.h1;
25471505Sigor@sysoev.ru 
25481505Sigor@sysoev.ru         if (h1p->chunked) {
25491505Sigor@sysoev.ru             if (r->resp.content_length != NULL) {
25501505Sigor@sysoev.ru                 peer->status = NXT_HTTP_BAD_GATEWAY;
25511505Sigor@sysoev.ru                 break;
25521505Sigor@sysoev.ru             }
25531505Sigor@sysoev.ru 
25541505Sigor@sysoev.ru             h1p->chunked_parse.mem_pool = c->mem_pool;
25551505Sigor@sysoev.ru 
25561505Sigor@sysoev.ru         } else if (r->resp.content_length_n > 0) {
25571505Sigor@sysoev.ru             h1p->remainder = r->resp.content_length_n;
25581270Sigor@sysoev.ru         }
25591270Sigor@sysoev.ru 
25601505Sigor@sysoev.ru         if (nxt_buf_mem_used_size(&b->mem) != 0) {
25611505Sigor@sysoev.ru             nxt_h1p_peer_body_process(task, peer, b);
25621505Sigor@sysoev.ru             return;
25631505Sigor@sysoev.ru         }
25641270Sigor@sysoev.ru 
25651270Sigor@sysoev.ru         r->state->ready_handler(task, r, peer);
25661270Sigor@sysoev.ru         return;
25671270Sigor@sysoev.ru 
25681270Sigor@sysoev.ru     case NXT_AGAIN:
25691270Sigor@sysoev.ru         if (nxt_buf_mem_free_size(&b->mem) != 0) {
25701270Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
25711270Sigor@sysoev.ru             return;
25721270Sigor@sysoev.ru         }
25731270Sigor@sysoev.ru 
25741270Sigor@sysoev.ru         /* Fall through. */
25751270Sigor@sysoev.ru 
25761270Sigor@sysoev.ru     default:
25771270Sigor@sysoev.ru     case NXT_ERROR:
25781270Sigor@sysoev.ru     case NXT_HTTP_PARSE_INVALID:
25791270Sigor@sysoev.ru     case NXT_HTTP_PARSE_UNSUPPORTED_VERSION:
25801270Sigor@sysoev.ru     case NXT_HTTP_PARSE_TOO_LARGE_FIELD:
25811270Sigor@sysoev.ru         peer->status = NXT_HTTP_BAD_GATEWAY;
25821270Sigor@sysoev.ru         break;
25831270Sigor@sysoev.ru     }
25841270Sigor@sysoev.ru 
25851270Sigor@sysoev.ru     nxt_http_proxy_buf_mem_free(task, r, b);
25861270Sigor@sysoev.ru 
25871270Sigor@sysoev.ru     r->state->error_handler(task, r, peer);
25881270Sigor@sysoev.ru }
25891270Sigor@sysoev.ru 
25901270Sigor@sysoev.ru 
25911270Sigor@sysoev.ru static nxt_int_t
nxt_h1p_peer_header_parse(nxt_http_peer_t * peer,nxt_buf_mem_t * bm)25921270Sigor@sysoev.ru nxt_h1p_peer_header_parse(nxt_http_peer_t *peer, nxt_buf_mem_t *bm)
25931270Sigor@sysoev.ru {
25941270Sigor@sysoev.ru     u_char     *p;
25951270Sigor@sysoev.ru     size_t     length;
25961270Sigor@sysoev.ru     nxt_int_t  status;
25971270Sigor@sysoev.ru 
25981270Sigor@sysoev.ru     if (peer->status < 0) {
25991270Sigor@sysoev.ru         length = nxt_buf_mem_used_size(bm);
26001270Sigor@sysoev.ru 
26011270Sigor@sysoev.ru         if (nxt_slow_path(length < 12)) {
26021270Sigor@sysoev.ru             return NXT_AGAIN;
26031270Sigor@sysoev.ru         }
26041270Sigor@sysoev.ru 
26051270Sigor@sysoev.ru         p = bm->pos;
26061270Sigor@sysoev.ru 
26072231Salx@nginx.com         if (nxt_slow_path(memcmp(p, "HTTP/1.", 7) != 0
26081270Sigor@sysoev.ru                           || (p[7] != '0' && p[7] != '1')))
26091270Sigor@sysoev.ru         {
26101270Sigor@sysoev.ru             return NXT_ERROR;
26111270Sigor@sysoev.ru         }
26121270Sigor@sysoev.ru 
26131270Sigor@sysoev.ru         status = nxt_int_parse(&p[9], 3);
26141270Sigor@sysoev.ru 
26151270Sigor@sysoev.ru         if (nxt_slow_path(status < 0)) {
26161270Sigor@sysoev.ru             return NXT_ERROR;
26171270Sigor@sysoev.ru         }
26181270Sigor@sysoev.ru 
26191270Sigor@sysoev.ru         p += 12;
26201270Sigor@sysoev.ru         length -= 12;
26211270Sigor@sysoev.ru 
26222232Salx@nginx.com         p = memchr(p, '\n', length);
26231270Sigor@sysoev.ru 
26241270Sigor@sysoev.ru         if (nxt_slow_path(p == NULL)) {
26251270Sigor@sysoev.ru             return NXT_AGAIN;
26261270Sigor@sysoev.ru         }
26271270Sigor@sysoev.ru 
26281270Sigor@sysoev.ru         bm->pos = p + 1;
26291270Sigor@sysoev.ru         peer->status = status;
26301270Sigor@sysoev.ru     }
26311270Sigor@sysoev.ru 
26321270Sigor@sysoev.ru     return nxt_http_parse_fields(&peer->proto.h1->parser, bm);
26331270Sigor@sysoev.ru }
26341270Sigor@sysoev.ru 
26351270Sigor@sysoev.ru 
26361270Sigor@sysoev.ru static void
nxt_h1p_peer_read(nxt_task_t * task,nxt_http_peer_t * peer)26371270Sigor@sysoev.ru nxt_h1p_peer_read(nxt_task_t *task, nxt_http_peer_t *peer)
26381270Sigor@sysoev.ru {
26391270Sigor@sysoev.ru     nxt_conn_t  *c;
26401270Sigor@sysoev.ru 
26411270Sigor@sysoev.ru     nxt_debug(task, "h1p peer read");
26421270Sigor@sysoev.ru 
26431270Sigor@sysoev.ru     c = peer->proto.h1->conn;
26441270Sigor@sysoev.ru     c->read_state = &nxt_h1p_peer_read_state;
26451270Sigor@sysoev.ru 
26461270Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
26471270Sigor@sysoev.ru }
26481270Sigor@sysoev.ru 
26491270Sigor@sysoev.ru 
26501270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_read_state
26511270Sigor@sysoev.ru     nxt_aligned(64) =
26521270Sigor@sysoev.ru {
26531270Sigor@sysoev.ru     .ready_handler = nxt_h1p_peer_read_done,
26541270Sigor@sysoev.ru     .close_handler = nxt_h1p_peer_closed,
26551270Sigor@sysoev.ru     .error_handler = nxt_h1p_peer_error,
26561270Sigor@sysoev.ru 
26571270Sigor@sysoev.ru     .io_read_handler = nxt_h1p_peer_io_read_handler,
26581270Sigor@sysoev.ru 
26591270Sigor@sysoev.ru     .timer_handler = nxt_h1p_peer_read_timeout,
26601270Sigor@sysoev.ru     .timer_value = nxt_h1p_peer_timer_value,
26611270Sigor@sysoev.ru     .timer_data = offsetof(nxt_socket_conf_t, proxy_read_timeout),
26621270Sigor@sysoev.ru     .timer_autoreset = 1,
26631270Sigor@sysoev.ru };
26641270Sigor@sysoev.ru 
26651270Sigor@sysoev.ru 
26661270Sigor@sysoev.ru static void
nxt_h1p_peer_read_done(nxt_task_t * task,void * obj,void * data)26671270Sigor@sysoev.ru nxt_h1p_peer_read_done(nxt_task_t *task, void *obj, void *data)
26681270Sigor@sysoev.ru {
26691505Sigor@sysoev.ru     nxt_buf_t        *out;
26701505Sigor@sysoev.ru     nxt_conn_t       *c;
26711505Sigor@sysoev.ru     nxt_http_peer_t  *peer;
26721270Sigor@sysoev.ru 
26731270Sigor@sysoev.ru     c = obj;
26741270Sigor@sysoev.ru     peer = data;
26751270Sigor@sysoev.ru 
26761270Sigor@sysoev.ru     nxt_debug(task, "h1p peer read done");
26771270Sigor@sysoev.ru 
26781505Sigor@sysoev.ru     out = c->read;
26791270Sigor@sysoev.ru     c->read = NULL;
26801270Sigor@sysoev.ru 
26811505Sigor@sysoev.ru     nxt_h1p_peer_body_process(task, peer, out);
26821505Sigor@sysoev.ru }
26831505Sigor@sysoev.ru 
26841505Sigor@sysoev.ru 
26851505Sigor@sysoev.ru static void
nxt_h1p_peer_body_process(nxt_task_t * task,nxt_http_peer_t * peer,nxt_buf_t * out)26861505Sigor@sysoev.ru nxt_h1p_peer_body_process(nxt_task_t *task, nxt_http_peer_t *peer,
26871505Sigor@sysoev.ru     nxt_buf_t *out)
26881505Sigor@sysoev.ru {
26891505Sigor@sysoev.ru     size_t              length;
26901505Sigor@sysoev.ru     nxt_h1proto_t       *h1p;
26911505Sigor@sysoev.ru     nxt_http_request_t  *r;
26921505Sigor@sysoev.ru 
26931505Sigor@sysoev.ru     h1p = peer->proto.h1;
26941505Sigor@sysoev.ru 
26951505Sigor@sysoev.ru     if (h1p->chunked) {
26961505Sigor@sysoev.ru         out = nxt_http_chunk_parse(task, &h1p->chunked_parse, out);
26971505Sigor@sysoev.ru 
26981505Sigor@sysoev.ru         if (h1p->chunked_parse.chunk_error || h1p->chunked_parse.error) {
26991505Sigor@sysoev.ru             peer->status = NXT_HTTP_BAD_GATEWAY;
27001505Sigor@sysoev.ru             r = peer->request;
27011505Sigor@sysoev.ru             r->state->error_handler(task, r, peer);
27021505Sigor@sysoev.ru             return;
27031505Sigor@sysoev.ru         }
27041505Sigor@sysoev.ru 
27051505Sigor@sysoev.ru         if (h1p->chunked_parse.last) {
27061505Sigor@sysoev.ru             nxt_buf_chain_add(&out, nxt_http_buf_last(peer->request));
27071505Sigor@sysoev.ru             peer->closed = 1;
27081505Sigor@sysoev.ru         }
27091505Sigor@sysoev.ru 
27101505Sigor@sysoev.ru     } else if (h1p->remainder > 0) {
27111505Sigor@sysoev.ru         length = nxt_buf_chain_length(out);
27121505Sigor@sysoev.ru         h1p->remainder -= length;
27131505Sigor@sysoev.ru     }
27141505Sigor@sysoev.ru 
27151505Sigor@sysoev.ru     peer->body = out;
27161505Sigor@sysoev.ru 
27171270Sigor@sysoev.ru     r = peer->request;
27181270Sigor@sysoev.ru     r->state->ready_handler(task, r, peer);
27191270Sigor@sysoev.ru }
27201270Sigor@sysoev.ru 
27211270Sigor@sysoev.ru 
27221270Sigor@sysoev.ru static void
nxt_h1p_peer_closed(nxt_task_t * task,void * obj,void * data)27231270Sigor@sysoev.ru nxt_h1p_peer_closed(nxt_task_t *task, void *obj, void *data)
27241270Sigor@sysoev.ru {
27251270Sigor@sysoev.ru     nxt_http_peer_t     *peer;
27261270Sigor@sysoev.ru     nxt_http_request_t  *r;
27271270Sigor@sysoev.ru 
27281270Sigor@sysoev.ru     peer = data;
27291270Sigor@sysoev.ru 
27301270Sigor@sysoev.ru     nxt_debug(task, "h1p peer closed");
27311270Sigor@sysoev.ru 
27321270Sigor@sysoev.ru     r = peer->request;
27331270Sigor@sysoev.ru 
27341270Sigor@sysoev.ru     if (peer->header_received) {
27351270Sigor@sysoev.ru         peer->body = nxt_http_buf_last(r);
27361270Sigor@sysoev.ru         peer->closed = 1;
27371505Sigor@sysoev.ru         r->inconsistent = (peer->proto.h1->remainder != 0);
27381270Sigor@sysoev.ru 
27391270Sigor@sysoev.ru         r->state->ready_handler(task, r, peer);
27401270Sigor@sysoev.ru 
27411270Sigor@sysoev.ru     } else {
27421270Sigor@sysoev.ru         peer->status = NXT_HTTP_BAD_GATEWAY;
27431270Sigor@sysoev.ru 
27441270Sigor@sysoev.ru         r->state->error_handler(task, r, peer);
27451270Sigor@sysoev.ru     }
27461270Sigor@sysoev.ru }
27471270Sigor@sysoev.ru 
27481270Sigor@sysoev.ru 
27491270Sigor@sysoev.ru static void
nxt_h1p_peer_error(nxt_task_t * task,void * obj,void * data)27501270Sigor@sysoev.ru nxt_h1p_peer_error(nxt_task_t *task, void *obj, void *data)
27511270Sigor@sysoev.ru {
27521270Sigor@sysoev.ru     nxt_http_peer_t     *peer;
27531270Sigor@sysoev.ru     nxt_http_request_t  *r;
27541270Sigor@sysoev.ru 
27551270Sigor@sysoev.ru     peer = data;
27561270Sigor@sysoev.ru 
27571270Sigor@sysoev.ru     nxt_debug(task, "h1p peer error");
27581270Sigor@sysoev.ru 
27591270Sigor@sysoev.ru     peer->status = NXT_HTTP_BAD_GATEWAY;
27601270Sigor@sysoev.ru 
27611270Sigor@sysoev.ru     r = peer->request;
27621270Sigor@sysoev.ru     r->state->error_handler(task, r, peer);
27631270Sigor@sysoev.ru }
27641270Sigor@sysoev.ru 
27651270Sigor@sysoev.ru 
27661270Sigor@sysoev.ru static void
nxt_h1p_peer_send_timeout(nxt_task_t * task,void * obj,void * data)27671270Sigor@sysoev.ru nxt_h1p_peer_send_timeout(nxt_task_t *task, void *obj, void *data)
27681270Sigor@sysoev.ru {
27691270Sigor@sysoev.ru     nxt_conn_t          *c;
27701270Sigor@sysoev.ru     nxt_timer_t         *timer;
27711270Sigor@sysoev.ru     nxt_http_peer_t     *peer;
27721270Sigor@sysoev.ru     nxt_http_request_t  *r;
27731270Sigor@sysoev.ru 
27741270Sigor@sysoev.ru     timer = obj;
27751270Sigor@sysoev.ru 
27761270Sigor@sysoev.ru     nxt_debug(task, "h1p peer send timeout");
27771270Sigor@sysoev.ru 
27781270Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
27791270Sigor@sysoev.ru     c->block_write = 1;
27801270Sigor@sysoev.ru     c->block_read = 1;
27811270Sigor@sysoev.ru 
27821270Sigor@sysoev.ru     peer = c->socket.data;
27831270Sigor@sysoev.ru     peer->status = NXT_HTTP_GATEWAY_TIMEOUT;
27841270Sigor@sysoev.ru 
27851270Sigor@sysoev.ru     r = peer->request;
27861270Sigor@sysoev.ru     r->state->error_handler(task, r, peer);
27871270Sigor@sysoev.ru }
27881270Sigor@sysoev.ru 
27891270Sigor@sysoev.ru 
27901270Sigor@sysoev.ru static void
nxt_h1p_peer_read_timeout(nxt_task_t * task,void * obj,void * data)27911270Sigor@sysoev.ru nxt_h1p_peer_read_timeout(nxt_task_t *task, void *obj, void *data)
27921270Sigor@sysoev.ru {
27931270Sigor@sysoev.ru     nxt_conn_t          *c;
27941270Sigor@sysoev.ru     nxt_timer_t         *timer;
27951270Sigor@sysoev.ru     nxt_http_peer_t     *peer;
27961270Sigor@sysoev.ru     nxt_http_request_t  *r;
27971270Sigor@sysoev.ru 
27981270Sigor@sysoev.ru     timer = obj;
27991270Sigor@sysoev.ru 
28001270Sigor@sysoev.ru     nxt_debug(task, "h1p peer read timeout");
28011270Sigor@sysoev.ru 
28021270Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
28031270Sigor@sysoev.ru     c->block_write = 1;
28041270Sigor@sysoev.ru     c->block_read = 1;
28051270Sigor@sysoev.ru 
28061270Sigor@sysoev.ru     peer = c->socket.data;
28071270Sigor@sysoev.ru     peer->status = NXT_HTTP_GATEWAY_TIMEOUT;
28081270Sigor@sysoev.ru 
28091270Sigor@sysoev.ru     r = peer->request;
28101270Sigor@sysoev.ru     r->state->error_handler(task, r, peer);
28111270Sigor@sysoev.ru }
28121270Sigor@sysoev.ru 
28131270Sigor@sysoev.ru 
28141270Sigor@sysoev.ru static nxt_msec_t
nxt_h1p_peer_timer_value(nxt_conn_t * c,uintptr_t data)28151270Sigor@sysoev.ru nxt_h1p_peer_timer_value(nxt_conn_t *c, uintptr_t data)
28161270Sigor@sysoev.ru {
28171270Sigor@sysoev.ru     nxt_http_peer_t  *peer;
28181270Sigor@sysoev.ru 
28191270Sigor@sysoev.ru     peer = c->socket.data;
28201270Sigor@sysoev.ru 
28211270Sigor@sysoev.ru     return nxt_value_at(nxt_msec_t, peer->request->conf->socket_conf, data);
28221270Sigor@sysoev.ru }
28231270Sigor@sysoev.ru 
28241270Sigor@sysoev.ru 
28251270Sigor@sysoev.ru static void
nxt_h1p_peer_close(nxt_task_t * task,nxt_http_peer_t * peer)28261270Sigor@sysoev.ru nxt_h1p_peer_close(nxt_task_t *task, nxt_http_peer_t *peer)
28271270Sigor@sysoev.ru {
28281270Sigor@sysoev.ru     nxt_conn_t  *c;
28291270Sigor@sysoev.ru 
28301270Sigor@sysoev.ru     nxt_debug(task, "h1p peer close");
28311270Sigor@sysoev.ru 
28321270Sigor@sysoev.ru     peer->closed = 1;
28331270Sigor@sysoev.ru 
28341270Sigor@sysoev.ru     c = peer->proto.h1->conn;
28351270Sigor@sysoev.ru     task = &c->task;
28361270Sigor@sysoev.ru     c->socket.task = task;
28371270Sigor@sysoev.ru     c->read_timer.task = task;
28381270Sigor@sysoev.ru     c->write_timer.task = task;
28391270Sigor@sysoev.ru 
28401270Sigor@sysoev.ru     if (c->socket.fd != -1) {
28411270Sigor@sysoev.ru         c->write_state = &nxt_h1p_peer_close_state;
28421270Sigor@sysoev.ru 
28431270Sigor@sysoev.ru         nxt_conn_close(task->thread->engine, c);
28441270Sigor@sysoev.ru 
28451270Sigor@sysoev.ru     } else {
28461270Sigor@sysoev.ru         nxt_h1p_peer_free(task, c, NULL);
28471270Sigor@sysoev.ru     }
28481270Sigor@sysoev.ru }
28491270Sigor@sysoev.ru 
28501270Sigor@sysoev.ru 
28511270Sigor@sysoev.ru static const nxt_conn_state_t  nxt_h1p_peer_close_state
28521270Sigor@sysoev.ru     nxt_aligned(64) =
28531270Sigor@sysoev.ru {
28541270Sigor@sysoev.ru     .ready_handler = nxt_h1p_peer_free,
28551270Sigor@sysoev.ru };
28561270Sigor@sysoev.ru 
28571270Sigor@sysoev.ru 
28581270Sigor@sysoev.ru static void
nxt_h1p_peer_free(nxt_task_t * task,void * obj,void * data)28591270Sigor@sysoev.ru nxt_h1p_peer_free(nxt_task_t *task, void *obj, void *data)
28601270Sigor@sysoev.ru {
28611270Sigor@sysoev.ru     nxt_conn_t  *c;
28621270Sigor@sysoev.ru 
28631270Sigor@sysoev.ru     c = obj;
28641270Sigor@sysoev.ru 
28651270Sigor@sysoev.ru     nxt_debug(task, "h1p peer free");
28661270Sigor@sysoev.ru 
28671270Sigor@sysoev.ru     nxt_conn_free(task, c);
28681270Sigor@sysoev.ru }
28691505Sigor@sysoev.ru 
28701505Sigor@sysoev.ru 
28711505Sigor@sysoev.ru static nxt_int_t
nxt_h1p_peer_transfer_encoding(void * ctx,nxt_http_field_t * field,uintptr_t data)28721505Sigor@sysoev.ru nxt_h1p_peer_transfer_encoding(void *ctx, nxt_http_field_t *field,
28731505Sigor@sysoev.ru     uintptr_t data)
28741505Sigor@sysoev.ru {
28751505Sigor@sysoev.ru     nxt_http_request_t  *r;
28761505Sigor@sysoev.ru 
28771505Sigor@sysoev.ru     r = ctx;
28781505Sigor@sysoev.ru     field->skip = 1;
28791505Sigor@sysoev.ru 
28801505Sigor@sysoev.ru     if (field->value_length == 7
28812231Salx@nginx.com         && memcmp(field->value, "chunked", 7) == 0)
28821505Sigor@sysoev.ru     {
28831505Sigor@sysoev.ru         r->peer->proto.h1->chunked = 1;
28841505Sigor@sysoev.ru     }
28851505Sigor@sysoev.ru 
28861505Sigor@sysoev.ru     return NXT_OK;
28871505Sigor@sysoev.ru }
2888