httpclient

Dependents:   Lab_6

Committer:
rr387
Date:
Thu Dec 30 15:21:34 2021 +0000
Revision:
0:a988a72f184b
retes

Who changed what in which revision?

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