GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/download.h Lines: 1 2 50.0 %
Date: 2018-06-18 21:15:20 Branches: 0 0 - %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009-2010  The Mana Developers
4
 *  Copyright (C) 2011-2018  The ManaPlus Developers
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#ifndef NET_DOWNLOAD_H
23
#define NET_DOWNLOAD_H
24
25
#include "enums/net/downloadstatus.h"
26
27
#include <curl/curl.h>
28
29
#include <string>
30
#include <queue>
31
32
#include "localconsts.h"
33
34
typedef int (*DownloadUpdate)(void *ptr,
35
                              const DownloadStatusT status,
36
                              size_t total,
37
                              const size_t remaining);
38
39
// Matches what CURL expects
40
typedef size_t (*WriteFunction)( void *ptr, size_t size, size_t nmemb,
41
                                 void *stream);
42
43
struct SDL_Thread;
44
45
namespace Net
46
{
47
class Download final
48
{
49
    public:
50
        Download(void *const ptr,
51
                 const std::string &url,
52
                 const DownloadUpdate updateFunction,
53
                 const bool ignoreError,
54
                 const bool isUpload,
55
                 const bool isXml);
56
57
        A_DELETE_COPY(Download)
58
59
        ~Download();
60
61
        void addHeader(const std::string &header);
62
63
        /**
64
         * Convience method for adding no-cache headers.
65
         */
66
        void noCache();
67
68
        void setFile(const std::string &filename,
69
                     const int64_t adler32);
70
71
        void setWriteFunction(WriteFunction write);
72
73
        /**
74
         * Starts the download thread.
75
         * @returns true  if thread was created
76
         *          false if the thread could not be made or download wasn't
77
         *                properly setup
78
         */
79
        bool start();
80
81
        /**
82
         * Cancels the download. Returns immediately, the cancelled status will
83
         * be noted in the next avialable update call.
84
         */
85
        void cancel();
86
87
        void addMirror(const std::string &str)
88
1
        { mUrlQueue.push(str); }
89
90
        const char *getError() const A_WARN_UNUSED;
91
92
        void setIgnoreError(const bool n)
93
        { mIgnoreError = n; }
94
95
        static size_t writeFunction(void *ptr, size_t size,
96
                                    size_t nmemb, void *stream);
97
98
        static void prepareForm(curl_httppost **form,
99
                                const std::string &fileName);
100
101
        static unsigned long fadler32(FILE *const file) A_WARN_UNUSED;
102
103
        static void addProxy(CURL *const curl);
104
105
        static void secureCurl(CURL *const curl);
106
107
        static void addHeaders(CURL *const curl);
108
109
        static void addCommonFlags(CURL *const curl);
110
111
        static unsigned long adlerBuffer(const char *const buffer, int size);
112
113
        static std::string getUploadResponse()
114
        { return mUploadResponse; }
115
116
    private:
117
        static int downloadThread(void *ptr);
118
        static int downloadProgress(void *clientp, double dltotal,
119
                                    double dlnow, double ultotal,
120
                                    double ulnow);
121
        static std::string mUploadResponse;
122
        void *mPtr;
123
        std::string mUrl;
124
        struct
125
        {
126
            unsigned cancel : 1;
127
            unsigned memoryWrite: 1;
128
            unsigned checkAdler: 1;
129
        } mOptions;
130
        std::string mFileName;
131
        std::queue<std::string> mUrlQueue;
132
        WriteFunction mWriteFunction;
133
        unsigned long mAdler;
134
        DownloadUpdate mUpdateFunction;
135
        SDL_Thread *mThread;
136
        CURL *mCurl;
137
        curl_slist *mHeaders;
138
        curl_httppost *mFormPost;
139
        char *mError;
140
        bool mIgnoreError;
141
        bool mUpload;
142
        bool mIsXml;
143
};
144
145
}  // namespace Net
146
147
#endif  // NET_DOWNLOAD_H