OpenTTD
tcp_http.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
12 #ifndef NETWORK_CORE_TCP_HTTP_H
13 #define NETWORK_CORE_TCP_HTTP_H
14 
15 #include "tcp.h"
16 
18 struct HTTPCallback {
23  virtual void OnFailure() = 0;
24 
31  virtual void OnReceiveData(const char *data, size_t length) = 0;
32 
34  virtual ~HTTPCallback() {}
35 };
36 
39 private:
40  char recv_buffer[4096];
41  int recv_pos;
44  const char *data;
46 
47  int HandleHeader();
48  int Receive();
49 public:
50  SOCKET sock;
51 
56  bool IsConnected() const
57  {
58  return this->sock != INVALID_SOCKET;
59  }
60 
61  NetworkRecvStatus CloseConnection(bool error = true) override;
62 
63  NetworkHTTPSocketHandler(SOCKET sock, HTTPCallback *callback,
64  const char *host, const char *url, const char *data, int depth);
65 
67 
68  static int Connect(char *uri, HTTPCallback *callback,
69  const char *data = nullptr, int depth = 0);
70 
71  static void HTTPReceive();
72 };
73 
77  const char *url;
78  const char *data;
79  int depth;
80 
81 public:
91  HTTPCallback *callback, const char *url,
92  const char *data = nullptr, int depth = 0) :
93  TCPConnecter(address),
94  callback(callback),
95  url(stredup(url)),
96  data(data),
97  depth(depth)
98  {
99  }
100 
103  {
104  free(this->url);
105  }
106 
107  void OnFailure() override
108  {
109  this->callback->OnFailure();
110  free(this->data);
111  }
112 
113  void OnConnect(SOCKET s) override
114  {
115  new NetworkHTTPSocketHandler(s, this->callback, this->address.GetHostname(), this->url, this->data, this->depth);
116  /* We've relinquished control of data now. */
117  this->data = nullptr;
118  }
119 };
120 
121 #endif /* NETWORK_CORE_TCP_HTTP_H */
Connect with a HTTP server and do ONE query.
Definition: tcp_http.h:75
SOCKET sock
The socket currently connected to.
Definition: tcp_http.h:50
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:62
virtual ~HTTPCallback()
Silentium.
Definition: tcp_http.h:34
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:27
int redirect_depth
The depth of the redirection.
Definition: tcp_http.h:45
const char * url
The URL we want to get at the server.
Definition: tcp_http.h:77
virtual void OnReceiveData(const char *data, size_t length)=0
We&#39;re receiving data.
const char * data
The data to send.
Definition: tcp_http.h:78
int recv_pos
Current position in buffer.
Definition: tcp_http.h:41
virtual void OnFailure()=0
An error has occurred and the connection has been closed.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:136
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
const char * data
The (POST) data we might want to forward (to a redirect).
Definition: tcp_http.h:44
~NetworkHTTPContentConnecter()
Free all our allocated data.
Definition: tcp_http.h:102
bool IsConnected() const
Whether this socket is currently bound to a socket.
Definition: tcp_http.h:56
void OnConnect(SOCKET s) override
Callback when the connection succeeded.
Definition: tcp_http.h:113
HTTPCallback * callback
Callback to tell that we received some data (or won&#39;t).
Definition: tcp_http.h:76
Base socket handler for HTTP traffic.
Definition: tcp_http.h:38
int depth
How far we have recursed.
Definition: tcp_http.h:79
void OnFailure() override
Callback for when the connection attempt failed.
Definition: tcp_http.h:107
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:112
Callback for when the HTTP handler has something to tell us.
Definition: tcp_http.h:18
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
HTTPCallback * callback
The callback to call for the incoming data.
Definition: tcp_http.h:43
int recv_length
Length of the data still retrieving.
Definition: tcp_http.h:42
Basic functions to receive and send TCP packets.
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:41
NetworkHTTPContentConnecter(const NetworkAddress &address, HTTPCallback *callback, const char *url, const char *data=nullptr, int depth=0)
Start the connecting.
Definition: tcp_http.h:90