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