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