Skip to content

Commit 923c751

Browse files
committed
Merge pull request #91261 from GrammAcc/fix-90160
[HTTPRequest] Add Missing Redirect Status Codes
2 parents 637ae4c + 90ea46f commit 923c751

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

scene/main/http_request.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ Error HTTPRequest::_get_redirect_headers(Vector<String> *r_headers) {
231231
return OK;
232232
}
233233

234+
bool HTTPRequest::_is_automatic_redirect() const {
235+
if (unlikely(method == HTTPClient::METHOD_CONNECT)) {
236+
// Never automatically redirect CONNECT requests.
237+
return false;
238+
} else if (response_code == 301 || response_code == 302 || response_code == 303 || response_code == 305) {
239+
// We change unsafe methods to GET for 301, 302, and 303, so these are always redirected.
240+
// 305 is deprecated and treated as equivalent to 302.
241+
return true;
242+
} else if ((response_code == 307 || response_code == 308) && _is_method_safe()) {
243+
// We only automatically redirect for safe methods on method-preserving status codes.
244+
return true;
245+
} else {
246+
return false;
247+
}
248+
}
249+
234250
bool HTTPRequest::_handle_response(bool *ret_value) {
235251
if (!client->has_response()) {
236252
_defer_done(RESULT_NO_RESPONSE, 0, PackedStringArray(), PackedByteArray());
@@ -251,8 +267,8 @@ bool HTTPRequest::_handle_response(bool *ret_value) {
251267
response_headers.push_back(E);
252268
}
253269

254-
if (response_code == 301 || response_code == 302) {
255-
// Handle redirect.
270+
if (_is_automatic_redirect()) {
271+
// Follow redirect.
256272

257273
if (max_redirects >= 0 && redirections >= max_redirects) {
258274
_defer_done(RESULT_REDIRECT_LIMIT_REACHED, response_code, response_headers, PackedByteArray());

scene/main/http_request.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class HTTPRequest : public Node {
105105
bool _is_content_header(const String &p_header) const;
106106
bool _is_method_safe() const;
107107
Error _get_redirect_headers(Vector<String> *r_headers);
108+
bool _is_automatic_redirect() const;
108109

109110
bool _handle_response(bool *ret_value);
110111

0 commit comments

Comments
 (0)