I made the following code to find the amount of bytes I need to read in order to reach the end of a given delimiter, by using SSL_peek.
int TcpClient::PeekEndOfDelimiter(const std::vector<unsigned char>& delimiter, int max_size) {
std::vector<unsigned char> peek_buf(max_size);
int total_bytes_peeked = 0;
while (total_bytes_peeked < max_size) {
int bytes_peeked = -1;
bytes_peeked = SSL_peek(ssl, peek_buf.data() + total_bytes_peeked, max_size - total_bytes_peeked);
if (bytes_peeked <= 0)
return -1;
total_bytes_peeked += bytes_peeked;
auto found = std::search(peek_buf.begin(), peek_buf.begin() + total_bytes_peeked,delimiter.begin(), delimiter.end());
if (found != peek_buf.begin() + total_bytes_peeked)
return found - peek_buf.begin() + delimiter.size();
}
return -1;
}
Now, I tried this code when reading an HTTP response, by first peeking the end of headers (rnrn) and it does fairly well on small HTTP responses, but when the response contains lots of headers, the function will not find the end of the delimiter. When debugging, I noticed that peek_buf contains duplicated data, thus, never reaching the end of headers no matter what max_size I pass to the function. Am I not understanding how SSL_peek really works? The entire response should be available to read, and even if it was not, I’m using a blocking context anyway so I really do not understand why this is happening. Thank you.