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