# tcp_http.h

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00014 #ifndef NETWORK_CORE_TCP_HTTP_H
    00015 #define NETWORK_CORE_TCP_HTTP_H
    00016
    00017 #include "tcp.h"
    00018
    00019 #ifdef ENABLE_NETWORK
    00020
    00022 struct HTTPCallback {
    00027   virtual void OnFailure() = 0;
    00028
    00035   virtual void OnReceiveData(const char *data, size_t length) = 0;
    00036
    00038   virtual ~HTTPCallback() {}
    00039 };
    00040
    00042 class NetworkHTTPSocketHandler : public NetworkSocketHandler {
    00043 private:
    00044   char recv_buffer[4096];
    00045   int recv_pos;
    00046   int recv_length;
    00047   HTTPCallback *callback;
    00048   const char *data;
    00049   int redirect_depth;
    00050
    00051   int HandleHeader();
    00052   int Receive();
    00053 public:
    00054   SOCKET sock;
    00055
    00060   bool IsConnected() const
    00061   {
    00062     return this->sock != INVALID_SOCKET;
    00063   }
    00064
    00065   virtual NetworkRecvStatus CloseConnection(bool error = true);
    00066
    00067   NetworkHTTPSocketHandler(SOCKET sock, HTTPCallback *callback,
    00068       const char *host, const char *url, const char *data, int depth);
    00069
    00070   ~NetworkHTTPSocketHandler();
    00071
    00072   static int Connect(char *uri, HTTPCallback *callback,
    00073       const char *data = NULL, int depth = 0);
    00074
    00075   static void HTTPReceive();
    00076 };
    00077
    00079 class NetworkHTTPContentConnecter : TCPConnecter {
    00080   HTTPCallback *callback;
    00081   const char *url;
    00082   const char *data;
    00083   int depth;
    00084
    00085 public:
    00094   NetworkHTTPContentConnecter(const NetworkAddress &address,
    00095       HTTPCallback *callback, const char *url,
    00096       const char *data = NULL, int depth = 0) :
    00097     TCPConnecter(address),
    00098     callback(callback),
    00099     url(strdup(url)),
    00100     data(data),
    00101     depth(depth)
    00102   {
    00103   }
    00104
    00106   ~NetworkHTTPContentConnecter()
    00107   {
    00108     free(this->url);
    00109   }
    00110
    00111   virtual void OnFailure()
    00112   {
    00113     this->callback->OnFailure();
    00114     free(this->data);
    00115   }
    00116
    00117   virtual void OnConnect(SOCKET s)
    00118   {
    00119     new NetworkHTTPSocketHandler(s, this->callback, this->address.GetHostname(), this->url, this->data, this->depth);
    00120     /* We've relinguished control of data now. */
    00121     this->data = NULL;
    00122   }
    00123 };
    00124
    00125 #endif /* ENABLE_NETWORK */
    00126
    00127 #endif /* NETWORK_CORE_TCP_HTTP_H */