xref: /unit/go/nxt_cgo_lib.c (revision 1544:05af370e63b7)
1 
2 /*
3  * Copyright (C) Max Romanov
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include "_cgo_export.h"
8 
9 #include <nxt_unit.h>
10 #include <nxt_unit_request.h>
11 
12 
13 static void nxt_cgo_request_handler(nxt_unit_request_info_t *req);
14 static nxt_cgo_str_t *nxt_cgo_str_init(nxt_cgo_str_t *dst,
15     nxt_unit_sptr_t *sptr, uint32_t length);
16 static int nxt_cgo_add_port(nxt_unit_ctx_t *, nxt_unit_port_t *port);
17 static void nxt_cgo_remove_port(nxt_unit_t *, nxt_unit_port_t *port);
18 static ssize_t nxt_cgo_port_send(nxt_unit_ctx_t *, nxt_unit_port_t *port,
19     const void *buf, size_t buf_size, const void *oob, size_t oob_size);
20 static ssize_t nxt_cgo_port_recv(nxt_unit_ctx_t *, nxt_unit_port_t *port,
21     void *buf, size_t buf_size, void *oob, size_t oob_size);
22 static void nxt_cgo_shm_ack_handler(nxt_unit_ctx_t *ctx);
23 
24 int
25 nxt_cgo_run(uintptr_t handler)
26 {
27     int              rc;
28     nxt_unit_ctx_t   *ctx;
29     nxt_unit_init_t  init;
30 
31     memset(&init, 0, sizeof(init));
32 
33     init.callbacks.request_handler = nxt_cgo_request_handler;
34     init.callbacks.add_port        = nxt_cgo_add_port;
35     init.callbacks.remove_port     = nxt_cgo_remove_port;
36     init.callbacks.port_send       = nxt_cgo_port_send;
37     init.callbacks.port_recv       = nxt_cgo_port_recv;
38     init.callbacks.shm_ack_handler = nxt_cgo_shm_ack_handler;
39 
40     init.data = (void *) handler;
41 
42     ctx = nxt_unit_init(&init);
43     if (ctx == NULL) {
44         return NXT_UNIT_ERROR;
45     }
46 
47     rc = nxt_unit_run(ctx);
48 
49     nxt_unit_done(ctx);
50 
51     return rc;
52 }
53 
54 
55 static void
56 nxt_cgo_request_handler(nxt_unit_request_info_t *req)
57 {
58     uint32_t            i;
59     uintptr_t           go_req;
60     nxt_cgo_str_t       method, uri, name, value, proto, host, remote_addr;
61     nxt_unit_field_t    *f;
62     nxt_unit_request_t  *r;
63 
64     r = req->request;
65 
66     go_req = nxt_go_request_create((uintptr_t) req,
67                 nxt_cgo_str_init(&method, &r->method, r->method_length),
68                 nxt_cgo_str_init(&uri, &r->target, r->target_length));
69 
70     nxt_go_request_set_proto(go_req,
71         nxt_cgo_str_init(&proto, &r->version, r->version_length), 1, 1);
72 
73     for (i = 0; i < r->fields_count; i++) {
74         f = &r->fields[i];
75 
76         nxt_go_request_add_header(go_req,
77             nxt_cgo_str_init(&name, &f->name, f->name_length),
78             nxt_cgo_str_init(&value, &f->value, f->value_length));
79     }
80 
81     nxt_go_request_set_content_length(go_req, r->content_length);
82     nxt_go_request_set_host(go_req,
83         nxt_cgo_str_init(&host, &r->server_name, r->server_name_length));
84     nxt_go_request_set_remote_addr(go_req,
85         nxt_cgo_str_init(&remote_addr, &r->remote, r->remote_length));
86 
87     if (r->tls) {
88         nxt_go_request_set_tls(go_req);
89     }
90 
91     nxt_go_request_handler(go_req, (uintptr_t) req->unit->data);
92 }
93 
94 
95 static nxt_cgo_str_t *
96 nxt_cgo_str_init(nxt_cgo_str_t *dst, nxt_unit_sptr_t *sptr, uint32_t length)
97 {
98     dst->length = length;
99     dst->start = nxt_unit_sptr_get(sptr);
100 
101     return dst;
102 }
103 
104 
105 static int
106 nxt_cgo_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)
107 {
108     nxt_go_add_port(port->id.pid, port->id.id,
109                     port->in_fd, port->out_fd);
110 
111     port->in_fd = -1;
112     port->out_fd = -1;
113 
114     return NXT_UNIT_OK;
115 }
116 
117 
118 static void
119 nxt_cgo_remove_port(nxt_unit_t *unit, nxt_unit_port_t *port)
120 {
121     nxt_go_remove_port(port->id.pid, port->id.id);
122 }
123 
124 
125 static ssize_t
126 nxt_cgo_port_send(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port,
127     const void *buf, size_t buf_size, const void *oob, size_t oob_size)
128 {
129     return nxt_go_port_send(port->id.pid, port->id.id,
130                             (void *) buf, buf_size, (void *) oob, oob_size);
131 }
132 
133 
134 static ssize_t
135 nxt_cgo_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port,
136     void *buf, size_t buf_size, void *oob, size_t oob_size)
137 {
138     return nxt_go_port_recv(port->id.pid, port->id.id,
139                             buf, buf_size, oob, oob_size);
140 }
141 
142 
143 static void
144 nxt_cgo_shm_ack_handler(nxt_unit_ctx_t *ctx)
145 {
146     return nxt_go_shm_ack_handler();
147 }
148 
149 
150 int
151 nxt_cgo_response_create(uintptr_t req, int status, int fields,
152     uint32_t fields_size)
153 {
154     return nxt_unit_response_init((nxt_unit_request_info_t *) req,
155                                   status, fields, fields_size);
156 }
157 
158 
159 int
160 nxt_cgo_response_add_field(uintptr_t req, uintptr_t name, uint8_t name_len,
161     uintptr_t value, uint32_t value_len)
162 {
163     return nxt_unit_response_add_field((nxt_unit_request_info_t *) req,
164                                        (char *) name, name_len,
165                                        (char *) value, value_len);
166 }
167 
168 
169 int
170 nxt_cgo_response_send(uintptr_t req)
171 {
172     return nxt_unit_response_send((nxt_unit_request_info_t *) req);
173 }
174 
175 
176 ssize_t
177 nxt_cgo_response_write(uintptr_t req, uintptr_t start, uint32_t len)
178 {
179     return nxt_unit_response_write_nb((nxt_unit_request_info_t *) req,
180                                       (void *) start, len, 0);
181 }
182 
183 
184 ssize_t
185 nxt_cgo_request_read(uintptr_t req, uintptr_t dst, uint32_t dst_len)
186 {
187     return nxt_unit_request_read((nxt_unit_request_info_t *) req,
188                                  (void *) dst, dst_len);
189 }
190 
191 
192 int
193 nxt_cgo_request_close(uintptr_t req)
194 {
195     return 0;
196 }
197 
198 
199 void
200 nxt_cgo_request_done(uintptr_t req, int res)
201 {
202     nxt_unit_request_done((nxt_unit_request_info_t *) req, res);
203 }
204 
205 
206 void
207 nxt_cgo_warn(uintptr_t msg, uint32_t msg_len)
208 {
209     nxt_unit_warn(NULL, "%.*s", (int) msg_len, (char *) msg);
210 }
211