]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/urllib.qc
entcs: cleanup
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / urllib.qc
index 9648b2080f5abb0cae734a96d3a220dcd93ae5c2..0a48ef6cd3b84b656d2cebd5d685b64d67f54d66 100644 (file)
@@ -24,16 +24,13 @@ int autocvar__urllib_nextslot;
 
 float url_URI_Get_Callback(int id, float status, string data)
 {
-       if(id < MIN_URL_ID)
-               return 0;
+       if (id < MIN_URL_ID) return 0;
        id -= MIN_URL_ID;
-       if(id >= NUM_URL_ID)
-               return 0;
+       if (id >= NUM_URL_ID) return 0;
        entity e;
        e = url_fromid[id];
-       if(!e)
-               return 0;
-       if(e.url_rbuf >= 0 || e.url_wbuf >= 0)
+       if (!e) return 0;
+       if (e.url_rbuf >= 0 || e.url_wbuf >= 0)
        {
                LOG_INFOF("WARNING: handle %d (%s) has already received data?!?\n", id + NUM_URL_ID, e.url_url);
                return 0;
@@ -43,16 +40,15 @@ float url_URI_Get_Callback(int id, float status, string data)
        url_fromid[id] = NULL;
 
        // if we get here, we MUST have both buffers cleared
-       if(e.url_rbuf != -1 || e.url_wbuf != -1 || e.url_fh != URL_FH_CURL)
-               error("url_URI_Get_Callback: not a request waiting for data");
+       if (e.url_rbuf != -1 || e.url_wbuf != -1 || e.url_fh != URL_FH_CURL) error("url_URI_Get_Callback: not a request waiting for data");
 
-       if(status == 0)
+       if (status == 0)
        {
                // WE GOT DATA!
                float n, i;
                n = tokenizebyseparator(data, "\n");
                e.url_rbuf = buf_create();
-               if(e.url_rbuf < 0)
+               if (e.url_rbuf < 0)
                {
                        LOG_INFO("url_URI_Get_Callback: out of memory in buf_create\n");
                        e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
@@ -61,7 +57,7 @@ float url_URI_Get_Callback(int id, float status, string data)
                        return 1;
                }
                e.url_rbufpos = 0;
-               if(e.url_rbuf < 0)
+               if (e.url_rbuf < 0)
                {
                        LOG_INFO("url_URI_Get_Callback: out of memory in buf_create\n");
                        e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
@@ -69,7 +65,7 @@ float url_URI_Get_Callback(int id, float status, string data)
                        remove(e);
                        return 1;
                }
-               for(i = 0; i < n; ++i)
+               for (i = 0; i < n; ++i)
                        bufstr_set(e.url_rbuf, i, argv(i));
                e.url_ready(e, e.url_ready_pass, URL_READY_CANREAD);
                return 1;
@@ -88,9 +84,9 @@ void url_single_fopen(string url, int mode, url_ready_func rdy, entity pass)
 {
        entity e;
        int i;
-       if(strstrofs(url, "://", 0) >= 0)
+       if (strstrofs(url, "://", 0) >= 0)
        {
-               switch(mode)
+               switch (mode)
                {
                        case FILE_WRITE:
                        case FILE_APPEND:
@@ -98,12 +94,12 @@ void url_single_fopen(string url, int mode, url_ready_func rdy, entity pass)
                                // attempts to close will result in a reading handle
 
                                // create a writing end that does nothing yet
-                               e = spawn();
-                               e.classname = "url_single_fopen_file";
+                               e = new(url_single_fopen_file);
+                               make_pure(e);
                                e.url_url = strzone(url);
                                e.url_fh = URL_FH_CURL;
                                e.url_wbuf = buf_create();
-                               if(e.url_wbuf < 0)
+                               if (e.url_wbuf < 0)
                                {
                                        LOG_INFO("url_single_fopen: out of memory in buf_create\n");
                                        rdy(e, pass, URL_READY_ERROR);
@@ -122,15 +118,13 @@ void url_single_fopen(string url, int mode, url_ready_func rdy, entity pass)
                                // read data only
 
                                // get slot for HTTP request
-                               for(i = autocvar__urllib_nextslot; i < NUM_URL_ID; ++i)
-                                       if(url_fromid[i] == NULL)
-                                               break;
-                               if(i >= NUM_URL_ID)
+                               for (i = autocvar__urllib_nextslot; i < NUM_URL_ID; ++i)
+                                       if (url_fromid[i] == NULL) break;
+                               if (i >= NUM_URL_ID)
                                {
-                                       for(i = 0; i < autocvar__urllib_nextslot; ++i)
-                                               if(url_fromid[i] == NULL)
-                                                       break;
-                                       if(i >= autocvar__urllib_nextslot)
+                                       for (i = 0; i < autocvar__urllib_nextslot; ++i)
+                                               if (url_fromid[i] == NULL) break;
+                                       if (i >= autocvar__urllib_nextslot)
                                        {
                                                LOG_INFO("url_single_fopen: too many concurrent requests\n");
                                                rdy(NULL, pass, URL_READY_ERROR);
@@ -139,7 +133,7 @@ void url_single_fopen(string url, int 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))
+                               if (!crypto_uri_postbuf(url, i + MIN_URL_ID, string_null, string_null, -1, 0))
                                {
                                        LOG_INFO("url_single_fopen: failure in crypto_uri_postbuf\n");
                                        rdy(NULL, pass, URL_READY_ERROR);
@@ -149,8 +143,8 @@ void url_single_fopen(string url, int mode, url_ready_func rdy, entity pass)
                                // Make a dummy handle object (no buffers at
                                // all). Wait for data to come from the
                                // server, then call the callback
-                               e = spawn();
-                               e.classname = "url_single_fopen_file";
+                               e = new(url_single_fopen_file);
+                               make_pure(e);
                                e.url_url = strzone(url);
                                e.url_fh = URL_FH_CURL;
                                e.url_rbuf = -1;
@@ -165,14 +159,14 @@ void url_single_fopen(string url, int mode, url_ready_func rdy, entity pass)
                                break;
                }
        }
-       else if(url == "-")
+       else if (url == "-")
        {
-               switch(mode)
+               switch (mode)
                {
                        case FILE_WRITE:
                        case FILE_APPEND:
-                               e = spawn();
-                               e.classname = "url_single_fopen_stdout";
+                               e = new(url_single_fopen_stdout);
+                               make_pure(e);
                                e.url_fh = URL_FH_STDOUT;
                                e.url_ready = rdy;
                                e.url_ready_pass = pass;
@@ -188,22 +182,20 @@ void url_single_fopen(string url, int mode, url_ready_func rdy, entity pass)
        {
                float fh;
                fh = fopen(url, mode);
-               if(fh < 0)
+               if (fh < 0)
                {
                        rdy(NULL, pass, URL_READY_ERROR);
                        return;
                }
                else
                {
-                       e = spawn();
-                       e.classname = "url_single_fopen_file";
+                       e = new(url_single_fopen_file);
+                       make_pure(e);
                        e.url_fh = fh;
                        e.url_ready = rdy;
                        e.url_ready_pass = pass;
-                       if(mode == FILE_READ)
-                               rdy(e, pass, URL_READY_CANREAD);
-                       else
-                               rdy(e, pass, URL_READY_CANWRITE);
+                       if (mode == FILE_READ) rdy(e, pass, URL_READY_CANREAD);
+                       else rdy(e, pass, URL_READY_CANWRITE);
                }
        }
 }
@@ -213,30 +205,28 @@ void url_fclose(entity e)
 {
        int i;
 
-       if(e.url_fh == URL_FH_CURL)
+       if (e.url_fh == URL_FH_CURL)
        {
-               if(e.url_rbuf == -1 || e.url_wbuf != -1) // not(post GET/POST request)
-               if(e.url_rbuf != -1 || e.url_wbuf == -1) // not(pre POST request)
-                       error("url_fclose: not closable in current state");
+               if (e.url_rbuf == -1 || e.url_wbuf != -1)     // not(post GET/POST request)
+                       if (e.url_rbuf != -1 || e.url_wbuf == -1) // not(pre POST request)
+                               error("url_fclose: not closable in current state");
 
                // closing an URL!
-               if(e.url_wbuf >= 0)
+               if (e.url_wbuf >= 0)
                {
                        // we are closing the write end (HTTP POST request)
 
                        // get slot for HTTP request
-                       for(i = autocvar__urllib_nextslot; i < NUM_URL_ID; ++i)
-                               if(url_fromid[i] == NULL)
-                                       break;
-                       if(i >= NUM_URL_ID)
+                       for (i = autocvar__urllib_nextslot; i < NUM_URL_ID; ++i)
+                               if (url_fromid[i] == NULL) break;
+                       if (i >= NUM_URL_ID)
                        {
-                               for(i = 0; i < autocvar__urllib_nextslot; ++i)
-                                       if(url_fromid[i] == NULL)
-                                               break;
-                               if(i >= autocvar__urllib_nextslot)
+                               for (i = 0; i < autocvar__urllib_nextslot; ++i)
+                                       if (url_fromid[i] == NULL) break;
+                               if (i >= autocvar__urllib_nextslot)
                                {
                                        LOG_INFO("url_fclose: too many concurrent requests\n");
-                                       e.url_ready(e,e.url_ready_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);
@@ -245,7 +235,7 @@ void url_fclose(entity e)
                        }
 
                        // POST the data
-                       if(!crypto_uri_postbuf(e.url_url, i + MIN_URL_ID, "text/plain", "", e.url_wbuf, 0))
+                       if (!crypto_uri_postbuf(e.url_url, i + MIN_URL_ID, "text/plain", "", e.url_wbuf, 0))
                        {
                                LOG_INFO("url_fclose: failure in crypto_uri_postbuf\n");
                                e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
@@ -275,16 +265,16 @@ void url_fclose(entity e)
                        remove(e);
                }
        }
-       else if(e.url_fh == URL_FH_STDOUT)
+       else if (e.url_fh == URL_FH_STDOUT)
        {
-               e.url_ready(e, e.url_ready_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);
-               e.url_ready(e, e.url_ready_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);
        }
 }
@@ -292,17 +282,16 @@ void url_fclose(entity e)
 // with \n (blame FRIK_FILE)
 string url_fgets(entity e)
 {
-       if(e.url_fh == URL_FH_CURL)
+       if (e.url_fh == URL_FH_CURL)
        {
-               if(e.url_rbuf == -1)
-                       error("url_fgets: not readable in current state");
+               if (e.url_rbuf == -1) error("url_fgets: not readable in current state");
                // curl
                string s;
                s = bufstr_get(e.url_rbuf, e.url_rbufpos);
                e.url_rbufpos += 1;
                return s;
        }
-       else if(e.url_fh == URL_FH_STDOUT)
+       else if (e.url_fh == URL_FH_STDOUT)
        {
                // stdout
                return string_null;
@@ -317,15 +306,14 @@ string url_fgets(entity e)
 // without \n (blame FRIK_FILE)
 void url_fputs(entity e, string s)
 {
-       if(e.url_fh == URL_FH_CURL)
+       if (e.url_fh == URL_FH_CURL)
        {
-               if(e.url_wbuf == -1)
-                       error("url_fputs: not writable in current state");
+               if (e.url_wbuf == -1) error("url_fputs: not writable in current state");
                // curl
                bufstr_set(e.url_wbuf, e.url_wbufpos, s);
                e.url_wbufpos += 1;
        }
-       else if(e.url_fh == URL_FH_STDOUT)
+       else if (e.url_fh == URL_FH_STDOUT)
        {
                // stdout
                LOG_INFO(s);
@@ -341,9 +329,9 @@ void url_fputs(entity e, string s)
 void url_multi_ready(entity fh, entity me, float status)
 {
        float n;
-       if(status == URL_READY_ERROR || status < 0)
+       if (status == URL_READY_ERROR || status < 0)
        {
-               if(status == -422) // Unprocessable Entity
+               if (status == -422)  // Unprocessable Entity
                {
                        LOG_INFO("uri_multi_ready: got HTTP error 422, data is in unusable format - not continuing\n");
                        me.url_ready(fh, me.url_ready_pass, status);
@@ -353,7 +341,7 @@ void url_multi_ready(entity fh, entity me, float status)
                }
                me.url_attempt += 1;
                n = tokenize_console(me.url_url);
-               if(n <= me.url_attempt)
+               if (n <= me.url_attempt)
                {
                        me.url_ready(fh, me.url_ready_pass, status);
                        strunzone(me.url_url);
@@ -369,16 +357,15 @@ void url_multi_fopen(string url, int mode, url_ready_func rdy, entity pass)
 {
        float n;
        n = tokenize_console(url);
-       if(n <= 0)
+       if (n <= 0)
        {
                LOG_INFO("url_multi_fopen: need at least one URL\n");
                rdy(NULL, pass, URL_READY_ERROR);
                return;
        }
 
-       entity me;
-       me = spawn();
-       me.classname = "url_multi";
+       entity me = new(url_multi);
+       make_pure(me);
        me.url_url = strzone(url);
        me.url_attempt = 0;
        me.url_mode = mode;