[openssl-users] BIO_read hangs, how can I know if the server wants to send data?

Hanno Böck hanno at hboeck.de
Tue Apr 26 13:28:31 UTC 2016


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: hanno at hboeck.de
GPG: BBB51E42
-------------- next part --------------
A non-text attachment was scrubbed...
Name: http-test-multiple.c
Type: text/x-c++src
Size: 681 bytes
Desc: not available
URL: <http://mta.openssl.org/pipermail/openssl-users/attachments/20160426/a45b616b/attachment.c>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://mta.openssl.org/pipermail/openssl-users/attachments/20160426/a45b616b/attachment.sig>


More information about the openssl-users mailing list