OpenTTD Source  12.0-beta2
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  void CloseSocket();
62 
64  const std::string &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 
76  std::string hostname;
78  const char *url;
79  const char *data;
80  int depth;
81 
82 public:
91  NetworkHTTPContentConnecter(const std::string &hostname, HTTPCallback *callback, const char *url, const char *data = nullptr, int depth = 0) :
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->hostname, 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 */
NetworkHTTPSocketHandler::IsConnected
bool IsConnected() const
Whether this socket is currently bound to a socket.
Definition: tcp_http.h:56
NetworkHTTPSocketHandler
Base socket handler for HTTP traffic.
Definition: tcp_http.h:38
NetworkHTTPSocketHandler::callback
HTTPCallback * callback
The callback to call for the incoming data.
Definition: tcp_http.h:43
HTTPCallback
Callback for when the HTTP handler has something to tell us.
Definition: tcp_http.h:18
TCPConnecter
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:71
NetworkSocketHandler
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:42
NetworkHTTPSocketHandler::data
const char * data
The (POST) data we might want to forward (to a redirect).
Definition: tcp_http.h:44
HTTPCallback::OnReceiveData
virtual void OnReceiveData(const char *data, size_t length)=0
We're receiving data.
NetworkHTTPContentConnecter::url
const char * url
The URL we want to get at the server.
Definition: tcp_http.h:78
NetworkHTTPContentConnecter::data
const char * data
The data to send.
Definition: tcp_http.h:79
NetworkHTTPSocketHandler::~NetworkHTTPSocketHandler
~NetworkHTTPSocketHandler()
Free whatever needs to be freed.
Definition: tcp_http.cpp:66
NetworkHTTPContentConnecter
Connect with a HTTP server and do ONE query.
Definition: tcp_http.h:75
NetworkHTTPSocketHandler::NetworkHTTPSocketHandler
NetworkHTTPSocketHandler(SOCKET sock, HTTPCallback *callback, const std::string &host, const char *url, const char *data, int depth)
Start the querying.
Definition: tcp_http.cpp:34
NetworkHTTPContentConnecter::callback
HTTPCallback * callback
Callback to tell that we received some data (or won't).
Definition: tcp_http.h:77
NetworkHTTPSocketHandler::redirect_depth
int redirect_depth
The depth of the redirection.
Definition: tcp_http.h:45
NetworkHTTPSocketHandler::recv_pos
int recv_pos
Current position in buffer.
Definition: tcp_http.h:41
NetworkHTTPSocketHandler::recv_buffer
char recv_buffer[4096]
Partially received message.
Definition: tcp_http.h:40
NetworkHTTPSocketHandler::CloseSocket
void CloseSocket()
Close the actual socket of the connection.
Definition: tcp_http.cpp:76
NetworkHTTPContentConnecter::OnConnect
void OnConnect(SOCKET s) override
Callback when the connection succeeded.
Definition: tcp_http.h:113
NetworkHTTPContentConnecter::hostname
std::string hostname
Hostname we are connecting to.
Definition: tcp_http.h:76
HTTPCallback::OnFailure
virtual void OnFailure()=0
An error has occurred and the connection has been closed.
NetworkHTTPSocketHandler::Connect
static int Connect(char *uri, HTTPCallback *callback, const char *data=nullptr, int depth=0)
Connect to the given URI.
Definition: tcp_http.cpp:192
NetworkHTTPSocketHandler::Receive
int Receive()
Handle receiving of HTTP data.
Definition: tcp_http.cpp:221
HTTPCallback::~HTTPCallback
virtual ~HTTPCallback()
Silentium.
Definition: tcp_http.h:34
tcp.h
NetworkHTTPSocketHandler::HTTPReceive
static void HTTPReceive()
Do the receiving for all HTTP connections.
Definition: tcp_http.cpp:288
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:137
NetworkHTTPContentConnecter::depth
int depth
How far we have recursed.
Definition: tcp_http.h:80
NetworkHTTPContentConnecter::~NetworkHTTPContentConnecter
~NetworkHTTPContentConnecter()
Free all our allocated data.
Definition: tcp_http.h:102
NetworkHTTPSocketHandler::recv_length
int recv_length
Length of the data still retrieving.
Definition: tcp_http.h:42
NetworkHTTPContentConnecter::OnFailure
void OnFailure() override
Callback for when the connection attempt failed.
Definition: tcp_http.h:107
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
NetworkHTTPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition: tcp_http.h:50
NetworkHTTPContentConnecter::NetworkHTTPContentConnecter
NetworkHTTPContentConnecter(const std::string &hostname, HTTPCallback *callback, const char *url, const char *data=nullptr, int depth=0)
Start the connecting.
Definition: tcp_http.h:91
NetworkHTTPSocketHandler::HandleHeader
int HandleHeader()
Handle the header of a HTTP reply.
Definition: tcp_http.cpp:105