2010-02-03 18:42:23 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file tcp_http.cpp Basic functions to receive and send HTTP TCP packets.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../../stdafx.h"
|
|
|
|
#include "../../debug.h"
|
|
|
|
#include "../../rev.h"
|
2021-04-29 14:43:13 +00:00
|
|
|
#include "../network_internal.h"
|
2021-04-26 13:18:10 +00:00
|
|
|
#include "game_info.h"
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
#include "tcp_http.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2010-02-03 18:42:23 +00:00
|
|
|
/** List of open HTTP connections. */
|
2019-03-03 17:30:09 +00:00
|
|
|
static std::vector<NetworkHTTPSocketHandler *> _http_connections;
|
2010-02-03 18:42:23 +00:00
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* Start the querying
|
2011-05-01 13:29:40 +00:00
|
|
|
* @param s the socket of this connection
|
2011-01-22 09:53:15 +00:00
|
|
|
* @param callback the callback for HTTP retrieval
|
2011-05-01 13:29:40 +00:00
|
|
|
* @param host the hostname of the server to connect to
|
2011-01-22 09:53:15 +00:00
|
|
|
* @param url the url at the server
|
|
|
|
* @param data the data to send
|
|
|
|
* @param depth the depth (redirect recursion) of the queries
|
|
|
|
*/
|
2010-02-03 18:42:23 +00:00
|
|
|
NetworkHTTPSocketHandler::NetworkHTTPSocketHandler(SOCKET s,
|
|
|
|
HTTPCallback *callback, const char *host, const char *url,
|
|
|
|
const char *data, int depth) :
|
|
|
|
NetworkSocketHandler(),
|
|
|
|
recv_pos(0),
|
|
|
|
recv_length(0),
|
|
|
|
callback(callback),
|
|
|
|
data(data),
|
|
|
|
redirect_depth(depth),
|
|
|
|
sock(s)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
size_t bufferSize = strlen(url) + strlen(host) + strlen(GetNetworkRevisionString()) + (data == nullptr ? 0 : strlen(data)) + 128;
|
2010-02-03 18:42:23 +00:00
|
|
|
char *buffer = AllocaM(char, bufferSize);
|
|
|
|
|
|
|
|
DEBUG(net, 7, "[tcp/http] requesting %s%s", host, url);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (data != nullptr) {
|
2019-01-28 10:10:59 +00:00
|
|
|
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);
|
2010-02-03 18:42:23 +00:00
|
|
|
} else {
|
2019-01-28 10:10:59 +00:00
|
|
|
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());
|
2010-02-03 18:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t size = strlen(buffer);
|
|
|
|
ssize_t res = send(this->sock, (const char*)buffer, size, 0);
|
|
|
|
if (res != size) {
|
|
|
|
/* Sending all data failed. Socket can't handle this little bit
|
|
|
|
* of information? Just fall back to the old system! */
|
|
|
|
this->callback->OnFailure();
|
|
|
|
delete this;
|
2010-11-23 22:16:11 +00:00
|
|
|
return;
|
2010-02-03 18:42:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 22:39:06 +00:00
|
|
|
_http_connections.push_back(this);
|
2010-02-03 18:42:23 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/** Free whatever needs to be freed. */
|
2010-02-03 18:42:23 +00:00
|
|
|
NetworkHTTPSocketHandler::~NetworkHTTPSocketHandler()
|
|
|
|
{
|
|
|
|
this->CloseConnection();
|
|
|
|
|
|
|
|
if (this->sock != INVALID_SOCKET) closesocket(this->sock);
|
|
|
|
this->sock = INVALID_SOCKET;
|
2011-11-12 13:00:29 +00:00
|
|
|
free(this->data);
|
2010-02-03 18:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NetworkRecvStatus NetworkHTTPSocketHandler::CloseConnection(bool error)
|
|
|
|
{
|
|
|
|
NetworkSocketHandler::CloseConnection(error);
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to simplify the error handling.
|
|
|
|
* @param msg the error message to show.
|
|
|
|
*/
|
|
|
|
#define return_error(msg) { DEBUG(net, 0, msg); return -1; }
|
|
|
|
|
|
|
|
static const char * const NEWLINE = "\r\n"; ///< End of line marker
|
|
|
|
static const char * const END_OF_HEADER = "\r\n\r\n"; ///< End of header marker
|
|
|
|
static const char * const HTTP_1_0 = "HTTP/1.0 "; ///< Preamble for HTTP 1.0 servers
|
|
|
|
static const char * const HTTP_1_1 = "HTTP/1.1 "; ///< Preamble for HTTP 1.1 servers
|
|
|
|
static const char * const CONTENT_LENGTH = "Content-Length: "; ///< Header for the length of the content
|
|
|
|
static const char * const LOCATION = "Location: "; ///< Header for location
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the header of a HTTP reply.
|
|
|
|
* @return amount of data to continue downloading.
|
|
|
|
* > 0: we need to download N bytes.
|
|
|
|
* = 0: we're being redirected.
|
2010-07-31 21:02:56 +00:00
|
|
|
* < 0: an error occurred. Downloading failed.
|
|
|
|
* @note if an error occurred the header might not be in its
|
2010-02-03 18:42:23 +00:00
|
|
|
* original state. No effort is undertaken to bring
|
|
|
|
* the header in its original state.
|
|
|
|
*/
|
|
|
|
int NetworkHTTPSocketHandler::HandleHeader()
|
|
|
|
{
|
|
|
|
assert(strlen(HTTP_1_0) == strlen(HTTP_1_1));
|
2019-04-10 21:07:06 +00:00
|
|
|
assert(strstr(this->recv_buffer, END_OF_HEADER) != nullptr);
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
/* We expect a HTTP/1.[01] reply */
|
|
|
|
if (strncmp(this->recv_buffer, HTTP_1_0, strlen(HTTP_1_0)) != 0 &&
|
|
|
|
strncmp(this->recv_buffer, HTTP_1_1, strlen(HTTP_1_1)) != 0) {
|
|
|
|
return_error("[tcp/http] received invalid HTTP reply");
|
|
|
|
}
|
|
|
|
|
|
|
|
char *status = this->recv_buffer + strlen(HTTP_1_0);
|
|
|
|
if (strncmp(status, "200", 3) == 0) {
|
|
|
|
/* We are going to receive a document. */
|
|
|
|
|
|
|
|
/* Get the length of the document to receive */
|
|
|
|
char *length = strcasestr(this->recv_buffer, CONTENT_LENGTH);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (length == nullptr) return_error("[tcp/http] missing 'content-length' header");
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
/* Skip the header */
|
|
|
|
length += strlen(CONTENT_LENGTH);
|
|
|
|
|
|
|
|
/* Search the end of the line. This is safe because the header will
|
|
|
|
* always end with two newlines. */
|
|
|
|
char *end_of_line = strstr(length, NEWLINE);
|
|
|
|
|
|
|
|
/* Read the length */
|
|
|
|
*end_of_line = '\0';
|
|
|
|
int len = atoi(length);
|
|
|
|
/* Restore the header. */
|
|
|
|
*end_of_line = '\r';
|
|
|
|
|
|
|
|
/* Make sure we're going to download at least something;
|
|
|
|
* zero sized files are, for OpenTTD's purposes, always
|
|
|
|
* wrong. You can't have gzips of 0 bytes! */
|
|
|
|
if (len == 0) return_error("[tcp/http] refusing to download 0 bytes");
|
|
|
|
|
|
|
|
DEBUG(net, 7, "[tcp/http] downloading %i bytes", len);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(status, "301", 3) != 0 &&
|
|
|
|
strncmp(status, "302", 3) != 0 &&
|
|
|
|
strncmp(status, "303", 3) != 0 &&
|
|
|
|
strncmp(status, "307", 3) != 0) {
|
|
|
|
/* We are not going to be redirected :(. */
|
|
|
|
|
|
|
|
/* Search the end of the line. This is safe because the header will
|
|
|
|
* always end with two newlines. */
|
|
|
|
*strstr(status, NEWLINE) = '\0';
|
|
|
|
DEBUG(net, 0, "[tcp/http] unhandled status reply %s", status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->redirect_depth == 5) return_error("[tcp/http] too many redirects, looping redirects?");
|
|
|
|
|
|
|
|
/* Redirect to other URL */
|
|
|
|
char *uri = strcasestr(this->recv_buffer, LOCATION);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (uri == nullptr) return_error("[tcp/http] missing 'location' header for redirect");
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
uri += strlen(LOCATION);
|
|
|
|
|
|
|
|
/* Search the end of the line. This is safe because the header will
|
|
|
|
* always end with two newlines. */
|
|
|
|
char *end_of_line = strstr(uri, NEWLINE);
|
|
|
|
*end_of_line = '\0';
|
|
|
|
|
|
|
|
DEBUG(net, 6, "[tcp/http] redirecting to %s", uri);
|
|
|
|
|
|
|
|
int ret = NetworkHTTPSocketHandler::Connect(uri, this->callback, this->data, this->redirect_depth + 1);
|
|
|
|
if (ret != 0) return ret;
|
|
|
|
|
2013-01-08 22:46:42 +00:00
|
|
|
/* We've relinquished control of data now. */
|
2019-04-10 21:07:06 +00:00
|
|
|
this->data = nullptr;
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
/* Restore the header. */
|
|
|
|
*end_of_line = '\r';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* Connect to the given URI.
|
|
|
|
* @param uri the URI to connect to.
|
|
|
|
* @param callback the callback to send data back on.
|
|
|
|
* @param data the data we want to send (as POST).
|
|
|
|
* @param depth the recursion/redirect depth.
|
|
|
|
*/
|
2010-02-10 21:06:05 +00:00
|
|
|
/* static */ int NetworkHTTPSocketHandler::Connect(char *uri, HTTPCallback *callback, const char *data, int depth)
|
2010-02-03 18:42:23 +00:00
|
|
|
{
|
|
|
|
char *hname = strstr(uri, "://");
|
2019-04-10 21:07:06 +00:00
|
|
|
if (hname == nullptr) return_error("[tcp/http] invalid location");
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
hname += 3;
|
|
|
|
|
|
|
|
char *url = strchr(hname, '/');
|
2019-04-10 21:07:06 +00:00
|
|
|
if (url == nullptr) return_error("[tcp/http] invalid location");
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
*url = '\0';
|
|
|
|
|
2021-04-29 10:09:03 +00:00
|
|
|
NetworkAddress address = ParseConnectionString(hname, 80);
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
/* Restore the URL. */
|
|
|
|
*url = '/';
|
|
|
|
new NetworkHTTPContentConnecter(address, callback, url, data, depth);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef return_error
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle receiving of HTTP data.
|
|
|
|
* @return state of the receival of HTTP data.
|
|
|
|
* > 0: we need more cycles for downloading
|
|
|
|
* = 0: we are done downloading
|
|
|
|
* < 0: we have hit an error
|
|
|
|
*/
|
|
|
|
int NetworkHTTPSocketHandler::Receive()
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
ssize_t res = recv(this->sock, (char *)this->recv_buffer + this->recv_pos, lengthof(this->recv_buffer) - this->recv_pos, 0);
|
|
|
|
if (res == -1) {
|
2021-04-27 08:50:28 +00:00
|
|
|
int err = NetworkGetLastError();
|
2010-02-03 18:42:23 +00:00
|
|
|
if (err != EWOULDBLOCK) {
|
2021-04-27 10:13:06 +00:00
|
|
|
/* Something went wrong... (ECONNRESET is connection reset by peer) */
|
|
|
|
if (err != ECONNRESET) DEBUG(net, 0, "recv failed with error %s", NetworkGetErrorString(err));
|
2010-02-03 18:42:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Connection would block, so stop for now */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No more data... did we get everything we wanted? */
|
|
|
|
if (res == 0) {
|
|
|
|
if (this->recv_length != 0) return -1;
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
this->callback->OnReceiveData(nullptr, 0);
|
2010-02-03 18:42:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait till we read the end-of-header identifier */
|
|
|
|
if (this->recv_length == 0) {
|
2021-01-08 10:16:18 +00:00
|
|
|
ssize_t read = this->recv_pos + res;
|
|
|
|
ssize_t end = std::min<ssize_t>(read, lengthof(this->recv_buffer) - 1);
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
/* Do a 'safe' search for the end of the header. */
|
|
|
|
char prev = this->recv_buffer[end];
|
|
|
|
this->recv_buffer[end] = '\0';
|
|
|
|
char *end_of_header = strstr(this->recv_buffer, END_OF_HEADER);
|
|
|
|
this->recv_buffer[end] = prev;
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (end_of_header == nullptr) {
|
2010-02-03 18:42:23 +00:00
|
|
|
if (read == lengthof(this->recv_buffer)) {
|
|
|
|
DEBUG(net, 0, "[tcp/http] header too big");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
this->recv_pos = read;
|
|
|
|
} else {
|
|
|
|
int ret = this->HandleHeader();
|
|
|
|
if (ret <= 0) return ret;
|
|
|
|
|
|
|
|
this->recv_length = ret;
|
|
|
|
|
|
|
|
end_of_header += strlen(END_OF_HEADER);
|
2021-01-08 10:16:18 +00:00
|
|
|
int len = std::min(read - (end_of_header - this->recv_buffer), res);
|
2010-02-03 18:42:23 +00:00
|
|
|
if (len != 0) {
|
|
|
|
this->callback->OnReceiveData(end_of_header, len);
|
|
|
|
this->recv_length -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->recv_pos = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-08 10:16:18 +00:00
|
|
|
res = std::min<ssize_t>(this->recv_length, res);
|
2010-02-03 18:42:23 +00:00
|
|
|
/* Receive whatever we're expecting. */
|
|
|
|
this->callback->OnReceiveData(this->recv_buffer, res);
|
|
|
|
this->recv_length -= res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* Do the receiving for all HTTP connections.
|
|
|
|
*/
|
2010-02-03 18:42:23 +00:00
|
|
|
/* static */ void NetworkHTTPSocketHandler::HTTPReceive()
|
|
|
|
{
|
|
|
|
/* No connections, just bail out. */
|
2018-09-23 11:23:54 +00:00
|
|
|
if (_http_connections.size() == 0) return;
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
fd_set read_fd;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
FD_ZERO(&read_fd);
|
2019-02-17 11:20:52 +00:00
|
|
|
for (NetworkHTTPSocketHandler *handler : _http_connections) {
|
|
|
|
FD_SET(handler->sock, &read_fd);
|
2010-02-03 18:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tv.tv_sec = tv.tv_usec = 0; // don't block at all.
|
2019-04-10 21:07:06 +00:00
|
|
|
int n = select(FD_SETSIZE, &read_fd, nullptr, nullptr, &tv);
|
2010-02-03 18:42:23 +00:00
|
|
|
if (n == -1) return;
|
|
|
|
|
2018-09-25 21:01:05 +00:00
|
|
|
for (auto iter = _http_connections.begin(); iter < _http_connections.end(); /* nothing */) {
|
2010-02-03 18:42:23 +00:00
|
|
|
NetworkHTTPSocketHandler *cur = *iter;
|
|
|
|
|
|
|
|
if (FD_ISSET(cur->sock, &read_fd)) {
|
|
|
|
int ret = cur->Receive();
|
|
|
|
/* First send the failure. */
|
|
|
|
if (ret < 0) cur->callback->OnFailure();
|
|
|
|
if (ret <= 0) {
|
|
|
|
/* Then... the connection can be closed */
|
|
|
|
cur->CloseConnection();
|
2018-09-25 21:01:05 +00:00
|
|
|
iter = _http_connections.erase(iter);
|
2010-02-03 18:42:23 +00:00
|
|
|
delete cur;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
}
|