tcp_http.h

Go to the documentation of this file.
00001 /* $Id: tcp_http.h 19001 2010-02-04 13:35:20Z rubidium $ */
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 "address.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 
00075   NetworkHTTPSocketHandler(SOCKET sock, HTTPCallback *callback,
00076       const char *host, const char *url, const char *data, int depth);
00077 
00079   ~NetworkHTTPSocketHandler();
00080 
00088   static int Connect(char *uri, HTTPCallback *callback,
00089       const char *data = NULL, int depth = 0);
00090 
00094   static void HTTPReceive();
00095 };
00096 
00098 class NetworkHTTPContentConnecter : TCPConnecter {
00099   HTTPCallback *callback; 
00100   const char *url;        
00101   const char *data;       
00102   int depth;              
00103 
00104 public:
00113   NetworkHTTPContentConnecter(const NetworkAddress &address,
00114       HTTPCallback *callback, const char *url,
00115       const char *data = NULL, int depth = 0) :
00116     TCPConnecter(address),
00117     callback(callback),
00118     url(strdup(url)),
00119     data(data),
00120     depth(depth)
00121   {
00122   }
00123 
00125   ~NetworkHTTPContentConnecter()
00126   {
00127     free((void*)this->url);
00128   }
00129 
00130   virtual void OnFailure()
00131   {
00132     this->callback->OnFailure();
00133     free((void*)this->data);
00134   }
00135 
00136   virtual void OnConnect(SOCKET s)
00137   {
00138     new NetworkHTTPSocketHandler(s, this->callback, this->address.GetHostname(), this->url, this->data, this->depth);
00139     /* We've relinguished control of data now. */
00140     this->data = NULL;
00141   }
00142 };
00143 
00144 #endif /* ENABLE_NETWORK */
00145 
00146 #endif /* NETWORK_CORE_HTTP_H */

Generated on Wed Mar 3 23:32:22 2010 for OpenTTD by  doxygen 1.6.1