xref: /unit/src/nodejs/unit-http/unit.h (revision 878:8867c1c7c2bf)
1 
2 /*
3  * Copyright (C) NGINX, Inc.
4  */
5 
6 #ifndef _NXT_NODEJS_UNIT_H_INCLUDED_
7 #define _NXT_NODEJS_UNIT_H_INCLUDED_
8 
9 #include <node_api.h>
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 #include "version.h"
16 #include <nxt_unit.h>
17 
18 #if NXT_UNIT_VERNUM != NXT_NODE_VERNUM
19 #error "libunit version mismatch."
20 #endif
21 
22 #include <nxt_unit_response.h>
23 #include <nxt_unit_request.h>
24 
25 
26 #ifdef __cplusplus
27 } /* extern "C" */
28 #endif
29 
30 
31 class Unit {
32 public:
33     static napi_value init(napi_env env, napi_value exports);
34 
35 private:
36     Unit(napi_env env);
37     ~Unit();
38 
39     static napi_value create(napi_env env, napi_callback_info info);
40     static void destroy(napi_env env, void *nativeObject, void *finalize_hint);
41 
42     static napi_value create_server(napi_env env, napi_callback_info info);
43     static napi_value listen(napi_env env, napi_callback_info info);
44     static napi_value _read(napi_env env, napi_callback_info info);
45     static void request_handler(nxt_unit_request_info_t *req);
46     static int add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port);
47     static void remove_port(nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id);
48     static void quit(nxt_unit_ctx_t *ctx);
49 
50     napi_value get_server_object();
51 
52     napi_value create_socket(napi_value server_obj,
53                              nxt_unit_request_info_t *req);
54 
55     napi_value create_request(napi_value server_obj, napi_value socket);
56 
57     napi_value create_response(napi_value server_obj, napi_value socket,
58                                napi_value request,
59                                nxt_unit_request_info_t *req, Unit *obj);
60 
61     static napi_value response_send_headers(napi_env env,
62                                             napi_callback_info info);
63 
64     static napi_value response_write(napi_env env, napi_callback_info info);
65     static napi_value response_end(napi_env env, napi_callback_info info);
66 
67     napi_status create_headers(nxt_unit_request_info_t *req,
68                                napi_value request);
69 
70     inline napi_status append_header(nxt_unit_field_t *f, napi_value headers,
71                                      napi_value raw_headers, uint32_t idx);
72 
73     static napi_ref constructor_;
74 
75     napi_env        env_;
76     napi_ref        wrapper_;
77     nxt_unit_ctx_t  *unit_ctx_;
78 };
79 
80 
81 #endif /* _NXT_NODEJS_H_INCLUDED_ */
82