I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tax 0:66300c77c6e9 1
tax 0:66300c77c6e9 2 /*
tax 0:66300c77c6e9 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
tax 0:66300c77c6e9 4
tax 0:66300c77c6e9 5 Permission is hereby granted, free of charge, to any person obtaining a copy
tax 0:66300c77c6e9 6 of this software and associated documentation files (the "Software"), to deal
tax 0:66300c77c6e9 7 in the Software without restriction, including without limitation the rights
tax 0:66300c77c6e9 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
tax 0:66300c77c6e9 9 copies of the Software, and to permit persons to whom the Software is
tax 0:66300c77c6e9 10 furnished to do so, subject to the following conditions:
tax 0:66300c77c6e9 11
tax 0:66300c77c6e9 12 The above copyright notice and this permission notice shall be included in
tax 0:66300c77c6e9 13 all copies or substantial portions of the Software.
tax 0:66300c77c6e9 14
tax 0:66300c77c6e9 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
tax 0:66300c77c6e9 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
tax 0:66300c77c6e9 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
tax 0:66300c77c6e9 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
tax 0:66300c77c6e9 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
tax 0:66300c77c6e9 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
tax 0:66300c77c6e9 21 THE SOFTWARE.
tax 0:66300c77c6e9 22 */
tax 0:66300c77c6e9 23
tax 0:66300c77c6e9 24 /** \file
tax 0:66300c77c6e9 25 HTTP Client header file
tax 0:66300c77c6e9 26 */
tax 0:66300c77c6e9 27
tax 0:66300c77c6e9 28 #ifndef HTTP_CLIENT_H
tax 0:66300c77c6e9 29 #define HTTP_CLIENT_H
tax 0:66300c77c6e9 30
tax 0:66300c77c6e9 31 class HTTPData;
tax 0:66300c77c6e9 32
tax 0:66300c77c6e9 33 #include "core/net.h"
tax 0:66300c77c6e9 34 #include "api/TCPSocket.h"
tax 0:66300c77c6e9 35 #include "api/DNSRequest.h"
tax 0:66300c77c6e9 36 #include "HTTPData.h"
tax 0:66300c77c6e9 37 #include "mbed.h"
tax 0:66300c77c6e9 38
tax 0:66300c77c6e9 39 #include <string>
tax 0:66300c77c6e9 40 using std::string;
tax 0:66300c77c6e9 41
tax 0:66300c77c6e9 42 #include <map>
tax 0:66300c77c6e9 43 using std::map;
tax 0:66300c77c6e9 44
tax 0:66300c77c6e9 45 ///HTTP client results
tax 0:66300c77c6e9 46 enum HTTPResult
tax 0:66300c77c6e9 47 {
tax 0:66300c77c6e9 48 HTTP_OK, ///<Success
tax 0:66300c77c6e9 49 HTTP_PROCESSING, ///<Processing
tax 0:66300c77c6e9 50 HTTP_PARSE, ///<URI Parse error
tax 0:66300c77c6e9 51 HTTP_DNS, ///<Could not resolve name
tax 0:66300c77c6e9 52 HTTP_PRTCL, ///<Protocol error
tax 0:66300c77c6e9 53 HTTP_NOTFOUND, ///<HTTP 404 Error
tax 0:66300c77c6e9 54 HTTP_REFUSED, ///<HTTP 403 Error
tax 0:66300c77c6e9 55 HTTP_ERROR, ///<HTTP xxx error
tax 0:66300c77c6e9 56 HTTP_TIMEOUT, ///<Connection timeout
tax 0:66300c77c6e9 57 HTTP_CONN ///<Connection error
tax 0:66300c77c6e9 58 };
tax 0:66300c77c6e9 59
tax 0:66300c77c6e9 60 #include "core/netservice.h"
tax 0:66300c77c6e9 61
tax 0:66300c77c6e9 62 ///A simple HTTP Client
tax 0:66300c77c6e9 63 /**
tax 0:66300c77c6e9 64 The HTTPClient is composed of:
tax 0:66300c77c6e9 65 - The actual client (HTTPClient)
tax 0:66300c77c6e9 66 - Classes that act as a data repository, each of which deriving from the HTTPData class (HTTPText for short text content, HTTPFile for file I/O, HTTPMap for key/value pairs, and HTTPStream for streaming purposes)
tax 0:66300c77c6e9 67 */
tax 0:66300c77c6e9 68 class HTTPClient : protected NetService
tax 0:66300c77c6e9 69 {
tax 0:66300c77c6e9 70 public:
tax 0:66300c77c6e9 71 ///Instantiates the HTTP client
tax 0:66300c77c6e9 72 HTTPClient();
tax 0:66300c77c6e9 73 virtual ~HTTPClient();
tax 0:66300c77c6e9 74
tax 0:66300c77c6e9 75 ///Provides a basic authentification feature (Base64 encoded username and password)
tax 0:66300c77c6e9 76 void basicAuth(const char* user, const char* password); //Basic Authentification
tax 0:66300c77c6e9 77
tax 0:66300c77c6e9 78 //High Level setup functions
tax 0:66300c77c6e9 79 ///Executes a GET Request (blocking)
tax 0:66300c77c6e9 80 /**
tax 0:66300c77c6e9 81 Executes a GET request on the URI uri
tax 0:66300c77c6e9 82 @param uri : URI on which to execute the request
tax 0:66300c77c6e9 83 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
tax 0:66300c77c6e9 84 Blocks until completion
tax 0:66300c77c6e9 85 */
tax 0:66300c77c6e9 86 HTTPResult get(const char* uri, HTTPData* pDataIn); //Blocking
tax 0:66300c77c6e9 87
tax 0:66300c77c6e9 88 ///Executes a GET Request (non blocking)
tax 0:66300c77c6e9 89 /**
tax 0:66300c77c6e9 90 Executes a GET request on the URI uri
tax 0:66300c77c6e9 91 @param uri : URI on which to execute the request
tax 0:66300c77c6e9 92 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
tax 0:66300c77c6e9 93 @param pMethod : callback function
tax 0:66300c77c6e9 94 The function returns immediately and calls the callback on completion or error
tax 0:66300c77c6e9 95 */
tax 0:66300c77c6e9 96 HTTPResult get(const char* uri, HTTPData* pDataIn, void (*pMethod)(HTTPResult)); //Non blocking
tax 0:66300c77c6e9 97
tax 0:66300c77c6e9 98 ///Executes a GET Request (non blocking)
tax 0:66300c77c6e9 99 /**
tax 0:66300c77c6e9 100 Executes a GET request on the URI uri
tax 0:66300c77c6e9 101 @param uri : URI on which to execute the request
tax 0:66300c77c6e9 102 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
tax 0:66300c77c6e9 103 @param pItem : instance of class on which to execute the callback method
tax 0:66300c77c6e9 104 @param pMethod : callback method
tax 0:66300c77c6e9 105 The function returns immediately and calls the callback on completion or error
tax 0:66300c77c6e9 106 */
tax 0:66300c77c6e9 107 template<class T>
tax 0:66300c77c6e9 108 HTTPResult get(const char* uri, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking
tax 0:66300c77c6e9 109 {
tax 0:66300c77c6e9 110 setOnResult(pItem, pMethod);
tax 0:66300c77c6e9 111 doGet(uri, pDataIn);
tax 0:66300c77c6e9 112 return HTTP_PROCESSING;
tax 0:66300c77c6e9 113 }
tax 0:66300c77c6e9 114
tax 0:66300c77c6e9 115 ///Executes a POST Request (blocking)
tax 0:66300c77c6e9 116 /**
tax 0:66300c77c6e9 117 Executes a POST request on the URI uri
tax 0:66300c77c6e9 118 @param uri : URI on which to execute the request
tax 0:66300c77c6e9 119 @param dataOut : a HTTPData instance that contains the data that will be posted
tax 0:66300c77c6e9 120 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
tax 0:66300c77c6e9 121 Blocks until completion
tax 0:66300c77c6e9 122 */
tax 0:66300c77c6e9 123 HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn); //Blocking
tax 0:66300c77c6e9 124
tax 0:66300c77c6e9 125 ///Executes a POST Request (non blocking)
tax 0:66300c77c6e9 126 /**
tax 0:66300c77c6e9 127 Executes a POST request on the URI uri
tax 0:66300c77c6e9 128 @param uri : URI on which to execute the request
tax 0:66300c77c6e9 129 @param dataOut : a HTTPData instance that contains the data that will be posted
tax 0:66300c77c6e9 130 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
tax 0:66300c77c6e9 131 @param pMethod : callback function
tax 0:66300c77c6e9 132 The function returns immediately and calls the callback on completion or error
tax 0:66300c77c6e9 133 */
tax 0:66300c77c6e9 134 HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, void (*pMethod)(HTTPResult)); //Non blocking
tax 0:66300c77c6e9 135
tax 0:66300c77c6e9 136 ///Executes a POST Request (non blocking)
tax 0:66300c77c6e9 137 /**
tax 0:66300c77c6e9 138 Executes a POST request on the URI uri
tax 0:66300c77c6e9 139 @param uri : URI on which to execute the request
tax 0:66300c77c6e9 140 @param dataOut : a HTTPData instance that contains the data that will be posted
tax 0:66300c77c6e9 141 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
tax 0:66300c77c6e9 142 @param pItem : instance of class on which to execute the callback method
tax 0:66300c77c6e9 143 @param pMethod : callback method
tax 0:66300c77c6e9 144 The function returns immediately and calls the callback on completion or error
tax 0:66300c77c6e9 145 */
tax 0:66300c77c6e9 146 template<class T>
tax 0:66300c77c6e9 147 HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking
tax 0:66300c77c6e9 148 {
tax 0:66300c77c6e9 149 setOnResult(pItem, pMethod);
tax 0:66300c77c6e9 150 doPost(uri, dataOut, pDataIn);
tax 0:66300c77c6e9 151 return HTTP_PROCESSING;
tax 0:66300c77c6e9 152 }
tax 0:66300c77c6e9 153
tax 0:66300c77c6e9 154 ///Executes a GET Request (non blocking)
tax 0:66300c77c6e9 155 /**
tax 0:66300c77c6e9 156 Executes a GET request on the URI uri
tax 0:66300c77c6e9 157 @param uri : URI on which to execute the request
tax 0:66300c77c6e9 158 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
tax 0:66300c77c6e9 159 The function returns immediately and calls the previously set callback on completion or error
tax 0:66300c77c6e9 160 */
tax 0:66300c77c6e9 161 void doGet(const char* uri, HTTPData* pDataIn);
tax 0:66300c77c6e9 162
tax 0:66300c77c6e9 163 ///Executes a POST Request (non blocking)
tax 0:66300c77c6e9 164 /**
tax 0:66300c77c6e9 165 Executes a POST request on the URI uri
tax 0:66300c77c6e9 166 @param uri : URI on which to execute the request
tax 0:66300c77c6e9 167 @param dataOut : a HTTPData instance that contains the data that will be posted
tax 0:66300c77c6e9 168 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
tax 0:66300c77c6e9 169 @param pMethod : callback function
tax 0:66300c77c6e9 170 The function returns immediately and calls the previously set callback on completion or error
tax 0:66300c77c6e9 171 */
tax 0:66300c77c6e9 172 void doPost(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn);
tax 0:66300c77c6e9 173
tax 0:66300c77c6e9 174 ///Setups the result callback
tax 0:66300c77c6e9 175 /**
tax 0:66300c77c6e9 176 @param pMethod : callback function
tax 0:66300c77c6e9 177 */
tax 0:66300c77c6e9 178 void setOnResult( void (*pMethod)(HTTPResult) );
tax 0:66300c77c6e9 179
tax 0:66300c77c6e9 180 ///Setups the result callback
tax 0:66300c77c6e9 181 /**
tax 0:66300c77c6e9 182 @param pItem : instance of class on which to execute the callback method
tax 0:66300c77c6e9 183 @param pMethod : callback method
tax 0:66300c77c6e9 184 */
tax 0:66300c77c6e9 185 class CDummy;
tax 0:66300c77c6e9 186 template<class T>
tax 0:66300c77c6e9 187 void setOnResult( T* pItem, void (T::*pMethod)(HTTPResult) )
tax 0:66300c77c6e9 188 {
tax 0:66300c77c6e9 189 m_pCb = NULL;
tax 0:66300c77c6e9 190 m_pCbItem = (CDummy*) pItem;
tax 0:66300c77c6e9 191 m_pCbMeth = (void (CDummy::*)(HTTPResult)) pMethod;
tax 0:66300c77c6e9 192 }
tax 0:66300c77c6e9 193
tax 0:66300c77c6e9 194 ///Setups timeout
tax 0:66300c77c6e9 195 /**
tax 0:66300c77c6e9 196 @param ms : time of connection inactivity in ms after which the request should timeout
tax 0:66300c77c6e9 197 */
tax 0:66300c77c6e9 198 void setTimeout(int ms);
tax 0:66300c77c6e9 199
tax 0:66300c77c6e9 200 virtual void poll(); //Called by NetServices
tax 0:66300c77c6e9 201
tax 0:66300c77c6e9 202 ///Gets last request's HTTP response code
tax 0:66300c77c6e9 203 /**
tax 0:66300c77c6e9 204 @return The HTTP response code of the last request
tax 0:66300c77c6e9 205 */
tax 0:66300c77c6e9 206 int getHTTPResponseCode();
tax 0:66300c77c6e9 207
tax 0:66300c77c6e9 208 ///Sets a specific request header
tax 0:66300c77c6e9 209 void setRequestHeader(const string& header, const string& value);
tax 0:66300c77c6e9 210
tax 0:66300c77c6e9 211 ///Gets a response header
tax 0:66300c77c6e9 212 string& getResponseHeader(const string& header);
tax 0:66300c77c6e9 213
tax 0:66300c77c6e9 214 ///Clears request headers
tax 0:66300c77c6e9 215 void resetRequestHeaders();
tax 0:66300c77c6e9 216
tax 0:66300c77c6e9 217 protected:
tax 0:66300c77c6e9 218 void resetTimeout();
tax 0:66300c77c6e9 219
tax 0:66300c77c6e9 220 void init();
tax 0:66300c77c6e9 221 void close();
tax 0:66300c77c6e9 222
tax 0:66300c77c6e9 223 void setup(const char* uri, HTTPData* pDataOut, HTTPData* pDataIn); //Setup request, make DNS Req if necessary
tax 0:66300c77c6e9 224 void connect(); //Start Connection
tax 0:66300c77c6e9 225
tax 0:66300c77c6e9 226 int tryRead(); //Read data and try to feed output
tax 0:66300c77c6e9 227 void readData(); //Data has been read
tax 0:66300c77c6e9 228 void writeData(); //Data has been written & buf is free
tax 0:66300c77c6e9 229
tax 0:66300c77c6e9 230 void onTCPSocketEvent(TCPSocketEvent e);
tax 0:66300c77c6e9 231 void onDNSReply(DNSReply r);
tax 0:66300c77c6e9 232 void onResult(HTTPResult r); //Called when exchange completed or on failure
tax 0:66300c77c6e9 233 void onTimeout(); //Connection has timed out
tax 0:66300c77c6e9 234
tax 0:66300c77c6e9 235 private:
tax 0:66300c77c6e9 236 HTTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
tax 0:66300c77c6e9 237
tax 0:66300c77c6e9 238 bool readHeaders(); //Called first when receiving data
tax 0:66300c77c6e9 239 bool writeHeaders(); //Called to create req
tax 0:66300c77c6e9 240 int readLine(char* str, int maxLen, bool* pIncomplete = NULL);
tax 0:66300c77c6e9 241
tax 0:66300c77c6e9 242 enum HTTP_METH
tax 0:66300c77c6e9 243 {
tax 0:66300c77c6e9 244 HTTP_GET,
tax 0:66300c77c6e9 245 HTTP_POST,
tax 0:66300c77c6e9 246 HTTP_HEAD
tax 0:66300c77c6e9 247 };
tax 0:66300c77c6e9 248
tax 0:66300c77c6e9 249 HTTP_METH m_meth;
tax 0:66300c77c6e9 250
tax 0:66300c77c6e9 251 CDummy* m_pCbItem;
tax 0:66300c77c6e9 252 void (CDummy::*m_pCbMeth)(HTTPResult);
tax 0:66300c77c6e9 253
tax 0:66300c77c6e9 254 void (*m_pCb)(HTTPResult);
tax 0:66300c77c6e9 255
tax 0:66300c77c6e9 256 TCPSocket* m_pTCPSocket;
tax 0:66300c77c6e9 257 map<string, string> m_reqHeaders;
tax 0:66300c77c6e9 258 map<string, string> m_respHeaders;
tax 0:66300c77c6e9 259
tax 0:66300c77c6e9 260 Timer m_watchdog;
tax 0:66300c77c6e9 261 int m_timeout;
tax 0:66300c77c6e9 262
tax 0:66300c77c6e9 263 DNSRequest* m_pDnsReq;
tax 0:66300c77c6e9 264
tax 0:66300c77c6e9 265 Host m_server;
tax 0:66300c77c6e9 266 string m_path;
tax 0:66300c77c6e9 267
tax 0:66300c77c6e9 268 bool m_closed;
tax 0:66300c77c6e9 269
tax 0:66300c77c6e9 270 enum HTTPStep
tax 0:66300c77c6e9 271 {
tax 0:66300c77c6e9 272 // HTTP_INIT,
tax 0:66300c77c6e9 273 HTTP_WRITE_HEADERS,
tax 0:66300c77c6e9 274 HTTP_WRITE_DATA,
tax 0:66300c77c6e9 275 HTTP_READ_HEADERS,
tax 0:66300c77c6e9 276 HTTP_READ_DATA,
tax 0:66300c77c6e9 277 HTTP_READ_DATA_INCOMPLETE,
tax 0:66300c77c6e9 278 HTTP_DONE,
tax 0:66300c77c6e9 279 HTTP_CLOSED
tax 0:66300c77c6e9 280 };
tax 0:66300c77c6e9 281
tax 0:66300c77c6e9 282 HTTPStep m_state;
tax 0:66300c77c6e9 283
tax 0:66300c77c6e9 284 HTTPData* m_pDataOut;
tax 0:66300c77c6e9 285 HTTPData* m_pDataIn;
tax 0:66300c77c6e9 286
tax 0:66300c77c6e9 287 bool m_dataChunked; //Data is encoded as chunks
tax 0:66300c77c6e9 288 int m_dataPos; //Position in data
tax 0:66300c77c6e9 289 int m_dataLen; //Data length
tax 0:66300c77c6e9 290 char* m_buf;
tax 0:66300c77c6e9 291 char* m_pBufRemaining; //Remaining
tax 0:66300c77c6e9 292 int m_bufRemainingLen; //Data length in m_pBufRemaining
tax 0:66300c77c6e9 293
tax 0:66300c77c6e9 294 int m_httpResponseCode;
tax 0:66300c77c6e9 295
tax 0:66300c77c6e9 296 HTTPResult m_blockingResult; //Result if blocking mode
tax 0:66300c77c6e9 297
tax 0:66300c77c6e9 298 };
tax 0:66300c77c6e9 299
tax 0:66300c77c6e9 300 //Including data containers here for more convenience
tax 0:66300c77c6e9 301 #include "data/HTTPFile.h"
tax 0:66300c77c6e9 302 #include "data/HTTPStream.h"
tax 0:66300c77c6e9 303 #include "data/HTTPText.h"
tax 0:66300c77c6e9 304 #include "data/HTTPMap.h"
tax 0:66300c77c6e9 305
tax 0:66300c77c6e9 306 #endif