16 #include "../../stdafx.h" 17 #include "../../debug.h" 18 #include "../../rev.h" 19 #include "../network_func.h" 23 #include "../../safeguards.h" 38 HTTPCallback *callback,
const char *host,
const char *url,
39 const char *data,
int depth) :
45 redirect_depth(depth),
48 size_t bufferSize = strlen(url) + strlen(host) + strlen(_openttd_revision) + (data == NULL ? 0 : strlen(data)) + 128;
49 char *buffer =
AllocaM(
char, bufferSize);
51 DEBUG(net, 7,
"[tcp/http] requesting %s%s", host, url);
53 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, _openttd_revision, (
int)strlen(data), data);
55 seprintf(buffer, buffer + bufferSize - 1,
"GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: OpenTTD/%s\r\n\r\n", url, host, _openttd_revision);
58 ssize_t size = strlen(buffer);
59 ssize_t res = send(this->
sock, (
const char*)buffer, size, 0);
68 *_http_connections.
Append() =
this;
76 if (this->
sock != INVALID_SOCKET) closesocket(this->
sock);
77 this->
sock = INVALID_SOCKET;
91 #define return_error(msg) { DEBUG(net, 0, msg); return -1; } 93 static const char *
const NEWLINE =
"\r\n";
95 static const char *
const HTTP_1_0 =
"HTTP/1.0 ";
96 static const char *
const HTTP_1_1 =
"HTTP/1.1 ";
98 static const char *
const LOCATION =
"Location: ";
122 if (strncmp(status,
"200", 3) == 0) {
127 if (length == NULL)
return_error(
"[tcp/http] missing 'content-length' header");
134 char *end_of_line = strstr(length,
NEWLINE);
138 int len = atoi(length);
145 if (len == 0)
return_error(
"[tcp/http] refusing to download 0 bytes");
147 DEBUG(net, 7,
"[tcp/http] downloading %i bytes", len);
151 if (strncmp(status,
"301", 3) != 0 &&
152 strncmp(status,
"302", 3) != 0 &&
153 strncmp(status,
"303", 3) != 0 &&
154 strncmp(status,
"307", 3) != 0) {
159 *strstr(status,
NEWLINE) =
'\0';
160 DEBUG(net, 0,
"[tcp/http] unhandled status reply %s", status);
168 if (uri == NULL)
return_error(
"[tcp/http] missing 'location' header for redirect");
174 char *end_of_line = strstr(uri,
NEWLINE);
177 DEBUG(net, 6,
"[tcp/http] redirecting to %s", uri);
180 if (ret != 0)
return ret;
199 char *hname = strstr(uri,
"://");
200 if (hname == NULL)
return_error(
"[tcp/http] invalid location");
204 char *url = strchr(hname,
'/');
205 if (url == NULL)
return_error(
"[tcp/http] invalid location");
210 const char *company = NULL;
211 const char *port = NULL;
213 if (company != NULL)
return_error(
"[tcp/http] invalid hostname");
237 int err = GET_LAST_ERROR();
238 if (err != EWOULDBLOCK) {
240 if (err != 104)
DEBUG(net, 0,
"recv failed with error %d", err);
266 if (end_of_header == NULL) {
268 DEBUG(net, 0,
"[tcp/http] header too big");
274 if (ret <= 0)
return ret;
302 if (_http_connections.
Length() == 0)
return;
309 FD_SET((*iter)->sock, &read_fd);
312 tv.tv_sec = tv.tv_usec = 0;
313 #if !defined(__MORPHOS__) && !defined(__AMIGA__) 314 int n = select(FD_SETSIZE, &read_fd, NULL, NULL, &tv);
316 int n = WaitSelect(FD_SETSIZE, &read_fd, NULL, NULL, &tv, NULL);
323 if (FD_ISSET(cur->
sock, &read_fd)) {
330 _http_connections.
Erase(iter);
void ParseConnectionString(const char **company, const char **port, char *connection_string)
Converts a string to ip/port/company Format: IP:port::company.
Connect with a HTTP server and do ONE query.
SOCKET sock
The socket currently connected to.
Basic functions to receive and send HTTP TCP packets.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
virtual NetworkRecvStatus CloseConnection(bool error=true)
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
int Receive()
Handle receiving of HTTP data.
char recv_buffer[4096]
Partially received message.
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 const char *const LOCATION
Header for location.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
const T * Begin() const
Get the pointer to the first item (const)
Simple vector template class.
#define AllocaM(T, num_elements)
alloca() has to be called in the parent function, so define AllocaM() as a macro
static void HTTPReceive()
Do the receiving for all HTTP connections.
const T * End() const
Get the pointer behind the last valid item (const)
int redirect_depth
The depth of the redirection.
T * Append(uint to_add=1)
Append an item and return it.
static const char *const CONTENT_LENGTH
Header for the length of the content.
static const char *const HTTP_1_1
Preamble for HTTP 1.1 servers.
uint Length() const
Get the number of items in the list.
virtual void OnReceiveData(const char *data, size_t length)=0
We're receiving data.
~NetworkHTTPSocketHandler()
Free whatever needs to be freed.
int recv_pos
Current position in buffer.
virtual void OnFailure()=0
An error has occurred and the connection has been closed.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
const char * data
The (POST) data we might want to forward (to a redirect).
#define lengthof(x)
Return the length of an fixed size array.
static T min(const T a, const T b)
Returns the minimum of two values.
int HandleHeader()
Handle the header of a HTTP reply.
#define DEBUG(name, level,...)
Output a line of debugging information.
Base socket handler for HTTP traffic.
virtual NetworkRecvStatus CloseConnection(bool error=true)
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Callback for when the HTTP handler has something to tell us.
void Erase(T *item)
Removes given item from this vector.
static int Connect(char *uri, HTTPCallback *callback, const char *data=NULL, int depth=0)
Connect to the given URI.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
HTTPCallback * callback
The callback to call for the incoming data.
static const char *const NEWLINE
End of line marker.
int recv_length
Length of the data still retrieving.
#define return_error(msg)
Helper to simplify the error handling.
static const char *const END_OF_HEADER
End of header marker.
SocketHandler for all network sockets in OpenTTD.
static SmallVector< NetworkHTTPSocketHandler *, 1 > _http_connections
List of open HTTP connections.