xref: /unit/src/nxt_http.h (revision 703:2d536dde84d2)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_HTTP_H_INCLUDED_
8 #define _NXT_HTTP_H_INCLUDED_
9 
10 
11 typedef enum {
12     NXT_HTTP_INVALID = 0,
13 
14     NXT_HTTP_OK = 200,
15 
16     NXT_HTTP_MULTIPLE_CHOICES = 300,
17     NXT_HTTP_MOVED_PERMANENTLY = 301,
18     NXT_HTTP_FOUND = 302,
19     NXT_HTTP_SEE_OTHER = 303,
20     NXT_HTTP_NOT_MODIFIED = 304,
21 
22     NXT_HTTP_BAD_REQUEST = 400,
23     NXT_HTTP_REQUEST_TIMEOUT = 408,
24     NXT_HTTP_LENGTH_REQUIRED = 411,
25     NXT_HTTP_PAYLOAD_TOO_LARGE = 413,
26     NXT_HTTP_URI_TOO_LONG = 414,
27     NXT_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
28 
29     NXT_HTTP_INTERNAL_SERVER_ERROR = 500,
30     NXT_HTTP_NOT_IMPLEMENTED = 501,
31     NXT_HTTP_BAD_GATEWAY = 502,
32     NXT_HTTP_SERVICE_UNAVAILABLE = 503,
33     NXT_HTTP_GATEWAY_TIMEOUT = 504,
34     NXT_HTTP_VERSION_NOT_SUPPORTED = 505,
35 } nxt_http_status_t;
36 
37 
38 typedef enum {
39     NXT_HTTP_TE_NONE = 0,
40     NXT_HTTP_TE_CHUNKED = 1,
41     NXT_HTTP_TE_UNSUPPORTED = 2,
42 } nxt_http_te_t;
43 
44 
45 typedef struct {
46     nxt_work_handler_t              ready_handler;
47     nxt_work_handler_t              error_handler;
48 } nxt_http_request_state_t;
49 
50 
51 typedef struct {
52     nxt_http_request_parse_t        parser;
53 
54     uint8_t                         nbuffers;
55     uint8_t                         keepalive;            /* 1 bit  */
56     uint8_t                         chunked;              /* 1 bit  */
57     nxt_http_te_t                   transfer_encoding:8;  /* 2 bits */
58 
59     uint32_t                        header_size;
60 
61     nxt_http_request_t              *request;
62     nxt_buf_t                       *buffers;
63     /*
64      * All fields before the conn field will
65      * be zeroed in a keep-alive connection.
66      */
67     nxt_conn_t                      *conn;
68 } nxt_h1proto_t;
69 
70 
71 typedef union {
72     void                            *any;
73     nxt_h1proto_t                   *h1;
74 } nxt_http_proto_t;
75 
76 
77 #define nxt_http_field_name_set(_field, _name)                                \
78     do {                                                                      \
79          (_field)->name_length = nxt_length(_name);                           \
80          (_field)->name = (u_char *) _name;                                   \
81     } while (0)
82 
83 
84 #define nxt_http_field_set(_field, _name, _value)                             \
85     do {                                                                      \
86          (_field)->name_length = nxt_length(_name);                           \
87          (_field)->value_length = nxt_length(_value);                         \
88          (_field)->name = (u_char *) _name;                                   \
89          (_field)->value = (u_char *) _value;                                 \
90     } while (0)
91 
92 
93 typedef struct {
94     nxt_list_t                      *fields;
95     nxt_http_field_t                *date;
96     nxt_http_field_t                *content_type;
97     nxt_http_field_t                *content_length;
98     nxt_off_t                       content_length_n;
99 } nxt_http_response_t;
100 
101 
102 struct nxt_http_request_s {
103     nxt_http_proto_t                proto;
104     nxt_socket_conf_joint_t         *conf;
105 
106     nxt_mp_t                        *mem_pool;
107 
108     nxt_buf_t                       *body;
109     nxt_buf_t                       *out;
110     const nxt_http_request_state_t  *state;
111 
112     nxt_str_t                       target;
113     nxt_str_t                       version;
114     nxt_str_t                       *method;
115     nxt_str_t                       *path;
116     nxt_str_t                       *args;
117 
118     nxt_list_t                      *fields;
119     nxt_http_field_t                *host;
120     nxt_http_field_t                *content_type;
121     nxt_http_field_t                *content_length;
122     nxt_http_field_t                *cookie;
123     nxt_http_field_t                *referer;
124     nxt_http_field_t                *user_agent;
125     nxt_off_t                       content_length_n;
126 
127     nxt_sockaddr_t                  *remote;
128     nxt_sockaddr_t                  *local;
129 
130     nxt_buf_t                       *last;
131 
132     nxt_http_response_t             resp;
133 
134     nxt_http_status_t               status:16;
135 
136     uint8_t                         protocol;     /* 2 bits */
137     uint8_t                         logged;       /* 1 bit  */
138     uint8_t                         header_sent;  /* 1 bit  */
139     uint8_t                         error;        /* 1 bit  */
140 };
141 
142 
143 typedef void (*nxt_http_proto_body_read_t)(nxt_task_t *task,
144     nxt_http_request_t *r);
145 typedef void (*nxt_http_proto_local_addr_t)(nxt_task_t *task,
146     nxt_http_request_t *r);
147 typedef void (*nxt_http_proto_header_send_t)(nxt_task_t *task,
148     nxt_http_request_t *r);
149 typedef void (*nxt_http_proto_send_t)(nxt_task_t *task, nxt_http_request_t *r,
150     nxt_buf_t *out);
151 typedef nxt_off_t (*nxt_http_proto_body_bytes_sent_t)(nxt_task_t *task,
152     nxt_http_proto_t proto);
153 typedef void (*nxt_http_proto_discard_t)(nxt_task_t *task,
154     nxt_http_request_t *r, nxt_buf_t *last);
155 typedef void (*nxt_http_proto_close_t)(nxt_task_t *task,
156     nxt_http_proto_t proto, nxt_socket_conf_joint_t *joint);
157 
158 
159 nxt_int_t nxt_http_init(nxt_task_t *task, nxt_runtime_t *rt);
160 nxt_int_t nxt_h1p_init(nxt_task_t *task, nxt_runtime_t *rt);
161 nxt_int_t nxt_http_response_hash_init(nxt_task_t *task, nxt_runtime_t *rt);
162 
163 void nxt_http_conn_init(nxt_task_t *task, void *obj, void *data);
164 nxt_http_request_t *nxt_http_request_create(nxt_task_t *task);
165 void nxt_http_request_error(nxt_task_t *task, nxt_http_request_t *r,
166     nxt_http_status_t status);
167 void nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r);
168 void nxt_http_request_local_addr(nxt_task_t *task, nxt_http_request_t *r);
169 void nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r);
170 void nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r,
171     nxt_buf_t *out);
172 nxt_buf_t *nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r,
173     size_t size);
174 nxt_buf_t *nxt_http_buf_last(nxt_http_request_t *r);
175 void nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data);
176 void nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data);
177 
178 nxt_int_t nxt_http_request_host(void *ctx, nxt_http_field_t *field,
179     uintptr_t data);
180 nxt_int_t nxt_http_request_field(void *ctx, nxt_http_field_t *field,
181     uintptr_t offset);
182 nxt_int_t nxt_http_request_content_length(void *ctx, nxt_http_field_t *field,
183     uintptr_t data);
184 
185 
186 extern nxt_lvlhsh_t                        nxt_response_fields_hash;
187 
188 extern const nxt_http_proto_body_read_t        nxt_http_proto_body_read[];
189 extern const nxt_http_proto_local_addr_t       nxt_http_proto_local_addr[];
190 extern const nxt_http_proto_header_send_t      nxt_http_proto_header_send[];
191 extern const nxt_http_proto_send_t             nxt_http_proto_send[];
192 extern const nxt_http_proto_body_bytes_sent_t  nxt_http_proto_body_bytes_sent[];
193 extern const nxt_http_proto_discard_t          nxt_http_proto_discard[];
194 extern const nxt_http_proto_close_t            nxt_http_proto_close[];
195 
196 
197 #endif  /* _NXT_HTTP_H_INCLUDED_ */
198