Experimental HTTPClient with proxy support

Committer:
igorsk
Date:
Wed Jun 29 16:01:58 2011 +0000
Revision:
0:b56b6a05cad4

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
igorsk 0:b56b6a05cad4 1
igorsk 0:b56b6a05cad4 2 /*
igorsk 0:b56b6a05cad4 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
igorsk 0:b56b6a05cad4 4
igorsk 0:b56b6a05cad4 5 Permission is hereby granted, free of charge, to any person obtaining a copy
igorsk 0:b56b6a05cad4 6 of this software and associated documentation files (the "Software"), to deal
igorsk 0:b56b6a05cad4 7 in the Software without restriction, including without limitation the rights
igorsk 0:b56b6a05cad4 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
igorsk 0:b56b6a05cad4 9 copies of the Software, and to permit persons to whom the Software is
igorsk 0:b56b6a05cad4 10 furnished to do so, subject to the following conditions:
igorsk 0:b56b6a05cad4 11
igorsk 0:b56b6a05cad4 12 The above copyright notice and this permission notice shall be included in
igorsk 0:b56b6a05cad4 13 all copies or substantial portions of the Software.
igorsk 0:b56b6a05cad4 14
igorsk 0:b56b6a05cad4 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
igorsk 0:b56b6a05cad4 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
igorsk 0:b56b6a05cad4 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
igorsk 0:b56b6a05cad4 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
igorsk 0:b56b6a05cad4 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
igorsk 0:b56b6a05cad4 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
igorsk 0:b56b6a05cad4 21 THE SOFTWARE.
igorsk 0:b56b6a05cad4 22 */
igorsk 0:b56b6a05cad4 23
igorsk 0:b56b6a05cad4 24 /** \file
igorsk 0:b56b6a05cad4 25 MySQL Client header file
igorsk 0:b56b6a05cad4 26 */
igorsk 0:b56b6a05cad4 27
igorsk 0:b56b6a05cad4 28 #ifndef MYSQL_CLIENT_H
igorsk 0:b56b6a05cad4 29 #define MYSQL_CLIENT_H
igorsk 0:b56b6a05cad4 30
igorsk 0:b56b6a05cad4 31 #include "core/net.h"
igorsk 0:b56b6a05cad4 32 #include "core/netservice.h"
igorsk 0:b56b6a05cad4 33 #include "api/TCPSocket.h"
igorsk 0:b56b6a05cad4 34 #include "api/DNSRequest.h"
igorsk 0:b56b6a05cad4 35 #include "mbed.h"
igorsk 0:b56b6a05cad4 36
igorsk 0:b56b6a05cad4 37 #include <string>
igorsk 0:b56b6a05cad4 38 using std::string;
igorsk 0:b56b6a05cad4 39
igorsk 0:b56b6a05cad4 40 #include <map>
igorsk 0:b56b6a05cad4 41 using std::map;
igorsk 0:b56b6a05cad4 42
igorsk 0:b56b6a05cad4 43 typedef unsigned char byte;
igorsk 0:b56b6a05cad4 44
igorsk 0:b56b6a05cad4 45 ///MySQL client results
igorsk 0:b56b6a05cad4 46 enum MySQLResult
igorsk 0:b56b6a05cad4 47 {
igorsk 0:b56b6a05cad4 48 MYSQL_OK, ///<Success
igorsk 0:b56b6a05cad4 49 MYSQL_PROCESSING, ///<Processing
igorsk 0:b56b6a05cad4 50 MYSQL_PRTCL, ///<Protocol error
igorsk 0:b56b6a05cad4 51 MYSQL_SETUP, ///<Not properly configured
igorsk 0:b56b6a05cad4 52 MYSQL_DNS, ///<Could not resolve name
igorsk 0:b56b6a05cad4 53 MYSQL_AUTHFAILED, ///<Auth failure
igorsk 0:b56b6a05cad4 54 MYSQL_READY, ///<Ready to send commands
igorsk 0:b56b6a05cad4 55 MYSQL_SQL, ///<SQL Error
igorsk 0:b56b6a05cad4 56 MYSQL_TIMEOUT, ///<Connection timeout
igorsk 0:b56b6a05cad4 57 MYSQL_CONN ///<Connection error
igorsk 0:b56b6a05cad4 58 };
igorsk 0:b56b6a05cad4 59
igorsk 0:b56b6a05cad4 60 ///A MySQL Client
igorsk 0:b56b6a05cad4 61 /**
igorsk 0:b56b6a05cad4 62 This MySQL client implements a limited subset of the MySQL internal client/server protocol (including authentication), for server versions 4.1 and newer.
igorsk 0:b56b6a05cad4 63 */
igorsk 0:b56b6a05cad4 64 class MySQLClient : protected NetService
igorsk 0:b56b6a05cad4 65 {
igorsk 0:b56b6a05cad4 66 public:
igorsk 0:b56b6a05cad4 67 ///Instantiates the MySQL client
igorsk 0:b56b6a05cad4 68 MySQLClient();
igorsk 0:b56b6a05cad4 69 virtual ~MySQLClient();
igorsk 0:b56b6a05cad4 70
igorsk 0:b56b6a05cad4 71 //High Level setup functions
igorsk 0:b56b6a05cad4 72
igorsk 0:b56b6a05cad4 73 ///Opens a connection to a server
igorsk 0:b56b6a05cad4 74 /**
igorsk 0:b56b6a05cad4 75 Opens a connection to the server host using the provided username, password passowrd and selecting database
igorsk 0:b56b6a05cad4 76 On completion of this call (and any further one), the callback set in parameter is fired with the result of that command in parameter
igorsk 0:b56b6a05cad4 77 @param host : server
igorsk 0:b56b6a05cad4 78 @param user : username
igorsk 0:b56b6a05cad4 79 @param db : database to use
igorsk 0:b56b6a05cad4 80 @param pMethod : callback to call on each request completion
igorsk 0:b56b6a05cad4 81 */
igorsk 0:b56b6a05cad4 82 MySQLResult open(Host& host, const string& user, const string& password, const string& db, void (*pMethod)(MySQLResult)); //Non blocking
igorsk 0:b56b6a05cad4 83
igorsk 0:b56b6a05cad4 84 ///Opens a connection to a server
igorsk 0:b56b6a05cad4 85 /**
igorsk 0:b56b6a05cad4 86 Opens a connection to the server host using the provided username, password passowrd and selecting database
igorsk 0:b56b6a05cad4 87 On completion of this call (and any further one), the callback set in parameter is fired with the result of that command in parameter
igorsk 0:b56b6a05cad4 88 @param host : server
igorsk 0:b56b6a05cad4 89 @param user : username
igorsk 0:b56b6a05cad4 90 @param db : database to use
igorsk 0:b56b6a05cad4 91 @param pItem : callback's class instance
igorsk 0:b56b6a05cad4 92 @param pMethod : callback's method to call on each request completion
igorsk 0:b56b6a05cad4 93 */
igorsk 0:b56b6a05cad4 94 template<class T>
igorsk 0:b56b6a05cad4 95 MySQLResult open(Host& host, const string& user, const string& password, const string& db, T* pItem, void (T::*pMethod)(MySQLResult)) //Non blocking
igorsk 0:b56b6a05cad4 96 {
igorsk 0:b56b6a05cad4 97 setOnResult(pItem, pMethod);
igorsk 0:b56b6a05cad4 98 setup(host, user, password, db);
igorsk 0:b56b6a05cad4 99 return MYSQL_PROCESSING;
igorsk 0:b56b6a05cad4 100 }
igorsk 0:b56b6a05cad4 101
igorsk 0:b56b6a05cad4 102
igorsk 0:b56b6a05cad4 103 ///Executes an SQL command
igorsk 0:b56b6a05cad4 104 /**
igorsk 0:b56b6a05cad4 105 Executes an SQL request on the SQL server
igorsk 0:b56b6a05cad4 106 This is a non-blocking function
igorsk 0:b56b6a05cad4 107 On completion, the callback set in the open function is fired with the result of the command in parameter
igorsk 0:b56b6a05cad4 108 @param sqlCommand SQL request to execute
igorsk 0:b56b6a05cad4 109 */
igorsk 0:b56b6a05cad4 110 MySQLResult sql(string& sqlCommand);
igorsk 0:b56b6a05cad4 111
igorsk 0:b56b6a05cad4 112 ///Closes the connection to the server
igorsk 0:b56b6a05cad4 113 MySQLResult exit();
igorsk 0:b56b6a05cad4 114
igorsk 0:b56b6a05cad4 115 void setOnResult( void (*pMethod)(MySQLResult) );
igorsk 0:b56b6a05cad4 116 class CDummy;
igorsk 0:b56b6a05cad4 117 template<class T>
igorsk 0:b56b6a05cad4 118 void setOnResult( T* pItem, void (T::*pMethod)(MySQLResult) )
igorsk 0:b56b6a05cad4 119 {
igorsk 0:b56b6a05cad4 120 m_pCb = NULL;
igorsk 0:b56b6a05cad4 121 m_pCbItem = (CDummy*) pItem;
igorsk 0:b56b6a05cad4 122 m_pCbMeth = (void (CDummy::*)(MySQLResult)) pMethod;
igorsk 0:b56b6a05cad4 123 }
igorsk 0:b56b6a05cad4 124
igorsk 0:b56b6a05cad4 125 ///Setups timeout
igorsk 0:b56b6a05cad4 126 /**
igorsk 0:b56b6a05cad4 127 @param ms : time of connection inactivity in ms after which the request should timeout
igorsk 0:b56b6a05cad4 128 */
igorsk 0:b56b6a05cad4 129 void setTimeout(int ms);
igorsk 0:b56b6a05cad4 130
igorsk 0:b56b6a05cad4 131 virtual void poll(); //Called by NetServices
igorsk 0:b56b6a05cad4 132
igorsk 0:b56b6a05cad4 133 protected:
igorsk 0:b56b6a05cad4 134 void resetTimeout();
igorsk 0:b56b6a05cad4 135
igorsk 0:b56b6a05cad4 136 void init();
igorsk 0:b56b6a05cad4 137 void close();
igorsk 0:b56b6a05cad4 138
igorsk 0:b56b6a05cad4 139 void setup(Host& host, const string& user, const string& password, const string& db); //Setup connection, make DNS Req if necessary
igorsk 0:b56b6a05cad4 140 void connect(); //Start Connection
igorsk 0:b56b6a05cad4 141
igorsk 0:b56b6a05cad4 142 void handleHandshake();
igorsk 0:b56b6a05cad4 143 void sendAuth();
igorsk 0:b56b6a05cad4 144
igorsk 0:b56b6a05cad4 145 void handleAuthResult();
igorsk 0:b56b6a05cad4 146 void sendAuth323();
igorsk 0:b56b6a05cad4 147
igorsk 0:b56b6a05cad4 148 void sendCommand(byte command, byte* arg, int len);
igorsk 0:b56b6a05cad4 149 void handleCommandResult();
igorsk 0:b56b6a05cad4 150
igorsk 0:b56b6a05cad4 151 void readData(); //Copy to buf
igorsk 0:b56b6a05cad4 152 void writeData(); //Copy from buf
igorsk 0:b56b6a05cad4 153
igorsk 0:b56b6a05cad4 154 void onTCPSocketEvent(TCPSocketEvent e);
igorsk 0:b56b6a05cad4 155 void onDNSReply(DNSReply r);
igorsk 0:b56b6a05cad4 156 void onResult(MySQLResult r); //Called when exchange completed or on failure
igorsk 0:b56b6a05cad4 157 void onTimeout(); //Connection has timed out
igorsk 0:b56b6a05cad4 158
igorsk 0:b56b6a05cad4 159 private:
igorsk 0:b56b6a05cad4 160 CDummy* m_pCbItem;
igorsk 0:b56b6a05cad4 161 void (CDummy::*m_pCbMeth)(MySQLResult);
igorsk 0:b56b6a05cad4 162
igorsk 0:b56b6a05cad4 163 void (*m_pCb)(MySQLResult);
igorsk 0:b56b6a05cad4 164
igorsk 0:b56b6a05cad4 165 TCPSocket* m_pTCPSocket;
igorsk 0:b56b6a05cad4 166
igorsk 0:b56b6a05cad4 167 Timer m_watchdog;
igorsk 0:b56b6a05cad4 168 int m_timeout;
igorsk 0:b56b6a05cad4 169
igorsk 0:b56b6a05cad4 170 DNSRequest* m_pDnsReq;
igorsk 0:b56b6a05cad4 171
igorsk 0:b56b6a05cad4 172 bool m_closed;
igorsk 0:b56b6a05cad4 173
igorsk 0:b56b6a05cad4 174 enum MySQLStep
igorsk 0:b56b6a05cad4 175 {
igorsk 0:b56b6a05cad4 176 // MYSQL_INIT,
igorsk 0:b56b6a05cad4 177 MYSQL_HANDSHAKE,
igorsk 0:b56b6a05cad4 178 MYSQL_AUTH,
igorsk 0:b56b6a05cad4 179 MYSQL_COMMANDS,
igorsk 0:b56b6a05cad4 180 MYSQL_CLOSED
igorsk 0:b56b6a05cad4 181 };
igorsk 0:b56b6a05cad4 182
igorsk 0:b56b6a05cad4 183 //Parameters
igorsk 0:b56b6a05cad4 184 Host m_host;
igorsk 0:b56b6a05cad4 185
igorsk 0:b56b6a05cad4 186 string m_user;
igorsk 0:b56b6a05cad4 187 string m_password;
igorsk 0:b56b6a05cad4 188 string m_db;
igorsk 0:b56b6a05cad4 189
igorsk 0:b56b6a05cad4 190 //Low-level buffers & state-machine
igorsk 0:b56b6a05cad4 191 MySQLStep m_state;
igorsk 0:b56b6a05cad4 192
igorsk 0:b56b6a05cad4 193 byte* m_buf;
igorsk 0:b56b6a05cad4 194 byte* m_pPos;
igorsk 0:b56b6a05cad4 195 int m_len;
igorsk 0:b56b6a05cad4 196 int m_size;
igorsk 0:b56b6a05cad4 197
igorsk 0:b56b6a05cad4 198 int m_packetId;
igorsk 0:b56b6a05cad4 199
igorsk 0:b56b6a05cad4 200 };
igorsk 0:b56b6a05cad4 201
igorsk 0:b56b6a05cad4 202 #endif