Debian Codegraph @

src/ncurl.c

raw symbols SCIP
1 // nanonext - C level - ncurl --------------------------------------------------
2
3 #define NANONEXT_HTTP
4 #include "nanonext.h"
5
6 // internals -------------------------------------------------------------------
7
8 // Helper to build named list of all response headers
9 static SEXP build_all_headers(nng_http_res *res) {
10 int count = 0;
11 for (nano_http_header_s *h = nano_http_header_first(res); h != NULL;
12 h = nano_http_header_next(res, h))
13 count++;
14
15 if (count == 0)
16 return R_NilValue;
17
18 SEXP rvec, rnames;
19 PROTECT(rvec = Rf_allocVector(VECSXP, count));
20 rnames = Rf_allocVector(STRSXP, count);
21 Rf_namesgets(rvec, rnames);
22
23 int i = 0;
24 for (nano_http_header_s *h = nano_http_header_first(res); h != NULL;
25 h = nano_http_header_next(res, h)) {
26 SET_STRING_ELT(rnames, i, Rf_mkChar(h->name));
27 SET_VECTOR_ELT(rvec, i, Rf_mkString(h->value));
28 i++;
29 }
30
31 UNPROTECT(1);
32 return rvec;
33 }
34
35 static SEXP mk_error_haio(const int xc, SEXP env) {
36
37 SEXP err;
38 PROTECT(err = Rf_ScalarInteger(xc));
39 Rf_classgets(err, nano_error);
40 Rf_defineVar(nano_ResultSymbol, err, env);
41 Rf_defineVar(nano_ProtocolSymbol, err, env);
42 Rf_defineVar(nano_ValueSymbol, err, env);
43 Rf_defineVar(nano_AioSymbol, R_NilValue, env);
44 UNPROTECT(1);
45 return err;
46
47 }
48
49 static SEXP mk_error_ncurl(const int xc) {
50
51 const char *names[] = {"status", "headers", "data", ""};
52 SEXP out, err;
53 PROTECT(out = Rf_mkNamed(VECSXP, names));
54 PROTECT(err = Rf_ScalarInteger(xc));
55 Rf_classgets(err, nano_error);
56 SET_VECTOR_ELT(out, 0, err);
57 SET_VECTOR_ELT(out, 1, err);
58 SET_VECTOR_ELT(out, 2, err);
59 UNPROTECT(2);
60 return out;
61
62 }
63
64 static SEXP mk_error_ncurlaio(const int xc) {
65
66 SEXP env, err;
67 PROTECT(env = R_NewEnv(R_NilValue, 0, 0));
68 NANO_CLASS2(env, "ncurlAio", "recvAio");
69 PROTECT(err = Rf_ScalarInteger(xc));
70 Rf_classgets(err, nano_error);
71 Rf_defineVar(nano_ResultSymbol, err, env);
72 Rf_defineVar(nano_StatusSymbol, err, env);
73 Rf_defineVar(nano_ProtocolSymbol, err, env);
74 Rf_defineVar(nano_HeadersSymbol, err, env);
75 Rf_defineVar(nano_ValueSymbol, err, env);
76 Rf_defineVar(nano_DataSymbol, err, env);
77 UNPROTECT(2);
78 return env;
79
80 }
81
82 static nano_buf nano_char_buf(const SEXP data) {
83
84 nano_buf buf = {0};
85 switch (TYPEOF(data)) {
86 case STRSXP: {
87 const char *s = CHAR(STRING_ELT(data, 0));
88 NANO_INIT(&buf, (unsigned char *) s, strlen(s));
89 break;
90 }
91 case RAWSXP:
92 NANO_INIT(&buf, NANO_DATAPTR(data), XLENGTH(data));
93 break;
94 }
95
96 return buf;
97
98 }
99
100 // aio completion callbacks ----------------------------------------------------
101
102 static void haio_complete(void *arg) {
103
104 nano_aio *haio = (nano_aio *) arg;
105 const int res = nng_aio_result(haio->aio);
106 haio->result = res - !res;
107
108 if (haio->cb != NULL)
109 later2(haio_invoke_cb, haio->cb);
110
111 }
112
113 static void session_complete(void *arg) {
114
115 nano_aio *haio = (nano_aio *) arg;
116 const int res = nng_aio_result(haio->aio);
117 haio->result = res - !res;
118
119 }
120
121 // finalisers ------------------------------------------------------------------
122
123 static void haio_finalizer(SEXP xptr) {
124
125 if (NANO_PTR(xptr) == NULL) return;
126 nano_aio *xp = (nano_aio *) NANO_PTR(xptr);
127 nano_handle *handle = (nano_handle *) xp->next;
128 nng_aio_free(xp->aio);
129 if (handle->cfg != NULL)
130 nng_tls_config_free(handle->cfg);
131 nng_http_res_free(handle->res);
132 nng_http_req_free(handle->req);
133 nng_http_client_free(handle->cli);
134 nng_url_free(handle->url);
135 free(handle);
136 free(xp);
137
138 }
139
140 static void session_finalizer(SEXP xptr) {
141
142 if (NANO_PTR(xptr) == NULL) return;
143 nano_aio *xp = (nano_aio *) NANO_PTR(xptr);
144 nano_handle *handle = (nano_handle *) xp->next;
145 if (xp->data != NULL)
146 nng_http_conn_close((nng_http_conn *) xp->data);
147 nng_aio_free(xp->aio);
148 if (handle->cfg != NULL)
149 nng_tls_config_free(handle->cfg);
150 nng_http_res_free(handle->res);
151 nng_http_req_free(handle->req);
152 nng_http_client_free(handle->cli);
153 nng_url_free(handle->url);
154 free(handle);
155 free(xp);
156
157 }
158
159 // ncurl - internal ------------------------------------------------------------
160
161 static inline SEXP create_aio_http(SEXP env, nano_aio *haio, int typ) {
162
163 if (haio->result > 0)
164 return mk_error_haio(haio->result, env);
165
166 void *dat;
167 size_t sz;
168 SEXP out, vec, rvec, response;
169 nano_handle *handle = (nano_handle *) haio->next;
170
171 PROTECT(response = nano_findVarInFrame(env, nano_ResponseSymbol, NULL));
172 const int all_resp = response != R_NilValue && TYPEOF(response) == LGLSXP && LOGICAL(response)[0] == 1;
173 int chk_resp = response != R_NilValue && TYPEOF(response) == STRSXP;
174 const uint16_t code = nng_http_res_get_status(handle->res), relo = code >= 300 && code < 400;
175 Rf_defineVar(nano_ResultSymbol, Rf_ScalarInteger(code), env);
176
177 if (all_resp) {
178 rvec = build_all_headers(handle->res);
179 } else {
180 if (relo) {
181 if (chk_resp) {
182 const R_xlen_t rlen = XLENGTH(response);
183 PROTECT(response = Rf_xlengthgets(response, rlen + 1));
184 SET_STRING_ELT(response, rlen, Rf_mkChar("Location"));
185 } else {
186 PROTECT(response = Rf_mkString("Location"));
187 chk_resp = 1;
188 }
189 }
190
191 if (chk_resp) {
192 const R_xlen_t rlen = XLENGTH(response);
193 PROTECT(rvec = Rf_allocVector(VECSXP, rlen));
194 Rf_namesgets(rvec, response);
195 const SEXP *response_p = STRING_PTR_RO(response);
196 for (R_xlen_t i = 0; i < rlen; i++) {
197 const char *r = nng_http_res_get_header(handle->res, CHAR(response_p[i]));
198 SET_VECTOR_ELT(rvec, i, r == NULL ? R_NilValue : Rf_mkString(r));
199 }
200 UNPROTECT(1);
201 } else {
202 rvec = R_NilValue;
203 }
204 if (relo) UNPROTECT(1);
205 }
206 UNPROTECT(1);
207 Rf_defineVar(nano_ProtocolSymbol, rvec, env);
208
209 nng_http_res_get_data(handle->res, &dat, &sz);
210
211 if (haio->mode) {
212 vec = nano_raw_char(dat, sz);
213 } else {
214 vec = Rf_allocVector(RAWSXP, sz);
215 if (dat != NULL)
216 memcpy(NANO_DATAPTR(vec), dat, sz);
217 }
218 Rf_defineVar(nano_ValueSymbol, vec, env);
219
220 Rf_defineVar(nano_AioSymbol, R_NilValue, env);
221
222 switch (typ) {
223 case 0: out = nano_findVarInFrame(env, nano_ResultSymbol, NULL); break;
224 case 1: out = nano_findVarInFrame(env, nano_ProtocolSymbol, NULL); break;
225 default: out = nano_findVarInFrame(env, nano_ValueSymbol, NULL); break;
226 }
227 return out;
228
229 }
230
231 static inline SEXP nano_aio_http_impl(SEXP env, const int typ) {
232
233 int found;
234 SEXP exist;
235 switch (typ) {
236 case 0: exist = nano_findVarInFrame(env, nano_ResultSymbol, &found); break;
237 case 1: exist = nano_findVarInFrame(env, nano_ProtocolSymbol, &found); break;
238 default: exist = nano_findVarInFrame(env, nano_ValueSymbol, &found); break;
239 }
240 if (found)
241 return exist;
242
243 const SEXP aio = nano_findVarInFrame(env, nano_AioSymbol, NULL);
244 nano_aio *haio = (nano_aio *) NANO_PTR(aio);
245
246 if (nng_aio_busy(haio->aio))
247 return nano_unresolved;
248
249 return create_aio_http(env, haio, typ);
250
251 }
252
253 SEXP nano_aio_http_status(SEXP env) {
254
255 int found;
256 SEXP exist = nano_findVarInFrame(env, nano_ResultSymbol, &found);
257 if (found)
258 return exist;
259
260 const SEXP aio = nano_findVarInFrame(env, nano_AioSymbol, NULL);
261 nano_aio *haio = (nano_aio *) NANO_PTR(aio);
262
263 return create_aio_http(env, haio, 0);
264
265 }
266
267 // ncurl - minimalist http client ----------------------------------------------
268
269 SEXP rnng_ncurl(SEXP http, SEXP convert, SEXP follow, SEXP method, SEXP headers,
270 SEXP data, SEXP response, SEXP timeout, SEXP tls) {
271
272 const char *addr = CHAR(STRING_ELT(http, 0));
273 const char *mthd = method != R_NilValue ? CHAR(STRING_ELT(method, 0)) : NULL;
274 const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) nano_integer(timeout);
275 if (tls != R_NilValue && NANO_PTR_CHECK(tls, nano_TlsSymbol))
276 Rf_error("`tls` is not a valid TLS Configuration");
277 const int all_resp = response != R_NilValue && TYPEOF(response) == LGLSXP && LOGICAL(response)[0] == 1;
278 int chk_resp = response != R_NilValue && TYPEOF(response) == STRSXP;
279
280 nng_url *url = NULL;
281 nng_http_client *client = NULL;
282 nng_http_req *req = NULL;
283 nng_http_res *res = NULL;
284 nng_aio *aio = NULL;
285 nng_tls_config *cfg = NULL;
286 uint16_t code, relo;
287 int xc;
288
289 if ((xc = nng_url_parse(&url, addr)))
290 goto fail;
291
292 relocall:
293
294 if ((xc = nng_http_client_alloc(&client, url)) ||
295 (xc = nng_http_req_alloc(&req, url)) ||
296 (xc = nng_http_res_alloc(&res)) ||
297 (xc = nng_aio_alloc(&aio, NULL, NULL)))
298 goto fail;
299
300 if (mthd != NULL && (xc = nng_http_req_set_method(req, mthd)))
301 goto fail;
302
303 if (headers != R_NilValue && TYPEOF(headers) == STRSXP) {
304 const R_xlen_t hlen = XLENGTH(headers);
305 SEXP hnames = Rf_getAttrib(headers, R_NamesSymbol);
306 if (TYPEOF(hnames) == STRSXP && XLENGTH(hnames) == hlen) {
307 const SEXP *hnames_p = STRING_PTR_RO(hnames);
308 const SEXP *headers_p = STRING_PTR_RO(headers);
309 for (R_xlen_t i = 0; i < hlen; i++) {
310 if ((xc = nng_http_req_set_header(req, CHAR(hnames_p[i]), CHAR(headers_p[i]))))
311 goto fail;
312 }
313 }
314 }
315 if (data != R_NilValue) {
316 nano_buf enc = nano_char_buf(data);
317 if (enc.cur && (xc = nng_http_req_set_data(req, enc.buf, enc.cur)))
318 goto fail;
319 }
320
321 if (!strcmp(url->u_scheme, "https")) {
322
323 if (tls == R_NilValue) {
324 if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_CLIENT)) ||
325 (xc = nng_tls_config_server_name(cfg, url->u_hostname)) ||
326 (xc = nng_tls_config_auth_mode(cfg, NNG_TLS_AUTH_MODE_NONE)) ||
327 (xc = nng_http_client_set_tls(client, cfg)))
328 goto fail;
329
330 } else {
331 cfg = (nng_tls_config *) NANO_PTR(tls);
332 nng_tls_config_hold(cfg);
333
334 if ((xc = nng_tls_config_server_name(cfg, url->u_hostname)) ||
335 (xc = nng_http_client_set_tls(client, cfg)))
336 goto fail;
337 }
338
339 }
340
341 nng_aio_set_timeout(aio, dur);
342 nng_http_client_transact(client, req, res, aio);
343 nng_aio_wait(aio);
344 if ((xc = nng_aio_result(aio)))
345 goto fail;
346
347 if (cfg != NULL)
348 nng_tls_config_free(cfg);
349 nng_aio_free(aio);
350
351 code = nng_http_res_get_status(res), relo = code >= 300 && code < 400;
352
353 if (relo && NANO_INTEGER(follow)) {
354 const char *location = nng_http_res_get_header(res, "Location");
355 if (location == NULL) goto resume;
356 nng_url *oldurl = url;
357 xc = nng_url_parse(&url, location);
358 if (xc) goto resume;
359 nng_http_res_free(res);
360 res = NULL;
361 nng_http_req_free(req);
362 req = NULL;
363 nng_http_client_free(client);
364 client = NULL;
365 nng_url_free(oldurl);
366 cfg = NULL;
367 goto relocall;
368 }
369
370 resume: ;
371
372 SEXP out, vec, rvec;
373 void *dat;
374 size_t sz;
375 const char *names[] = {"status", "headers", "data", ""};
376
377 PROTECT(out = Rf_mkNamed(VECSXP, names));
378
379 SET_VECTOR_ELT(out, 0, Rf_ScalarInteger(code));
380
381 if (all_resp) {
382 rvec = build_all_headers(res);
383 SET_VECTOR_ELT(out, 1, rvec);
384 } else {
385 if (relo) {
386 if (chk_resp) {
387 const R_xlen_t rlen = XLENGTH(response);
388 PROTECT(response = Rf_xlengthgets(response, rlen + 1));
389 SET_STRING_ELT(response, rlen, Rf_mkChar("Location"));
390 } else {
391 PROTECT(response = Rf_mkString("Location"));
392 chk_resp = 1;
393 }
394 }
395
396 if (chk_resp) {
397 const R_xlen_t rlen = XLENGTH(response);
398 rvec = Rf_allocVector(VECSXP, rlen);
399 SET_VECTOR_ELT(out, 1, rvec);
400 Rf_namesgets(rvec, response);
401 const SEXP *response_p = STRING_PTR_RO(response);
402 for (R_xlen_t i = 0; i < rlen; i++) {
403 const char *r = nng_http_res_get_header(res, CHAR(response_p[i]));
404 SET_VECTOR_ELT(rvec, i, r == NULL ? R_NilValue : Rf_mkString(r));
405 }
406 } else {
407 rvec = R_NilValue;
408 SET_VECTOR_ELT(out, 1, rvec);
409 }
410 if (relo) UNPROTECT(1);
411 }
412
413 nng_http_res_get_data(res, &dat, &sz);
414
415 if (NANO_INTEGER(convert)) {
416 vec = nano_raw_char(dat, sz);
417 } else {
418 vec = Rf_allocVector(RAWSXP, sz);
419 if (dat != NULL)
420 memcpy(NANO_DATAPTR(vec), dat, sz);
421 }
422 SET_VECTOR_ELT(out, 2, vec);
423
424 nng_http_res_free(res);
425 nng_http_req_free(req);
426 nng_http_client_free(client);
427 nng_url_free(url);
428
429 UNPROTECT(1);
430 return out;
431
432 fail:
433 if (cfg != NULL)
434 nng_tls_config_free(cfg);
435 nng_aio_free(aio);
436 if (res != NULL)
437 nng_http_res_free(res);
438 if (req != NULL)
439 nng_http_req_free(req);
440 if (client != NULL)
441 nng_http_client_free(client);
442 nng_url_free(url);
443 return mk_error_ncurl(xc);
444
445 }
446
447 // ncurl aio -------------------------------------------------------------------
448
449 SEXP rnng_ncurl_aio(SEXP http, SEXP convert, SEXP method, SEXP headers, SEXP data,
450 SEXP response, SEXP timeout, SEXP tls, SEXP clo) {
451
452 const char *httr = CHAR(STRING_ELT(http, 0));
453 const char *mthd = method != R_NilValue ? CHAR(STRING_ELT(method, 0)) : NULL;
454 const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) nano_integer(timeout);
455 if (tls != R_NilValue && NANO_PTR_CHECK(tls, nano_TlsSymbol))
456 Rf_error("`tls` is not a valid TLS Configuration");
457
458 SEXP aio, env, fun;
459 int xc;
460 nano_aio *haio = NULL;
461 nano_handle *handle = NULL;
462 haio = calloc(1, sizeof(nano_aio));
463 NANO_ENSURE_ALLOC(haio);
464 handle = calloc(1, sizeof(nano_handle));
465 NANO_ENSURE_ALLOC(handle);
466
467 haio->type = HTTP_AIO;
468 haio->mode = (uint8_t) NANO_INTEGER(convert);
469 haio->next = handle;
470
471 if ((xc = nng_url_parse(&handle->url, httr)) ||
472 (xc = nng_http_client_alloc(&handle->cli, handle->url)) ||
473 (xc = nng_http_req_alloc(&handle->req, handle->url)) ||
474 (xc = nng_http_res_alloc(&handle->res)) ||
475 (xc = nng_aio_alloc(&haio->aio, haio_complete, haio)))
476 goto fail;
477
478 if (mthd != NULL && (xc = nng_http_req_set_method(handle->req, mthd)))
479 goto fail;
480
481 if (headers != R_NilValue && TYPEOF(headers) == STRSXP) {
482 const R_xlen_t hlen = XLENGTH(headers);
483 SEXP hnames = Rf_getAttrib(headers, R_NamesSymbol);
484 if (TYPEOF(hnames) == STRSXP && XLENGTH(hnames) == hlen) {
485 const SEXP *hnames_p = STRING_PTR_RO(hnames);
486 const SEXP *headers_p = STRING_PTR_RO(headers);
487 for (R_xlen_t i = 0; i < hlen; i++) {
488 if ((xc = nng_http_req_set_header(handle->req, CHAR(hnames_p[i]), CHAR(headers_p[i]))))
489 goto fail;
490 }
491 }
492 }
493 if (data != R_NilValue) {
494 nano_buf enc = nano_char_buf(data);
495 if (enc.cur && (xc = nng_http_req_copy_data(handle->req, enc.buf, enc.cur)))
496 goto fail;
497 }
498
499 if (!strcmp(handle->url->u_scheme, "https")) {
500
501 if (tls == R_NilValue) {
502 if ((xc = nng_tls_config_alloc(&handle->cfg, NNG_TLS_MODE_CLIENT)) ||
503 (xc = nng_tls_config_server_name(handle->cfg, handle->url->u_hostname)) ||
504 (xc = nng_tls_config_auth_mode(handle->cfg, NNG_TLS_AUTH_MODE_NONE)) ||
505 (xc = nng_http_client_set_tls(handle->cli, handle->cfg)))
506 goto fail;
507
508 } else {
509 handle->cfg = (nng_tls_config *) NANO_PTR(tls);
510 nng_tls_config_hold(handle->cfg);
511
512 if ((xc = nng_tls_config_server_name(handle->cfg, handle->url->u_hostname)) ||
513 (xc = nng_http_client_set_tls(handle->cli, handle->cfg)))
514 goto fail;
515 }
516
517 }
518
519 nng_aio_set_timeout(haio->aio, dur);
520 nng_http_client_transact(handle->cli, handle->req, handle->res, haio->aio);
521
522 PROTECT(aio = R_MakeExternalPtr(haio, nano_AioSymbol, R_NilValue));
523 R_RegisterCFinalizerEx(aio, haio_finalizer, TRUE);
524
525 PROTECT(env = R_NewEnv(R_NilValue, 0, 0));
526 NANO_CLASS2(env, "ncurlAio", "recvAio");
527 Rf_defineVar(nano_AioSymbol, aio, env);
528 Rf_defineVar(nano_ResponseSymbol, response, env);
529
530 int i = 0;
531 for (SEXP fnlist = nano_aioNFuncs; fnlist != R_NilValue; fnlist = CDR(fnlist)) {
532 PROTECT(fun = R_mkClosure(R_NilValue, CAR(fnlist), clo));
533 switch (++i) {
534 case 1: R_MakeActiveBinding(nano_StatusSymbol, fun, env);
535 case 2: R_MakeActiveBinding(nano_HeadersSymbol, fun, env);
536 case 3: R_MakeActiveBinding(nano_DataSymbol, fun, env);
537 }
538 UNPROTECT(1);
539 }
540
541 UNPROTECT(2);
542 return env;
543
544 fail:
545 if (handle->cfg != NULL)
546 nng_tls_config_free(handle->cfg);
547 nng_aio_free(haio->aio);
548 if (handle->res != NULL)
549 nng_http_res_free(handle->res);
550 if (handle->req != NULL)
551 nng_http_req_free(handle->req);
552 if (handle->cli != NULL)
553 nng_http_client_free(handle->cli);
554 nng_url_free(handle->url);
555 failmem:
556 free(handle);
557 free(haio);
558 return mk_error_ncurlaio(xc);
559
560 }
561
562 SEXP rnng_aio_http_status(SEXP env) {
563 return nano_aio_http_impl(env, 0);
564 }
565
566 SEXP rnng_aio_http_headers(SEXP env) {
567 return nano_aio_http_impl(env, 1);
568 }
569
570 SEXP rnng_aio_http_data(SEXP env) {
571 return nano_aio_http_impl(env, 2);
572 }
573
574 // ncurl session ---------------------------------------------------------------
575
576 SEXP rnng_ncurl_session(SEXP http, SEXP convert, SEXP method, SEXP headers, SEXP data,
577 SEXP response, SEXP timeout, SEXP tls) {
578
579 const char *httr = CHAR(STRING_ELT(http, 0));
580 const char *mthd = method != R_NilValue ? CHAR(STRING_ELT(method, 0)) : NULL;
581 const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) nano_integer(timeout);
582 if (tls != R_NilValue && NANO_PTR_CHECK(tls, nano_TlsSymbol))
583 Rf_error("`tls` is not a valid TLS Configuration");
584
585 SEXP sess;
586 int xc;
587 nano_aio *haio = NULL;
588 nano_handle *handle = NULL;
589 haio = calloc(1, sizeof(nano_aio));
590 NANO_ENSURE_ALLOC(haio);
591 handle = calloc(1, sizeof(nano_handle));
592 NANO_ENSURE_ALLOC(handle);
593
594 haio->type = HTTP_AIO;
595 haio->mode = (uint8_t) NANO_INTEGER(convert);
596 haio->next = handle;
597
598 if ((xc = nng_url_parse(&handle->url, httr)) ||
599 (xc = nng_http_client_alloc(&handle->cli, handle->url)) ||
600 (xc = nng_http_req_alloc(&handle->req, handle->url)) ||
601 (xc = nng_http_res_alloc(&handle->res)) ||
602 (xc = nng_aio_alloc(&haio->aio, session_complete, haio)))
603 goto fail;
604
605 if (mthd != NULL && (xc = nng_http_req_set_method(handle->req, mthd)))
606 goto fail;
607
608 if (headers != R_NilValue && TYPEOF(headers) == STRSXP) {
609 const R_xlen_t hlen = XLENGTH(headers);
610 SEXP hnames = Rf_getAttrib(headers, R_NamesSymbol);
611 if (TYPEOF(hnames) == STRSXP && XLENGTH(hnames) == hlen) {
612 const SEXP *hnames_p = STRING_PTR_RO(hnames);
613 const SEXP *headers_p = STRING_PTR_RO(headers);
614 for (R_xlen_t i = 0; i < hlen; i++) {
615 if ((xc = nng_http_req_set_header(handle->req, CHAR(hnames_p[i]), CHAR(headers_p[i]))))
616 goto fail;
617 }
618 }
619 }
620 if (data != R_NilValue) {
621 nano_buf enc = nano_char_buf(data);
622 if (enc.cur && (xc = nng_http_req_copy_data(handle->req, enc.buf, enc.cur)))
623 goto fail;
624 }
625
626 if (!strcmp(handle->url->u_scheme, "https")) {
627
628 if (tls == R_NilValue) {
629 if ((xc = nng_tls_config_alloc(&handle->cfg, NNG_TLS_MODE_CLIENT)) ||
630 (xc = nng_tls_config_server_name(handle->cfg, handle->url->u_hostname)) ||
631 (xc = nng_tls_config_auth_mode(handle->cfg, NNG_TLS_AUTH_MODE_NONE)) ||
632 (xc = nng_http_client_set_tls(handle->cli, handle->cfg)))
633 goto fail;
634
635 } else {
636
637 handle->cfg = (nng_tls_config *) NANO_PTR(tls);
638 nng_tls_config_hold(handle->cfg);
639
640 if ((xc = nng_tls_config_server_name(handle->cfg, handle->url->u_hostname)) ||
641 (xc = nng_http_client_set_tls(handle->cli, handle->cfg)))
642 goto fail;
643 }
644
645 }
646
647 nng_aio_set_timeout(haio->aio, dur);
648 nng_http_client_connect(handle->cli, haio->aio);
649 nng_aio_wait(haio->aio);
650 if ((xc = haio->result) > 0)
651 goto fail;
652
653 nng_http_conn *conn = nng_aio_get_output(haio->aio, 0);
654 haio->data = conn;
655
656 int store_resp = response != R_NilValue &&
657 (TYPEOF(response) == STRSXP || (TYPEOF(response) == LGLSXP && LOGICAL(response)[0] == 1));
658 PROTECT(sess = R_MakeExternalPtr(haio, nano_StatusSymbol, store_resp ? response : R_NilValue));
659 R_RegisterCFinalizerEx(sess, session_finalizer, TRUE);
660 Rf_classgets(sess, Rf_mkString("ncurlSession"));
661
662 UNPROTECT(1);
663 return sess;
664
665 fail:
666 if (handle->cfg != NULL)
667 nng_tls_config_free(handle->cfg);
668 nng_aio_free(haio->aio);
669 if (handle->res != NULL)
670 nng_http_res_free(handle->res);
671 if (handle->req != NULL)
672 nng_http_req_free(handle->req);
673 if (handle->cli != NULL)
674 nng_http_client_free(handle->cli);
675 nng_url_free(handle->url);
676 failmem:
677 free(handle);
678 free(haio);
679 ERROR_RET(xc);
680
681 }
682
683 SEXP rnng_ncurl_transact(SEXP session) {
684
685 if (NANO_PTR_CHECK(session, nano_StatusSymbol))
686 Rf_error("`session` is not a valid or active ncurlSession");
687
688 nano_aio *haio = (nano_aio *) NANO_PTR(session);
689
690 if (haio->data == NULL)
691 return mk_error_ncurl(7);
692
693 nng_http_conn *conn = (nng_http_conn *) haio->data;
694 nano_handle *handle = (nano_handle *) haio->next;
695 nng_http_conn_transact(conn, handle->req, handle->res, haio->aio);
696 nng_aio_wait(haio->aio);
697 if (haio->result > 0)
698 return mk_error_ncurl(haio->result);
699
700 SEXP out, vec, rvec, response;
701 void *dat;
702 size_t sz;
703 const char *names[] = {"status", "headers", "data", ""};
704
705 PROTECT(out = Rf_mkNamed(VECSXP, names));
706
707 const uint16_t code = nng_http_res_get_status(handle->res);
708 SET_VECTOR_ELT(out, 0, Rf_ScalarInteger(code));
709
710 response = NANO_PROT(session);
711 if (response != R_NilValue && TYPEOF(response) == LGLSXP && LOGICAL(response)[0] == 1) {
712 rvec = build_all_headers(handle->res);
713 SET_VECTOR_ELT(out, 1, rvec);
714 } else if (response != R_NilValue && TYPEOF(response) == STRSXP) {
715 const R_xlen_t rlen = XLENGTH(response);
716 rvec = Rf_allocVector(VECSXP, rlen);
717 SET_VECTOR_ELT(out, 1, rvec);
718 Rf_namesgets(rvec, response);
719 const SEXP *response_p = STRING_PTR_RO(response);
720 for (R_xlen_t i = 0; i < rlen; i++) {
721 const char *r = nng_http_res_get_header(handle->res, CHAR(response_p[i]));
722 SET_VECTOR_ELT(rvec, i, r == NULL ? R_NilValue : Rf_mkString(r));
723 }
724 } else {
725 rvec = R_NilValue;
726 SET_VECTOR_ELT(out, 1, rvec);
727 }
728
729 nng_http_res_get_data(handle->res, &dat, &sz);
730
731 if (haio->mode) {
732 vec = nano_raw_char(dat, sz);
733 } else {
734 vec = Rf_allocVector(RAWSXP, sz);
735 if (dat != NULL)
736 memcpy(NANO_DATAPTR(vec), dat, sz);
737 }
738 SET_VECTOR_ELT(out, 2, vec);
739
740 UNPROTECT(1);
741 return out;
742
743 }
744
745 SEXP rnng_ncurl_session_close(SEXP session) {
746
747 if (NANO_PTR_CHECK(session, nano_StatusSymbol))
748 Rf_error("`session` is not a valid or active ncurlSession");
749
750 nano_aio *haio = (nano_aio *) NANO_PTR(session);
751
752 if (haio->data == NULL)
753 ERROR_RET(7);
754
755 nng_http_conn_close((nng_http_conn *) haio->data);
756 haio->data = NULL;
757 Rf_setAttrib(session, nano_StateSymbol, Rf_mkString("closed"));
758
759 return nano_success;
760
761 }