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 
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 */