mirror of
https://gitlab.com/Theopse/fbi-i18n-zh.git
synced 2025-05-01 13:56:35 +08:00
Add support for HTTP relative redirects.
This commit is contained in:
parent
6a4a446df0
commit
e09bb54fab
@ -26,6 +26,35 @@ struct http_context_s {
|
|||||||
u32 bufferSize;
|
u32 bufferSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void http_resolve_redirect(char* oldUrl, const char* redirectTo, size_t size) {
|
||||||
|
if(size > 0) {
|
||||||
|
if(redirectTo[0] == '/') {
|
||||||
|
char* baseEnd = oldUrl;
|
||||||
|
|
||||||
|
// Find the third slash to find the end of the URL's base; e.g. https://www.example.com/
|
||||||
|
u32 slashCount = 0;
|
||||||
|
while(*baseEnd != '\0' && (baseEnd = strchr(baseEnd + 1, '/')) != NULL) {
|
||||||
|
slashCount++;
|
||||||
|
if(slashCount == 3) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there are less than 3 slashes, assume the base URL ends at the end of the string; e.g. https://www.example.com
|
||||||
|
if(slashCount != 3) {
|
||||||
|
baseEnd = oldUrl + strlen(oldUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t baseLen = baseEnd - oldUrl;
|
||||||
|
if(baseLen < size) {
|
||||||
|
string_copy(baseEnd, redirectTo, size - baseLen);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
string_copy(oldUrl, redirectTo, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Result http_open(http_context* context, const char* url, bool userAgent) {
|
Result http_open(http_context* context, const char* url, bool userAgent) {
|
||||||
return http_open_ranged(context, url, userAgent, 0, 0);
|
return http_open_ranged(context, url, userAgent, 0, 0);
|
||||||
}
|
}
|
||||||
@ -64,9 +93,12 @@ Result http_open_ranged(http_context* context, const char* url, bool userAgent,
|
|||||||
if(response == 301 || response == 302 || response == 303) {
|
if(response == 301 || response == 302 || response == 303) {
|
||||||
redirectCount++;
|
redirectCount++;
|
||||||
|
|
||||||
memset(currUrl, '\0', sizeof(currUrl));
|
char redirectTo[1024];
|
||||||
if(R_SUCCEEDED(res = httpcGetResponseHeader(&ctx->httpc, "Location", currUrl, sizeof(currUrl)))) {
|
memset(redirectTo, '\0', sizeof(redirectTo));
|
||||||
|
if(R_SUCCEEDED(res = httpcGetResponseHeader(&ctx->httpc, "Location", redirectTo, sizeof(redirectTo)))) {
|
||||||
httpcCloseContext(&ctx->httpc);
|
httpcCloseContext(&ctx->httpc);
|
||||||
|
|
||||||
|
http_resolve_redirect(currUrl, redirectTo, sizeof(currUrl));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
resolved = true;
|
resolved = true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user