NetBurner 3.5.0
PDF Version
 
http_funcs.h
Go to the documentation of this file.
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
11#ifndef _HTTP_FUNCS_H
12#define _HTTP_FUNCS_H
13
14#include <nbrtos.h>
15#include <nettypes.h>
16#include <dns.h>
17#include <webclient/web_buffers.h>
18
62{
63public:
64 enum eProtocol_t {
65 eProto_None,
66 eProto_HTTP,
67 eProto_HTTPS,
68 eProto_SSH,
69 eProto_SMTP,
70 eProto_MQTT,
71 eProto_MQTTS,
72 eProto_WS,
73 eProto_WSS
74 };
75private:
76
77 char URIBuf[256];
78 const char *CachedProtoStr;
79 char *CachedHost;
80 char *CachedPath;
81 const char *UserAgent;
82 IPADDR CachedHostIp;
83 tick_t ValidUntil;
84 uint16_t CachedPort;
85 eProtocol_t CachedProto;
86 uint8_t len;
87
88 #ifdef WEB_CLIENT_SSL_SUPPORT
89 bool secure;
90 #endif
91
92 int advanceCompareState(int state);
93 bool equals(const char *pUrl);
94 bool equals(const ParsedURI &rhs);
95 static ParsedURI *getParsedURI(OS_FIFO *pFifo);
96 public:
106 void NewUri(const char *uri, uint16_t timeout = 20 * TICKS_PER_SECOND, bool skipLookup = false);
107
108
113
121 ParsedURI(const char *uri, uint16_t timeout = 20 * TICKS_PER_SECOND, bool skipLookup = false)
122 {
123 NewUri(uri, timeout);
124 };
125
126
133 bool valid() const
134 {
135 return ((ValidUntil == 0xFFFFFFFF)|| (ValidUntil &&(((uint32_t)((int32_t)ValidUntil - ((int32_t)TimeTick))) >= 0)))&& URIBuf[0];
136 };
137
138
144 const char *GetPath()
145 {
146 if(CachedPath) return CachedPath;
147 return "";
148 };
149
150
156 const char *GetHost()
157 {
158 return CachedHost;
159 };
160
161
168 {
169 if (valid())
170 return CachedHostIp;
171 else
172 return IPADDR4::NullIP();
173 };
174
175
181 uint16_t GetPort()
182 {
183 return CachedPort;
184 };
185
186 eProtocol_t GetProto()
187 {
188 return CachedProto;
189 }
190
191 #ifdef WEB_CLIENT_SSL_SUPPORT
198 bool IsSecure()
199 {
200 return secure;
201 }
202 #endif
203 void dump();
204
208 void Invalidate() { ValidUntil = 0; CachedHostIp.SetNull(); };
209 void Resolve(bool block = true, uint16_t timeout = 20 * TICKS_PER_SECOND);
210
211 //Get an FD from UDP rx and send out requests...
212 int start_fd_dns();
213 bool process_fd_dns(int fd);
214
215 ParsedURI & operator=(const char *rhs) { NewUri(rhs); return *this; }
216 ParsedURI & operator=(const ParsedURI &rhs);
217 bool operator==(const ParsedURI &rhs) { return equals(rhs); }
218 bool operator!=(const ParsedURI &rhs) { return !equals(rhs); }
219 void SetUserAgent(const char* ua){UserAgent=ua;}
220 const char * GetUserAgent() {return UserAgent; }
221};
222
223
229void SetHttpDiag(bool b);
230
231
232#ifndef WebErrorReporterFunc
233typedef void(WebErrorReporterFunc)(int ErrorState);
234#endif
235
236extern WebErrorReporterFunc *pWebErrorReporter;
237
238
239class ParsedJsonDataSet; // Forward declaration
240
241
253 const char *separator,
254 uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND,
255 uint32_t contentLength = 0);
256
267int DoMultipartStartPost(const char *pUrl,
268 const char *separator,
269 uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND,
270 uint32_t contentLength = 0);
271
281void DoMultipartItem(int tcpfd, const char *Disposition, const char *separator,const unsigned char * data, int len);
282
283
291void DoMultipartBoundary(int tcpfd, const char *Disposition, const char *separator);
292
293
304bool DoMultipartFinished(int tcpfd, const char *separator, buffer_object &result_buffer, uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
305
318 char * headers,
319 char * form_data,
320 buffer_object & result_buffer,
321 uint16_t TIMEOUT_WAIT);
322
334bool DoUrlEncodedFormPost( const char * pUrl,
335 char * headers,
336 char * form_data,
337 buffer_object & result_buffer,
338 uint16_t TIMEOUT_WAIT);
339
351bool DoJsonPost(const char *pUrl,
352 const char *Json_Data_To_Post,
353 buffer_object &result_buffer,
354 const char *AdditionalHeaders = NULL,
355 uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
356
369 const char *Json_Data_To_Post,
370 buffer_object &result_buffer,
371 const char *AdditionalHeaders = NULL,
372 uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
373
385bool DoJsonPost(const char *pUrl,
386 ParsedJsonDataSet &jsonout,
387 buffer_object &result_buffer,
388 const char *AdditionalHeaders,
389 uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
390
403 ParsedJsonDataSet &jsonout,
404 buffer_object &result_buffer,
405 const char *AdditionalHeaders,
406 uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
407
408// Do a JSON post using a JSON file template with function file variables call backs etc... like the webserver
420bool DoJsonPostHttpFile(const char *pUrl,
421 const char *FragmentName,
422 buffer_object &result_buffer,
423 const char *AdditionalHeaders,
424 uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
425
438 const char *FragmentName,
439 buffer_object &result_buffer,
440 const char *AdditionalHeaders,
441 uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
442
452bool DoGet(ParsedURI &TheUri, buffer_object &result_buffer, uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
453
464bool DoGetEx(ParsedURI &TheUri,const char * headers, buffer_object &result_buffer, uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
465
466
478bool DoGet(const char *pUrl, buffer_object &result_buffer, uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
479
492bool DoGetEx(const char *pUrl,const char * headers,buffer_object &result_buffer, uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
493
494
495
509int DoGet(ParsedURI &TheUri, unsigned char *result, int maxl, uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
510
525int DoGetEx(ParsedURI &TheUri, const char* headers,unsigned char *result, int maxl, uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
526
527
540int DoGet(const char *pUrl, unsigned char *result, int maxl, uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
541
555int DoGetEx(const char *pUrl,const char * headers,unsigned char *result, int maxl, uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
556
557
558
559//Turn on update diagnostics
560void SetHttpUpDiag(bool b);
561
562
571int DoGetUpdate(ParsedURI &TheUri, uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
572
581int DoGetUpdate(const char *pUrl, uint16_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
582
583
584
595int PopulateAuthHeader(const char * user, const char * password, char * buffer, int maxlen);
596
597/* Parse the uri and open tcpp to the correct host/port nothing is sent*/
598int StartConnection(ParsedURI &TheUri,TickTimeout & TT);
599
600inline int StartConnection(ParsedURI &TheUri,uint32_t timeout=TICKS_PER_SECOND*10)
601{
602TickTimeout tt(timeout);
603return StartConnection(TheUri,tt);
604}
605
606
607int WriteHttpRequestHeaders(int fd,
608 ParsedURI &uri,
609 const char * Action,
610 int content_len=0,
611 const char * content_type=0,
612 const char * extra_header=0);
613
614
615
616
617/* Returns an FD otherwise starts a transaction and sends headers...
618including the host: heder type.
619headers should be formatted without leading and trailing \r\n
620*/
621int StartTransaction(ParsedURI &TheUri, const char *headers = 0, const char *action = "GET", uint32_t TIMEOUT_WAIT = 10 * TICKS_PER_SECOND);
622int StartTransaction(const char * pUrl, const char * headers=0,const char * action="GET", uint32_t TIMEOUT_WAIT= 10 * TICKS_PER_SECOND);
623
624
625
630#endif
static IPADDR4 NullIP()
C++ static function for a null IP address.
Definition nettypes.h:346
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
void SetNull()
Set the IP address value of an IPADDR6 object to null.
Definition ipv6_addr.h:288
A class to create, read, and modify a JSON object.
Definition json_lexer.h:530
Parsed Uniform Resource Identifier Class (URI)
Definition http_funcs.h:62
ParsedURI()
Constructor to create a new blank URI object. For rare cases.
bool IsSecure()
Check the security state of the cached URI object. Requires WEB_CLIENT_SSL_SUPPORT be defined in pred...
Definition http_funcs.h:198
const char * GetHost()
Get the cached URI Host name.
Definition http_funcs.h:156
const char * GetPath()
Get the cached URI path.
Definition http_funcs.h:144
bool valid() const
Check to see if URI is valid.
Definition http_funcs.h:133
void Invalidate()
Set the state of the cached URI object to invalid.
Definition http_funcs.h:208
ParsedURI(const char *uri, uint16_t timeout=20 *TICKS_PER_SECOND, bool skipLookup=false)
Constructor to create a new URI object.
Definition http_funcs.h:121
void NewUri(const char *uri, uint16_t timeout=20 *TICKS_PER_SECOND, bool skipLookup=false)
Replace the existing (URI) object with new information.
uint16_t GetPort()
Get the cached URI network port number.
Definition http_funcs.h:181
IPADDR GetAddr()
Get the resolved host Address.
Definition http_funcs.h:167
TickTimeout objects are used to facilitate sequential function calls with timeout parameters that nee...
Definition nbrtos.h:157
NetBurner Domain Name Server Header File.
#define TICKS_PER_SECOND
System clock ticks per second.
Definition nbrtos/include/constants.h:41
int DoGetUpdate(ParsedURI &TheUri, uint16_t TIMEOUT_WAIT=10 *TICKS_PER_SECOND)
Execute a firmware update from the specified URI.
int DoMultipartStartPost(ParsedURI &TheUri, const char *separator, uint16_t TIMEOUT_WAIT=10 *TICKS_PER_SECOND, uint32_t contentLength=0)
Start a multipart HTTP post using a pre-parsed URI object.
void DoMultipartItem(int tcpfd, const char *Disposition, const char *separator, const unsigned char *data, int len)
Send a multipart item.
void DoMultipartBoundary(int tcpfd, const char *Disposition, const char *separator)
Send a multipart boundary.
bool DoJsonPostHttpFile(const char *pUrl, const char *FragmentName, buffer_object &result_buffer, const char *AdditionalHeaders, uint16_t TIMEOUT_WAIT=10 *TICKS_PER_SECOND)
Post a JSON file using HTTP and a URL string.
bool DoMultipartFinished(int tcpfd, const char *separator, buffer_object &result_buffer, uint16_t TIMEOUT_WAIT=10 *TICKS_PER_SECOND)
Send a multipart item.
bool DoGet(ParsedURI &TheUri, buffer_object &result_buffer, uint16_t TIMEOUT_WAIT=10 *TICKS_PER_SECOND)
bool DoGetEx(ParsedURI &TheUri, const char *headers, buffer_object &result_buffer, uint16_t TIMEOUT_WAIT=10 *TICKS_PER_SECOND)
bool DoJsonPost(const char *pUrl, const char *Json_Data_To_Post, buffer_object &result_buffer, const char *AdditionalHeaders=NULL, uint16_t TIMEOUT_WAIT=10 *TICKS_PER_SECOND)
Post a JSON file using a HTTP POST and a URL string and pointer to JSON data.
bool DoUrlEncodedFormPost(ParsedURI &TheUri, char *headers, char *form_data, buffer_object &result_buffer, uint16_t TIMEOUT_WAIT)
Post a JSON file using a HTTP Form Post and a a ParsedURI object.
void SetHttpDiag(bool b)
Enable/disable Web Client HTTP diagnostics to the console port.
int PopulateAuthHeader(const char *user, const char *password, char *buffer, int maxlen)
Fill in a username and password into a buffer for use as an extra header.
NetBurner Real-Time Operating System (NBRTOS) API.
NetBurner IPADDR4 Class. See the IPADDR4 Class page for complete documentation.
Definition nbrtos.h:907