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