Skip to content

Commit 8d451fd

Browse files
committed
Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3: Reduce code duplication in HTTP header checks
2 parents e855b28 + 3d9c023 commit 8d451fd

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
@@ -105,6 +105,17 @@ static inline void strip_header(char *header_bag, char *lc_header_bag,
105105
}
106106
}
107107

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

462-
s = t;
463-
while ((s = strstr(s, "user-agent:"))) {
464-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
465-
*(s-1) == '\t' || *(s-1) == ' ') {
466-
have_header |= HTTP_HEADER_USER_AGENT;
467-
break;
468-
}
469-
s++;
473+
if (check_has_header(t, "user-agent:")) {
474+
have_header |= HTTP_HEADER_USER_AGENT;
470475
}
471-
472-
s = t;
473-
while ((s = strstr(s, "host:"))) {
474-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
475-
*(s-1) == '\t' || *(s-1) == ' ') {
476-
have_header |= HTTP_HEADER_HOST;
477-
break;
478-
}
479-
s++;
476+
if (check_has_header(t, "host:")) {
477+
have_header |= HTTP_HEADER_HOST;
480478
}
481-
482-
s = t;
483-
while ((s = strstr(s, "from:"))) {
484-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
485-
*(s-1) == '\t' || *(s-1) == ' ') {
486-
have_header |= HTTP_HEADER_FROM;
487-
break;
488-
}
489-
s++;
479+
if (check_has_header(t, "from:")) {
480+
have_header |= HTTP_HEADER_FROM;
490481
}
491-
492-
s = t;
493-
while ((s = strstr(s, "authorization:"))) {
494-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
495-
*(s-1) == '\t' || *(s-1) == ' ') {
496-
have_header |= HTTP_HEADER_AUTH;
497-
break;
498-
}
499-
s++;
482+
if (check_has_header(t, "authorization:")) {
483+
have_header |= HTTP_HEADER_AUTH;
500484
}
501-
502-
s = t;
503-
while ((s = strstr(s, "content-length:"))) {
504-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
505-
*(s-1) == '\t' || *(s-1) == ' ') {
506-
have_header |= HTTP_HEADER_CONTENT_LENGTH;
507-
break;
508-
}
509-
s++;
485+
if (check_has_header(t, "content-length:")) {
486+
have_header |= HTTP_HEADER_CONTENT_LENGTH;
510487
}
511-
512-
s = t;
513-
while ((s = strstr(s, "content-type:"))) {
514-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
515-
*(s-1) == '\t' || *(s-1) == ' ') {
516-
have_header |= HTTP_HEADER_TYPE;
517-
break;
518-
}
519-
s++;
488+
if (check_has_header(t, "content-type:")) {
489+
have_header |= HTTP_HEADER_TYPE;
520490
}
521-
522-
s = t;
523-
while ((s = strstr(s, "connection:"))) {
524-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
525-
*(s-1) == '\t' || *(s-1) == ' ') {
526-
have_header |= HTTP_HEADER_CONNECTION;
527-
break;
528-
}
529-
s++;
491+
if (check_has_header(t, "connection:")) {
492+
have_header |= HTTP_HEADER_CONNECTION;
530493
}
531494

532495
/* remove Proxy-Authorization header */

0 commit comments

Comments
 (0)