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/>.
9 * @file tcp_http.h Basic functions to receive and send HTTP TCP packets.
12 #ifndef NETWORK_CORE_TCP_HTTP_H
13 #define NETWORK_CORE_TCP_HTTP_H
17 /** Callback for when the HTTP handler has something to tell us. */
20 * An error has occurred and the connection has been closed.
21 * @note HTTP socket handler is closed/freed.
23 virtual void OnFailure() = 0;
26 * We're receiving data.
27 * @param data the received data, nullptr when all data has been received.
28 * @param length the amount of received data, 0 when all data has been received.
29 * @note When nullptr is sent the HTTP socket handler is closed/freed.
31 virtual void OnReceiveData(const char *data
, size_t length
) = 0;
34 virtual ~HTTPCallback() {}
37 /** Base socket handler for HTTP traffic. */
38 class NetworkHTTPSocketHandler
: public NetworkSocketHandler
{
40 char recv_buffer
[4096]; ///< Partially received message.
41 int recv_pos
; ///< Current position in buffer.
42 int recv_length
; ///< Length of the data still retrieving.
43 HTTPCallback
*callback
; ///< The callback to call for the incoming data.
44 const char *data
; ///< The (POST) data we might want to forward (to a redirect).
45 int redirect_depth
; ///< The depth of the redirection.
50 SOCKET sock
; ///< The socket currently connected to
53 * Whether this socket is currently bound to a socket.
54 * @return true when the socket is bound, false otherwise
56 bool IsConnected() const
58 return this->sock
!= INVALID_SOCKET
;
61 NetworkRecvStatus
CloseConnection(bool error
= true) override
;
63 NetworkHTTPSocketHandler(SOCKET sock
, HTTPCallback
*callback
,
64 const char *host
, const char *url
, const char *data
, int depth
);
66 ~NetworkHTTPSocketHandler();
68 static int Connect(char *uri
, HTTPCallback
*callback
,
69 const char *data
= nullptr, int depth
= 0);
71 static void HTTPReceive();
74 /** Connect with a HTTP server and do ONE query. */
75 class NetworkHTTPContentConnecter
: TCPConnecter
{
76 HTTPCallback
*callback
; ///< Callback to tell that we received some data (or won't).
77 const char *url
; ///< The URL we want to get at the server.
78 const char *data
; ///< The data to send
79 int depth
; ///< How far we have recursed
83 * Start the connecting.
84 * @param address the address to connect to
85 * @param callback the callback for HTTP retrieval
86 * @param url the url at the server
87 * @param data the data to send
88 * @param depth the depth (redirect recursion) of the queries
90 NetworkHTTPContentConnecter(const NetworkAddress
&address
,
91 HTTPCallback
*callback
, const char *url
,
92 const char *data
= nullptr, int depth
= 0) :
93 TCPConnecter(address
),
101 /** Free all our allocated data. */
102 ~NetworkHTTPContentConnecter()
107 void OnFailure() override
109 this->callback
->OnFailure();
113 void OnConnect(SOCKET s
) override
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;
121 #endif /* NETWORK_CORE_TCP_HTTP_H */