MiKTeX 26.5-next
A scalable TeX distribution
Loading...
Searching...
No Matches
CurlWebFile.h
1/* CurlWebFile.h: -*- C++ -*-
2
3 Copyright (C) 2001-2021 Christian Schenk
4
5 This file is part of MiKTeX Package Manager.
6
7 MiKTeX Package Manager is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2, or
10 (at your option) any later version.
11
12 MiKTeX Package Manager is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with MiKTeX Package Manager; if not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22#if !defined(DB88FC1DC9B0497C965F3CFB916FA93A)
23#define DB88FC1DC9B0497C965F3CFB916FA93A
24
25#if defined(HAVE_LIBCURL)
26
27#include <string>
28#include <memory>
29#include <unordered_map>
30
31#include <curl/curl.h>
32
33#include <miktex/Core/Debug>
34#include <miktex/Core/Session>
35#include <miktex/Trace/TraceStream>
36
37#include "CurlWebSession.h"
38#include "WebFile.h"
39
40MPM_INTERNAL_BEGIN_NAMESPACE;
41
42#if defined(_MSC_VER)
43# pragma push_macro("min")
44# undef min
45#endif
46
47class CircularBuffer
48{
49public:
50 CircularBuffer()
51 {
52 capacity = 10 * CURL_MAX_WRITE_SIZE;
53 buffer = new unsigned char[capacity];
54 }
55
56public:
57 ~CircularBuffer()
58 {
59 delete[] buffer;
60 }
61
62public:
63 void Clear()
64 {
65 size = 0;
66 head = 0;
67 tail = 0;
68 }
69
70public:
71 void Write(const void* data, std::size_t count)
72 {
73 if (!CanWrite(count))
74 {
75 MIKTEX_UNEXPECTED();
76 }
77 std::size_t num1 = std::min(count, capacity - tail);
78 std::size_t num2 = count - num1;
79 memcpy(buffer + tail, data, num1);
80 memcpy(buffer, reinterpret_cast<const unsigned char*>(data) + num1, num2);
81 tail = (tail + count) % capacity;
82 size += count;
83 }
84
85public:
86 void Read(void* data, std::size_t count)
87 {
88 if (!CanRead(count))
89 {
90 MIKTEX_UNEXPECTED();
91 }
92 std::size_t num1 = std::min(count, capacity - head);
93 std::size_t num2 = count - num1;
94 memcpy(data, buffer + head, num1);
95 memcpy(reinterpret_cast<unsigned char*>(data) + num1, buffer, num2);
96 head = (head + count) % capacity;
97 size -= count;
98 }
99
100public:
101 bool CanWrite(std::size_t n) const
102 {
103 return size + n <= capacity;
104 }
105
106public:
107 std::size_t GetSize() const
108 {
109 return size;
110 }
111
112public:
113 void Reserve(std::size_t newCapacity)
114 {
115 MIKTEX_ASSERT(newCapacity >= capacity);
116 unsigned char* newBuffer = new unsigned char[newCapacity];
117 std::size_t oldSize = size;
118 Read(newBuffer, size);
119 delete[] buffer;
120 buffer = newBuffer;
121 capacity = newCapacity;
122 size = oldSize;
123 head = 0;
124 tail = size;
125 }
126
127public:
128 std::size_t GetCapacity() const
129 {
130 return capacity;
131 }
132
133private:
134 bool CanRead(std::size_t n) const
135 {
136 return size >= n;
137 }
138
139private:
140 std::size_t capacity = 0;
141 std::size_t size = 0;
142 std::size_t head = 0;
143 std::size_t tail = 0;
144 unsigned char* buffer = nullptr;
145};
146
147#if defined(_MSC_VER)
148# pragma pop_macro("min")
149#endif
150
151class CurlWebFile :
152 public WebFile
153{
154public:
155 CurlWebFile(std::shared_ptr<CurlWebSession> webSession, const std::string& url, const std::unordered_map<std::string, std::string>& formData);
156
157public:
158 ~CurlWebFile() override;
159
160public:
161 std::size_t Read(void* data, std::size_t n) override;
162
163public:
164 void Close() override;
165
166private:
167 static std::size_t WriteCallback(char* data, std::size_t elemSize, std::size_t numElements, void* pv);
168
169private:
170 void Initialize();
171
172private:
173 bool initialized = false;
174
175private:
176 std::shared_ptr<CurlWebSession> webSession;
177
178private:
179 std::string url;
180
181private:
182 std::string urlEncodedpostFields;
183
184private:
185 CircularBuffer buffer;
186
187private:
188 std::unique_ptr<MiKTeX::Trace::TraceStream> trace_mpm;
189};
190
191MPM_INTERNAL_END_NAMESPACE;
192
193#endif
194
195#endif
@ Write
Write access.
Definition File.h:77
@ Read
Read access.
Definition File.h:75