Go to the documentation of this file.
12 #include "../../stdafx.h"
13 #include "../../debug.h"
14 #include "../../rev.h"
15 #include "../network_func.h"
20 #include "../../safeguards.h"
35 HTTPCallback *callback,
const char *host,
const char *url,
36 const char *data,
int depth) :
42 redirect_depth(depth),
46 char *buffer =
AllocaM(
char, bufferSize);
48 DEBUG(net, 7,
"[tcp/http] requesting %s%s", host, url);
49 if (
data !=
nullptr) {
50 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);
55 ssize_t size = strlen(buffer);
56 ssize_t res = send(this->
sock, (
const char*)buffer, size, 0);
73 if (this->
sock != INVALID_SOCKET) closesocket(this->
sock);
74 this->
sock = INVALID_SOCKET;
88 #define return_error(msg) { DEBUG(net, 0, msg); return -1; }
90 static const char *
const NEWLINE =
"\r\n";
92 static const char *
const HTTP_1_0 =
"HTTP/1.0 ";
93 static const char *
const HTTP_1_1 =
"HTTP/1.1 ";
95 static const char *
const LOCATION =
"Location: ";
119 if (strncmp(status,
"200", 3) == 0) {
124 if (length ==
nullptr)
return_error(
"[tcp/http] missing 'content-length' header");
131 char *end_of_line = strstr(length,
NEWLINE);
135 int len = atoi(length);
142 if (len == 0)
return_error(
"[tcp/http] refusing to download 0 bytes");
144 DEBUG(net, 7,
"[tcp/http] downloading %i bytes", len);
148 if (strncmp(status,
"301", 3) != 0 &&
149 strncmp(status,
"302", 3) != 0 &&
150 strncmp(status,
"303", 3) != 0 &&
151 strncmp(status,
"307", 3) != 0) {
156 *strstr(status,
NEWLINE) =
'\0';
157 DEBUG(net, 0,
"[tcp/http] unhandled status reply %s", status);
165 if (uri ==
nullptr)
return_error(
"[tcp/http] missing 'location' header for redirect");
171 char *end_of_line = strstr(uri,
NEWLINE);
174 DEBUG(net, 6,
"[tcp/http] redirecting to %s", uri);
177 if (ret != 0)
return ret;
180 this->
data =
nullptr;
196 char *hname = strstr(uri,
"://");
197 if (hname ==
nullptr)
return_error(
"[tcp/http] invalid location");
201 char *url = strchr(hname,
'/');
202 if (url ==
nullptr)
return_error(
"[tcp/http] invalid location");
207 const char *company =
nullptr;
208 const char *port =
nullptr;
210 if (company !=
nullptr)
return_error(
"[tcp/http] invalid hostname");
254 ssize_t read = this->
recv_pos + res;
263 if (end_of_header ==
nullptr) {
265 DEBUG(net, 0,
"[tcp/http] header too big");
271 if (ret <= 0)
return ret;
276 int len = std::min(read - (end_of_header - this->
recv_buffer), res);
306 FD_SET(handler->sock, &read_fd);
309 tv.tv_sec = tv.tv_usec = 0;
310 int n = select(FD_SETSIZE, &read_fd,
nullptr,
nullptr, &tv);
316 if (FD_ISSET(cur->
sock, &read_fd)) {
static const char *const END_OF_HEADER
End of header marker.
Base socket handler for HTTP traffic.
HTTPCallback * callback
The callback to call for the incoming data.
Callback for when the HTTP handler has something to tell us.
SocketHandler for all network sockets in OpenTTD.
const char * data
The (POST) data we might want to forward (to a redirect).
virtual void OnReceiveData(const char *data, size_t length)=0
We're receiving data.
virtual NetworkRecvStatus CloseConnection(bool error=true)
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
static const char *const LOCATION
Header for location.
~NetworkHTTPSocketHandler()
Free whatever needs to be freed.
Connect with a HTTP server and do ONE query.
int redirect_depth
The depth of the redirection.
int recv_pos
Current position in buffer.
#define DEBUG(name, level,...)
Output a line of debugging information.
char recv_buffer[4096]
Partially received message.
static const char *const HTTP_1_1
Preamble for HTTP 1.1 servers.
bool WouldBlock() const
Check whether this error describes that the operation would block.
static NetworkError GetLast()
Get the last network error.
void ParseConnectionString(const char **company, const char **port, char *connection_string)
Converts a string to ip/port/company Format: IP:port::company.
static std::vector< NetworkHTTPSocketHandler * > _http_connections
List of open HTTP connections.
virtual void OnFailure()=0
An error has occurred and the connection has been closed.
NetworkRecvStatus CloseConnection(bool error=true) override
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
static int Connect(char *uri, HTTPCallback *callback, const char *data=nullptr, int depth=0)
Connect to the given URI.
int Receive()
Handle receiving of HTTP data.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
NetworkHTTPSocketHandler(SOCKET sock, HTTPCallback *callback, const char *host, const char *url, const char *data, int depth)
Start the querying.
static const char *const HTTP_1_0
Preamble for HTTP 1.0 servers.
static void HTTPReceive()
Do the receiving for all HTTP connections.
bool IsConnectionReset() const
Check whether this error describes a connection reset.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Abstraction of a network error where all implementation details of the error codes are encapsulated i...
const char * GetNetworkRevisionString()
Get the network version string used by this build.
#define return_error(msg)
Helper to simplify the error handling.
static const char *const NEWLINE
End of line marker.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
int recv_length
Length of the data still retrieving.
#define lengthof(x)
Return the length of an fixed size array.
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
const char * AsString() const
Get the string representation of the error message.
static const char *const CONTENT_LENGTH
Header for the length of the content.
SOCKET sock
The socket currently connected to.
#define AllocaM(T, num_elements)
alloca() has to be called in the parent function, so define AllocaM() as a macro
int HandleHeader()
Handle the header of a HTTP reply.