Skip to content

Commit cc704f5

Browse files
committed
Reduce code duplication in HTTP header checks
1 parent ce2abdd commit cc704f5

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

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

460-
s = t;
461-
while ((s = strstr(s, "user-agent:"))) {
462-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
463-
*(s-1) == '\t' || *(s-1) == ' ') {
464-
have_header |= HTTP_HEADER_USER_AGENT;
465-
break;
466-
}
467-
s++;
471+
if (check_has_header(t, "user-agent:")) {
472+
have_header |= HTTP_HEADER_USER_AGENT;
468473
}
469-
470-
s = t;
471-
while ((s = strstr(s, "host:"))) {
472-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
473-
*(s-1) == '\t' || *(s-1) == ' ') {
474-
have_header |= HTTP_HEADER_HOST;
475-
break;
476-
}
477-
s++;
474+
if (check_has_header(t, "host:")) {
475+
have_header |= HTTP_HEADER_HOST;
478476
}
479-
480-
s = t;
481-
while ((s = strstr(s, "from:"))) {
482-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
483-
*(s-1) == '\t' || *(s-1) == ' ') {
484-
have_header |= HTTP_HEADER_FROM;
485-
break;
486-
}
487-
s++;
477+
if (check_has_header(t, "from:")) {
478+
have_header |= HTTP_HEADER_FROM;
488479
}
489-
490-
s = t;
491-
while ((s = strstr(s, "authorization:"))) {
492-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
493-
*(s-1) == '\t' || *(s-1) == ' ') {
494-
have_header |= HTTP_HEADER_AUTH;
495-
break;
496-
}
497-
s++;
480+
if (check_has_header(t, "authorization:")) {
481+
have_header |= HTTP_HEADER_AUTH;
498482
}
499-
500-
s = t;
501-
while ((s = strstr(s, "content-length:"))) {
502-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
503-
*(s-1) == '\t' || *(s-1) == ' ') {
504-
have_header |= HTTP_HEADER_CONTENT_LENGTH;
505-
break;
506-
}
507-
s++;
483+
if (check_has_header(t, "content-length:")) {
484+
have_header |= HTTP_HEADER_CONTENT_LENGTH;
508485
}
509-
510-
s = t;
511-
while ((s = strstr(s, "content-type:"))) {
512-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
513-
*(s-1) == '\t' || *(s-1) == ' ') {
514-
have_header |= HTTP_HEADER_TYPE;
515-
break;
516-
}
517-
s++;
486+
if (check_has_header(t, "content-type:")) {
487+
have_header |= HTTP_HEADER_TYPE;
518488
}
519-
520-
s = t;
521-
while ((s = strstr(s, "connection:"))) {
522-
if (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
523-
*(s-1) == '\t' || *(s-1) == ' ') {
524-
have_header |= HTTP_HEADER_CONNECTION;
525-
break;
526-
}
527-
s++;
489+
if (check_has_header(t, "connection:")) {
490+
have_header |= HTTP_HEADER_CONNECTION;
528491
}
529492

530493
/* remove Proxy-Authorization header */

0 commit comments

Comments
 (0)