OpenTTD Source
1.11.0-beta2
|
Go to the documentation of this file.
12 #include "../../stdafx.h"
13 #include "../../debug.h"
14 #include "../../rev.h"
15 #include "../network_func.h"
19 #include "../../safeguards.h"
34 HTTPCallback *callback,
const char *host,
const char *url,
35 const char *data,
int depth) :
41 redirect_depth(depth),
45 char *buffer =
AllocaM(
char, bufferSize);
47 DEBUG(net, 7,
"[tcp/http] requesting %s%s", host, url);
48 if (
data !=
nullptr) {
49 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);
54 ssize_t size = strlen(buffer);
55 ssize_t res = send(this->
sock, (
const char*)buffer, size, 0);
72 if (this->
sock != INVALID_SOCKET) closesocket(this->
sock);
73 this->
sock = INVALID_SOCKET;
87 #define return_error(msg) { DEBUG(net, 0, msg); return -1; }
89 static const char *
const NEWLINE =
"\r\n";
91 static const char *
const HTTP_1_0 =
"HTTP/1.0 ";
92 static const char *
const HTTP_1_1 =
"HTTP/1.1 ";
94 static const char *
const LOCATION =
"Location: ";
118 if (strncmp(status,
"200", 3) == 0) {
123 if (length ==
nullptr)
return_error(
"[tcp/http] missing 'content-length' header");
130 char *end_of_line = strstr(length,
NEWLINE);
134 int len = atoi(length);
141 if (len == 0)
return_error(
"[tcp/http] refusing to download 0 bytes");
143 DEBUG(net, 7,
"[tcp/http] downloading %i bytes", len);
147 if (strncmp(status,
"301", 3) != 0 &&
148 strncmp(status,
"302", 3) != 0 &&
149 strncmp(status,
"303", 3) != 0 &&
150 strncmp(status,
"307", 3) != 0) {
155 *strstr(status,
NEWLINE) =
'\0';
156 DEBUG(net, 0,
"[tcp/http] unhandled status reply %s", status);
164 if (uri ==
nullptr)
return_error(
"[tcp/http] missing 'location' header for redirect");
170 char *end_of_line = strstr(uri,
NEWLINE);
173 DEBUG(net, 6,
"[tcp/http] redirecting to %s", uri);
176 if (ret != 0)
return ret;
179 this->
data =
nullptr;
195 char *hname = strstr(uri,
"://");
196 if (hname ==
nullptr)
return_error(
"[tcp/http] invalid location");
200 char *url = strchr(hname,
'/');
201 if (url ==
nullptr)
return_error(
"[tcp/http] invalid location");
206 const char *company =
nullptr;
207 const char *port =
nullptr;
209 if (company !=
nullptr)
return_error(
"[tcp/http] invalid hostname");
233 int err = GET_LAST_ERROR();
234 if (err != EWOULDBLOCK) {
236 if (err != 104)
DEBUG(net, 0,
"recv failed with error %d", err);
253 ssize_t read = this->
recv_pos + res;
262 if (end_of_header ==
nullptr) {
264 DEBUG(net, 0,
"[tcp/http] header too big");
270 if (ret <= 0)
return ret;
275 int len = std::min(read - (end_of_header - this->
recv_buffer), res);
305 FD_SET(handler->sock, &read_fd);
308 tv.tv_sec = tv.tv_usec = 0;
309 int n = select(FD_SETSIZE, &read_fd,
nullptr,
nullptr, &tv);
315 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.
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.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
#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 * GetNetworkRevisionString()
Get the network version string used by this build.
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.