]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - libcurl.c
Adding developer_curl to print verbose curl output
[xonotic/darkplaces.git] / libcurl.c
index f71dfaafc6beef6ebd4673dca9ffbb7aa69681e1..c9f272e4478d4887a48e70e22f28988572d8cfe2 100644 (file)
--- a/libcurl.c
+++ b/libcurl.c
@@ -2,7 +2,7 @@
 #include "fs.h"
 #include "libcurl.h"
 #include "thread.h"
-
+#include "com_list.h"
 #include "image.h"
 #include "jpeg.h"
 #include "image_png.h"
@@ -15,6 +15,7 @@ static cvar_t sv_curl_maxspeed = {CF_SERVER | CF_ARCHIVE, "sv_curl_maxspeed","0"
 static cvar_t cl_curl_enabled = {CF_CLIENT | CF_ARCHIVE, "cl_curl_enabled","1", "whether client's download support is enabled"};
 static cvar_t cl_curl_useragent = {CF_CLIENT, "cl_curl_useragent","1", "send the User-Agent string (note: turning this off may break stuff)"};
 static cvar_t cl_curl_useragent_append = {CF_CLIENT, "cl_curl_useragent_append","", "a string to append to the User-Agent string (useful for name and version number of your mod)"};
+static cvar_t developer_curl = {CF_CLIENT | CF_SERVER, "developer_curl","0", "whether verbose curl output should be printed to stderr"};
 
 /*
 =================================================================
@@ -62,6 +63,7 @@ typedef enum
        CINIT(LOW_SPEED_TIME, LONG, 20),
        CINIT(RESUME_FROM, LONG, 21),
        CINIT(HTTPHEADER, OBJECTPOINT, 23),
+       CINIT(VERBOSE, LONG, 41),
        CINIT(POST, LONG, 47),         /* HTTP POST method */
        CINIT(FOLLOWLOCATION, LONG, 52),  /* use Location: Luke! */
        CINIT(POSTFIELDSIZE, LONG, 60),
@@ -209,7 +211,7 @@ typedef struct downloadinfo_s
        size_t bytes_received; // for buffer
        double bytes_received_curl; // for throttling
        double bytes_sent_curl; // for throttling
-       struct downloadinfo_s *next, *prev;
+       llist_t list;
        qbool forthismap;
        double maxspeed;
        curl_slist *slist; // http headers
@@ -225,7 +227,7 @@ typedef struct downloadinfo_s
        const char *extraheaders;
 }
 downloadinfo;
-static downloadinfo *downloads = NULL;
+LIST_HEAD(downloads);
 static int numdownloads = 0;
 
 static qbool noclear = false;
@@ -284,7 +286,7 @@ void Curl_Clear_forthismap(void)
        if(noclear)
                return;
        if (curl_mutex) Thread_LockMutex(curl_mutex);
-       for(di = downloads; di; di = di->next)
+       List_For_Each_Entry(di, &downloads, downloadinfo, list)
                di->forthismap = false;
        Curl_CommandWhenError(NULL);
        Curl_CommandWhenDone(NULL);
@@ -620,12 +622,7 @@ static void Curl_EndDownload(downloadinfo *di, CurlStatus status, CURLcode error
                        CLEAR_AND_RETRY();
        }
 
-       if(di->prev)
-               di->prev->next = di->next;
-       else
-               downloads = di->next;
-       if(di->next)
-               di->next->prev = di->prev;
+       List_Delete(&di->list);
 
        --numdownloads;
        if(di->forthismap)
