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