X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=qcsrc%2Fcommon%2Furllib.qc;h=8529ebd6dd52f38bab8fef47ecd3b6fc48b72070;hb=bb80a6aba067167c6ef8d5f3465f03bd34142fa2;hp=d23873e74c81eea11a1f877c43a9ce44252a1380;hpb=9d0acc2e4117dc6d1810a034c635a631cec501a8;p=xonotic%2Fxonotic-data.pk3dir.git diff --git a/qcsrc/common/urllib.qc b/qcsrc/common/urllib.qc index d23873e74..8529ebd6d 100644 --- a/qcsrc/common/urllib.qc +++ b/qcsrc/common/urllib.qc @@ -1,7 +1,21 @@ +#if defined(CSQC) + #include "../dpdefs/csprogsdefs.qh" + #include "constants.qh" + #include "util.qh" + #include "urllib.qh" +#elif defined(MENUQC) +#elif defined(SVQC) + #include "../dpdefs/progsdefs.qh" + #include "../dpdefs/dpextensions.qh" + #include "constants.qh" + #include "util.qh" + #include "urllib.qh" +#endif + // files .float url_fh; -#define URL_FH_CURL -1 -#define URL_FH_STDOUT -2 +const float URL_FH_CURL = -1; +const float URL_FH_STDOUT = -2; // URLs .string url_url; @@ -13,10 +27,14 @@ .url_ready_func url_ready; .entity url_ready_pass; +// for multi handles +.int url_attempt; +.int url_mode; + entity url_fromid[NUM_URL_ID]; -float autocvar__urllib_nextslot; +int autocvar__urllib_nextslot; -float url_URI_Get_Callback(float id, float status, string data) +float url_URI_Get_Callback(int id, float status, string data) { if(id < MIN_URL_ID) return 0; @@ -29,7 +47,7 @@ float url_URI_Get_Callback(float id, float status, string data) return 0; if(e.url_rbuf >= 0 || e.url_wbuf >= 0) { - print(sprintf("WARNING: handle %d (%s) has already received data?!?\n", id + NUM_URL_ID, e.url_url)); + printf("WARNING: handle %d (%s) has already received data?!?\n", id + NUM_URL_ID, e.url_url); return 0; } @@ -78,10 +96,10 @@ float url_URI_Get_Callback(float id, float status, string data) } } -void url_fopen(string url, float mode, url_ready_func rdy, entity pass) +void url_single_fopen(string url, int mode, url_ready_func rdy, entity pass) { entity e; - float i; + int i; if(strstrofs(url, "://", 0) >= 0) { switch(mode) @@ -93,13 +111,13 @@ void url_fopen(string url, float mode, url_ready_func rdy, entity pass) // create a writing end that does nothing yet e = spawn(); - e.classname = "url_fopen_file"; + e.classname = "url_single_fopen_file"; e.url_url = strzone(url); e.url_fh = URL_FH_CURL; e.url_wbuf = buf_create(); if(e.url_wbuf < 0) { - print("url_fopen: out of memory in buf_create\n"); + print("url_single_fopen: out of memory in buf_create\n"); rdy(e, pass, URL_READY_ERROR); strunzone(e.url_url); remove(e); @@ -107,6 +125,8 @@ void url_fopen(string url, float mode, url_ready_func rdy, entity pass) } e.url_wbufpos = 0; e.url_rbuf = -1; + e.url_ready = rdy; + e.url_ready_pass = pass; rdy(e, pass, URL_READY_CANWRITE); break; @@ -124,7 +144,7 @@ void url_fopen(string url, float mode, url_ready_func rdy, entity pass) break; if(i >= autocvar__urllib_nextslot) { - print("url_fopen: too many concurrent requests\n"); + print("url_single_fopen: too many concurrent requests\n"); rdy(world, pass, URL_READY_ERROR); return; } @@ -133,7 +153,7 @@ void url_fopen(string url, float mode, url_ready_func rdy, entity pass) // GET the data if(!crypto_uri_postbuf(url, i + MIN_URL_ID, string_null, string_null, -1, 0)) { - print("url_fopen: failure in crypto_uri_postbuf\n"); + print("url_single_fopen: failure in crypto_uri_postbuf\n"); rdy(world, pass, URL_READY_ERROR); return; } @@ -142,7 +162,7 @@ void url_fopen(string url, float mode, url_ready_func rdy, entity pass) // all). Wait for data to come from the // server, then call the callback e = spawn(); - e.classname = "url_fopen_file"; + e.classname = "url_single_fopen_file"; e.url_url = strzone(url); e.url_fh = URL_FH_CURL; e.url_rbuf = -1; @@ -153,7 +173,7 @@ void url_fopen(string url, float mode, url_ready_func rdy, entity pass) url_fromid[i] = e; // make sure this slot won't be reused quickly even on map change - cvar_set("_urllib_nextslot", ftos(mod(i + 1, NUM_URL_ID))); + cvar_set("_urllib_nextslot", ftos((i + 1) % NUM_URL_ID)); break; } } @@ -164,12 +184,14 @@ void url_fopen(string url, float mode, url_ready_func rdy, entity pass) case FILE_WRITE: case FILE_APPEND: e = spawn(); - e.classname = "url_fopen_stdout"; + e.classname = "url_single_fopen_stdout"; e.url_fh = URL_FH_STDOUT; + e.url_ready = rdy; + e.url_ready_pass = pass; rdy(e, pass, URL_READY_CANWRITE); break; case FILE_READ: - print("url_fopen: cannot open '-' for reading\n"); + print("url_single_fopen: cannot open '-' for reading\n"); rdy(world, pass, URL_READY_ERROR); break; } @@ -186,8 +208,10 @@ void url_fopen(string url, float mode, url_ready_func rdy, entity pass) else { e = spawn(); - e.classname = "url_fopen_file"; + e.classname = "url_single_fopen_file"; e.url_fh = fh; + e.url_ready = rdy; + e.url_ready_pass = pass; if(mode == FILE_READ) rdy(e, pass, URL_READY_CANREAD); else @@ -197,9 +221,9 @@ void url_fopen(string url, float mode, url_ready_func rdy, entity pass) } // close a file -void url_fclose(entity e, url_ready_func rdy, entity pass) +void url_fclose(entity e) { - float i; + int i; if(e.url_fh == URL_FH_CURL) { @@ -224,7 +248,7 @@ void url_fclose(entity e, url_ready_func rdy, entity pass) if(i >= autocvar__urllib_nextslot) { print("url_fclose: too many concurrent requests\n"); - rdy(e, pass, URL_READY_ERROR); + e.url_ready(e,e.url_ready_pass, URL_READY_ERROR); buf_del(e.url_wbuf); strunzone(e.url_url); remove(e); @@ -236,7 +260,7 @@ void url_fclose(entity e, url_ready_func rdy, entity pass) if(!crypto_uri_postbuf(e.url_url, i + MIN_URL_ID, "text/plain", "", e.url_wbuf, 0)) { print("url_fclose: failure in crypto_uri_postbuf\n"); - rdy(e, pass, URL_READY_ERROR); + e.url_ready(e, e.url_ready_pass, URL_READY_ERROR); buf_del(e.url_wbuf); strunzone(e.url_url); remove(e); @@ -248,18 +272,16 @@ void url_fclose(entity e, url_ready_func rdy, entity pass) // call the callback buf_del(e.url_wbuf); e.url_wbuf = -1; - e.url_ready = rdy; - e.url_ready_pass = pass; e.url_id = i; url_fromid[i] = e; // make sure this slot won't be reused quickly even on map change - cvar_set("_urllib_nextslot", ftos(mod(i + 1, NUM_URL_ID))); + cvar_set("_urllib_nextslot", ftos((i + 1) % NUM_URL_ID)); } else { // we have READ all data, just close - rdy(e, pass, URL_READY_CLOSED); + e.url_ready(e, e.url_ready_pass, URL_READY_CLOSED); buf_del(e.url_rbuf); strunzone(e.url_url); remove(e); @@ -267,14 +289,14 @@ void url_fclose(entity e, url_ready_func rdy, entity pass) } else if(e.url_fh == URL_FH_STDOUT) { - rdy(e, pass, URL_READY_CLOSED); // closing creates no reading handle + e.url_ready(e, e.url_ready_pass, URL_READY_CLOSED); // closing creates no reading handle remove(e); } else { // file fclose(e.url_fh); - rdy(e, pass, URL_READY_CLOSED); // closing creates no reading handle + e.url_ready(e, e.url_ready_pass, URL_READY_CLOSED); // closing creates no reading handle remove(e); } } @@ -326,3 +348,53 @@ void url_fputs(entity e, string s) fputs(e.url_fh, s); } } + +// multi URL object, tries URLs separated by space in sequence +void url_multi_ready(entity fh, entity me, float status) +{ + float n; + if(status == URL_READY_ERROR || status < 0) + { + if(status == -422) // Unprocessable Entity + { + print("uri_multi_ready: got HTTP error 422, data is in unusable format - not continuing\n"); + me.url_ready(fh, me.url_ready_pass, status); + strunzone(me.url_url); + remove(me); + return; + } + me.url_attempt += 1; + n = tokenize_console(me.url_url); + if(n <= me.url_attempt) + { + me.url_ready(fh, me.url_ready_pass, status); + strunzone(me.url_url); + remove(me); + return; + } + url_single_fopen(argv(me.url_attempt), me.url_mode, url_multi_ready, me); + return; + } + me.url_ready(fh, me.url_ready_pass, status); +} +void url_multi_fopen(string url, int mode, url_ready_func rdy, entity pass) +{ + float n; + n = tokenize_console(url); + if(n <= 0) + { + print("url_multi_fopen: need at least one URL\n"); + rdy(world, pass, URL_READY_ERROR); + return; + } + + entity me; + me = spawn(); + me.classname = "url_multi"; + me.url_url = strzone(url); + me.url_attempt = 0; + me.url_mode = mode; + me.url_ready = rdy; + me.url_ready_pass = pass; + url_single_fopen(argv(0), mode, url_multi_ready, me); +}