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