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