I'm having trouble using SSL_read_ex with non-blocking sockets, especially I'm having trouble when requesting an invalid resource from Google: https://www.google.com/<something invalid here>.
What happens is that if I try to do a GET / to google there is no problem and I can get the relative page, but if I try to request an invalid resource everything stops working.
DETAILED EXPLANATION:
I'm writing a wrapper for libssl that will be used within the interpreter I'm developing. Right now I'm developing a library to allow HTTP requests using my own language. The HTTP library needs to use the SSL wrapper to access https sites.
The wrapper library uses non-blocking sockets for data exchange and my current implementation of SSL is based on the following scheme:
- I call SSL_read_ex (if there is data return to user, otherwise...)
- If SSL_ERROR_WANT_READ I check if there are data that must be sent by reading from "out_bio", if present I send them and listen again
- I receive data on the socket, write it to "in_bio" and call SSL_read_ex again
The code I'm referring to is this: https://github.com/ArgonLang/arlib/blob/main/ssl/socket.cpp#L140
Now this scheme always works except when I request a non-existing page from google (why google? it's the only site I've found that causes this strange behavior but I believe there are others)
To rule out other problems I tried to check with wireshark that there were actually no further packets arriving on my socket (and thus rule out an "event loop" side problem). I have tried to make requests through wget and I have compared the packets sent by my wrapper with those sent by wget and I have not noticed any difference, wget manages without any problem to receive a 404 response from google. My wrapper instead gets stuck on SSL_ERROR_WANT_READ even though there are no more incoming packets.
SOLUTION:
Ok, after further analysis I finally managed to solve the problem, in particular I set the following flag: SSL_MODE_AUTO_RETRY and enabled SSL_set_read_ahead.