Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++

Make web uploads about as fast as FTP

Leonardo Lobato0e8bab4ab311
diff --git a/src/web/file_store.cpp b/src/web/file_store.cpp
index 7db2b6e..da2818d 100644
--- a/src/web/file_store.cpp
+++ b/src/web/file_store.cpp
@@ -26,7 +26,9 @@ namespace fs = std::filesystem;
using platform::DirectoryPolicy;
using platform::ManagedDirectory;
-constexpr std::size_t kTransferBytes = 256 * 1024;
+// Console storage takes large writes far better than small ones: 127 KiB at a time gave 34 MB/s on internal storage
+// and 1 MiB at a time gave 89 MB/s, with the same disk and the same network (2026-11-04, 1 GiB, wired).
+constexpr std::size_t kTransferBytes = 1024 * 1024;
constexpr std::uint64_t kSpaceMargin = 64ull * 1024 * 1024;
constexpr std::size_t kMaxZipEntries = 65535;
constexpr std::uint64_t kMaxZipBytes = 0x7fffffffu;
@@ -438,16 +440,21 @@ FileError FileStore::Upload(const FileRoot& root, const std::string& path, const
std::uint64_t left = upload.length;
while (left > 0) {
const auto want = static_cast<std::size_t>(std::min<std::uint64_t>(left, buffer.size()));
- const auto count = read(buffer.data(), want);
- if (count <= 0) return finish({408, "The upload stopped before the end."});
+ // The whole buffer before each write: a read gives only what has arrived.
+ std::size_t have = 0;
+ while (have < want) {
+ const auto count = read(buffer.data() + have, want - have);
+ if (count <= 0) return finish({408, "The upload stopped before the end."});
+ have += static_cast<std::size_t>(count);
+ }
std::size_t written = 0;
- while (written < static_cast<std::size_t>(count)) {
- const auto wrote = ::write(fd, buffer.data() + written, static_cast<std::size_t>(count) - written);
+ while (written < have) {
+ const auto wrote = ::write(fd, buffer.data() + written, have - written);
if (wrote < 0 && errno == EINTR) continue;
if (wrote <= 0) return finish(FromErrno(errno, "The file can't be written"));
written += static_cast<std::size_t>(wrote);
}
- left -= static_cast<std::uint64_t>(count);
+ left -= static_cast<std::uint64_t>(have);
}
if (!Step("sync-temporary") || ::fsync(fd) != 0) return finish(FromErrno(errno ? errno : EIO, "The file can't be saved"));
}
diff --git a/src/web/server.cpp b/src/web/server.cpp
index 7e2069b..81134b4 100644
--- a/src/web/server.cpp
+++ b/src/web/server.cpp
@@ -86,6 +86,9 @@ bool Server::Start(const ServerOptions& options, Handler handler, std::string* e
int on = 1;
setsockopt(listener_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
NoSigpipe(listener_);
+ // A system with a lower limit may refuse the size instead of giving what it can.
+ for (int wanted = options.receive_buffer_bytes; wanted >= 64 * 1024; wanted /= 2)
+ if (setsockopt(listener_, SOL_SOCKET, SO_RCVBUF, &wanted, sizeof wanted) == 0) break;
sockaddr_in address{};
address.sin_family = AF_INET;
address.sin_port = htons(static_cast<std::uint16_t>(port));
@@ -109,6 +112,10 @@ bool Server::Start(const ServerOptions& options, Handler handler, std::string* e
socklen_t size = sizeof bound;
if (::getsockname(listener_, reinterpret_cast<sockaddr*>(&bound), &size) != 0) return fail("getsockname: " + ErrorText(errno));
port_ = ntohs(bound.sin_port);
+ int receive_buffer = 0;
+ socklen_t receive_size = sizeof receive_buffer;
+ getsockopt(listener_, SOL_SOCKET, SO_RCVBUF, &receive_buffer, &receive_size);
+ Log("receive buffer=" + std::to_string(receive_buffer));
idle_ = 0;
for (int i = 0; i < std::max(options.workers, 1); ++i) workers_.emplace_back([this] { Work(); });
acceptor_ = std::thread([this] { Run(); });
diff --git a/src/web/server.h b/src/web/server.h
index 2708a69..7dc6f60 100644
--- a/src/web/server.h
+++ b/src/web/server.h
@@ -25,6 +25,9 @@ struct ServerOptions {
int write_timeout_ms = 10'000;
// A connection that has sent nothing gives up after this (a browser's spare connection), so it doesn't hold a worker.
int silent_timeout_ms = 2'000;
+ // Asked for on the listener, before it binds, so accepted sockets inherit it. The console gives at most 512 KiB, and
+ // the default of 64 KiB leaves too little ready for each large write to storage.
+ int receive_buffer_bytes = 4 * 1024 * 1024;
// A request body may go quiet for this long.
int body_timeout_ms = 30'000;
// How often the accept wait looks at Stop. Closing a listening socket does not reliably wake a
diff --git a/tests/web_file_store_test.cpp b/tests/web_file_store_test.cpp
index 147963f..db069f6 100644
--- a/tests/web_file_store_test.cpp
+++ b/tests/web_file_store_test.cpp
@@ -561,6 +561,31 @@ int main() {
fs::remove(folder / "large.txt");
}
+ // A file larger than the transfer buffer, in the odd sizes a network gives: every byte, in order. Cut off part way
+ // through a buffer, nothing is published.
+ {
+ std::string large(3 * 1024 * 1024 + 123, '\0');
+ for (std::size_t i = 0; i < large.size(); ++i) large[i] = static_cast<char>((i * 31) ^ (i >> 8));
+ const auto pieces = [&](std::size_t stop_at) {
+ auto offset = std::make_shared<std::size_t>(0);
+ auto turn = std::make_shared<std::size_t>(0);
+ return UploadReader([&large, offset, turn, stop_at](char* data, std::size_t size) -> std::ptrdiff_t {
+ if (*offset >= stop_at) return -1;
+ const std::size_t sizes[] = {1, 4096, 70001, 13, 1460};
+ const auto count = std::min({size, sizes[(*turn)++ % 5], large.size() - *offset});
+ std::copy_n(large.data() + *offset, count, data);
+ *offset += count;
+ return static_cast<std::ptrdiff_t>(count);
+ });
+ };
+ UploadOptions whole;
+ whole.length = large.size();
+ CHECK(store.Upload(root, "large.bin", whole, pieces(large.size()), nullptr).ok() && Text(folder / "large.bin") == large);
+ CHECK(store.Upload(root, "cut.bin", whole, pieces(1500 * 1024), nullptr).status == 408 && !fs::exists(folder / "cut.bin"));
+ for (const auto& name : Names(folder)) CHECK(name.rfind(".playden-upload-", 0) != 0);
+ fs::remove(folder / "large.bin");
+ }
+
// New folders and deletes.
CHECK(store.MakeFolder(root, "made").ok() && fs::is_directory(folder / "made"));
CHECK(store.MakeFolder(root, "made").status == 409 && store.MakeFolder(root, "a/b").status == 404);
diff --git a/tests/web_server_test.cpp b/tests/web_server_test.cpp
index bd88512..6156ffb 100644
--- a/tests/web_server_test.cpp
+++ b/tests/web_server_test.cpp
@@ -2,6 +2,8 @@
#include "web/router.h"
#include "web/server.h"
+#include <algorithm>
+
#include <arpa/inet.h>
#include <chrono>
#include <cstdio>
@@ -76,11 +78,18 @@ int main() {
options.read_timeout_ms = 500;
options.write_timeout_ms = 500;
options.poll_interval_ms = 50;
+ options.receive_buffer_bytes = 256 * 1024;
options.log = [&](const std::string& line) { std::lock_guard<std::mutex> lock(lines_mutex); lines.push_back(line); };
Server server;
std::string error;
CHECK(server.Start(options, [&router](const Request& request, Response& response) { router.Handle(request, response); }, &error));
CHECK(server.running() && server.port() != 0);
+ // The receive buffer is set on the listener, for every connection it accepts.
+ {
+ std::lock_guard<std::mutex> lock(lines_mutex);
+ const auto line = std::find_if(lines.begin(), lines.end(), [](const std::string& text) { return text.rfind("receive buffer=", 0) == 0; });
+ CHECK(line != lines.end() && std::atoi(line->c_str() + 15) >= 256 * 1024);
+ }
auto answer = Exchange(server.port(), "GET / HTTP/1.1\r\nHost: x\r\n\r\n");
CHECK(answer.rfind("HTTP/1.1 200 OK\r\n", 0) == 0 && answer.size() > 6 && answer.compare(answer.size() - 6, 6, "hello\n") == 0);