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