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.
services/email/smtp/SMTPClient.h@0:ec559500a63f, 2011-04-08 (annotated)
- Committer:
- andrewbonney
- Date:
- Fri Apr 08 14:39:41 2011 +0000
- Revision:
- 0:ec559500a63f
Who changed what in which revision?
User | Revision | Line number | New 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) y Segundo Equipo |
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 | SMTP Client header file |
andrewbonney | 0:ec559500a63f | 26 | */ |
andrewbonney | 0:ec559500a63f | 27 | |
andrewbonney | 0:ec559500a63f | 28 | #ifndef SMTP_CLIENT_H |
andrewbonney | 0:ec559500a63f | 29 | #define SMTP_CLIENT_H |
andrewbonney | 0:ec559500a63f | 30 | |
andrewbonney | 0:ec559500a63f | 31 | class EmailMessage; |
andrewbonney | 0:ec559500a63f | 32 | |
andrewbonney | 0:ec559500a63f | 33 | #include "core/net.h" |
andrewbonney | 0:ec559500a63f | 34 | #include "api/TCPSocket.h" |
andrewbonney | 0:ec559500a63f | 35 | #include "api/DNSRequest.h" |
andrewbonney | 0:ec559500a63f | 36 | #include "EmailMessage.h" |
andrewbonney | 0:ec559500a63f | 37 | #include "mbed.h" |
andrewbonney | 0:ec559500a63f | 38 | |
andrewbonney | 0:ec559500a63f | 39 | ///SMTP client results |
andrewbonney | 0:ec559500a63f | 40 | enum SMTPResult { |
andrewbonney | 0:ec559500a63f | 41 | SMTP_OK, ///<Success |
andrewbonney | 0:ec559500a63f | 42 | SMTP_PROCESSING, ///<Processing |
andrewbonney | 0:ec559500a63f | 43 | SMTP_DNS, ///<Could not resolve name |
andrewbonney | 0:ec559500a63f | 44 | SMTP_PRTCL, ///<Protocol error |
andrewbonney | 0:ec559500a63f | 45 | SMTP_TIMEOUT, ///<Connection timeout |
andrewbonney | 0:ec559500a63f | 46 | SMTP_DISC ///<Disconnected |
andrewbonney | 0:ec559500a63f | 47 | }; |
andrewbonney | 0:ec559500a63f | 48 | |
andrewbonney | 0:ec559500a63f | 49 | ///SMTP authentication |
andrewbonney | 0:ec559500a63f | 50 | enum SMTPAuth { |
andrewbonney | 0:ec559500a63f | 51 | SMTP_AUTH_NONE, ///<No authentication |
andrewbonney | 0:ec559500a63f | 52 | SMTP_AUTH_PLAIN ///<AUTH PLAIN authentication |
andrewbonney | 0:ec559500a63f | 53 | }; |
andrewbonney | 0:ec559500a63f | 54 | #include "core/netservice.h" |
andrewbonney | 0:ec559500a63f | 55 | |
andrewbonney | 0:ec559500a63f | 56 | ///A simple SMTP Client |
andrewbonney | 0:ec559500a63f | 57 | /** |
andrewbonney | 0:ec559500a63f | 58 | The SMTPClient is composed of: |
andrewbonney | 0:ec559500a63f | 59 | - The actual client (SMTPClient) |
andrewbonney | 0:ec559500a63f | 60 | - A class (EmailMessage) to hold the message addresses and content for sending |
andrewbonney | 0:ec559500a63f | 61 | */ |
andrewbonney | 0:ec559500a63f | 62 | class SMTPClient : protected NetService { |
andrewbonney | 0:ec559500a63f | 63 | public: |
andrewbonney | 0:ec559500a63f | 64 | ///Instantiates the SMTP client |
andrewbonney | 0:ec559500a63f | 65 | SMTPClient(); |
andrewbonney | 0:ec559500a63f | 66 | |
andrewbonney | 0:ec559500a63f | 67 | ///Destructor for the SMTP client |
andrewbonney | 0:ec559500a63f | 68 | virtual ~SMTPClient(); |
andrewbonney | 0:ec559500a63f | 69 | |
andrewbonney | 0:ec559500a63f | 70 | ///Full constructor for the SMTP client |
andrewbonney | 0:ec559500a63f | 71 | /** |
andrewbonney | 0:ec559500a63f | 72 | @param host : SMTP server host |
andrewbonney | 0:ec559500a63f | 73 | @param heloDomain : domain name of client |
andrewbonney | 0:ec559500a63f | 74 | @param user : username |
andrewbonney | 0:ec559500a63f | 75 | @param password : password |
andrewbonney | 0:ec559500a63f | 76 | @param auth : authentication type |
andrewbonney | 0:ec559500a63f | 77 | */ |
andrewbonney | 0:ec559500a63f | 78 | SMTPClient(const Host& host, const char* heloDomain, const char* user, const char* password, SMTPAuth auth); |
andrewbonney | 0:ec559500a63f | 79 | |
andrewbonney | 0:ec559500a63f | 80 | ///Set server host |
andrewbonney | 0:ec559500a63f | 81 | /** |
andrewbonney | 0:ec559500a63f | 82 | @param host : SMTP server host |
andrewbonney | 0:ec559500a63f | 83 | */ |
andrewbonney | 0:ec559500a63f | 84 | void setServer(const Host& host); |
andrewbonney | 0:ec559500a63f | 85 | |
andrewbonney | 0:ec559500a63f | 86 | ///Provides a plain authentication feature (Base64 encoded username and password) |
andrewbonney | 0:ec559500a63f | 87 | /** |
andrewbonney | 0:ec559500a63f | 88 | @param user : username |
andrewbonney | 0:ec559500a63f | 89 | @param password : password |
andrewbonney | 0:ec559500a63f | 90 | */ |
andrewbonney | 0:ec559500a63f | 91 | void setAuth(const char* user, const char* password); // Plain authentication |
andrewbonney | 0:ec559500a63f | 92 | |
andrewbonney | 0:ec559500a63f | 93 | ///Turns off authentication |
andrewbonney | 0:ec559500a63f | 94 | void clearAuth(); // Clear authentication |
andrewbonney | 0:ec559500a63f | 95 | |
andrewbonney | 0:ec559500a63f | 96 | ///Set HELO domain (defaults to localhost if not set) |
andrewbonney | 0:ec559500a63f | 97 | /** |
andrewbonney | 0:ec559500a63f | 98 | @param heloDomain : domain name of client (strictly should be fully qualified domain name) |
andrewbonney | 0:ec559500a63f | 99 | */ |
andrewbonney | 0:ec559500a63f | 100 | void setHeloDomain(const char* heloDomain); |
andrewbonney | 0:ec559500a63f | 101 | |
andrewbonney | 0:ec559500a63f | 102 | //High Level setup functions |
andrewbonney | 0:ec559500a63f | 103 | ///Sends the message (blocking) |
andrewbonney | 0:ec559500a63f | 104 | /** |
andrewbonney | 0:ec559500a63f | 105 | @param pMessage : pointer to a message |
andrewbonney | 0:ec559500a63f | 106 | |
andrewbonney | 0:ec559500a63f | 107 | Blocks until completion |
andrewbonney | 0:ec559500a63f | 108 | */ |
andrewbonney | 0:ec559500a63f | 109 | SMTPResult send(EmailMessage* pMessage); //Blocking |
andrewbonney | 0:ec559500a63f | 110 | |
andrewbonney | 0:ec559500a63f | 111 | ///Sends the message (non blocking) |
andrewbonney | 0:ec559500a63f | 112 | /** |
andrewbonney | 0:ec559500a63f | 113 | @param pMessage : pointer to a message |
andrewbonney | 0:ec559500a63f | 114 | @param pMethod : callback function |
andrewbonney | 0:ec559500a63f | 115 | |
andrewbonney | 0:ec559500a63f | 116 | The function returns immediately and calls the callback on completion or error |
andrewbonney | 0:ec559500a63f | 117 | */ |
andrewbonney | 0:ec559500a63f | 118 | SMTPResult send(EmailMessage* pMessage, void (*pMethod)(SMTPResult)); //Non blocking |
andrewbonney | 0:ec559500a63f | 119 | |
andrewbonney | 0:ec559500a63f | 120 | ///Sends the message (non blocking) |
andrewbonney | 0:ec559500a63f | 121 | /** |
andrewbonney | 0:ec559500a63f | 122 | @param pMessage : pointer to a message |
andrewbonney | 0:ec559500a63f | 123 | @param pItem : instance of class on which to execute the callback method |
andrewbonney | 0:ec559500a63f | 124 | @param pMethod : callback method |
andrewbonney | 0:ec559500a63f | 125 | |
andrewbonney | 0:ec559500a63f | 126 | The function returns immediately and calls the callback on completion or error |
andrewbonney | 0:ec559500a63f | 127 | */ |
andrewbonney | 0:ec559500a63f | 128 | template<class T> |
andrewbonney | 0:ec559500a63f | 129 | SMTPResult send(EmailMessage* pMessage, T* pItem, void (T::*pMethod)(SMTPResult)) { //Non blocking |
andrewbonney | 0:ec559500a63f | 130 | setOnResult(pItem, pMethod); |
andrewbonney | 0:ec559500a63f | 131 | doSend(pMessage); |
andrewbonney | 0:ec559500a63f | 132 | return SMTP_PROCESSING; |
andrewbonney | 0:ec559500a63f | 133 | } |
andrewbonney | 0:ec559500a63f | 134 | |
andrewbonney | 0:ec559500a63f | 135 | ///Sends the message (non blocking) |
andrewbonney | 0:ec559500a63f | 136 | /** |
andrewbonney | 0:ec559500a63f | 137 | @param pMessage : pointer to a message |
andrewbonney | 0:ec559500a63f | 138 | |
andrewbonney | 0:ec559500a63f | 139 | The function returns immediately and calls the previously set callback on completion or error |
andrewbonney | 0:ec559500a63f | 140 | */ |
andrewbonney | 0:ec559500a63f | 141 | void doSend(EmailMessage* pMessage); |
andrewbonney | 0:ec559500a63f | 142 | |
andrewbonney | 0:ec559500a63f | 143 | ///Setup the result callback |
andrewbonney | 0:ec559500a63f | 144 | /** |
andrewbonney | 0:ec559500a63f | 145 | @param pMethod : callback function |
andrewbonney | 0:ec559500a63f | 146 | */ |
andrewbonney | 0:ec559500a63f | 147 | void setOnResult( void (*pMethod)(SMTPResult) ); |
andrewbonney | 0:ec559500a63f | 148 | |
andrewbonney | 0:ec559500a63f | 149 | ///Setup the result callback |
andrewbonney | 0:ec559500a63f | 150 | /** |
andrewbonney | 0:ec559500a63f | 151 | @param pItem : instance of class on which to execute the callback method |
andrewbonney | 0:ec559500a63f | 152 | @param pMethod : callback method |
andrewbonney | 0:ec559500a63f | 153 | */ |
andrewbonney | 0:ec559500a63f | 154 | class CDummy; |
andrewbonney | 0:ec559500a63f | 155 | template<class T> |
andrewbonney | 0:ec559500a63f | 156 | void setOnResult( T* pItem, void (T::*pMethod)(SMTPResult) ) { |
andrewbonney | 0:ec559500a63f | 157 | m_pCb = NULL; |
andrewbonney | 0:ec559500a63f | 158 | m_pCbItem = (CDummy*) pItem; |
andrewbonney | 0:ec559500a63f | 159 | m_pCbMeth = (void (CDummy::*)(SMTPResult)) pMethod; |
andrewbonney | 0:ec559500a63f | 160 | } |
andrewbonney | 0:ec559500a63f | 161 | |
andrewbonney | 0:ec559500a63f | 162 | ///Setup timeout |
andrewbonney | 0:ec559500a63f | 163 | /** |
andrewbonney | 0:ec559500a63f | 164 | @param ms : time of connection inactivity in ms after which the request should timeout |
andrewbonney | 0:ec559500a63f | 165 | */ |
andrewbonney | 0:ec559500a63f | 166 | void setTimeout(int ms); |
andrewbonney | 0:ec559500a63f | 167 | |
andrewbonney | 0:ec559500a63f | 168 | ///Gets the last response from the server |
andrewbonney | 0:ec559500a63f | 169 | string& getLastResponse(); |
andrewbonney | 0:ec559500a63f | 170 | |
andrewbonney | 0:ec559500a63f | 171 | virtual void poll(); //Called by NetServices |
andrewbonney | 0:ec559500a63f | 172 | |
andrewbonney | 0:ec559500a63f | 173 | protected: |
andrewbonney | 0:ec559500a63f | 174 | int rc(char* buf); //Return code |
andrewbonney | 0:ec559500a63f | 175 | void process(bool writeable); //Main state-machine |
andrewbonney | 0:ec559500a63f | 176 | |
andrewbonney | 0:ec559500a63f | 177 | void resetTimeout(); |
andrewbonney | 0:ec559500a63f | 178 | |
andrewbonney | 0:ec559500a63f | 179 | void init(); |
andrewbonney | 0:ec559500a63f | 180 | void close(); |
andrewbonney | 0:ec559500a63f | 181 | |
andrewbonney | 0:ec559500a63f | 182 | void setup(EmailMessage* pMessage); //Setup request, make DNS Req if necessary |
andrewbonney | 0:ec559500a63f | 183 | void connect(); //Start Connection |
andrewbonney | 0:ec559500a63f | 184 | |
andrewbonney | 0:ec559500a63f | 185 | int tryRead(); //Read data and try to feed output |
andrewbonney | 0:ec559500a63f | 186 | void readData(); //Data has been read |
andrewbonney | 0:ec559500a63f | 187 | void writeData(); //Data has been written & buf is free |
andrewbonney | 0:ec559500a63f | 188 | |
andrewbonney | 0:ec559500a63f | 189 | void onTCPSocketEvent(TCPSocketEvent e); |
andrewbonney | 0:ec559500a63f | 190 | void onDNSReply(DNSReply r); |
andrewbonney | 0:ec559500a63f | 191 | void onResult(SMTPResult r); //Called when exchange completed or on failure |
andrewbonney | 0:ec559500a63f | 192 | void onTimeout(); //Connection has timed out |
andrewbonney | 0:ec559500a63f | 193 | |
andrewbonney | 0:ec559500a63f | 194 | string encodePlainAuth(); // Encode plain authentication (username and password in based 64) |
andrewbonney | 0:ec559500a63f | 195 | bool okPostAuthentication(int code); // True if server response is ok following authentication |
andrewbonney | 0:ec559500a63f | 196 | |
andrewbonney | 0:ec559500a63f | 197 | private: |
andrewbonney | 0:ec559500a63f | 198 | SMTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available |
andrewbonney | 0:ec559500a63f | 199 | |
andrewbonney | 0:ec559500a63f | 200 | CDummy* m_pCbItem; |
andrewbonney | 0:ec559500a63f | 201 | void (CDummy::*m_pCbMeth)(SMTPResult); |
andrewbonney | 0:ec559500a63f | 202 | void (*m_pCb)(SMTPResult); |
andrewbonney | 0:ec559500a63f | 203 | |
andrewbonney | 0:ec559500a63f | 204 | TCPSocket* m_pTCPSocket; |
andrewbonney | 0:ec559500a63f | 205 | |
andrewbonney | 0:ec559500a63f | 206 | Timer m_watchdog; |
andrewbonney | 0:ec559500a63f | 207 | int m_timeout; |
andrewbonney | 0:ec559500a63f | 208 | |
andrewbonney | 0:ec559500a63f | 209 | DNSRequest* m_pDnsReq; |
andrewbonney | 0:ec559500a63f | 210 | Host m_server; |
andrewbonney | 0:ec559500a63f | 211 | |
andrewbonney | 0:ec559500a63f | 212 | bool m_closed; |
andrewbonney | 0:ec559500a63f | 213 | |
andrewbonney | 0:ec559500a63f | 214 | enum SMTPStep { |
andrewbonney | 0:ec559500a63f | 215 | SMTP_HELLO, |
andrewbonney | 0:ec559500a63f | 216 | SMTP_AUTH, |
andrewbonney | 0:ec559500a63f | 217 | SMTP_FROM, |
andrewbonney | 0:ec559500a63f | 218 | SMTP_TO, |
andrewbonney | 0:ec559500a63f | 219 | SMTP_DATA, |
andrewbonney | 0:ec559500a63f | 220 | SMTP_BODY, |
andrewbonney | 0:ec559500a63f | 221 | SMTP_BODYMORE, |
andrewbonney | 0:ec559500a63f | 222 | SMTP_EOF, |
andrewbonney | 0:ec559500a63f | 223 | SMTP_BYE, |
andrewbonney | 0:ec559500a63f | 224 | SMTP_CLOSED |
andrewbonney | 0:ec559500a63f | 225 | }; |
andrewbonney | 0:ec559500a63f | 226 | |
andrewbonney | 0:ec559500a63f | 227 | SMTPStep m_state; |
andrewbonney | 0:ec559500a63f | 228 | |
andrewbonney | 0:ec559500a63f | 229 | EmailMessage* m_pMessage; |
andrewbonney | 0:ec559500a63f | 230 | |
andrewbonney | 0:ec559500a63f | 231 | int m_posInMsg; |
andrewbonney | 0:ec559500a63f | 232 | int m_posInCRLF; |
andrewbonney | 0:ec559500a63f | 233 | |
andrewbonney | 0:ec559500a63f | 234 | SMTPResult m_blockingResult; //Result if blocking mode |
andrewbonney | 0:ec559500a63f | 235 | string m_response; |
andrewbonney | 0:ec559500a63f | 236 | int m_responseCode; |
andrewbonney | 0:ec559500a63f | 237 | string m_username; |
andrewbonney | 0:ec559500a63f | 238 | string m_password; |
andrewbonney | 0:ec559500a63f | 239 | SMTPAuth m_auth; |
andrewbonney | 0:ec559500a63f | 240 | string m_heloDomain; |
andrewbonney | 0:ec559500a63f | 241 | |
andrewbonney | 0:ec559500a63f | 242 | vector<string>::iterator m_To; |
andrewbonney | 0:ec559500a63f | 243 | }; |
andrewbonney | 0:ec559500a63f | 244 | |
andrewbonney | 0:ec559500a63f | 245 | #endif // SMTP_CLIENT_H |