NetServices Stack source
Dependents: HelloWorld ServoInterfaceBoardExample1 4180_Lab4
services/http/client/HTTPClient.h@0:632c9925f013, 2010-06-11 (annotated)
- Committer:
- donatien
- Date:
- Fri Jun 11 16:05:15 2010 +0000
- Revision:
- 0:632c9925f013
- Child:
- 5:dd63a1e02b1b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:632c9925f013 | 1 | |
donatien | 0:632c9925f013 | 2 | /* |
donatien | 0:632c9925f013 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
donatien | 0:632c9925f013 | 4 | |
donatien | 0:632c9925f013 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
donatien | 0:632c9925f013 | 6 | of this software and associated documentation files (the "Software"), to deal |
donatien | 0:632c9925f013 | 7 | in the Software without restriction, including without limitation the rights |
donatien | 0:632c9925f013 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
donatien | 0:632c9925f013 | 9 | copies of the Software, and to permit persons to whom the Software is |
donatien | 0:632c9925f013 | 10 | furnished to do so, subject to the following conditions: |
donatien | 0:632c9925f013 | 11 | |
donatien | 0:632c9925f013 | 12 | The above copyright notice and this permission notice shall be included in |
donatien | 0:632c9925f013 | 13 | all copies or substantial portions of the Software. |
donatien | 0:632c9925f013 | 14 | |
donatien | 0:632c9925f013 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 0:632c9925f013 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 0:632c9925f013 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 0:632c9925f013 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 0:632c9925f013 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 0:632c9925f013 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
donatien | 0:632c9925f013 | 21 | THE SOFTWARE. |
donatien | 0:632c9925f013 | 22 | */ |
donatien | 0:632c9925f013 | 23 | |
donatien | 0:632c9925f013 | 24 | #ifndef HTTP_CLIENT_H |
donatien | 0:632c9925f013 | 25 | #define HTTP_CLIENT_H |
donatien | 0:632c9925f013 | 26 | |
donatien | 0:632c9925f013 | 27 | class HTTPData; |
donatien | 0:632c9925f013 | 28 | |
donatien | 0:632c9925f013 | 29 | #include "if/net/net.h" |
donatien | 0:632c9925f013 | 30 | #include "api/TCPSocket.h" |
donatien | 0:632c9925f013 | 31 | #include "api/DNSRequest.h" |
donatien | 0:632c9925f013 | 32 | #include "HTTPData.h" |
donatien | 0:632c9925f013 | 33 | #include "mbed.h" |
donatien | 0:632c9925f013 | 34 | |
donatien | 0:632c9925f013 | 35 | #include <string> |
donatien | 0:632c9925f013 | 36 | using std::string; |
donatien | 0:632c9925f013 | 37 | |
donatien | 0:632c9925f013 | 38 | #include <map> |
donatien | 0:632c9925f013 | 39 | using std::map; |
donatien | 0:632c9925f013 | 40 | |
donatien | 0:632c9925f013 | 41 | enum HTTPResult |
donatien | 0:632c9925f013 | 42 | { |
donatien | 0:632c9925f013 | 43 | HTTP_OK, |
donatien | 0:632c9925f013 | 44 | HTTP_PROCESSING, |
donatien | 0:632c9925f013 | 45 | HTTP_PARSE, //URI Parse error |
donatien | 0:632c9925f013 | 46 | HTTP_DNS, //Could not resolve name |
donatien | 0:632c9925f013 | 47 | HTTP_PRTCL, //Protocol error |
donatien | 0:632c9925f013 | 48 | HTTP_NOTFOUND, //404 Error |
donatien | 0:632c9925f013 | 49 | HTTP_REFUSED, //403 Error |
donatien | 0:632c9925f013 | 50 | HTTP_ERROR, //xxx error |
donatien | 0:632c9925f013 | 51 | HTTP_TIMEOUT, //Connection timeout |
donatien | 0:632c9925f013 | 52 | HTTP_CONN //Connection error |
donatien | 0:632c9925f013 | 53 | }; |
donatien | 0:632c9925f013 | 54 | |
donatien | 0:632c9925f013 | 55 | |
donatien | 0:632c9925f013 | 56 | |
donatien | 0:632c9925f013 | 57 | class HTTPClient : protected NetService |
donatien | 0:632c9925f013 | 58 | { |
donatien | 0:632c9925f013 | 59 | public: |
donatien | 0:632c9925f013 | 60 | HTTPClient(); |
donatien | 0:632c9925f013 | 61 | virtual ~HTTPClient(); |
donatien | 0:632c9925f013 | 62 | |
donatien | 0:632c9925f013 | 63 | void basicAuth(const char* user, const char* password); //Basic Authentification |
donatien | 0:632c9925f013 | 64 | |
donatien | 0:632c9925f013 | 65 | //High Level setup functions |
donatien | 0:632c9925f013 | 66 | HTTPResult get(const char* uri, HTTPData* pDataIn); //Blocking |
donatien | 0:632c9925f013 | 67 | HTTPResult get(const char* uri, HTTPData* pDataIn, void (*pMethod)(HTTPResult)); //Non blocking |
donatien | 0:632c9925f013 | 68 | template<class T> |
donatien | 0:632c9925f013 | 69 | HTTPResult get(const char* uri, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking |
donatien | 0:632c9925f013 | 70 | { |
donatien | 0:632c9925f013 | 71 | setOnResult(pItem, pMethod); |
donatien | 0:632c9925f013 | 72 | doGet(uri, pDataIn); |
donatien | 0:632c9925f013 | 73 | return HTTP_PROCESSING; |
donatien | 0:632c9925f013 | 74 | } |
donatien | 0:632c9925f013 | 75 | |
donatien | 0:632c9925f013 | 76 | HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn); //Blocking |
donatien | 0:632c9925f013 | 77 | HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, void (*pMethod)(HTTPResult)); //Non blocking |
donatien | 0:632c9925f013 | 78 | template<class T> |
donatien | 0:632c9925f013 | 79 | HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking |
donatien | 0:632c9925f013 | 80 | { |
donatien | 0:632c9925f013 | 81 | setOnResult(pItem, pMethod); |
donatien | 0:632c9925f013 | 82 | doPost(uri, dataOut, pDataIn); |
donatien | 0:632c9925f013 | 83 | return HTTP_PROCESSING; |
donatien | 0:632c9925f013 | 84 | } |
donatien | 0:632c9925f013 | 85 | |
donatien | 0:632c9925f013 | 86 | void doGet(const char* uri, HTTPData* pDataIn); |
donatien | 0:632c9925f013 | 87 | void doPost(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn); |
donatien | 0:632c9925f013 | 88 | |
donatien | 0:632c9925f013 | 89 | void setOnResult( void (*pMethod)(HTTPResult) ); |
donatien | 0:632c9925f013 | 90 | class CDummy; |
donatien | 0:632c9925f013 | 91 | template<class T> |
donatien | 0:632c9925f013 | 92 | //Linker bug : Must be defined here :( |
donatien | 0:632c9925f013 | 93 | void setOnResult( T* pItem, void (T::*pMethod)(HTTPResult) ) |
donatien | 0:632c9925f013 | 94 | { |
donatien | 0:632c9925f013 | 95 | m_pCb = NULL; |
donatien | 0:632c9925f013 | 96 | m_pCbItem = (CDummy*) pItem; |
donatien | 0:632c9925f013 | 97 | m_pCbMeth = (void (CDummy::*)(HTTPResult)) pMethod; |
donatien | 0:632c9925f013 | 98 | } |
donatien | 0:632c9925f013 | 99 | |
donatien | 0:632c9925f013 | 100 | void setTimeout(int ms); |
donatien | 0:632c9925f013 | 101 | |
donatien | 0:632c9925f013 | 102 | virtual void poll(); //Called by NetServices |
donatien | 0:632c9925f013 | 103 | |
donatien | 0:632c9925f013 | 104 | int getHTTPResponseCode(); |
donatien | 0:632c9925f013 | 105 | void setRequestHeader(const string& header, const string& value); |
donatien | 0:632c9925f013 | 106 | string& getResponseHeader(const string& header); |
donatien | 0:632c9925f013 | 107 | void resetRequestHeaders(); |
donatien | 0:632c9925f013 | 108 | |
donatien | 0:632c9925f013 | 109 | protected: |
donatien | 0:632c9925f013 | 110 | void resetTimeout(); |
donatien | 0:632c9925f013 | 111 | |
donatien | 0:632c9925f013 | 112 | void init(); |
donatien | 0:632c9925f013 | 113 | void close(); |
donatien | 0:632c9925f013 | 114 | |
donatien | 0:632c9925f013 | 115 | void setup(const char* uri, HTTPData* pDataOut, HTTPData* pDataIn); //Setup request, make DNS Req if necessary |
donatien | 0:632c9925f013 | 116 | void connect(); //Start Connection |
donatien | 0:632c9925f013 | 117 | |
donatien | 0:632c9925f013 | 118 | int tryRead(); //Read data and try to feed output |
donatien | 0:632c9925f013 | 119 | void readData(); //Data has been read |
donatien | 0:632c9925f013 | 120 | void writeData(); //Data has been written & buf is free |
donatien | 0:632c9925f013 | 121 | |
donatien | 0:632c9925f013 | 122 | void onTCPSocketEvent(TCPSocketEvent e); |
donatien | 0:632c9925f013 | 123 | void onDNSReply(DNSReply r); |
donatien | 0:632c9925f013 | 124 | void onResult(HTTPResult r); //Called when exchange completed or on failure |
donatien | 0:632c9925f013 | 125 | void onTimeout(); //Connection has timed out |
donatien | 0:632c9925f013 | 126 | |
donatien | 0:632c9925f013 | 127 | private: |
donatien | 0:632c9925f013 | 128 | HTTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available |
donatien | 0:632c9925f013 | 129 | |
donatien | 0:632c9925f013 | 130 | bool readHeaders(); //Called first when receiving data |
donatien | 0:632c9925f013 | 131 | bool writeHeaders(); //Called to create req |
donatien | 0:632c9925f013 | 132 | int readLine(char* str, int maxLen, bool* pIncomplete = NULL); |
donatien | 0:632c9925f013 | 133 | |
donatien | 0:632c9925f013 | 134 | enum HTTP_METH |
donatien | 0:632c9925f013 | 135 | { |
donatien | 0:632c9925f013 | 136 | HTTP_GET, |
donatien | 0:632c9925f013 | 137 | HTTP_POST, |
donatien | 0:632c9925f013 | 138 | HTTP_HEAD |
donatien | 0:632c9925f013 | 139 | }; |
donatien | 0:632c9925f013 | 140 | |
donatien | 0:632c9925f013 | 141 | HTTP_METH m_meth; |
donatien | 0:632c9925f013 | 142 | |
donatien | 0:632c9925f013 | 143 | CDummy* m_pCbItem; |
donatien | 0:632c9925f013 | 144 | void (CDummy::*m_pCbMeth)(HTTPResult); |
donatien | 0:632c9925f013 | 145 | |
donatien | 0:632c9925f013 | 146 | void (*m_pCb)(HTTPResult); |
donatien | 0:632c9925f013 | 147 | |
donatien | 0:632c9925f013 | 148 | TCPSocket* m_pTCPSocket; |
donatien | 0:632c9925f013 | 149 | map<string, string> m_reqHeaders; |
donatien | 0:632c9925f013 | 150 | map<string, string> m_respHeaders; |
donatien | 0:632c9925f013 | 151 | |
donatien | 0:632c9925f013 | 152 | Timer m_watchdog; |
donatien | 0:632c9925f013 | 153 | int m_timeout; |
donatien | 0:632c9925f013 | 154 | |
donatien | 0:632c9925f013 | 155 | DNSRequest* m_pDnsReq; |
donatien | 0:632c9925f013 | 156 | |
donatien | 0:632c9925f013 | 157 | Host m_server; |
donatien | 0:632c9925f013 | 158 | string m_path; |
donatien | 0:632c9925f013 | 159 | |
donatien | 0:632c9925f013 | 160 | bool m_closed; |
donatien | 0:632c9925f013 | 161 | |
donatien | 0:632c9925f013 | 162 | enum HTTPStep |
donatien | 0:632c9925f013 | 163 | { |
donatien | 0:632c9925f013 | 164 | // HTTP_INIT, |
donatien | 0:632c9925f013 | 165 | HTTP_WRITE_HEADERS, |
donatien | 0:632c9925f013 | 166 | HTTP_WRITE_DATA, |
donatien | 0:632c9925f013 | 167 | HTTP_READ_HEADERS, |
donatien | 0:632c9925f013 | 168 | HTTP_READ_DATA, |
donatien | 0:632c9925f013 | 169 | HTTP_READ_DATA_INCOMPLETE, |
donatien | 0:632c9925f013 | 170 | HTTP_DONE, |
donatien | 0:632c9925f013 | 171 | HTTP_CLOSED |
donatien | 0:632c9925f013 | 172 | }; |
donatien | 0:632c9925f013 | 173 | |
donatien | 0:632c9925f013 | 174 | HTTPStep m_state; |
donatien | 0:632c9925f013 | 175 | |
donatien | 0:632c9925f013 | 176 | HTTPData* m_pDataOut; |
donatien | 0:632c9925f013 | 177 | HTTPData* m_pDataIn; |
donatien | 0:632c9925f013 | 178 | |
donatien | 0:632c9925f013 | 179 | bool m_dataChunked; //Data is encoded as chunks |
donatien | 0:632c9925f013 | 180 | int m_dataPos; //Position in data |
donatien | 0:632c9925f013 | 181 | int m_dataLen; //Data length |
donatien | 0:632c9925f013 | 182 | char* m_buf; |
donatien | 0:632c9925f013 | 183 | char* m_pBufRemaining; //Remaining |
donatien | 0:632c9925f013 | 184 | int m_bufRemainingLen; //Data length in m_pBufRemaining |
donatien | 0:632c9925f013 | 185 | |
donatien | 0:632c9925f013 | 186 | int m_httpResponseCode; |
donatien | 0:632c9925f013 | 187 | |
donatien | 0:632c9925f013 | 188 | HTTPResult m_blockingResult; //Result if blocking mode |
donatien | 0:632c9925f013 | 189 | |
donatien | 0:632c9925f013 | 190 | }; |
donatien | 0:632c9925f013 | 191 | |
donatien | 0:632c9925f013 | 192 | //Including data containers here for more convenience |
donatien | 0:632c9925f013 | 193 | #include "data/HTTPFile.h" |
donatien | 0:632c9925f013 | 194 | #include "data/HTTPStream.h" |
donatien | 0:632c9925f013 | 195 | #include "data/HTTPText.h" |
donatien | 0:632c9925f013 | 196 | #include "data/HTTPMap.h" |
donatien | 0:632c9925f013 | 197 | |
donatien | 0:632c9925f013 | 198 | #endif |