(svn r28004) -Update from Eints:
[openttd.git] / src / network / core / tcp_http.h
blob36520f1364190d872522d79c5a3d74f9f1893f70
1 /* $Id$ */
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 */
10 /**
11 * @file tcp_http.h Basic functions to receive and send HTTP TCP packets.
14 #ifndef NETWORK_CORE_TCP_HTTP_H
15 #define NETWORK_CORE_TCP_HTTP_H
17 #include "tcp.h"
19 #ifdef ENABLE_NETWORK
21 /** Callback for when the HTTP handler has something to tell us. */
22 struct HTTPCallback {
23 /**
24 * An error has occurred and the connection has been closed.
25 * @note HTTP socket handler is closed/freed.
27 virtual void OnFailure() = 0;
29 /**
30 * We're receiving data.
31 * @param data the received data, NULL when all data has been received.
32 * @param length the amount of received data, 0 when all data has been received.
33 * @note When NULL is sent the HTTP socket handler is closed/freed.
35 virtual void OnReceiveData(const char *data, size_t length) = 0;
37 /** Silentium */
38 virtual ~HTTPCallback() {}
41 /** Base socket handler for HTTP traffic. */
42 class NetworkHTTPSocketHandler : public NetworkSocketHandler {
43 private:
44 char recv_buffer[4096]; ///< Partially received message.
45 int recv_pos; ///< Current position in buffer.
46 int recv_length; ///< Length of the data still retrieving.
47 HTTPCallback *callback; ///< The callback to call for the incoming data.
48 const char *data; ///< The (POST) data we might want to forward (to a redirect).
49 int redirect_depth; ///< The depth of the redirection.
51 int HandleHeader();
52 int Receive();
53 public:
54 SOCKET sock; ///< The socket currently connected to
56 /**
57 * Whether this socket is currently bound to a socket.
58 * @return true when the socket is bound, false otherwise
60 bool IsConnected() const
62 return this->sock != INVALID_SOCKET;
65 virtual NetworkRecvStatus CloseConnection(bool error = true);
67 NetworkHTTPSocketHandler(SOCKET sock, HTTPCallback *callback,
68 const char *host, const char *url, const char *data, int depth);
70 ~NetworkHTTPSocketHandler();
72 static int Connect(char *uri, HTTPCallback *callback,
73 const char *data = NULL, int depth = 0);
75 static void HTTPReceive();
78 /** Connect with a HTTP server and do ONE query. */
79 class NetworkHTTPContentConnecter : TCPConnecter {
80 HTTPCallback *callback; ///< Callback to tell that we received some data (or won't).
81 const char *url; ///< The URL we want to get at the server.
82 const char *data; ///< The data to send
83 int depth; ///< How far we have recursed
85 public:
86 /**
87 * Start the connecting.
88 * @param address the address to connect to
89 * @param callback the callback for HTTP retrieval
90 * @param url the url at the server
91 * @param data the data to send
92 * @param depth the depth (redirect recursion) of the queries
94 NetworkHTTPContentConnecter(const NetworkAddress &address,
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)
105 /** Free all our allocated data. */
106 ~NetworkHTTPContentConnecter()
108 free(this->url);
111 virtual void OnFailure()
113 this->callback->OnFailure();
114 free(this->data);
117 virtual void OnConnect(SOCKET s)
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;
125 #endif /* ENABLE_NETWORK */
127 #endif /* NETWORK_CORE_TCP_HTTP_H */