Lines Matching refs:rctx

92     OSSL_HTTP_REQ_CTX *rctx;
99 if ((rctx = OPENSSL_zalloc(sizeof(*rctx))) == NULL)
101 rctx->state = OHS_ERROR;
102 rctx->buf_size = buf_size > 0 ? buf_size : OSSL_HTTP_DEFAULT_MAX_LINE_LEN;
103 rctx->buf = OPENSSL_malloc(rctx->buf_size);
104 rctx->wbio = wbio;
105 rctx->rbio = rbio;
106 if (rctx->buf == NULL) {
107 OPENSSL_free(rctx);
110 rctx->max_resp_len = OSSL_HTTP_DEFAULT_MAX_RESP_LEN;
111 /* everything else is 0, e.g. rctx->len_to_send, or NULL, e.g. rctx->mem */
112 return rctx;
115 void OSSL_HTTP_REQ_CTX_free(OSSL_HTTP_REQ_CTX *rctx)
117 if (rctx == NULL)
124 if (rctx->free_wbio)
125 BIO_free_all(rctx->wbio);
126 /* do not free rctx->rbio */
127 BIO_free(rctx->mem);
128 BIO_free(rctx->req);
129 OPENSSL_free(rctx->buf);
130 OPENSSL_free(rctx->proxy);
131 OPENSSL_free(rctx->server);
132 OPENSSL_free(rctx->port);
133 OPENSSL_free(rctx->expected_ct);
134 OPENSSL_free(rctx);
137 BIO *OSSL_HTTP_REQ_CTX_get0_mem_bio(const OSSL_HTTP_REQ_CTX *rctx)
139 if (rctx == NULL) {
143 return rctx->mem;
146 size_t OSSL_HTTP_REQ_CTX_get_resp_len(const OSSL_HTTP_REQ_CTX *rctx)
148 if (rctx == NULL) {
152 return rctx->resp_len;
155 void OSSL_HTTP_REQ_CTX_set_max_response_length(OSSL_HTTP_REQ_CTX *rctx,
158 if (rctx == NULL) {
162 rctx->max_resp_len = len != 0 ? (size_t)len : OSSL_HTTP_DEFAULT_MAX_RESP_LEN;
166 * Create request line using |rctx| and |path| (or "/" in case |path| is NULL).
169 int OSSL_HTTP_REQ_CTX_set_request_line(OSSL_HTTP_REQ_CTX *rctx, int method_POST,
173 if (rctx == NULL) {
177 BIO_free(rctx->mem);
178 if ((rctx->mem = BIO_new(BIO_s_mem())) == NULL)
181 rctx->method_POST = method_POST != 0;
182 if (BIO_printf(rctx->mem, "%s ", rctx->method_POST ? "POST" : "GET") <= 0)
190 if (BIO_printf(rctx->mem, OSSL_HTTP_PREFIX"%s", server) <= 0)
192 if (port != NULL && BIO_printf(rctx->mem, ":%s", port) <= 0)
199 if (path[0] != '/' && BIO_printf(rctx->mem, "/") <= 0)
205 if (BIO_printf(rctx->mem, "%s "HTTP_1_0"\r\n", path) <= 0)
208 rctx->resp_len = 0;
209 rctx->state = OHS_ADD_HEADERS;
213 int OSSL_HTTP_REQ_CTX_add1_header(OSSL_HTTP_REQ_CTX *rctx,
216 if (rctx == NULL || name == NULL) {
220 if (rctx->mem == NULL) {
225 if (BIO_puts(rctx->mem, name) <= 0)
228 if (BIO_write(rctx->mem, ": ", 2) != 2)
230 if (BIO_puts(rctx->mem, value) <= 0)
233 return BIO_write(rctx->mem, "\r\n", 2) == 2;
236 int OSSL_HTTP_REQ_CTX_set_expected(OSSL_HTTP_REQ_CTX *rctx,
240 if (rctx == NULL) {
245 && rctx->state != OHS_ERROR && rctx->state != OHS_ADD_HEADERS) {
251 OPENSSL_free(rctx->expected_ct);
252 rctx->expected_ct = NULL;
254 && (rctx->expected_ct = OPENSSL_strdup(content_type)) == NULL)
257 rctx->expect_asn1 = asn1;
259 rctx->max_time = timeout > 0 ? time(NULL) + timeout : 0;
261 rctx->max_time = rctx->max_total_time;
262 rctx->keep_alive = keep_alive;
266 static int set1_content(OSSL_HTTP_REQ_CTX *rctx,
274 if (rctx == NULL || (req == NULL && content_type != NULL)) {
279 if (rctx->keep_alive != 0
280 && !OSSL_HTTP_REQ_CTX_add1_header(rctx, "Connection", "keep-alive"))
283 BIO_free(rctx->req);
284 rctx->req = NULL;
287 if (!rctx->method_POST) {
293 && BIO_printf(rctx->mem, "Content-Type: %s\r\n", content_type) <= 0)
321 && BIO_printf(rctx->mem, "Content-Length: %ld\r\n", req_len) < 0)
326 rctx->req = req;
330 int OSSL_HTTP_REQ_CTX_set1_req(OSSL_HTTP_REQ_CTX *rctx, const char *content_type,
338 res = res && set1_content(rctx, content_type, mem);
343 static int add1_headers(OSSL_HTTP_REQ_CTX *rctx,
354 if (!OSSL_HTTP_REQ_CTX_add1_header(rctx, hdr->name, hdr->value))
358 if (add_host && !OSSL_HTTP_REQ_CTX_add1_header(rctx, "Host", host))
371 OSSL_HTTP_REQ_CTX *rctx = OSSL_HTTP_REQ_CTX_new(wbio, rbio, buf_size);
373 if (rctx == NULL)
375 rctx->free_wbio = free_wbio;
376 rctx->upd_fn = bio_update_fn;
377 rctx->upd_arg = arg;
378 rctx->use_ssl = use_ssl;
380 && (rctx->proxy = OPENSSL_strdup(proxy)) == NULL)
383 && (rctx->server = OPENSSL_strdup(server)) == NULL)
386 && (rctx->port = OPENSSL_strdup(port)) == NULL)
388 rctx->max_total_time =
390 return rctx;
393 OSSL_HTTP_REQ_CTX_free(rctx);
481 static int check_set_resp_len(OSSL_HTTP_REQ_CTX *rctx, size_t len)
483 if (rctx->max_resp_len != 0 && len > rctx->max_resp_len)
485 "length=%zu, max=%zu", len, rctx->max_resp_len);
486 if (rctx->resp_len != 0 && rctx->resp_len != len)
489 len, rctx->resp_len);
490 rctx->resp_len = len;
510 * Try exchanging request and response via HTTP on (non-)blocking BIO in rctx.
513 int OSSL_HTTP_REQ_CTX_nbio(OSSL_HTTP_REQ_CTX *rctx)
521 if (rctx == NULL) {
525 if (rctx->mem == NULL || rctx->wbio == NULL || rctx->rbio == NULL) {
530 rctx->redirection_url = NULL;
532 buf = (char *)rctx->buf;
533 if ((rctx->state & OHS_NOREAD) == 0) {
534 if (rctx->expect_asn1) {
535 n = BIO_read(rctx->rbio, rctx->buf, rctx->buf_size);
538 n = BIO_gets(rctx->rbio, buf, rctx->buf_size);
541 n = BIO_get_line(rctx->rbio, buf, rctx->buf_size);
547 if (BIO_should_retry(rctx->rbio))
554 if (BIO_write(rctx->mem, rctx->buf, n) != n)
558 switch (rctx->state) {
561 if (BIO_write(rctx->mem, "\r\n", 2) != 2) {
562 rctx->state = OHS_ERROR;
565 rctx->state = OHS_WRITE_INIT;
569 rctx->len_to_send = BIO_get_mem_data(rctx->mem, &rctx->pos);
570 rctx->state = OHS_WRITE_HDR;
574 /* Copy some chunk of data from rctx->mem to rctx->wbio */
576 /* Copy some chunk of data from rctx->req to rctx->wbio */
578 if (rctx->len_to_send > 0) {
579 i = BIO_write(rctx->wbio, rctx->pos, rctx->len_to_send);
581 if (BIO_should_retry(rctx->wbio))
583 rctx->state = OHS_ERROR;
586 rctx->pos += i;
587 rctx->len_to_send -= i;
590 if (rctx->state == OHS_WRITE_HDR) {
591 (void)BIO_reset(rctx->mem);
592 rctx->state = OHS_WRITE_REQ;
594 if (rctx->req != NULL && !BIO_eof(rctx->req)) {
595 n = BIO_read(rctx->req, rctx->buf, rctx->buf_size);
597 if (BIO_should_retry(rctx->req))
602 rctx->pos = rctx->buf;
603 rctx->len_to_send = n;
606 rctx->state = OHS_FLUSH;
611 i = BIO_flush(rctx->wbio);
614 rctx->state = OHS_FIRSTLINE;
618 if (BIO_should_retry(rctx->wbio))
621 rctx->state = OHS_ERROR;
638 n = BIO_get_mem_data(rctx->mem, &p);
640 if (n >= rctx->buf_size) {
641 rctx->state = OHS_ERROR;
646 n = BIO_gets(rctx->mem, buf, rctx->buf_size);
649 if (BIO_should_retry(rctx->mem))
651 rctx->state = OHS_ERROR;
656 if (n == rctx->buf_size) {
658 rctx->state = OHS_ERROR;
663 if (rctx->state == OHS_FIRSTLINE) {
666 rctx->state = OHS_HEADERS;
670 if (!rctx->method_POST) { /* method is GET */
671 rctx->state = OHS_REDIRECT;
678 rctx->state = OHS_ERROR;
695 if (rctx->state == OHS_REDIRECT
697 rctx->redirection_url = value;
700 if (rctx->state == OHS_HEADERS && rctx->expected_ct != NULL
702 if (OPENSSL_strcasecmp(rctx->expected_ct, value) != 0) {
705 rctx->expected_ct, value);
725 if (!check_set_resp_len(rctx, resp_len))
731 for (p = rctx->buf; *p != '\0'; p++) {
738 if (rctx->keep_alive != 0 /* do not let server initiate keep_alive */
740 if (rctx->keep_alive == 2) {
741 rctx->keep_alive = 0;
745 rctx->keep_alive = 0;
748 if (rctx->state == OHS_ERROR)
751 if (rctx->expected_ct != NULL && !found_expected_ct) {
753 "expected=%s", rctx->expected_ct);
756 if (rctx->state == OHS_REDIRECT) {
762 if (!rctx->expect_asn1) {
763 rctx->state = OHS_STREAM;
767 rctx->state = OHS_ASN1_HEADER;
776 n = BIO_get_mem_data(rctx->mem, &p);
810 if (!check_set_resp_len(rctx, resp_len))
813 rctx->state = OHS_ASN1_CONTENT;
818 n = BIO_get_mem_data(rctx->mem, NULL);
819 if (n < 0 || (size_t)n < rctx->resp_len)
822 rctx->state = OHS_ASN1_DONE;
827 int OSSL_HTTP_REQ_CTX_nbio_d2i(OSSL_HTTP_REQ_CTX *rctx,
834 if ((rv = OSSL_HTTP_REQ_CTX_nbio(rctx)) != 1)
836 *pval = ASN1_item_d2i(NULL, &p, BIO_get_mem_data(rctx->mem, &p), it);
877 BIO *OSSL_HTTP_REQ_CTX_exchange(OSSL_HTTP_REQ_CTX *rctx)
881 if (rctx == NULL) {
887 rv = OSSL_HTTP_REQ_CTX_nbio(rctx);
891 /* will not actually wait if rctx->max_time == 0 */
892 if (BIO_wait(rctx->rbio, rctx->max_time, 100 /* milliseconds */) <= 0)
897 if (rctx->redirection_url == NULL) { /* an error occurred */
898 if (rctx->len_to_send > 0)
905 return rctx->state == OHS_STREAM ? rctx->rbio : rctx->mem;
908 int OSSL_HTTP_is_alive(const OSSL_HTTP_REQ_CTX *rctx)
910 return rctx != NULL && rctx->keep_alive != 0;
923 OSSL_HTTP_REQ_CTX *rctx = NULL;
990 rctx = http_req_ctx_new(bio == NULL, cbio, rbio != NULL ? rbio : cbio,
995 if (rctx != NULL)
1001 return rctx;
1004 int OSSL_HTTP_set1_request(OSSL_HTTP_REQ_CTX *rctx, const char *path,
1012 if (rctx == NULL) {
1016 use_http_proxy = rctx->proxy != NULL && !rctx->use_ssl;
1017 if (use_http_proxy && rctx->server == NULL) {
1021 rctx->max_resp_len = max_resp_len; /* allows for 0: indefinite */
1023 return OSSL_HTTP_REQ_CTX_set_request_line(rctx, req != NULL,
1024 use_http_proxy ? rctx->server
1025 : NULL, rctx->port, path)
1026 && add1_headers(rctx, headers, rctx->server)
1027 && OSSL_HTTP_REQ_CTX_set_expected(rctx, expected_content_type,
1029 && set1_content(rctx, content_type, req);
1033 * Exchange single HTTP request and response according to rctx.
1034 * If rctx->method_POST then use POST, else use GET and ignore content_type.
1037 BIO *OSSL_HTTP_exchange(OSSL_HTTP_REQ_CTX *rctx, char **redirection_url)
1041 if (rctx == NULL) {
1049 resp = OSSL_HTTP_REQ_CTX_exchange(rctx);
1051 if (rctx->redirection_url != NULL) {
1056 *redirection_url = OPENSSL_strdup(rctx->redirection_url);
1071 if (rctx->server != NULL) {
1073 rctx->use_ssl ? "s" : "", rctx->server,
1074 rctx->port != NULL ? ":" : "",
1075 rctx->port != NULL ? rctx->port : "");
1078 if (rctx->proxy != NULL)
1079 ERR_add_error_data(2, " proxy=", rctx->proxy);
1082 rctx->use_ssl ? " violating the protocol" :
1125 OSSL_HTTP_REQ_CTX *rctx = NULL;
1141 rctx = OSSL_HTTP_open(host, port, proxy, no_proxy,
1145 if (rctx != NULL) {
1146 if (!OSSL_HTTP_set1_request(rctx, path, headers,
1152 OSSL_HTTP_REQ_CTX_free(rctx);
1153 rctx = NULL;
1155 resp = OSSL_HTTP_exchange(rctx, &redirection_url);
1170 (void)OSSL_HTTP_close(rctx, 1);
1171 rctx = NULL;
1180 (void)OSSL_HTTP_close(rctx, 1);
1181 rctx = NULL;
1189 if (!OSSL_HTTP_close(rctx, resp != NULL)) {
1191 rctx = NULL;
1212 OSSL_HTTP_REQ_CTX *rctx = prctx == NULL ? NULL : *prctx;
1215 if (rctx == NULL) {
1216 rctx = OSSL_HTTP_open(server, port, proxy, no_proxy,
1221 if (rctx != NULL) {
1222 if (OSSL_HTTP_set1_request(rctx, path, headers, content_type, req,
1225 resp = OSSL_HTTP_exchange(rctx, NULL);
1226 if (resp == NULL || !OSSL_HTTP_is_alive(rctx)) {
1227 if (!OSSL_HTTP_close(rctx, resp != NULL)) {
1231 rctx = NULL;
1235 *prctx = rctx;
1239 int OSSL_HTTP_close(OSSL_HTTP_REQ_CTX *rctx, int ok)
1245 if (rctx != NULL && rctx->upd_fn != NULL) {
1246 wbio = (*rctx->upd_fn)(rctx->wbio, rctx->upd_arg,
1250 rctx->wbio = wbio;
1252 OSSL_HTTP_REQ_CTX_free(rctx);