summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--externals/httplib/README.md2
-rw-r--r--externals/httplib/httplib.h2419
-rw-r--r--src/core/hle/service/bcat/backend/boxcat.cpp7
-rw-r--r--src/video_core/CMakeLists.txt5
-rw-r--r--src/video_core/engines/maxwell_3d.cpp81
-rw-r--r--src/video_core/engines/maxwell_3d.h42
-rw-r--r--src/video_core/memory_manager.cpp11
-rw-r--r--src/video_core/query_cache.h359
-rw-r--r--src/video_core/rasterizer_interface.h19
-rw-r--r--src/video_core/renderer_opengl/gl_query_cache.cpp120
-rw-r--r--src/video_core/renderer_opengl/gl_query_cache.h78
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.cpp78
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.h47
-rw-r--r--src/video_core/renderer_opengl/gl_resource_manager.cpp17
-rw-r--r--src/video_core/renderer_opengl/gl_resource_manager.h25
-rw-r--r--src/video_core/renderer_vulkan/vk_device.cpp10
-rw-r--r--src/video_core/renderer_vulkan/vk_query_cache.cpp122
-rw-r--r--src/video_core/renderer_vulkan/vk_query_cache.h104
-rw-r--r--src/video_core/renderer_vulkan/vk_rasterizer.cpp31
-rw-r--r--src/video_core/renderer_vulkan/vk_rasterizer.h11
-rw-r--r--src/video_core/renderer_vulkan/vk_scheduler.cpp8
-rw-r--r--src/video_core/renderer_vulkan/vk_scheduler.h15
-rw-r--r--src/video_core/renderer_vulkan/vk_shader_decompiler.cpp17
-rw-r--r--src/video_core/shader/decode/conversion.cpp14
-rw-r--r--src/video_core/shader/decode/texture.cpp71
-rw-r--r--src/video_core/texture_cache/texture_cache.h19
-rw-r--r--src/web_service/web_backend.cpp7
27 files changed, 2896 insertions, 843 deletions
diff --git a/externals/httplib/README.md b/externals/httplib/README.md
index 0e26522b5..73037d297 100644
--- a/externals/httplib/README.md
+++ b/externals/httplib/README.md
@@ -1,4 +1,4 @@
1From https://github.com/yhirose/cpp-httplib/commit/d9479bc0b12e8a1e8bce2d34da4feeef488581f3 1From https://github.com/yhirose/cpp-httplib/tree/fce8e6fefdab4ad48bc5b25c98e5ebfda4f3cf53
2 2
3MIT License 3MIT License
4 4
diff --git a/externals/httplib/httplib.h b/externals/httplib/httplib.h
index fa2edcc94..e03842e6d 100644
--- a/externals/httplib/httplib.h
+++ b/externals/httplib/httplib.h
@@ -1,7 +1,7 @@
1// 1//
2// httplib.h 2// httplib.h
3// 3//
4// Copyright (c) 2019 Yuji Hirose. All rights reserved. 4// Copyright (c) 2020 Yuji Hirose. All rights reserved.
5// MIT License 5// MIT License
6// 6//
7 7
@@ -11,6 +11,7 @@
11/* 11/*
12 * Configuration 12 * Configuration
13 */ 13 */
14
14#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 15#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND
15#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 16#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5
16#endif 17#endif
@@ -40,7 +41,7 @@
40#endif 41#endif
41 42
42#ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH 43#ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH
43#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH (std::numeric_limits<size_t>::max)() 44#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH (std::numeric_limits<size_t>::max())
44#endif 45#endif
45 46
46#ifndef CPPHTTPLIB_RECV_BUFSIZ 47#ifndef CPPHTTPLIB_RECV_BUFSIZ
@@ -48,9 +49,14 @@
48#endif 49#endif
49 50
50#ifndef CPPHTTPLIB_THREAD_POOL_COUNT 51#ifndef CPPHTTPLIB_THREAD_POOL_COUNT
51#define CPPHTTPLIB_THREAD_POOL_COUNT 8 52#define CPPHTTPLIB_THREAD_POOL_COUNT \
53 (std::max(1u, std::thread::hardware_concurrency() - 1))
52#endif 54#endif
53 55
56/*
57 * Headers
58 */
59
54#ifdef _WIN32 60#ifdef _WIN32
55#ifndef _CRT_SECURE_NO_WARNINGS 61#ifndef _CRT_SECURE_NO_WARNINGS
56#define _CRT_SECURE_NO_WARNINGS 62#define _CRT_SECURE_NO_WARNINGS
@@ -62,9 +68,9 @@
62 68
63#if defined(_MSC_VER) 69#if defined(_MSC_VER)
64#ifdef _WIN64 70#ifdef _WIN64
65typedef __int64 ssize_t; 71using ssize_t = __int64;
66#else 72#else
67typedef int ssize_t; 73using ssize_t = int;
68#endif 74#endif
69 75
70#if _MSC_VER < 1900 76#if _MSC_VER < 1900
@@ -100,7 +106,7 @@ typedef int ssize_t;
100#define strcasecmp _stricmp 106#define strcasecmp _stricmp
101#endif // strcasecmp 107#endif // strcasecmp
102 108
103typedef SOCKET socket_t; 109using socket_t = SOCKET;
104#ifdef CPPHTTPLIB_USE_POLL 110#ifdef CPPHTTPLIB_USE_POLL
105#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) 111#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
106#endif 112#endif
@@ -109,23 +115,25 @@ typedef SOCKET socket_t;
109 115
110#include <arpa/inet.h> 116#include <arpa/inet.h>
111#include <cstring> 117#include <cstring>
118#include <ifaddrs.h>
112#include <netdb.h> 119#include <netdb.h>
113#include <netinet/in.h> 120#include <netinet/in.h>
114#ifdef CPPHTTPLIB_USE_POLL 121#ifdef CPPHTTPLIB_USE_POLL
115#include <poll.h> 122#include <poll.h>
116#endif 123#endif
124#include <csignal>
117#include <pthread.h> 125#include <pthread.h>
118#include <signal.h>
119#include <sys/select.h> 126#include <sys/select.h>
120#include <sys/socket.h> 127#include <sys/socket.h>
121#include <unistd.h> 128#include <unistd.h>
122 129
123typedef int socket_t; 130using socket_t = int;
124#define INVALID_SOCKET (-1) 131#define INVALID_SOCKET (-1)
125#endif //_WIN32 132#endif //_WIN32
126 133
127#include <assert.h> 134#include <array>
128#include <atomic> 135#include <atomic>
136#include <cassert>
129#include <condition_variable> 137#include <condition_variable>
130#include <errno.h> 138#include <errno.h>
131#include <fcntl.h> 139#include <fcntl.h>
@@ -143,9 +151,13 @@ typedef int socket_t;
143 151
144#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 152#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
145#include <openssl/err.h> 153#include <openssl/err.h>
154#include <openssl/md5.h>
146#include <openssl/ssl.h> 155#include <openssl/ssl.h>
147#include <openssl/x509v3.h> 156#include <openssl/x509v3.h>
148 157
158#include <iomanip>
159#include <sstream>
160
149// #if OPENSSL_VERSION_NUMBER < 0x1010100fL 161// #if OPENSSL_VERSION_NUMBER < 0x1010100fL
150// #error Sorry, OpenSSL versions prior to 1.1.1 are not supported 162// #error Sorry, OpenSSL versions prior to 1.1.1 are not supported
151// #endif 163// #endif
@@ -162,6 +174,9 @@ inline const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *asn1) {
162#include <zlib.h> 174#include <zlib.h>
163#endif 175#endif
164 176
177/*
178 * Declaration
179 */
165namespace httplib { 180namespace httplib {
166 181
167namespace detail { 182namespace detail {
@@ -176,37 +191,15 @@ struct ci {
176 191
177} // namespace detail 192} // namespace detail
178 193
179enum class HttpVersion { v1_0 = 0, v1_1 }; 194using Headers = std::multimap<std::string, std::string, detail::ci>;
180
181typedef std::multimap<std::string, std::string, detail::ci> Headers;
182
183typedef std::multimap<std::string, std::string> Params;
184typedef std::smatch Match;
185
186typedef std::function<void(const char *data, size_t data_len)> DataSink;
187
188typedef std::function<void()> Done;
189
190typedef std::function<void(size_t offset, size_t length, DataSink sink,
191 Done done)>
192 ContentProvider;
193 195
194typedef std::function<bool(const char *data, size_t data_length, size_t offset, 196using Params = std::multimap<std::string, std::string>;
195 uint64_t content_length)> 197using Match = std::smatch;
196 ContentReceiver;
197 198
198typedef std::function<bool(uint64_t current, uint64_t total)> Progress; 199using Progress = std::function<bool(uint64_t current, uint64_t total)>;
199 200
200struct Response; 201struct Response;
201typedef std::function<bool(const Response &response)> ResponseHandler; 202using ResponseHandler = std::function<bool(const Response &response)>;
202
203struct MultipartFile {
204 std::string filename;
205 std::string content_type;
206 size_t offset = 0;
207 size_t length = 0;
208};
209typedef std::multimap<std::string, MultipartFile> MultipartFiles;
210 203
211struct MultipartFormData { 204struct MultipartFormData {
212 std::string name; 205 std::string name;
@@ -214,10 +207,53 @@ struct MultipartFormData {
214 std::string filename; 207 std::string filename;
215 std::string content_type; 208 std::string content_type;
216}; 209};
217typedef std::vector<MultipartFormData> MultipartFormDataItems; 210using MultipartFormDataItems = std::vector<MultipartFormData>;
211using MultipartFormDataMap = std::multimap<std::string, MultipartFormData>;
212
213class DataSink {
214public:
215 DataSink() = default;
216 DataSink(const DataSink &) = delete;
217 DataSink &operator=(const DataSink &) = delete;
218 DataSink(DataSink &&) = delete;
219 DataSink &operator=(DataSink &&) = delete;
220
221 std::function<void(const char *data, size_t data_len)> write;
222 std::function<void()> done;
223 std::function<bool()> is_writable;
224};
225
226using ContentProvider =
227 std::function<void(size_t offset, size_t length, DataSink &sink)>;
228
229using ContentReceiver =
230 std::function<bool(const char *data, size_t data_length)>;
231
232using MultipartContentHeader =
233 std::function<bool(const MultipartFormData &file)>;
234
235class ContentReader {
236public:
237 using Reader = std::function<bool(ContentReceiver receiver)>;
238 using MultipartReader = std::function<bool(MultipartContentHeader header,
239 ContentReceiver receiver)>;
240
241 ContentReader(Reader reader, MultipartReader muitlpart_reader)
242 : reader_(reader), muitlpart_reader_(muitlpart_reader) {}
243
244 bool operator()(MultipartContentHeader header,
245 ContentReceiver receiver) const {
246 return muitlpart_reader_(header, receiver);
247 }
248
249 bool operator()(ContentReceiver receiver) const { return reader_(receiver); }
250
251 Reader reader_;
252 MultipartReader muitlpart_reader_;
253};
218 254
219typedef std::pair<ssize_t, ssize_t> Range; 255using Range = std::pair<ssize_t, ssize_t>;
220typedef std::vector<Range> Ranges; 256using Ranges = std::vector<Range>;
221 257
222struct Request { 258struct Request {
223 std::string method; 259 std::string method;
@@ -229,7 +265,7 @@ struct Request {
229 std::string version; 265 std::string version;
230 std::string target; 266 std::string target;
231 Params params; 267 Params params;
232 MultipartFiles files; 268 MultipartFormDataMap files;
233 Ranges ranges; 269 Ranges ranges;
234 Match matches; 270 Match matches;
235 271
@@ -253,13 +289,19 @@ struct Request {
253 std::string get_param_value(const char *key, size_t id = 0) const; 289 std::string get_param_value(const char *key, size_t id = 0) const;
254 size_t get_param_value_count(const char *key) const; 290 size_t get_param_value_count(const char *key) const;
255 291
292 bool is_multipart_form_data() const;
293
256 bool has_file(const char *key) const; 294 bool has_file(const char *key) const;
257 MultipartFile get_file_value(const char *key) const; 295 MultipartFormData get_file_value(const char *key) const;
296
297 // private members...
298 size_t content_length;
299 ContentProvider content_provider;
258}; 300};
259 301
260struct Response { 302struct Response {
261 std::string version; 303 std::string version;
262 int status; 304 int status = -1;
263 Headers headers; 305 Headers headers;
264 std::string body; 306 std::string body;
265 307
@@ -269,106 +311,81 @@ struct Response {
269 void set_header(const char *key, const char *val); 311 void set_header(const char *key, const char *val);
270 void set_header(const char *key, const std::string &val); 312 void set_header(const char *key, const std::string &val);
271 313
272 void set_redirect(const char *uri); 314 void set_redirect(const char *url);
273 void set_content(const char *s, size_t n, const char *content_type); 315 void set_content(const char *s, size_t n, const char *content_type);
274 void set_content(const std::string &s, const char *content_type); 316 void set_content(const std::string &s, const char *content_type);
275 317
276 void set_content_provider( 318 void set_content_provider(
277 size_t length, 319 size_t length,
278 std::function<void(size_t offset, size_t length, DataSink sink)> provider, 320 std::function<void(size_t offset, size_t length, DataSink &sink)>
321 provider,
279 std::function<void()> resource_releaser = [] {}); 322 std::function<void()> resource_releaser = [] {});
280 323
281 void set_chunked_content_provider( 324 void set_chunked_content_provider(
282 std::function<void(size_t offset, DataSink sink, Done done)> provider, 325 std::function<void(size_t offset, DataSink &sink)> provider,
283 std::function<void()> resource_releaser = [] {}); 326 std::function<void()> resource_releaser = [] {});
284 327
285 Response() : status(-1), content_provider_resource_length(0) {} 328 Response() = default;
286 329 Response(const Response &) = default;
330 Response &operator=(const Response &) = default;
331 Response(Response &&) = default;
332 Response &operator=(Response &&) = default;
287 ~Response() { 333 ~Response() {
288 if (content_provider_resource_releaser) { 334 if (content_provider_resource_releaser) {
289 content_provider_resource_releaser(); 335 content_provider_resource_releaser();
290 } 336 }
291 } 337 }
292 338
293 size_t content_provider_resource_length; 339 // private members...
340 size_t content_length = 0;
294 ContentProvider content_provider; 341 ContentProvider content_provider;
295 std::function<void()> content_provider_resource_releaser; 342 std::function<void()> content_provider_resource_releaser;
296}; 343};
297 344
298class Stream { 345class Stream {
299public: 346public:
300 virtual ~Stream() {} 347 virtual ~Stream() = default;
348
349 virtual bool is_readable() const = 0;
350 virtual bool is_writable() const = 0;
351
301 virtual int read(char *ptr, size_t size) = 0; 352 virtual int read(char *ptr, size_t size) = 0;
302 virtual int write(const char *ptr, size_t size1) = 0; 353 virtual int write(const char *ptr, size_t size) = 0;
303 virtual int write(const char *ptr) = 0;
304 virtual int write(const std::string &s) = 0;
305 virtual std::string get_remote_addr() const = 0; 354 virtual std::string get_remote_addr() const = 0;
306 355
307 template <typename... Args> 356 template <typename... Args>
308 int write_format(const char *fmt, const Args &... args); 357 int write_format(const char *fmt, const Args &... args);
309}; 358 int write(const char *ptr);
310 359 int write(const std::string &s);
311class SocketStream : public Stream {
312public:
313 SocketStream(socket_t sock);
314 virtual ~SocketStream();
315
316 virtual int read(char *ptr, size_t size);
317 virtual int write(const char *ptr, size_t size);
318 virtual int write(const char *ptr);
319 virtual int write(const std::string &s);
320 virtual std::string get_remote_addr() const;
321
322private:
323 socket_t sock_;
324};
325
326class BufferStream : public Stream {
327public:
328 BufferStream() {}
329 virtual ~BufferStream() {}
330
331 virtual int read(char *ptr, size_t size);
332 virtual int write(const char *ptr, size_t size);
333 virtual int write(const char *ptr);
334 virtual int write(const std::string &s);
335 virtual std::string get_remote_addr() const;
336
337 const std::string &get_buffer() const;
338
339private:
340 std::string buffer;
341}; 360};
342 361
343class TaskQueue { 362class TaskQueue {
344public: 363public:
345 TaskQueue() {} 364 TaskQueue() = default;
346 virtual ~TaskQueue() {} 365 virtual ~TaskQueue() = default;
347 virtual void enqueue(std::function<void()> fn) = 0; 366 virtual void enqueue(std::function<void()> fn) = 0;
348 virtual void shutdown() = 0; 367 virtual void shutdown() = 0;
349}; 368};
350 369
351#if CPPHTTPLIB_THREAD_POOL_COUNT > 0
352class ThreadPool : public TaskQueue { 370class ThreadPool : public TaskQueue {
353public: 371public:
354 ThreadPool(size_t n) : shutdown_(false) { 372 explicit ThreadPool(size_t n) : shutdown_(false) {
355 while (n) { 373 while (n) {
356 auto t = std::make_shared<std::thread>(worker(*this)); 374 threads_.emplace_back(worker(*this));
357 threads_.push_back(t);
358 n--; 375 n--;
359 } 376 }
360 } 377 }
361 378
362 ThreadPool(const ThreadPool &) = delete; 379 ThreadPool(const ThreadPool &) = delete;
363 virtual ~ThreadPool() {} 380 ~ThreadPool() override = default;
364 381
365 virtual void enqueue(std::function<void()> fn) override { 382 void enqueue(std::function<void()> fn) override {
366 std::unique_lock<std::mutex> lock(mutex_); 383 std::unique_lock<std::mutex> lock(mutex_);
367 jobs_.push_back(fn); 384 jobs_.push_back(fn);
368 cond_.notify_one(); 385 cond_.notify_one();
369 } 386 }
370 387
371 virtual void shutdown() override { 388 void shutdown() override {
372 // Stop all worker threads... 389 // Stop all worker threads...
373 { 390 {
374 std::unique_lock<std::mutex> lock(mutex_); 391 std::unique_lock<std::mutex> lock(mutex_);
@@ -378,14 +395,14 @@ public:
378 cond_.notify_all(); 395 cond_.notify_all();
379 396
380 // Join... 397 // Join...
381 for (auto t : threads_) { 398 for (auto &t : threads_) {
382 t->join(); 399 t.join();
383 } 400 }
384 } 401 }
385 402
386private: 403private:
387 struct worker { 404 struct worker {
388 worker(ThreadPool &pool) : pool_(pool) {} 405 explicit worker(ThreadPool &pool) : pool_(pool) {}
389 406
390 void operator()() { 407 void operator()() {
391 for (;;) { 408 for (;;) {
@@ -411,7 +428,7 @@ private:
411 }; 428 };
412 friend struct worker; 429 friend struct worker;
413 430
414 std::vector<std::shared_ptr<std::thread>> threads_; 431 std::vector<std::thread> threads_;
415 std::list<std::function<void()>> jobs_; 432 std::list<std::function<void()>> jobs_;
416 433
417 bool shutdown_; 434 bool shutdown_;
@@ -419,46 +436,16 @@ private:
419 std::condition_variable cond_; 436 std::condition_variable cond_;
420 std::mutex mutex_; 437 std::mutex mutex_;
421}; 438};
422#else
423class Threads : public TaskQueue {
424public:
425 Threads() : running_threads_(0) {}
426 virtual ~Threads() {}
427
428 virtual void enqueue(std::function<void()> fn) override {
429 std::thread([=]() {
430 {
431 std::lock_guard<std::mutex> guard(running_threads_mutex_);
432 running_threads_++;
433 }
434
435 fn();
436
437 {
438 std::lock_guard<std::mutex> guard(running_threads_mutex_);
439 running_threads_--;
440 }
441 }).detach();
442 }
443
444 virtual void shutdown() override {
445 for (;;) {
446 std::this_thread::sleep_for(std::chrono::milliseconds(10));
447 std::lock_guard<std::mutex> guard(running_threads_mutex_);
448 if (!running_threads_) { break; }
449 }
450 }
451 439
452private: 440using Logger = std::function<void(const Request &, const Response &)>;
453 std::mutex running_threads_mutex_;
454 int running_threads_;
455};
456#endif
457 441
458class Server { 442class Server {
459public: 443public:
460 typedef std::function<void(const Request &, Response &)> Handler; 444 using Handler = std::function<void(const Request &, Response &)>;
461 typedef std::function<void(const Request &, const Response &)> Logger; 445 using HandlerWithContentReader = std::function<void(
446 const Request &, Response &, const ContentReader &content_reader)>;
447 using Expect100ContinueHandler =
448 std::function<int(const Request &, Response &)>;
462 449
463 Server(); 450 Server();
464 451
@@ -468,21 +455,32 @@ public:
468 455
469 Server &Get(const char *pattern, Handler handler); 456 Server &Get(const char *pattern, Handler handler);
470 Server &Post(const char *pattern, Handler handler); 457 Server &Post(const char *pattern, Handler handler);
471 458 Server &Post(const char *pattern, HandlerWithContentReader handler);
472 Server &Put(const char *pattern, Handler handler); 459 Server &Put(const char *pattern, Handler handler);
460 Server &Put(const char *pattern, HandlerWithContentReader handler);
473 Server &Patch(const char *pattern, Handler handler); 461 Server &Patch(const char *pattern, Handler handler);
462 Server &Patch(const char *pattern, HandlerWithContentReader handler);
474 Server &Delete(const char *pattern, Handler handler); 463 Server &Delete(const char *pattern, Handler handler);
475 Server &Options(const char *pattern, Handler handler); 464 Server &Options(const char *pattern, Handler handler);
476 465
477 bool set_base_dir(const char *path); 466 [[deprecated]] bool set_base_dir(const char *dir,
467 const char *mount_point = nullptr);
468 bool set_mount_point(const char *mount_point, const char *dir);
469 bool remove_mount_point(const char *mount_point);
470 void set_file_extension_and_mimetype_mapping(const char *ext,
471 const char *mime);
478 void set_file_request_handler(Handler handler); 472 void set_file_request_handler(Handler handler);
479 473
480 void set_error_handler(Handler handler); 474 void set_error_handler(Handler handler);
481 void set_logger(Logger logger); 475 void set_logger(Logger logger);
482 476
477 void set_expect_100_continue_handler(Expect100ContinueHandler handler);
478
483 void set_keep_alive_max_count(size_t count); 479 void set_keep_alive_max_count(size_t count);
480 void set_read_timeout(time_t sec, time_t usec);
484 void set_payload_max_length(size_t length); 481 void set_payload_max_length(size_t length);
485 482
483 bool bind_to_port(const char *host, int port, int socket_flags = 0);
486 int bind_to_any_port(const char *host, int socket_flags = 0); 484 int bind_to_any_port(const char *host, int socket_flags = 0);
487 bool listen_after_bind(); 485 bool listen_after_bind();
488 486
@@ -496,22 +494,29 @@ public:
496protected: 494protected:
497 bool process_request(Stream &strm, bool last_connection, 495 bool process_request(Stream &strm, bool last_connection,
498 bool &connection_close, 496 bool &connection_close,
499 std::function<void(Request &)> setup_request); 497 const std::function<void(Request &)> &setup_request);
500 498
501 size_t keep_alive_max_count_; 499 size_t keep_alive_max_count_;
500 time_t read_timeout_sec_;
501 time_t read_timeout_usec_;
502 size_t payload_max_length_; 502 size_t payload_max_length_;
503 503
504private: 504private:
505 typedef std::vector<std::pair<std::regex, Handler>> Handlers; 505 using Handlers = std::vector<std::pair<std::regex, Handler>>;
506 using HandlersForContentReader =
507 std::vector<std::pair<std::regex, HandlerWithContentReader>>;
506 508
507 socket_t create_server_socket(const char *host, int port, 509 socket_t create_server_socket(const char *host, int port,
508 int socket_flags) const; 510 int socket_flags) const;
509 int bind_internal(const char *host, int port, int socket_flags); 511 int bind_internal(const char *host, int port, int socket_flags);
510 bool listen_internal(); 512 bool listen_internal();
511 513
512 bool routing(Request &req, Response &res); 514 bool routing(Request &req, Response &res, Stream &strm, bool last_connection);
513 bool handle_file_request(Request &req, Response &res); 515 bool handle_file_request(Request &req, Response &res, bool head = false);
514 bool dispatch_request(Request &req, Response &res, Handlers &handlers); 516 bool dispatch_request(Request &req, Response &res, Handlers &handlers);
517 bool dispatch_request_for_content_reader(Request &req, Response &res,
518 ContentReader content_reader,
519 HandlersForContentReader &handlers);
515 520
516 bool parse_request_line(const char *s, Request &req); 521 bool parse_request_line(const char *s, Request &req);
517 bool write_response(Stream &strm, bool last_connection, const Request &req, 522 bool write_response(Stream &strm, bool last_connection, const Request &req,
@@ -519,26 +524,43 @@ private:
519 bool write_content_with_provider(Stream &strm, const Request &req, 524 bool write_content_with_provider(Stream &strm, const Request &req,
520 Response &res, const std::string &boundary, 525 Response &res, const std::string &boundary,
521 const std::string &content_type); 526 const std::string &content_type);
527 bool read_content(Stream &strm, bool last_connection, Request &req,
528 Response &res);
529 bool read_content_with_content_receiver(
530 Stream &strm, bool last_connection, Request &req, Response &res,
531 ContentReceiver receiver, MultipartContentHeader multipart_header,
532 ContentReceiver multipart_receiver);
533 bool read_content_core(Stream &strm, bool last_connection, Request &req,
534 Response &res, ContentReceiver receiver,
535 MultipartContentHeader mulitpart_header,
536 ContentReceiver multipart_receiver);
522 537
523 virtual bool process_and_close_socket(socket_t sock); 538 virtual bool process_and_close_socket(socket_t sock);
524 539
525 std::atomic<bool> is_running_; 540 std::atomic<bool> is_running_;
526 std::atomic<socket_t> svr_sock_; 541 std::atomic<socket_t> svr_sock_;
527 std::string base_dir_; 542 std::vector<std::pair<std::string, std::string>> base_dirs_;
543 std::map<std::string, std::string> file_extension_and_mimetype_map_;
528 Handler file_request_handler_; 544 Handler file_request_handler_;
529 Handlers get_handlers_; 545 Handlers get_handlers_;
530 Handlers post_handlers_; 546 Handlers post_handlers_;
547 HandlersForContentReader post_handlers_for_content_reader_;
531 Handlers put_handlers_; 548 Handlers put_handlers_;
549 HandlersForContentReader put_handlers_for_content_reader_;
532 Handlers patch_handlers_; 550 Handlers patch_handlers_;
551 HandlersForContentReader patch_handlers_for_content_reader_;
533 Handlers delete_handlers_; 552 Handlers delete_handlers_;
534 Handlers options_handlers_; 553 Handlers options_handlers_;
535 Handler error_handler_; 554 Handler error_handler_;
536 Logger logger_; 555 Logger logger_;
556 Expect100ContinueHandler expect_100_continue_handler_;
537}; 557};
538 558
539class Client { 559class Client {
540public: 560public:
541 Client(const char *host, int port = 80, time_t timeout_sec = 300); 561 explicit Client(const std::string &host, int port = 80,
562 const std::string &client_cert_path = std::string(),
563 const std::string &client_key_path = std::string());
542 564
543 virtual ~Client(); 565 virtual ~Client();
544 566
@@ -586,6 +608,15 @@ public:
586 const std::string &body, 608 const std::string &body,
587 const char *content_type); 609 const char *content_type);
588 610
611 std::shared_ptr<Response> Post(const char *path, size_t content_length,
612 ContentProvider content_provider,
613 const char *content_type);
614
615 std::shared_ptr<Response> Post(const char *path, const Headers &headers,
616 size_t content_length,
617 ContentProvider content_provider,
618 const char *content_type);
619
589 std::shared_ptr<Response> Post(const char *path, const Params &params); 620 std::shared_ptr<Response> Post(const char *path, const Params &params);
590 621
591 std::shared_ptr<Response> Post(const char *path, const Headers &headers, 622 std::shared_ptr<Response> Post(const char *path, const Headers &headers,
@@ -604,6 +635,20 @@ public:
604 const std::string &body, 635 const std::string &body,
605 const char *content_type); 636 const char *content_type);
606 637
638 std::shared_ptr<Response> Put(const char *path, size_t content_length,
639 ContentProvider content_provider,
640 const char *content_type);
641
642 std::shared_ptr<Response> Put(const char *path, const Headers &headers,
643 size_t content_length,
644 ContentProvider content_provider,
645 const char *content_type);
646
647 std::shared_ptr<Response> Put(const char *path, const Params &params);
648
649 std::shared_ptr<Response> Put(const char *path, const Headers &headers,
650 const Params &params);
651
607 std::shared_ptr<Response> Patch(const char *path, const std::string &body, 652 std::shared_ptr<Response> Patch(const char *path, const std::string &body,
608 const char *content_type); 653 const char *content_type);
609 654
@@ -611,6 +656,15 @@ public:
611 const std::string &body, 656 const std::string &body,
612 const char *content_type); 657 const char *content_type);
613 658
659 std::shared_ptr<Response> Patch(const char *path, size_t content_length,
660 ContentProvider content_provider,
661 const char *content_type);
662
663 std::shared_ptr<Response> Patch(const char *path, const Headers &headers,
664 size_t content_length,
665 ContentProvider content_provider,
666 const char *content_type);
667
614 std::shared_ptr<Response> Delete(const char *path); 668 std::shared_ptr<Response> Delete(const char *path);
615 669
616 std::shared_ptr<Response> Delete(const char *path, const std::string &body, 670 std::shared_ptr<Response> Delete(const char *path, const std::string &body,
@@ -631,9 +685,33 @@ public:
631 bool send(const std::vector<Request> &requests, 685 bool send(const std::vector<Request> &requests,
632 std::vector<Response> &responses); 686 std::vector<Response> &responses);
633 687
688 void set_timeout_sec(time_t timeout_sec);
689
690 void set_read_timeout(time_t sec, time_t usec);
691
634 void set_keep_alive_max_count(size_t count); 692 void set_keep_alive_max_count(size_t count);
635 693
636 void follow_location(bool on); 694 void set_basic_auth(const char *username, const char *password);
695
696#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
697 void set_digest_auth(const char *username, const char *password);
698#endif
699
700 void set_follow_location(bool on);
701
702 void set_compress(bool on);
703
704 void set_interface(const char *intf);
705
706 void set_proxy(const char *host, int port);
707
708 void set_proxy_basic_auth(const char *username, const char *password);
709
710#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
711 void set_proxy_digest_auth(const char *username, const char *password);
712#endif
713
714 void set_logger(Logger logger);
637 715
638protected: 716protected:
639 bool process_request(Stream &strm, const Request &req, Response &res, 717 bool process_request(Stream &strm, const Request &req, Response &res,
@@ -641,16 +719,85 @@ protected:
641 719
642 const std::string host_; 720 const std::string host_;
643 const int port_; 721 const int port_;
644 time_t timeout_sec_;
645 const std::string host_and_port_; 722 const std::string host_and_port_;
646 size_t keep_alive_max_count_; 723
647 size_t follow_location_; 724 // Settings
725 std::string client_cert_path_;
726 std::string client_key_path_;
727
728 time_t timeout_sec_ = 300;
729 time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND;
730 time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND;
731
732 size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT;
733
734 std::string basic_auth_username_;
735 std::string basic_auth_password_;
736#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
737 std::string digest_auth_username_;
738 std::string digest_auth_password_;
739#endif
740
741 bool follow_location_ = false;
742
743 bool compress_ = false;
744
745 std::string interface_;
746
747 std::string proxy_host_;
748 int proxy_port_;
749
750 std::string proxy_basic_auth_username_;
751 std::string proxy_basic_auth_password_;
752#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
753 std::string proxy_digest_auth_username_;
754 std::string proxy_digest_auth_password_;
755#endif
756
757 Logger logger_;
758
759 void copy_settings(const Client &rhs) {
760 client_cert_path_ = rhs.client_cert_path_;
761 client_key_path_ = rhs.client_key_path_;
762 timeout_sec_ = rhs.timeout_sec_;
763 read_timeout_sec_ = rhs.read_timeout_sec_;
764 read_timeout_usec_ = rhs.read_timeout_usec_;
765 keep_alive_max_count_ = rhs.keep_alive_max_count_;
766 basic_auth_username_ = rhs.basic_auth_username_;
767 basic_auth_password_ = rhs.basic_auth_password_;
768#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
769 digest_auth_username_ = rhs.digest_auth_username_;
770 digest_auth_password_ = rhs.digest_auth_password_;
771#endif
772 follow_location_ = rhs.follow_location_;
773 compress_ = rhs.compress_;
774 interface_ = rhs.interface_;
775 proxy_host_ = rhs.proxy_host_;
776 proxy_port_ = rhs.proxy_port_;
777 proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_;
778 proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_;
779#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
780 proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_;
781 proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_;
782#endif
783 logger_ = rhs.logger_;
784 }
648 785
649private: 786private:
650 socket_t create_client_socket() const; 787 socket_t create_client_socket() const;
651 bool read_response_line(Stream &strm, Response &res); 788 bool read_response_line(Stream &strm, Response &res);
652 void write_request(Stream &strm, const Request &req, bool last_connection); 789 bool write_request(Stream &strm, const Request &req, bool last_connection);
653 bool redirect(const Request &req, Response &res); 790 bool redirect(const Request &req, Response &res);
791 bool handle_request(Stream &strm, const Request &req, Response &res,
792 bool last_connection, bool &connection_close);
793#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
794 bool connect(socket_t sock, Response &res, bool &error);
795#endif
796
797 std::shared_ptr<Response> send_with_content_provider(
798 const char *method, const char *path, const Headers &headers,
799 const std::string &body, size_t content_length,
800 ContentProvider content_provider, const char *content_type);
654 801
655 virtual bool process_and_close_socket( 802 virtual bool process_and_close_socket(
656 socket_t sock, size_t request_count, 803 socket_t sock, size_t request_count,
@@ -692,22 +839,6 @@ inline void Post(std::vector<Request> &requests, const char *path,
692} 839}
693 840
694#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 841#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
695class SSLSocketStream : public Stream {
696public:
697 SSLSocketStream(socket_t sock, SSL *ssl);
698 virtual ~SSLSocketStream();
699
700 virtual int read(char *ptr, size_t size);
701 virtual int write(const char *ptr, size_t size);
702 virtual int write(const char *ptr);
703 virtual int write(const std::string &s);
704 virtual std::string get_remote_addr() const;
705
706private:
707 socket_t sock_;
708 SSL *ssl_;
709};
710
711class SSLServer : public Server { 842class SSLServer : public Server {
712public: 843public:
713 SSLServer(const char *cert_path, const char *private_key_path, 844 SSLServer(const char *cert_path, const char *private_key_path,
@@ -727,9 +858,9 @@ private:
727 858
728class SSLClient : public Client { 859class SSLClient : public Client {
729public: 860public:
730 SSLClient(const char *host, int port = 443, time_t timeout_sec = 300, 861 SSLClient(const std::string &host, int port = 443,
731 const char *client_cert_path = nullptr, 862 const std::string &client_cert_path = std::string(),
732 const char *client_key_path = nullptr); 863 const std::string &client_key_path = std::string());
733 864
734 virtual ~SSLClient(); 865 virtual ~SSLClient();
735 866
@@ -737,11 +868,12 @@ public:
737 868
738 void set_ca_cert_path(const char *ca_ceert_file_path, 869 void set_ca_cert_path(const char *ca_ceert_file_path,
739 const char *ca_cert_dir_path = nullptr); 870 const char *ca_cert_dir_path = nullptr);
871
740 void enable_server_certificate_verification(bool enabled); 872 void enable_server_certificate_verification(bool enabled);
741 873
742 long get_openssl_verify_result() const; 874 long get_openssl_verify_result() const;
743 875
744 SSL_CTX* ssl_context() const noexcept; 876 SSL_CTX *ssl_context() const noexcept;
745 877
746private: 878private:
747 virtual bool process_and_close_socket( 879 virtual bool process_and_close_socket(
@@ -759,6 +891,7 @@ private:
759 SSL_CTX *ctx_; 891 SSL_CTX *ctx_;
760 std::mutex ctx_mutex_; 892 std::mutex ctx_mutex_;
761 std::vector<std::string> host_components_; 893 std::vector<std::string> host_components_;
894
762 std::string ca_cert_file_path_; 895 std::string ca_cert_file_path_;
763 std::string ca_cert_dir_path_; 896 std::string ca_cert_dir_path_;
764 bool server_certificate_verification_ = false; 897 bool server_certificate_verification_ = false;
@@ -766,9 +899,12 @@ private:
766}; 899};
767#endif 900#endif
768 901
902// ----------------------------------------------------------------------------
903
769/* 904/*
770 * Implementation 905 * Implementation
771 */ 906 */
907
772namespace detail { 908namespace detail {
773 909
774inline bool is_hex(char c, int &v) { 910inline bool is_hex(char c, int &v) {
@@ -932,8 +1068,8 @@ inline void read_file(const std::string &path, std::string &out) {
932 1068
933inline std::string file_extension(const std::string &path) { 1069inline std::string file_extension(const std::string &path) {
934 std::smatch m; 1070 std::smatch m;
935 auto pat = std::regex("\\.([a-zA-Z0-9]+)$"); 1071 static auto re = std::regex("\\.([a-zA-Z0-9]+)$");
936 if (std::regex_search(path, m, pat)) { return m[1].str(); } 1072 if (std::regex_search(path, m, re)) { return m[1].str(); }
937 return std::string(); 1073 return std::string();
938} 1074}
939 1075
@@ -976,6 +1112,11 @@ public:
976 } 1112 }
977 } 1113 }
978 1114
1115 bool end_with_crlf() const {
1116 auto end = ptr() + size();
1117 return size() >= 2 && end[-2] == '\r' && end[-1] == '\n';
1118 }
1119
979 bool getline() { 1120 bool getline() {
980 fixed_buffer_used_size_ = 0; 1121 fixed_buffer_used_size_ = 0;
981 glowable_buffer_.clear(); 1122 glowable_buffer_.clear();
@@ -1019,7 +1160,7 @@ private:
1019 Stream &strm_; 1160 Stream &strm_;
1020 char *fixed_buffer_; 1161 char *fixed_buffer_;
1021 const size_t fixed_buffer_size_; 1162 const size_t fixed_buffer_size_;
1022 size_t fixed_buffer_used_size_; 1163 size_t fixed_buffer_used_size_ = 0;
1023 std::string glowable_buffer_; 1164 std::string glowable_buffer_;
1024}; 1165};
1025 1166
@@ -1053,6 +1194,28 @@ inline int select_read(socket_t sock, time_t sec, time_t usec) {
1053#endif 1194#endif
1054} 1195}
1055 1196
1197inline int select_write(socket_t sock, time_t sec, time_t usec) {
1198#ifdef CPPHTTPLIB_USE_POLL
1199 struct pollfd pfd_read;
1200 pfd_read.fd = sock;
1201 pfd_read.events = POLLOUT;
1202
1203 auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
1204
1205 return poll(&pfd_read, 1, timeout);
1206#else
1207 fd_set fds;
1208 FD_ZERO(&fds);
1209 FD_SET(sock, &fds);
1210
1211 timeval tv;
1212 tv.tv_sec = static_cast<long>(sec);
1213 tv.tv_usec = static_cast<long>(usec);
1214
1215 return select(static_cast<int>(sock + 1), nullptr, &fds, nullptr, &tv);
1216#endif
1217}
1218
1056inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) { 1219inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
1057#ifdef CPPHTTPLIB_USE_POLL 1220#ifdef CPPHTTPLIB_USE_POLL
1058 struct pollfd pfd_read; 1221 struct pollfd pfd_read;
@@ -1065,7 +1228,8 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
1065 pfd_read.revents & (POLLIN | POLLOUT)) { 1228 pfd_read.revents & (POLLIN | POLLOUT)) {
1066 int error = 0; 1229 int error = 0;
1067 socklen_t len = sizeof(error); 1230 socklen_t len = sizeof(error);
1068 return getsockopt(sock, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error), &len) >= 0 && 1231 return getsockopt(sock, SOL_SOCKET, SO_ERROR,
1232 reinterpret_cast<char *>(&error), &len) >= 0 &&
1069 !error; 1233 !error;
1070 } 1234 }
1071 return false; 1235 return false;
@@ -1085,27 +1249,86 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
1085 (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { 1249 (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) {
1086 int error = 0; 1250 int error = 0;
1087 socklen_t len = sizeof(error); 1251 socklen_t len = sizeof(error);
1088 return getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &len) >= 0 && 1252 return getsockopt(sock, SOL_SOCKET, SO_ERROR,
1253 reinterpret_cast<char *>(&error), &len) >= 0 &&
1089 !error; 1254 !error;
1090 } 1255 }
1091 return false; 1256 return false;
1092#endif 1257#endif
1093} 1258}
1094 1259
1260class SocketStream : public Stream {
1261public:
1262 SocketStream(socket_t sock, time_t read_timeout_sec,
1263 time_t read_timeout_usec);
1264 ~SocketStream() override;
1265
1266 bool is_readable() const override;
1267 bool is_writable() const override;
1268 int read(char *ptr, size_t size) override;
1269 int write(const char *ptr, size_t size) override;
1270 std::string get_remote_addr() const override;
1271
1272private:
1273 socket_t sock_;
1274 time_t read_timeout_sec_;
1275 time_t read_timeout_usec_;
1276};
1277
1278#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
1279class SSLSocketStream : public Stream {
1280public:
1281 SSLSocketStream(socket_t sock, SSL *ssl, time_t read_timeout_sec,
1282 time_t read_timeout_usec);
1283 virtual ~SSLSocketStream();
1284
1285 bool is_readable() const override;
1286 bool is_writable() const override;
1287 int read(char *ptr, size_t size) override;
1288 int write(const char *ptr, size_t size) override;
1289 std::string get_remote_addr() const override;
1290
1291private:
1292 socket_t sock_;
1293 SSL *ssl_;
1294 time_t read_timeout_sec_;
1295 time_t read_timeout_usec_;
1296};
1297#endif
1298
1299class BufferStream : public Stream {
1300public:
1301 BufferStream() = default;
1302 ~BufferStream() override = default;
1303
1304 bool is_readable() const override;
1305 bool is_writable() const override;
1306 int read(char *ptr, size_t size) override;
1307 int write(const char *ptr, size_t size) override;
1308 std::string get_remote_addr() const override;
1309
1310 const std::string &get_buffer() const;
1311
1312private:
1313 std::string buffer;
1314 int position = 0;
1315};
1316
1095template <typename T> 1317template <typename T>
1096inline bool process_and_close_socket(bool is_client_request, socket_t sock, 1318inline bool process_socket(bool is_client_request, socket_t sock,
1097 size_t keep_alive_max_count, T callback) { 1319 size_t keep_alive_max_count, time_t read_timeout_sec,
1320 time_t read_timeout_usec, T callback) {
1098 assert(keep_alive_max_count > 0); 1321 assert(keep_alive_max_count > 0);
1099 1322
1100 bool ret = false; 1323 auto ret = false;
1101 1324
1102 if (keep_alive_max_count > 1) { 1325 if (keep_alive_max_count > 1) {
1103 auto count = keep_alive_max_count; 1326 auto count = keep_alive_max_count;
1104 while (count > 0 && 1327 while (count > 0 &&
1105 (is_client_request || 1328 (is_client_request ||
1106 detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, 1329 select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND,
1107 CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0)) { 1330 CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0)) {
1108 SocketStream strm(sock); 1331 SocketStream strm(sock, read_timeout_sec, read_timeout_usec);
1109 auto last_connection = count == 1; 1332 auto last_connection = count == 1;
1110 auto connection_close = false; 1333 auto connection_close = false;
1111 1334
@@ -1114,12 +1337,22 @@ inline bool process_and_close_socket(bool is_client_request, socket_t sock,
1114 1337
1115 count--; 1338 count--;
1116 } 1339 }
1117 } else { 1340 } else { // keep_alive_max_count is 0 or 1
1118 SocketStream strm(sock); 1341 SocketStream strm(sock, read_timeout_sec, read_timeout_usec);
1119 auto dummy_connection_close = false; 1342 auto dummy_connection_close = false;
1120 ret = callback(strm, true, dummy_connection_close); 1343 ret = callback(strm, true, dummy_connection_close);
1121 } 1344 }
1122 1345
1346 return ret;
1347}
1348
1349template <typename T>
1350inline bool process_and_close_socket(bool is_client_request, socket_t sock,
1351 size_t keep_alive_max_count,
1352 time_t read_timeout_sec,
1353 time_t read_timeout_usec, T callback) {
1354 auto ret = process_socket(is_client_request, sock, keep_alive_max_count,
1355 read_timeout_sec, read_timeout_usec, callback);
1123 close_socket(sock); 1356 close_socket(sock);
1124 return ret; 1357 return ret;
1125} 1358}
@@ -1165,6 +1398,23 @@ socket_t create_socket(const char *host, int port, Fn fn,
1165#ifdef _WIN32 1398#ifdef _WIN32
1166 auto sock = WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, 1399 auto sock = WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol,
1167 nullptr, 0, WSA_FLAG_NO_HANDLE_INHERIT); 1400 nullptr, 0, WSA_FLAG_NO_HANDLE_INHERIT);
1401 /**
1402 * Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1
1403 * and above the socket creation fails on older Windows Systems.
1404 *
1405 * Let's try to create a socket the old way in this case.
1406 *
1407 * Reference:
1408 * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa
1409 *
1410 * WSA_FLAG_NO_HANDLE_INHERIT:
1411 * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with
1412 * SP1, and later
1413 *
1414 */
1415 if (sock == INVALID_SOCKET) {
1416 sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
1417 }
1168#else 1418#else
1169 auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); 1419 auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
1170#endif 1420#endif
@@ -1176,9 +1426,11 @@ socket_t create_socket(const char *host, int port, Fn fn,
1176 1426
1177 // Make 'reuse address' option available 1427 // Make 'reuse address' option available
1178 int yes = 1; 1428 int yes = 1;
1179 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char*>(&yes), sizeof(yes)); 1429 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char *>(&yes),
1430 sizeof(yes));
1180#ifdef SO_REUSEPORT 1431#ifdef SO_REUSEPORT
1181 setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<char*>(&yes), sizeof(yes)); 1432 setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<char *>(&yes),
1433 sizeof(yes));
1182#endif 1434#endif
1183 1435
1184 // bind or connect 1436 // bind or connect
@@ -1213,27 +1465,105 @@ inline bool is_connection_error() {
1213#endif 1465#endif
1214} 1466}
1215 1467
1468inline bool bind_ip_address(socket_t sock, const char *host) {
1469 struct addrinfo hints;
1470 struct addrinfo *result;
1471
1472 memset(&hints, 0, sizeof(struct addrinfo));
1473 hints.ai_family = AF_UNSPEC;
1474 hints.ai_socktype = SOCK_STREAM;
1475 hints.ai_protocol = 0;
1476
1477 if (getaddrinfo(host, "0", &hints, &result)) { return false; }
1478
1479 auto ret = false;
1480 for (auto rp = result; rp; rp = rp->ai_next) {
1481 const auto &ai = *rp;
1482 if (!::bind(sock, ai.ai_addr, static_cast<int>(ai.ai_addrlen))) {
1483 ret = true;
1484 break;
1485 }
1486 }
1487
1488 freeaddrinfo(result);
1489 return ret;
1490}
1491
1492inline std::string if2ip(const std::string &ifn) {
1493#ifndef _WIN32
1494 struct ifaddrs *ifap;
1495 getifaddrs(&ifap);
1496 for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) {
1497 if (ifa->ifa_addr && ifn == ifa->ifa_name) {
1498 if (ifa->ifa_addr->sa_family == AF_INET) {
1499 auto sa = reinterpret_cast<struct sockaddr_in *>(ifa->ifa_addr);
1500 char buf[INET_ADDRSTRLEN];
1501 if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) {
1502 freeifaddrs(ifap);
1503 return std::string(buf, INET_ADDRSTRLEN);
1504 }
1505 }
1506 }
1507 }
1508 freeifaddrs(ifap);
1509#endif
1510 return std::string();
1511}
1512
1513inline socket_t create_client_socket(const char *host, int port,
1514 time_t timeout_sec,
1515 const std::string &intf) {
1516 return create_socket(
1517 host, port, [&](socket_t sock, struct addrinfo &ai) -> bool {
1518 if (!intf.empty()) {
1519 auto ip = if2ip(intf);
1520 if (ip.empty()) { ip = intf; }
1521 if (!bind_ip_address(sock, ip.c_str())) { return false; }
1522 }
1523
1524 set_nonblocking(sock, true);
1525
1526 auto ret = ::connect(sock, ai.ai_addr, static_cast<int>(ai.ai_addrlen));
1527 if (ret < 0) {
1528 if (is_connection_error() ||
1529 !wait_until_socket_is_ready(sock, timeout_sec, 0)) {
1530 close_socket(sock);
1531 return false;
1532 }
1533 }
1534
1535 set_nonblocking(sock, false);
1536 return true;
1537 });
1538}
1539
1216inline std::string get_remote_addr(socket_t sock) { 1540inline std::string get_remote_addr(socket_t sock) {
1217 struct sockaddr_storage addr; 1541 struct sockaddr_storage addr;
1218 socklen_t len = sizeof(addr); 1542 socklen_t len = sizeof(addr);
1219 1543
1220 if (!getpeername(sock, reinterpret_cast<struct sockaddr *>(&addr), &len)) { 1544 if (!getpeername(sock, reinterpret_cast<struct sockaddr *>(&addr), &len)) {
1221 char ipstr[NI_MAXHOST]; 1545 std::array<char, NI_MAXHOST> ipstr{};
1222 1546
1223 if (!getnameinfo(reinterpret_cast<struct sockaddr *>(&addr), len, ipstr, sizeof(ipstr), 1547 if (!getnameinfo(reinterpret_cast<struct sockaddr *>(&addr), len,
1224 nullptr, 0, NI_NUMERICHOST)) { 1548 ipstr.data(), static_cast<unsigned int>(ipstr.size()), nullptr, 0, NI_NUMERICHOST)) {
1225 return ipstr; 1549 return ipstr.data();
1226 } 1550 }
1227 } 1551 }
1228 1552
1229 return std::string(); 1553 return std::string();
1230} 1554}
1231 1555
1232inline const char *find_content_type(const std::string &path) { 1556inline const char *
1557find_content_type(const std::string &path,
1558 const std::map<std::string, std::string> &user_data) {
1233 auto ext = file_extension(path); 1559 auto ext = file_extension(path);
1560
1561 auto it = user_data.find(ext);
1562 if (it != user_data.end()) { return it->second.c_str(); }
1563
1234 if (ext == "txt") { 1564 if (ext == "txt") {
1235 return "text/plain"; 1565 return "text/plain";
1236 } else if (ext == "html") { 1566 } else if (ext == "html" || ext == "htm") {
1237 return "text/html"; 1567 return "text/html";
1238 } else if (ext == "css") { 1568 } else if (ext == "css") {
1239 return "text/css"; 1569 return "text/css";
@@ -1253,6 +1583,8 @@ inline const char *find_content_type(const std::string &path) {
1253 return "application/pdf"; 1583 return "application/pdf";
1254 } else if (ext == "js") { 1584 } else if (ext == "js") {
1255 return "application/javascript"; 1585 return "application/javascript";
1586 } else if (ext == "wasm") {
1587 return "application/wasm";
1256 } else if (ext == "xml") { 1588 } else if (ext == "xml") {
1257 return "application/xml"; 1589 return "application/xml";
1258 } else if (ext == "xhtml") { 1590 } else if (ext == "xhtml") {
@@ -1263,19 +1595,25 @@ inline const char *find_content_type(const std::string &path) {
1263 1595
1264inline const char *status_message(int status) { 1596inline const char *status_message(int status) {
1265 switch (status) { 1597 switch (status) {
1598 case 100: return "Continue";
1266 case 200: return "OK"; 1599 case 200: return "OK";
1600 case 202: return "Accepted";
1601 case 204: return "No Content";
1267 case 206: return "Partial Content"; 1602 case 206: return "Partial Content";
1268 case 301: return "Moved Permanently"; 1603 case 301: return "Moved Permanently";
1269 case 302: return "Found"; 1604 case 302: return "Found";
1270 case 303: return "See Other"; 1605 case 303: return "See Other";
1271 case 304: return "Not Modified"; 1606 case 304: return "Not Modified";
1272 case 400: return "Bad Request"; 1607 case 400: return "Bad Request";
1608 case 401: return "Unauthorized";
1273 case 403: return "Forbidden"; 1609 case 403: return "Forbidden";
1274 case 404: return "Not Found"; 1610 case 404: return "Not Found";
1275 case 413: return "Payload Too Large"; 1611 case 413: return "Payload Too Large";
1276 case 414: return "Request-URI Too Long"; 1612 case 414: return "Request-URI Too Long";
1277 case 415: return "Unsupported Media Type"; 1613 case 415: return "Unsupported Media Type";
1278 case 416: return "Range Not Satisfiable"; 1614 case 416: return "Range Not Satisfiable";
1615 case 417: return "Expectation Failed";
1616 case 503: return "Service Unavailable";
1279 1617
1280 default: 1618 default:
1281 case 500: return "Internal Server Error"; 1619 case 500: return "Internal Server Error";
@@ -1302,18 +1640,18 @@ inline bool compress(std::string &content) {
1302 if (ret != Z_OK) { return false; } 1640 if (ret != Z_OK) { return false; }
1303 1641
1304 strm.avail_in = content.size(); 1642 strm.avail_in = content.size();
1305 strm.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(content.data())); 1643 strm.next_in =
1644 const_cast<Bytef *>(reinterpret_cast<const Bytef *>(content.data()));
1306 1645
1307 std::string compressed; 1646 std::string compressed;
1308 1647
1309 const auto bufsiz = 16384; 1648 std::array<char, 16384> buff{};
1310 char buff[bufsiz];
1311 do { 1649 do {
1312 strm.avail_out = bufsiz; 1650 strm.avail_out = buff.size();
1313 strm.next_out = reinterpret_cast<Bytef*>(buff); 1651 strm.next_out = reinterpret_cast<Bytef *>(buff.data());
1314 ret = deflate(&strm, Z_FINISH); 1652 ret = deflate(&strm, Z_FINISH);
1315 assert(ret != Z_STREAM_ERROR); 1653 assert(ret != Z_STREAM_ERROR);
1316 compressed.append(buff, bufsiz - strm.avail_out); 1654 compressed.append(buff.data(), buff.size() - strm.avail_out);
1317 } while (strm.avail_out == 0); 1655 } while (strm.avail_out == 0);
1318 1656
1319 assert(ret == Z_STREAM_END); 1657 assert(ret == Z_STREAM_END);
@@ -1347,13 +1685,12 @@ public:
1347 int ret = Z_OK; 1685 int ret = Z_OK;
1348 1686
1349 strm.avail_in = data_length; 1687 strm.avail_in = data_length;
1350 strm.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef *>(data)); 1688 strm.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(data));
1351 1689
1352 const auto bufsiz = 16384; 1690 std::array<char, 16384> buff{};
1353 char buff[bufsiz];
1354 do { 1691 do {
1355 strm.avail_out = bufsiz; 1692 strm.avail_out = buff.size();
1356 strm.next_out = reinterpret_cast<Bytef*>(buff); 1693 strm.next_out = reinterpret_cast<Bytef *>(buff.data());
1357 1694
1358 ret = inflate(&strm, Z_NO_FLUSH); 1695 ret = inflate(&strm, Z_NO_FLUSH);
1359 assert(ret != Z_STREAM_ERROR); 1696 assert(ret != Z_STREAM_ERROR);
@@ -1363,10 +1700,12 @@ public:
1363 case Z_MEM_ERROR: inflateEnd(&strm); return false; 1700 case Z_MEM_ERROR: inflateEnd(&strm); return false;
1364 } 1701 }
1365 1702
1366 if (!callback(buff, bufsiz - strm.avail_out)) { return false; } 1703 if (!callback(buff.data(), buff.size() - strm.avail_out)) {
1704 return false;
1705 }
1367 } while (strm.avail_out == 0); 1706 } while (strm.avail_out == 0);
1368 1707
1369 return ret == Z_STREAM_END; 1708 return ret == Z_OK || ret == Z_STREAM_END;
1370 } 1709 }
1371 1710
1372private: 1711private:
@@ -1397,18 +1736,35 @@ inline uint64_t get_header_value_uint64(const Headers &headers, const char *key,
1397} 1736}
1398 1737
1399inline bool read_headers(Stream &strm, Headers &headers) { 1738inline bool read_headers(Stream &strm, Headers &headers) {
1400 static std::regex re(R"((.+?):\s*(.+?)\s*\r\n)");
1401
1402 const auto bufsiz = 2048; 1739 const auto bufsiz = 2048;
1403 char buf[bufsiz]; 1740 char buf[bufsiz];
1404 1741 stream_line_reader line_reader(strm, buf, bufsiz);
1405 stream_line_reader reader(strm, buf, bufsiz);
1406 1742
1407 for (;;) { 1743 for (;;) {
1408 if (!reader.getline()) { return false; } 1744 if (!line_reader.getline()) { return false; }
1409 if (!strcmp(reader.ptr(), "\r\n")) { break; } 1745
1746 // Check if the line ends with CRLF.
1747 if (line_reader.end_with_crlf()) {
1748 // Blank line indicates end of headers.
1749 if (line_reader.size() == 2) { break; }
1750 } else {
1751 continue; // Skip invalid line.
1752 }
1753
1754 // Skip trailing spaces and tabs.
1755 auto end = line_reader.ptr() + line_reader.size() - 2;
1756 while (line_reader.ptr() < end && (end[-1] == ' ' || end[-1] == '\t')) {
1757 end--;
1758 }
1759
1760 // Horizontal tab and ' ' are considered whitespace and are ignored when on
1761 // the left or right side of the header value:
1762 // - https://stackoverflow.com/questions/50179659/
1763 // - https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html
1764 static const std::regex re(R"((.+?):[\t ]*(.+))");
1765
1410 std::cmatch m; 1766 std::cmatch m;
1411 if (std::regex_match(reader.ptr(), m, re)) { 1767 if (std::regex_match(line_reader.ptr(), end, m, re)) {
1412 auto key = std::string(m[1]); 1768 auto key = std::string(m[1]);
1413 auto val = std::string(m[2]); 1769 auto val = std::string(m[2]);
1414 headers.emplace(key, val); 1770 headers.emplace(key, val);
@@ -1418,12 +1774,8 @@ inline bool read_headers(Stream &strm, Headers &headers) {
1418 return true; 1774 return true;
1419} 1775}
1420 1776
1421typedef std::function<bool(const char *data, size_t data_length)>
1422 ContentReceiverCore;
1423
1424inline bool read_content_with_length(Stream &strm, uint64_t len, 1777inline bool read_content_with_length(Stream &strm, uint64_t len,
1425 Progress progress, 1778 Progress progress, ContentReceiver out) {
1426 ContentReceiverCore out) {
1427 char buf[CPPHTTPLIB_RECV_BUFSIZ]; 1779 char buf[CPPHTTPLIB_RECV_BUFSIZ];
1428 1780
1429 uint64_t r = 0; 1781 uint64_t r = 0;
@@ -1455,7 +1807,7 @@ inline void skip_content_with_length(Stream &strm, uint64_t len) {
1455 } 1807 }
1456} 1808}
1457 1809
1458inline bool read_content_without_length(Stream &strm, ContentReceiverCore out) { 1810inline bool read_content_without_length(Stream &strm, ContentReceiver out) {
1459 char buf[CPPHTTPLIB_RECV_BUFSIZ]; 1811 char buf[CPPHTTPLIB_RECV_BUFSIZ];
1460 for (;;) { 1812 for (;;) {
1461 auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); 1813 auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ);
@@ -1470,33 +1822,34 @@ inline bool read_content_without_length(Stream &strm, ContentReceiverCore out) {
1470 return true; 1822 return true;
1471} 1823}
1472 1824
1473inline bool read_content_chunked(Stream &strm, ContentReceiverCore out) { 1825inline bool read_content_chunked(Stream &strm, ContentReceiver out) {
1474 const auto bufsiz = 16; 1826 const auto bufsiz = 16;
1475 char buf[bufsiz]; 1827 char buf[bufsiz];
1476 1828
1477 stream_line_reader reader(strm, buf, bufsiz); 1829 stream_line_reader line_reader(strm, buf, bufsiz);
1478 1830
1479 if (!reader.getline()) { return false; } 1831 if (!line_reader.getline()) { return false; }
1480 1832
1481 auto chunk_len = std::stoi(reader.ptr(), 0, 16); 1833 auto chunk_len = std::stoi(line_reader.ptr(), 0, 16);
1482 1834
1483 while (chunk_len > 0) { 1835 while (chunk_len > 0) {
1484 if (!read_content_with_length(strm, chunk_len, nullptr, out)) { 1836 if (!read_content_with_length(strm, chunk_len, nullptr, out)) {
1485 return false; 1837 return false;
1486 } 1838 }
1487 1839
1488 if (!reader.getline()) { return false; } 1840 if (!line_reader.getline()) { return false; }
1489 1841
1490 if (strcmp(reader.ptr(), "\r\n")) { break; } 1842 if (strcmp(line_reader.ptr(), "\r\n")) { break; }
1491 1843
1492 if (!reader.getline()) { return false; } 1844 if (!line_reader.getline()) { return false; }
1493 1845
1494 chunk_len = std::stoi(reader.ptr(), 0, 16); 1846 chunk_len = std::stoi(line_reader.ptr(), 0, 16);
1495 } 1847 }
1496 1848
1497 if (chunk_len == 0) { 1849 if (chunk_len == 0) {
1498 // Reader terminator after chunks 1850 // Reader terminator after chunks
1499 if (!reader.getline() || strcmp(reader.ptr(), "\r\n")) return false; 1851 if (!line_reader.getline() || strcmp(line_reader.ptr(), "\r\n"))
1852 return false;
1500 } 1853 }
1501 1854
1502 return true; 1855 return true;
@@ -1509,14 +1862,14 @@ inline bool is_chunked_transfer_encoding(const Headers &headers) {
1509 1862
1510template <typename T> 1863template <typename T>
1511bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, 1864bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
1512 Progress progress, ContentReceiverCore receiver) { 1865 Progress progress, ContentReceiver receiver) {
1513 1866
1514 ContentReceiverCore out = [&](const char *buf, size_t n) { 1867 ContentReceiver out = [&](const char *buf, size_t n) {
1515 return receiver(buf, n); 1868 return receiver(buf, n);
1516 }; 1869 };
1517 1870
1518#ifdef CPPHTTPLIB_ZLIB_SUPPORT 1871#ifdef CPPHTTPLIB_ZLIB_SUPPORT
1519 detail::decompressor decompressor; 1872 decompressor decompressor;
1520 1873
1521 if (!decompressor.is_valid()) { 1874 if (!decompressor.is_valid()) {
1522 status = 500; 1875 status = 500;
@@ -1586,39 +1939,47 @@ inline ssize_t write_content(Stream &strm, ContentProvider content_provider,
1586 size_t end_offset = offset + length; 1939 size_t end_offset = offset + length;
1587 while (offset < end_offset) { 1940 while (offset < end_offset) {
1588 ssize_t written_length = 0; 1941 ssize_t written_length = 0;
1589 content_provider( 1942
1590 offset, end_offset - offset, 1943 DataSink data_sink;
1591 [&](const char *d, size_t l) { 1944 data_sink.write = [&](const char *d, size_t l) {
1592 offset += l; 1945 offset += l;
1593 written_length = strm.write(d, l); 1946 written_length = strm.write(d, l);
1594 }, 1947 };
1595 [&](void) { written_length = -1; }); 1948 data_sink.done = [&](void) { written_length = -1; };
1949 data_sink.is_writable = [&](void) { return strm.is_writable(); };
1950
1951 content_provider(offset, end_offset - offset, data_sink);
1596 if (written_length < 0) { return written_length; } 1952 if (written_length < 0) { return written_length; }
1597 } 1953 }
1598 return static_cast<ssize_t>(offset - begin_offset); 1954 return static_cast<ssize_t>(offset - begin_offset);
1599} 1955}
1600 1956
1957template <typename T>
1601inline ssize_t write_content_chunked(Stream &strm, 1958inline ssize_t write_content_chunked(Stream &strm,
1602 ContentProvider content_provider) { 1959 ContentProvider content_provider,
1960 T is_shutting_down) {
1603 size_t offset = 0; 1961 size_t offset = 0;
1604 auto data_available = true; 1962 auto data_available = true;
1605 ssize_t total_written_length = 0; 1963 ssize_t total_written_length = 0;
1606 while (data_available) { 1964 while (data_available && !is_shutting_down()) {
1607 ssize_t written_length = 0; 1965 ssize_t written_length = 0;
1608 content_provider( 1966
1609 offset, 0, 1967 DataSink data_sink;
1610 [&](const char *d, size_t l) { 1968 data_sink.write = [&](const char *d, size_t l) {
1611 data_available = l > 0; 1969 data_available = l > 0;
1612 offset += l; 1970 offset += l;
1613 1971
1614 // Emit chunked response header and footer for each chunk 1972 // Emit chunked response header and footer for each chunk
1615 auto chunk = from_i_to_hex(l) + "\r\n" + std::string(d, l) + "\r\n"; 1973 auto chunk = from_i_to_hex(l) + "\r\n" + std::string(d, l) + "\r\n";
1616 written_length = strm.write(chunk); 1974 written_length = strm.write(chunk);
1617 }, 1975 };
1618 [&](void) { 1976 data_sink.done = [&](void) {
1619 data_available = false; 1977 data_available = false;
1620 written_length = strm.write("0\r\n\r\n"); 1978 written_length = strm.write("0\r\n\r\n");
1621 }); 1979 };
1980 data_sink.is_writable = [&](void) { return strm.is_writable(); };
1981
1982 content_provider(offset, 0, data_sink);
1622 1983
1623 if (written_length < 0) { return written_length; } 1984 if (written_length < 0) { return written_length; }
1624 total_written_length += written_length; 1985 total_written_length += written_length;
@@ -1629,17 +1990,12 @@ inline ssize_t write_content_chunked(Stream &strm,
1629template <typename T> 1990template <typename T>
1630inline bool redirect(T &cli, const Request &req, Response &res, 1991inline bool redirect(T &cli, const Request &req, Response &res,
1631 const std::string &path) { 1992 const std::string &path) {
1632 Request new_req; 1993 Request new_req = req;
1633 new_req.method = req.method;
1634 new_req.path = path; 1994 new_req.path = path;
1635 new_req.headers = req.headers; 1995 new_req.redirect_count -= 1;
1636 new_req.body = req.body;
1637 new_req.redirect_count = req.redirect_count - 1;
1638 new_req.response_handler = req.response_handler;
1639 new_req.content_receiver = req.content_receiver;
1640 new_req.progress = req.progress;
1641 1996
1642 Response new_res; 1997 Response new_res;
1998
1643 auto ret = cli.send(new_req, new_res); 1999 auto ret = cli.send(new_req, new_res);
1644 if (ret) { res = new_res; } 2000 if (ret) { res = new_res; }
1645 return ret; 2001 return ret;
@@ -1656,7 +2012,7 @@ inline std::string encode_url(const std::string &s) {
1656 case '\n': result += "%0A"; break; 2012 case '\n': result += "%0A"; break;
1657 case '\'': result += "%27"; break; 2013 case '\'': result += "%27"; break;
1658 case ',': result += "%2C"; break; 2014 case ',': result += "%2C"; break;
1659 case ':': result += "%3A"; break; 2015 // case ':': result += "%3A"; break; // ok? probably...
1660 case ';': result += "%3B"; break; 2016 case ';': result += "%3B"; break;
1661 default: 2017 default:
1662 auto c = static_cast<uint8_t>(s[i]); 2018 auto c = static_cast<uint8_t>(s[i]);
@@ -1716,11 +2072,11 @@ inline void parse_query_text(const std::string &s, Params &params) {
1716 split(&s[0], &s[s.size()], '&', [&](const char *b, const char *e) { 2072 split(&s[0], &s[s.size()], '&', [&](const char *b, const char *e) {
1717 std::string key; 2073 std::string key;
1718 std::string val; 2074 std::string val;
1719 split(b, e, '=', [&](const char *b, const char *e) { 2075 split(b, e, '=', [&](const char *b2, const char *e2) {
1720 if (key.empty()) { 2076 if (key.empty()) {
1721 key.assign(b, e); 2077 key.assign(b2, e2);
1722 } else { 2078 } else {
1723 val.assign(b, e); 2079 val.assign(b2, e2);
1724 } 2080 }
1725 }); 2081 });
1726 params.emplace(key, decode_url(val)); 2082 params.emplace(key, decode_url(val));
@@ -1736,112 +2092,207 @@ inline bool parse_multipart_boundary(const std::string &content_type,
1736 return true; 2092 return true;
1737} 2093}
1738 2094
1739inline bool parse_multipart_formdata(const std::string &boundary, 2095inline bool parse_range_header(const std::string &s, Ranges &ranges) {
1740 const std::string &body, 2096 static auto re_first_range = std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))");
1741 MultipartFiles &files) { 2097 std::smatch m;
1742 static std::string dash = "--"; 2098 if (std::regex_match(s, m, re_first_range)) {
1743 static std::string crlf = "\r\n"; 2099 auto pos = m.position(1);
1744 2100 auto len = m.length(1);
1745 static std::regex re_content_type("Content-Type: (.*?)", 2101 bool all_valid_ranges = true;
1746 std::regex_constants::icase); 2102 split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) {
1747 2103 if (!all_valid_ranges) return;
1748 static std::regex re_content_disposition( 2104 static auto re_another_range = std::regex(R"(\s*(\d*)-(\d*))");
1749 "Content-Disposition: form-data; name=\"(.*?)\"(?:; filename=\"(.*?)\")?", 2105 std::cmatch cm;
1750 std::regex_constants::icase); 2106 if (std::regex_match(b, e, cm, re_another_range)) {
1751 2107 ssize_t first = -1;
1752 auto dash_boundary = dash + boundary; 2108 if (!cm.str(1).empty()) {
1753 2109 first = static_cast<ssize_t>(std::stoll(cm.str(1)));
1754 auto pos = body.find(dash_boundary); 2110 }
1755 if (pos != 0) { return false; }
1756
1757 pos += dash_boundary.size();
1758 2111
1759 auto next_pos = body.find(crlf, pos); 2112 ssize_t last = -1;
1760 if (next_pos == std::string::npos) { return false; } 2113 if (!cm.str(2).empty()) {
2114 last = static_cast<ssize_t>(std::stoll(cm.str(2)));
2115 }
1761 2116
1762 pos = next_pos + crlf.size(); 2117 if (first != -1 && last != -1 && first > last) {
2118 all_valid_ranges = false;
2119 return;
2120 }
2121 ranges.emplace_back(std::make_pair(first, last));
2122 }
2123 });
2124 return all_valid_ranges;
2125 }
2126 return false;
2127}
1763 2128
1764 while (pos < body.size()) { 2129class MultipartFormDataParser {
1765 next_pos = body.find(crlf, pos); 2130public:
1766 if (next_pos == std::string::npos) { return false; } 2131 MultipartFormDataParser() {}
1767 2132
1768 std::string name; 2133 void set_boundary(const std::string &boundary) { boundary_ = boundary; }
1769 MultipartFile file;
1770 2134
1771 auto header = body.substr(pos, (next_pos - pos)); 2135 bool is_valid() const { return is_valid_; }
1772 2136
1773 while (pos != next_pos) { 2137 template <typename T, typename U>
1774 std::smatch m; 2138 bool parse(const char *buf, size_t n, T content_callback, U header_callback) {
1775 if (std::regex_match(header, m, re_content_type)) { 2139 static const std::regex re_content_type(R"(^Content-Type:\s*(.*?)\s*$)",
1776 file.content_type = m[1]; 2140 std::regex_constants::icase);
1777 } else if (std::regex_match(header, m, re_content_disposition)) { 2141
1778 name = m[1]; 2142 static const std::regex re_content_disposition(
1779 file.filename = m[2]; 2143 "^Content-Disposition:\\s*form-data;\\s*name=\"(.*?)\"(?:;\\s*filename="
2144 "\"(.*?)\")?\\s*$",
2145 std::regex_constants::icase);
2146
2147 buf_.append(buf, n); // TODO: performance improvement
2148
2149 while (!buf_.empty()) {
2150 switch (state_) {
2151 case 0: { // Initial boundary
2152 auto pattern = dash_ + boundary_ + crlf_;
2153 if (pattern.size() > buf_.size()) { return true; }
2154 auto pos = buf_.find(pattern);
2155 if (pos != 0) {
2156 is_done_ = true;
2157 return false;
2158 }
2159 buf_.erase(0, pattern.size());
2160 off_ += pattern.size();
2161 state_ = 1;
2162 break;
1780 } 2163 }
2164 case 1: { // New entry
2165 clear_file_info();
2166 state_ = 2;
2167 break;
2168 }
2169 case 2: { // Headers
2170 auto pos = buf_.find(crlf_);
2171 while (pos != std::string::npos) {
2172 // Empty line
2173 if (pos == 0) {
2174 if (!header_callback(file_)) {
2175 is_valid_ = false;
2176 is_done_ = false;
2177 return false;
2178 }
2179 buf_.erase(0, crlf_.size());
2180 off_ += crlf_.size();
2181 state_ = 3;
2182 break;
2183 }
1781 2184
1782 pos = next_pos + crlf.size(); 2185 auto header = buf_.substr(0, pos);
1783 2186 {
1784 next_pos = body.find(crlf, pos); 2187 std::smatch m;
1785 if (next_pos == std::string::npos) { return false; } 2188 if (std::regex_match(header, m, re_content_type)) {
1786 2189 file_.content_type = m[1];
1787 header = body.substr(pos, (next_pos - pos)); 2190 } else if (std::regex_match(header, m, re_content_disposition)) {
1788 } 2191 file_.name = m[1];
1789 2192 file_.filename = m[2];
1790 pos = next_pos + crlf.size(); 2193 }
2194 }
1791 2195
1792 next_pos = body.find(crlf + dash_boundary, pos); 2196 buf_.erase(0, pos + crlf_.size());
2197 off_ += pos + crlf_.size();
2198 pos = buf_.find(crlf_);
2199 }
2200 break;
2201 }
2202 case 3: { // Body
2203 {
2204 auto pattern = crlf_ + dash_;
2205 if (pattern.size() > buf_.size()) { return true; }
2206
2207 auto pos = buf_.find(pattern);
2208 if (pos == std::string::npos) { pos = buf_.size(); }
2209 if (!content_callback(buf_.data(), pos)) {
2210 is_valid_ = false;
2211 is_done_ = false;
2212 return false;
2213 }
1793 2214
1794 if (next_pos == std::string::npos) { return false; } 2215 off_ += pos;
2216 buf_.erase(0, pos);
2217 }
1795 2218
1796 file.offset = pos; 2219 {
1797 file.length = next_pos - pos; 2220 auto pattern = crlf_ + dash_ + boundary_;
2221 if (pattern.size() > buf_.size()) { return true; }
2222
2223 auto pos = buf_.find(pattern);
2224 if (pos != std::string::npos) {
2225 if (!content_callback(buf_.data(), pos)) {
2226 is_valid_ = false;
2227 is_done_ = false;
2228 return false;
2229 }
1798 2230
1799 pos = next_pos + crlf.size() + dash_boundary.size(); 2231 off_ += pos + pattern.size();
2232 buf_.erase(0, pos + pattern.size());
2233 state_ = 4;
2234 } else {
2235 if (!content_callback(buf_.data(), pattern.size())) {
2236 is_valid_ = false;
2237 is_done_ = false;
2238 return false;
2239 }
1800 2240
1801 next_pos = body.find(crlf, pos); 2241 off_ += pattern.size();
1802 if (next_pos == std::string::npos) { return false; } 2242 buf_.erase(0, pattern.size());
2243 }
2244 }
2245 break;
2246 }
2247 case 4: { // Boundary
2248 if (crlf_.size() > buf_.size()) { return true; }
2249 if (buf_.find(crlf_) == 0) {
2250 buf_.erase(0, crlf_.size());
2251 off_ += crlf_.size();
2252 state_ = 1;
2253 } else {
2254 auto pattern = dash_ + crlf_;
2255 if (pattern.size() > buf_.size()) { return true; }
2256 if (buf_.find(pattern) == 0) {
2257 buf_.erase(0, pattern.size());
2258 off_ += pattern.size();
2259 is_valid_ = true;
2260 state_ = 5;
2261 } else {
2262 is_done_ = true;
2263 return true;
2264 }
2265 }
2266 break;
2267 }
2268 case 5: { // Done
2269 is_valid_ = false;
2270 return false;
2271 }
2272 }
2273 }
1803 2274
1804 files.emplace(name, file); 2275 return true;
2276 }
1805 2277
1806 pos = next_pos + crlf.size(); 2278private:
2279 void clear_file_info() {
2280 file_.name.clear();
2281 file_.filename.clear();
2282 file_.content_type.clear();
1807 } 2283 }
1808 2284
1809 return true; 2285 const std::string dash_ = "--";
1810} 2286 const std::string crlf_ = "\r\n";
2287 std::string boundary_;
1811 2288
1812inline bool parse_range_header(const std::string &s, Ranges &ranges) { 2289 std::string buf_;
1813 try { 2290 size_t state_ = 0;
1814 static auto re = std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))"); 2291 size_t is_valid_ = false;
1815 std::smatch m; 2292 size_t is_done_ = false;
1816 if (std::regex_match(s, m, re)) { 2293 size_t off_ = 0;
1817 auto pos = m.position(1); 2294 MultipartFormData file_;
1818 auto len = m.length(1); 2295};
1819 detail::split(&s[pos], &s[pos + len], ',',
1820 [&](const char *b, const char *e) {
1821 static auto re = std::regex(R"(\s*(\d*)-(\d*))");
1822 std::cmatch m;
1823 if (std::regex_match(b, e, m, re)) {
1824 ssize_t first = -1;
1825 if (!m.str(1).empty()) {
1826 first = static_cast<ssize_t>(std::stoll(m.str(1)));
1827 }
1828
1829 ssize_t last = -1;
1830 if (!m.str(2).empty()) {
1831 last = static_cast<ssize_t>(std::stoll(m.str(2)));
1832 }
1833
1834 if (first != -1 && last != -1 && first > last) {
1835 throw std::runtime_error("invalid range error");
1836 }
1837 ranges.emplace_back(std::make_pair(first, last));
1838 }
1839 });
1840 return true;
1841 }
1842 return false;
1843 } catch (...) { return false; }
1844}
1845 2296
1846inline std::string to_lower(const char *beg, const char *end) { 2297inline std::string to_lower(const char *beg, const char *end) {
1847 std::string out; 2298 std::string out;
@@ -1915,7 +2366,7 @@ bool process_multipart_ranges_data(const Request &req, Response &res,
1915 ctoken("\r\n"); 2366 ctoken("\r\n");
1916 } 2367 }
1917 2368
1918 auto offsets = detail::get_range_offset_and_length(req, res.body.size(), i); 2369 auto offsets = get_range_offset_and_length(req, res.body.size(), i);
1919 auto offset = offsets.first; 2370 auto offset = offsets.first;
1920 auto length = offsets.second; 2371 auto length = offsets.second;
1921 2372
@@ -1978,8 +2429,7 @@ inline bool write_multipart_ranges_data(Stream &strm, const Request &req,
1978 [&](const std::string &token) { strm.write(token); }, 2429 [&](const std::string &token) { strm.write(token); },
1979 [&](const char *token) { strm.write(token); }, 2430 [&](const char *token) { strm.write(token); },
1980 [&](size_t offset, size_t length) { 2431 [&](size_t offset, size_t length) {
1981 return detail::write_content(strm, res.content_provider, offset, 2432 return write_content(strm, res.content_provider, offset, length) >= 0;
1982 length) >= 0;
1983 }); 2433 });
1984} 2434}
1985 2435
@@ -1988,11 +2438,56 @@ get_range_offset_and_length(const Request &req, const Response &res,
1988 size_t index) { 2438 size_t index) {
1989 auto r = req.ranges[index]; 2439 auto r = req.ranges[index];
1990 2440
1991 if (r.second == -1) { r.second = res.content_provider_resource_length - 1; } 2441 if (r.second == -1) { r.second = res.content_length - 1; }
1992 2442
1993 return std::make_pair(r.first, r.second - r.first + 1); 2443 return std::make_pair(r.first, r.second - r.first + 1);
1994} 2444}
1995 2445
2446inline bool expect_content(const Request &req) {
2447 if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" ||
2448 req.method == "PRI") {
2449 return true;
2450 }
2451 // TODO: check if Content-Length is set
2452 return false;
2453}
2454
2455#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2456template <typename CTX, typename Init, typename Update, typename Final>
2457inline std::string message_digest(const std::string &s, Init init,
2458 Update update, Final final,
2459 size_t digest_length) {
2460 using namespace std;
2461
2462 std::vector<unsigned char> md(digest_length, 0);
2463 CTX ctx;
2464 init(&ctx);
2465 update(&ctx, s.data(), s.size());
2466 final(md.data(), &ctx);
2467
2468 stringstream ss;
2469 for (auto c : md) {
2470 ss << setfill('0') << setw(2) << hex << (unsigned int)c;
2471 }
2472 return ss.str();
2473}
2474
2475inline std::string MD5(const std::string &s) {
2476 return message_digest<MD5_CTX>(s, MD5_Init, MD5_Update, MD5_Final,
2477 MD5_DIGEST_LENGTH);
2478}
2479
2480inline std::string SHA_256(const std::string &s) {
2481 return message_digest<SHA256_CTX>(s, SHA256_Init, SHA256_Update, SHA256_Final,
2482 SHA256_DIGEST_LENGTH);
2483}
2484
2485inline std::string SHA_512(const std::string &s) {
2486 return message_digest<SHA512_CTX>(s, SHA512_Init, SHA512_Update, SHA512_Final,
2487 SHA512_DIGEST_LENGTH);
2488}
2489#endif
2490
1996#ifdef _WIN32 2491#ifdef _WIN32
1997class WSInit { 2492class WSInit {
1998public: 2493public:
@@ -2025,9 +2520,103 @@ inline std::pair<std::string, std::string> make_range_header(Ranges ranges) {
2025 2520
2026inline std::pair<std::string, std::string> 2521inline std::pair<std::string, std::string>
2027make_basic_authentication_header(const std::string &username, 2522make_basic_authentication_header(const std::string &username,
2028 const std::string &password) { 2523 const std::string &password,
2524 bool is_proxy = false) {
2029 auto field = "Basic " + detail::base64_encode(username + ":" + password); 2525 auto field = "Basic " + detail::base64_encode(username + ":" + password);
2030 return std::make_pair("Authorization", field); 2526 auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
2527 return std::make_pair(key, field);
2528}
2529
2530#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2531inline std::pair<std::string, std::string> make_digest_authentication_header(
2532 const Request &req, const std::map<std::string, std::string> &auth,
2533 size_t cnonce_count, const std::string &cnonce, const std::string &username,
2534 const std::string &password, bool is_proxy = false) {
2535 using namespace std;
2536
2537 string nc;
2538 {
2539 stringstream ss;
2540 ss << setfill('0') << setw(8) << hex << cnonce_count;
2541 nc = ss.str();
2542 }
2543
2544 auto qop = auth.at("qop");
2545 if (qop.find("auth-int") != std::string::npos) {
2546 qop = "auth-int";
2547 } else {
2548 qop = "auth";
2549 }
2550
2551 std::string algo = "MD5";
2552 if (auth.find("algorithm") != auth.end()) { algo = auth.at("algorithm"); }
2553
2554 string response;
2555 {
2556 auto H = algo == "SHA-256"
2557 ? detail::SHA_256
2558 : algo == "SHA-512" ? detail::SHA_512 : detail::MD5;
2559
2560 auto A1 = username + ":" + auth.at("realm") + ":" + password;
2561
2562 auto A2 = req.method + ":" + req.path;
2563 if (qop == "auth-int") { A2 += ":" + H(req.body); }
2564
2565 response = H(H(A1) + ":" + auth.at("nonce") + ":" + nc + ":" + cnonce +
2566 ":" + qop + ":" + H(A2));
2567 }
2568
2569 auto field = "Digest username=\"hello\", realm=\"" + auth.at("realm") +
2570 "\", nonce=\"" + auth.at("nonce") + "\", uri=\"" + req.path +
2571 "\", algorithm=" + algo + ", qop=" + qop + ", nc=\"" + nc +
2572 "\", cnonce=\"" + cnonce + "\", response=\"" + response + "\"";
2573
2574 auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
2575 return std::make_pair(key, field);
2576}
2577#endif
2578
2579inline bool parse_www_authenticate(const httplib::Response &res,
2580 std::map<std::string, std::string> &auth,
2581 bool is_proxy) {
2582 auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate";
2583 if (res.has_header(auth_key)) {
2584 static auto re = std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~");
2585 auto s = res.get_header_value(auth_key);
2586 auto pos = s.find(' ');
2587 if (pos != std::string::npos) {
2588 auto type = s.substr(0, pos);
2589 if (type == "Basic") {
2590 return false;
2591 } else if (type == "Digest") {
2592 s = s.substr(pos + 1);
2593 auto beg = std::sregex_iterator(s.begin(), s.end(), re);
2594 for (auto i = beg; i != std::sregex_iterator(); ++i) {
2595 auto m = *i;
2596 auto key = s.substr(m.position(1), m.length(1));
2597 auto val = m.length(2) > 0 ? s.substr(m.position(2), m.length(2))
2598 : s.substr(m.position(3), m.length(3));
2599 auth[key] = val;
2600 }
2601 return true;
2602 }
2603 }
2604 }
2605 return false;
2606}
2607
2608// https://stackoverflow.com/questions/440133/how-do-i-create-a-random-alpha-numeric-string-in-c/440240#answer-440240
2609inline std::string random_string(size_t length) {
2610 auto randchar = []() -> char {
2611 const char charset[] = "0123456789"
2612 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2613 "abcdefghijklmnopqrstuvwxyz";
2614 const size_t max_index = (sizeof(charset) - 1);
2615 return charset[rand() % max_index];
2616 };
2617 std::string str(length, 0);
2618 std::generate_n(str.begin(), length, randchar);
2619 return str;
2031} 2620}
2032 2621
2033// Request implementation 2622// Request implementation
@@ -2068,14 +2657,19 @@ inline size_t Request::get_param_value_count(const char *key) const {
2068 return std::distance(r.first, r.second); 2657 return std::distance(r.first, r.second);
2069} 2658}
2070 2659
2660inline bool Request::is_multipart_form_data() const {
2661 const auto &content_type = get_header_value("Content-Type");
2662 return !content_type.find("multipart/form-data");
2663}
2664
2071inline bool Request::has_file(const char *key) const { 2665inline bool Request::has_file(const char *key) const {
2072 return files.find(key) != files.end(); 2666 return files.find(key) != files.end();
2073} 2667}
2074 2668
2075inline MultipartFile Request::get_file_value(const char *key) const { 2669inline MultipartFormData Request::get_file_value(const char *key) const {
2076 auto it = files.find(key); 2670 auto it = files.find(key);
2077 if (it != files.end()) { return it->second; } 2671 if (it != files.end()) { return it->second; }
2078 return MultipartFile(); 2672 return MultipartFormData();
2079} 2673}
2080 2674
2081// Response implementation 2675// Response implementation
@@ -2119,40 +2713,47 @@ inline void Response::set_content(const std::string &s,
2119} 2713}
2120 2714
2121inline void Response::set_content_provider( 2715inline void Response::set_content_provider(
2122 size_t length, 2716 size_t in_length,
2123 std::function<void(size_t offset, size_t length, DataSink sink)> provider, 2717 std::function<void(size_t offset, size_t length, DataSink &sink)> provider,
2124 std::function<void()> resource_releaser) { 2718 std::function<void()> resource_releaser) {
2125 assert(length > 0); 2719 assert(in_length > 0);
2126 content_provider_resource_length = length; 2720 content_length = in_length;
2127 content_provider = [provider](size_t offset, size_t length, DataSink sink, 2721 content_provider = [provider](size_t offset, size_t length, DataSink &sink) {
2128 Done) { provider(offset, length, sink); }; 2722 provider(offset, length, sink);
2723 };
2129 content_provider_resource_releaser = resource_releaser; 2724 content_provider_resource_releaser = resource_releaser;
2130} 2725}
2131 2726
2132inline void Response::set_chunked_content_provider( 2727inline void Response::set_chunked_content_provider(
2133 std::function<void(size_t offset, DataSink sink, Done done)> provider, 2728 std::function<void(size_t offset, DataSink &sink)> provider,
2134 std::function<void()> resource_releaser) { 2729 std::function<void()> resource_releaser) {
2135 content_provider_resource_length = 0; 2730 content_length = 0;
2136 content_provider = [provider](size_t offset, size_t, DataSink sink, 2731 content_provider = [provider](size_t offset, size_t, DataSink &sink) {
2137 Done done) { provider(offset, sink, done); }; 2732 provider(offset, sink);
2733 };
2138 content_provider_resource_releaser = resource_releaser; 2734 content_provider_resource_releaser = resource_releaser;
2139} 2735}
2140 2736
2141// Rstream implementation 2737// Rstream implementation
2738inline int Stream::write(const char *ptr) { return write(ptr, strlen(ptr)); }
2739
2740inline int Stream::write(const std::string &s) {
2741 return write(s.data(), s.size());
2742}
2743
2142template <typename... Args> 2744template <typename... Args>
2143inline int Stream::write_format(const char *fmt, const Args &... args) { 2745inline int Stream::write_format(const char *fmt, const Args &... args) {
2144 const auto bufsiz = 2048; 2746 std::array<char, 2048> buf;
2145 char buf[bufsiz];
2146 2747
2147#if defined(_MSC_VER) && _MSC_VER < 1900 2748#if defined(_MSC_VER) && _MSC_VER < 1900
2148 auto n = _snprintf_s(buf, bufsiz, bufsiz - 1, fmt, args...); 2749 auto n = _snprintf_s(buf, bufsiz, buf.size() - 1, fmt, args...);
2149#else 2750#else
2150 auto n = snprintf(buf, bufsiz - 1, fmt, args...); 2751 auto n = snprintf(buf.data(), buf.size() - 1, fmt, args...);
2151#endif 2752#endif
2152 if (n <= 0) { return n; } 2753 if (n <= 0) { return n; }
2153 2754
2154 if (n >= bufsiz - 1) { 2755 if (n >= static_cast<int>(buf.size()) - 1) {
2155 std::vector<char> glowable_buf(bufsiz); 2756 std::vector<char> glowable_buf(buf.size());
2156 2757
2157 while (n >= static_cast<int>(glowable_buf.size() - 1)) { 2758 while (n >= static_cast<int>(glowable_buf.size() - 1)) {
2158 glowable_buf.resize(glowable_buf.size() * 2); 2759 glowable_buf.resize(glowable_buf.size() * 2);
@@ -2165,33 +2766,36 @@ inline int Stream::write_format(const char *fmt, const Args &... args) {
2165 } 2766 }
2166 return write(&glowable_buf[0], n); 2767 return write(&glowable_buf[0], n);
2167 } else { 2768 } else {
2168 return write(buf, n); 2769 return write(buf.data(), n);
2169 } 2770 }
2170} 2771}
2171 2772
2773namespace detail {
2774
2172// Socket stream implementation 2775// Socket stream implementation
2173inline SocketStream::SocketStream(socket_t sock) : sock_(sock) {} 2776inline SocketStream::SocketStream(socket_t sock, time_t read_timeout_sec,
2777 time_t read_timeout_usec)
2778 : sock_(sock), read_timeout_sec_(read_timeout_sec),
2779 read_timeout_usec_(read_timeout_usec) {}
2174 2780
2175inline SocketStream::~SocketStream() {} 2781inline SocketStream::~SocketStream() {}
2176 2782
2177inline int SocketStream::read(char *ptr, size_t size) { 2783inline bool SocketStream::is_readable() const {
2178 if (detail::select_read(sock_, CPPHTTPLIB_READ_TIMEOUT_SECOND, 2784 return detail::select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0;
2179 CPPHTTPLIB_READ_TIMEOUT_USECOND) > 0) {
2180 return recv(sock_, ptr, static_cast<int>(size), 0);
2181 }
2182 return -1;
2183} 2785}
2184 2786
2185inline int SocketStream::write(const char *ptr, size_t size) { 2787inline bool SocketStream::is_writable() const {
2186 return send(sock_, ptr, static_cast<int>(size), 0); 2788 return detail::select_write(sock_, 0, 0) > 0;
2187} 2789}
2188 2790
2189inline int SocketStream::write(const char *ptr) { 2791inline int SocketStream::read(char *ptr, size_t size) {
2190 return write(ptr, strlen(ptr)); 2792 if (is_readable()) { return recv(sock_, ptr, static_cast<int>(size), 0); }
2793 return -1;
2191} 2794}
2192 2795
2193inline int SocketStream::write(const std::string &s) { 2796inline int SocketStream::write(const char *ptr, size_t size) {
2194 return write(s.data(), s.size()); 2797 if (is_writable()) { return send(sock_, ptr, static_cast<int>(size), 0); }
2798 return -1;
2195} 2799}
2196 2800
2197inline std::string SocketStream::get_remote_addr() const { 2801inline std::string SocketStream::get_remote_addr() const {
@@ -2199,12 +2803,18 @@ inline std::string SocketStream::get_remote_addr() const {
2199} 2803}
2200 2804
2201// Buffer stream implementation 2805// Buffer stream implementation
2806inline bool BufferStream::is_readable() const { return true; }
2807
2808inline bool BufferStream::is_writable() const { return true; }
2809
2202inline int BufferStream::read(char *ptr, size_t size) { 2810inline int BufferStream::read(char *ptr, size_t size) {
2203#if defined(_MSC_VER) && _MSC_VER < 1900 2811#if defined(_MSC_VER) && _MSC_VER < 1900
2204 return static_cast<int>(buffer._Copy_s(ptr, size, size)); 2812 int len_read = static_cast<int>(buffer._Copy_s(ptr, size, size, position));
2205#else 2813#else
2206 return static_cast<int>(buffer.copy(ptr, size)); 2814 int len_read = static_cast<int>(buffer.copy(ptr, size, position));
2207#endif 2815#endif
2816 position += len_read;
2817 return len_read;
2208} 2818}
2209 2819
2210inline int BufferStream::write(const char *ptr, size_t size) { 2820inline int BufferStream::write(const char *ptr, size_t size) {
@@ -2212,33 +2822,23 @@ inline int BufferStream::write(const char *ptr, size_t size) {
2212 return static_cast<int>(size); 2822 return static_cast<int>(size);
2213} 2823}
2214 2824
2215inline int BufferStream::write(const char *ptr) {
2216 return write(ptr, strlen(ptr));
2217}
2218
2219inline int BufferStream::write(const std::string &s) {
2220 return write(s.data(), s.size());
2221}
2222
2223inline std::string BufferStream::get_remote_addr() const { return ""; } 2825inline std::string BufferStream::get_remote_addr() const { return ""; }
2224 2826
2225inline const std::string &BufferStream::get_buffer() const { return buffer; } 2827inline const std::string &BufferStream::get_buffer() const { return buffer; }
2226 2828
2829} // namespace detail
2830
2227// HTTP server implementation 2831// HTTP server implementation
2228inline Server::Server() 2832inline Server::Server()
2229 : keep_alive_max_count_(CPPHTTPLIB_KEEPALIVE_MAX_COUNT), 2833 : keep_alive_max_count_(CPPHTTPLIB_KEEPALIVE_MAX_COUNT),
2834 read_timeout_sec_(CPPHTTPLIB_READ_TIMEOUT_SECOND),
2835 read_timeout_usec_(CPPHTTPLIB_READ_TIMEOUT_USECOND),
2230 payload_max_length_(CPPHTTPLIB_PAYLOAD_MAX_LENGTH), is_running_(false), 2836 payload_max_length_(CPPHTTPLIB_PAYLOAD_MAX_LENGTH), is_running_(false),
2231 svr_sock_(INVALID_SOCKET) { 2837 svr_sock_(INVALID_SOCKET) {
2232#ifndef _WIN32 2838#ifndef _WIN32
2233 signal(SIGPIPE, SIG_IGN); 2839 signal(SIGPIPE, SIG_IGN);
2234#endif 2840#endif
2235 new_task_queue = [] { 2841 new_task_queue = [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); };
2236#if CPPHTTPLIB_THREAD_POOL_COUNT > 0
2237 return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT);
2238#else
2239 return new Threads();
2240#endif
2241 };
2242} 2842}
2243 2843
2244inline Server::~Server() {} 2844inline Server::~Server() {}
@@ -2253,16 +2853,37 @@ inline Server &Server::Post(const char *pattern, Handler handler) {
2253 return *this; 2853 return *this;
2254} 2854}
2255 2855
2856inline Server &Server::Post(const char *pattern,
2857 HandlerWithContentReader handler) {
2858 post_handlers_for_content_reader_.push_back(
2859 std::make_pair(std::regex(pattern), handler));
2860 return *this;
2861}
2862
2256inline Server &Server::Put(const char *pattern, Handler handler) { 2863inline Server &Server::Put(const char *pattern, Handler handler) {
2257 put_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); 2864 put_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
2258 return *this; 2865 return *this;
2259} 2866}
2260 2867
2868inline Server &Server::Put(const char *pattern,
2869 HandlerWithContentReader handler) {
2870 put_handlers_for_content_reader_.push_back(
2871 std::make_pair(std::regex(pattern), handler));
2872 return *this;
2873}
2874
2261inline Server &Server::Patch(const char *pattern, Handler handler) { 2875inline Server &Server::Patch(const char *pattern, Handler handler) {
2262 patch_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); 2876 patch_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
2263 return *this; 2877 return *this;
2264} 2878}
2265 2879
2880inline Server &Server::Patch(const char *pattern,
2881 HandlerWithContentReader handler) {
2882 patch_handlers_for_content_reader_.push_back(
2883 std::make_pair(std::regex(pattern), handler));
2884 return *this;
2885}
2886
2266inline Server &Server::Delete(const char *pattern, Handler handler) { 2887inline Server &Server::Delete(const char *pattern, Handler handler) {
2267 delete_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); 2888 delete_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
2268 return *this; 2889 return *this;
@@ -2273,32 +2894,68 @@ inline Server &Server::Options(const char *pattern, Handler handler) {
2273 return *this; 2894 return *this;
2274} 2895}
2275 2896
2276inline bool Server::set_base_dir(const char *path) { 2897inline bool Server::set_base_dir(const char *dir, const char *mount_point) {
2277 if (detail::is_dir(path)) { 2898 return set_mount_point(mount_point, dir);
2278 base_dir_ = path; 2899}
2279 return true; 2900
2901inline bool Server::set_mount_point(const char *mount_point, const char *dir) {
2902 if (detail::is_dir(dir)) {
2903 std::string mnt = mount_point ? mount_point : "/";
2904 if (!mnt.empty() && mnt[0] == '/') {
2905 base_dirs_.emplace_back(mnt, dir);
2906 return true;
2907 }
2908 }
2909 return false;
2910}
2911
2912inline bool Server::remove_mount_point(const char *mount_point) {
2913 for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) {
2914 if (it->first == mount_point) {
2915 base_dirs_.erase(it);
2916 return true;
2917 }
2280 } 2918 }
2281 return false; 2919 return false;
2282} 2920}
2283 2921
2922inline void Server::set_file_extension_and_mimetype_mapping(const char *ext,
2923 const char *mime) {
2924 file_extension_and_mimetype_map_[ext] = mime;
2925}
2926
2284inline void Server::set_file_request_handler(Handler handler) { 2927inline void Server::set_file_request_handler(Handler handler) {
2285 file_request_handler_ = handler; 2928 file_request_handler_ = std::move(handler);
2286} 2929}
2287 2930
2288inline void Server::set_error_handler(Handler handler) { 2931inline void Server::set_error_handler(Handler handler) {
2289 error_handler_ = handler; 2932 error_handler_ = std::move(handler);
2290} 2933}
2291 2934
2292inline void Server::set_logger(Logger logger) { logger_ = logger; } 2935inline void Server::set_logger(Logger logger) { logger_ = std::move(logger); }
2936
2937inline void
2938Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) {
2939 expect_100_continue_handler_ = std::move(handler);
2940}
2293 2941
2294inline void Server::set_keep_alive_max_count(size_t count) { 2942inline void Server::set_keep_alive_max_count(size_t count) {
2295 keep_alive_max_count_ = count; 2943 keep_alive_max_count_ = count;
2296} 2944}
2297 2945
2946inline void Server::set_read_timeout(time_t sec, time_t usec) {
2947 read_timeout_sec_ = sec;
2948 read_timeout_usec_ = usec;
2949}
2950
2298inline void Server::set_payload_max_length(size_t length) { 2951inline void Server::set_payload_max_length(size_t length) {
2299 payload_max_length_ = length; 2952 payload_max_length_ = length;
2300} 2953}
2301 2954
2955inline bool Server::bind_to_port(const char *host, int port, int socket_flags) {
2956 if (bind_internal(host, port, socket_flags) < 0) return false;
2957 return true;
2958}
2302inline int Server::bind_to_any_port(const char *host, int socket_flags) { 2959inline int Server::bind_to_any_port(const char *host, int socket_flags) {
2303 return bind_internal(host, 0, socket_flags); 2960 return bind_internal(host, 0, socket_flags);
2304} 2961}
@@ -2306,8 +2963,7 @@ inline int Server::bind_to_any_port(const char *host, int socket_flags) {
2306inline bool Server::listen_after_bind() { return listen_internal(); } 2963inline bool Server::listen_after_bind() { return listen_internal(); }
2307 2964
2308inline bool Server::listen(const char *host, int port, int socket_flags) { 2965inline bool Server::listen(const char *host, int port, int socket_flags) {
2309 if (bind_internal(host, port, socket_flags) < 0) return false; 2966 return bind_to_port(host, port, socket_flags) && listen_internal();
2310 return listen_internal();
2311} 2967}
2312 2968
2313inline bool Server::is_running() const { return is_running_; } 2969inline bool Server::is_running() const { return is_running_; }
@@ -2322,8 +2978,9 @@ inline void Server::stop() {
2322} 2978}
2323 2979
2324inline bool Server::parse_request_line(const char *s, Request &req) { 2980inline bool Server::parse_request_line(const char *s, Request &req) {
2325 static std::regex re("(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH|PRI) " 2981 const static std::regex re(
2326 "(([^?]+)(?:\\?(.+?))?) (HTTP/1\\.[01])\r\n"); 2982 "(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH|PRI) "
2983 "(([^?]+)(?:\\?(.*?))?) (HTTP/1\\.[01])\r\n");
2327 2984
2328 std::cmatch m; 2985 std::cmatch m;
2329 if (std::regex_match(s, m, re)) { 2986 if (std::regex_match(s, m, re)) {
@@ -2348,9 +3005,11 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
2348 3005
2349 if (400 <= res.status && error_handler_) { error_handler_(req, res); } 3006 if (400 <= res.status && error_handler_) { error_handler_(req, res); }
2350 3007
3008 detail::BufferStream bstrm;
3009
2351 // Response line 3010 // Response line
2352 if (!strm.write_format("HTTP/1.1 %d %s\r\n", res.status, 3011 if (!bstrm.write_format("HTTP/1.1 %d %s\r\n", res.status,
2353 detail::status_message(res.status))) { 3012 detail::status_message(res.status))) {
2354 return false; 3013 return false;
2355 } 3014 }
2356 3015
@@ -2363,11 +3022,12 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
2363 res.set_header("Connection", "Keep-Alive"); 3022 res.set_header("Connection", "Keep-Alive");
2364 } 3023 }
2365 3024
2366 if (!res.has_header("Content-Type")) { 3025 if (!res.has_header("Content-Type") &&
3026 (!res.body.empty() || res.content_length > 0)) {
2367 res.set_header("Content-Type", "text/plain"); 3027 res.set_header("Content-Type", "text/plain");
2368 } 3028 }
2369 3029
2370 if (!res.has_header("Accept-Ranges")) { 3030 if (!res.has_header("Accept-Ranges") && req.method == "HEAD") {
2371 res.set_header("Accept-Ranges", "bytes"); 3031 res.set_header("Accept-Ranges", "bytes");
2372 } 3032 }
2373 3033
@@ -2388,17 +3048,17 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
2388 } 3048 }
2389 3049
2390 if (res.body.empty()) { 3050 if (res.body.empty()) {
2391 if (res.content_provider_resource_length > 0) { 3051 if (res.content_length > 0) {
2392 size_t length = 0; 3052 size_t length = 0;
2393 if (req.ranges.empty()) { 3053 if (req.ranges.empty()) {
2394 length = res.content_provider_resource_length; 3054 length = res.content_length;
2395 } else if (req.ranges.size() == 1) { 3055 } else if (req.ranges.size() == 1) {
2396 auto offsets = detail::get_range_offset_and_length( 3056 auto offsets =
2397 req, res.content_provider_resource_length, 0); 3057 detail::get_range_offset_and_length(req, res.content_length, 0);
2398 auto offset = offsets.first; 3058 auto offset = offsets.first;
2399 length = offsets.second; 3059 length = offsets.second;
2400 auto content_range = detail::make_content_range_header_field( 3060 auto content_range = detail::make_content_range_header_field(
2401 offset, length, res.content_provider_resource_length); 3061 offset, length, res.content_length);
2402 res.set_header("Content-Range", content_range); 3062 res.set_header("Content-Range", content_range);
2403 } else { 3063 } else {
2404 length = detail::get_multipart_ranges_data_length(req, res, boundary, 3064 length = detail::get_multipart_ranges_data_length(req, res, boundary,
@@ -2430,7 +3090,7 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
2430 } 3090 }
2431 3091
2432#ifdef CPPHTTPLIB_ZLIB_SUPPORT 3092#ifdef CPPHTTPLIB_ZLIB_SUPPORT
2433 // TODO: 'Accpet-Encoding' has gzip, not gzip;q=0 3093 // TODO: 'Accept-Encoding' has gzip, not gzip;q=0
2434 const auto &encodings = req.get_header_value("Accept-Encoding"); 3094 const auto &encodings = req.get_header_value("Accept-Encoding");
2435 if (encodings.find("gzip") != std::string::npos && 3095 if (encodings.find("gzip") != std::string::npos &&
2436 detail::can_compress(res.get_header_value("Content-Type"))) { 3096 detail::can_compress(res.get_header_value("Content-Type"))) {
@@ -2444,7 +3104,11 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
2444 res.set_header("Content-Length", length); 3104 res.set_header("Content-Length", length);
2445 } 3105 }
2446 3106
2447 if (!detail::write_headers(strm, res, Headers())) { return false; } 3107 if (!detail::write_headers(bstrm, res, Headers())) { return false; }
3108
3109 // Flush buffer
3110 auto &data = bstrm.get_buffer();
3111 strm.write(data.data(), data.size());
2448 3112
2449 // Body 3113 // Body
2450 if (req.method != "HEAD") { 3114 if (req.method != "HEAD") {
@@ -2468,15 +3132,15 @@ inline bool
2468Server::write_content_with_provider(Stream &strm, const Request &req, 3132Server::write_content_with_provider(Stream &strm, const Request &req,
2469 Response &res, const std::string &boundary, 3133 Response &res, const std::string &boundary,
2470 const std::string &content_type) { 3134 const std::string &content_type) {
2471 if (res.content_provider_resource_length) { 3135 if (res.content_length) {
2472 if (req.ranges.empty()) { 3136 if (req.ranges.empty()) {
2473 if (detail::write_content(strm, res.content_provider, 0, 3137 if (detail::write_content(strm, res.content_provider, 0,
2474 res.content_provider_resource_length) < 0) { 3138 res.content_length) < 0) {
2475 return false; 3139 return false;
2476 } 3140 }
2477 } else if (req.ranges.size() == 1) { 3141 } else if (req.ranges.size() == 1) {
2478 auto offsets = detail::get_range_offset_and_length( 3142 auto offsets =
2479 req, res.content_provider_resource_length, 0); 3143 detail::get_range_offset_and_length(req, res.content_length, 0);
2480 auto offset = offsets.first; 3144 auto offset = offsets.first;
2481 auto length = offsets.second; 3145 auto length = offsets.second;
2482 if (detail::write_content(strm, res.content_provider, offset, length) < 3146 if (detail::write_content(strm, res.content_provider, offset, length) <
@@ -2490,29 +3154,123 @@ Server::write_content_with_provider(Stream &strm, const Request &req,
2490 } 3154 }
2491 } 3155 }
2492 } else { 3156 } else {
2493 if (detail::write_content_chunked(strm, res.content_provider) < 0) { 3157 auto is_shutting_down = [this]() {
3158 return this->svr_sock_ == INVALID_SOCKET;
3159 };
3160 if (detail::write_content_chunked(strm, res.content_provider,
3161 is_shutting_down) < 0) {
2494 return false; 3162 return false;
2495 } 3163 }
2496 } 3164 }
2497 return true; 3165 return true;
2498} 3166}
2499 3167
2500inline bool Server::handle_file_request(Request &req, Response &res) { 3168inline bool Server::read_content(Stream &strm, bool last_connection,
2501 if (!base_dir_.empty() && detail::is_valid_path(req.path)) { 3169 Request &req, Response &res) {
2502 std::string path = base_dir_ + req.path; 3170 MultipartFormDataMap::iterator cur;
3171 auto ret = read_content_core(
3172 strm, last_connection, req, res,
3173 // Regular
3174 [&](const char *buf, size_t n) {
3175 if (req.body.size() + n > req.body.max_size()) { return false; }
3176 req.body.append(buf, n);
3177 return true;
3178 },
3179 // Multipart
3180 [&](const MultipartFormData &file) {
3181 cur = req.files.emplace(file.name, file);
3182 return true;
3183 },
3184 [&](const char *buf, size_t n) {
3185 auto &content = cur->second.content;
3186 if (content.size() + n > content.max_size()) { return false; }
3187 content.append(buf, n);
3188 return true;
3189 });
2503 3190
2504 if (!path.empty() && path.back() == '/') { path += "index.html"; } 3191 const auto &content_type = req.get_header_value("Content-Type");
3192 if (!content_type.find("application/x-www-form-urlencoded")) {
3193 detail::parse_query_text(req.body, req.params);
3194 }
2505 3195
2506 if (detail::is_file(path)) { 3196 return ret;
2507 detail::read_file(path, res.body); 3197}
2508 auto type = detail::find_content_type(path); 3198
2509 if (type) { res.set_header("Content-Type", type); } 3199inline bool Server::read_content_with_content_receiver(
2510 res.status = 200; 3200 Stream &strm, bool last_connection, Request &req, Response &res,
2511 if (file_request_handler_) { file_request_handler_(req, res); } 3201 ContentReceiver receiver, MultipartContentHeader multipart_header,
2512 return true; 3202 ContentReceiver multipart_receiver) {
3203 return read_content_core(strm, last_connection, req, res, receiver,
3204 multipart_header, multipart_receiver);
3205}
3206
3207inline bool Server::read_content_core(Stream &strm, bool last_connection,
3208 Request &req, Response &res,
3209 ContentReceiver receiver,
3210 MultipartContentHeader mulitpart_header,
3211 ContentReceiver multipart_receiver) {
3212 detail::MultipartFormDataParser multipart_form_data_parser;
3213 ContentReceiver out;
3214
3215 if (req.is_multipart_form_data()) {
3216 const auto &content_type = req.get_header_value("Content-Type");
3217 std::string boundary;
3218 if (!detail::parse_multipart_boundary(content_type, boundary)) {
3219 res.status = 400;
3220 return write_response(strm, last_connection, req, res);
2513 } 3221 }
3222
3223 multipart_form_data_parser.set_boundary(boundary);
3224 out = [&](const char *buf, size_t n) {
3225 return multipart_form_data_parser.parse(buf, n, multipart_receiver,
3226 mulitpart_header);
3227 };
3228 } else {
3229 out = receiver;
3230 }
3231
3232 if (!detail::read_content(strm, req, payload_max_length_, res.status,
3233 Progress(), out)) {
3234 return write_response(strm, last_connection, req, res);
2514 } 3235 }
2515 3236
3237 if (req.is_multipart_form_data()) {
3238 if (!multipart_form_data_parser.is_valid()) {
3239 res.status = 400;
3240 return write_response(strm, last_connection, req, res);
3241 }
3242 }
3243
3244 return true;
3245}
3246
3247inline bool Server::handle_file_request(Request &req, Response &res,
3248 bool head) {
3249 for (const auto &kv : base_dirs_) {
3250 const auto &mount_point = kv.first;
3251 const auto &base_dir = kv.second;
3252
3253 // Prefix match
3254 if (!req.path.find(mount_point)) {
3255 std::string sub_path = "/" + req.path.substr(mount_point.size());
3256 if (detail::is_valid_path(sub_path)) {
3257 auto path = base_dir + sub_path;
3258 if (path.back() == '/') { path += "index.html"; }
3259
3260 if (detail::is_file(path)) {
3261 detail::read_file(path, res.body);
3262 auto type =
3263 detail::find_content_type(path, file_extension_and_mimetype_map_);
3264 if (type) { res.set_header("Content-Type", type); }
3265 res.status = 200;
3266 if (!head && file_request_handler_) {
3267 file_request_handler_(req, res);
3268 }
3269 return true;
3270 }
3271 }
3272 }
3273 }
2516 return false; 3274 return false;
2517} 3275}
2518 3276
@@ -2605,9 +3363,51 @@ inline bool Server::listen_internal() {
2605 return ret; 3363 return ret;
2606} 3364}
2607 3365
2608inline bool Server::routing(Request &req, Response &res) { 3366inline bool Server::routing(Request &req, Response &res, Stream &strm,
2609 if (req.method == "GET" && handle_file_request(req, res)) { return true; } 3367 bool last_connection) {
3368 // File handler
3369 bool is_head_request = req.method == "HEAD";
3370 if ((req.method == "GET" || is_head_request) &&
3371 handle_file_request(req, res, is_head_request)) {
3372 return true;
3373 }
3374
3375 if (detail::expect_content(req)) {
3376 // Content reader handler
3377 {
3378 ContentReader reader(
3379 [&](ContentReceiver receiver) {
3380 return read_content_with_content_receiver(
3381 strm, last_connection, req, res, receiver, nullptr, nullptr);
3382 },
3383 [&](MultipartContentHeader header, ContentReceiver receiver) {
3384 return read_content_with_content_receiver(
3385 strm, last_connection, req, res, nullptr, header, receiver);
3386 });
3387
3388 if (req.method == "POST") {
3389 if (dispatch_request_for_content_reader(
3390 req, res, reader, post_handlers_for_content_reader_)) {
3391 return true;
3392 }
3393 } else if (req.method == "PUT") {
3394 if (dispatch_request_for_content_reader(
3395 req, res, reader, put_handlers_for_content_reader_)) {
3396 return true;
3397 }
3398 } else if (req.method == "PATCH") {
3399 if (dispatch_request_for_content_reader(
3400 req, res, reader, patch_handlers_for_content_reader_)) {
3401 return true;
3402 }
3403 }
3404 }
2610 3405
3406 // Read content into `req.body`
3407 if (!read_content(strm, last_connection, req, res)) { return false; }
3408 }
3409
3410 // Regular handler
2611 if (req.method == "GET" || req.method == "HEAD") { 3411 if (req.method == "GET" || req.method == "HEAD") {
2612 return dispatch_request(req, res, get_handlers_); 3412 return dispatch_request(req, res, get_handlers_);
2613 } else if (req.method == "POST") { 3413 } else if (req.method == "POST") {
@@ -2640,17 +3440,31 @@ inline bool Server::dispatch_request(Request &req, Response &res,
2640 return false; 3440 return false;
2641} 3441}
2642 3442
3443inline bool Server::dispatch_request_for_content_reader(
3444 Request &req, Response &res, ContentReader content_reader,
3445 HandlersForContentReader &handlers) {
3446 for (const auto &x : handlers) {
3447 const auto &pattern = x.first;
3448 const auto &handler = x.second;
3449
3450 if (std::regex_match(req.path, req.matches, pattern)) {
3451 handler(req, res, content_reader);
3452 return true;
3453 }
3454 }
3455 return false;
3456}
3457
2643inline bool 3458inline bool
2644Server::process_request(Stream &strm, bool last_connection, 3459Server::process_request(Stream &strm, bool last_connection,
2645 bool &connection_close, 3460 bool &connection_close,
2646 std::function<void(Request &)> setup_request) { 3461 const std::function<void(Request &)> &setup_request) {
2647 const auto bufsiz = 2048; 3462 std::array<char, 2048> buf{};
2648 char buf[bufsiz];
2649 3463
2650 detail::stream_line_reader reader(strm, buf, bufsiz); 3464 detail::stream_line_reader line_reader(strm, buf.data(), buf.size());
2651 3465
2652 // Connection has been closed on client 3466 // Connection has been closed on client
2653 if (!reader.getline()) { return false; } 3467 if (!line_reader.getline()) { return false; }
2654 3468
2655 Request req; 3469 Request req;
2656 Response res; 3470 Response res;
@@ -2658,7 +3472,7 @@ Server::process_request(Stream &strm, bool last_connection,
2658 res.version = "HTTP/1.1"; 3472 res.version = "HTTP/1.1";
2659 3473
2660 // Check if the request URI doesn't exceed the limit 3474 // Check if the request URI doesn't exceed the limit
2661 if (reader.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { 3475 if (line_reader.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) {
2662 Headers dummy; 3476 Headers dummy;
2663 detail::read_headers(strm, dummy); 3477 detail::read_headers(strm, dummy);
2664 res.status = 414; 3478 res.status = 414;
@@ -2666,7 +3480,7 @@ Server::process_request(Stream &strm, bool last_connection,
2666 } 3480 }
2667 3481
2668 // Request line and headers 3482 // Request line and headers
2669 if (!parse_request_line(reader.ptr(), req) || 3483 if (!parse_request_line(line_reader.ptr(), req) ||
2670 !detail::read_headers(strm, req.headers)) { 3484 !detail::read_headers(strm, req.headers)) {
2671 res.status = 400; 3485 res.status = 400;
2672 return write_response(strm, last_connection, req, res); 3486 return write_response(strm, last_connection, req, res);
@@ -2683,33 +3497,6 @@ Server::process_request(Stream &strm, bool last_connection,
2683 3497
2684 req.set_header("REMOTE_ADDR", strm.get_remote_addr()); 3498 req.set_header("REMOTE_ADDR", strm.get_remote_addr());
2685 3499
2686 // Body
2687 if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" || req.method == "PRI") {
2688 if (!detail::read_content(strm, req, payload_max_length_, res.status,
2689 Progress(), [&](const char *buf, size_t n) {
2690 if (req.body.size() + n > req.body.max_size()) {
2691 return false;
2692 }
2693 req.body.append(buf, n);
2694 return true;
2695 })) {
2696 return write_response(strm, last_connection, req, res);
2697 }
2698
2699 const auto &content_type = req.get_header_value("Content-Type");
2700
2701 if (!content_type.find("application/x-www-form-urlencoded")) {
2702 detail::parse_query_text(req.body, req.params);
2703 } else if (!content_type.find("multipart/form-data")) {
2704 std::string boundary;
2705 if (!detail::parse_multipart_boundary(content_type, boundary) ||
2706 !detail::parse_multipart_formdata(boundary, req.body, req.files)) {
2707 res.status = 400;
2708 return write_response(strm, last_connection, req, res);
2709 }
2710 }
2711 }
2712
2713 if (req.has_header("Range")) { 3500 if (req.has_header("Range")) {
2714 const auto &range_header_value = req.get_header_value("Range"); 3501 const auto &range_header_value = req.get_header_value("Range");
2715 if (!detail::parse_range_header(range_header_value, req.ranges)) { 3502 if (!detail::parse_range_header(range_header_value, req.ranges)) {
@@ -2719,7 +3506,23 @@ Server::process_request(Stream &strm, bool last_connection,
2719 3506
2720 if (setup_request) { setup_request(req); } 3507 if (setup_request) { setup_request(req); }
2721 3508
2722 if (routing(req, res)) { 3509 if (req.get_header_value("Expect") == "100-continue") {
3510 auto status = 100;
3511 if (expect_100_continue_handler_) {
3512 status = expect_100_continue_handler_(req, res);
3513 }
3514 switch (status) {
3515 case 100:
3516 case 417:
3517 strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status,
3518 detail::status_message(status));
3519 break;
3520 default: return write_response(strm, last_connection, req, res);
3521 }
3522 }
3523
3524 // Rounting
3525 if (routing(req, res, strm, last_connection)) {
2723 if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; } 3526 if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; }
2724 } else { 3527 } else {
2725 if (res.status == -1) { res.status = 404; } 3528 if (res.status == -1) { res.status = 404; }
@@ -2732,7 +3535,7 @@ inline bool Server::is_valid() const { return true; }
2732 3535
2733inline bool Server::process_and_close_socket(socket_t sock) { 3536inline bool Server::process_and_close_socket(socket_t sock) {
2734 return detail::process_and_close_socket( 3537 return detail::process_and_close_socket(
2735 false, sock, keep_alive_max_count_, 3538 false, sock, keep_alive_max_count_, read_timeout_sec_, read_timeout_usec_,
2736 [this](Stream &strm, bool last_connection, bool &connection_close) { 3539 [this](Stream &strm, bool last_connection, bool &connection_close) {
2737 return process_request(strm, last_connection, connection_close, 3540 return process_request(strm, last_connection, connection_close,
2738 nullptr); 3541 nullptr);
@@ -2740,47 +3543,37 @@ inline bool Server::process_and_close_socket(socket_t sock) {
2740} 3543}
2741 3544
2742// HTTP client implementation 3545// HTTP client implementation
2743inline Client::Client(const char *host, int port, time_t timeout_sec) 3546inline Client::Client(const std::string &host, int port,
2744 : host_(host), port_(port), timeout_sec_(timeout_sec), 3547 const std::string &client_cert_path,
3548 const std::string &client_key_path)
3549 : host_(host), port_(port),
2745 host_and_port_(host_ + ":" + std::to_string(port_)), 3550 host_and_port_(host_ + ":" + std::to_string(port_)),
2746 keep_alive_max_count_(CPPHTTPLIB_KEEPALIVE_MAX_COUNT), 3551 client_cert_path_(client_cert_path), client_key_path_(client_key_path) {}
2747 follow_location_(false) {}
2748 3552
2749inline Client::~Client() {} 3553inline Client::~Client() {}
2750 3554
2751inline bool Client::is_valid() const { return true; } 3555inline bool Client::is_valid() const { return true; }
2752 3556
2753inline socket_t Client::create_client_socket() const { 3557inline socket_t Client::create_client_socket() const {
2754 return detail::create_socket( 3558 if (!proxy_host_.empty()) {
2755 host_.c_str(), port_, [=](socket_t sock, struct addrinfo &ai) -> bool { 3559 return detail::create_client_socket(proxy_host_.c_str(), proxy_port_,
2756 detail::set_nonblocking(sock, true); 3560 timeout_sec_, interface_);
2757 3561 }
2758 auto ret = connect(sock, ai.ai_addr, static_cast<int>(ai.ai_addrlen)); 3562 return detail::create_client_socket(host_.c_str(), port_, timeout_sec_,
2759 if (ret < 0) { 3563 interface_);
2760 if (detail::is_connection_error() ||
2761 !detail::wait_until_socket_is_ready(sock, timeout_sec_, 0)) {
2762 detail::close_socket(sock);
2763 return false;
2764 }
2765 }
2766
2767 detail::set_nonblocking(sock, false);
2768 return true;
2769 });
2770} 3564}
2771 3565
2772inline bool Client::read_response_line(Stream &strm, Response &res) { 3566inline bool Client::read_response_line(Stream &strm, Response &res) {
2773 const auto bufsiz = 2048; 3567 std::array<char, 2048> buf;
2774 char buf[bufsiz];
2775 3568
2776 detail::stream_line_reader reader(strm, buf, bufsiz); 3569 detail::stream_line_reader line_reader(strm, buf.data(), buf.size());
2777 3570
2778 if (!reader.getline()) { return false; } 3571 if (!line_reader.getline()) { return false; }
2779 3572
2780 const static std::regex re("(HTTP/1\\.[01]) (\\d+?) .*\r\n"); 3573 const static std::regex re("(HTTP/1\\.[01]) (\\d+?) .*\r\n");
2781 3574
2782 std::cmatch m; 3575 std::cmatch m;
2783 if (std::regex_match(reader.ptr(), m, re)) { 3576 if (std::regex_match(line_reader.ptr(), m, re)) {
2784 res.version = std::string(m[1]); 3577 res.version = std::string(m[1]);
2785 res.status = std::stoi(std::string(m[2])); 3578 res.status = std::stoi(std::string(m[2]));
2786 } 3579 }
@@ -2789,22 +3582,21 @@ inline bool Client::read_response_line(Stream &strm, Response &res) {
2789} 3582}
2790 3583
2791inline bool Client::send(const Request &req, Response &res) { 3584inline bool Client::send(const Request &req, Response &res) {
2792 if (req.path.empty()) { return false; }
2793
2794 auto sock = create_client_socket(); 3585 auto sock = create_client_socket();
2795 if (sock == INVALID_SOCKET) { return false; } 3586 if (sock == INVALID_SOCKET) { return false; }
2796 3587
2797 auto ret = process_and_close_socket( 3588#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2798 sock, 1, [&](Stream &strm, bool last_connection, bool &connection_close) { 3589 if (is_ssl() && !proxy_host_.empty()) {
2799 return process_request(strm, req, res, last_connection, 3590 bool error;
2800 connection_close); 3591 if (!connect(sock, res, error)) { return error; }
2801 });
2802
2803 if (ret && follow_location_ && (300 < res.status && res.status < 400)) {
2804 ret = redirect(req, res);
2805 } 3592 }
3593#endif
2806 3594
2807 return ret; 3595 return process_and_close_socket(
3596 sock, 1, [&](Stream &strm, bool last_connection, bool &connection_close) {
3597 return handle_request(strm, req, res, last_connection,
3598 connection_close);
3599 });
2808} 3600}
2809 3601
2810inline bool Client::send(const std::vector<Request> &requests, 3602inline bool Client::send(const std::vector<Request> &requests,
@@ -2814,32 +3606,136 @@ inline bool Client::send(const std::vector<Request> &requests,
2814 auto sock = create_client_socket(); 3606 auto sock = create_client_socket();
2815 if (sock == INVALID_SOCKET) { return false; } 3607 if (sock == INVALID_SOCKET) { return false; }
2816 3608
2817 if (!process_and_close_socket( 3609#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2818 sock, requests.size() - i, 3610 if (is_ssl() && !proxy_host_.empty()) {
2819 [&](Stream &strm, bool last_connection, bool &connection_close) -> bool { 3611 Response res;
2820 auto &req = requests[i]; 3612 bool error;
2821 auto res = Response(); 3613 if (!connect(sock, res, error)) { return false; }
2822 i++; 3614 }
3615#endif
3616
3617 if (!process_and_close_socket(sock, requests.size() - i,
3618 [&](Stream &strm, bool last_connection,
3619 bool &connection_close) -> bool {
3620 auto &req = requests[i++];
3621 auto res = Response();
3622 auto ret = handle_request(strm, req, res,
3623 last_connection,
3624 connection_close);
3625 if (ret) {
3626 responses.emplace_back(std::move(res));
3627 }
3628 return ret;
3629 })) {
3630 return false;
3631 }
3632 }
2823 3633
2824 if (req.path.empty()) { return false; } 3634 return true;
2825 auto ret = process_request(strm, req, res, last_connection, 3635}
2826 connection_close); 3636
3637inline bool Client::handle_request(Stream &strm, const Request &req,
3638 Response &res, bool last_connection,
3639 bool &connection_close) {
3640 if (req.path.empty()) { return false; }
3641
3642 bool ret;
3643
3644 if (!is_ssl() && !proxy_host_.empty()) {
3645 auto req2 = req;
3646 req2.path = "http://" + host_and_port_ + req.path;
3647 ret = process_request(strm, req2, res, last_connection, connection_close);
3648 } else {
3649 ret = process_request(strm, req, res, last_connection, connection_close);
3650 }
3651
3652 if (!ret) { return false; }
3653
3654 if (300 < res.status && res.status < 400 && follow_location_) {
3655 ret = redirect(req, res);
3656 }
2827 3657
2828 if (ret && follow_location_ && 3658#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2829 (300 < res.status && res.status < 400)) { 3659 if (res.status == 401 || res.status == 407) {
2830 ret = redirect(req, res); 3660 auto is_proxy = res.status == 407;
2831 } 3661 const auto &username =
3662 is_proxy ? proxy_digest_auth_username_ : digest_auth_username_;
3663 const auto &password =
3664 is_proxy ? proxy_digest_auth_password_ : digest_auth_password_;
3665
3666 if (!username.empty() && !password.empty()) {
3667 std::map<std::string, std::string> auth;
3668 if (parse_www_authenticate(res, auth, is_proxy)) {
3669 Request new_req = req;
3670 auto key = is_proxy ? "Proxy-Authorization" : "WWW-Authorization";
3671 new_req.headers.erase(key);
3672 new_req.headers.insert(make_digest_authentication_header(
3673 req, auth, 1, random_string(10), username, password, is_proxy));
3674
3675 Response new_res;
3676
3677 ret = send(new_req, new_res);
3678 if (ret) { res = new_res; }
3679 }
3680 }
3681 }
3682#endif
3683
3684 return ret;
3685}
2832 3686
2833 if (ret) { responses.emplace_back(std::move(res)); } 3687#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
3688inline bool Client::connect(socket_t sock, Response &res, bool &error) {
3689 error = true;
3690 Response res2;
3691
3692 if (!detail::process_socket(
3693 true, sock, 1, read_timeout_sec_, read_timeout_usec_,
3694 [&](Stream &strm, bool /*last_connection*/, bool &connection_close) {
3695 Request req2;
3696 req2.method = "CONNECT";
3697 req2.path = host_and_port_;
3698 return process_request(strm, req2, res2, false, connection_close);
3699 })) {
3700 detail::close_socket(sock);
3701 error = false;
3702 return false;
3703 }
2834 3704
2835 return ret; 3705 if (res2.status == 407) {
2836 })) { 3706 if (!proxy_digest_auth_username_.empty() &&
3707 !proxy_digest_auth_password_.empty()) {
3708 std::map<std::string, std::string> auth;
3709 if (parse_www_authenticate(res2, auth, true)) {
3710 Response res3;
3711 if (!detail::process_socket(
3712 true, sock, 1, read_timeout_sec_, read_timeout_usec_,
3713 [&](Stream &strm, bool /*last_connection*/,
3714 bool &connection_close) {
3715 Request req3;
3716 req3.method = "CONNECT";
3717 req3.path = host_and_port_;
3718 req3.headers.insert(make_digest_authentication_header(
3719 req3, auth, 1, random_string(10),
3720 proxy_digest_auth_username_, proxy_digest_auth_password_,
3721 true));
3722 return process_request(strm, req3, res3, false,
3723 connection_close);
3724 })) {
3725 detail::close_socket(sock);
3726 error = false;
3727 return false;
3728 }
3729 }
3730 } else {
3731 res = res2;
2837 return false; 3732 return false;
2838 } 3733 }
2839 } 3734 }
2840 3735
2841 return true; 3736 return true;
2842} 3737}
3738#endif
2843 3739
2844inline bool Client::redirect(const Request &req, Response &res) { 3740inline bool Client::redirect(const Request &req, Response &res) {
2845 if (req.redirect_count == 0) { return false; } 3741 if (req.redirect_count == 0) { return false; }
@@ -2847,46 +3743,47 @@ inline bool Client::redirect(const Request &req, Response &res) {
2847 auto location = res.get_header_value("location"); 3743 auto location = res.get_header_value("location");
2848 if (location.empty()) { return false; } 3744 if (location.empty()) { return false; }
2849 3745
2850 std::regex re( 3746 const static std::regex re(
2851 R"(^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*(?:\?[^#]*)?)(?:#.*)?)"); 3747 R"(^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*(?:\?[^#]*)?)(?:#.*)?)");
2852 3748
3749 std::smatch m;
3750 if (!regex_match(location, m, re)) { return false; }
3751
2853 auto scheme = is_ssl() ? "https" : "http"; 3752 auto scheme = is_ssl() ? "https" : "http";
2854 3753
2855 std::smatch m; 3754 auto next_scheme = m[1].str();
2856 if (regex_match(location, m, re)) { 3755 auto next_host = m[2].str();
2857 auto next_scheme = m[1].str(); 3756 auto next_path = m[3].str();
2858 auto next_host = m[2].str(); 3757 if (next_scheme.empty()) { next_scheme = scheme; }
2859 auto next_path = m[3].str(); 3758 if (next_scheme.empty()) { next_scheme = scheme; }
2860 if (next_host.empty()) { next_host = host_; } 3759 if (next_host.empty()) { next_host = host_; }
2861 if (next_path.empty()) { next_path = "/"; } 3760 if (next_path.empty()) { next_path = "/"; }
2862 3761
2863 if (next_scheme == scheme && next_host == host_) { 3762 if (next_scheme == scheme && next_host == host_) {
2864 return detail::redirect(*this, req, res, next_path); 3763 return detail::redirect(*this, req, res, next_path);
2865 } else { 3764 } else {
2866 if (next_scheme == "https") { 3765 if (next_scheme == "https") {
2867#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 3766#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2868 SSLClient cli(next_host.c_str()); 3767 SSLClient cli(next_host.c_str());
2869 cli.follow_location(true); 3768 cli.copy_settings(*this);
2870 return detail::redirect(cli, req, res, next_path); 3769 return detail::redirect(cli, req, res, next_path);
2871#else 3770#else
2872 return false; 3771 return false;
2873#endif 3772#endif
2874 } else { 3773 } else {
2875 Client cli(next_host.c_str()); 3774 Client cli(next_host.c_str());
2876 cli.follow_location(true); 3775 cli.copy_settings(*this);
2877 return detail::redirect(cli, req, res, next_path); 3776 return detail::redirect(cli, req, res, next_path);
2878 }
2879 } 3777 }
2880 } 3778 }
2881 return false;
2882} 3779}
2883 3780
2884inline void Client::write_request(Stream &strm, const Request &req, 3781inline bool Client::write_request(Stream &strm, const Request &req,
2885 bool last_connection) { 3782 bool last_connection) {
2886 BufferStream bstrm; 3783 detail::BufferStream bstrm;
2887 3784
2888 // Request line 3785 // Request line
2889 auto path = detail::encode_url(req.path); 3786 const auto &path = detail::encode_url(req.path);
2890 3787
2891 bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str()); 3788 bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str());
2892 3789
@@ -2913,11 +3810,14 @@ inline void Client::write_request(Stream &strm, const Request &req,
2913 if (!req.has_header("Accept")) { headers.emplace("Accept", "*/*"); } 3810 if (!req.has_header("Accept")) { headers.emplace("Accept", "*/*"); }
2914 3811
2915 if (!req.has_header("User-Agent")) { 3812 if (!req.has_header("User-Agent")) {
2916 headers.emplace("User-Agent", "cpp-httplib/0.2"); 3813 headers.emplace("User-Agent", "cpp-httplib/0.5");
2917 } 3814 }
2918 3815
2919 if (req.body.empty()) { 3816 if (req.body.empty()) {
2920 if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH") { 3817 if (req.content_provider) {
3818 auto length = std::to_string(req.content_length);
3819 headers.emplace("Content-Length", length);
3820 } else {
2921 headers.emplace("Content-Length", "0"); 3821 headers.emplace("Content-Length", "0");
2922 } 3822 }
2923 } else { 3823 } else {
@@ -2931,21 +3831,100 @@ inline void Client::write_request(Stream &strm, const Request &req,
2931 } 3831 }
2932 } 3832 }
2933 3833
2934 detail::write_headers(bstrm, req, headers); 3834 if (!basic_auth_username_.empty() && !basic_auth_password_.empty()) {
3835 headers.insert(make_basic_authentication_header(
3836 basic_auth_username_, basic_auth_password_, false));
3837 }
2935 3838
2936 // Body 3839 if (!proxy_basic_auth_username_.empty() &&
2937 if (!req.body.empty()) { bstrm.write(req.body); } 3840 !proxy_basic_auth_password_.empty()) {
3841 headers.insert(make_basic_authentication_header(
3842 proxy_basic_auth_username_, proxy_basic_auth_password_, true));
3843 }
3844
3845 detail::write_headers(bstrm, req, headers);
2938 3846
2939 // Flush buffer 3847 // Flush buffer
2940 auto &data = bstrm.get_buffer(); 3848 auto &data = bstrm.get_buffer();
2941 strm.write(data.data(), data.size()); 3849 strm.write(data.data(), data.size());
3850
3851 // Body
3852 if (req.body.empty()) {
3853 if (req.content_provider) {
3854 size_t offset = 0;
3855 size_t end_offset = req.content_length;
3856
3857 DataSink data_sink;
3858 data_sink.write = [&](const char *d, size_t l) {
3859 auto written_length = strm.write(d, l);
3860 offset += written_length;
3861 };
3862 data_sink.is_writable = [&](void) { return strm.is_writable(); };
3863
3864 while (offset < end_offset) {
3865 req.content_provider(offset, end_offset - offset, data_sink);
3866 }
3867 }
3868 } else {
3869 strm.write(req.body);
3870 }
3871
3872 return true;
3873}
3874
3875inline std::shared_ptr<Response> Client::send_with_content_provider(
3876 const char *method, const char *path, const Headers &headers,
3877 const std::string &body, size_t content_length,
3878 ContentProvider content_provider, const char *content_type) {
3879 Request req;
3880 req.method = method;
3881 req.headers = headers;
3882 req.path = path;
3883
3884 req.headers.emplace("Content-Type", content_type);
3885
3886#ifdef CPPHTTPLIB_ZLIB_SUPPORT
3887 if (compress_) {
3888 if (content_provider) {
3889 size_t offset = 0;
3890
3891 DataSink data_sink;
3892 data_sink.write = [&](const char *data, size_t data_len) {
3893 req.body.append(data, data_len);
3894 offset += data_len;
3895 };
3896 data_sink.is_writable = [&](void) { return true; };
3897
3898 while (offset < content_length) {
3899 content_provider(offset, content_length - offset, data_sink);
3900 }
3901 } else {
3902 req.body = body;
3903 }
3904
3905 if (!detail::compress(req.body)) { return nullptr; }
3906 req.headers.emplace("Content-Encoding", "gzip");
3907 } else
3908#endif
3909 {
3910 if (content_provider) {
3911 req.content_length = content_length;
3912 req.content_provider = content_provider;
3913 } else {
3914 req.body = body;
3915 }
3916 }
3917
3918 auto res = std::make_shared<Response>();
3919
3920 return send(req, *res) ? res : nullptr;
2942} 3921}
2943 3922
2944inline bool Client::process_request(Stream &strm, const Request &req, 3923inline bool Client::process_request(Stream &strm, const Request &req,
2945 Response &res, bool last_connection, 3924 Response &res, bool last_connection,
2946 bool &connection_close) { 3925 bool &connection_close) {
2947 // Send request 3926 // Send request
2948 write_request(strm, req, last_connection); 3927 if (!write_request(strm, req, last_connection)) { return false; }
2949 3928
2950 // Receive response and headers 3929 // Receive response and headers
2951 if (!read_response_line(strm, res) || 3930 if (!read_response_line(strm, res) ||
@@ -2963,21 +3942,16 @@ inline bool Client::process_request(Stream &strm, const Request &req,
2963 } 3942 }
2964 3943
2965 // Body 3944 // Body
2966 if (req.method != "HEAD") { 3945 if (req.method != "HEAD" && req.method != "CONNECT") {
2967 detail::ContentReceiverCore out = [&](const char *buf, size_t n) { 3946 ContentReceiver out = [&](const char *buf, size_t n) {
2968 if (res.body.size() + n > res.body.max_size()) { return false; } 3947 if (res.body.size() + n > res.body.max_size()) { return false; }
2969 res.body.append(buf, n); 3948 res.body.append(buf, n);
2970 return true; 3949 return true;
2971 }; 3950 };
2972 3951
2973 if (req.content_receiver) { 3952 if (req.content_receiver) {
2974 auto offset = std::make_shared<size_t>(); 3953 out = [&](const char *buf, size_t n) {
2975 auto length = get_header_value_uint64(res.headers, "Content-Length", 0); 3954 return req.content_receiver(buf, n);
2976 auto receiver = req.content_receiver;
2977 out = [offset, length, receiver](const char *buf, size_t n) {
2978 auto ret = receiver(buf, n, *offset, length);
2979 (*offset) += n;
2980 return ret;
2981 }; 3955 };
2982 } 3956 }
2983 3957
@@ -2988,6 +3962,9 @@ inline bool Client::process_request(Stream &strm, const Request &req,
2988 } 3962 }
2989 } 3963 }
2990 3964
3965 // Log
3966 if (logger_) { logger_(req, res); }
3967
2991 return true; 3968 return true;
2992} 3969}
2993 3970
@@ -2997,25 +3974,25 @@ inline bool Client::process_and_close_socket(
2997 bool &connection_close)> 3974 bool &connection_close)>
2998 callback) { 3975 callback) {
2999 request_count = std::min(request_count, keep_alive_max_count_); 3976 request_count = std::min(request_count, keep_alive_max_count_);
3000 return detail::process_and_close_socket(true, sock, request_count, callback); 3977 return detail::process_and_close_socket(true, sock, request_count,
3978 read_timeout_sec_, read_timeout_usec_,
3979 callback);
3001} 3980}
3002 3981
3003inline bool Client::is_ssl() const { return false; } 3982inline bool Client::is_ssl() const { return false; }
3004 3983
3005inline std::shared_ptr<Response> Client::Get(const char *path) { 3984inline std::shared_ptr<Response> Client::Get(const char *path) {
3006 Progress dummy; 3985 return Get(path, Headers(), Progress());
3007 return Get(path, Headers(), dummy);
3008} 3986}
3009 3987
3010inline std::shared_ptr<Response> Client::Get(const char *path, 3988inline std::shared_ptr<Response> Client::Get(const char *path,
3011 Progress progress) { 3989 Progress progress) {
3012 return Get(path, Headers(), progress); 3990 return Get(path, Headers(), std::move(progress));
3013} 3991}
3014 3992
3015inline std::shared_ptr<Response> Client::Get(const char *path, 3993inline std::shared_ptr<Response> Client::Get(const char *path,
3016 const Headers &headers) { 3994 const Headers &headers) {
3017 Progress dummy; 3995 return Get(path, headers, Progress());
3018 return Get(path, headers, dummy);
3019} 3996}
3020 3997
3021inline std::shared_ptr<Response> 3998inline std::shared_ptr<Response>
@@ -3024,7 +4001,7 @@ Client::Get(const char *path, const Headers &headers, Progress progress) {
3024 req.method = "GET"; 4001 req.method = "GET";
3025 req.path = path; 4002 req.path = path;
3026 req.headers = headers; 4003 req.headers = headers;
3027 req.progress = progress; 4004 req.progress = std::move(progress);
3028 4005
3029 auto res = std::make_shared<Response>(); 4006 auto res = std::make_shared<Response>();
3030 return send(req, *res) ? res : nullptr; 4007 return send(req, *res) ? res : nullptr;
@@ -3032,36 +4009,36 @@ Client::Get(const char *path, const Headers &headers, Progress progress) {
3032 4009
3033inline std::shared_ptr<Response> Client::Get(const char *path, 4010inline std::shared_ptr<Response> Client::Get(const char *path,
3034 ContentReceiver content_receiver) { 4011 ContentReceiver content_receiver) {
3035 Progress dummy; 4012 return Get(path, Headers(), nullptr, std::move(content_receiver), Progress());
3036 return Get(path, Headers(), nullptr, content_receiver, dummy);
3037} 4013}
3038 4014
3039inline std::shared_ptr<Response> Client::Get(const char *path, 4015inline std::shared_ptr<Response> Client::Get(const char *path,
3040 ContentReceiver content_receiver, 4016 ContentReceiver content_receiver,
3041 Progress progress) { 4017 Progress progress) {
3042 return Get(path, Headers(), nullptr, content_receiver, progress); 4018 return Get(path, Headers(), nullptr, std::move(content_receiver),
4019 std::move(progress));
3043} 4020}
3044 4021
3045inline std::shared_ptr<Response> Client::Get(const char *path, 4022inline std::shared_ptr<Response> Client::Get(const char *path,
3046 const Headers &headers, 4023 const Headers &headers,
3047 ContentReceiver content_receiver) { 4024 ContentReceiver content_receiver) {
3048 Progress dummy; 4025 return Get(path, headers, nullptr, std::move(content_receiver), Progress());
3049 return Get(path, headers, nullptr, content_receiver, dummy);
3050} 4026}
3051 4027
3052inline std::shared_ptr<Response> Client::Get(const char *path, 4028inline std::shared_ptr<Response> Client::Get(const char *path,
3053 const Headers &headers, 4029 const Headers &headers,
3054 ContentReceiver content_receiver, 4030 ContentReceiver content_receiver,
3055 Progress progress) { 4031 Progress progress) {
3056 return Get(path, headers, nullptr, content_receiver, progress); 4032 return Get(path, headers, nullptr, std::move(content_receiver),
4033 std::move(progress));
3057} 4034}
3058 4035
3059inline std::shared_ptr<Response> Client::Get(const char *path, 4036inline std::shared_ptr<Response> Client::Get(const char *path,
3060 const Headers &headers, 4037 const Headers &headers,
3061 ResponseHandler response_handler, 4038 ResponseHandler response_handler,
3062 ContentReceiver content_receiver) { 4039 ContentReceiver content_receiver) {
3063 Progress dummy; 4040 return Get(path, headers, std::move(response_handler), content_receiver,
3064 return Get(path, headers, response_handler, content_receiver, dummy); 4041 Progress());
3065} 4042}
3066 4043
3067inline std::shared_ptr<Response> Client::Get(const char *path, 4044inline std::shared_ptr<Response> Client::Get(const char *path,
@@ -3073,9 +4050,9 @@ inline std::shared_ptr<Response> Client::Get(const char *path,
3073 req.method = "GET"; 4050 req.method = "GET";
3074 req.path = path; 4051 req.path = path;
3075 req.headers = headers; 4052 req.headers = headers;
3076 req.response_handler = response_handler; 4053 req.response_handler = std::move(response_handler);
3077 req.content_receiver = content_receiver; 4054 req.content_receiver = std::move(content_receiver);
3078 req.progress = progress; 4055 req.progress = std::move(progress);
3079 4056
3080 auto res = std::make_shared<Response>(); 4057 auto res = std::make_shared<Response>();
3081 return send(req, *res) ? res : nullptr; 4058 return send(req, *res) ? res : nullptr;
@@ -3107,17 +4084,8 @@ inline std::shared_ptr<Response> Client::Post(const char *path,
3107 const Headers &headers, 4084 const Headers &headers,
3108 const std::string &body, 4085 const std::string &body,
3109 const char *content_type) { 4086 const char *content_type) {
3110 Request req; 4087 return send_with_content_provider("POST", path, headers, body, 0, nullptr,
3111 req.method = "POST"; 4088 content_type);
3112 req.headers = headers;
3113 req.path = path;
3114
3115 req.headers.emplace("Content-Type", content_type);
3116 req.body = body;
3117
3118 auto res = std::make_shared<Response>();
3119
3120 return send(req, *res) ? res : nullptr;
3121} 4089}
3122 4090
3123inline std::shared_ptr<Response> Client::Post(const char *path, 4091inline std::shared_ptr<Response> Client::Post(const char *path,
@@ -3125,6 +4093,21 @@ inline std::shared_ptr<Response> Client::Post(const char *path,
3125 return Post(path, Headers(), params); 4093 return Post(path, Headers(), params);
3126} 4094}
3127 4095
4096inline std::shared_ptr<Response> Client::Post(const char *path,
4097 size_t content_length,
4098 ContentProvider content_provider,
4099 const char *content_type) {
4100 return Post(path, Headers(), content_length, content_provider, content_type);
4101}
4102
4103inline std::shared_ptr<Response>
4104Client::Post(const char *path, const Headers &headers, size_t content_length,
4105 ContentProvider content_provider, const char *content_type) {
4106 return send_with_content_provider("POST", path, headers, std::string(),
4107 content_length, content_provider,
4108 content_type);
4109}
4110
3128inline std::shared_ptr<Response> 4111inline std::shared_ptr<Response>
3129Client::Post(const char *path, const Headers &headers, const Params &params) { 4112Client::Post(const char *path, const Headers &headers, const Params &params) {
3130 std::string query; 4113 std::string query;
@@ -3146,35 +4129,28 @@ Client::Post(const char *path, const MultipartFormDataItems &items) {
3146inline std::shared_ptr<Response> 4129inline std::shared_ptr<Response>
3147Client::Post(const char *path, const Headers &headers, 4130Client::Post(const char *path, const Headers &headers,
3148 const MultipartFormDataItems &items) { 4131 const MultipartFormDataItems &items) {
3149 Request req;
3150 req.method = "POST";
3151 req.headers = headers;
3152 req.path = path;
3153
3154 auto boundary = detail::make_multipart_data_boundary(); 4132 auto boundary = detail::make_multipart_data_boundary();
3155 4133
3156 req.headers.emplace("Content-Type", 4134 std::string body;
3157 "multipart/form-data; boundary=" + boundary);
3158 4135
3159 for (const auto &item : items) { 4136 for (const auto &item : items) {
3160 req.body += "--" + boundary + "\r\n"; 4137 body += "--" + boundary + "\r\n";
3161 req.body += "Content-Disposition: form-data; name=\"" + item.name + "\""; 4138 body += "Content-Disposition: form-data; name=\"" + item.name + "\"";
3162 if (!item.filename.empty()) { 4139 if (!item.filename.empty()) {
3163 req.body += "; filename=\"" + item.filename + "\""; 4140 body += "; filename=\"" + item.filename + "\"";
3164 } 4141 }
3165 req.body += "\r\n"; 4142 body += "\r\n";
3166 if (!item.content_type.empty()) { 4143 if (!item.content_type.empty()) {
3167 req.body += "Content-Type: " + item.content_type + "\r\n"; 4144 body += "Content-Type: " + item.content_type + "\r\n";
3168 } 4145 }
3169 req.body += "\r\n"; 4146 body += "\r\n";
3170 req.body += item.content + "\r\n"; 4147 body += item.content + "\r\n";
3171 } 4148 }
3172 4149
3173 req.body += "--" + boundary + "--\r\n"; 4150 body += "--" + boundary + "--\r\n";
3174 4151
3175 auto res = std::make_shared<Response>(); 4152 std::string content_type = "multipart/form-data; boundary=" + boundary;
3176 4153 return Post(path, headers, body, content_type.c_str());
3177 return send(req, *res) ? res : nullptr;
3178} 4154}
3179 4155
3180inline std::shared_ptr<Response> Client::Put(const char *path, 4156inline std::shared_ptr<Response> Client::Put(const char *path,
@@ -3187,17 +4163,41 @@ inline std::shared_ptr<Response> Client::Put(const char *path,
3187 const Headers &headers, 4163 const Headers &headers,
3188 const std::string &body, 4164 const std::string &body,
3189 const char *content_type) { 4165 const char *content_type) {
3190 Request req; 4166 return send_with_content_provider("PUT", path, headers, body, 0, nullptr,
3191 req.method = "PUT"; 4167 content_type);
3192 req.headers = headers; 4168}
3193 req.path = path;
3194 4169
3195 req.headers.emplace("Content-Type", content_type); 4170inline std::shared_ptr<Response> Client::Put(const char *path,
3196 req.body = body; 4171 size_t content_length,
4172 ContentProvider content_provider,
4173 const char *content_type) {
4174 return Put(path, Headers(), content_length, content_provider, content_type);
4175}
3197 4176
3198 auto res = std::make_shared<Response>(); 4177inline std::shared_ptr<Response>
4178Client::Put(const char *path, const Headers &headers, size_t content_length,
4179 ContentProvider content_provider, const char *content_type) {
4180 return send_with_content_provider("PUT", path, headers, std::string(),
4181 content_length, content_provider,
4182 content_type);
4183}
3199 4184
3200 return send(req, *res) ? res : nullptr; 4185inline std::shared_ptr<Response> Client::Put(const char *path,
4186 const Params &params) {
4187 return Put(path, Headers(), params);
4188}
4189
4190inline std::shared_ptr<Response>
4191Client::Put(const char *path, const Headers &headers, const Params &params) {
4192 std::string query;
4193 for (auto it = params.begin(); it != params.end(); ++it) {
4194 if (it != params.begin()) { query += "&"; }
4195 query += it->first;
4196 query += "=";
4197 query += detail::encode_url(it->second);
4198 }
4199
4200 return Put(path, headers, query, "application/x-www-form-urlencoded");
3201} 4201}
3202 4202
3203inline std::shared_ptr<Response> Client::Patch(const char *path, 4203inline std::shared_ptr<Response> Client::Patch(const char *path,
@@ -3210,17 +4210,23 @@ inline std::shared_ptr<Response> Client::Patch(const char *path,
3210 const Headers &headers, 4210 const Headers &headers,
3211 const std::string &body, 4211 const std::string &body,
3212 const char *content_type) { 4212 const char *content_type) {
3213 Request req; 4213 return send_with_content_provider("PATCH", path, headers, body, 0, nullptr,
3214 req.method = "PATCH"; 4214 content_type);
3215 req.headers = headers; 4215}
3216 req.path = path;
3217
3218 req.headers.emplace("Content-Type", content_type);
3219 req.body = body;
3220 4216
3221 auto res = std::make_shared<Response>(); 4217inline std::shared_ptr<Response> Client::Patch(const char *path,
4218 size_t content_length,
4219 ContentProvider content_provider,
4220 const char *content_type) {
4221 return Patch(path, Headers(), content_length, content_provider, content_type);
4222}
3222 4223
3223 return send(req, *res) ? res : nullptr; 4224inline std::shared_ptr<Response>
4225Client::Patch(const char *path, const Headers &headers, size_t content_length,
4226 ContentProvider content_provider, const char *content_type) {
4227 return send_with_content_provider("PATCH", path, headers, std::string(),
4228 content_length, content_provider,
4229 content_type);
3224} 4230}
3225 4231
3226inline std::shared_ptr<Response> Client::Delete(const char *path) { 4232inline std::shared_ptr<Response> Client::Delete(const char *path) {
@@ -3271,11 +4277,58 @@ inline std::shared_ptr<Response> Client::Options(const char *path,
3271 return send(req, *res) ? res : nullptr; 4277 return send(req, *res) ? res : nullptr;
3272} 4278}
3273 4279
4280inline void Client::set_timeout_sec(time_t timeout_sec) {
4281 timeout_sec_ = timeout_sec;
4282}
4283
4284inline void Client::set_read_timeout(time_t sec, time_t usec) {
4285 read_timeout_sec_ = sec;
4286 read_timeout_usec_ = usec;
4287}
4288
3274inline void Client::set_keep_alive_max_count(size_t count) { 4289inline void Client::set_keep_alive_max_count(size_t count) {
3275 keep_alive_max_count_ = count; 4290 keep_alive_max_count_ = count;
3276} 4291}
3277 4292
3278inline void Client::follow_location(bool on) { follow_location_ = on; } 4293inline void Client::set_basic_auth(const char *username, const char *password) {
4294 basic_auth_username_ = username;
4295 basic_auth_password_ = password;
4296}
4297
4298#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
4299inline void Client::set_digest_auth(const char *username,
4300 const char *password) {
4301 digest_auth_username_ = username;
4302 digest_auth_password_ = password;
4303}
4304#endif
4305
4306inline void Client::set_follow_location(bool on) { follow_location_ = on; }
4307
4308inline void Client::set_compress(bool on) { compress_ = on; }
4309
4310inline void Client::set_interface(const char *intf) { interface_ = intf; }
4311
4312inline void Client::set_proxy(const char *host, int port) {
4313 proxy_host_ = host;
4314 proxy_port_ = port;
4315}
4316
4317inline void Client::set_proxy_basic_auth(const char *username,
4318 const char *password) {
4319 proxy_basic_auth_username_ = username;
4320 proxy_basic_auth_password_ = password;
4321}
4322
4323#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
4324inline void Client::set_proxy_digest_auth(const char *username,
4325 const char *password) {
4326 proxy_digest_auth_username_ = username;
4327 proxy_digest_auth_password_ = password;
4328}
4329#endif
4330
4331inline void Client::set_logger(Logger logger) { logger_ = std::move(logger); }
3279 4332
3280/* 4333/*
3281 * SSL Implementation 4334 * SSL Implementation
@@ -3284,11 +4337,10 @@ inline void Client::follow_location(bool on) { follow_location_ = on; }
3284namespace detail { 4337namespace detail {
3285 4338
3286template <typename U, typename V, typename T> 4339template <typename U, typename V, typename T>
3287inline bool process_and_close_socket_ssl(bool is_client_request, socket_t sock, 4340inline bool process_and_close_socket_ssl(
3288 size_t keep_alive_max_count, 4341 bool is_client_request, socket_t sock, size_t keep_alive_max_count,
3289 SSL_CTX *ctx, std::mutex &ctx_mutex, 4342 time_t read_timeout_sec, time_t read_timeout_usec, SSL_CTX *ctx,
3290 U SSL_connect_or_accept, V setup, 4343 std::mutex &ctx_mutex, U SSL_connect_or_accept, V setup, T callback) {
3291 T callback) {
3292 assert(keep_alive_max_count > 0); 4344 assert(keep_alive_max_count > 0);
3293 4345
3294 SSL *ssl = nullptr; 4346 SSL *ssl = nullptr;
@@ -3316,7 +4368,7 @@ inline bool process_and_close_socket_ssl(bool is_client_request, socket_t sock,
3316 return false; 4368 return false;
3317 } 4369 }
3318 4370
3319 bool ret = false; 4371 auto ret = false;
3320 4372
3321 if (SSL_connect_or_accept(ssl) == 1) { 4373 if (SSL_connect_or_accept(ssl) == 1) {
3322 if (keep_alive_max_count > 1) { 4374 if (keep_alive_max_count > 1) {
@@ -3325,7 +4377,7 @@ inline bool process_and_close_socket_ssl(bool is_client_request, socket_t sock,
3325 (is_client_request || 4377 (is_client_request ||
3326 detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, 4378 detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND,
3327 CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0)) { 4379 CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0)) {
3328 SSLSocketStream strm(sock, ssl); 4380 SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec);
3329 auto last_connection = count == 1; 4381 auto last_connection = count == 1;
3330 auto connection_close = false; 4382 auto connection_close = false;
3331 4383
@@ -3335,7 +4387,7 @@ inline bool process_and_close_socket_ssl(bool is_client_request, socket_t sock,
3335 count--; 4387 count--;
3336 } 4388 }
3337 } else { 4389 } else {
3338 SSLSocketStream strm(sock, ssl); 4390 SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec);
3339 auto dummy_connection_close = false; 4391 auto dummy_connection_close = false;
3340 ret = callback(ssl, strm, true, dummy_connection_close); 4392 ret = callback(ssl, strm, true, dummy_connection_close);
3341 } 4393 }
@@ -3382,11 +4434,20 @@ private:
3382class SSLInit { 4434class SSLInit {
3383public: 4435public:
3384 SSLInit() { 4436 SSLInit() {
4437#if OPENSSL_VERSION_NUMBER < 0x1010001fL
3385 SSL_load_error_strings(); 4438 SSL_load_error_strings();
3386 SSL_library_init(); 4439 SSL_library_init();
4440#else
4441 OPENSSL_init_ssl(
4442 OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
4443#endif
3387 } 4444 }
3388 4445
3389 ~SSLInit() { ERR_free_strings(); } 4446 ~SSLInit() {
4447#if OPENSSL_VERSION_NUMBER < 0x1010001fL
4448 ERR_free_strings();
4449#endif
4450 }
3390 4451
3391private: 4452private:
3392#if OPENSSL_VERSION_NUMBER < 0x10100000L 4453#if OPENSSL_VERSION_NUMBER < 0x10100000L
@@ -3394,41 +4455,44 @@ private:
3394#endif 4455#endif
3395}; 4456};
3396 4457
3397static SSLInit sslinit_;
3398
3399} // namespace detail
3400
3401// SSL socket stream implementation 4458// SSL socket stream implementation
3402inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl) 4459inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl,
3403 : sock_(sock), ssl_(ssl) {} 4460 time_t read_timeout_sec,
4461 time_t read_timeout_usec)
4462 : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec),
4463 read_timeout_usec_(read_timeout_usec) {}
3404 4464
3405inline SSLSocketStream::~SSLSocketStream() {} 4465inline SSLSocketStream::~SSLSocketStream() {}
3406 4466
4467inline bool SSLSocketStream::is_readable() const {
4468 return detail::select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0;
4469}
4470
4471inline bool SSLSocketStream::is_writable() const {
4472 return detail::select_write(sock_, 0, 0) > 0;
4473}
4474
3407inline int SSLSocketStream::read(char *ptr, size_t size) { 4475inline int SSLSocketStream::read(char *ptr, size_t size) {
3408 if (SSL_pending(ssl_) > 0 || 4476 if (SSL_pending(ssl_) > 0 ||
3409 detail::select_read(sock_, CPPHTTPLIB_READ_TIMEOUT_SECOND, 4477 select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0) {
3410 CPPHTTPLIB_READ_TIMEOUT_USECOND) > 0) {
3411 return SSL_read(ssl_, ptr, static_cast<int>(size)); 4478 return SSL_read(ssl_, ptr, static_cast<int>(size));
3412 } 4479 }
3413 return -1; 4480 return -1;
3414} 4481}
3415 4482
3416inline int SSLSocketStream::write(const char *ptr, size_t size) { 4483inline int SSLSocketStream::write(const char *ptr, size_t size) {
3417 return SSL_write(ssl_, ptr, static_cast<int>(size)); 4484 if (is_writable()) { return SSL_write(ssl_, ptr, static_cast<int>(size)); }
3418} 4485 return -1;
3419
3420inline int SSLSocketStream::write(const char *ptr) {
3421 return write(ptr, strlen(ptr));
3422}
3423
3424inline int SSLSocketStream::write(const std::string &s) {
3425 return write(s.data(), s.size());
3426} 4486}
3427 4487
3428inline std::string SSLSocketStream::get_remote_addr() const { 4488inline std::string SSLSocketStream::get_remote_addr() const {
3429 return detail::get_remote_addr(sock_); 4489 return detail::get_remote_addr(sock_);
3430} 4490}
3431 4491
4492static SSLInit sslinit_;
4493
4494} // namespace detail
4495
3432// SSL HTTP server implementation 4496// SSL HTTP server implementation
3433inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, 4497inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path,
3434 const char *client_ca_cert_file_path, 4498 const char *client_ca_cert_file_path,
@@ -3476,8 +4540,8 @@ inline bool SSLServer::is_valid() const { return ctx_; }
3476 4540
3477inline bool SSLServer::process_and_close_socket(socket_t sock) { 4541inline bool SSLServer::process_and_close_socket(socket_t sock) {
3478 return detail::process_and_close_socket_ssl( 4542 return detail::process_and_close_socket_ssl(
3479 false, sock, keep_alive_max_count_, ctx_, ctx_mutex_, SSL_accept, 4543 false, sock, keep_alive_max_count_, read_timeout_sec_, read_timeout_usec_,
3480 [](SSL * /*ssl*/) { return true; }, 4544 ctx_, ctx_mutex_, SSL_accept, [](SSL * /*ssl*/) { return true; },
3481 [this](SSL *ssl, Stream &strm, bool last_connection, 4545 [this](SSL *ssl, Stream &strm, bool last_connection,
3482 bool &connection_close) { 4546 bool &connection_close) {
3483 return process_request(strm, last_connection, connection_close, 4547 return process_request(strm, last_connection, connection_close,
@@ -3486,21 +4550,21 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
3486} 4550}
3487 4551
3488// SSL HTTP client implementation 4552// SSL HTTP client implementation
3489inline SSLClient::SSLClient(const char *host, int port, time_t timeout_sec, 4553inline SSLClient::SSLClient(const std::string &host, int port,
3490 const char *client_cert_path, 4554 const std::string &client_cert_path,
3491 const char *client_key_path) 4555 const std::string &client_key_path)
3492 : Client(host, port, timeout_sec) { 4556 : Client(host, port, client_cert_path, client_key_path) {
3493 ctx_ = SSL_CTX_new(SSLv23_client_method()); 4557 ctx_ = SSL_CTX_new(SSLv23_client_method());
3494 4558
3495 detail::split(&host_[0], &host_[host_.size()], '.', 4559 detail::split(&host_[0], &host_[host_.size()], '.',
3496 [&](const char *b, const char *e) { 4560 [&](const char *b, const char *e) {
3497 host_components_.emplace_back(std::string(b, e)); 4561 host_components_.emplace_back(std::string(b, e));
3498 }); 4562 });
3499 if (client_cert_path && client_key_path) { 4563 if (!client_cert_path.empty() && !client_key_path.empty()) {
3500 if (SSL_CTX_use_certificate_file(ctx_, client_cert_path, 4564 if (SSL_CTX_use_certificate_file(ctx_, client_cert_path.c_str(),
3501 SSL_FILETYPE_PEM) != 1 || 4565 SSL_FILETYPE_PEM) != 1 ||
3502 SSL_CTX_use_PrivateKey_file(ctx_, client_key_path, SSL_FILETYPE_PEM) != 4566 SSL_CTX_use_PrivateKey_file(ctx_, client_key_path.c_str(),
3503 1) { 4567 SSL_FILETYPE_PEM) != 1) {
3504 SSL_CTX_free(ctx_); 4568 SSL_CTX_free(ctx_);
3505 ctx_ = nullptr; 4569 ctx_ = nullptr;
3506 } 4570 }
@@ -3527,9 +4591,7 @@ inline long SSLClient::get_openssl_verify_result() const {
3527 return verify_result_; 4591 return verify_result_;
3528} 4592}
3529 4593
3530inline SSL_CTX* SSLClient::ssl_context() const noexcept { 4594inline SSL_CTX *SSLClient::ssl_context() const noexcept { return ctx_; }
3531 return ctx_;
3532}
3533 4595
3534inline bool SSLClient::process_and_close_socket( 4596inline bool SSLClient::process_and_close_socket(
3535 socket_t sock, size_t request_count, 4597 socket_t sock, size_t request_count,
@@ -3541,7 +4603,8 @@ inline bool SSLClient::process_and_close_socket(
3541 4603
3542 return is_valid() && 4604 return is_valid() &&
3543 detail::process_and_close_socket_ssl( 4605 detail::process_and_close_socket_ssl(
3544 true, sock, request_count, ctx_, ctx_mutex_, 4606 true, sock, request_count, read_timeout_sec_, read_timeout_usec_,
4607 ctx_, ctx_mutex_,
3545 [&](SSL *ssl) { 4608 [&](SSL *ssl) {
3546 if (ca_cert_file_path_.empty()) { 4609 if (ca_cert_file_path_.empty()) {
3547 SSL_CTX_set_verify(ctx_, SSL_VERIFY_NONE, nullptr); 4610 SSL_CTX_set_verify(ctx_, SSL_VERIFY_NONE, nullptr);
@@ -3712,6 +4775,8 @@ inline bool SSLClient::check_host_name(const char *pattern,
3712} 4775}
3713#endif 4776#endif
3714 4777
4778// ----------------------------------------------------------------------------
4779
3715} // namespace httplib 4780} // namespace httplib
3716 4781
3717#endif // CPPHTTPLIB_HTTPLIB_H 4782#endif // CPPHTTPLIB_HTTPLIB_H
diff --git a/src/core/hle/service/bcat/backend/boxcat.cpp b/src/core/hle/service/bcat/backend/boxcat.cpp
index 67e39a5c4..f589864ee 100644
--- a/src/core/hle/service/bcat/backend/boxcat.cpp
+++ b/src/core/hle/service/bcat/backend/boxcat.cpp
@@ -200,7 +200,8 @@ private:
200 DownloadResult DownloadInternal(const std::string& resolved_path, u32 timeout_seconds, 200 DownloadResult DownloadInternal(const std::string& resolved_path, u32 timeout_seconds,
201 const std::string& content_type_name) { 201 const std::string& content_type_name) {
202 if (client == nullptr) { 202 if (client == nullptr) {
203 client = std::make_unique<httplib::SSLClient>(BOXCAT_HOSTNAME, PORT, timeout_seconds); 203 client = std::make_unique<httplib::SSLClient>(BOXCAT_HOSTNAME, PORT);
204 client->set_timeout_sec(timeout_seconds);
204 } 205 }
205 206
206 httplib::Headers headers{ 207 httplib::Headers headers{
@@ -448,8 +449,8 @@ std::optional<std::vector<u8>> Boxcat::GetLaunchParameter(TitleIDVersion title)
448 449
449Boxcat::StatusResult Boxcat::GetStatus(std::optional<std::string>& global, 450Boxcat::StatusResult Boxcat::GetStatus(std::optional<std::string>& global,
450 std::map<std::string, EventStatus>& games) { 451 std::map<std::string, EventStatus>& games) {
451 httplib::SSLClient client{BOXCAT_HOSTNAME, static_cast<int>(PORT), 452 httplib::SSLClient client{BOXCAT_HOSTNAME, static_cast<int>(PORT)};
452 static_cast<int>(TIMEOUT_SECONDS)}; 453 client.set_timeout_sec(static_cast<int>(TIMEOUT_SECONDS));
453 454
454 httplib::Headers headers{ 455 httplib::Headers headers{
455 {std::string("Game-Assets-API-Version"), std::string(BOXCAT_API_VERSION)}, 456 {std::string("Game-Assets-API-Version"), std::string(BOXCAT_API_VERSION)},
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt
index db9332d00..4b0c6346f 100644
--- a/src/video_core/CMakeLists.txt
+++ b/src/video_core/CMakeLists.txt
@@ -37,6 +37,7 @@ add_library(video_core STATIC
37 memory_manager.h 37 memory_manager.h
38 morton.cpp 38 morton.cpp
39 morton.h 39 morton.h
40 query_cache.h
40 rasterizer_accelerated.cpp 41 rasterizer_accelerated.cpp
41 rasterizer_accelerated.h 42 rasterizer_accelerated.h
42 rasterizer_cache.cpp 43 rasterizer_cache.cpp
@@ -74,6 +75,8 @@ add_library(video_core STATIC
74 renderer_opengl/gl_stream_buffer.h 75 renderer_opengl/gl_stream_buffer.h
75 renderer_opengl/gl_texture_cache.cpp 76 renderer_opengl/gl_texture_cache.cpp
76 renderer_opengl/gl_texture_cache.h 77 renderer_opengl/gl_texture_cache.h
78 renderer_opengl/gl_query_cache.cpp
79 renderer_opengl/gl_query_cache.h
77 renderer_opengl/maxwell_to_gl.h 80 renderer_opengl/maxwell_to_gl.h
78 renderer_opengl/renderer_opengl.cpp 81 renderer_opengl/renderer_opengl.cpp
79 renderer_opengl/renderer_opengl.h 82 renderer_opengl/renderer_opengl.h
@@ -177,6 +180,8 @@ if (ENABLE_VULKAN)
177 renderer_vulkan/vk_memory_manager.h 180 renderer_vulkan/vk_memory_manager.h
178 renderer_vulkan/vk_pipeline_cache.cpp 181 renderer_vulkan/vk_pipeline_cache.cpp
179 renderer_vulkan/vk_pipeline_cache.h 182 renderer_vulkan/vk_pipeline_cache.h
183 renderer_vulkan/vk_query_cache.cpp
184 renderer_vulkan/vk_query_cache.h
180 renderer_vulkan/vk_rasterizer.cpp 185 renderer_vulkan/vk_rasterizer.cpp
181 renderer_vulkan/vk_rasterizer.h 186 renderer_vulkan/vk_rasterizer.h
182 renderer_vulkan/vk_renderpass_cache.cpp 187 renderer_vulkan/vk_renderpass_cache.cpp
diff --git a/src/video_core/engines/maxwell_3d.cpp b/src/video_core/engines/maxwell_3d.cpp
index 0b3e8749b..b28de1092 100644
--- a/src/video_core/engines/maxwell_3d.cpp
+++ b/src/video_core/engines/maxwell_3d.cpp
@@ -4,6 +4,7 @@
4 4
5#include <cinttypes> 5#include <cinttypes>
6#include <cstring> 6#include <cstring>
7#include <optional>
7#include "common/assert.h" 8#include "common/assert.h"
8#include "core/core.h" 9#include "core/core.h"
9#include "core/core_timing.h" 10#include "core/core_timing.h"
@@ -16,6 +17,8 @@
16 17
17namespace Tegra::Engines { 18namespace Tegra::Engines {
18 19
20using VideoCore::QueryType;
21
19/// First register id that is actually a Macro call. 22/// First register id that is actually a Macro call.
20constexpr u32 MacroRegistersStart = 0xE00; 23constexpr u32 MacroRegistersStart = 0xE00;
21 24
@@ -400,6 +403,10 @@ void Maxwell3D::CallMethod(const GPU::MethodCall& method_call) {
400 ProcessQueryCondition(); 403 ProcessQueryCondition();
401 break; 404 break;
402 } 405 }
406 case MAXWELL3D_REG_INDEX(counter_reset): {
407 ProcessCounterReset();
408 break;
409 }
403 case MAXWELL3D_REG_INDEX(sync_info): { 410 case MAXWELL3D_REG_INDEX(sync_info): {
404 ProcessSyncPoint(); 411 ProcessSyncPoint();
405 break; 412 break;
@@ -482,7 +489,7 @@ void Maxwell3D::FlushMMEInlineDraw() {
482 489
483 const bool is_indexed = mme_draw.current_mode == MMEDrawMode::Indexed; 490 const bool is_indexed = mme_draw.current_mode == MMEDrawMode::Indexed;
484 if (ShouldExecute()) { 491 if (ShouldExecute()) {
485 rasterizer.DrawMultiBatch(is_indexed); 492 rasterizer.Draw(is_indexed, true);
486 } 493 }
487 494
488 // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if 495 // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if
@@ -544,40 +551,28 @@ void Maxwell3D::ProcessQueryGet() {
544 "Units other than CROP are unimplemented"); 551 "Units other than CROP are unimplemented");
545 552
546 switch (regs.query.query_get.operation) { 553 switch (regs.query.query_get.operation) {
547 case Regs::QueryOperation::Release: { 554 case Regs::QueryOperation::Release:
548 const u64 result = regs.query.query_sequence; 555 StampQueryResult(regs.query.query_sequence, regs.query.query_get.short_query == 0);
549 StampQueryResult(result, regs.query.query_get.short_query == 0);
550 break; 556 break;
551 } 557 case Regs::QueryOperation::Acquire:
552 case Regs::QueryOperation::Acquire: { 558 // TODO(Blinkhawk): Under this operation, the GPU waits for the CPU to write a value that
553 // Todo(Blinkhawk): Under this operation, the GPU waits for the CPU 559 // matches the current payload.
554 // to write a value that matches the current payload.
555 UNIMPLEMENTED_MSG("Unimplemented query operation ACQUIRE"); 560 UNIMPLEMENTED_MSG("Unimplemented query operation ACQUIRE");
556 break; 561 break;
557 } 562 case Regs::QueryOperation::Counter:
558 case Regs::QueryOperation::Counter: { 563 if (const std::optional<u64> result = GetQueryResult()) {
559 u64 result{}; 564 // If the query returns an empty optional it means it's cached and deferred.
560 switch (regs.query.query_get.select) { 565 // In this case we have a non-empty result, so we stamp it immediately.
561 case Regs::QuerySelect::Zero: 566 StampQueryResult(*result, regs.query.query_get.short_query == 0);
562 result = 0;
563 break;
564 default:
565 result = 1;
566 UNIMPLEMENTED_MSG("Unimplemented query select type {}",
567 static_cast<u32>(regs.query.query_get.select.Value()));
568 } 567 }
569 StampQueryResult(result, regs.query.query_get.short_query == 0);
570 break; 568 break;
571 } 569 case Regs::QueryOperation::Trap:
572 case Regs::QueryOperation::Trap: {
573 UNIMPLEMENTED_MSG("Unimplemented query operation TRAP"); 570 UNIMPLEMENTED_MSG("Unimplemented query operation TRAP");
574 break; 571 break;
575 } 572 default:
576 default: {
577 UNIMPLEMENTED_MSG("Unknown query operation"); 573 UNIMPLEMENTED_MSG("Unknown query operation");
578 break; 574 break;
579 } 575 }
580 }
581} 576}
582 577
583void Maxwell3D::ProcessQueryCondition() { 578void Maxwell3D::ProcessQueryCondition() {
@@ -593,20 +588,20 @@ void Maxwell3D::ProcessQueryCondition() {
593 } 588 }
594 case Regs::ConditionMode::ResNonZero: { 589 case Regs::ConditionMode::ResNonZero: {
595 Regs::QueryCompare cmp; 590 Regs::QueryCompare cmp;
596 memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp)); 591 memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
597 execute_on = cmp.initial_sequence != 0U && cmp.initial_mode != 0U; 592 execute_on = cmp.initial_sequence != 0U && cmp.initial_mode != 0U;
598 break; 593 break;
599 } 594 }
600 case Regs::ConditionMode::Equal: { 595 case Regs::ConditionMode::Equal: {
601 Regs::QueryCompare cmp; 596 Regs::QueryCompare cmp;
602 memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp)); 597 memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
603 execute_on = 598 execute_on =
604 cmp.initial_sequence == cmp.current_sequence && cmp.initial_mode == cmp.current_mode; 599 cmp.initial_sequence == cmp.current_sequence && cmp.initial_mode == cmp.current_mode;
605 break; 600 break;
606 } 601 }
607 case Regs::ConditionMode::NotEqual: { 602 case Regs::ConditionMode::NotEqual: {
608 Regs::QueryCompare cmp; 603 Regs::QueryCompare cmp;
609 memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp)); 604 memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
610 execute_on = 605 execute_on =
611 cmp.initial_sequence != cmp.current_sequence || cmp.initial_mode != cmp.current_mode; 606 cmp.initial_sequence != cmp.current_sequence || cmp.initial_mode != cmp.current_mode;
612 break; 607 break;
@@ -619,6 +614,18 @@ void Maxwell3D::ProcessQueryCondition() {
619 } 614 }
620} 615}
621 616
617void Maxwell3D::ProcessCounterReset() {
618 switch (regs.counter_reset) {
619 case Regs::CounterReset::SampleCnt:
620 rasterizer.ResetCounter(QueryType::SamplesPassed);
621 break;
622 default:
623 LOG_WARNING(Render_OpenGL, "Unimplemented counter reset={}",
624 static_cast<int>(regs.counter_reset));
625 break;
626 }
627}
628
622void Maxwell3D::ProcessSyncPoint() { 629void Maxwell3D::ProcessSyncPoint() {
623 const u32 sync_point = regs.sync_info.sync_point.Value(); 630 const u32 sync_point = regs.sync_info.sync_point.Value();
624 const u32 increment = regs.sync_info.increment.Value(); 631 const u32 increment = regs.sync_info.increment.Value();
@@ -647,7 +654,7 @@ void Maxwell3D::DrawArrays() {
647 654
648 const bool is_indexed{regs.index_array.count && !regs.vertex_buffer.count}; 655 const bool is_indexed{regs.index_array.count && !regs.vertex_buffer.count};
649 if (ShouldExecute()) { 656 if (ShouldExecute()) {
650 rasterizer.DrawBatch(is_indexed); 657 rasterizer.Draw(is_indexed, false);
651 } 658 }
652 659
653 // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if 660 // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if
@@ -661,6 +668,22 @@ void Maxwell3D::DrawArrays() {
661 } 668 }
662} 669}
663 670
671std::optional<u64> Maxwell3D::GetQueryResult() {
672 switch (regs.query.query_get.select) {
673 case Regs::QuerySelect::Zero:
674 return 0;
675 case Regs::QuerySelect::SamplesPassed:
676 // Deferred.
677 rasterizer.Query(regs.query.QueryAddress(), VideoCore::QueryType::SamplesPassed,
678 system.GPU().GetTicks());
679 return {};
680 default:
681 UNIMPLEMENTED_MSG("Unimplemented query select type {}",
682 static_cast<u32>(regs.query.query_get.select.Value()));
683 return 1;
684 }
685}
686
664void Maxwell3D::ProcessCBBind(std::size_t stage_index) { 687void Maxwell3D::ProcessCBBind(std::size_t stage_index) {
665 // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader stage. 688 // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader stage.
666 auto& shader = state.shader_stages[stage_index]; 689 auto& shader = state.shader_stages[stage_index];
diff --git a/src/video_core/engines/maxwell_3d.h b/src/video_core/engines/maxwell_3d.h
index 0a2af54e5..26939be3f 100644
--- a/src/video_core/engines/maxwell_3d.h
+++ b/src/video_core/engines/maxwell_3d.h
@@ -6,6 +6,7 @@
6 6
7#include <array> 7#include <array>
8#include <bitset> 8#include <bitset>
9#include <optional>
9#include <type_traits> 10#include <type_traits>
10#include <unordered_map> 11#include <unordered_map>
11#include <vector> 12#include <vector>
@@ -409,6 +410,27 @@ public:
409 Linear = 1, 410 Linear = 1,
410 }; 411 };
411 412
413 enum class CounterReset : u32 {
414 SampleCnt = 0x01,
415 Unk02 = 0x02,
416 Unk03 = 0x03,
417 Unk04 = 0x04,
418 EmittedPrimitives = 0x10, // Not tested
419 Unk11 = 0x11,
420 Unk12 = 0x12,
421 Unk13 = 0x13,
422 Unk15 = 0x15,
423 Unk16 = 0x16,
424 Unk17 = 0x17,
425 Unk18 = 0x18,
426 Unk1A = 0x1A,
427 Unk1B = 0x1B,
428 Unk1C = 0x1C,
429 Unk1D = 0x1D,
430 Unk1E = 0x1E,
431 GeneratedPrimitives = 0x1F,
432 };
433
412 struct Cull { 434 struct Cull {
413 enum class FrontFace : u32 { 435 enum class FrontFace : u32 {
414 ClockWise = 0x0900, 436 ClockWise = 0x0900,
@@ -857,7 +879,7 @@ public:
857 BitField<7, 1, u32> c7; 879 BitField<7, 1, u32> c7;
858 } clip_distance_enabled; 880 } clip_distance_enabled;
859 881
860 INSERT_UNION_PADDING_WORDS(0x1); 882 u32 samplecnt_enable;
861 883
862 float point_size; 884 float point_size;
863 885
@@ -865,7 +887,11 @@ public:
865 887
866 u32 point_sprite_enable; 888 u32 point_sprite_enable;
867 889
868 INSERT_UNION_PADDING_WORDS(0x5); 890 INSERT_UNION_PADDING_WORDS(0x3);
891
892 CounterReset counter_reset;
893
894 INSERT_UNION_PADDING_WORDS(0x1);
869 895
870 u32 zeta_enable; 896 u32 zeta_enable;
871 897
@@ -1412,12 +1438,15 @@ private:
1412 /// Handles a write to the QUERY_GET register. 1438 /// Handles a write to the QUERY_GET register.
1413 void ProcessQueryGet(); 1439 void ProcessQueryGet();
1414 1440
1415 // Writes the query result accordingly 1441 /// Writes the query result accordingly.
1416 void StampQueryResult(u64 payload, bool long_query); 1442 void StampQueryResult(u64 payload, bool long_query);
1417 1443
1418 // Handles Conditional Rendering 1444 /// Handles conditional rendering.
1419 void ProcessQueryCondition(); 1445 void ProcessQueryCondition();
1420 1446
1447 /// Handles counter resets.
1448 void ProcessCounterReset();
1449
1421 /// Handles writes to syncing register. 1450 /// Handles writes to syncing register.
1422 void ProcessSyncPoint(); 1451 void ProcessSyncPoint();
1423 1452
@@ -1434,6 +1463,9 @@ private:
1434 1463
1435 // Handles a instance drawcall from MME 1464 // Handles a instance drawcall from MME
1436 void StepInstance(MMEDrawMode expected_mode, u32 count); 1465 void StepInstance(MMEDrawMode expected_mode, u32 count);
1466
1467 /// Returns a query's value or an empty object if the value will be deferred through a cache.
1468 std::optional<u64> GetQueryResult();
1437}; 1469};
1438 1470
1439#define ASSERT_REG_POSITION(field_name, position) \ 1471#define ASSERT_REG_POSITION(field_name, position) \
@@ -1499,8 +1531,10 @@ ASSERT_REG_POSITION(screen_y_control, 0x4EB);
1499ASSERT_REG_POSITION(vb_element_base, 0x50D); 1531ASSERT_REG_POSITION(vb_element_base, 0x50D);
1500ASSERT_REG_POSITION(vb_base_instance, 0x50E); 1532ASSERT_REG_POSITION(vb_base_instance, 0x50E);
1501ASSERT_REG_POSITION(clip_distance_enabled, 0x544); 1533ASSERT_REG_POSITION(clip_distance_enabled, 0x544);
1534ASSERT_REG_POSITION(samplecnt_enable, 0x545);
1502ASSERT_REG_POSITION(point_size, 0x546); 1535ASSERT_REG_POSITION(point_size, 0x546);
1503ASSERT_REG_POSITION(point_sprite_enable, 0x548); 1536ASSERT_REG_POSITION(point_sprite_enable, 0x548);
1537ASSERT_REG_POSITION(counter_reset, 0x54C);
1504ASSERT_REG_POSITION(zeta_enable, 0x54E); 1538ASSERT_REG_POSITION(zeta_enable, 0x54E);
1505ASSERT_REG_POSITION(multisample_control, 0x54F); 1539ASSERT_REG_POSITION(multisample_control, 0x54F);
1506ASSERT_REG_POSITION(condition, 0x554); 1540ASSERT_REG_POSITION(condition, 0x554);
diff --git a/src/video_core/memory_manager.cpp b/src/video_core/memory_manager.cpp
index 11848fbce..f5d33f27a 100644
--- a/src/video_core/memory_manager.cpp
+++ b/src/video_core/memory_manager.cpp
@@ -9,6 +9,7 @@
9#include "core/hle/kernel/process.h" 9#include "core/hle/kernel/process.h"
10#include "core/hle/kernel/vm_manager.h" 10#include "core/hle/kernel/vm_manager.h"
11#include "core/memory.h" 11#include "core/memory.h"
12#include "video_core/gpu.h"
12#include "video_core/memory_manager.h" 13#include "video_core/memory_manager.h"
13#include "video_core/rasterizer_interface.h" 14#include "video_core/rasterizer_interface.h"
14 15
@@ -84,7 +85,9 @@ GPUVAddr MemoryManager::UnmapBuffer(GPUVAddr gpu_addr, u64 size) {
84 const auto cpu_addr = GpuToCpuAddress(gpu_addr); 85 const auto cpu_addr = GpuToCpuAddress(gpu_addr);
85 ASSERT(cpu_addr); 86 ASSERT(cpu_addr);
86 87
87 rasterizer.FlushAndInvalidateRegion(cache_addr, aligned_size); 88 // Flush and invalidate through the GPU interface, to be asynchronous if possible.
89 system.GPU().FlushAndInvalidateRegion(cache_addr, aligned_size);
90
88 UnmapRange(gpu_addr, aligned_size); 91 UnmapRange(gpu_addr, aligned_size);
89 ASSERT(system.CurrentProcess() 92 ASSERT(system.CurrentProcess()
90 ->VMManager() 93 ->VMManager()
@@ -242,6 +245,8 @@ void MemoryManager::ReadBlock(GPUVAddr src_addr, void* dest_buffer, const std::s
242 switch (page_table.attributes[page_index]) { 245 switch (page_table.attributes[page_index]) {
243 case Common::PageType::Memory: { 246 case Common::PageType::Memory: {
244 const u8* src_ptr{page_table.pointers[page_index] + page_offset}; 247 const u8* src_ptr{page_table.pointers[page_index] + page_offset};
248 // Flush must happen on the rasterizer interface, such that memory is always synchronous
249 // when it is read (even when in asynchronous GPU mode). Fixes Dead Cells title menu.
245 rasterizer.FlushRegion(ToCacheAddr(src_ptr), copy_amount); 250 rasterizer.FlushRegion(ToCacheAddr(src_ptr), copy_amount);
246 std::memcpy(dest_buffer, src_ptr, copy_amount); 251 std::memcpy(dest_buffer, src_ptr, copy_amount);
247 break; 252 break;
@@ -292,6 +297,8 @@ void MemoryManager::WriteBlock(GPUVAddr dest_addr, const void* src_buffer, const
292 switch (page_table.attributes[page_index]) { 297 switch (page_table.attributes[page_index]) {
293 case Common::PageType::Memory: { 298 case Common::PageType::Memory: {
294 u8* dest_ptr{page_table.pointers[page_index] + page_offset}; 299 u8* dest_ptr{page_table.pointers[page_index] + page_offset};
300 // Invalidate must happen on the rasterizer interface, such that memory is always
301 // synchronous when it is written (even when in asynchronous GPU mode).
295 rasterizer.InvalidateRegion(ToCacheAddr(dest_ptr), copy_amount); 302 rasterizer.InvalidateRegion(ToCacheAddr(dest_ptr), copy_amount);
296 std::memcpy(dest_ptr, src_buffer, copy_amount); 303 std::memcpy(dest_ptr, src_buffer, copy_amount);
297 break; 304 break;
@@ -339,6 +346,8 @@ void MemoryManager::CopyBlock(GPUVAddr dest_addr, GPUVAddr src_addr, const std::
339 346
340 switch (page_table.attributes[page_index]) { 347 switch (page_table.attributes[page_index]) {
341 case Common::PageType::Memory: { 348 case Common::PageType::Memory: {
349 // Flush must happen on the rasterizer interface, such that memory is always synchronous
350 // when it is copied (even when in asynchronous GPU mode).
342 const u8* src_ptr{page_table.pointers[page_index] + page_offset}; 351 const u8* src_ptr{page_table.pointers[page_index] + page_offset};
343 rasterizer.FlushRegion(ToCacheAddr(src_ptr), copy_amount); 352 rasterizer.FlushRegion(ToCacheAddr(src_ptr), copy_amount);
344 WriteBlock(dest_addr, src_ptr, copy_amount); 353 WriteBlock(dest_addr, src_ptr, copy_amount);
diff --git a/src/video_core/query_cache.h b/src/video_core/query_cache.h
new file mode 100644
index 000000000..e66054ed0
--- /dev/null
+++ b/src/video_core/query_cache.h
@@ -0,0 +1,359 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <algorithm>
8#include <array>
9#include <cstring>
10#include <iterator>
11#include <memory>
12#include <mutex>
13#include <optional>
14#include <unordered_map>
15#include <vector>
16
17#include "common/assert.h"
18#include "core/core.h"
19#include "video_core/engines/maxwell_3d.h"
20#include "video_core/gpu.h"
21#include "video_core/memory_manager.h"
22#include "video_core/rasterizer_interface.h"
23
24namespace VideoCommon {
25
26template <class QueryCache, class HostCounter>
27class CounterStreamBase {
28public:
29 explicit CounterStreamBase(QueryCache& cache, VideoCore::QueryType type)
30 : cache{cache}, type{type} {}
31
32 /// Updates the state of the stream, enabling or disabling as needed.
33 void Update(bool enabled) {
34 if (enabled) {
35 Enable();
36 } else {
37 Disable();
38 }
39 }
40
41 /// Resets the stream to zero. It doesn't disable the query after resetting.
42 void Reset() {
43 if (current) {
44 current->EndQuery();
45
46 // Immediately start a new query to avoid disabling its state.
47 current = cache.Counter(nullptr, type);
48 }
49 last = nullptr;
50 }
51
52 /// Returns the current counter slicing as needed.
53 std::shared_ptr<HostCounter> Current() {
54 if (!current) {
55 return nullptr;
56 }
57 current->EndQuery();
58 last = std::move(current);
59 current = cache.Counter(last, type);
60 return last;
61 }
62
63 /// Returns true when the counter stream is enabled.
64 bool IsEnabled() const {
65 return current != nullptr;
66 }
67
68private:
69 /// Enables the stream.
70 void Enable() {
71 if (current) {
72 return;
73 }
74 current = cache.Counter(last, type);
75 }
76
77 // Disables the stream.
78 void Disable() {
79 if (current) {
80 current->EndQuery();
81 }
82 last = std::exchange(current, nullptr);
83 }
84
85 QueryCache& cache;
86 const VideoCore::QueryType type;
87
88 std::shared_ptr<HostCounter> current;
89 std::shared_ptr<HostCounter> last;
90};
91
92template <class QueryCache, class CachedQuery, class CounterStream, class HostCounter,
93 class QueryPool>
94class QueryCacheBase {
95public:
96 explicit QueryCacheBase(Core::System& system, VideoCore::RasterizerInterface& rasterizer)
97 : system{system}, rasterizer{rasterizer}, streams{{CounterStream{
98 static_cast<QueryCache&>(*this),
99 VideoCore::QueryType::SamplesPassed}}} {}
100
101 void InvalidateRegion(CacheAddr addr, std::size_t size) {
102 std::unique_lock lock{mutex};
103 FlushAndRemoveRegion(addr, size);
104 }
105
106 void FlushRegion(CacheAddr addr, std::size_t size) {
107 std::unique_lock lock{mutex};
108 FlushAndRemoveRegion(addr, size);
109 }
110
111 /**
112 * Records a query in GPU mapped memory, potentially marked with a timestamp.
113 * @param gpu_addr GPU address to flush to when the mapped memory is read.
114 * @param type Query type, e.g. SamplesPassed.
115 * @param timestamp Timestamp, when empty the flushed query is assumed to be short.
116 */
117 void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) {
118 std::unique_lock lock{mutex};
119 auto& memory_manager = system.GPU().MemoryManager();
120 const auto host_ptr = memory_manager.GetPointer(gpu_addr);
121
122 CachedQuery* query = TryGet(ToCacheAddr(host_ptr));
123 if (!query) {
124 const auto cpu_addr = memory_manager.GpuToCpuAddress(gpu_addr);
125 ASSERT_OR_EXECUTE(cpu_addr, return;);
126
127 query = Register(type, *cpu_addr, host_ptr, timestamp.has_value());
128 }
129
130 query->BindCounter(Stream(type).Current(), timestamp);
131 }
132
133 /// Updates counters from GPU state. Expected to be called once per draw, clear or dispatch.
134 void UpdateCounters() {
135 std::unique_lock lock{mutex};
136 const auto& regs = system.GPU().Maxwell3D().regs;
137 Stream(VideoCore::QueryType::SamplesPassed).Update(regs.samplecnt_enable);
138 }
139
140 /// Resets a counter to zero. It doesn't disable the query after resetting.
141 void ResetCounter(VideoCore::QueryType type) {
142 std::unique_lock lock{mutex};
143 Stream(type).Reset();
144 }
145
146 /// Disable all active streams. Expected to be called at the end of a command buffer.
147 void DisableStreams() {
148 std::unique_lock lock{mutex};
149 for (auto& stream : streams) {
150 stream.Update(false);
151 }
152 }
153
154 /// Returns a new host counter.
155 std::shared_ptr<HostCounter> Counter(std::shared_ptr<HostCounter> dependency,
156 VideoCore::QueryType type) {
157 return std::make_shared<HostCounter>(static_cast<QueryCache&>(*this), std::move(dependency),
158 type);
159 }
160
161 /// Returns the counter stream of the specified type.
162 CounterStream& Stream(VideoCore::QueryType type) {
163 return streams[static_cast<std::size_t>(type)];
164 }
165
166 /// Returns the counter stream of the specified type.
167 const CounterStream& Stream(VideoCore::QueryType type) const {
168 return streams[static_cast<std::size_t>(type)];
169 }
170
171protected:
172 std::array<QueryPool, VideoCore::NumQueryTypes> query_pools;
173
174private:
175 /// Flushes a memory range to guest memory and removes it from the cache.
176 void FlushAndRemoveRegion(CacheAddr addr, std::size_t size) {
177 const u64 addr_begin = static_cast<u64>(addr);
178 const u64 addr_end = addr_begin + static_cast<u64>(size);
179 const auto in_range = [addr_begin, addr_end](CachedQuery& query) {
180 const u64 cache_begin = query.GetCacheAddr();
181 const u64 cache_end = cache_begin + query.SizeInBytes();
182 return cache_begin < addr_end && addr_begin < cache_end;
183 };
184
185 const u64 page_end = addr_end >> PAGE_SHIFT;
186 for (u64 page = addr_begin >> PAGE_SHIFT; page <= page_end; ++page) {
187 const auto& it = cached_queries.find(page);
188 if (it == std::end(cached_queries)) {
189 continue;
190 }
191 auto& contents = it->second;
192 for (auto& query : contents) {
193 if (!in_range(query)) {
194 continue;
195 }
196 rasterizer.UpdatePagesCachedCount(query.CpuAddr(), query.SizeInBytes(), -1);
197 query.Flush();
198 }
199 contents.erase(std::remove_if(std::begin(contents), std::end(contents), in_range),
200 std::end(contents));
201 }
202 }
203
204 /// Registers the passed parameters as cached and returns a pointer to the stored cached query.
205 CachedQuery* Register(VideoCore::QueryType type, VAddr cpu_addr, u8* host_ptr, bool timestamp) {
206 rasterizer.UpdatePagesCachedCount(cpu_addr, CachedQuery::SizeInBytes(timestamp), 1);
207 const u64 page = static_cast<u64>(ToCacheAddr(host_ptr)) >> PAGE_SHIFT;
208 return &cached_queries[page].emplace_back(static_cast<QueryCache&>(*this), type, cpu_addr,
209 host_ptr);
210 }
211
212 /// Tries to a get a cached query. Returns nullptr on failure.
213 CachedQuery* TryGet(CacheAddr addr) {
214 const u64 page = static_cast<u64>(addr) >> PAGE_SHIFT;
215 const auto it = cached_queries.find(page);
216 if (it == std::end(cached_queries)) {
217 return nullptr;
218 }
219 auto& contents = it->second;
220 const auto found =
221 std::find_if(std::begin(contents), std::end(contents),
222 [addr](auto& query) { return query.GetCacheAddr() == addr; });
223 return found != std::end(contents) ? &*found : nullptr;
224 }
225
226 static constexpr std::uintptr_t PAGE_SIZE = 4096;
227 static constexpr unsigned PAGE_SHIFT = 12;
228
229 Core::System& system;
230 VideoCore::RasterizerInterface& rasterizer;
231
232 std::recursive_mutex mutex;
233
234 std::unordered_map<u64, std::vector<CachedQuery>> cached_queries;
235
236 std::array<CounterStream, VideoCore::NumQueryTypes> streams;
237};
238
239template <class QueryCache, class HostCounter>
240class HostCounterBase {
241public:
242 explicit HostCounterBase(std::shared_ptr<HostCounter> dependency_)
243 : dependency{std::move(dependency_)}, depth{dependency ? (dependency->Depth() + 1) : 0} {
244 // Avoid nesting too many dependencies to avoid a stack overflow when these are deleted.
245 constexpr u64 depth_threshold = 96;
246 if (depth > depth_threshold) {
247 depth = 0;
248 base_result = dependency->Query();
249 dependency = nullptr;
250 }
251 }
252 virtual ~HostCounterBase() = default;
253
254 /// Returns the current value of the query.
255 u64 Query() {
256 if (result) {
257 return *result;
258 }
259
260 u64 value = BlockingQuery() + base_result;
261 if (dependency) {
262 value += dependency->Query();
263 dependency = nullptr;
264 }
265
266 result = value;
267 return *result;
268 }
269
270 /// Returns true when flushing this query will potentially wait.
271 bool WaitPending() const noexcept {
272 return result.has_value();
273 }
274
275 u64 Depth() const noexcept {
276 return depth;
277 }
278
279protected:
280 /// Returns the value of query from the backend API blocking as needed.
281 virtual u64 BlockingQuery() const = 0;
282
283private:
284 std::shared_ptr<HostCounter> dependency; ///< Counter to add to this value.
285 std::optional<u64> result; ///< Filled with the already returned value.
286 u64 depth; ///< Number of nested dependencies.
287 u64 base_result = 0; ///< Equivalent to nested dependencies value.
288};
289
290template <class HostCounter>
291class CachedQueryBase {
292public:
293 explicit CachedQueryBase(VAddr cpu_addr, u8* host_ptr)
294 : cpu_addr{cpu_addr}, host_ptr{host_ptr} {}
295 virtual ~CachedQueryBase() = default;
296
297 CachedQueryBase(CachedQueryBase&&) noexcept = default;
298 CachedQueryBase(const CachedQueryBase&) = delete;
299
300 CachedQueryBase& operator=(CachedQueryBase&&) noexcept = default;
301 CachedQueryBase& operator=(const CachedQueryBase&) = delete;
302
303 /// Flushes the query to guest memory.
304 virtual void Flush() {
305 // When counter is nullptr it means that it's just been reseted. We are supposed to write a
306 // zero in these cases.
307 const u64 value = counter ? counter->Query() : 0;
308 std::memcpy(host_ptr, &value, sizeof(u64));
309
310 if (timestamp) {
311 std::memcpy(host_ptr + TIMESTAMP_OFFSET, &*timestamp, sizeof(u64));
312 }
313 }
314
315 /// Binds a counter to this query.
316 void BindCounter(std::shared_ptr<HostCounter> counter_, std::optional<u64> timestamp_) {
317 if (counter) {
318 // If there's an old counter set it means the query is being rewritten by the game.
319 // To avoid losing the data forever, flush here.
320 Flush();
321 }
322 counter = std::move(counter_);
323 timestamp = timestamp_;
324 }
325
326 VAddr CpuAddr() const noexcept {
327 return cpu_addr;
328 }
329
330 CacheAddr GetCacheAddr() const noexcept {
331 return ToCacheAddr(host_ptr);
332 }
333
334 u64 SizeInBytes() const noexcept {
335 return SizeInBytes(timestamp.has_value());
336 }
337
338 static constexpr u64 SizeInBytes(bool with_timestamp) noexcept {
339 return with_timestamp ? LARGE_QUERY_SIZE : SMALL_QUERY_SIZE;
340 }
341
342protected:
343 /// Returns true when querying the counter may potentially block.
344 bool WaitPending() const noexcept {
345 return counter && counter->WaitPending();
346 }
347
348private:
349 static constexpr std::size_t SMALL_QUERY_SIZE = 8; // Query size without timestamp.
350 static constexpr std::size_t LARGE_QUERY_SIZE = 16; // Query size with timestamp.
351 static constexpr std::intptr_t TIMESTAMP_OFFSET = 8; // Timestamp offset in a large query.
352
353 VAddr cpu_addr; ///< Guest CPU address.
354 u8* host_ptr; ///< Writable host pointer.
355 std::shared_ptr<HostCounter> counter; ///< Host counter to query, owns the dependency tree.
356 std::optional<u64> timestamp; ///< Timestamp to flush to guest memory.
357};
358
359} // namespace VideoCommon
diff --git a/src/video_core/rasterizer_interface.h b/src/video_core/rasterizer_interface.h
index c586cd6fe..f18eaf4bc 100644
--- a/src/video_core/rasterizer_interface.h
+++ b/src/video_core/rasterizer_interface.h
@@ -6,6 +6,7 @@
6 6
7#include <atomic> 7#include <atomic>
8#include <functional> 8#include <functional>
9#include <optional>
9#include "common/common_types.h" 10#include "common/common_types.h"
10#include "video_core/engines/fermi_2d.h" 11#include "video_core/engines/fermi_2d.h"
11#include "video_core/gpu.h" 12#include "video_core/gpu.h"
@@ -17,6 +18,11 @@ class MemoryManager;
17 18
18namespace VideoCore { 19namespace VideoCore {
19 20
21enum class QueryType {
22 SamplesPassed,
23};
24constexpr std::size_t NumQueryTypes = 1;
25
20enum class LoadCallbackStage { 26enum class LoadCallbackStage {
21 Prepare, 27 Prepare,
22 Decompile, 28 Decompile,
@@ -29,11 +35,8 @@ class RasterizerInterface {
29public: 35public:
30 virtual ~RasterizerInterface() {} 36 virtual ~RasterizerInterface() {}
31 37
32 /// Draw the current batch of vertex arrays 38 /// Dispatches a draw invocation
33 virtual bool DrawBatch(bool is_indexed) = 0; 39 virtual void Draw(bool is_indexed, bool is_instanced) = 0;
34
35 /// Draw the current batch of multiple instances of vertex arrays
36 virtual bool DrawMultiBatch(bool is_indexed) = 0;
37 40
38 /// Clear the current framebuffer 41 /// Clear the current framebuffer
39 virtual void Clear() = 0; 42 virtual void Clear() = 0;
@@ -41,6 +44,12 @@ public:
41 /// Dispatches a compute shader invocation 44 /// Dispatches a compute shader invocation
42 virtual void DispatchCompute(GPUVAddr code_addr) = 0; 45 virtual void DispatchCompute(GPUVAddr code_addr) = 0;
43 46
47 /// Resets the counter of a query
48 virtual void ResetCounter(QueryType type) = 0;
49
50 /// Records a GPU query and caches it
51 virtual void Query(GPUVAddr gpu_addr, QueryType type, std::optional<u64> timestamp) = 0;
52
44 /// Notify rasterizer that all caches should be flushed to Switch memory 53 /// Notify rasterizer that all caches should be flushed to Switch memory
45 virtual void FlushAll() = 0; 54 virtual void FlushAll() = 0;
46 55
diff --git a/src/video_core/renderer_opengl/gl_query_cache.cpp b/src/video_core/renderer_opengl/gl_query_cache.cpp
new file mode 100644
index 000000000..f12e9f55f
--- /dev/null
+++ b/src/video_core/renderer_opengl/gl_query_cache.cpp
@@ -0,0 +1,120 @@
1// Copyright 2019 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <cstring>
7#include <memory>
8#include <unordered_map>
9#include <utility>
10#include <vector>
11
12#include <glad/glad.h>
13
14#include "common/assert.h"
15#include "core/core.h"
16#include "video_core/engines/maxwell_3d.h"
17#include "video_core/memory_manager.h"
18#include "video_core/renderer_opengl/gl_query_cache.h"
19#include "video_core/renderer_opengl/gl_rasterizer.h"
20
21namespace OpenGL {
22
23namespace {
24
25constexpr std::array<GLenum, VideoCore::NumQueryTypes> QueryTargets = {GL_SAMPLES_PASSED};
26
27constexpr GLenum GetTarget(VideoCore::QueryType type) {
28 return QueryTargets[static_cast<std::size_t>(type)];
29}
30
31} // Anonymous namespace
32
33QueryCache::QueryCache(Core::System& system, RasterizerOpenGL& gl_rasterizer)
34 : VideoCommon::QueryCacheBase<
35 QueryCache, CachedQuery, CounterStream, HostCounter,
36 std::vector<OGLQuery>>{system,
37 static_cast<VideoCore::RasterizerInterface&>(gl_rasterizer)},
38 gl_rasterizer{gl_rasterizer} {}
39
40QueryCache::~QueryCache() = default;
41
42OGLQuery QueryCache::AllocateQuery(VideoCore::QueryType type) {
43 auto& reserve = query_pools[static_cast<std::size_t>(type)];
44 OGLQuery query;
45 if (reserve.empty()) {
46 query.Create(GetTarget(type));
47 return query;
48 }
49
50 query = std::move(reserve.back());
51 reserve.pop_back();
52 return query;
53}
54
55void QueryCache::Reserve(VideoCore::QueryType type, OGLQuery&& query) {
56 query_pools[static_cast<std::size_t>(type)].push_back(std::move(query));
57}
58
59bool QueryCache::AnyCommandQueued() const noexcept {
60 return gl_rasterizer.AnyCommandQueued();
61}
62
63HostCounter::HostCounter(QueryCache& cache, std::shared_ptr<HostCounter> dependency,
64 VideoCore::QueryType type)
65 : VideoCommon::HostCounterBase<QueryCache, HostCounter>{std::move(dependency)}, cache{cache},
66 type{type}, query{cache.AllocateQuery(type)} {
67 glBeginQuery(GetTarget(type), query.handle);
68}
69
70HostCounter::~HostCounter() {
71 cache.Reserve(type, std::move(query));
72}
73
74void HostCounter::EndQuery() {
75 if (!cache.AnyCommandQueued()) {
76 // There are chances a query waited on without commands (glDraw, glClear, glDispatch). Not
77 // having any of these causes a lock. glFlush is considered a command, so we can safely wait
78 // for this. Insert to the OpenGL command stream a flush.
79 glFlush();
80 }
81 glEndQuery(GetTarget(type));
82}
83
84u64 HostCounter::BlockingQuery() const {
85 GLint64 value;
86 glGetQueryObjecti64v(query.handle, GL_QUERY_RESULT, &value);
87 return static_cast<u64>(value);
88}
89
90CachedQuery::CachedQuery(QueryCache& cache, VideoCore::QueryType type, VAddr cpu_addr, u8* host_ptr)
91 : VideoCommon::CachedQueryBase<HostCounter>{cpu_addr, host_ptr}, cache{&cache}, type{type} {}
92
93CachedQuery::CachedQuery(CachedQuery&& rhs) noexcept
94 : VideoCommon::CachedQueryBase<HostCounter>(std::move(rhs)), cache{rhs.cache}, type{rhs.type} {}
95
96CachedQuery& CachedQuery::operator=(CachedQuery&& rhs) noexcept {
97 VideoCommon::CachedQueryBase<HostCounter>::operator=(std::move(rhs));
98 cache = rhs.cache;
99 type = rhs.type;
100 return *this;
101}
102
103void CachedQuery::Flush() {
104 // Waiting for a query while another query of the same target is enabled locks Nvidia's driver.
105 // To avoid this disable and re-enable keeping the dependency stream.
106 // But we only have to do this if we have pending waits to be done.
107 auto& stream = cache->Stream(type);
108 const bool slice_counter = WaitPending() && stream.IsEnabled();
109 if (slice_counter) {
110 stream.Update(false);
111 }
112
113 VideoCommon::CachedQueryBase<HostCounter>::Flush();
114
115 if (slice_counter) {
116 stream.Update(true);
117 }
118}
119
120} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_query_cache.h b/src/video_core/renderer_opengl/gl_query_cache.h
new file mode 100644
index 000000000..d8e7052a1
--- /dev/null
+++ b/src/video_core/renderer_opengl/gl_query_cache.h
@@ -0,0 +1,78 @@
1// Copyright 2019 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <memory>
9#include <vector>
10
11#include "common/common_types.h"
12#include "video_core/query_cache.h"
13#include "video_core/rasterizer_interface.h"
14#include "video_core/renderer_opengl/gl_resource_manager.h"
15
16namespace Core {
17class System;
18}
19
20namespace OpenGL {
21
22class CachedQuery;
23class HostCounter;
24class QueryCache;
25class RasterizerOpenGL;
26
27using CounterStream = VideoCommon::CounterStreamBase<QueryCache, HostCounter>;
28
29class QueryCache final : public VideoCommon::QueryCacheBase<QueryCache, CachedQuery, CounterStream,
30 HostCounter, std::vector<OGLQuery>> {
31public:
32 explicit QueryCache(Core::System& system, RasterizerOpenGL& rasterizer);
33 ~QueryCache();
34
35 OGLQuery AllocateQuery(VideoCore::QueryType type);
36
37 void Reserve(VideoCore::QueryType type, OGLQuery&& query);
38
39 bool AnyCommandQueued() const noexcept;
40
41private:
42 RasterizerOpenGL& gl_rasterizer;
43};
44
45class HostCounter final : public VideoCommon::HostCounterBase<QueryCache, HostCounter> {
46public:
47 explicit HostCounter(QueryCache& cache, std::shared_ptr<HostCounter> dependency,
48 VideoCore::QueryType type);
49 ~HostCounter();
50
51 void EndQuery();
52
53private:
54 u64 BlockingQuery() const override;
55
56 QueryCache& cache;
57 const VideoCore::QueryType type;
58 OGLQuery query;
59};
60
61class CachedQuery final : public VideoCommon::CachedQueryBase<HostCounter> {
62public:
63 explicit CachedQuery(QueryCache& cache, VideoCore::QueryType type, VAddr cpu_addr,
64 u8* host_ptr);
65 CachedQuery(CachedQuery&& rhs) noexcept;
66 CachedQuery(const CachedQuery&) = delete;
67
68 CachedQuery& operator=(CachedQuery&& rhs) noexcept;
69 CachedQuery& operator=(const CachedQuery&) = delete;
70
71 void Flush() override;
72
73private:
74 QueryCache* cache;
75 VideoCore::QueryType type;
76};
77
78} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index b0eb14c8b..e1965fb21 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -25,6 +25,7 @@
25#include "video_core/engines/maxwell_3d.h" 25#include "video_core/engines/maxwell_3d.h"
26#include "video_core/engines/shader_type.h" 26#include "video_core/engines/shader_type.h"
27#include "video_core/memory_manager.h" 27#include "video_core/memory_manager.h"
28#include "video_core/renderer_opengl/gl_query_cache.h"
28#include "video_core/renderer_opengl/gl_rasterizer.h" 29#include "video_core/renderer_opengl/gl_rasterizer.h"
29#include "video_core/renderer_opengl/gl_shader_cache.h" 30#include "video_core/renderer_opengl/gl_shader_cache.h"
30#include "video_core/renderer_opengl/gl_shader_gen.h" 31#include "video_core/renderer_opengl/gl_shader_gen.h"
@@ -92,8 +93,8 @@ std::size_t GetConstBufferSize(const Tegra::Engines::ConstBufferInfo& buffer,
92RasterizerOpenGL::RasterizerOpenGL(Core::System& system, Core::Frontend::EmuWindow& emu_window, 93RasterizerOpenGL::RasterizerOpenGL(Core::System& system, Core::Frontend::EmuWindow& emu_window,
93 ScreenInfo& info) 94 ScreenInfo& info)
94 : RasterizerAccelerated{system.Memory()}, texture_cache{system, *this, device}, 95 : RasterizerAccelerated{system.Memory()}, texture_cache{system, *this, device},
95 shader_cache{*this, system, emu_window, device}, system{system}, screen_info{info}, 96 shader_cache{*this, system, emu_window, device}, query_cache{system, *this}, system{system},
96 buffer_cache{*this, system, device, STREAM_BUFFER_SIZE} { 97 screen_info{info}, buffer_cache{*this, system, device, STREAM_BUFFER_SIZE} {
97 shader_program_manager = std::make_unique<GLShader::ProgramManager>(); 98 shader_program_manager = std::make_unique<GLShader::ProgramManager>();
98 state.draw.shader_program = 0; 99 state.draw.shader_program = 0;
99 state.Apply(); 100 state.Apply();
@@ -541,11 +542,16 @@ void RasterizerOpenGL::Clear() {
541 } else if (use_stencil) { 542 } else if (use_stencil) {
542 glClearBufferiv(GL_STENCIL, 0, &regs.clear_stencil); 543 glClearBufferiv(GL_STENCIL, 0, &regs.clear_stencil);
543 } 544 }
545
546 ++num_queued_commands;
544} 547}
545 548
546void RasterizerOpenGL::Draw(bool is_indexed, bool is_instanced) { 549void RasterizerOpenGL::Draw(bool is_indexed, bool is_instanced) {
547 MICROPROFILE_SCOPE(OpenGL_Drawing); 550 MICROPROFILE_SCOPE(OpenGL_Drawing);
548 auto& gpu = system.GPU().Maxwell3D(); 551 auto& gpu = system.GPU().Maxwell3D();
552 const auto& regs = gpu.regs;
553
554 query_cache.UpdateCounters();
549 555
550 SyncRasterizeEnable(state); 556 SyncRasterizeEnable(state);
551 SyncColorMask(); 557 SyncColorMask();
@@ -611,7 +617,7 @@ void RasterizerOpenGL::Draw(bool is_indexed, bool is_instanced) {
611 617
612 // Setup shaders and their used resources. 618 // Setup shaders and their used resources.
613 texture_cache.GuardSamplers(true); 619 texture_cache.GuardSamplers(true);
614 const auto primitive_mode = MaxwellToGL::PrimitiveTopology(gpu.regs.draw.topology); 620 const GLenum primitive_mode = MaxwellToGL::PrimitiveTopology(gpu.regs.draw.topology);
615 SetupShaders(primitive_mode); 621 SetupShaders(primitive_mode);
616 texture_cache.GuardSamplers(false); 622 texture_cache.GuardSamplers(false);
617 623
@@ -638,35 +644,47 @@ void RasterizerOpenGL::Draw(bool is_indexed, bool is_instanced) {
638 glTextureBarrier(); 644 glTextureBarrier();
639 } 645 }
640 646
647 ++num_queued_commands;
648
641 const GLuint base_instance = static_cast<GLuint>(gpu.regs.vb_base_instance); 649 const GLuint base_instance = static_cast<GLuint>(gpu.regs.vb_base_instance);
642 const GLsizei num_instances = 650 const GLsizei num_instances =
643 static_cast<GLsizei>(is_instanced ? gpu.mme_draw.instance_count : 1); 651 static_cast<GLsizei>(is_instanced ? gpu.mme_draw.instance_count : 1);
644 if (is_indexed) { 652 if (is_indexed) {
645 const GLenum index_format = MaxwellToGL::IndexFormat(gpu.regs.index_array.format);
646 const GLint base_vertex = static_cast<GLint>(gpu.regs.vb_element_base); 653 const GLint base_vertex = static_cast<GLint>(gpu.regs.vb_element_base);
647 const GLsizei num_vertices = static_cast<GLsizei>(gpu.regs.index_array.count); 654 const GLsizei num_vertices = static_cast<GLsizei>(gpu.regs.index_array.count);
648 glDrawElementsInstancedBaseVertexBaseInstance( 655 const GLvoid* offset = reinterpret_cast<const GLvoid*>(index_buffer_offset);
649 primitive_mode, num_vertices, index_format, 656 const GLenum format = MaxwellToGL::IndexFormat(gpu.regs.index_array.format);
650 reinterpret_cast<const void*>(index_buffer_offset), num_instances, base_vertex, 657 if (num_instances == 1 && base_instance == 0 && base_vertex == 0) {
651 base_instance); 658 glDrawElements(primitive_mode, num_vertices, format, offset);
659 } else if (num_instances == 1 && base_instance == 0) {
660 glDrawElementsBaseVertex(primitive_mode, num_vertices, format, offset, base_vertex);
661 } else if (base_vertex == 0 && base_instance == 0) {
662 glDrawElementsInstanced(primitive_mode, num_vertices, format, offset, num_instances);
663 } else if (base_vertex == 0) {
664 glDrawElementsInstancedBaseInstance(primitive_mode, num_vertices, format, offset,
665 num_instances, base_instance);
666 } else if (base_instance == 0) {
667 glDrawElementsInstancedBaseVertex(primitive_mode, num_vertices, format, offset,
668 num_instances, base_vertex);
669 } else {
670 glDrawElementsInstancedBaseVertexBaseInstance(primitive_mode, num_vertices, format,
671 offset, num_instances, base_vertex,
672 base_instance);
673 }
652 } else { 674 } else {
653 const GLint base_vertex = static_cast<GLint>(gpu.regs.vertex_buffer.first); 675 const GLint base_vertex = static_cast<GLint>(gpu.regs.vertex_buffer.first);
654 const GLsizei num_vertices = static_cast<GLsizei>(gpu.regs.vertex_buffer.count); 676 const GLsizei num_vertices = static_cast<GLsizei>(gpu.regs.vertex_buffer.count);
655 glDrawArraysInstancedBaseInstance(primitive_mode, base_vertex, num_vertices, num_instances, 677 if (num_instances == 1 && base_instance == 0) {
656 base_instance); 678 glDrawArrays(primitive_mode, base_vertex, num_vertices);
679 } else if (base_instance == 0) {
680 glDrawArraysInstanced(primitive_mode, base_vertex, num_vertices, num_instances);
681 } else {
682 glDrawArraysInstancedBaseInstance(primitive_mode, base_vertex, num_vertices,
683 num_instances, base_instance);
684 }
657 } 685 }
658} 686}
659 687
660bool RasterizerOpenGL::DrawBatch(bool is_indexed) {
661 Draw(is_indexed, false);
662 return true;
663}
664
665bool RasterizerOpenGL::DrawMultiBatch(bool is_indexed) {
666 Draw(is_indexed, true);
667 return true;
668}
669
670void RasterizerOpenGL::DispatchCompute(GPUVAddr code_addr) { 688void RasterizerOpenGL::DispatchCompute(GPUVAddr code_addr) {
671 if (device.HasBrokenCompute()) { 689 if (device.HasBrokenCompute()) {
672 return; 690 return;
@@ -707,6 +725,16 @@ void RasterizerOpenGL::DispatchCompute(GPUVAddr code_addr) {
707 state.ApplyProgramPipeline(); 725 state.ApplyProgramPipeline();
708 726
709 glDispatchCompute(launch_desc.grid_dim_x, launch_desc.grid_dim_y, launch_desc.grid_dim_z); 727 glDispatchCompute(launch_desc.grid_dim_x, launch_desc.grid_dim_y, launch_desc.grid_dim_z);
728 ++num_queued_commands;
729}
730
731void RasterizerOpenGL::ResetCounter(VideoCore::QueryType type) {
732 query_cache.ResetCounter(type);
733}
734
735void RasterizerOpenGL::Query(GPUVAddr gpu_addr, VideoCore::QueryType type,
736 std::optional<u64> timestamp) {
737 query_cache.Query(gpu_addr, type, timestamp);
710} 738}
711 739
712void RasterizerOpenGL::FlushAll() {} 740void RasterizerOpenGL::FlushAll() {}
@@ -718,6 +746,7 @@ void RasterizerOpenGL::FlushRegion(CacheAddr addr, u64 size) {
718 } 746 }
719 texture_cache.FlushRegion(addr, size); 747 texture_cache.FlushRegion(addr, size);
720 buffer_cache.FlushRegion(addr, size); 748 buffer_cache.FlushRegion(addr, size);
749 query_cache.FlushRegion(addr, size);
721} 750}
722 751
723void RasterizerOpenGL::InvalidateRegion(CacheAddr addr, u64 size) { 752void RasterizerOpenGL::InvalidateRegion(CacheAddr addr, u64 size) {
@@ -728,6 +757,7 @@ void RasterizerOpenGL::InvalidateRegion(CacheAddr addr, u64 size) {
728 texture_cache.InvalidateRegion(addr, size); 757 texture_cache.InvalidateRegion(addr, size);
729 shader_cache.InvalidateRegion(addr, size); 758 shader_cache.InvalidateRegion(addr, size);
730 buffer_cache.InvalidateRegion(addr, size); 759 buffer_cache.InvalidateRegion(addr, size);
760 query_cache.InvalidateRegion(addr, size);
731} 761}
732 762
733void RasterizerOpenGL::FlushAndInvalidateRegion(CacheAddr addr, u64 size) { 763void RasterizerOpenGL::FlushAndInvalidateRegion(CacheAddr addr, u64 size) {
@@ -738,10 +768,18 @@ void RasterizerOpenGL::FlushAndInvalidateRegion(CacheAddr addr, u64 size) {
738} 768}
739 769
740void RasterizerOpenGL::FlushCommands() { 770void RasterizerOpenGL::FlushCommands() {
771 // Only flush when we have commands queued to OpenGL.
772 if (num_queued_commands == 0) {
773 return;
774 }
775 num_queued_commands = 0;
741 glFlush(); 776 glFlush();
742} 777}
743 778
744void RasterizerOpenGL::TickFrame() { 779void RasterizerOpenGL::TickFrame() {
780 // Ticking a frame means that buffers will be swapped, calling glFlush implicitly.
781 num_queued_commands = 0;
782
745 buffer_cache.TickFrame(); 783 buffer_cache.TickFrame();
746} 784}
747 785
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h
index 0501f3828..68abe9a21 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer.h
@@ -24,6 +24,7 @@
24#include "video_core/renderer_opengl/gl_buffer_cache.h" 24#include "video_core/renderer_opengl/gl_buffer_cache.h"
25#include "video_core/renderer_opengl/gl_device.h" 25#include "video_core/renderer_opengl/gl_device.h"
26#include "video_core/renderer_opengl/gl_framebuffer_cache.h" 26#include "video_core/renderer_opengl/gl_framebuffer_cache.h"
27#include "video_core/renderer_opengl/gl_query_cache.h"
27#include "video_core/renderer_opengl/gl_resource_manager.h" 28#include "video_core/renderer_opengl/gl_resource_manager.h"
28#include "video_core/renderer_opengl/gl_sampler_cache.h" 29#include "video_core/renderer_opengl/gl_sampler_cache.h"
29#include "video_core/renderer_opengl/gl_shader_cache.h" 30#include "video_core/renderer_opengl/gl_shader_cache.h"
@@ -57,10 +58,11 @@ public:
57 ScreenInfo& info); 58 ScreenInfo& info);
58 ~RasterizerOpenGL() override; 59 ~RasterizerOpenGL() override;
59 60
60 bool DrawBatch(bool is_indexed) override; 61 void Draw(bool is_indexed, bool is_instanced) override;
61 bool DrawMultiBatch(bool is_indexed) override;
62 void Clear() override; 62 void Clear() override;
63 void DispatchCompute(GPUVAddr code_addr) override; 63 void DispatchCompute(GPUVAddr code_addr) override;
64 void ResetCounter(VideoCore::QueryType type) override;
65 void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
64 void FlushAll() override; 66 void FlushAll() override;
65 void FlushRegion(CacheAddr addr, u64 size) override; 67 void FlushRegion(CacheAddr addr, u64 size) override;
66 void InvalidateRegion(CacheAddr addr, u64 size) override; 68 void InvalidateRegion(CacheAddr addr, u64 size) override;
@@ -75,6 +77,11 @@ public:
75 void LoadDiskResources(const std::atomic_bool& stop_loading, 77 void LoadDiskResources(const std::atomic_bool& stop_loading,
76 const VideoCore::DiskResourceLoadCallback& callback) override; 78 const VideoCore::DiskResourceLoadCallback& callback) override;
77 79
80 /// Returns true when there are commands queued to the OpenGL server.
81 bool AnyCommandQueued() const {
82 return num_queued_commands > 0;
83 }
84
78private: 85private:
79 /// Configures the color and depth framebuffer states. 86 /// Configures the color and depth framebuffer states.
80 void ConfigureFramebuffers(); 87 void ConfigureFramebuffers();
@@ -102,9 +109,6 @@ private:
102 void SetupGlobalMemory(u32 binding, const GLShader::GlobalMemoryEntry& entry, GPUVAddr gpu_addr, 109 void SetupGlobalMemory(u32 binding, const GLShader::GlobalMemoryEntry& entry, GPUVAddr gpu_addr,
103 std::size_t size); 110 std::size_t size);
104 111
105 /// Syncs all the state, shaders, render targets and textures setting before a draw call.
106 void Draw(bool is_indexed, bool is_instanced);
107
108 /// Configures the current textures to use for the draw command. 112 /// Configures the current textures to use for the draw command.
109 void SetupDrawTextures(std::size_t stage_index, const Shader& shader); 113 void SetupDrawTextures(std::size_t stage_index, const Shader& shader);
110 114
@@ -180,10 +184,23 @@ private:
180 /// Syncs the alpha test state to match the guest state 184 /// Syncs the alpha test state to match the guest state
181 void SyncAlphaTest(); 185 void SyncAlphaTest();
182 186
183 /// Check for extension that are not strictly required 187 /// Check for extension that are not strictly required but are needed for correct emulation
184 /// but are needed for correct emulation
185 void CheckExtensions(); 188 void CheckExtensions();
186 189
190 std::size_t CalculateVertexArraysSize() const;
191
192 std::size_t CalculateIndexBufferSize() const;
193
194 /// Updates and returns a vertex array object representing current vertex format
195 GLuint SetupVertexFormat();
196
197 void SetupVertexBuffer(GLuint vao);
198 void SetupVertexInstances(GLuint vao);
199
200 GLintptr SetupIndexBuffer();
201
202 void SetupShaders(GLenum primitive_mode);
203
187 const Device device; 204 const Device device;
188 OpenGLState state; 205 OpenGLState state;
189 206
@@ -191,6 +208,7 @@ private:
191 ShaderCacheOpenGL shader_cache; 208 ShaderCacheOpenGL shader_cache;
192 SamplerCacheOpenGL sampler_cache; 209 SamplerCacheOpenGL sampler_cache;
193 FramebufferCacheOpenGL framebuffer_cache; 210 FramebufferCacheOpenGL framebuffer_cache;
211 QueryCache query_cache;
194 212
195 Core::System& system; 213 Core::System& system;
196 ScreenInfo& screen_info; 214 ScreenInfo& screen_info;
@@ -208,19 +226,8 @@ private:
208 BindBuffersRangePushBuffer bind_ubo_pushbuffer{GL_UNIFORM_BUFFER}; 226 BindBuffersRangePushBuffer bind_ubo_pushbuffer{GL_UNIFORM_BUFFER};
209 BindBuffersRangePushBuffer bind_ssbo_pushbuffer{GL_SHADER_STORAGE_BUFFER}; 227 BindBuffersRangePushBuffer bind_ssbo_pushbuffer{GL_SHADER_STORAGE_BUFFER};
210 228
211 std::size_t CalculateVertexArraysSize() const; 229 /// Number of commands queued to the OpenGL driver. Reseted on flush.
212 230 std::size_t num_queued_commands = 0;
213 std::size_t CalculateIndexBufferSize() const;
214
215 /// Updates and returns a vertex array object representing current vertex format
216 GLuint SetupVertexFormat();
217
218 void SetupVertexBuffer(GLuint vao);
219 void SetupVertexInstances(GLuint vao);
220
221 GLintptr SetupIndexBuffer();
222
223 void SetupShaders(GLenum primitive_mode);
224}; 231};
225 232
226} // namespace OpenGL 233} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_resource_manager.cpp b/src/video_core/renderer_opengl/gl_resource_manager.cpp
index 5c96c1d46..f0ddfb276 100644
--- a/src/video_core/renderer_opengl/gl_resource_manager.cpp
+++ b/src/video_core/renderer_opengl/gl_resource_manager.cpp
@@ -207,4 +207,21 @@ void OGLFramebuffer::Release() {
207 handle = 0; 207 handle = 0;
208} 208}
209 209
210void OGLQuery::Create(GLenum target) {
211 if (handle != 0)
212 return;
213
214 MICROPROFILE_SCOPE(OpenGL_ResourceCreation);
215 glCreateQueries(target, 1, &handle);
216}
217
218void OGLQuery::Release() {
219 if (handle == 0)
220 return;
221
222 MICROPROFILE_SCOPE(OpenGL_ResourceDeletion);
223 glDeleteQueries(1, &handle);
224 handle = 0;
225}
226
210} // namespace OpenGL 227} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_resource_manager.h b/src/video_core/renderer_opengl/gl_resource_manager.h
index 3a85a1d4c..514d1d165 100644
--- a/src/video_core/renderer_opengl/gl_resource_manager.h
+++ b/src/video_core/renderer_opengl/gl_resource_manager.h
@@ -266,4 +266,29 @@ public:
266 GLuint handle = 0; 266 GLuint handle = 0;
267}; 267};
268 268
269class OGLQuery : private NonCopyable {
270public:
271 OGLQuery() = default;
272
273 OGLQuery(OGLQuery&& o) noexcept : handle(std::exchange(o.handle, 0)) {}
274
275 ~OGLQuery() {
276 Release();
277 }
278
279 OGLQuery& operator=(OGLQuery&& o) noexcept {
280 Release();
281 handle = std::exchange(o.handle, 0);
282 return *this;
283 }
284
285 /// Creates a new internal OpenGL resource and stores the handle
286 void Create(GLenum target);
287
288 /// Deletes the internal OpenGL resource
289 void Release();
290
291 GLuint handle = 0;
292};
293
269} // namespace OpenGL 294} // namespace OpenGL
diff --git a/src/video_core/renderer_vulkan/vk_device.cpp b/src/video_core/renderer_vulkan/vk_device.cpp
index 0b3fdef1b..3a77aced9 100644
--- a/src/video_core/renderer_vulkan/vk_device.cpp
+++ b/src/video_core/renderer_vulkan/vk_device.cpp
@@ -104,6 +104,7 @@ bool VKDevice::Create(const vk::DispatchLoaderDynamic& dldi, vk::Instance instan
104 features.depthBiasClamp = true; 104 features.depthBiasClamp = true;
105 features.geometryShader = true; 105 features.geometryShader = true;
106 features.tessellationShader = true; 106 features.tessellationShader = true;
107 features.occlusionQueryPrecise = true;
107 features.fragmentStoresAndAtomics = true; 108 features.fragmentStoresAndAtomics = true;
108 features.shaderImageGatherExtended = true; 109 features.shaderImageGatherExtended = true;
109 features.shaderStorageImageWriteWithoutFormat = true; 110 features.shaderStorageImageWriteWithoutFormat = true;
@@ -117,6 +118,10 @@ bool VKDevice::Create(const vk::DispatchLoaderDynamic& dldi, vk::Instance instan
117 bit8_storage.uniformAndStorageBuffer8BitAccess = true; 118 bit8_storage.uniformAndStorageBuffer8BitAccess = true;
118 SetNext(next, bit8_storage); 119 SetNext(next, bit8_storage);
119 120
121 vk::PhysicalDeviceHostQueryResetFeaturesEXT host_query_reset;
122 host_query_reset.hostQueryReset = true;
123 SetNext(next, host_query_reset);
124
120 vk::PhysicalDeviceFloat16Int8FeaturesKHR float16_int8; 125 vk::PhysicalDeviceFloat16Int8FeaturesKHR float16_int8;
121 if (is_float16_supported) { 126 if (is_float16_supported) {
122 float16_int8.shaderFloat16 = true; 127 float16_int8.shaderFloat16 = true;
@@ -273,6 +278,7 @@ bool VKDevice::IsSuitable(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDev
273 VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME, 278 VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME,
274 VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME, 279 VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME,
275 VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME, 280 VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME,
281 VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME,
276 }; 282 };
277 std::bitset<required_extensions.size()> available_extensions{}; 283 std::bitset<required_extensions.size()> available_extensions{};
278 284
@@ -340,6 +346,7 @@ bool VKDevice::IsSuitable(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDev
340 std::make_pair(features.depthBiasClamp, "depthBiasClamp"), 346 std::make_pair(features.depthBiasClamp, "depthBiasClamp"),
341 std::make_pair(features.geometryShader, "geometryShader"), 347 std::make_pair(features.geometryShader, "geometryShader"),
342 std::make_pair(features.tessellationShader, "tessellationShader"), 348 std::make_pair(features.tessellationShader, "tessellationShader"),
349 std::make_pair(features.occlusionQueryPrecise, "occlusionQueryPrecise"),
343 std::make_pair(features.fragmentStoresAndAtomics, "fragmentStoresAndAtomics"), 350 std::make_pair(features.fragmentStoresAndAtomics, "fragmentStoresAndAtomics"),
344 std::make_pair(features.shaderImageGatherExtended, "shaderImageGatherExtended"), 351 std::make_pair(features.shaderImageGatherExtended, "shaderImageGatherExtended"),
345 std::make_pair(features.shaderStorageImageWriteWithoutFormat, 352 std::make_pair(features.shaderStorageImageWriteWithoutFormat,
@@ -376,7 +383,7 @@ std::vector<const char*> VKDevice::LoadExtensions(const vk::DispatchLoaderDynami
376 } 383 }
377 }; 384 };
378 385
379 extensions.reserve(13); 386 extensions.reserve(14);
380 extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); 387 extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
381 extensions.push_back(VK_KHR_16BIT_STORAGE_EXTENSION_NAME); 388 extensions.push_back(VK_KHR_16BIT_STORAGE_EXTENSION_NAME);
382 extensions.push_back(VK_KHR_8BIT_STORAGE_EXTENSION_NAME); 389 extensions.push_back(VK_KHR_8BIT_STORAGE_EXTENSION_NAME);
@@ -384,6 +391,7 @@ std::vector<const char*> VKDevice::LoadExtensions(const vk::DispatchLoaderDynami
384 extensions.push_back(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME); 391 extensions.push_back(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME);
385 extensions.push_back(VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME); 392 extensions.push_back(VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME);
386 extensions.push_back(VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME); 393 extensions.push_back(VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME);
394 extensions.push_back(VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME);
387 395
388 [[maybe_unused]] const bool nsight = 396 [[maybe_unused]] const bool nsight =
389 std::getenv("NVTX_INJECTION64_PATH") || std::getenv("NSIGHT_LAUNCHED"); 397 std::getenv("NVTX_INJECTION64_PATH") || std::getenv("NSIGHT_LAUNCHED");
diff --git a/src/video_core/renderer_vulkan/vk_query_cache.cpp b/src/video_core/renderer_vulkan/vk_query_cache.cpp
new file mode 100644
index 000000000..ffbf60dda
--- /dev/null
+++ b/src/video_core/renderer_vulkan/vk_query_cache.cpp
@@ -0,0 +1,122 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <cstddef>
7#include <cstdint>
8#include <utility>
9#include <vector>
10
11#include "video_core/renderer_vulkan/declarations.h"
12#include "video_core/renderer_vulkan/vk_device.h"
13#include "video_core/renderer_vulkan/vk_query_cache.h"
14#include "video_core/renderer_vulkan/vk_resource_manager.h"
15#include "video_core/renderer_vulkan/vk_scheduler.h"
16
17namespace Vulkan {
18
19namespace {
20
21constexpr std::array QUERY_TARGETS = {vk::QueryType::eOcclusion};
22
23constexpr vk::QueryType GetTarget(VideoCore::QueryType type) {
24 return QUERY_TARGETS[static_cast<std::size_t>(type)];
25}
26
27} // Anonymous namespace
28
29QueryPool::QueryPool() : VKFencedPool{GROW_STEP} {}
30
31QueryPool::~QueryPool() = default;
32
33void QueryPool::Initialize(const VKDevice& device_, VideoCore::QueryType type_) {
34 device = &device_;
35 type = type_;
36}
37
38std::pair<vk::QueryPool, std::uint32_t> QueryPool::Commit(VKFence& fence) {
39 std::size_t index;
40 do {
41 index = CommitResource(fence);
42 } while (usage[index]);
43 usage[index] = true;
44
45 return {*pools[index / GROW_STEP], static_cast<std::uint32_t>(index % GROW_STEP)};
46}
47
48void QueryPool::Allocate(std::size_t begin, std::size_t end) {
49 usage.resize(end);
50
51 const auto dev = device->GetLogical();
52 const u32 size = static_cast<u32>(end - begin);
53 const vk::QueryPoolCreateInfo query_pool_ci({}, GetTarget(type), size, {});
54 pools.push_back(dev.createQueryPoolUnique(query_pool_ci, nullptr, device->GetDispatchLoader()));
55}
56
57void QueryPool::Reserve(std::pair<vk::QueryPool, std::uint32_t> query) {
58 const auto it =
59 std::find_if(std::begin(pools), std::end(pools),
60 [query_pool = query.first](auto& pool) { return query_pool == *pool; });
61 ASSERT(it != std::end(pools));
62
63 const std::ptrdiff_t pool_index = std::distance(std::begin(pools), it);
64 usage[pool_index * GROW_STEP + static_cast<std::ptrdiff_t>(query.second)] = false;
65}
66
67VKQueryCache::VKQueryCache(Core::System& system, VideoCore::RasterizerInterface& rasterizer,
68 const VKDevice& device, VKScheduler& scheduler)
69 : VideoCommon::QueryCacheBase<VKQueryCache, CachedQuery, CounterStream, HostCounter,
70 QueryPool>{system, rasterizer},
71 device{device}, scheduler{scheduler} {
72 for (std::size_t i = 0; i < static_cast<std::size_t>(VideoCore::NumQueryTypes); ++i) {
73 query_pools[i].Initialize(device, static_cast<VideoCore::QueryType>(i));
74 }
75}
76
77VKQueryCache::~VKQueryCache() = default;
78
79std::pair<vk::QueryPool, std::uint32_t> VKQueryCache::AllocateQuery(VideoCore::QueryType type) {
80 return query_pools[static_cast<std::size_t>(type)].Commit(scheduler.GetFence());
81}
82
83void VKQueryCache::Reserve(VideoCore::QueryType type,
84 std::pair<vk::QueryPool, std::uint32_t> query) {
85 query_pools[static_cast<std::size_t>(type)].Reserve(query);
86}
87
88HostCounter::HostCounter(VKQueryCache& cache, std::shared_ptr<HostCounter> dependency,
89 VideoCore::QueryType type)
90 : VideoCommon::HostCounterBase<VKQueryCache, HostCounter>{std::move(dependency)}, cache{cache},
91 type{type}, query{cache.AllocateQuery(type)}, ticks{cache.Scheduler().Ticks()} {
92 const auto dev = cache.Device().GetLogical();
93 cache.Scheduler().Record([dev, query = query](vk::CommandBuffer cmdbuf, auto& dld) {
94 dev.resetQueryPoolEXT(query.first, query.second, 1, dld);
95 cmdbuf.beginQuery(query.first, query.second, vk::QueryControlFlagBits::ePrecise, dld);
96 });
97}
98
99HostCounter::~HostCounter() {
100 cache.Reserve(type, query);
101}
102
103void HostCounter::EndQuery() {
104 cache.Scheduler().Record([query = query](auto cmdbuf, auto& dld) {
105 cmdbuf.endQuery(query.first, query.second, dld);
106 });
107}
108
109u64 HostCounter::BlockingQuery() const {
110 if (ticks >= cache.Scheduler().Ticks()) {
111 cache.Scheduler().Flush();
112 }
113
114 const auto dev = cache.Device().GetLogical();
115 const auto& dld = cache.Device().GetDispatchLoader();
116 u64 value;
117 dev.getQueryPoolResults(query.first, query.second, 1, sizeof(value), &value, sizeof(value),
118 vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait, dld);
119 return value;
120}
121
122} // namespace Vulkan
diff --git a/src/video_core/renderer_vulkan/vk_query_cache.h b/src/video_core/renderer_vulkan/vk_query_cache.h
new file mode 100644
index 000000000..c3092ee96
--- /dev/null
+++ b/src/video_core/renderer_vulkan/vk_query_cache.h
@@ -0,0 +1,104 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <cstddef>
8#include <cstdint>
9#include <memory>
10#include <utility>
11#include <vector>
12
13#include "common/common_types.h"
14#include "video_core/query_cache.h"
15#include "video_core/renderer_vulkan/declarations.h"
16#include "video_core/renderer_vulkan/vk_resource_manager.h"
17
18namespace VideoCore {
19class RasterizerInterface;
20}
21
22namespace Vulkan {
23
24class CachedQuery;
25class HostCounter;
26class VKDevice;
27class VKQueryCache;
28class VKScheduler;
29
30using CounterStream = VideoCommon::CounterStreamBase<VKQueryCache, HostCounter>;
31
32class QueryPool final : public VKFencedPool {
33public:
34 explicit QueryPool();
35 ~QueryPool() override;
36
37 void Initialize(const VKDevice& device, VideoCore::QueryType type);
38
39 std::pair<vk::QueryPool, std::uint32_t> Commit(VKFence& fence);
40
41 void Reserve(std::pair<vk::QueryPool, std::uint32_t> query);
42
43protected:
44 void Allocate(std::size_t begin, std::size_t end) override;
45
46private:
47 static constexpr std::size_t GROW_STEP = 512;
48
49 const VKDevice* device = nullptr;
50 VideoCore::QueryType type = {};
51
52 std::vector<UniqueQueryPool> pools;
53 std::vector<bool> usage;
54};
55
56class VKQueryCache final
57 : public VideoCommon::QueryCacheBase<VKQueryCache, CachedQuery, CounterStream, HostCounter,
58 QueryPool> {
59public:
60 explicit VKQueryCache(Core::System& system, VideoCore::RasterizerInterface& rasterizer,
61 const VKDevice& device, VKScheduler& scheduler);
62 ~VKQueryCache();
63
64 std::pair<vk::QueryPool, std::uint32_t> AllocateQuery(VideoCore::QueryType type);
65
66 void Reserve(VideoCore::QueryType type, std::pair<vk::QueryPool, std::uint32_t> query);
67
68 const VKDevice& Device() const noexcept {
69 return device;
70 }
71
72 VKScheduler& Scheduler() const noexcept {
73 return scheduler;
74 }
75
76private:
77 const VKDevice& device;
78 VKScheduler& scheduler;
79};
80
81class HostCounter final : public VideoCommon::HostCounterBase<VKQueryCache, HostCounter> {
82public:
83 explicit HostCounter(VKQueryCache& cache, std::shared_ptr<HostCounter> dependency,
84 VideoCore::QueryType type);
85 ~HostCounter();
86
87 void EndQuery();
88
89private:
90 u64 BlockingQuery() const override;
91
92 VKQueryCache& cache;
93 const VideoCore::QueryType type;
94 const std::pair<vk::QueryPool, std::uint32_t> query;
95 const u64 ticks;
96};
97
98class CachedQuery : public VideoCommon::CachedQueryBase<HostCounter> {
99public:
100 explicit CachedQuery(VKQueryCache&, VideoCore::QueryType, VAddr cpu_addr, u8* host_ptr)
101 : VideoCommon::CachedQueryBase<HostCounter>{cpu_addr, host_ptr} {}
102};
103
104} // namespace Vulkan
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.cpp b/src/video_core/renderer_vulkan/vk_rasterizer.cpp
index aada38702..31c078f6a 100644
--- a/src/video_core/renderer_vulkan/vk_rasterizer.cpp
+++ b/src/video_core/renderer_vulkan/vk_rasterizer.cpp
@@ -289,25 +289,19 @@ RasterizerVulkan::RasterizerVulkan(Core::System& system, Core::Frontend::EmuWind
289 staging_pool), 289 staging_pool),
290 pipeline_cache(system, *this, device, scheduler, descriptor_pool, update_descriptor_queue), 290 pipeline_cache(system, *this, device, scheduler, descriptor_pool, update_descriptor_queue),
291 buffer_cache(*this, system, device, memory_manager, scheduler, staging_pool), 291 buffer_cache(*this, system, device, memory_manager, scheduler, staging_pool),
292 sampler_cache(device) {} 292 sampler_cache(device), query_cache(system, *this, device, scheduler) {
293 293 scheduler.SetQueryCache(query_cache);
294RasterizerVulkan::~RasterizerVulkan() = default;
295
296bool RasterizerVulkan::DrawBatch(bool is_indexed) {
297 Draw(is_indexed, false);
298 return true;
299} 294}
300 295
301bool RasterizerVulkan::DrawMultiBatch(bool is_indexed) { 296RasterizerVulkan::~RasterizerVulkan() = default;
302 Draw(is_indexed, true);
303 return true;
304}
305 297
306void RasterizerVulkan::Draw(bool is_indexed, bool is_instanced) { 298void RasterizerVulkan::Draw(bool is_indexed, bool is_instanced) {
307 MICROPROFILE_SCOPE(Vulkan_Drawing); 299 MICROPROFILE_SCOPE(Vulkan_Drawing);
308 300
309 FlushWork(); 301 FlushWork();
310 302
303 query_cache.UpdateCounters();
304
311 const auto& gpu = system.GPU().Maxwell3D(); 305 const auto& gpu = system.GPU().Maxwell3D();
312 GraphicsPipelineCacheKey key{GetFixedPipelineState(gpu.regs)}; 306 GraphicsPipelineCacheKey key{GetFixedPipelineState(gpu.regs)};
313 307
@@ -362,6 +356,8 @@ void RasterizerVulkan::Draw(bool is_indexed, bool is_instanced) {
362void RasterizerVulkan::Clear() { 356void RasterizerVulkan::Clear() {
363 MICROPROFILE_SCOPE(Vulkan_Clearing); 357 MICROPROFILE_SCOPE(Vulkan_Clearing);
364 358
359 query_cache.UpdateCounters();
360
365 const auto& gpu = system.GPU().Maxwell3D(); 361 const auto& gpu = system.GPU().Maxwell3D();
366 if (!system.GPU().Maxwell3D().ShouldExecute()) { 362 if (!system.GPU().Maxwell3D().ShouldExecute()) {
367 return; 363 return;
@@ -429,6 +425,8 @@ void RasterizerVulkan::DispatchCompute(GPUVAddr code_addr) {
429 sampled_views.clear(); 425 sampled_views.clear();
430 image_views.clear(); 426 image_views.clear();
431 427
428 query_cache.UpdateCounters();
429
432 const auto& launch_desc = system.GPU().KeplerCompute().launch_description; 430 const auto& launch_desc = system.GPU().KeplerCompute().launch_description;
433 const ComputePipelineCacheKey key{ 431 const ComputePipelineCacheKey key{
434 code_addr, 432 code_addr,
@@ -471,17 +469,28 @@ void RasterizerVulkan::DispatchCompute(GPUVAddr code_addr) {
471 }); 469 });
472} 470}
473 471
472void RasterizerVulkan::ResetCounter(VideoCore::QueryType type) {
473 query_cache.ResetCounter(type);
474}
475
476void RasterizerVulkan::Query(GPUVAddr gpu_addr, VideoCore::QueryType type,
477 std::optional<u64> timestamp) {
478 query_cache.Query(gpu_addr, type, timestamp);
479}
480
474void RasterizerVulkan::FlushAll() {} 481void RasterizerVulkan::FlushAll() {}
475 482
476void RasterizerVulkan::FlushRegion(CacheAddr addr, u64 size) { 483void RasterizerVulkan::FlushRegion(CacheAddr addr, u64 size) {
477 texture_cache.FlushRegion(addr, size); 484 texture_cache.FlushRegion(addr, size);
478 buffer_cache.FlushRegion(addr, size); 485 buffer_cache.FlushRegion(addr, size);
486 query_cache.FlushRegion(addr, size);
479} 487}
480 488
481void RasterizerVulkan::InvalidateRegion(CacheAddr addr, u64 size) { 489void RasterizerVulkan::InvalidateRegion(CacheAddr addr, u64 size) {
482 texture_cache.InvalidateRegion(addr, size); 490 texture_cache.InvalidateRegion(addr, size);
483 pipeline_cache.InvalidateRegion(addr, size); 491 pipeline_cache.InvalidateRegion(addr, size);
484 buffer_cache.InvalidateRegion(addr, size); 492 buffer_cache.InvalidateRegion(addr, size);
493 query_cache.InvalidateRegion(addr, size);
485} 494}
486 495
487void RasterizerVulkan::FlushAndInvalidateRegion(CacheAddr addr, u64 size) { 496void RasterizerVulkan::FlushAndInvalidateRegion(CacheAddr addr, u64 size) {
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.h b/src/video_core/renderer_vulkan/vk_rasterizer.h
index 7be71e734..138903d60 100644
--- a/src/video_core/renderer_vulkan/vk_rasterizer.h
+++ b/src/video_core/renderer_vulkan/vk_rasterizer.h
@@ -24,6 +24,7 @@
24#include "video_core/renderer_vulkan/vk_descriptor_pool.h" 24#include "video_core/renderer_vulkan/vk_descriptor_pool.h"
25#include "video_core/renderer_vulkan/vk_memory_manager.h" 25#include "video_core/renderer_vulkan/vk_memory_manager.h"
26#include "video_core/renderer_vulkan/vk_pipeline_cache.h" 26#include "video_core/renderer_vulkan/vk_pipeline_cache.h"
27#include "video_core/renderer_vulkan/vk_query_cache.h"
27#include "video_core/renderer_vulkan/vk_renderpass_cache.h" 28#include "video_core/renderer_vulkan/vk_renderpass_cache.h"
28#include "video_core/renderer_vulkan/vk_resource_manager.h" 29#include "video_core/renderer_vulkan/vk_resource_manager.h"
29#include "video_core/renderer_vulkan/vk_sampler_cache.h" 30#include "video_core/renderer_vulkan/vk_sampler_cache.h"
@@ -96,7 +97,7 @@ struct ImageView {
96 vk::ImageLayout* layout = nullptr; 97 vk::ImageLayout* layout = nullptr;
97}; 98};
98 99
99class RasterizerVulkan : public VideoCore::RasterizerAccelerated { 100class RasterizerVulkan final : public VideoCore::RasterizerAccelerated {
100public: 101public:
101 explicit RasterizerVulkan(Core::System& system, Core::Frontend::EmuWindow& render_window, 102 explicit RasterizerVulkan(Core::System& system, Core::Frontend::EmuWindow& render_window,
102 VKScreenInfo& screen_info, const VKDevice& device, 103 VKScreenInfo& screen_info, const VKDevice& device,
@@ -104,10 +105,11 @@ public:
104 VKScheduler& scheduler); 105 VKScheduler& scheduler);
105 ~RasterizerVulkan() override; 106 ~RasterizerVulkan() override;
106 107
107 bool DrawBatch(bool is_indexed) override; 108 void Draw(bool is_indexed, bool is_instanced) override;
108 bool DrawMultiBatch(bool is_indexed) override;
109 void Clear() override; 109 void Clear() override;
110 void DispatchCompute(GPUVAddr code_addr) override; 110 void DispatchCompute(GPUVAddr code_addr) override;
111 void ResetCounter(VideoCore::QueryType type) override;
112 void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
111 void FlushAll() override; 113 void FlushAll() override;
112 void FlushRegion(CacheAddr addr, u64 size) override; 114 void FlushRegion(CacheAddr addr, u64 size) override;
113 void InvalidateRegion(CacheAddr addr, u64 size) override; 115 void InvalidateRegion(CacheAddr addr, u64 size) override;
@@ -140,8 +142,6 @@ private:
140 142
141 static constexpr std::size_t ZETA_TEXCEPTION_INDEX = 8; 143 static constexpr std::size_t ZETA_TEXCEPTION_INDEX = 8;
142 144
143 void Draw(bool is_indexed, bool is_instanced);
144
145 void FlushWork(); 145 void FlushWork();
146 146
147 Texceptions UpdateAttachments(); 147 Texceptions UpdateAttachments();
@@ -247,6 +247,7 @@ private:
247 VKPipelineCache pipeline_cache; 247 VKPipelineCache pipeline_cache;
248 VKBufferCache buffer_cache; 248 VKBufferCache buffer_cache;
249 VKSamplerCache sampler_cache; 249 VKSamplerCache sampler_cache;
250 VKQueryCache query_cache;
250 251
251 std::array<View, Maxwell::NumRenderTargets> color_attachments; 252 std::array<View, Maxwell::NumRenderTargets> color_attachments;
252 View zeta_attachment; 253 View zeta_attachment;
diff --git a/src/video_core/renderer_vulkan/vk_scheduler.cpp b/src/video_core/renderer_vulkan/vk_scheduler.cpp
index d66133ad1..92bd6c344 100644
--- a/src/video_core/renderer_vulkan/vk_scheduler.cpp
+++ b/src/video_core/renderer_vulkan/vk_scheduler.cpp
@@ -6,6 +6,7 @@
6#include "common/microprofile.h" 6#include "common/microprofile.h"
7#include "video_core/renderer_vulkan/declarations.h" 7#include "video_core/renderer_vulkan/declarations.h"
8#include "video_core/renderer_vulkan/vk_device.h" 8#include "video_core/renderer_vulkan/vk_device.h"
9#include "video_core/renderer_vulkan/vk_query_cache.h"
9#include "video_core/renderer_vulkan/vk_resource_manager.h" 10#include "video_core/renderer_vulkan/vk_resource_manager.h"
10#include "video_core/renderer_vulkan/vk_scheduler.h" 11#include "video_core/renderer_vulkan/vk_scheduler.h"
11 12
@@ -139,6 +140,8 @@ void VKScheduler::SubmitExecution(vk::Semaphore semaphore) {
139} 140}
140 141
141void VKScheduler::AllocateNewContext() { 142void VKScheduler::AllocateNewContext() {
143 ++ticks;
144
142 std::unique_lock lock{mutex}; 145 std::unique_lock lock{mutex};
143 current_fence = next_fence; 146 current_fence = next_fence;
144 next_fence = &resource_manager.CommitFence(); 147 next_fence = &resource_manager.CommitFence();
@@ -146,6 +149,10 @@ void VKScheduler::AllocateNewContext() {
146 current_cmdbuf = resource_manager.CommitCommandBuffer(*current_fence); 149 current_cmdbuf = resource_manager.CommitCommandBuffer(*current_fence);
147 current_cmdbuf.begin({vk::CommandBufferUsageFlagBits::eOneTimeSubmit}, 150 current_cmdbuf.begin({vk::CommandBufferUsageFlagBits::eOneTimeSubmit},
148 device.GetDispatchLoader()); 151 device.GetDispatchLoader());
152 // Enable counters once again. These are disabled when a command buffer is finished.
153 if (query_cache) {
154 query_cache->UpdateCounters();
155 }
149} 156}
150 157
151void VKScheduler::InvalidateState() { 158void VKScheduler::InvalidateState() {
@@ -159,6 +166,7 @@ void VKScheduler::InvalidateState() {
159} 166}
160 167
161void VKScheduler::EndPendingOperations() { 168void VKScheduler::EndPendingOperations() {
169 query_cache->DisableStreams();
162 EndRenderPass(); 170 EndRenderPass();
163} 171}
164 172
diff --git a/src/video_core/renderer_vulkan/vk_scheduler.h b/src/video_core/renderer_vulkan/vk_scheduler.h
index bcdffbba0..62fd7858b 100644
--- a/src/video_core/renderer_vulkan/vk_scheduler.h
+++ b/src/video_core/renderer_vulkan/vk_scheduler.h
@@ -4,6 +4,7 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <atomic>
7#include <condition_variable> 8#include <condition_variable>
8#include <memory> 9#include <memory>
9#include <optional> 10#include <optional>
@@ -18,6 +19,7 @@ namespace Vulkan {
18 19
19class VKDevice; 20class VKDevice;
20class VKFence; 21class VKFence;
22class VKQueryCache;
21class VKResourceManager; 23class VKResourceManager;
22 24
23class VKFenceView { 25class VKFenceView {
@@ -67,6 +69,11 @@ public:
67 /// Binds a pipeline to the current execution context. 69 /// Binds a pipeline to the current execution context.
68 void BindGraphicsPipeline(vk::Pipeline pipeline); 70 void BindGraphicsPipeline(vk::Pipeline pipeline);
69 71
72 /// Assigns the query cache.
73 void SetQueryCache(VKQueryCache& query_cache_) {
74 query_cache = &query_cache_;
75 }
76
70 /// Returns true when viewports have been set in the current command buffer. 77 /// Returns true when viewports have been set in the current command buffer.
71 bool TouchViewports() { 78 bool TouchViewports() {
72 return std::exchange(state.viewports, true); 79 return std::exchange(state.viewports, true);
@@ -112,6 +119,11 @@ public:
112 return current_fence; 119 return current_fence;
113 } 120 }
114 121
122 /// Returns the current command buffer tick.
123 u64 Ticks() const {
124 return ticks;
125 }
126
115private: 127private:
116 class Command { 128 class Command {
117 public: 129 public:
@@ -205,6 +217,8 @@ private:
205 217
206 const VKDevice& device; 218 const VKDevice& device;
207 VKResourceManager& resource_manager; 219 VKResourceManager& resource_manager;
220 VKQueryCache* query_cache = nullptr;
221
208 vk::CommandBuffer current_cmdbuf; 222 vk::CommandBuffer current_cmdbuf;
209 VKFence* current_fence = nullptr; 223 VKFence* current_fence = nullptr;
210 VKFence* next_fence = nullptr; 224 VKFence* next_fence = nullptr;
@@ -227,6 +241,7 @@ private:
227 Common::SPSCQueue<std::unique_ptr<CommandChunk>> chunk_reserve; 241 Common::SPSCQueue<std::unique_ptr<CommandChunk>> chunk_reserve;
228 std::mutex mutex; 242 std::mutex mutex;
229 std::condition_variable cv; 243 std::condition_variable cv;
244 std::atomic<u64> ticks = 0;
230 bool quit = false; 245 bool quit = false;
231}; 246};
232 247
diff --git a/src/video_core/renderer_vulkan/vk_shader_decompiler.cpp b/src/video_core/renderer_vulkan/vk_shader_decompiler.cpp
index 24a658dce..f64f5da28 100644
--- a/src/video_core/renderer_vulkan/vk_shader_decompiler.cpp
+++ b/src/video_core/renderer_vulkan/vk_shader_decompiler.cpp
@@ -275,12 +275,14 @@ public:
275 AddCapability(spv::Capability::ImageGatherExtended); 275 AddCapability(spv::Capability::ImageGatherExtended);
276 AddCapability(spv::Capability::SampledBuffer); 276 AddCapability(spv::Capability::SampledBuffer);
277 AddCapability(spv::Capability::StorageImageWriteWithoutFormat); 277 AddCapability(spv::Capability::StorageImageWriteWithoutFormat);
278 AddCapability(spv::Capability::DrawParameters);
278 AddCapability(spv::Capability::SubgroupBallotKHR); 279 AddCapability(spv::Capability::SubgroupBallotKHR);
279 AddCapability(spv::Capability::SubgroupVoteKHR); 280 AddCapability(spv::Capability::SubgroupVoteKHR);
280 AddExtension("SPV_KHR_shader_ballot"); 281 AddExtension("SPV_KHR_shader_ballot");
281 AddExtension("SPV_KHR_subgroup_vote"); 282 AddExtension("SPV_KHR_subgroup_vote");
282 AddExtension("SPV_KHR_storage_buffer_storage_class"); 283 AddExtension("SPV_KHR_storage_buffer_storage_class");
283 AddExtension("SPV_KHR_variable_pointers"); 284 AddExtension("SPV_KHR_variable_pointers");
285 AddExtension("SPV_KHR_shader_draw_parameters");
284 286
285 if (ir.UsesViewportIndex()) { 287 if (ir.UsesViewportIndex()) {
286 AddCapability(spv::Capability::MultiViewport); 288 AddCapability(spv::Capability::MultiViewport);
@@ -492,9 +494,11 @@ private:
492 interfaces.push_back(AddGlobalVariable(Name(out_vertex, "out_vertex"))); 494 interfaces.push_back(AddGlobalVariable(Name(out_vertex, "out_vertex")));
493 495
494 // Declare input attributes 496 // Declare input attributes
495 vertex_index = DeclareInputBuiltIn(spv::BuiltIn::VertexIndex, t_in_uint, "vertex_index"); 497 vertex_index = DeclareInputBuiltIn(spv::BuiltIn::VertexIndex, t_in_int, "vertex_index");
496 instance_index = 498 instance_index =
497 DeclareInputBuiltIn(spv::BuiltIn::InstanceIndex, t_in_uint, "instance_index"); 499 DeclareInputBuiltIn(spv::BuiltIn::InstanceIndex, t_in_int, "instance_index");
500 base_vertex = DeclareInputBuiltIn(spv::BuiltIn::BaseVertex, t_in_int, "base_vertex");
501 base_instance = DeclareInputBuiltIn(spv::BuiltIn::BaseInstance, t_in_int, "base_instance");
498 } 502 }
499 503
500 void DeclareTessControl() { 504 void DeclareTessControl() {
@@ -1068,9 +1072,12 @@ private:
1068 return {OpLoad(t_float, AccessElement(t_in_float, tess_coord, element)), 1072 return {OpLoad(t_float, AccessElement(t_in_float, tess_coord, element)),
1069 Type::Float}; 1073 Type::Float};
1070 case 2: 1074 case 2:
1071 return {OpLoad(t_uint, instance_index), Type::Uint}; 1075 return {
1076 OpISub(t_int, OpLoad(t_int, instance_index), OpLoad(t_int, base_instance)),
1077 Type::Int};
1072 case 3: 1078 case 3:
1073 return {OpLoad(t_uint, vertex_index), Type::Uint}; 1079 return {OpISub(t_int, OpLoad(t_int, vertex_index), OpLoad(t_int, base_vertex)),
1080 Type::Int};
1074 } 1081 }
1075 UNIMPLEMENTED_MSG("Unmanaged TessCoordInstanceIDVertexID element={}", element); 1082 UNIMPLEMENTED_MSG("Unmanaged TessCoordInstanceIDVertexID element={}", element);
1076 return {Constant(t_uint, 0U), Type::Uint}; 1083 return {Constant(t_uint, 0U), Type::Uint};
@@ -2542,6 +2549,8 @@ private:
2542 2549
2543 Id instance_index{}; 2550 Id instance_index{};
2544 Id vertex_index{}; 2551 Id vertex_index{};
2552 Id base_instance{};
2553 Id base_vertex{};
2545 std::array<Id, Maxwell::NumRenderTargets> frag_colors{}; 2554 std::array<Id, Maxwell::NumRenderTargets> frag_colors{};
2546 Id frag_depth{}; 2555 Id frag_depth{};
2547 Id frag_coord{}; 2556 Id frag_coord{};
diff --git a/src/video_core/shader/decode/conversion.cpp b/src/video_core/shader/decode/conversion.cpp
index 0eeb75559..6ead42070 100644
--- a/src/video_core/shader/decode/conversion.cpp
+++ b/src/video_core/shader/decode/conversion.cpp
@@ -83,14 +83,14 @@ u32 ShaderIR::DecodeConversion(NodeBlock& bb, u32 pc) {
83 83
84 const bool input_signed = instr.conversion.is_input_signed; 84 const bool input_signed = instr.conversion.is_input_signed;
85 85
86 if (instr.conversion.src_size == Register::Size::Byte) { 86 if (const u32 offset = static_cast<u32>(instr.conversion.int_src.selector); offset > 0) {
87 const u32 offset = static_cast<u32>(instr.conversion.int_src.selector) * 8; 87 ASSERT(instr.conversion.src_size == Register::Size::Byte ||
88 if (offset > 0) { 88 instr.conversion.src_size == Register::Size::Short);
89 value = SignedOperation(OperationCode::ILogicalShiftRight, input_signed, 89 if (instr.conversion.src_size == Register::Size::Short) {
90 std::move(value), Immediate(offset)); 90 ASSERT(offset == 0 || offset == 2);
91 } 91 }
92 } else { 92 value = SignedOperation(OperationCode::ILogicalShiftRight, input_signed,
93 UNIMPLEMENTED_IF(instr.conversion.int_src.selector != 0); 93 std::move(value), Immediate(offset * 8));
94 } 94 }
95 95
96 value = ConvertIntegerSize(value, instr.conversion.src_size, input_signed); 96 value = ConvertIntegerSize(value, instr.conversion.src_size, input_signed);
diff --git a/src/video_core/shader/decode/texture.cpp b/src/video_core/shader/decode/texture.cpp
index 351c8c2f1..542636430 100644
--- a/src/video_core/shader/decode/texture.cpp
+++ b/src/video_core/shader/decode/texture.cpp
@@ -522,68 +522,53 @@ Node4 ShaderIR::GetTextureCode(Instruction instr, TextureType texture_type,
522 Node array, Node depth_compare, u32 bias_offset, 522 Node array, Node depth_compare, u32 bias_offset,
523 std::vector<Node> aoffi, 523 std::vector<Node> aoffi,
524 std::optional<Tegra::Shader::Register> bindless_reg) { 524 std::optional<Tegra::Shader::Register> bindless_reg) {
525 const auto is_array = static_cast<bool>(array); 525 const bool is_array = array != nullptr;
526 const auto is_shadow = static_cast<bool>(depth_compare); 526 const bool is_shadow = depth_compare != nullptr;
527 const bool is_bindless = bindless_reg.has_value(); 527 const bool is_bindless = bindless_reg.has_value();
528 528
529 UNIMPLEMENTED_IF_MSG((texture_type == TextureType::Texture3D && (is_array || is_shadow)) || 529 UNIMPLEMENTED_IF(texture_type == TextureType::TextureCube && is_array && is_shadow);
530 (texture_type == TextureType::TextureCube && is_array && is_shadow), 530 ASSERT_MSG(texture_type != TextureType::Texture3D || is_array || is_shadow,
531 "This method is not supported."); 531 "Illegal texture type");
532 532
533 const SamplerInfo info{texture_type, is_array, is_shadow, false}; 533 const SamplerInfo info{texture_type, is_array, is_shadow, false};
534 Node index_var{}; 534 Node index_var;
535 const Sampler* sampler = is_bindless ? GetBindlessSampler(*bindless_reg, index_var, info) 535 const Sampler* sampler = is_bindless ? GetBindlessSampler(*bindless_reg, index_var, info)
536 : GetSampler(instr.sampler, info); 536 : GetSampler(instr.sampler, info);
537 Node4 values; 537 if (!sampler) {
538 if (sampler == nullptr) { 538 return {Immediate(0), Immediate(0), Immediate(0), Immediate(0)};
539 for (u32 element = 0; element < values.size(); ++element) {
540 values[element] = Immediate(0);
541 }
542 return values;
543 } 539 }
544 540
545 const bool lod_needed = process_mode == TextureProcessMode::LZ || 541 const bool lod_needed = process_mode == TextureProcessMode::LZ ||
546 process_mode == TextureProcessMode::LL || 542 process_mode == TextureProcessMode::LL ||
547 process_mode == TextureProcessMode::LLA; 543 process_mode == TextureProcessMode::LLA;
548 544 const OperationCode opcode = lod_needed ? OperationCode::TextureLod : OperationCode::Texture;
549 // LOD selection (either via bias or explicit textureLod) not supported in GL for
550 // sampler2DArrayShadow and samplerCubeArrayShadow.
551 const bool gl_lod_supported =
552 !((texture_type == Tegra::Shader::TextureType::Texture2D && is_array && is_shadow) ||
553 (texture_type == Tegra::Shader::TextureType::TextureCube && is_array && is_shadow));
554
555 const OperationCode read_method =
556 (lod_needed && gl_lod_supported) ? OperationCode::TextureLod : OperationCode::Texture;
557
558 UNIMPLEMENTED_IF(process_mode != TextureProcessMode::None && !gl_lod_supported);
559 545
560 Node bias; 546 Node bias;
561 Node lod; 547 Node lod;
562 if (process_mode != TextureProcessMode::None && gl_lod_supported) { 548 switch (process_mode) {
563 switch (process_mode) { 549 case TextureProcessMode::None:
564 case TextureProcessMode::LZ: 550 break;
565 lod = Immediate(0.0f); 551 case TextureProcessMode::LZ:
566 break; 552 lod = Immediate(0.0f);
567 case TextureProcessMode::LB: 553 break;
568 // If present, lod or bias are always stored in the register 554 case TextureProcessMode::LB:
569 // indexed by the gpr20 field with an offset depending on the 555 // If present, lod or bias are always stored in the register indexed by the gpr20 field with
570 // usage of the other registers 556 // an offset depending on the usage of the other registers.
571 bias = GetRegister(instr.gpr20.Value() + bias_offset); 557 bias = GetRegister(instr.gpr20.Value() + bias_offset);
572 break; 558 break;
573 case TextureProcessMode::LL: 559 case TextureProcessMode::LL:
574 lod = GetRegister(instr.gpr20.Value() + bias_offset); 560 lod = GetRegister(instr.gpr20.Value() + bias_offset);
575 break; 561 break;
576 default: 562 default:
577 UNIMPLEMENTED_MSG("Unimplemented process mode={}", static_cast<u32>(process_mode)); 563 UNIMPLEMENTED_MSG("Unimplemented process mode={}", static_cast<u32>(process_mode));
578 break; 564 break;
579 }
580 } 565 }
581 566
567 Node4 values;
582 for (u32 element = 0; element < values.size(); ++element) { 568 for (u32 element = 0; element < values.size(); ++element) {
583 auto copy_coords = coords;
584 MetaTexture meta{*sampler, array, depth_compare, aoffi, {}, {}, bias, 569 MetaTexture meta{*sampler, array, depth_compare, aoffi, {}, {}, bias,
585 lod, {}, element, index_var}; 570 lod, {}, element, index_var};
586 values[element] = Operation(read_method, meta, std::move(copy_coords)); 571 values[element] = Operation(opcode, meta, coords);
587 } 572 }
588 573
589 return values; 574 return values;
diff --git a/src/video_core/texture_cache/texture_cache.h b/src/video_core/texture_cache/texture_cache.h
index f4c015635..0d105d386 100644
--- a/src/video_core/texture_cache/texture_cache.h
+++ b/src/video_core/texture_cache/texture_cache.h
@@ -721,7 +721,6 @@ private:
721 std::pair<TSurface, TView> GetSurface(const GPUVAddr gpu_addr, const CacheAddr cache_addr, 721 std::pair<TSurface, TView> GetSurface(const GPUVAddr gpu_addr, const CacheAddr cache_addr,
722 const SurfaceParams& params, bool preserve_contents, 722 const SurfaceParams& params, bool preserve_contents,
723 bool is_render) { 723 bool is_render) {
724
725 // Step 1 724 // Step 1
726 // Check Level 1 Cache for a fast structural match. If candidate surface 725 // Check Level 1 Cache for a fast structural match. If candidate surface
727 // matches at certain level we are pretty much done. 726 // matches at certain level we are pretty much done.
@@ -733,14 +732,18 @@ private:
733 return RecycleSurface(overlaps, params, gpu_addr, preserve_contents, 732 return RecycleSurface(overlaps, params, gpu_addr, preserve_contents,
734 topological_result); 733 topological_result);
735 } 734 }
735
736 const auto struct_result = current_surface->MatchesStructure(params); 736 const auto struct_result = current_surface->MatchesStructure(params);
737 if (struct_result != MatchStructureResult::None && 737 if (struct_result != MatchStructureResult::None) {
738 (params.target != SurfaceTarget::Texture3D || 738 const auto& old_params = current_surface->GetSurfaceParams();
739 current_surface->MatchTarget(params.target))) { 739 const bool not_3d = params.target != SurfaceTarget::Texture3D &&
740 if (struct_result == MatchStructureResult::FullMatch) { 740 old_params.target != SurfaceTarget::Texture3D;
741 return ManageStructuralMatch(current_surface, params, is_render); 741 if (not_3d || current_surface->MatchTarget(params.target)) {
742 } else { 742 if (struct_result == MatchStructureResult::FullMatch) {
743 return RebuildSurface(current_surface, params, is_render); 743 return ManageStructuralMatch(current_surface, params, is_render);
744 } else {
745 return RebuildSurface(current_surface, params, is_render);
746 }
744 } 747 }
745 } 748 }
746 } 749 }
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp
index 6683f459f..737ffe409 100644
--- a/src/web_service/web_backend.cpp
+++ b/src/web_service/web_backend.cpp
@@ -73,14 +73,12 @@ struct Client::Impl {
73 if (!parsedUrl.GetPort(&port)) { 73 if (!parsedUrl.GetPort(&port)) {
74 port = HTTP_PORT; 74 port = HTTP_PORT;
75 } 75 }
76 cli = std::make_unique<httplib::Client>(parsedUrl.m_Host.c_str(), port, 76 cli = std::make_unique<httplib::Client>(parsedUrl.m_Host.c_str(), port);
77 TIMEOUT_SECONDS);
78 } else if (parsedUrl.m_Scheme == "https") { 77 } else if (parsedUrl.m_Scheme == "https") {
79 if (!parsedUrl.GetPort(&port)) { 78 if (!parsedUrl.GetPort(&port)) {
80 port = HTTPS_PORT; 79 port = HTTPS_PORT;
81 } 80 }
82 cli = std::make_unique<httplib::SSLClient>(parsedUrl.m_Host.c_str(), port, 81 cli = std::make_unique<httplib::SSLClient>(parsedUrl.m_Host.c_str(), port);
83 TIMEOUT_SECONDS);
84 } else { 82 } else {
85 LOG_ERROR(WebService, "Bad URL scheme {}", parsedUrl.m_Scheme); 83 LOG_ERROR(WebService, "Bad URL scheme {}", parsedUrl.m_Scheme);
86 return Common::WebResult{Common::WebResult::Code::InvalidURL, "Bad URL scheme"}; 84 return Common::WebResult{Common::WebResult::Code::InvalidURL, "Bad URL scheme"};
@@ -90,6 +88,7 @@ struct Client::Impl {
90 LOG_ERROR(WebService, "Invalid URL {}", host + path); 88 LOG_ERROR(WebService, "Invalid URL {}", host + path);
91 return Common::WebResult{Common::WebResult::Code::InvalidURL, "Invalid URL"}; 89 return Common::WebResult{Common::WebResult::Code::InvalidURL, "Invalid URL"};
92 } 90 }
91 cli->set_timeout_sec(TIMEOUT_SECONDS);
93 92
94 httplib::Headers params; 93 httplib::Headers params;
95 if (!jwt.empty()) { 94 if (!jwt.empty()) {