@@ -687,7 +684,7 @@ static void CheckPendingDownloads(void)
        if(numdownloads < cl_curl_maxdownloads.integer)
        {
                downloadinfo *di;
-               for(di = downloads; di; di = di->next)
+               List_For_Each_Entry(di, &downloads, downloadinfo, list)
                {
                        if(!di->started)
                        {
@@ -739,6 +736,8 @@ static void CheckPendingDownloads(void)
                                }
                                else
                                        qcurl_easy_setopt(di->curle, CURLOPT_USERAGENT, "");
+                               if(developer_curl.integer) 
+                                       qcurl_easy_setopt(di->curle, CURLOPT_VERBOSE, (long) 1);
                                qcurl_easy_setopt(di->curle, CURLOPT_REFERER, di->referer);
                                qcurl_easy_setopt(di->curle, CURLOPT_RESUME_FROM, (long) di->startpos);
                                qcurl_easy_setopt(di->curle, CURLOPT_FOLLOWLOCATION, 1);
@@ -843,7 +842,7 @@ static downloadinfo *Curl_Find(const char *filename)
        downloadinfo *di;
        if(!curl_dll)
                return NULL;
-       for(di = downloads; di; di = di->next)
+       List_For_Each_Entry(di, &downloads, downloadinfo, list)
                if(!strcasecmp(di->filename, filename))
                        return di;
        return NULL;
@@ -851,19 +850,16 @@ static downloadinfo *Curl_Find(const char *filename)
 
 void Curl_Cancel_ToMemory(curl_callback_t callback, void *cbdata)
 {
-       downloadinfo *di;
+       downloadinfo *di, *ndi;
        if(!curl_dll)
                return;
-       for(di = downloads; di; )
+       List_For_Each_Entry_Safe(di, ndi, &downloads, downloadinfo, list)
        {
                if(di->callback == callback && di->callback_data == cbdata)
                {
                        di->callback = curl_quiet_callback; // do NOT call the callback
                        Curl_EndDownload(di, CURL_DOWNLOAD_ABORTED, CURLE_OK, NULL);
-                       di = downloads;
                }
-               else
-                       di = di->next;
        }
 }
 
@@ -1067,11 +1063,6 @@ static qbool Curl_Begin(const char *URL, const char *extraheaders, double maxspe
                di->bytes_received_curl = 0;
                di->bytes_sent_curl = 0;
                di->extraheaders = extraheaders;
-               di->next = downloads;
-               di->prev = NULL;
-               if(di->next)
-                       di->next->prev = di;
-
                di->buffer = buf;
                di->buffersize = bufsize;
                if(callback == NULL)
@@ -1098,8 +1089,11 @@ static qbool Curl_Begin(const char *URL, const char *extraheaders, double maxspe
                        di->postbufsize = 0;
                }
 
-               downloads = di;
-               if (curl_mutex) Thread_UnlockMutex(curl_mutex);
+               List_Add(&di->list, &downloads);
+
+               if (curl_mutex)
+                       Thread_UnlockMutex(curl_mutex);
+
                return true;
        }
 }
@@ -1142,7 +1136,7 @@ void Curl_Frame(void)
 
        Curl_CheckCommandWhenDone();
 
-       if(!downloads)
+       if(List_Is_Empty(&downloads))
        {
                if (curl_mutex) Thread_UnlockMutex(curl_mutex);
                return;
@@ -1164,7 +1158,7 @@ void Curl_Frame(void)
                }
                while(mc == CURLM_CALL_MULTI_PERFORM);
 
-               for(di = downloads; di; di = di->next)
+               List_For_Each_Entry(di, &downloads, downloadinfo, list)
                {
                        double b = 0;
                        if(di->curle)
@@ -1223,7 +1217,7 @@ void Curl_Frame(void)
        // use the slowest allowing download to derive the maxspeed... this CAN
        // be done better, but maybe later
        maxspeed = cl_curl_maxspeed.value;
-       for(di = downloads; di; di = di->next)
+       List_For_Each_Entry(di, &downloads, downloadinfo, list)
                if(di->maxspeed > 0)
                        if(di->maxspeed < maxspeed || maxspeed <= 0)
                                maxspeed = di->maxspeed;
@@ -1255,9 +1249,9 @@ void Curl_CancelAll(void)
 
        if (curl_mutex) Thread_LockMutex(curl_mutex);
 
-       while(downloads)
+       while(!List_Is_Empty(&downloads))
        {
-               Curl_EndDownload(downloads, CURL_DOWNLOAD_ABORTED, CURLE_OK, NULL);
+               Curl_EndDownload(List_First_Entry(&downloads, downloadinfo, list), CURL_DOWNLOAD_ABORTED, CURLE_OK, NULL);
                // INVARIANT: downloads will point to the next download after that!
        }
 
@@ -1276,7 +1270,7 @@ qbool Curl_Running(void)
        if(!curl_dll)
                return false;
 
-       return downloads != NULL;
+       return !List_Is_Empty(&downloads);
 }
 
 /*
@@ -1343,7 +1337,7 @@ static void Curl_Info_f(cmd_state_t *cmd)
        {
                if (curl_mutex) Thread_LockMutex(curl_mutex);
                Con_Print("Currently running downloads:\n");
-               for(di = downloads; di; di = di->next)
+               List_For_Each_Entry(di, &downloads, downloadinfo, list)
                {
                        double speed, percent;
                        Con_Printf("  %s -> %s ",  CleanURL(di->url, urlbuf, sizeof(urlbuf)), di->filename);
@@ -1494,7 +1488,7 @@ static void Curl_Curl_f(cmd_state_t *cmd)
                                                dpsnprintf(donecommand, sizeof(donecommand), "connect %s", cls.netcon->address);
                                                Curl_CommandWhenDone(donecommand);
                                                noclear = true;
-                                               CL_Disconnect(false, NULL);
+                                               CL_Disconnect();
                                                noclear = false;
                                                Curl_CheckCommandWhenDone();
                                        }
@@ -1552,6 +1546,7 @@ void Curl_Init_Commands(void)
        Cvar_RegisterVariable (&sv_curl_maxspeed);
        Cvar_RegisterVariable (&cl_curl_useragent);
        Cvar_RegisterVariable (&cl_curl_useragent_append);
+       Cvar_RegisterVariable (&developer_curl);
        Cmd_AddCommand(CF_CLIENT | CF_CLIENT_FROM_SERVER, "curl", Curl_Curl_f, "download data from an URL and add to search path");
        //Cmd_AddCommand(cmd_local, "curlcat", Curl_CurlCat_f, "display data from an URL (debugging command)");
 }
@@ -1584,12 +1579,12 @@ Curl_downloadinfo_t *Curl_GetDownloadInfo(int *nDownloads, const char **addition
        if (curl_mutex) Thread_LockMutex(curl_mutex);
 
        i = 0;
-       for(di = downloads; di; di = di->next)
+       List_For_Each_Entry(di, &downloads, downloadinfo, list)
                ++i;
 
        downinfo = (Curl_downloadinfo_t *) Z_Malloc(sizeof(*downinfo) * i);
        i = 0;
-       for(di = downloads; di; di = di->next)
+       List_For_Each_Entry(di, &downloads, downloadinfo, list)
        {
                // do not show infobars for background downloads
                if(developer.integer <= 0)