u-blox modem HTTP client test
Dependencies: UbloxUSBModem mbed
Revision 6:d17e425e9425, committed 2014-06-03
- Comitter:
- mbed_official
- Date:
- Tue Jun 03 11:30:51 2014 +0100
- Parent:
- 5:dcad6eda763e
- Commit message:
- Synchronized with git revision bcacbb9fbf3432829227430830cca4315b57c1b9
Full URL: https://github.com/mbedmicro/mbed/commit/bcacbb9fbf3432829227430830cca4315b57c1b9/
Changed in this revision
--- a/HTTPClient/HTTPClient.cpp Mon Nov 04 09:00:29 2013 +0000 +++ b/HTTPClient/HTTPClient.cpp Tue Jun 03 11:30:51 2014 +0100 @@ -21,15 +21,15 @@ #if 0 //Enable debug #include <cstdio> -#define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__); -#define WARN(x, ...) std::printf("[HTTPClient : WARN]"x"\r\n", ##__VA_ARGS__); -#define ERR(x, ...) std::printf("[HTTPClient : ERR]"x"\r\n", ##__VA_ARGS__); +#define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[HTTPClient : WARN]"x"\r\n", ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[HTTPClient : ERR]"x"\r\n", ##__VA_ARGS__); #else //Disable debug -#define DBG(x, ...) +#define DBG(x, ...) #define WARN(x, ...) -#define ERR(x, ...) +#define ERR(x, ...) #endif @@ -114,10 +114,10 @@ } while(0) HTTPResult HTTPClient::connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request -{ +{ m_httpResponseCode = 0; //Invalidate code m_timeout = timeout; - + pDataIn->writeReset(); if( pDataOut ) { @@ -194,14 +194,14 @@ CHECK_CONN_ERR(ret); } } - + //Close headers DBG("Headers sent"); ret = send("\r\n"); CHECK_CONN_ERR(ret); size_t trfLen; - + //Send data (if available) if( pDataOut != NULL ) { @@ -249,7 +249,7 @@ } } - + //Receive response DBG("Receiving response"); ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes @@ -276,7 +276,7 @@ if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 300) ) { - //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers + //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers WARN("Response code %d", m_httpResponseCode); PRTCL_ERR(); } @@ -484,13 +484,13 @@ { DBG("Trying to read between %d and %d bytes", minLen, maxLen); size_t readLen = 0; - + if(!m_sock.is_connected()) { WARN("Connection was closed by server"); - return HTTP_CLOSED; //Connection was closed by server + return HTTP_CLOSED; //Connection was closed by server } - + int ret; while(readLen < maxLen) { @@ -506,7 +506,7 @@ m_sock.set_blocking(false, 0); ret = m_sock.receive(buf + readLen, maxLen - readLen); } - + if( ret > 0) { readLen += ret; @@ -525,10 +525,10 @@ } else { - break; + break; } } - + if(!m_sock.is_connected()) { break; @@ -547,13 +547,13 @@ } DBG("Trying to write %d bytes", len); size_t writtenLen = 0; - + if(!m_sock.is_connected()) { WARN("Connection was closed by server"); - return HTTP_CLOSED; //Connection was closed by server + return HTTP_CLOSED; //Connection was closed by server } - + m_sock.set_blocking(false, m_timeout); int ret = m_sock.send_all(buf, len); if(ret > 0) @@ -570,7 +570,7 @@ ERR("Connection error (send returned %d)", ret); return HTTP_CONN; } - + DBG("Written %d bytes", writtenLen); return HTTP_OK; }
--- a/HTTPClient/HTTPClient.h Mon Nov 04 09:00:29 2013 +0000 +++ b/HTTPClient/HTTPClient.h Tue Jun 03 11:30:51 2014 +0100 @@ -60,7 +60,7 @@ ///Instantiate the HTTP client HTTPClient(); ~HTTPClient(); - + #if 0 //TODO add header handlers /** Provides a basic authentification feature (Base64 encoded username and password) @@ -70,7 +70,7 @@ */ void basicAuth(const char* user, const char* password); //Basic Authentification #endif - + //High Level setup functions /** Execute a GET request on the URL Blocks until completion @@ -80,7 +80,7 @@ @return 0 on success, HTTP error (<0) on failure */ HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking - + /** Execute a GET request on the URL Blocks until completion This is a helper to directly get a piece of text from a HTTP result @@ -101,7 +101,7 @@ @return 0 on success, HTTP error (<0) on failure */ HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking - + /** Execute a PUT request on the URL Blocks until completion @param url : url on which to execute the request @@ -111,7 +111,7 @@ @return 0 on success, HTTP error (<0) on failure */ HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking - + /** Execute a DELETE request on the URL Blocks until completion @param url : url on which to execute the request @@ -120,12 +120,12 @@ @return 0 on success, HTTP error (<0) on failure */ HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking - + /** Get last request's HTTP response code @return The HTTP response code of the last request */ int getHTTPResponseCode(); - + private: enum HTTP_METH { @@ -143,7 +143,7 @@ //Parameters TCPSocketConnection m_sock; - + int m_timeout; const char* m_basicAuthUser;
--- a/HTTPClient/IHTTPData.h Mon Nov 04 09:00:29 2013 +0000 +++ b/HTTPClient/IHTTPData.h Tue Jun 03 11:30:51 2014 +0100 @@ -29,8 +29,8 @@ { protected: friend class HTTPClient; - - /** Reset stream to its beginning + + /** Reset stream to its beginning * Called by the HTTPClient on each new request */ virtual void readReset() = 0; @@ -41,17 +41,17 @@ * @param pReadLen Pointer to the variable on which the actual copied data length will be stored */ virtual int read(char* buf, size_t len, size_t* pReadLen) = 0; - + /** Get MIME type * @param type Internet media type from Content-Type header */ virtual int getDataType(char* type, size_t maxTypeLen) = 0; //Internet media type for Content-Type header - + /** Determine whether the HTTP client should chunk the data * Used for Transfer-Encoding header */ virtual bool getIsChunked() = 0; - + /** If the data is not chunked, get its size * Used for Content-Length header */ @@ -65,7 +65,7 @@ protected: friend class HTTPClient; - /** Reset stream to its beginning + /** Reset stream to its beginning * Called by the HTTPClient on each new request */ virtual void writeReset() = 0; @@ -85,7 +85,7 @@ * Recovered from Transfer-Encoding header */ virtual void setIsChunked(bool chunked) = 0; - + /** If the data is not chunked, set its size * From Content-Length header */
--- a/HTTPClient/data/HTTPText.h Mon Nov 04 09:00:29 2013 +0000 +++ b/HTTPClient/data/HTTPText.h Tue Jun 03 11:30:51 2014 +0100 @@ -42,7 +42,7 @@ protected: //IHTTPDataIn virtual void readReset(); - + virtual int read(char* buf, size_t len, size_t* pReadLen); virtual int getDataType(char* type, size_t maxTypeLen); //Internet media type for Content-Type header @@ -53,7 +53,7 @@ //IHTTPDataOut virtual void writeReset(); - + virtual int write(const char* buf, size_t len); virtual void setDataType(const char* type); //Internet media type from Content-Type header
--- a/httptest.cpp Mon Nov 04 09:00:29 2013 +0000 +++ b/httptest.cpp Tue Jun 03 11:30:51 2014 +0100 @@ -4,9 +4,9 @@ #include "httptest.h" int httptest(CellularModem& modem, const char* apn, const char* username, const char* password) -{ +{ printf("Connecting...\n"); - + HTTPClient http; char str[512]; @@ -18,7 +18,7 @@ printf("Could not connect\n"); return false; } - + //GET data printf("Trying to fetch page...\n"); ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); @@ -33,7 +33,7 @@ modem.disconnect(); return false; } - + //POST data HTTPMap map; HTTPText text(str, 512); @@ -52,7 +52,7 @@ modem.disconnect(); return false; } - + modem.disconnect(); return true; }