xref: /unit/src/nxt_application.h (revision 216:07257705cd64)
1 
2 /*
3  * Copyright (C) Max Romanov
4  * Copyright (C) Valentin V. Bartenev
5  * Copyright (C) NGINX, Inc.
6  */
7 
8 #ifndef _NXT_APPLICATION_H_INCLUDED_
9 #define _NXT_APPLICATION_H_INCLUDED_
10 
11 
12 typedef enum {
13     NXT_APP_PYTHON,
14     NXT_APP_PHP,
15     NXT_APP_GO,
16 
17     NXT_APP_UNKNOWN,
18 } nxt_app_type_t;
19 
20 
21 typedef struct nxt_app_module_s  nxt_application_module_t;
22 typedef struct nxt_app_module_s  nxt_app_module_t;
23 
24 
25 typedef struct {
26     nxt_str_t                 type;
27     nxt_str_t                 version;
28     char                      *file;
29     nxt_application_module_t  *module;
30 } nxt_app_lang_module_t;
31 
32 
33 typedef struct nxt_common_app_conf_s nxt_common_app_conf_t;
34 
35 
36 typedef struct {
37     nxt_str_t  path;
38     nxt_str_t  module;
39 } nxt_python_app_conf_t;
40 
41 
42 typedef struct {
43     nxt_str_t  root;
44     nxt_str_t  script;
45     nxt_str_t  index;
46 } nxt_php_app_conf_t;
47 
48 
49 typedef struct {
50     nxt_str_t  executable;
51 } nxt_go_app_conf_t;
52 
53 
54 struct nxt_common_app_conf_s {
55     nxt_str_t       name;
56     nxt_str_t       type;
57     nxt_str_t       user;
58     nxt_str_t       group;
59 
60     uint32_t   workers;
61 
62     union {
63         nxt_python_app_conf_t  python;
64         nxt_php_app_conf_t     php;
65         nxt_go_app_conf_t      go;
66     } u;
67 };
68 
69 
70 typedef struct {
71     nxt_str_t                  name;
72     nxt_str_t                  value;
73 } nxt_app_header_field_t;
74 
75 
76 typedef struct {
77     nxt_str_t                  method;
78     nxt_str_t                  target;
79     nxt_str_t                  version;
80     nxt_str_t                  path;
81     nxt_str_t                  query;
82 
83     nxt_list_t                 *fields;
84 
85     nxt_str_t                  cookie;
86     nxt_str_t                  content_length;
87     nxt_str_t                  content_type;
88     nxt_str_t                  host;
89 
90     off_t                      parsed_content_length;
91     nxt_bool_t                 done;
92 
93     size_t                     bufs;
94     nxt_buf_t                  *buf;
95 } nxt_app_request_header_t;
96 
97 
98 typedef struct {
99     size_t                     preread_size;
100     nxt_bool_t                 done;
101 
102     nxt_buf_t                  *buf;
103 } nxt_app_request_body_t;
104 
105 
106 typedef struct {
107     nxt_app_request_header_t   header;
108     nxt_app_request_body_t     body;
109 
110     nxt_str_t                  remote;
111 } nxt_app_request_t;
112 
113 
114 typedef struct nxt_app_parse_ctx_s nxt_app_parse_ctx_t;
115 
116 struct nxt_app_parse_ctx_s {
117     nxt_app_request_t         r;
118     nxt_http_request_parse_t  parser;
119     nxt_mp_t                  *mem_pool;
120 };
121 
122 
123 nxt_int_t nxt_app_http_req_init(nxt_task_t *task, nxt_app_parse_ctx_t *ctx);
124 
125 nxt_int_t nxt_app_http_req_header_parse(nxt_task_t *task,
126     nxt_app_parse_ctx_t *ctx, nxt_buf_t *buf);
127 
128 nxt_int_t nxt_app_http_req_body_read(nxt_task_t *task,
129     nxt_app_parse_ctx_t *ctx, nxt_buf_t *buf);
130 
131 
132 nxt_int_t nxt_app_http_req_done(nxt_task_t *task, nxt_app_parse_ctx_t *ctx);
133 
134 nxt_int_t nxt_app_http_init(nxt_task_t *task, nxt_runtime_t *rt);
135 
136 
137 typedef struct nxt_app_wmsg_s  nxt_app_wmsg_t;
138 typedef struct nxt_app_rmsg_s  nxt_app_rmsg_t;
139 
140 struct nxt_app_wmsg_s {
141     nxt_port_t                 *port;  /* where prepared buf will be sent */
142     nxt_buf_t                  *write;
143     nxt_buf_t                  **buf;
144     uint32_t                   stream;
145 };
146 
147 struct nxt_app_rmsg_s {
148     nxt_buf_t                 *buf;   /* current buffer to read */
149 };
150 
151 
152 nxt_inline u_char *
153 nxt_app_msg_write_length(u_char *dst, size_t length);
154 
155 /* TODO asynchronous mmap buffer assignment */
156 NXT_EXPORT u_char *nxt_app_msg_write_get_buf(nxt_task_t *task,
157     nxt_app_wmsg_t *msg, size_t size);
158 
159 NXT_EXPORT nxt_int_t nxt_app_msg_write(nxt_task_t *task, nxt_app_wmsg_t *msg,
160     u_char *c, size_t size);
161 
162 NXT_EXPORT nxt_int_t nxt_app_msg_write_prefixed_upcase(nxt_task_t *task,
163     nxt_app_wmsg_t *msg, const nxt_str_t *prefix, const nxt_str_t *v);
164 
165 nxt_inline nxt_int_t
166 nxt_app_msg_write_nvp_(nxt_task_t *task, nxt_app_wmsg_t *msg,
167     u_char *n, size_t nsize, u_char *v, size_t vsize);
168 
169 
170 #define nxt_app_msg_write_const(task, msg, c)                                 \
171     nxt_app_msg_write((task), (msg), (u_char *)(c), sizeof(c) - 1)
172 
173 #define nxt_app_msg_write_str(task, msg, str)                                 \
174     nxt_app_msg_write((task), (msg), (str)->start, (str)->length)
175 
176 #define nxt_app_msg_write_cstr(task, msg, c)                                  \
177     nxt_app_msg_write((task), (msg), (c), nxt_strlen(c))
178 
179 #define nxt_app_msg_write_nvp(task, msg, n, v)                                \
180     nxt_app_msg_write_nvp_((task), (msg), (u_char *)(n), sizeof(n) - 1,       \
181                            (v)->start, (v)->length)
182 
183 nxt_inline nxt_int_t nxt_app_msg_write_size(nxt_task_t *task,
184     nxt_app_wmsg_t *msg, size_t size);
185 
186 NXT_EXPORT nxt_int_t nxt_app_msg_flush(nxt_task_t *task, nxt_app_wmsg_t *msg,
187     nxt_bool_t last);
188 
189 NXT_EXPORT nxt_int_t nxt_app_msg_write_raw(nxt_task_t *task,
190     nxt_app_wmsg_t *msg, const u_char *c, size_t size);
191 
192 NXT_EXPORT nxt_int_t nxt_app_msg_read_str(nxt_task_t *task, nxt_app_rmsg_t *msg,
193     nxt_str_t *str);
194 
195 NXT_EXPORT size_t nxt_app_msg_read_raw(nxt_task_t *task,
196     nxt_app_rmsg_t *msg, void *buf, size_t size);
197 
198 NXT_EXPORT nxt_int_t nxt_app_msg_read_nvp(nxt_task_t *task,
199     nxt_app_rmsg_t *rmsg, nxt_str_t *n, nxt_str_t *v);
200 
201 NXT_EXPORT nxt_int_t nxt_app_msg_read_size(nxt_task_t *task,
202     nxt_app_rmsg_t *rmsg, size_t *size);
203 
204 
205 struct nxt_app_module_s {
206     nxt_str_t                  type;
207     nxt_str_t                  version;
208 
209     nxt_int_t                  (*init)(nxt_task_t *task,
210                                     nxt_common_app_conf_t *conf);
211     nxt_int_t                  (*run)(nxt_task_t *task,
212                                     nxt_app_rmsg_t *rmsg,
213                                     nxt_app_wmsg_t *wmsg);
214 };
215 
216 
217 nxt_int_t nxt_app_http_read_body(nxt_app_request_t *r, u_char *data,
218     size_t len);
219 nxt_int_t nxt_app_write(nxt_app_request_t *r, const u_char *data, size_t len);
220 
221 nxt_inline u_char *
222 nxt_app_msg_write_length(u_char *dst, size_t length)
223 {
224     if (length < 128) {
225         *dst = length;
226         dst++;
227     } else {
228         dst[0] = 0x80U | (length >> 24);
229         dst[1] = 0xFFU & (length >> 16);
230         dst[2] = 0xFFU & (length >> 8);
231         dst[3] = 0xFFU & length;
232         dst += 4;
233     }
234 
235     return dst;
236 }
237 
238 
239 nxt_inline nxt_int_t
240 nxt_app_msg_write_nvp_(nxt_task_t *task, nxt_app_wmsg_t *msg,
241     u_char *n, size_t nsize, u_char *v, size_t vsize)
242 {
243     nxt_int_t rc;
244 
245     rc = nxt_app_msg_write(task, msg, n, nsize);
246     if (nxt_slow_path(rc != NXT_OK)) {
247         return rc;
248     }
249 
250     return nxt_app_msg_write(task, msg, v, vsize);
251 }
252 
253 
254 nxt_inline nxt_int_t
255 nxt_app_msg_write_size(nxt_task_t *task, nxt_app_wmsg_t *msg, size_t size)
256 {
257     u_char  *dst;
258     size_t  dst_length;
259 
260     dst_length = size < 128 ? 1 : 4;
261 
262     dst = nxt_app_msg_write_get_buf(task, msg, dst_length);
263     if (nxt_slow_path(dst == NULL)) {
264         return NXT_ERROR;
265     }
266 
267     nxt_app_msg_write_length(dst, size);
268 
269     return NXT_OK;
270 }
271 
272 
273 nxt_inline u_char *
274 nxt_app_msg_read_length(u_char *src, size_t *length)
275 {
276     if (src[0] < 128) {
277         *length = src[0];
278         src++;
279     } else {
280         *length = ((src[0] & 0x7fU) << 24) +
281                   (src[1] << 16) +
282                   (src[2] << 8) +
283                   src[3];
284         src += 4;
285     }
286 
287     return src;
288 }
289 
290 
291 nxt_app_lang_module_t *nxt_app_lang_module(nxt_runtime_t *rt, nxt_str_t *name);
292 nxt_app_type_t nxt_app_parse_type(nxt_str_t *str);
293 
294 
295 extern nxt_application_module_t  nxt_go_module;
296 
297 
298 #endif /* _NXT_APPLICATION_H_INCLIDED_ */
299