Skip to content

Commit 05bc761

Browse files
committed
standard/http_fopen_wrapper: use zend_string API in php_stream_http_response_headers_parse()
1 parent 2502608 commit 05bc761

1 file changed

Lines changed: 19 additions & 22 deletions

File tree

ext/standard/http_fopen_wrapper.c

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,17 @@ static bool php_stream_http_response_header_trim(char *http_header_line,
187187
* last header line. */
188188
static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *wrapper,
189189
php_stream *stream, php_stream_context *context, int options,
190-
zend_string *last_header_line_str, char *header_line, size_t *header_line_length,
190+
zend_string *last_header_line, char *header_line, size_t *header_line_length,
191191
int response_code, zval *response_header,
192192
php_stream_http_response_header_info *header_info)
193193
{
194-
char *last_header_line = ZSTR_VAL(last_header_line_str);
195-
size_t last_header_line_length = ZSTR_LEN(last_header_line_str);
196-
char *last_header_line_end = ZSTR_VAL(last_header_line_str) + ZSTR_LEN(last_header_line_str) - 1;
197-
198194
/* Process non empty header line. */
199195
if (header_line && (*header_line != '\n' && *header_line != '\r')) {
200196
/* Removing trailing white spaces. */
201197
if (php_stream_http_response_header_trim(header_line, header_line_length) &&
202198
*header_line_length == 0) {
203199
/* Only spaces so treat as an empty folding header. */
204-
return last_header_line_str;
200+
return last_header_line;
205201
}
206202

207203
/* Process folding headers if starting with a space or a tab. */
@@ -218,27 +214,27 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w
218214
ZEND_ASSERT(http_folded_header_line_length > 0);
219215
/* Concatenate last header line, space and current header line. */
220216
zend_string *extended_header_str = zend_string_concat3(
221-
last_header_line, last_header_line_length,
217+
ZSTR_VAL(last_header_line), ZSTR_LEN(last_header_line),
222218
" ", 1,
223219
http_folded_header_line, http_folded_header_line_length);
224-
zend_string_efree(last_header_line_str);
225-
last_header_line_str = extended_header_str;
220+
zend_string_efree(last_header_line);
221+
last_header_line = extended_header_str;
226222
/* Return new header line. */
227-
return last_header_line_str;
223+
return last_header_line;
228224
}
229225
}
230226

231227
/* Find header separator position. */
232-
char *last_header_value = memchr(last_header_line, ':', last_header_line_length);
228+
char *last_header_value = memchr(ZSTR_VAL(last_header_line), ':', ZSTR_LEN(last_header_line));
233229
if (last_header_value) {
234230
/* Verify there is no space in header name */
235-
const char *last_header_name = last_header_line + 1;
231+
const char *last_header_name = ZSTR_VAL(last_header_line) + 1;
236232
while (last_header_name < last_header_value) {
237233
if (*last_header_name == ' ' || *last_header_name == '\t') {
238234
header_info->error = true;
239235
php_stream_wrapper_log_warn(wrapper, context, options, InvalidResponse,
240236
"HTTP invalid response format (space in header name)!");
241-
zend_string_efree(last_header_line_str);
237+
zend_string_efree(last_header_line);
242238
return NULL;
243239
}
244240
++last_header_name;
@@ -247,6 +243,7 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w
247243
last_header_value++; /* Skip ':'. */
248244

249245
/* Strip leading whitespace. */
246+
const char *last_header_line_end = ZSTR_VAL(last_header_line) + ZSTR_LEN(last_header_line) - 1;
250247
while (last_header_value < last_header_line_end
251248
&& (*last_header_value == ' ' || *last_header_value == '\t')) {
252249
last_header_value++;
@@ -256,14 +253,14 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w
256253
header_info->error = true;
257254
php_stream_wrapper_log_warn(wrapper, context, options, InvalidResponse,
258255
"HTTP invalid response format (no colon in header line)!");
259-
zend_string_efree(last_header_line_str);
256+
zend_string_efree(last_header_line);
260257
return NULL;
261258
}
262259

263260
bool store_header = true;
264261
zval *tmpzval = NULL;
265262

266-
if (!strncasecmp(last_header_line, "Location:", sizeof("Location:")-1)) {
263+
if (zend_string_starts_with_literal_ci(last_header_line, "Location:")) {
267264
/* Check if the location should be followed. */
268265
if (context && (tmpzval = php_stream_context_get_option(context, "http", "follow_location")) != NULL) {
269266
header_info->follow_location = zend_is_true(tmpzval);
@@ -281,7 +278,7 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w
281278
php_stream_wrapper_log_warn(wrapper, context, options, InvalidResponse,
282279
"HTTP Location header size is over the limit of %d bytes",
283280
HTTP_HEADER_MAX_LOCATION_SIZE);
284-
zend_string_efree(last_header_line_str);
281+
zend_string_efree(last_header_line);
285282
return NULL;
286283
}
287284
if (header_info->location_len == 0) {
@@ -291,9 +288,9 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w
291288
}
292289
header_info->location_len = last_header_value_len;
293290
memcpy(header_info->location, last_header_value, last_header_value_len + 1);
294-
} else if (!strncasecmp(last_header_line, "Content-Type:", sizeof("Content-Type:")-1)) {
291+
} else if (zend_string_starts_with_literal_ci(last_header_line, "Content-Type:")) {
295292
php_stream_notify_info(context, PHP_STREAM_NOTIFY_MIME_TYPE_IS, last_header_value, 0);
296-
} else if (!strncasecmp(last_header_line, "Content-Length:", sizeof("Content-Length:")-1)) {
293+
} else if (zend_string_starts_with_literal_ci(last_header_line, "Content-Length:")) {
297294
/* https://www.rfc-editor.org/rfc/rfc9110.html#name-content-length */
298295
const char *ptr = last_header_value;
299296
/* must contain only digits, no + or - symbols */
@@ -304,11 +301,11 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w
304301
if (endptr && !*endptr) {
305302
/* truncate for 32-bit such that no negative file sizes occur */
306303
header_info->file_size = MIN(parsed, ZEND_LONG_MAX);
307-
php_stream_notify_file_size(context, header_info->file_size, last_header_line, 0);
304+
php_stream_notify_file_size(context, header_info->file_size, ZSTR_VAL(last_header_line), 0);
308305
}
309306
}
310307
} else if (
311-
!strncasecmp(last_header_line, "Transfer-Encoding:", sizeof("Transfer-Encoding:")-1)
308+
zend_string_starts_with_literal_ci(last_header_line, "Transfer-Encoding:")
312309
&& !strncasecmp(last_header_value, "Chunked", sizeof("Chunked")-1)
313310
) {
314311
/* Create filter to decode response body. */
@@ -335,10 +332,10 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w
335332

336333
if (store_header) {
337334
zval http_header;
338-
ZVAL_NEW_STR(&http_header, last_header_line_str);
335+
ZVAL_NEW_STR(&http_header, last_header_line);
339336
zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_header);
340337
} else {
341-
zend_string_efree(last_header_line_str);
338+
zend_string_efree(last_header_line);
342339
}
343340

344341
return NULL;

0 commit comments

Comments
 (0)