Skip to content

Commit 1cf98db

Browse files
authored
Deduplicate Proxy-Authorization code from php_stream_url_wrap_http_ex() (#15818)
Extracts this code to a separate function to reduce code duplication.
1 parent 520fce5 commit 1cf98db

File tree

1 file changed

+35
-41
lines changed

1 file changed

+35
-41
lines changed

ext/standard/http_fopen_wrapper.c

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,34 @@ static bool check_has_header(const char *headers, const char *header) {
115115
return 0;
116116
}
117117

118+
static zend_result php_stream_handle_proxy_authorization_header(const char *s, smart_str *header)
119+
{
120+
const char *p;
121+
122+
do {
123+
while (*s == ' ' || *s == '\t') s++;
124+
p = s;
125+
while (*p != 0 && *p != ':' && *p != '\r' && *p !='\n') p++;
126+
if (*p == ':') {
127+
p++;
128+
if (p - s == sizeof("Proxy-Authorization:") - 1 &&
129+
zend_binary_strcasecmp(s, sizeof("Proxy-Authorization:") - 1,
130+
"Proxy-Authorization:", sizeof("Proxy-Authorization:") - 1) == 0) {
131+
while (*p != 0 && *p != '\r' && *p !='\n') p++;
132+
smart_str_appendl(header, s, p - s);
133+
smart_str_appendl(header, "\r\n", sizeof("\r\n")-1);
134+
return SUCCESS;
135+
} else {
136+
while (*p != 0 && *p != '\r' && *p !='\n') p++;
137+
}
138+
}
139+
s = p;
140+
while (*s == '\r' || *s == '\n') s++;
141+
} while (*s != 0);
142+
143+
return FAILURE;
144+
}
145+
118146
static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
119147
const char *path, const char *mode, int options, zend_string **opened_path,
120148
php_stream_context *context, int redirect_max, int flags,
@@ -254,58 +282,24 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
254282

255283
/* check if we have Proxy-Authorization header */
256284
if (context && (tmpzval = php_stream_context_get_option(context, "http", "header")) != NULL) {
257-
char *s, *p;
285+
const char *s;
258286

259287
if (Z_TYPE_P(tmpzval) == IS_ARRAY) {
260288
zval *tmpheader = NULL;
261289

262290
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(tmpzval), tmpheader) {
263291
if (Z_TYPE_P(tmpheader) == IS_STRING) {
264292
s = Z_STRVAL_P(tmpheader);
265-
do {
266-
while (*s == ' ' || *s == '\t') s++;
267-
p = s;
268-
while (*p != 0 && *p != ':' && *p != '\r' && *p !='\n') p++;
269-
if (*p == ':') {
270-
p++;
271-
if (p - s == sizeof("Proxy-Authorization:") - 1 &&
272-
zend_binary_strcasecmp(s, sizeof("Proxy-Authorization:") - 1,
273-
"Proxy-Authorization:", sizeof("Proxy-Authorization:") - 1) == 0) {
274-
while (*p != 0 && *p != '\r' && *p !='\n') p++;
275-
smart_str_appendl(&header, s, p - s);
276-
smart_str_appendl(&header, "\r\n", sizeof("\r\n")-1);
277-
goto finish;
278-
} else {
279-
while (*p != 0 && *p != '\r' && *p !='\n') p++;
280-
}
281-
}
282-
s = p;
283-
while (*s == '\r' || *s == '\n') s++;
284-
} while (*s != 0);
293+
if (php_stream_handle_proxy_authorization_header(s, &header) == SUCCESS) {
294+
goto finish;
295+
}
285296
}
286297
} ZEND_HASH_FOREACH_END();
287298
} else if (Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval)) {
288299
s = Z_STRVAL_P(tmpzval);
289-
do {
290-
while (*s == ' ' || *s == '\t') s++;
291-
p = s;
292-
while (*p != 0 && *p != ':' && *p != '\r' && *p !='\n') p++;
293-
if (*p == ':') {
294-
p++;
295-
if (p - s == sizeof("Proxy-Authorization:") - 1 &&
296-
zend_binary_strcasecmp(s, sizeof("Proxy-Authorization:") - 1,
297-
"Proxy-Authorization:", sizeof("Proxy-Authorization:") - 1) == 0) {
298-
while (*p != 0 && *p != '\r' && *p !='\n') p++;
299-
smart_str_appendl(&header, s, p - s);
300-
smart_str_appendl(&header, "\r\n", sizeof("\r\n")-1);
301-
goto finish;
302-
} else {
303-
while (*p != 0 && *p != '\r' && *p !='\n') p++;
304-
}
305-
}
306-
s = p;
307-
while (*s == '\r' || *s == '\n') s++;
308-
} while (*s != 0);
300+
if (php_stream_handle_proxy_authorization_header(s, &header) == SUCCESS) {
301+
goto finish;
302+
}
309303
}
310304
}
311305
finish:

0 commit comments

Comments
 (0)