test

Dependencies:   BME280 SB1602E

Fork of MultiIoTBoardLib by Junichi Katsu

Committer:
jksoft
Date:
Fri Jul 28 02:23:25 2017 +0000
Revision:
0:bad9495b4215
First edition

Who changed what in which revision?

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