diff options
author | rubidium42 <rubidium@openttd.org> | 2021-06-13 21:47:32 +0200 |
---|---|---|
committer | rubidium42 <rubidium42@users.noreply.github.com> | 2021-06-15 06:13:00 +0200 |
commit | 49dcf0c772606bfd76ffa083a11d77c8e273f919 (patch) | |
tree | 429482f6ea05e0cc612a43e885f7e36dbd7cd889 /src | |
parent | 53b4786037d8b040991eb7d2146ac11ebd8cb718 (diff) | |
download | openttd-49dcf0c772606bfd76ffa083a11d77c8e273f919.tar.xz |
Codechange: [Network] Simplify constructing the HTTP request with fmt
Diffstat (limited to 'src')
-rw-r--r-- | src/network/core/tcp_http.cpp | 15 | ||||
-rw-r--r-- | src/network/core/tcp_http.h | 4 |
2 files changed, 8 insertions, 11 deletions
diff --git a/src/network/core/tcp_http.cpp b/src/network/core/tcp_http.cpp index abb240708..3472a7642 100644 --- a/src/network/core/tcp_http.cpp +++ b/src/network/core/tcp_http.cpp @@ -32,7 +32,7 @@ static std::vector<NetworkHTTPSocketHandler *> _http_connections; * @param depth the depth (redirect recursion) of the queries */ NetworkHTTPSocketHandler::NetworkHTTPSocketHandler(SOCKET s, - HTTPCallback *callback, const char *host, const char *url, + HTTPCallback *callback, const std::string &host, const char *url, const char *data, int depth) : NetworkSocketHandler(), recv_pos(0), @@ -42,19 +42,16 @@ NetworkHTTPSocketHandler::NetworkHTTPSocketHandler(SOCKET s, redirect_depth(depth), sock(s) { - size_t bufferSize = strlen(url) + strlen(host) + strlen(GetNetworkRevisionString()) + (data == nullptr ? 0 : strlen(data)) + 128; - char *buffer = AllocaM(char, bufferSize); - Debug(net, 5, "[tcp/http] Requesting {}{}", host, url); + std::string request; if (data != nullptr) { - seprintf(buffer, buffer + bufferSize - 1, "POST %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: OpenTTD/%s\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n%s\r\n", url, host, GetNetworkRevisionString(), (int)strlen(data), data); + request = fmt::format("POST {} HTTP/1.0\r\nHost: {}\r\nUser-Agent: OpenTTD/{}\r\nContent-Type: text/plain\r\nContent-Length: {}\r\n\r\n{}\r\n", url, host, GetNetworkRevisionString(), strlen(data), data); } else { - seprintf(buffer, buffer + bufferSize - 1, "GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: OpenTTD/%s\r\n\r\n", url, host, GetNetworkRevisionString()); + request = fmt::format("GET {} HTTP/1.0\r\nHost: {}\r\nUser-Agent: OpenTTD/{}\r\n\r\n", url, host, GetNetworkRevisionString()); } - ssize_t size = strlen(buffer); - ssize_t res = send(this->sock, (const char*)buffer, size, 0); - if (res != size) { + ssize_t res = send(this->sock, request.data(), (int)request.size(), 0); + if (res != (ssize_t)request.size()) { /* Sending all data failed. Socket can't handle this little bit * of information? Just fall back to the old system! */ this->callback->OnFailure(); diff --git a/src/network/core/tcp_http.h b/src/network/core/tcp_http.h index da7a04ac4..608d50200 100644 --- a/src/network/core/tcp_http.h +++ b/src/network/core/tcp_http.h @@ -61,7 +61,7 @@ public: void CloseSocket(); NetworkHTTPSocketHandler(SOCKET sock, HTTPCallback *callback, - const char *host, const char *url, const char *data, int depth); + const std::string &host, const char *url, const char *data, int depth); ~NetworkHTTPSocketHandler(); @@ -112,7 +112,7 @@ public: void OnConnect(SOCKET s) override { - new NetworkHTTPSocketHandler(s, this->callback, this->hostname.c_str(), this->url, this->data, this->depth); + new NetworkHTTPSocketHandler(s, this->callback, this->hostname, this->url, this->data, this->depth); /* We've relinquished control of data now. */ this->data = nullptr; } |