Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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