5H FOR COMPILING. 5 FUCKING HOURS FOR MACRO ERRORS. Co-Authored-By: Ne_Eo <NeeEoo@users.noreply.github.com>
72 lines
1.2 KiB
Haxe
72 lines
1.2 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:
|
|
trace("Redirected with status code: " + status);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|