Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RodColeman 0:850eacf3e945 1
RodColeman 0:850eacf3e945 2 /*
RodColeman 0:850eacf3e945 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) y Segundo Equipo
RodColeman 0:850eacf3e945 4
RodColeman 0:850eacf3e945 5 Permission is hereby granted, free of charge, to any person obtaining a copy
RodColeman 0:850eacf3e945 6 of this software and associated documentation files (the "Software"), to deal
RodColeman 0:850eacf3e945 7 in the Software without restriction, including without limitation the rights
RodColeman 0:850eacf3e945 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
RodColeman 0:850eacf3e945 9 copies of the Software, and to permit persons to whom the Software is
RodColeman 0:850eacf3e945 10 furnished to do so, subject to the following conditions:
RodColeman 0:850eacf3e945 11
RodColeman 0:850eacf3e945 12 The above copyright notice and this permission notice shall be included in
RodColeman 0:850eacf3e945 13 all copies or substantial portions of the Software.
RodColeman 0:850eacf3e945 14
RodColeman 0:850eacf3e945 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
RodColeman 0:850eacf3e945 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
RodColeman 0:850eacf3e945 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
RodColeman 0:850eacf3e945 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
RodColeman 0:850eacf3e945 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
RodColeman 0:850eacf3e945 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
RodColeman 0:850eacf3e945 21 THE SOFTWARE.
RodColeman 0:850eacf3e945 22 */
RodColeman 0:850eacf3e945 23
RodColeman 0:850eacf3e945 24 /** \file
RodColeman 0:850eacf3e945 25 SMTP Client header file
RodColeman 0:850eacf3e945 26 */
RodColeman 0:850eacf3e945 27
RodColeman 0:850eacf3e945 28 #ifndef SMTP_CLIENT_H
RodColeman 0:850eacf3e945 29 #define SMTP_CLIENT_H
RodColeman 0:850eacf3e945 30
RodColeman 0:850eacf3e945 31 class EmailMessage;
RodColeman 0:850eacf3e945 32
RodColeman 0:850eacf3e945 33 #include "core/net.h"
RodColeman 0:850eacf3e945 34 #include "api/TCPSocket.h"
RodColeman 0:850eacf3e945 35 #include "api/DNSRequest.h"
RodColeman 0:850eacf3e945 36 #include "EmailMessage.h"
RodColeman 0:850eacf3e945 37 #include "mbed.h"
RodColeman 0:850eacf3e945 38
RodColeman 0:850eacf3e945 39 ///SMTP client results
RodColeman 0:850eacf3e945 40 enum SMTPResult {
RodColeman 0:850eacf3e945 41 SMTP_OK, ///<Success
RodColeman 0:850eacf3e945 42 SMTP_PROCESSING, ///<Processing
RodColeman 0:850eacf3e945 43 SMTP_DNS, ///<Could not resolve name
RodColeman 0:850eacf3e945 44 SMTP_PRTCL, ///<Protocol error
RodColeman 0:850eacf3e945 45 SMTP_TIMEOUT, ///<Connection timeout
RodColeman 0:850eacf3e945 46 SMTP_DISC ///<Disconnected
RodColeman 0:850eacf3e945 47 };
RodColeman 0:850eacf3e945 48
RodColeman 0:850eacf3e945 49 ///SMTP authentication
RodColeman 0:850eacf3e945 50 enum SMTPAuth {
RodColeman 0:850eacf3e945 51 SMTP_AUTH_NONE, ///<No authentication
RodColeman 0:850eacf3e945 52 SMTP_AUTH_PLAIN ///<AUTH PLAIN authentication
RodColeman 0:850eacf3e945 53 };
RodColeman 0:850eacf3e945 54 #include "core/netservice.h"
RodColeman 0:850eacf3e945 55
RodColeman 0:850eacf3e945 56 ///A simple SMTP Client
RodColeman 0:850eacf3e945 57 /**
RodColeman 0:850eacf3e945 58 The SMTPClient is composed of:
RodColeman 0:850eacf3e945 59 - The actual client (SMTPClient)
RodColeman 0:850eacf3e945 60 - A class (EmailMessage) to hold the message addresses and content for sending
RodColeman 0:850eacf3e945 61 */
RodColeman 0:850eacf3e945 62 class SMTPClient : protected NetService {
RodColeman 0:850eacf3e945 63 public:
RodColeman 0:850eacf3e945 64 ///Instantiates the SMTP client
RodColeman 0:850eacf3e945 65 SMTPClient();
RodColeman 0:850eacf3e945 66
RodColeman 0:850eacf3e945 67 ///Destructor for the SMTP client
RodColeman 0:850eacf3e945 68 virtual ~SMTPClient();
RodColeman 0:850eacf3e945 69
RodColeman 0:850eacf3e945 70 ///Full constructor for the SMTP client
RodColeman 0:850eacf3e945 71 /**
RodColeman 0:850eacf3e945 72 @param host : SMTP server host
RodColeman 0:850eacf3e945 73 @param heloDomain : domain name of client
RodColeman 0:850eacf3e945 74 @param user : username
RodColeman 0:850eacf3e945 75 @param password : password
RodColeman 0:850eacf3e945 76 @param auth : authentication type
RodColeman 0:850eacf3e945 77 */
RodColeman 0:850eacf3e945 78 SMTPClient(const Host& host, const char* heloDomain, const char* user, const char* password, SMTPAuth auth);
RodColeman 0:850eacf3e945 79
RodColeman 0:850eacf3e945 80 ///Set server host
RodColeman 0:850eacf3e945 81 /**
RodColeman 0:850eacf3e945 82 @param host : SMTP server host
RodColeman 0:850eacf3e945 83 */
RodColeman 0:850eacf3e945 84 void setServer(const Host& host);
RodColeman 0:850eacf3e945 85
RodColeman 0:850eacf3e945 86 ///Provides a plain authentication feature (Base64 encoded username and password)
RodColeman 0:850eacf3e945 87 /**
RodColeman 0:850eacf3e945 88 @param user : username
RodColeman 0:850eacf3e945 89 @param password : password
RodColeman 0:850eacf3e945 90 */
RodColeman 0:850eacf3e945 91 void setAuth(const char* user, const char* password); // Plain authentication
RodColeman 0:850eacf3e945 92
RodColeman 0:850eacf3e945 93 ///Turns off authentication
RodColeman 0:850eacf3e945 94 void clearAuth(); // Clear authentication
RodColeman 0:850eacf3e945 95
RodColeman 0:850eacf3e945 96 ///Set HELO domain (defaults to localhost if not set)
RodColeman 0:850eacf3e945 97 /**
RodColeman 0:850eacf3e945 98 @param heloDomain : domain name of client (strictly should be fully qualified domain name)
RodColeman 0:850eacf3e945 99 */
RodColeman 0:850eacf3e945 100 void setHeloDomain(const char* heloDomain);
RodColeman 0:850eacf3e945 101
RodColeman 0:850eacf3e945 102 //High Level setup functions
RodColeman 0:850eacf3e945 103 ///Sends the message (blocking)
RodColeman 0:850eacf3e945 104 /**
RodColeman 0:850eacf3e945 105 @param pMessage : pointer to a message
RodColeman 0:850eacf3e945 106
RodColeman 0:850eacf3e945 107 Blocks until completion
RodColeman 0:850eacf3e945 108 */
RodColeman 0:850eacf3e945 109 SMTPResult send(EmailMessage* pMessage); //Blocking
RodColeman 0:850eacf3e945 110
RodColeman 0:850eacf3e945 111 ///Sends the message (non blocking)
RodColeman 0:850eacf3e945 112 /**
RodColeman 0:850eacf3e945 113 @param pMessage : pointer to a message
RodColeman 0:850eacf3e945 114 @param pMethod : callback function
RodColeman 0:850eacf3e945 115
RodColeman 0:850eacf3e945 116 The function returns immediately and calls the callback on completion or error
RodColeman 0:850eacf3e945 117 */
RodColeman 0:850eacf3e945 118 SMTPResult send(EmailMessage* pMessage, void (*pMethod)(SMTPResult)); //Non blocking
RodColeman 0:850eacf3e945 119
RodColeman 0:850eacf3e945 120 ///Sends the message (non blocking)
RodColeman 0:850eacf3e945 121 /**
RodColeman 0:850eacf3e945 122 @param pMessage : pointer to a message
RodColeman 0:850eacf3e945 123 @param pItem : instance of class on which to execute the callback method
RodColeman 0:850eacf3e945 124 @param pMethod : callback method
RodColeman 0:850eacf3e945 125
RodColeman 0:850eacf3e945 126 The function returns immediately and calls the callback on completion or error
RodColeman 0:850eacf3e945 127 */
RodColeman 0:850eacf3e945 128 template<class T>
RodColeman 0:850eacf3e945 129 SMTPResult send(EmailMessage* pMessage, T* pItem, void (T::*pMethod)(SMTPResult)) { //Non blocking
RodColeman 0:850eacf3e945 130 setOnResult(pItem, pMethod);
RodColeman 0:850eacf3e945 131 doSend(pMessage);
RodColeman 0:850eacf3e945 132 return SMTP_PROCESSING;
RodColeman 0:850eacf3e945 133 }
RodColeman 0:850eacf3e945 134
RodColeman 0:850eacf3e945 135 ///Sends the message (non blocking)
RodColeman 0:850eacf3e945 136 /**
RodColeman 0:850eacf3e945 137 @param pMessage : pointer to a message
RodColeman 0:850eacf3e945 138
RodColeman 0:850eacf3e945 139 The function returns immediately and calls the previously set callback on completion or error
RodColeman 0:850eacf3e945 140 */
RodColeman 0:850eacf3e945 141 void doSend(EmailMessage* pMessage);
RodColeman 0:850eacf3e945 142
RodColeman 0:850eacf3e945 143 ///Setup the result callback
RodColeman 0:850eacf3e945 144 /**
RodColeman 0:850eacf3e945 145 @param pMethod : callback function
RodColeman 0:850eacf3e945 146 */
RodColeman 0:850eacf3e945 147 void setOnResult( void (*pMethod)(SMTPResult) );
RodColeman 0:850eacf3e945 148
RodColeman 0:850eacf3e945 149 ///Setup the result callback
RodColeman 0:850eacf3e945 150 /**
RodColeman 0:850eacf3e945 151 @param pItem : instance of class on which to execute the callback method
RodColeman 0:850eacf3e945 152 @param pMethod : callback method
RodColeman 0:850eacf3e945 153 */
RodColeman 0:850eacf3e945 154 class CDummy;
RodColeman 0:850eacf3e945 155 template<class T>
RodColeman 0:850eacf3e945 156 void setOnResult( T* pItem, void (T::*pMethod)(SMTPResult) ) {
RodColeman 0:850eacf3e945 157 m_pCb = NULL;
RodColeman 0:850eacf3e945 158 m_pCbItem = (CDummy*) pItem;
RodColeman 0:850eacf3e945 159 m_pCbMeth = (void (CDummy::*)(SMTPResult)) pMethod;
RodColeman 0:850eacf3e945 160 }
RodColeman 0:850eacf3e945 161
RodColeman 0:850eacf3e945 162 ///Setup timeout
RodColeman 0:850eacf3e945 163 /**
RodColeman 0:850eacf3e945 164 @param ms : time of connection inactivity in ms after which the request should timeout
RodColeman 0:850eacf3e945 165 */
RodColeman 0:850eacf3e945 166 void setTimeout(int ms);
RodColeman 0:850eacf3e945 167
RodColeman 0:850eacf3e945 168 ///Gets the last response from the server
RodColeman 0:850eacf3e945 169 string& getLastResponse();
RodColeman 0:850eacf3e945 170
RodColeman 0:850eacf3e945 171 virtual void poll(); //Called by NetServices
RodColeman 0:850eacf3e945 172
RodColeman 0:850eacf3e945 173 protected:
RodColeman 0:850eacf3e945 174 int rc(char* buf); //Return code
RodColeman 0:850eacf3e945 175 void process(bool writeable); //Main state-machine
RodColeman 0:850eacf3e945 176
RodColeman 0:850eacf3e945 177 void resetTimeout();
RodColeman 0:850eacf3e945 178
RodColeman 0:850eacf3e945 179 void init();
RodColeman 0:850eacf3e945 180 void close();
RodColeman 0:850eacf3e945 181
RodColeman 0:850eacf3e945 182 void setup(EmailMessage* pMessage); //Setup request, make DNS Req if necessary
RodColeman 0:850eacf3e945 183 void connect(); //Start Connection
RodColeman 0:850eacf3e945 184
RodColeman 0:850eacf3e945 185 int tryRead(); //Read data and try to feed output
RodColeman 0:850eacf3e945 186 void readData(); //Data has been read
RodColeman 0:850eacf3e945 187 void writeData(); //Data has been written & buf is free
RodColeman 0:850eacf3e945 188
RodColeman 0:850eacf3e945 189 void onTCPSocketEvent(TCPSocketEvent e);
RodColeman 0:850eacf3e945 190 void onDNSReply(DNSReply r);
RodColeman 0:850eacf3e945 191 void onResult(SMTPResult r); //Called when exchange completed or on failure
RodColeman 0:850eacf3e945 192 void onTimeout(); //Connection has timed out
RodColeman 0:850eacf3e945 193
RodColeman 0:850eacf3e945 194 string encodePlainAuth(); // Encode plain authentication (username and password in based 64)
RodColeman 0:850eacf3e945 195 bool okPostAuthentication(int code); // True if server response is ok following authentication
RodColeman 0:850eacf3e945 196
RodColeman 0:850eacf3e945 197 private:
RodColeman 0:850eacf3e945 198 SMTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
RodColeman 0:850eacf3e945 199
RodColeman 0:850eacf3e945 200 CDummy* m_pCbItem;
RodColeman 0:850eacf3e945 201 void (CDummy::*m_pCbMeth)(SMTPResult);
RodColeman 0:850eacf3e945 202 void (*m_pCb)(SMTPResult);
RodColeman 0:850eacf3e945 203
RodColeman 0:850eacf3e945 204 TCPSocket* m_pTCPSocket;
RodColeman 0:850eacf3e945 205
RodColeman 0:850eacf3e945 206 Timer m_watchdog;
RodColeman 0:850eacf3e945 207 int m_timeout;
RodColeman 0:850eacf3e945 208
RodColeman 0:850eacf3e945 209 DNSRequest* m_pDnsReq;
RodColeman 0:850eacf3e945 210 Host m_server;
RodColeman 0:850eacf3e945 211
RodColeman 0:850eacf3e945 212 bool m_closed;
RodColeman 0:850eacf3e945 213
RodColeman 0:850eacf3e945 214 enum SMTPStep {
RodColeman 0:850eacf3e945 215 SMTP_HELLO,
RodColeman 0:850eacf3e945 216 SMTP_AUTH,
RodColeman 0:850eacf3e945 217 SMTP_FROM,
RodColeman 0:850eacf3e945 218 SMTP_TO,
RodColeman 0:850eacf3e945 219 SMTP_DATA,
RodColeman 0:850eacf3e945 220 SMTP_BODY,
RodColeman 0:850eacf3e945 221 SMTP_BODYMORE,
RodColeman 0:850eacf3e945 222 SMTP_EOF,
RodColeman 0:850eacf3e945 223 SMTP_BYE,
RodColeman 0:850eacf3e945 224 SMTP_CLOSED
RodColeman 0:850eacf3e945 225 };
RodColeman 0:850eacf3e945 226
RodColeman 0:850eacf3e945 227 SMTPStep m_state;
RodColeman 0:850eacf3e945 228
RodColeman 0:850eacf3e945 229 EmailMessage* m_pMessage;
RodColeman 0:850eacf3e945 230
RodColeman 0:850eacf3e945 231 int m_posInMsg;
RodColeman 0:850eacf3e945 232 int m_posInCRLF;
RodColeman 0:850eacf3e945 233
RodColeman 0:850eacf3e945 234 SMTPResult m_blockingResult; //Result if blocking mode
RodColeman 0:850eacf3e945 235 string m_response;
RodColeman 0:850eacf3e945 236 int m_responseCode;
RodColeman 0:850eacf3e945 237 string m_username;
RodColeman 0:850eacf3e945 238 string m_password;
RodColeman 0:850eacf3e945 239 SMTPAuth m_auth;
RodColeman 0:850eacf3e945 240 string m_heloDomain;
RodColeman 0:850eacf3e945 241
RodColeman 0:850eacf3e945 242 vector<string>::iterator m_To;
RodColeman 0:850eacf3e945 243 };
RodColeman 0:850eacf3e945 244
RodColeman 0:850eacf3e945 245 #endif // SMTP_CLIENT_H