Hi,

I have a problem here using OpenSSL, maybe I have some fundamental
misunderstanding of how the api is supposed to be used.

What I want to do: Send a couple of HTTP requests over one connection
(with HTTP/1.1, keep-alive enabled).
Seems simple enough: I send a HTTP request and then read what the
server sends, then send the next.

However: How do I know when the server has stopped sending?
I have attached a code sample (it's missing lots of error checking in
the initialization phase, but that's just for simplification of the
code and shouldn't matter for now).

The relevant part is here:
        for (i = 0; i < 5; i++) {
                printf("calling BIO_write\n");
                r = BIO_write(bio, request, strlen(request));
                printf("%i bytes written\n", r);
                do {
                        printf("calling BIO_read\n");
                        r = BIO_read(bio, buf, 1024);
                        printf("%i bytes read\n", r);
                } while (r > 0);
        }

Now when I run this code it sends one write and reads a couple of
times. However when it's done BIO_read will block the program execution
and not return until a timeout.

So I need a way to know that there's nothing to read before calling
BIO_read. Searching the docs I thought SSL_pending() might be what I
need. However it always returns zero, no matter if the server has
something to send or not.

Another sidenote: I have set the timeout of the context to 2, but it
still hangs for much longer, so the timeout value doesn't seem to have
any effect.

I also tried a number of other things, including using SSL_read/write,
BIO_puts/gets (I didn't really find any good explanation when to use
which of the three), using a nonblocking bio (but that was totally
confusing) etc.

Any help apprechiated.

-- 
Hanno Böck
https://hboeck.de/

mail/jabber: ha...@hboeck.de
GPG: BBB51E42
#include <openssl/ssl.h>

int main()
{
	SSL_CTX *ctx;
	BIO *bio;
	SSL *ssl;
	char *buf[1024];
	int r, i;
	char *request = "GET / HTTP/1.1\r\nHost: x\r\n\r\n";

	SSL_library_init();
	SSL_load_error_strings();

	ctx = SSL_CTX_new(TLSv1_2_method());
	SSL_CTX_set_timeout(ctx, 2);

	bio = BIO_new_ssl_connect(ctx);
	BIO_set_conn_hostname(bio, "google.com:443");
	BIO_get_ssl(bio, &ssl);

	BIO_do_connect(bio);

	for (i = 0; i < 5; i++) {
		printf("calling BIO_write\n");
		r = BIO_write(bio, request, strlen(request));
		printf("%i bytes written\n", r);
		do {
			printf("calling BIO_read\n");
			r = BIO_read(bio, buf, 1024);
			printf("%i bytes read\n", r);
		} while (r > 0);
	}

}

Attachment: pgpavpcJww3Rb.pgp
Description: OpenPGP digital signature

-- 
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users

Reply via email to