iSDIO Library for TOSHIBA FlashAir. include HTTP or HTTPS Client.

Dependencies:   SDFileSystem

Dependents:   FlashAir_Twitter Neon_F303K8_04

Fork of HTTPClient by Donatien Garnier

Committer:
ban4jp
Date:
Mon Dec 15 12:23:22 2014 +0000
Revision:
20:51abf34bcc06
Parent:
19:3b1625dbd7e9
Initial commit.

Who changed what in which revision?

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