Files
CodenameEngine/source/funkin/backend/utils/HttpUtil.hx
T
⍚~Nex 5eb0801c22 optimized EVEN MORE the cne credits menu
fully asynced and looks better :D
2024-03-24 21:00:12 +01:00

72 lines
1.3 KiB
Haxe

package funkin.backend.utils;
import haxe.Http;
class HttpUtil
{
public static var userAgent:String = "request";
public static function requestText(url:String)
{
var r = null;
var h = new Http(url);
h.setHeader("User-Agent", userAgent);
h.onStatus = function(s)
{
if (isRedirect(s))
r = requestText(h.responseHeaders.get("Location"));
};
h.onData = function(d)
{
if (r == null)
r = d;
}
h.onError = function(e)
{
throw e;
}
h.request(false);
return r;
}
public static function requestBytes(url:String)
{
var r = null;
var h = new Http(url);
h.setHeader("User-Agent", userAgent);
h.onStatus = function(s)
{
if (isRedirect(s))
r = requestBytes(h.responseHeaders.get("Location"));
};
h.onBytes = function(d)
{
if (r == null)
r = d;
}
h.onError = function(e)
{
throw e;
}
h.request(false);
return r;
}
private static function isRedirect(status:Int):Bool
{
switch (status)
{
// 301: Moved Permanently, 302: Found (Moved Temporarily), 307: Temporary Redirect, 308: Permanent Redirect - Nex
case 301 | 302 | 307 | 308:
Logs.traceColored([Logs.logText('[Connection Status] ', BLUE), Logs.logText('Redirected with status code: ', YELLOW), Logs.logText('$status', GREEN)], WARNING);
return true;
}
return false;
}
}