GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/download.h Lines: 1 2 50.0 %
Date: 2017-11-29 Branches: 0 0 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009-2010  The Mana Developers
4
 *  Copyright (C) 2011-2017  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, const int64_t adler32 = -1);
69
70
        void setWriteFunction(WriteFunction write);
71
72
        /**
73
         * Starts the download thread.
74
         * @returns true  if thread was created
75
         *          false if the thread could not be made or download wasn't
76
         *                properly setup
77
         */
78
        bool start();
79
80
        /**
81
         * Cancels the download. Returns immediately, the cancelled status will
82
         * be noted in the next avialable update call.
83
         */
84
        void cancel();
85
86
        void addMirror(const std::string &str)
87
1
        { mUrlQueue.push(str); }
88
89
        const char *getError() const A_WARN_UNUSED;
90
91
        void setIgnoreError(const bool n)
92
        { mIgnoreError = n; }
93
94
        static size_t writeFunction(void *ptr, size_t size,
95
                                    size_t nmemb, void *stream);
96
97
        static void prepareForm(curl_httppost **form,
98
                                const std::string &fileName);
99
100
        static unsigned long fadler32(FILE *const file) A_WARN_UNUSED;
101
102
        static void addProxy(CURL *const curl);
103
104
        static void secureCurl(CURL *const curl);
105
106
        static void addHeaders(CURL *const curl);
107
108
        static unsigned long adlerBuffer(const char *const buffer, int size);
109
110
        static std::string getUploadResponse()
111
        { return mUploadResponse; }
112
113
    private:
114
        static int downloadThread(void *ptr);
115
        static int downloadProgress(void *clientp, double dltotal,
116
                                    double dlnow, double ultotal,
117
                                    double ulnow);
118
        static std::string mUploadResponse;
119
        void *mPtr;
120
        std::string mUrl;
121
        struct
122
        {
123
            unsigned cancel : 1;
124
            unsigned memoryWrite: 1;
125
            unsigned checkAdler: 1;
126
        } mOptions;
127
        std::string mFileName;
128
        std::queue<std::string> mUrlQueue;
129
        WriteFunction mWriteFunction;
130
        unsigned long mAdler;
131
        DownloadUpdate mUpdateFunction;
132
        SDL_Thread *mThread;
133
        CURL *mCurl;
134
        curl_slist *mHeaders;
135
        curl_httppost *mFormPost;
136
        char *mError;
137
        bool mIgnoreError;
138
        bool mUpload;
139
        bool mIsXml;
140
};
141
142
}  // namespace Net
143
144
#endif  // NET_DOWNLOAD_H