HTTPClient test for IoT Workshop

Dependencies:   UbloxUSBModem mbed

Fork of UbloxModemHTTPClientTest by mbed official

Committer:
sam_grove
Date:
Mon Feb 03 14:35:03 2014 +0000
Revision:
6:8cf840145f92
Parent:
1:0112fc45285a
update to make CDMA default

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 1:0112fc45285a 1 /* HTTPClient.h */
bogdanm 1:0112fc45285a 2 /* Copyright (C) 2012 mbed.org, MIT License
bogdanm 1:0112fc45285a 3 *
bogdanm 1:0112fc45285a 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
bogdanm 1:0112fc45285a 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
bogdanm 1:0112fc45285a 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
bogdanm 1:0112fc45285a 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
bogdanm 1:0112fc45285a 8 * furnished to do so, subject to the following conditions:
bogdanm 1:0112fc45285a 9 *
bogdanm 1:0112fc45285a 10 * The above copyright notice and this permission notice shall be included in all copies or
bogdanm 1:0112fc45285a 11 * substantial portions of the Software.
bogdanm 1:0112fc45285a 12 *
bogdanm 1:0112fc45285a 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
bogdanm 1:0112fc45285a 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
bogdanm 1:0112fc45285a 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
bogdanm 1:0112fc45285a 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
bogdanm 1:0112fc45285a 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
bogdanm 1:0112fc45285a 18 */
bogdanm 1:0112fc45285a 19
bogdanm 1:0112fc45285a 20 /** \file
bogdanm 1:0112fc45285a 21 HTTP Client header file
bogdanm 1:0112fc45285a 22 */
bogdanm 1:0112fc45285a 23
bogdanm 1:0112fc45285a 24 #ifndef HTTP_CLIENT_H
bogdanm 1:0112fc45285a 25 #define HTTP_CLIENT_H
bogdanm 1:0112fc45285a 26
bogdanm 1:0112fc45285a 27 #include "TCPSocketConnection.h"
bogdanm 1:0112fc45285a 28
bogdanm 1:0112fc45285a 29 #define HTTP_CLIENT_DEFAULT_TIMEOUT 15000
bogdanm 1:0112fc45285a 30
bogdanm 1:0112fc45285a 31 class HTTPData;
bogdanm 1:0112fc45285a 32
bogdanm 1:0112fc45285a 33 #include "IHTTPData.h"
bogdanm 1:0112fc45285a 34 #include "mbed.h"
bogdanm 1:0112fc45285a 35
bogdanm 1:0112fc45285a 36 ///HTTP client results
bogdanm 1:0112fc45285a 37 enum HTTPResult
bogdanm 1:0112fc45285a 38 {
bogdanm 1:0112fc45285a 39 HTTP_PROCESSING, ///<Processing
bogdanm 1:0112fc45285a 40 HTTP_PARSE, ///<url Parse error
bogdanm 1:0112fc45285a 41 HTTP_DNS, ///<Could not resolve name
bogdanm 1:0112fc45285a 42 HTTP_PRTCL, ///<Protocol error
bogdanm 1:0112fc45285a 43 HTTP_NOTFOUND, ///<HTTP 404 Error
bogdanm 1:0112fc45285a 44 HTTP_REFUSED, ///<HTTP 403 Error
bogdanm 1:0112fc45285a 45 HTTP_ERROR, ///<HTTP xxx error
bogdanm 1:0112fc45285a 46 HTTP_TIMEOUT, ///<Connection timeout
bogdanm 1:0112fc45285a 47 HTTP_CONN, ///<Connection error
bogdanm 1:0112fc45285a 48 HTTP_CLOSED, ///<Connection was closed by remote host
bogdanm 1:0112fc45285a 49 HTTP_OK = 0, ///<Success
bogdanm 1:0112fc45285a 50 };
bogdanm 1:0112fc45285a 51
bogdanm 1:0112fc45285a 52 /**A simple HTTP Client
bogdanm 1:0112fc45285a 53 The HTTPClient is composed of:
bogdanm 1:0112fc45285a 54 - The actual client (HTTPClient)
bogdanm 1:0112fc45285a 55 - 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)
bogdanm 1:0112fc45285a 56 */
bogdanm 1:0112fc45285a 57 class HTTPClient
bogdanm 1:0112fc45285a 58 {
bogdanm 1:0112fc45285a 59 public:
bogdanm 1:0112fc45285a 60 ///Instantiate the HTTP client
bogdanm 1:0112fc45285a 61 HTTPClient();
bogdanm 1:0112fc45285a 62 ~HTTPClient();
bogdanm 1:0112fc45285a 63
bogdanm 1:0112fc45285a 64 #if 0 //TODO add header handlers
bogdanm 1:0112fc45285a 65 /**
bogdanm 1:0112fc45285a 66 Provides a basic authentification feature (Base64 encoded username and password)
bogdanm 1:0112fc45285a 67 Pass two NULL pointers to switch back to no authentication
bogdanm 1:0112fc45285a 68 @param user username to use for authentication, must remain valid durlng the whole HTTP session
bogdanm 1:0112fc45285a 69 @param user password to use for authentication, must remain valid durlng the whole HTTP session
bogdanm 1:0112fc45285a 70 */
bogdanm 1:0112fc45285a 71 void basicAuth(const char* user, const char* password); //Basic Authentification
bogdanm 1:0112fc45285a 72 #endif
bogdanm 1:0112fc45285a 73
bogdanm 1:0112fc45285a 74 //High Level setup functions
bogdanm 1:0112fc45285a 75 /** Execute a GET request on the URL
bogdanm 1:0112fc45285a 76 Blocks until completion
bogdanm 1:0112fc45285a 77 @param url : url on which to execute the request
bogdanm 1:0112fc45285a 78 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
bogdanm 1:0112fc45285a 79 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
bogdanm 1:0112fc45285a 80 @return 0 on success, HTTP error (<0) on failure
bogdanm 1:0112fc45285a 81 */
bogdanm 1:0112fc45285a 82 HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
bogdanm 1:0112fc45285a 83
bogdanm 1:0112fc45285a 84 /** Execute a GET request on the URL
bogdanm 1:0112fc45285a 85 Blocks until completion
bogdanm 1:0112fc45285a 86 This is a helper to directly get a piece of text from a HTTP result
bogdanm 1:0112fc45285a 87 @param url : url on which to execute the request
bogdanm 1:0112fc45285a 88 @param result : pointer to a char array in which the result will be stored
bogdanm 1:0112fc45285a 89 @param maxResultLen : length of the char array (including space for the NULL-terminating char)
bogdanm 1:0112fc45285a 90 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
bogdanm 1:0112fc45285a 91 @return 0 on success, HTTP error (<0) on failure
bogdanm 1:0112fc45285a 92 */
bogdanm 1:0112fc45285a 93 HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
bogdanm 1:0112fc45285a 94
bogdanm 1:0112fc45285a 95 /** Execute a POST request on the URL
bogdanm 1:0112fc45285a 96 Blocks until completion
bogdanm 1:0112fc45285a 97 @param url : url on which to execute the request
bogdanm 1:0112fc45285a 98 @param dataOut : a IHTTPDataOut instance that contains the data that will be posted
bogdanm 1:0112fc45285a 99 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
bogdanm 1:0112fc45285a 100 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
bogdanm 1:0112fc45285a 101 @return 0 on success, HTTP error (<0) on failure
bogdanm 1:0112fc45285a 102 */
bogdanm 1:0112fc45285a 103 HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
bogdanm 1:0112fc45285a 104
bogdanm 1:0112fc45285a 105 /** Execute a PUT request on the URL
bogdanm 1:0112fc45285a 106 Blocks until completion
bogdanm 1:0112fc45285a 107 @param url : url on which to execute the request
bogdanm 1:0112fc45285a 108 @param dataOut : a IHTTPDataOut instance that contains the data that will be put
bogdanm 1:0112fc45285a 109 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
bogdanm 1:0112fc45285a 110 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
bogdanm 1:0112fc45285a 111 @return 0 on success, HTTP error (<0) on failure
bogdanm 1:0112fc45285a 112 */
bogdanm 1:0112fc45285a 113 HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
bogdanm 1:0112fc45285a 114
bogdanm 1:0112fc45285a 115 /** Execute a DELETE request on the URL
bogdanm 1:0112fc45285a 116 Blocks until completion
bogdanm 1:0112fc45285a 117 @param url : url on which to execute the request
bogdanm 1:0112fc45285a 118 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
bogdanm 1:0112fc45285a 119 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
bogdanm 1:0112fc45285a 120 @return 0 on success, HTTP error (<0) on failure
bogdanm 1:0112fc45285a 121 */
bogdanm 1:0112fc45285a 122 HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
bogdanm 1:0112fc45285a 123
bogdanm 1:0112fc45285a 124 /** Get last request's HTTP response code
bogdanm 1:0112fc45285a 125 @return The HTTP response code of the last request
bogdanm 1:0112fc45285a 126 */
bogdanm 1:0112fc45285a 127 int getHTTPResponseCode();
bogdanm 1:0112fc45285a 128
bogdanm 1:0112fc45285a 129 private:
bogdanm 1:0112fc45285a 130 enum HTTP_METH
bogdanm 1:0112fc45285a 131 {
bogdanm 1:0112fc45285a 132 HTTP_GET,
bogdanm 1:0112fc45285a 133 HTTP_POST,
bogdanm 1:0112fc45285a 134 HTTP_PUT,
bogdanm 1:0112fc45285a 135 HTTP_DELETE,
bogdanm 1:0112fc45285a 136 HTTP_HEAD
bogdanm 1:0112fc45285a 137 };
bogdanm 1:0112fc45285a 138
bogdanm 1:0112fc45285a 139 HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request
bogdanm 1:0112fc45285a 140 HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure
bogdanm 1:0112fc45285a 141 HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure
bogdanm 1:0112fc45285a 142 HTTPResult parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen); //Parse URL
bogdanm 1:0112fc45285a 143
bogdanm 1:0112fc45285a 144 //Parameters
bogdanm 1:0112fc45285a 145 TCPSocketConnection m_sock;
bogdanm 1:0112fc45285a 146
bogdanm 1:0112fc45285a 147 int m_timeout;
bogdanm 1:0112fc45285a 148
bogdanm 1:0112fc45285a 149 const char* m_basicAuthUser;
bogdanm 1:0112fc45285a 150 const char* m_basicAuthPassword;
bogdanm 1:0112fc45285a 151 int m_httpResponseCode;
bogdanm 1:0112fc45285a 152
bogdanm 1:0112fc45285a 153 };
bogdanm 1:0112fc45285a 154
bogdanm 1:0112fc45285a 155 //Including data containers here for more convenience
bogdanm 1:0112fc45285a 156 #include "data/HTTPText.h"
bogdanm 1:0112fc45285a 157 #include "data/HTTPMap.h"
bogdanm 1:0112fc45285a 158
bogdanm 1:0112fc45285a 159 #endif