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