| 1 |
|
| 2 |
|
| 3 |
#define NANONEXT_HTTP |
| 4 |
#include "nanonext.h" |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#define DL_PREPEND(head, node) do { \ |
| 9 |
(node)->next = (head); \ |
| 10 |
if ((head) != NULL) \ |
| 11 |
(head)->prev = (node); \ |
| 12 |
(head) = (node); \ |
| 13 |
} while (0) |
| 14 |
|
| 15 |
#define DL_REMOVE(head, node) do { \ |
| 16 |
if ((node)->prev != NULL) \ |
| 17 |
(node)->prev->next = (node)->next; \ |
| 18 |
else if ((head) == (node)) \ |
| 19 |
(head) = (node)->next; \ |
| 20 |
if ((node)->next != NULL) \ |
| 21 |
(node)->next->prev = (node)->prev; \ |
| 22 |
(node)->next = (node)->prev = NULL; \ |
| 23 |
} while (0) |
| 24 |
|
| 25 |
#define SEXP_TO_BUF(data, buf, len) do { \ |
| 26 |
switch (TYPEOF(data)) { \ |
| 27 |
case RAWSXP: \ |
| 28 |
(buf) = (unsigned char *) DATAPTR_RO(data); \ |
| 29 |
(len) = (size_t) XLENGTH(data); \ |
| 30 |
break; \ |
| 31 |
case STRSXP: \ |
| 32 |
(buf) = (unsigned char *) CHAR(STRING_ELT(data, 0)); \ |
| 33 |
(len) = strlen((const char *) (buf)); \ |
| 34 |
break; \ |
| 35 |
default: \ |
| 36 |
Rf_error("`data` must be raw or character"); \ |
| 37 |
} \ |
| 38 |
} while (0) |
| 39 |
|
| 40 |
#define STREAM_CONN_CHECK(xptr, sc) do { \ |
| 41 |
if (NANO_PTR_CHECK(xptr, nano_ConnSymbol)) \ |
| 42 |
Rf_error("`conn` is not a valid connection"); \ |
| 43 |
nano_conn *conn_ = (nano_conn *) NANO_PTR(xptr); \ |
| 44 |
if (conn_->type != NANO_CONN_HTTP_STREAM) \ |
| 45 |
Rf_error("`conn` is not an HTTP stream connection"); \ |
| 46 |
(sc) = (nano_stream_conn *) conn_; \ |
| 47 |
} while (0) |
| 48 |
|
| 49 |
static inline void prot_add(SEXP prot, SEXP obj) { |
| 50 |
SETCDR(prot, Rf_cons(obj, CDR(prot))); |
| 51 |
} |
| 52 |
|
| 53 |
static inline void prot_remove(SEXP prot, SEXP obj) { |
| 54 |
SEXP prev = prot; |
| 55 |
for (SEXP cur = CDR(prot); cur != R_NilValue; prev = cur, cur = CDR(cur)) { |
| 56 |
if (CAR(cur) == obj) { |
| 57 |
SETCDR(prev, CDR(cur)); |
| 58 |
return; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
static SEXP get_list_element(SEXP list, const char *name) { |
| 64 |
SEXP names = Rf_getAttrib(list, R_NamesSymbol); |
| 65 |
if (names != R_NilValue) { |
| 66 |
for (R_xlen_t i = 0; i < Rf_xlength(list); i++) { |
| 67 |
if (!strcmp(CHAR(STRING_ELT(names, i)), name)) |
| 68 |
return VECTOR_ELT(list, i); |
| 69 |
} |
| 70 |
} |
| 71 |
return R_NilValue; |
| 72 |
} |
| 73 |
|
| 74 |
static void http_handler_cb(nng_aio *); |
| 75 |
static void http_invoke_callback(void *); |
| 76 |
static int http_request_unlink(nano_http_request *); |
| 77 |
static void http_cancel_pending_requests(nano_http_server *); |
| 78 |
static void ws_accept_cb(void *); |
| 79 |
static int conn_close_locked(nano_conn *); |
| 80 |
static void conn_close(nano_conn *); |
| 81 |
static void ws_recv_cb(void *); |
| 82 |
static void conn_cleanup(nano_conn *); |
| 83 |
static void ws_invoke_onopen(void *); |
| 84 |
static void ws_invoke_onmessage(void *); |
| 85 |
static void conn_invoke_onclose(void *); |
| 86 |
static void http_server_finalizer(SEXP); |
| 87 |
static void http_server_stop(nano_http_server *); |
| 88 |
static void http_server_cleanup(void *); |
| 89 |
static SEXP make_request_list(nng_http_req *); |
| 90 |
|
| 91 |
static void stream_handler_cb(nng_aio *); |
| 92 |
static void stream_invoke_onrequest(void *); |
| 93 |
static int stream_write_headers(nano_stream_conn *); |
| 94 |
static int stream_write_chunk(nano_stream_conn *, const unsigned char *, size_t); |
| 95 |
static int stream_write_terminator(nano_stream_conn *); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
static void http_handler_cb(nng_aio *aio) { |
| 100 |
|
| 101 |
nng_http_req *req = nng_aio_get_input(aio, 0); |
| 102 |
nng_http_handler *h = nng_aio_get_input(aio, 1); |
| 103 |
nano_http_handler_info *info = nng_http_handler_get_data(h); |
| 104 |
nano_http_server *srv = info->server; |
| 105 |
|
| 106 |
if (srv->state == SERVER_STOPPED) { |
| 107 |
nng_aio_finish(aio, NNG_ECANCELED); |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
nano_http_request *r = calloc(1, sizeof(nano_http_request)); |
| 112 |
if (r == NULL) { |
| 113 |
nng_aio_finish(aio, NNG_ENOMEM); |
| 114 |
return; |
| 115 |
} |
| 116 |
r->aio = aio; |
| 117 |
r->req = req; |
| 118 |
r->callback = info->callback; |
| 119 |
r->server = srv; |
| 120 |
|
| 121 |
nng_mtx_lock(srv->mtx); |
| 122 |
DL_PREPEND(srv->pending_reqs, r); |
| 123 |
nng_mtx_unlock(srv->mtx); |
| 124 |
|
| 125 |
later2(http_invoke_callback, r); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
static int http_request_unlink(nano_http_request *r) { |
| 130 |
|
| 131 |
nano_http_server *srv = r->server; |
| 132 |
nng_mtx_lock(srv->mtx); |
| 133 |
DL_REMOVE(srv->pending_reqs, r); |
| 134 |
const int cancelled = r->cancelled; |
| 135 |
nng_mtx_unlock(srv->mtx); |
| 136 |
|
| 137 |
return cancelled; |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
static void http_cancel_pending_requests(nano_http_server *srv) { |
| 142 |
|
| 143 |
nng_mtx_lock(srv->mtx); |
| 144 |
nano_http_request *r = srv->pending_reqs; |
| 145 |
while (r != NULL) { |
| 146 |
r->cancelled = 1; |
| 147 |
nng_aio_finish(r->aio, NNG_ECANCELED); |
| 148 |
r = r->next; |
| 149 |
} |
| 150 |
nng_mtx_unlock(srv->mtx); |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
static void http_server_stop(nano_http_server *srv) { |
| 156 |
|
| 157 |
nng_http_server_stop(srv->server); |
| 158 |
http_cancel_pending_requests(srv); |
| 159 |
|
| 160 |
for (int i = 0; i < srv->handler_count; i++) { |
| 161 |
if (srv->handlers[i].listener != NULL) { |
| 162 |
nng_aio_stop(srv->handlers[i].accept_aio); |
| 163 |
nng_stream_listener_close(srv->handlers[i].listener); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
nng_mtx_lock(srv->mtx); |
| 168 |
for (nano_conn *conn = srv->handlers[i].conns; conn != NULL; conn = conn->next) { |
| 169 |
conn_close_locked(conn); |
| 170 |
if (!conn->onclose_scheduled) { |
| 171 |
conn->onclose_scheduled = 1; |
| 172 |
later2(conn_invoke_onclose, conn); |
| 173 |
} |
| 174 |
} |
| 175 |
nng_mtx_unlock(srv->mtx); |
| 176 |
|
| 177 |
|
| 178 |
for (nano_conn *conn = srv->handlers[i].conns; conn != NULL; conn = conn->next) { |
| 179 |
if (conn->type == NANO_CONN_WEBSOCKET) { |
| 180 |
nano_ws_conn *ws = (nano_ws_conn *) conn; |
| 181 |
nng_aio_wait(ws->recv_aio); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
static SEXP make_ws_request_list(nng_stream *stream) { |
| 191 |
|
| 192 |
const char *names[] = {"uri", "headers", ""}; |
| 193 |
SEXP request; |
| 194 |
PROTECT(request = Rf_mkNamed(VECSXP, names)); |
| 195 |
|
| 196 |
char *uri_str = NULL; |
| 197 |
if (nng_stream_get_string(stream, NNG_OPT_WS_REQUEST_URI, &uri_str) == 0) { |
| 198 |
SET_VECTOR_ELT(request, 0, Rf_mkString(uri_str)); |
| 199 |
nng_strfree(uri_str); |
| 200 |
} |
| 201 |
|
| 202 |
char *headers_str = NULL; |
| 203 |
if (nng_stream_get_string(stream, NNG_OPT_WS_REQUEST_HEADERS, &headers_str) == 0) { |
| 204 |
|
| 205 |
int count = 0; |
| 206 |
for (const char *p = headers_str; *p != '\0'; ) { |
| 207 |
const char *eol = strstr(p, "\r\n"); |
| 208 |
if (eol == NULL) break; |
| 209 |
if (eol > p) count++; |
| 210 |
p = eol + 2; |
| 211 |
} |
| 212 |
|
| 213 |
SEXP headers = Rf_allocVector(STRSXP, count); |
| 214 |
SET_VECTOR_ELT(request, 1, headers); |
| 215 |
SEXP hdr_names = Rf_allocVector(STRSXP, count); |
| 216 |
Rf_namesgets(headers, hdr_names); |
| 217 |
|
| 218 |
int i = 0; |
| 219 |
for (const char *p = headers_str; *p != '\0' && i < count; ) { |
| 220 |
const char *eol = strstr(p, "\r\n"); |
| 221 |
if (eol == NULL) break; |
| 222 |
if (eol > p) { |
| 223 |
const char *sep = strstr(p, ": "); |
| 224 |
if (sep != NULL && sep < eol) { |
| 225 |
SET_STRING_ELT(hdr_names, i, Rf_mkCharLen(p, (int) (sep - p))); |
| 226 |
SET_STRING_ELT(headers, i, Rf_mkCharLen(sep + 2, (int) (eol - sep - 2))); |
| 227 |
} else { |
| 228 |
SET_STRING_ELT(hdr_names, i, Rf_mkCharLen(p, (int) (eol - p))); |
| 229 |
SET_STRING_ELT(headers, i, Rf_mkChar("")); |
| 230 |
} |
| 231 |
i++; |
| 232 |
} |
| 233 |
p = eol + 2; |
| 234 |
} |
| 235 |
|
| 236 |
nng_strfree(headers_str); |
| 237 |
} |
| 238 |
|
| 239 |
UNPROTECT(1); |
| 240 |
return request; |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
static SEXP make_request_list(nng_http_req *req) { |
| 245 |
|
| 246 |
const char *names[] = {"method", "uri", "headers", "body", ""}; |
| 247 |
SEXP request, headers, hdr_names, body; |
| 248 |
PROTECT(request = Rf_mkNamed(VECSXP, names)); |
| 249 |
SET_VECTOR_ELT(request, 0, Rf_mkString(nng_http_req_get_method(req))); |
| 250 |
SET_VECTOR_ELT(request, 1, Rf_mkString(nng_http_req_get_uri(req))); |
| 251 |
|
| 252 |
int header_count = 0; |
| 253 |
for (nano_http_header_s *hdr = nano_http_header_first(req); hdr != NULL; |
| 254 |
hdr = nano_http_header_next(req, hdr)) |
| 255 |
header_count++; |
| 256 |
|
| 257 |
headers = Rf_allocVector(STRSXP, header_count); |
| 258 |
SET_VECTOR_ELT(request, 2, headers); |
| 259 |
hdr_names = Rf_allocVector(STRSXP, header_count); |
| 260 |
Rf_namesgets(headers, hdr_names); |
| 261 |
int i = 0; |
| 262 |
for (nano_http_header_s *hdr = nano_http_header_first(req); hdr != NULL; |
| 263 |
hdr = nano_http_header_next(req, hdr), i++) { |
| 264 |
SET_STRING_ELT(headers, i, Rf_mkChar(hdr->value)); |
| 265 |
SET_STRING_ELT(hdr_names, i, Rf_mkChar(hdr->name)); |
| 266 |
} |
| 267 |
|
| 268 |
void *body_data; |
| 269 |
size_t body_len; |
| 270 |
nng_http_req_get_data(req, &body_data, &body_len); |
| 271 |
body = Rf_allocVector(RAWSXP, body_len); |
| 272 |
SET_VECTOR_ELT(request, 3, body); |
| 273 |
if (body_len) |
| 274 |
memcpy(NANO_DATAPTR(body), body_data, body_len); |
| 275 |
|
| 276 |
UNPROTECT(1); |
| 277 |
return request; |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
static void http_server_cleanup(void *arg) { |
| 282 |
|
| 283 |
nano_http_server *srv = (nano_http_server *) arg; |
| 284 |
|
| 285 |
for (int i = 0; i < srv->handler_count; i++) { |
| 286 |
if (srv->handlers[i].handler != NULL) |
| 287 |
nng_http_server_del_handler(srv->server, srv->handlers[i].handler); |
| 288 |
|
| 289 |
if (srv->handlers[i].listener != NULL) { |
| 290 |
nng_stream_listener_free(srv->handlers[i].listener); |
| 291 |
nng_aio_free(srv->handlers[i].accept_aio); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
nano_conn *conn = srv->handlers[i].conns; |
| 296 |
while (conn != NULL) { |
| 297 |
nano_conn *next = conn->next; |
| 298 |
if (conn->xptr != R_NilValue) |
| 299 |
R_ClearExternalPtr(conn->xptr); |
| 300 |
if (conn->type == NANO_CONN_WEBSOCKET) { |
| 301 |
nano_ws_conn *ws = (nano_ws_conn *) conn; |
| 302 |
nng_stream_free(ws->stream); |
| 303 |
nng_aio_free(ws->recv_aio); |
| 304 |
} else { |
| 305 |
nano_stream_conn *sc = (nano_stream_conn *) conn; |
| 306 |
for (int j = 0; j < sc->resp_header_count; j++) { |
| 307 |
free(sc->resp_header_names[j]); |
| 308 |
free(sc->resp_header_values[j]); |
| 309 |
} |
| 310 |
free(sc->resp_header_names); |
| 311 |
free(sc->resp_header_values); |
| 312 |
} |
| 313 |
nng_aio_free(conn->send_aio); |
| 314 |
free(conn); |
| 315 |
conn = next; |
| 316 |
} |
| 317 |
} |
| 318 |
free(srv->handlers); |
| 319 |
|
| 320 |
if (srv->tls != NULL) |
| 321 |
nng_tls_config_free(srv->tls); |
| 322 |
nng_http_server_release(srv->server); |
| 323 |
if (srv->mtx != NULL) |
| 324 |
nng_mtx_free(srv->mtx); |
| 325 |
free(srv); |
| 326 |
|
| 327 |
} |
| 328 |
|
| 329 |
static void http_server_finalizer(SEXP xptr) { |
| 330 |
|
| 331 |
if (NANO_PTR(xptr) == NULL) return; |
| 332 |
nano_http_server *srv = (nano_http_server *) NANO_PTR(xptr); |
| 333 |
|
| 334 |
if (srv->state == SERVER_STARTED) { |
| 335 |
srv->state = SERVER_STOPPED; |
| 336 |
http_server_stop(srv); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
later2(http_server_cleanup, srv); |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
static void http_invoke_callback(void *arg) { |
| 345 |
|
| 346 |
nano_http_request *r = (nano_http_request *) arg; |
| 347 |
|
| 348 |
const int cancelled = http_request_unlink(r); |
| 349 |
if (cancelled) { |
| 350 |
free(r); |
| 351 |
return; |
| 352 |
} |
| 353 |
|
| 354 |
SEXP req_list, call; |
| 355 |
PROTECT(req_list = make_request_list(r->req)); |
| 356 |
PROTECT(call = Rf_lang2(r->callback, req_list)); |
| 357 |
int err; |
| 358 |
SEXP result = R_tryEval(call, R_GlobalEnv, &err); |
| 359 |
UNPROTECT(2); |
| 360 |
|
| 361 |
nng_http_res *res = NULL; |
| 362 |
if (err) { |
| 363 |
nng_http_res_alloc_error(&res, 500); |
| 364 |
} else { |
| 365 |
PROTECT(result); |
| 366 |
nng_http_res_alloc(&res); |
| 367 |
|
| 368 |
SEXP status_elem = get_list_element(result, "status"); |
| 369 |
int status_code = TYPEOF(status_elem) == INTSXP ? NANO_INTEGER(status_elem) : 200; |
| 370 |
nng_http_res_set_status(res, (uint16_t) status_code); |
| 371 |
|
| 372 |
SEXP res_headers = get_list_element(result, "headers"); |
| 373 |
if (TYPEOF(res_headers) == STRSXP) { |
| 374 |
PROTECT(res_headers); |
| 375 |
SEXP hdr_nm = Rf_getAttrib(res_headers, R_NamesSymbol); |
| 376 |
if (TYPEOF(hdr_nm) == STRSXP) { |
| 377 |
const SEXP *hdr_nm_p = STRING_PTR_RO(hdr_nm); |
| 378 |
const SEXP *res_headers_p = STRING_PTR_RO(res_headers); |
| 379 |
for (int i = 0; i < XLENGTH(res_headers); i++) { |
| 380 |
nng_http_res_set_header(res, CHAR(hdr_nm_p[i]), CHAR(res_headers_p[i])); |
| 381 |
} |
| 382 |
} |
| 383 |
UNPROTECT(1); |
| 384 |
} |
| 385 |
|
| 386 |
SEXP res_body = get_list_element(result, "body"); |
| 387 |
switch (TYPEOF(res_body)) { |
| 388 |
case RAWSXP: |
| 389 |
nng_http_res_copy_data(res, NANO_DATAPTR(res_body), XLENGTH(res_body)); |
| 390 |
break; |
| 391 |
case STRSXP: |
| 392 |
if (XLENGTH(res_body) > 0) { |
| 393 |
const char *str = CHAR(STRING_ELT(res_body, 0)); |
| 394 |
nng_http_res_copy_data(res, str, strlen(str)); |
| 395 |
if (nng_http_res_get_header(res, "Content-Type") == NULL) |
| 396 |
nng_http_res_set_header(res, "Content-Type", "text/plain; charset=utf-8"); |
| 397 |
} |
| 398 |
break; |
| 399 |
default: |
| 400 |
break; |
| 401 |
} |
| 402 |
|
| 403 |
UNPROTECT(1); |
| 404 |
} |
| 405 |
|
| 406 |
nng_aio_set_output(r->aio, 0, res); |
| 407 |
nng_aio_finish(r->aio, 0); |
| 408 |
free(r); |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
static void ws_accept_cb(void *arg) { |
| 415 |
|
| 416 |
nano_http_handler_info *info = (nano_http_handler_info *) arg; |
| 417 |
nano_http_server *srv = info->server; |
| 418 |
const int xc = nng_aio_result(info->accept_aio); |
| 419 |
|
| 420 |
if (xc == 0) { |
| 421 |
nng_stream *stream = nng_aio_get_output(info->accept_aio, 0); |
| 422 |
|
| 423 |
nano_ws_conn *ws = calloc(1, sizeof(nano_ws_conn)); |
| 424 |
if (ws == NULL) { |
| 425 |
nng_stream_free(stream); |
| 426 |
goto next; |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
ws->conn.type = NANO_CONN_WEBSOCKET; |
| 431 |
ws->conn.handler = info; |
| 432 |
ws->conn.state = CONN_STATE_OPEN; |
| 433 |
ws->conn.xptr = R_NilValue; |
| 434 |
|
| 435 |
|
| 436 |
ws->stream = stream; |
| 437 |
|
| 438 |
if (nng_aio_alloc(&ws->recv_aio, ws_recv_cb, ws) || |
| 439 |
nng_aio_alloc(&ws->conn.send_aio, NULL, NULL)) { |
| 440 |
nng_aio_free(ws->recv_aio); |
| 441 |
nng_stream_free(stream); |
| 442 |
free(ws); |
| 443 |
goto next; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
nano_conn *conn = &ws->conn; |
| 448 |
nng_mtx_lock(srv->mtx); |
| 449 |
conn->id = ++srv->conn_counter; |
| 450 |
DL_PREPEND(info->conns, conn); |
| 451 |
nng_mtx_unlock(srv->mtx); |
| 452 |
|
| 453 |
later2(ws_invoke_onopen, ws); |
| 454 |
} |
| 455 |
|
| 456 |
next: |
| 457 |
if (srv->state != SERVER_STOPPED) { |
| 458 |
nng_stream_listener_accept(info->listener, info->accept_aio); |
| 459 |
} |
| 460 |
|
| 461 |
} |
| 462 |
|
| 463 |
static inline int conn_is_open(nano_conn *conn) { |
| 464 |
|
| 465 |
nano_http_server *srv = conn->handler->server; |
| 466 |
nng_mtx_lock(srv->mtx); |
| 467 |
const int is_open = conn->state == CONN_STATE_OPEN; |
| 468 |
nng_mtx_unlock(srv->mtx); |
| 469 |
return is_open; |
| 470 |
|
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
static int conn_close_locked(nano_conn *conn) { |
| 475 |
|
| 476 |
if (conn->state != CONN_STATE_OPEN) |
| 477 |
return 0; |
| 478 |
|
| 479 |
conn->state = CONN_STATE_CLOSING; |
| 480 |
|
| 481 |
|
| 482 |
nng_aio_cancel(conn->send_aio); |
| 483 |
|
| 484 |
|
| 485 |
if (conn->type == NANO_CONN_WEBSOCKET) { |
| 486 |
nano_ws_conn *ws = (nano_ws_conn *) conn; |
| 487 |
nng_aio_cancel(ws->recv_aio); |
| 488 |
nng_stream_close(ws->stream); |
| 489 |
} else { |
| 490 |
nano_stream_conn *sc = (nano_stream_conn *) conn; |
| 491 |
nng_http_conn_close(sc->http); |
| 492 |
} |
| 493 |
return 1; |
| 494 |
|
| 495 |
} |
| 496 |
|
| 497 |
static void conn_close(nano_conn *conn) { |
| 498 |
|
| 499 |
nano_http_server *srv = conn->handler->server; |
| 500 |
nng_mtx_lock(srv->mtx); |
| 501 |
conn_close_locked(conn); |
| 502 |
const int scheduled = conn->onclose_scheduled; |
| 503 |
conn->onclose_scheduled = 1; |
| 504 |
nng_mtx_unlock(srv->mtx); |
| 505 |
|
| 506 |
if (!scheduled) |
| 507 |
later2(conn_invoke_onclose, conn); |
| 508 |
|
| 509 |
} |
| 510 |
|
| 511 |
static void ws_recv_cb(void *arg) { |
| 512 |
|
| 513 |
nano_ws_conn *ws = (nano_ws_conn *) arg; |
| 514 |
const int xc = nng_aio_result(ws->recv_aio); |
| 515 |
|
| 516 |
if (xc == 0) { |
| 517 |
nng_msg *msgp = nng_aio_get_msg(ws->recv_aio); |
| 518 |
|
| 519 |
ws_message *msg = malloc(sizeof(ws_message)); |
| 520 |
if (msg != NULL) { |
| 521 |
msg->conn = ws; |
| 522 |
msg->msg = msgp; |
| 523 |
later2(ws_invoke_onmessage, msg); |
| 524 |
} else { |
| 525 |
nng_msg_free(msgp); |
| 526 |
} |
| 527 |
|
| 528 |
nano_http_server *srv = ws->conn.handler->server; |
| 529 |
nng_mtx_lock(srv->mtx); |
| 530 |
if (ws->conn.state == CONN_STATE_OPEN) |
| 531 |
nng_stream_recv(ws->stream, ws->recv_aio); |
| 532 |
nng_mtx_unlock(srv->mtx); |
| 533 |
|
| 534 |
} else { |
| 535 |
conn_close(&ws->conn); |
| 536 |
} |
| 537 |
|
| 538 |
} |
| 539 |
|
| 540 |
static void conn_cleanup(nano_conn *conn) { |
| 541 |
|
| 542 |
nano_http_handler_info *info = conn->handler; |
| 543 |
nano_http_server *srv = info->server; |
| 544 |
|
| 545 |
nng_mtx_lock(srv->mtx); |
| 546 |
DL_REMOVE(info->conns, conn); |
| 547 |
nng_mtx_unlock(srv->mtx); |
| 548 |
|
| 549 |
conn->state = CONN_STATE_CLOSED; |
| 550 |
|
| 551 |
if (conn->xptr != R_NilValue) { |
| 552 |
prot_remove(srv->prot, conn->xptr); |
| 553 |
R_ClearExternalPtr(conn->xptr); |
| 554 |
conn->xptr = R_NilValue; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
if (conn->type == NANO_CONN_WEBSOCKET) { |
| 559 |
nano_ws_conn *ws = (nano_ws_conn *) conn; |
| 560 |
nng_stream_free(ws->stream); |
| 561 |
nng_aio_free(ws->recv_aio); |
| 562 |
} else { |
| 563 |
nano_stream_conn *sc = (nano_stream_conn *) conn; |
| 564 |
for (int i = 0; i < sc->resp_header_count; i++) { |
| 565 |
free(sc->resp_header_names[i]); |
| 566 |
free(sc->resp_header_values[i]); |
| 567 |
} |
| 568 |
free(sc->resp_header_names); |
| 569 |
free(sc->resp_header_values); |
| 570 |
} |
| 571 |
|
| 572 |
nng_aio_free(conn->send_aio); |
| 573 |
free(conn); |
| 574 |
|
| 575 |
} |
| 576 |
|
| 577 |
static void ws_invoke_onopen(void *arg) { |
| 578 |
|
| 579 |
nano_ws_conn *ws = (nano_ws_conn *) arg; |
| 580 |
if (!conn_is_open(&ws->conn)) |
| 581 |
return; |
| 582 |
nano_http_handler_info *info = ws->conn.handler; |
| 583 |
|
| 584 |
SEXP xptr; |
| 585 |
PROTECT(xptr = R_MakeExternalPtr(ws, nano_ConnSymbol, R_NilValue)); |
| 586 |
NANO_CLASS2(xptr, "nanoWsConn", "nano"); |
| 587 |
Rf_setAttrib(xptr, nano_IdSymbol, Rf_ScalarInteger(ws->conn.id)); |
| 588 |
prot_add(info->server->prot, xptr); |
| 589 |
ws->conn.xptr = xptr; |
| 590 |
UNPROTECT(1); |
| 591 |
|
| 592 |
if (info->on_open != R_NilValue) { |
| 593 |
SEXP request, call; |
| 594 |
PROTECT(request = make_ws_request_list(ws->stream)); |
| 595 |
PROTECT(call = Rf_lang3(info->on_open, ws->conn.xptr, request)); |
| 596 |
R_tryEval(call, R_GlobalEnv, NULL); |
| 597 |
UNPROTECT(2); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
if (NANO_PTR(ws->conn.xptr) == NULL) |
| 602 |
return; |
| 603 |
|
| 604 |
nano_http_server *srv = info->server; |
| 605 |
nng_mtx_lock(srv->mtx); |
| 606 |
if (ws->conn.state == CONN_STATE_OPEN) |
| 607 |
nng_stream_recv(ws->stream, ws->recv_aio); |
| 608 |
nng_mtx_unlock(srv->mtx); |
| 609 |
|
| 610 |
} |
| 611 |
|
| 612 |
static void ws_invoke_onmessage(void *arg) { |
| 613 |
|
| 614 |
ws_message *wsmsg = (ws_message *) arg; |
| 615 |
nano_ws_conn *ws = wsmsg->conn; |
| 616 |
if (!conn_is_open(&ws->conn)) { |
| 617 |
nng_msg_free(wsmsg->msg); |
| 618 |
free(wsmsg); |
| 619 |
return; |
| 620 |
} |
| 621 |
nano_http_handler_info *info = ws->conn.handler; |
| 622 |
|
| 623 |
const size_t len = nng_msg_len(wsmsg->msg); |
| 624 |
unsigned char *buf = nng_msg_body(wsmsg->msg); |
| 625 |
|
| 626 |
SEXP data; |
| 627 |
if (info->textframes) { |
| 628 |
PROTECT(data = Rf_ScalarString(Rf_mkCharLenCE((char *) buf, len, CE_UTF8))); |
| 629 |
} else { |
| 630 |
PROTECT(data = Rf_allocVector(RAWSXP, len)); |
| 631 |
memcpy(NANO_DATAPTR(data), buf, len); |
| 632 |
} |
| 633 |
|
| 634 |
if (info->callback != R_NilValue) { |
| 635 |
SEXP call; |
| 636 |
PROTECT(call = Rf_lang3(info->callback, ws->conn.xptr, data)); |
| 637 |
R_tryEval(call, R_GlobalEnv, NULL); |
| 638 |
UNPROTECT(1); |
| 639 |
} |
| 640 |
|
| 641 |
UNPROTECT(1); |
| 642 |
nng_msg_free(wsmsg->msg); |
| 643 |
free(wsmsg); |
| 644 |
|
| 645 |
} |
| 646 |
|
| 647 |
static void conn_invoke_onclose(void *arg) { |
| 648 |
|
| 649 |
nano_conn *conn = (nano_conn *) arg; |
| 650 |
nano_http_handler_info *info = conn->handler; |
| 651 |
|
| 652 |
if (info->on_close != R_NilValue && conn->xptr != R_NilValue) { |
| 653 |
SEXP call; |
| 654 |
PROTECT(call = Rf_lang2(info->on_close, conn->xptr)); |
| 655 |
R_tryEval(call, R_GlobalEnv, NULL); |
| 656 |
UNPROTECT(1); |
| 657 |
} |
| 658 |
|
| 659 |
conn_cleanup(conn); |
| 660 |
|
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
SEXP rnng_http_server_create(SEXP url, SEXP handlers, SEXP tls) { |
| 666 |
|
| 667 |
if (tls != R_NilValue && NANO_PTR_CHECK(tls, nano_TlsSymbol)) |
| 668 |
Rf_error("`tls` is not a valid TLS Configuration"); |
| 669 |
|
| 670 |
if (eln2 == NULL) |
| 671 |
nano_load_later(); |
| 672 |
|
| 673 |
const char *ur = CHAR(STRING_ELT(url, 0)); |
| 674 |
nng_url *up = NULL; |
| 675 |
nano_http_handler_info *hinfo = NULL; |
| 676 |
int xc; |
| 677 |
int handlers_added = 0; |
| 678 |
|
| 679 |
nano_http_server *srv = calloc(1, sizeof(nano_http_server)); |
| 680 |
if (srv == NULL) { |
| 681 |
xc = NNG_ENOMEM; |
| 682 |
goto fail; |
| 683 |
} |
| 684 |
srv->xptr = R_NilValue; |
| 685 |
srv->prot = R_NilValue; |
| 686 |
if ((xc = nng_mtx_alloc(&srv->mtx))) |
| 687 |
goto fail; |
| 688 |
|
| 689 |
SEXP prot; |
| 690 |
PROTECT(prot = Rf_cons(R_NilValue, R_NilValue)); |
| 691 |
|
| 692 |
if ((xc = nng_url_parse(&up, ur))) |
| 693 |
goto fail; |
| 694 |
|
| 695 |
if ((xc = nng_http_server_hold(&srv->server, up))) |
| 696 |
goto fail; |
| 697 |
|
| 698 |
if (tls != R_NilValue) { |
| 699 |
srv->tls = (nng_tls_config *) NANO_PTR(tls); |
| 700 |
nng_tls_config_hold(srv->tls); |
| 701 |
prot_add(prot, tls); |
| 702 |
if ((xc = nng_http_server_set_tls(srv->server, srv->tls))) |
| 703 |
goto fail; |
| 704 |
} |
| 705 |
|
| 706 |
const R_xlen_t handler_count = Rf_xlength(handlers); |
| 707 |
srv->handler_count = handler_count; |
| 708 |
|
| 709 |
if (handler_count > 0) { |
| 710 |
hinfo = calloc(handler_count, sizeof(nano_http_handler_info)); |
| 711 |
if (hinfo == NULL) { |
| 712 |
xc = NNG_ENOMEM; |
| 713 |
goto fail; |
| 714 |
} |
| 715 |
srv->handlers = hinfo; |
| 716 |
|
| 717 |
for (R_xlen_t i = 0; i < handler_count; i++) { |
| 718 |
srv->handlers[i].callback = R_NilValue; |
| 719 |
srv->handlers[i].on_open = R_NilValue; |
| 720 |
srv->handlers[i].on_close = R_NilValue; |
| 721 |
} |
| 722 |
|
| 723 |
for (R_xlen_t i = 0; i < handler_count; i++) { |
| 724 |
SEXP h = VECTOR_ELT(handlers, i); |
| 725 |
|
| 726 |
const int type = NANO_INTEGER(get_list_element(h, "type")); |
| 727 |
SEXP path_elem = get_list_element(h, "path"); |
| 728 |
const char *path = CHAR(STRING_ELT(path_elem, 0)); |
| 729 |
SEXP tree_elem = get_list_element(h, "prefix"); |
| 730 |
const int tree = (tree_elem != R_NilValue) ? NANO_INTEGER(tree_elem) : 0; |
| 731 |
|
| 732 |
switch (type) { |
| 733 |
case 1: { |
| 734 |
|
| 735 |
SEXP method_elem = get_list_element(h, "method"); |
| 736 |
const char *method = CHAR(STRING_ELT(method_elem, 0)); |
| 737 |
if (method[0] == '*' && method[1] == '\0') |
| 738 |
method = NULL; |
| 739 |
|
| 740 |
if ((xc = nng_http_handler_alloc(&srv->handlers[i].handler, path, http_handler_cb))) |
| 741 |
goto fail; |
| 742 |
|
| 743 |
if ((xc = nng_http_handler_set_method(srv->handlers[i].handler, method))) |
| 744 |
goto fail; |
| 745 |
|
| 746 |
if (tree && (xc = nng_http_handler_set_tree(srv->handlers[i].handler))) |
| 747 |
goto fail; |
| 748 |
|
| 749 |
SEXP callback = get_list_element(h, "callback"); |
| 750 |
prot_add(prot, callback); |
| 751 |
srv->handlers[i].callback = callback; |
| 752 |
srv->handlers[i].server = srv; |
| 753 |
nng_http_handler_set_data(srv->handlers[i].handler, &srv->handlers[i], NULL); |
| 754 |
|
| 755 |
if ((xc = nng_http_server_add_handler(srv->server, srv->handlers[i].handler))) |
| 756 |
goto fail; |
| 757 |
|
| 758 |
handlers_added++; |
| 759 |
break; |
| 760 |
} |
| 761 |
case 2: { |
| 762 |
|
| 763 |
SEXP textframes_elem = get_list_element(h, "textframes"); |
| 764 |
const int textframes = textframes_elem != R_NilValue ? NANO_INTEGER(textframes_elem) : 0; |
| 765 |
|
| 766 |
const char *scheme = strcmp(up->u_scheme, "https") == 0 ? "wss" : "ws"; |
| 767 |
const size_t ws_url_len = strlen(scheme) + strlen(up->u_hostname) + |
| 768 |
strlen(up->u_port) + strlen(path) + 5; |
| 769 |
if (ws_url_len > NANO_URL_MAX) { |
| 770 |
xc = NNG_EINVAL; |
| 771 |
goto fail; |
| 772 |
} |
| 773 |
char ws_url[ws_url_len]; |
| 774 |
snprintf(ws_url, ws_url_len, "%s://%s:%s%s", scheme, up->u_hostname, up->u_port, path); |
| 775 |
|
| 776 |
if ((xc = nng_stream_listener_alloc(&srv->handlers[i].listener, ws_url)) || |
| 777 |
(xc = nng_stream_listener_set_bool(srv->handlers[i].listener, "ws:msgmode", true))) |
| 778 |
goto fail; |
| 779 |
|
| 780 |
if (textframes) { |
| 781 |
if ((xc = nng_stream_listener_set_bool(srv->handlers[i].listener, "ws:recv-text", true)) || |
| 782 |
(xc = nng_stream_listener_set_bool(srv->handlers[i].listener, "ws:send-text", true))) |
| 783 |
goto fail; |
| 784 |
|
| 785 |
srv->handlers[i].textframes = 1; |
| 786 |
} |
| 787 |
|
| 788 |
if (srv->tls != NULL) { |
| 789 |
nng_stream_listener_set_ptr(srv->handlers[i].listener, NNG_OPT_TLS_CONFIG, srv->tls); |
| 790 |
} |
| 791 |
|
| 792 |
SEXP on_message = get_list_element(h, "on_message"); |
| 793 |
prot_add(prot, on_message); |
| 794 |
srv->handlers[i].callback = on_message; |
| 795 |
SEXP on_open = get_list_element(h, "on_open"); |
| 796 |
if (on_open != R_NilValue) { |
| 797 |
prot_add(prot, on_open); |
| 798 |
srv->handlers[i].on_open = on_open; |
| 799 |
} |
| 800 |
SEXP on_close = get_list_element(h, "on_close"); |
| 801 |
if (on_close != R_NilValue) { |
| 802 |
prot_add(prot, on_close); |
| 803 |
srv->handlers[i].on_close = on_close; |
| 804 |
} |
| 805 |
|
| 806 |
srv->handlers[i].server = srv; |
| 807 |
|
| 808 |
if ((xc = nng_aio_alloc(&srv->handlers[i].accept_aio, ws_accept_cb, &srv->handlers[i]))) |
| 809 |
goto fail; |
| 810 |
|
| 811 |
handlers_added++; |
| 812 |
break; |
| 813 |
} |
| 814 |
case 3: { |
| 815 |
|
| 816 |
SEXP file_elem = get_list_element(h, "file"); |
| 817 |
const char *file = CHAR(STRING_ELT(file_elem, 0)); |
| 818 |
|
| 819 |
if ((xc = nng_http_handler_alloc_file(&srv->handlers[i].handler, path, file))) |
| 820 |
goto fail; |
| 821 |
|
| 822 |
if (tree && (xc = nng_http_handler_set_tree(srv->handlers[i].handler))) |
| 823 |
goto fail; |
| 824 |
|
| 825 |
if ((xc = nng_http_server_add_handler(srv->server, srv->handlers[i].handler))) |
| 826 |
goto fail; |
| 827 |
|
| 828 |
handlers_added++; |
| 829 |
break; |
| 830 |
} |
| 831 |
case 4: { |
| 832 |
|
| 833 |
SEXP dir_elem = get_list_element(h, "directory"); |
| 834 |
const char *dir = CHAR(STRING_ELT(dir_elem, 0)); |
| 835 |
|
| 836 |
if ((xc = nng_http_handler_alloc_directory(&srv->handlers[i].handler, path, dir))) |
| 837 |
goto fail; |
| 838 |
|
| 839 |
if ((xc = nng_http_server_add_handler(srv->server, srv->handlers[i].handler))) |
| 840 |
goto fail; |
| 841 |
|
| 842 |
handlers_added++; |
| 843 |
break; |
| 844 |
} |
| 845 |
case 5: { |
| 846 |
|
| 847 |
SEXP ct_elem = get_list_element(h, "content_type"); |
| 848 |
const char *content_type = TYPEOF(ct_elem) == STRSXP ? |
| 849 |
CHAR(STRING_ELT(ct_elem, 0)) : "application/octet-stream"; |
| 850 |
SEXP data_elem = get_list_element(h, "data"); |
| 851 |
const unsigned char *data; |
| 852 |
size_t size; |
| 853 |
|
| 854 |
switch (TYPEOF(data_elem)) { |
| 855 |
case STRSXP: |
| 856 |
data = (const unsigned char *) CHAR(STRING_ELT(data_elem, 0)); |
| 857 |
size = strlen((const char *) data); |
| 858 |
break; |
| 859 |
default: |
| 860 |
data = NANO_DATAPTR(data_elem); |
| 861 |
size = (size_t) XLENGTH(data_elem); |
| 862 |
} |
| 863 |
|
| 864 |
if ((xc = nng_http_handler_alloc_static(&srv->handlers[i].handler, |
| 865 |
path, data, size, content_type))) |
| 866 |
goto fail; |
| 867 |
|
| 868 |
if (tree && (xc = nng_http_handler_set_tree(srv->handlers[i].handler))) |
| 869 |
goto fail; |
| 870 |
|
| 871 |
if ((xc = nng_http_server_add_handler(srv->server, srv->handlers[i].handler))) |
| 872 |
goto fail; |
| 873 |
|
| 874 |
handlers_added++; |
| 875 |
break; |
| 876 |
} |
| 877 |
case 6: { |
| 878 |
|
| 879 |
SEXP loc_elem = get_list_element(h, "location"); |
| 880 |
const char *location = CHAR(STRING_ELT(loc_elem, 0)); |
| 881 |
SEXP status_elem = get_list_element(h, "status"); |
| 882 |
const int status_int = NANO_INTEGER(status_elem); |
| 883 |
if ((xc = nng_http_handler_alloc_redirect(&srv->handlers[i].handler, |
| 884 |
path, |
| 885 |
(uint16_t) status_int, |
| 886 |
location))) |
| 887 |
goto fail; |
| 888 |
|
| 889 |
if (tree && (xc = nng_http_handler_set_tree(srv->handlers[i].handler))) |
| 890 |
goto fail; |
| 891 |
|
| 892 |
if ((xc = nng_http_server_add_handler(srv->server, srv->handlers[i].handler))) |
| 893 |
goto fail; |
| 894 |
|
| 895 |
handlers_added++; |
| 896 |
break; |
| 897 |
} |
| 898 |
case 7: { |
| 899 |
|
| 900 |
SEXP on_request = get_list_element(h, "on_request"); |
| 901 |
SEXP on_close = get_list_element(h, "on_close"); |
| 902 |
SEXP method_elem = get_list_element(h, "method"); |
| 903 |
const char *method = CHAR(STRING_ELT(method_elem, 0)); |
| 904 |
if (method[0] == '*' && method[1] == '\0') |
| 905 |
method = NULL; |
| 906 |
|
| 907 |
if ((xc = nng_http_handler_alloc(&srv->handlers[i].handler, path, stream_handler_cb))) |
| 908 |
goto fail; |
| 909 |
if ((xc = nng_http_handler_set_method(srv->handlers[i].handler, method))) |
| 910 |
goto fail; |
| 911 |
if (tree && (xc = nng_http_handler_set_tree(srv->handlers[i].handler))) |
| 912 |
goto fail; |
| 913 |
|
| 914 |
srv->handlers[i].callback = on_request; |
| 915 |
prot_add(prot, on_request); |
| 916 |
|
| 917 |
if (on_close != R_NilValue) { |
| 918 |
srv->handlers[i].on_close = on_close; |
| 919 |
prot_add(prot, on_close); |
| 920 |
} |
| 921 |
|
| 922 |
srv->handlers[i].server = srv; |
| 923 |
nng_http_handler_set_data(srv->handlers[i].handler, &srv->handlers[i], NULL); |
| 924 |
|
| 925 |
if ((xc = nng_http_server_add_handler(srv->server, srv->handlers[i].handler))) |
| 926 |
goto fail; |
| 927 |
|
| 928 |
handlers_added++; |
| 929 |
break; |
| 930 |
} |
| 931 |
default: |
| 932 |
xc = NNG_EINVAL; |
| 933 |
goto fail; |
| 934 |
} |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
nng_url_free(up); |
| 939 |
|
| 940 |
SEXP xptr = PROTECT(R_MakeExternalPtr(srv, nano_HttpServerSymbol, prot)); |
| 941 |
R_RegisterCFinalizerEx(xptr, http_server_finalizer, TRUE); |
| 942 |
srv->xptr = xptr; |
| 943 |
srv->prot = prot; |
| 944 |
|
| 945 |
NANO_CLASS2(xptr, "nanoServer", "nano"); |
| 946 |
Rf_setAttrib(xptr, nano_UrlSymbol, url); |
| 947 |
Rf_setAttrib(xptr, nano_StateSymbol, Rf_mkString("not started")); |
| 948 |
|
| 949 |
UNPROTECT(2); |
| 950 |
return xptr; |
| 951 |
|
| 952 |
fail: |
| 953 |
if (srv->tls != NULL) |
| 954 |
nng_tls_config_free(srv->tls); |
| 955 |
for (int i = 0; i < handlers_added; i++) { |
| 956 |
if (srv->handlers[i].handler != NULL) |
| 957 |
nng_http_server_del_handler(srv->server, srv->handlers[i].handler); |
| 958 |
if (srv->handlers[i].listener != NULL) |
| 959 |
nng_stream_listener_free(srv->handlers[i].listener); |
| 960 |
if (srv->handlers[i].accept_aio != NULL) |
| 961 |
nng_aio_free(srv->handlers[i].accept_aio); |
| 962 |
} |
| 963 |
if (hinfo != NULL && handlers_added < handler_count) { |
| 964 |
if (srv->handlers[handlers_added].handler != NULL) |
| 965 |
nng_http_handler_free(srv->handlers[handlers_added].handler); |
| 966 |
if (srv->handlers[handlers_added].listener != NULL) |
| 967 |
nng_stream_listener_free(srv->handlers[handlers_added].listener); |
| 968 |
if (srv->handlers[handlers_added].accept_aio != NULL) |
| 969 |
nng_aio_free(srv->handlers[handlers_added].accept_aio); |
| 970 |
} |
| 971 |
if (srv->server != NULL) nng_http_server_release(srv->server); |
| 972 |
if (srv->mtx != NULL) nng_mtx_free(srv->mtx); |
| 973 |
nng_url_free(up); |
| 974 |
free(hinfo); |
| 975 |
free(srv); |
| 976 |
ERROR_OUT(xc); |
| 977 |
|
| 978 |
} |
| 979 |
|
| 980 |
SEXP rnng_http_server_start(SEXP xptr) { |
| 981 |
|
| 982 |
if (NANO_PTR_CHECK(xptr, nano_HttpServerSymbol)) |
| 983 |
Rf_error("`server` is not a valid HTTP Server"); |
| 984 |
|
| 985 |
nano_http_server *srv = (nano_http_server *) NANO_PTR(xptr); |
| 986 |
|
| 987 |
if (srv->state == SERVER_STARTED) |
| 988 |
return nano_success; |
| 989 |
|
| 990 |
if (srv->state == SERVER_STOPPED) |
| 991 |
Rf_error("server has been stopped"); |
| 992 |
|
| 993 |
int xc; |
| 994 |
|
| 995 |
if ((xc = nng_http_server_start(srv->server))) |
| 996 |
ERROR_OUT(xc); |
| 997 |
|
| 998 |
for (int i = 0; i < srv->handler_count; i++) { |
| 999 |
if (srv->handlers[i].listener != NULL) { |
| 1000 |
if ((xc = nng_stream_listener_listen(srv->handlers[i].listener))) |
| 1001 |
goto fail; |
| 1002 |
nng_stream_listener_accept(srv->handlers[i].listener, srv->handlers[i].accept_aio); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
srv->state = SERVER_STARTED; |
| 1007 |
Rf_setAttrib(xptr, nano_StateSymbol, Rf_mkString("started")); |
| 1008 |
|
| 1009 |
SEXP url = Rf_getAttrib(xptr, nano_UrlSymbol); |
| 1010 |
nng_url *up; |
| 1011 |
if (nng_url_parse(&up, CHAR(STRING_ELT(url, 0))) == 0) { |
| 1012 |
if (up->u_port != NULL && up->u_port[0] == '0' && up->u_port[1] == '\0') { |
| 1013 |
nng_sockaddr sa; |
| 1014 |
if (nng_http_server_get_addr(srv->server, &sa) == 0 && |
| 1015 |
(sa.s_family == NNG_AF_INET || sa.s_family == NNG_AF_INET6)) { |
| 1016 |
int port = (int) ntohs(sa.s_family == NNG_AF_INET ? sa.s_in.sa_port : sa.s_in6.sa_port); |
| 1017 |
Rf_setAttrib(xptr, nano_UrlSymbol, nano_url_with_port(up, port)); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
nng_url_free(up); |
| 1021 |
} |
| 1022 |
|
| 1023 |
return nano_success; |
| 1024 |
|
| 1025 |
fail: |
| 1026 |
for (int j = 0; j < srv->handler_count; j++) { |
| 1027 |
if (srv->handlers[j].listener != NULL) |
| 1028 |
nng_stream_listener_close(srv->handlers[j].listener); |
| 1029 |
} |
| 1030 |
nng_http_server_stop(srv->server); |
| 1031 |
ERROR_OUT(xc); |
| 1032 |
|
| 1033 |
} |
| 1034 |
|
| 1035 |
SEXP rnng_http_server_close(SEXP xptr) { |
| 1036 |
|
| 1037 |
if (NANO_PTR_CHECK(xptr, nano_HttpServerSymbol)) |
| 1038 |
Rf_error("`server` is not a valid HTTP Server"); |
| 1039 |
|
| 1040 |
http_server_finalizer(xptr); |
| 1041 |
R_ClearExternalPtr(xptr); |
| 1042 |
Rf_setAttrib(xptr, nano_StateSymbol, Rf_mkString("closed")); |
| 1043 |
|
| 1044 |
return nano_success; |
| 1045 |
|
| 1046 |
} |
| 1047 |
|
| 1048 |
SEXP rnng_ws_send(SEXP xptr, SEXP data) { |
| 1049 |
|
| 1050 |
if (NANO_PTR_CHECK(xptr, nano_ConnSymbol)) |
| 1051 |
return mk_error(NNG_ECLOSED); |
| 1052 |
|
| 1053 |
nano_ws_conn *ws = (nano_ws_conn *) NANO_PTR(xptr); |
| 1054 |
|
| 1055 |
unsigned char *buf; |
| 1056 |
size_t len; |
| 1057 |
SEXP_TO_BUF(data, buf, len); |
| 1058 |
|
| 1059 |
nng_msg *msgp = NULL; |
| 1060 |
int xc; |
| 1061 |
if ((xc = nng_msg_alloc(&msgp, len))) |
| 1062 |
return mk_error(xc); |
| 1063 |
|
| 1064 |
memcpy(nng_msg_body(msgp), buf, len); |
| 1065 |
|
| 1066 |
nng_aio_set_msg(ws->conn.send_aio, msgp); |
| 1067 |
nng_stream_send(ws->stream, ws->conn.send_aio); |
| 1068 |
nng_aio_wait(ws->conn.send_aio); |
| 1069 |
|
| 1070 |
if ((xc = nng_aio_result(ws->conn.send_aio))) { |
| 1071 |
nng_msg_free(nng_aio_get_msg(ws->conn.send_aio)); |
| 1072 |
return mk_error(xc); |
| 1073 |
} |
| 1074 |
|
| 1075 |
return nano_success; |
| 1076 |
|
| 1077 |
} |
| 1078 |
|
| 1079 |
SEXP rnng_ws_close(SEXP xptr) { |
| 1080 |
|
| 1081 |
if (NANO_PTR_CHECK(xptr, nano_ConnSymbol)) |
| 1082 |
return mk_error(NNG_ECLOSED); |
| 1083 |
|
| 1084 |
nano_ws_conn *ws = (nano_ws_conn *) NANO_PTR(xptr); |
| 1085 |
conn_close(&ws->conn); |
| 1086 |
|
| 1087 |
return nano_success; |
| 1088 |
|
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
static void stream_handler_cb(nng_aio *aio) { |
| 1094 |
|
| 1095 |
nng_http_req *req = nng_aio_get_input(aio, 0); |
| 1096 |
nng_http_handler *h = nng_aio_get_input(aio, 1); |
| 1097 |
nng_http_conn *http_conn = nng_aio_get_input(aio, 2); |
| 1098 |
|
| 1099 |
nano_http_handler_info *info = nng_http_handler_get_data(h); |
| 1100 |
nano_http_server *srv = info->server; |
| 1101 |
|
| 1102 |
if (srv->state == SERVER_STOPPED) { |
| 1103 |
nng_aio_finish(aio, NNG_ECANCELED); |
| 1104 |
return; |
| 1105 |
} |
| 1106 |
|
| 1107 |
|
| 1108 |
nng_http_hijack(http_conn); |
| 1109 |
|
| 1110 |
nano_stream_conn *sc = calloc(1, sizeof(nano_stream_conn)); |
| 1111 |
if (sc == NULL) { |
| 1112 |
nng_http_conn_close(http_conn); |
| 1113 |
nng_aio_finish(aio, 0); |
| 1114 |
return; |
| 1115 |
} |
| 1116 |
|
| 1117 |
sc->conn.type = NANO_CONN_HTTP_STREAM; |
| 1118 |
sc->conn.handler = info; |
| 1119 |
sc->conn.state = CONN_STATE_OPEN; |
| 1120 |
sc->conn.xptr = R_NilValue; |
| 1121 |
|
| 1122 |
sc->http = http_conn; |
| 1123 |
sc->req = req; |
| 1124 |
sc->status_code = 200; |
| 1125 |
|
| 1126 |
if (nng_aio_alloc(&sc->conn.send_aio, NULL, NULL)) { |
| 1127 |
nng_http_conn_close(http_conn); |
| 1128 |
free(sc); |
| 1129 |
nng_aio_finish(aio, 0); |
| 1130 |
return; |
| 1131 |
} |
| 1132 |
|
| 1133 |
nano_conn *conn = &sc->conn; |
| 1134 |
nng_mtx_lock(srv->mtx); |
| 1135 |
conn->id = ++srv->conn_counter; |
| 1136 |
DL_PREPEND(info->conns, conn); |
| 1137 |
nng_mtx_unlock(srv->mtx); |
| 1138 |
|
| 1139 |
later2(stream_invoke_onrequest, conn); |
| 1140 |
nng_aio_finish(aio, 0); |
| 1141 |
|
| 1142 |
} |
| 1143 |
|
| 1144 |
static void stream_invoke_onrequest(void *arg) { |
| 1145 |
|
| 1146 |
nano_conn *conn = (nano_conn *) arg; |
| 1147 |
nano_stream_conn *sc = (nano_stream_conn *) conn; |
| 1148 |
nano_http_handler_info *info = conn->handler; |
| 1149 |
nano_http_server *srv = info->server; |
| 1150 |
|
| 1151 |
if (!conn_is_open(conn)) { |
| 1152 |
conn_cleanup(conn); |
| 1153 |
return; |
| 1154 |
} |
| 1155 |
|
| 1156 |
SEXP xptr; |
| 1157 |
PROTECT(xptr = R_MakeExternalPtr(conn, nano_ConnSymbol, R_NilValue)); |
| 1158 |
NANO_CLASS2(xptr, "nanoStreamConn", "nano"); |
| 1159 |
Rf_setAttrib(xptr, nano_IdSymbol, Rf_ScalarInteger(conn->id)); |
| 1160 |
prot_add(srv->prot, xptr); |
| 1161 |
conn->xptr = xptr; |
| 1162 |
UNPROTECT(1); |
| 1163 |
|
| 1164 |
if (info->callback != R_NilValue) { |
| 1165 |
SEXP request, call; |
| 1166 |
PROTECT(request = make_request_list(sc->req)); |
| 1167 |
PROTECT(call = Rf_lang3(info->callback, conn->xptr, request)); |
| 1168 |
R_tryEval(call, R_GlobalEnv, NULL); |
| 1169 |
UNPROTECT(2); |
| 1170 |
} |
| 1171 |
|
| 1172 |
} |
| 1173 |
|
| 1174 |
static int stream_write_headers(nano_stream_conn *sc) { |
| 1175 |
|
| 1176 |
nng_http_res *res; |
| 1177 |
int xc; |
| 1178 |
|
| 1179 |
if ((xc = nng_http_res_alloc(&res))) |
| 1180 |
return xc; |
| 1181 |
|
| 1182 |
if ((xc = nng_http_res_set_status(res, (uint16_t) sc->status_code))) |
| 1183 |
goto cleanup; |
| 1184 |
|
| 1185 |
if ((xc = nng_http_res_set_header(res, "Transfer-Encoding", "chunked"))) |
| 1186 |
goto cleanup; |
| 1187 |
|
| 1188 |
for (int i = 0; i < sc->resp_header_count; i++) { |
| 1189 |
if ((xc = nng_http_res_set_header(res, sc->resp_header_names[i], |
| 1190 |
sc->resp_header_values[i]))) |
| 1191 |
goto cleanup; |
| 1192 |
} |
| 1193 |
|
| 1194 |
nng_http_conn_write_res(sc->http, res, sc->conn.send_aio); |
| 1195 |
nng_aio_wait(sc->conn.send_aio); |
| 1196 |
xc = nng_aio_result(sc->conn.send_aio); |
| 1197 |
|
| 1198 |
cleanup: |
| 1199 |
nng_http_res_free(res); |
| 1200 |
return xc; |
| 1201 |
|
| 1202 |
} |
| 1203 |
|
| 1204 |
static int stream_write_chunk(nano_stream_conn *sc, const unsigned char *data, |
| 1205 |
size_t len) { |
| 1206 |
|
| 1207 |
|
| 1208 |
char header[24]; |
| 1209 |
int header_len = snprintf(header, sizeof(header), "%" PRIxPTR "\r\n", (uintptr_t) len); |
| 1210 |
|
| 1211 |
size_t chunk_len = (size_t) header_len + len + 2; |
| 1212 |
unsigned char *chunk = malloc(chunk_len); |
| 1213 |
if (chunk == NULL) |
| 1214 |
return NNG_ENOMEM; |
| 1215 |
|
| 1216 |
memcpy(chunk, header, (size_t) header_len); |
| 1217 |
if (len > 0) |
| 1218 |
memcpy(chunk + header_len, data, len); |
| 1219 |
memcpy(chunk + header_len + len, "\r\n", 2); |
| 1220 |
|
| 1221 |
nng_iov iov = { |
| 1222 |
.iov_buf = chunk, |
| 1223 |
.iov_len = chunk_len |
| 1224 |
}; |
| 1225 |
int xc; |
| 1226 |
if ((xc = nng_aio_set_iov(sc->conn.send_aio, 1, &iov))) { |
| 1227 |
free(chunk); |
| 1228 |
return xc; |
| 1229 |
} |
| 1230 |
|
| 1231 |
nng_http_conn_write_all(sc->http, sc->conn.send_aio); |
| 1232 |
nng_aio_wait(sc->conn.send_aio); |
| 1233 |
xc = nng_aio_result(sc->conn.send_aio); |
| 1234 |
|
| 1235 |
free(chunk); |
| 1236 |
return xc; |
| 1237 |
|
| 1238 |
} |
| 1239 |
|
| 1240 |
static int stream_write_terminator(nano_stream_conn *sc) { |
| 1241 |
|
| 1242 |
static const char terminator[] = "0\r\n\r\n"; |
| 1243 |
|
| 1244 |
nng_iov iov = { |
| 1245 |
.iov_buf = (void *) terminator, |
| 1246 |
.iov_len = 5 |
| 1247 |
}; |
| 1248 |
int xc; |
| 1249 |
if ((xc = nng_aio_set_iov(sc->conn.send_aio, 1, &iov))) |
| 1250 |
return xc; |
| 1251 |
|
| 1252 |
nng_http_conn_write_all(sc->http, sc->conn.send_aio); |
| 1253 |
nng_aio_wait(sc->conn.send_aio); |
| 1254 |
return nng_aio_result(sc->conn.send_aio); |
| 1255 |
|
| 1256 |
} |
| 1257 |
|
| 1258 |
SEXP rnng_stream_conn_send(SEXP xptr, SEXP data) { |
| 1259 |
|
| 1260 |
nano_stream_conn *sc; |
| 1261 |
STREAM_CONN_CHECK(xptr, sc); |
| 1262 |
|
| 1263 |
unsigned char *buf; |
| 1264 |
size_t len; |
| 1265 |
SEXP_TO_BUF(data, buf, len); |
| 1266 |
|
| 1267 |
int xc; |
| 1268 |
|
| 1269 |
if (!sc->headers_sent) { |
| 1270 |
if ((xc = stream_write_headers(sc))) |
| 1271 |
return mk_error(xc); |
| 1272 |
sc->headers_sent = 1; |
| 1273 |
} |
| 1274 |
|
| 1275 |
if ((xc = stream_write_chunk(sc, buf, len))) |
| 1276 |
return mk_error(xc); |
| 1277 |
|
| 1278 |
return nano_success; |
| 1279 |
|
| 1280 |
} |
| 1281 |
|
| 1282 |
SEXP rnng_stream_conn_set_status(SEXP xptr, SEXP code) { |
| 1283 |
|
| 1284 |
nano_stream_conn *sc; |
| 1285 |
STREAM_CONN_CHECK(xptr, sc); |
| 1286 |
|
| 1287 |
if (sc->headers_sent) |
| 1288 |
Rf_error("cannot set status after headers have been sent"); |
| 1289 |
|
| 1290 |
sc->status_code = nano_integer(code); |
| 1291 |
return nano_success; |
| 1292 |
|
| 1293 |
} |
| 1294 |
|
| 1295 |
SEXP rnng_stream_conn_set_header(SEXP xptr, SEXP name, SEXP value) { |
| 1296 |
|
| 1297 |
nano_stream_conn *sc; |
| 1298 |
STREAM_CONN_CHECK(xptr, sc); |
| 1299 |
|
| 1300 |
if (sc->headers_sent) |
| 1301 |
Rf_error("cannot set header after headers have been sent"); |
| 1302 |
|
| 1303 |
const char *hdr_name = CHAR(STRING_ELT(name, 0)); |
| 1304 |
const char *hdr_value = CHAR(STRING_ELT(value, 0)); |
| 1305 |
|
| 1306 |
if (sc->resp_header_count >= sc->resp_header_capacity) { |
| 1307 |
int new_cap = sc->resp_header_capacity == 0 ? 8 : sc->resp_header_capacity * 2; |
| 1308 |
char **new_names = realloc(sc->resp_header_names, new_cap * sizeof(char *)); |
| 1309 |
char **new_values = realloc(sc->resp_header_values, new_cap * sizeof(char *)); |
| 1310 |
if (new_names == NULL || new_values == NULL) { |
| 1311 |
free(new_names); |
| 1312 |
free(new_values); |
| 1313 |
Rf_error("memory allocation failed"); |
| 1314 |
} |
| 1315 |
sc->resp_header_names = new_names; |
| 1316 |
sc->resp_header_values = new_values; |
| 1317 |
sc->resp_header_capacity = new_cap; |
| 1318 |
} |
| 1319 |
|
| 1320 |
sc->resp_header_names[sc->resp_header_count] = strdup(hdr_name); |
| 1321 |
sc->resp_header_values[sc->resp_header_count] = strdup(hdr_value); |
| 1322 |
if (sc->resp_header_names[sc->resp_header_count] == NULL || |
| 1323 |
sc->resp_header_values[sc->resp_header_count] == NULL) |
| 1324 |
Rf_error("memory allocation failed"); |
| 1325 |
|
| 1326 |
sc->resp_header_count++; |
| 1327 |
return nano_success; |
| 1328 |
|
| 1329 |
} |
| 1330 |
|
| 1331 |
SEXP rnng_conn_close(SEXP xptr) { |
| 1332 |
|
| 1333 |
if (NANO_PTR_CHECK(xptr, nano_ConnSymbol)) |
| 1334 |
return mk_error(NNG_ECLOSED); |
| 1335 |
|
| 1336 |
nano_conn *conn = (nano_conn *) NANO_PTR(xptr); |
| 1337 |
|
| 1338 |
if (conn->type == NANO_CONN_HTTP_STREAM) { |
| 1339 |
nano_stream_conn *sc = (nano_stream_conn *) conn; |
| 1340 |
if (conn_is_open(conn) && sc->headers_sent) |
| 1341 |
stream_write_terminator(sc); |
| 1342 |
} |
| 1343 |
|
| 1344 |
conn_close(conn); |
| 1345 |
return nano_success; |
| 1346 |
|
| 1347 |
} |
| 1348 |
|
| 1349 |
SEXP rnng_http_server_stop(SEXP xptr) { |
| 1350 |
|
| 1351 |
if (NANO_PTR_CHECK(xptr, nano_HttpServerSymbol)) |
| 1352 |
Rf_error("`server` is not a valid HTTP Server"); |
| 1353 |
|
| 1354 |
nano_http_server *srv = (nano_http_server *) NANO_PTR(xptr); |
| 1355 |
|
| 1356 |
if (srv->state == SERVER_STOPPED) |
| 1357 |
return nano_success; |
| 1358 |
|
| 1359 |
if (srv->state == SERVER_STARTED) { |
| 1360 |
srv->state = SERVER_STOPPED; |
| 1361 |
http_server_stop(srv); |
| 1362 |
} |
| 1363 |
|
| 1364 |
Rf_setAttrib(xptr, nano_StateSymbol, Rf_mkString("stopped")); |
| 1365 |
return nano_success; |
| 1366 |
|
| 1367 |
} |