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