1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

For #1657, fix the http read bug

This commit is contained in:
winlin 2020-11-06 19:53:37 +08:00
parent c3f23f4c23
commit 79655adea0
2 changed files with 35 additions and 29 deletions

View file

@ -681,40 +681,46 @@ srs_error_t SrsSslConnection::read(void* plaintext, size_t nn_plaintext, ssize_t
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
ssize_t nn = 0; while (true) {
int nn_padding = BIO_ctrl_pending(bio_in); ssize_t nn = 0;
if (nn_padding > 0) { int nn_padding = BIO_ctrl_pending(bio_in);
// Already exists in SSL cache, read it out. if (nn_padding > 0) {
nn = (ssize_t)srs_min(nn_plaintext, nn_padding); // Already exists in SSL cache, read it out.
} else { nn = (ssize_t)srs_min(nn_plaintext, nn_padding);
// TODO: Can we avoid copy? } else {
int nn_cipher = nn_plaintext; // TODO: Can we avoid copy?
char* cipher = new char[nn_cipher]; int nn_cipher = nn_plaintext;
SrsAutoFreeA(char, cipher); char* cipher = new char[nn_cipher];
SrsAutoFreeA(char, cipher);
// Read the cipher from SSL. // Read the cipher from SSL.
if ((err = transport->read(cipher, nn_cipher, &nn)) != srs_success) { if ((err = transport->read(cipher, nn_cipher, &nn)) != srs_success) {
return srs_error_wrap(err, "https: read"); return srs_error_wrap(err, "https: read");
}
int r0 = BIO_write(bio_in, cipher, nn);
if (r0 <= 0) {
// TODO: 0 or -1 maybe block, use BIO_should_retry to check.
return srs_error_new(ERROR_HTTPS_READ, "BIO_write r0=%d, cipher=%p, size=%d", r0, cipher, nn);
}
} }
int r0 = BIO_write(bio_in, cipher, nn); int r0 = SSL_read(ssl, plaintext, nn);
int r1 = SSL_get_error(ssl, r0); int r2 = BIO_ctrl_pending(bio_in);
if (r0 <= 0) { if (r0 <= 0) {
// TODO: 0 or -1 maybe block, use BIO_should_retry to check. // We try to read the cached data, but actually there is nothing, so try again.
return srs_error_new(ERROR_HTTPS_READ, "BIO_write r0=%d, cipher=%p, size=%d", r0, cipher, nn); if (nn_padding > 0 && r2 <= 0) {
continue;
}
return srs_error_new(ERROR_HTTPS_READ, "SSL_read r0=%d, r1=%d, r2=%d, padding=%d, size=%d",
r0, r1, r2, nn_padding, nn);
} }
}
int r0 = SSL_read(ssl, plaintext, nn); srs_assert(r0 <= nn_plaintext);
int r1 = SSL_get_error(ssl, r0); if (nread) {
int r2 = BIO_ctrl_pending(bio_in); *nread = r0;
if (r0 <= 0) { }
return srs_error_new(ERROR_HTTPS_READ, "SSL_read r0=%d, r1=%d, r2=%d, padding=%d, size=%d", break;
r0, r1, r2, nn_padding, nn);
}
srs_assert(r0 <= nn_plaintext);
if (nread) {
*nread = r0;
} }
return err; return err;

View file

@ -24,6 +24,6 @@
#ifndef SRS_CORE_VERSION4_HPP #ifndef SRS_CORE_VERSION4_HPP
#define SRS_CORE_VERSION4_HPP #define SRS_CORE_VERSION4_HPP
#define SRS_VERSION4_REVISION 49 #define SRS_VERSION4_REVISION 50
#endif #endif