http_client.c: make prefix checking more readable and more efficient
#include "internal/sockets.h"
#include "internal/cryptlib.h" /* for ossl_assert() */
+#define HAS_PREFIX(str, prefix) (strncmp(str, prefix, sizeof(prefix) - 1) == 0)
#define HTTP_PREFIX "HTTP/"
#define HTTP_VERSION_PATT "1." /* allow 1.x */
#define HTTP_PREFIX_VERSION HTTP_PREFIX""HTTP_VERSION_PATT
@@ -376,7 +377,7 @@ static int parse_http_line1(char *line, int *found_keep_alive)
int i, retcode;
char *code, *reason, *end;
- if (strncmp(line, HTTP_PREFIX_VERSION, strlen(HTTP_PREFIX_VERSION)) != 0)
+ if (!HAS_PREFIX(line, HTTP_PREFIX_VERSION))
goto err;
/* above HTTP 1.0, connection persistence is the default */
*found_keep_alive = line[strlen(HTTP_PREFIX_VERSION)] > '0';
@@ -1037,16 +1038,14 @@ BIO *OSSL_HTTP_exchange(OSSL_HTTP_REQ_CTX *rctx, char **redirection_url)
static int redirection_ok(int n_redir, const char *old_url, const char *new_url)
{
- size_t https_len = strlen(OSSL_HTTPS_NAME":");
-
if (n_redir >= HTTP_VERSION_MAX_REDIRECTIONS) {
ERR_raise(ERR_LIB_HTTP, HTTP_R_TOO_MANY_REDIRECTIONS);
return 0;
}
if (*new_url == '/') /* redirection to same server => same protocol */
return 1;
- if (strncmp(old_url, OSSL_HTTPS_NAME":", https_len) == 0 &&
- strncmp(new_url, OSSL_HTTPS_NAME":", https_len) != 0) {
+ if (HAS_PREFIX(old_url, OSSL_HTTPS_NAME":") &&
+ !HAS_PREFIX(new_url, OSSL_HTTPS_NAME":")) {
ERR_raise(ERR_LIB_HTTP, HTTP_R_REDIRECTION_FROM_HTTPS_TO_HTTP);
return 0;
}
@@ -1297,7 +1296,7 @@ int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
continue;
/* Check for HTTP/1.x */
- if (strncmp(mbuf, HTTP_PREFIX, strlen(HTTP_PREFIX)) != 0) {
+ if (!HAS_PREFIX(mbuf, HTTP_PREFIX) != 0) {
ERR_raise(ERR_LIB_HTTP, HTTP_R_HEADER_PARSE_ERROR);
BIO_printf(bio_err, "%s: HTTP CONNECT failed, non-HTTP response\n",
prog);
@@ -1305,7 +1304,7 @@ int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
goto end;
}
mbufp = mbuf + strlen(HTTP_PREFIX);
- if (strncmp(mbufp, HTTP_VERSION_PATT, strlen(HTTP_VERSION_PATT)) != 0) {
+ if (!HAS_PREFIX(mbufp, HTTP_VERSION_PATT) != 0) {
ERR_raise(ERR_LIB_HTTP, HTTP_R_RECEIVED_WRONG_HTTP_VERSION);
BIO_printf(bio_err,
"%s: HTTP CONNECT failed, bad HTTP version %.*s\n",
@@ -1315,7 +1314,7 @@ int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
mbufp += HTTP_VERSION_STR_LEN;
/* RFC 7231 4.3.6: any 2xx status code is valid */
- if (strncmp(mbufp, " 2", strlen(" 2")) != 0) {
+ if (!HAS_PREFIX(mbufp, " 2")) {
/* chop any trailing whitespace */
while (read_len > 0 && ossl_isspace(mbuf[read_len - 1]))
read_len--;
