Skip to content

Commit 3d9c023

Browse files
committed
Reduce code duplication in HTTP header checks
1 parent d0d6050 commit 3d9c023

File tree

1 file changed

+25
-62
lines changed

1 file changed

+25
-62
lines changed

ext/standard/http_fopen_wrapper.c

Lines changed: 25 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ static inline void strip_header(char *header_bag, char *lc_header_bag,
106106
}
107107
}
108108

109+
static zend_bool check_has_header(const char *headers, const char *header) {
110+
const char *s = headers;
111+
while ((s = strstr(s, header))) {
112+
if (s == headers || *(s-1) == '\r' || *(s-1) == '\n' || *(s-1) == '\t' || *(s-1) == ' ') {
113+
return 1;
114+
}
115+
s++;
116+
}
117+
return 0;
118+
}
119+
109120
static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
110121
const char *path, const char *mode, int options, zend_string **opened_path,
111122
php_stream_context *context, int redirect_max, int flags,
@@ -460,74 +471,26 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
460471
strip_header(user_headers, t, "content-type:");
461472
}
462473

463-
s = t;
464-
while ((s = strstr(s, "user-agent:"))) {
465-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
466-
*(s-1) == '\t' || *(s-1) == ' ') {
467-
have_header |= HTTP_HEADER_USER_AGENT;
468-
break;
469-
}
470-
s++;
474+
if (check_has_header(t, "user-agent:")) {
475+
have_header |= HTTP_HEADER_USER_AGENT;
471476
}
472-
473-
s = t;
474-
while ((s = strstr(s, "host:"))) {
475-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
476-
*(s-1) == '\t' || *(s-1) == ' ') {
477-
have_header |= HTTP_HEADER_HOST;
478-
break;
479-
}
480-
s++;
477+
if (check_has_header(t, "host:")) {
478+
have_header |= HTTP_HEADER_HOST;
481479
}
482-
483-
s = t;
484-
while ((s = strstr(s, "from:"))) {
485-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
486-
*(s-1) == '\t' || *(s-1) == ' ') {
487-
have_header |= HTTP_HEADER_FROM;
488-
break;
489-
}
490-
s++;
480+
if (check_has_header(t, "from:")) {
481+
have_header |= HTTP_HEADER_FROM;
491482
}
492-
493-
s = t;
494-
while ((s = strstr(s, "authorization:"))) {
495-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
496-
*(s-1) == '\t' || *(s-1) == ' ') {
497-
have_header |= HTTP_HEADER_AUTH;
498-
break;
499-
}
500-
s++;
483+
if (check_has_header(t, "authorization:")) {
484+
have_header |= HTTP_HEADER_AUTH;
501485
}
502-
503-
s = t;
504-
while ((s = strstr(s, "content-length:"))) {
505-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
506-
*(s-1) == '\t' || *(s-1) == ' ') {
507-
have_header |= HTTP_HEADER_CONTENT_LENGTH;
508-
break;
509-
}
510-
s++;
486+
if (check_has_header(t, "content-length:")) {
487+
have_header |= HTTP_HEADER_CONTENT_LENGTH;
511488
}
512-
513-
s = t;
514-
while ((s = strstr(s, "content-type:"))) {
515-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
516-
*(s-1) == '\t' || *(s-1) == ' ') {
517-
have_header |= HTTP_HEADER_TYPE;
518-
break;
519-
}
520-
s++;
489+
if (check_has_header(t, "content-type:")) {
490+
have_header |= HTTP_HEADER_TYPE;
521491
}
522-
523-
s = t;
524-
while ((s = strstr(s, "connection:"))) {
525-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
526-
*(s-1) == '\t' || *(s-1) == ' ') {
527-
have_header |= HTTP_HEADER_CONNECTION;
528-
break;
529-
}
530-
s++;
492+
if (check_has_header(t, "connection:")) {
493+
have_header |= HTTP_HEADER_CONNECTION;
531494
}
532495

533496
/* remove Proxy-Authorization header */

0 commit comments

Comments
 (0)