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