-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAsyncHTTPClientLight.h
More file actions
173 lines (132 loc) · 4.25 KB
/
Copy pathAsyncHTTPClientLight.h
File metadata and controls
173 lines (132 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#pragma once
#include <Arduino.h>
#include <vector>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <functional>
#define LINE_BUFFER_SIZE 768
#define PATH_BUFFER 512
#define CHUNK_WAITING -1
#define WORKBUFFER workBuffer
#define PATHBUFFER workBuffer
#define WORKBUFFER_SIZE 512
enum class HTTPEventType {
Response, // fine risposta
Error,
Timeout,
Overload,
Chunk, // dati chunked
Receiving,
Line // linea normale (non chunked)
};
enum ChunkState {
WAITING_SIZE,
READING_DATA,
SKIPPING_CRLF,
FINISHED
};
struct HTTPResponse {
int statusCode = -1;
uint32_t restime = 0;
char inprogressTitle[64] = {0};
char contentType[45] = {0};
int contentLength = 0;
bool isStream = false;
bool isChunked = false;
int expectedLength = CHUNK_WAITING;
char* ptr_workbuffer = nullptr; // per chunked e altro..
char msg_error[40] = {0}; // per messaggi di errore
};
//using UnifiedCallback = std::function<void(HTTPEventType, const char* data, size_t length)>;
using UnifiedCallback = std::function<void(HTTPEventType type, const HTTPResponse* response)>;
class AsyncHTTPClientLight {
public:
AsyncHTTPClientLight();
void setResponsePayload(char* buffer, size_t maxLen) {
responsePayloadBuffer = buffer;
responsePayloadMaxLen = maxLen;
newPtrOut = true;
}
const char* getResponsePayload() {
return responsePayloadBuffer;
}
void beginRequest(const String& url, const String& method = "GET", const String& payload = ""){
const char* p = (payload.length() > 0)? payload.c_str(): nullptr;
return beginRequest(url.c_str(), method.c_str(), p);
};
void beginRequest(const char* url, const char* method = "GET", const char* payload = nullptr);
int runSync(const String& url, const String& method = "GET", const String& payload = ""){
const char* p = (payload.length() > 0)? payload.c_str(): nullptr;
return runSync(url.c_str(), method.c_str(), p);
};
int runSync(const char* url, const char* method = "GET", const char* payload = "");
void addHeader(const String& key, const String& value);
void addTitle(const String& title);
void setTimeout(unsigned long ms);
void setDebug(bool enabled);
void setLogToFile(bool enabled);
void setMaxRetries(int retries);
void setmaxRedirects(int ndirect);
void onEvent(UnifiedCallback cb);
int getLastHTTPcode() const;
void poll();
void poll2();
bool isFinished();
bool finished;
private:
WiFiClient plainClient;
WiFiClientSecure secureClient;
WiFiClient* client;
HTTPResponse response;
char* responsePayloadBuffer = nullptr;
size_t responsePayloadMaxLen = 0;
bool newPtrOut = false;
char host[40];
char workBuffer[WORKBUFFER_SIZE];
int port;
bool useSSL;
String method = " ";
char* ptr_Inpayload = nullptr;
std::vector<std::pair<String, String>> pendingHeaders;
std::vector<std::pair<String, String>> inprogressHeaders;
enum State { IDLE,
CONNECTING,
SENDING,
RECEIVING };
State state;
char lineBuffer[LINE_BUFFER_SIZE];
size_t bufIndex = 0;
String logPrefix;
char pendingTitle[64] = {0}; // titolo massimo 63 caratteri + terminatore
bool _isSyncMode = false;
unsigned long timeoutMs;
unsigned long lastActivity;
bool payloadAllocated = false;
bool headersParsed;
int chunkState = WAITING_SIZE;
int requestCounter;
int maxRetries;
int retryCount;
int redirectCount;
int maxRedirects = 1;
bool debugEnabled;
bool logToFile;
UnifiedCallback unifiedCallback;
std::function<void(int)> retryCallback;
void reset();
bool parseURL(const char* url);
void log(const String& msg);
void checktimeout();
void connecting();
void sending();
void receiving();
int trimmer(char* buftrim, int dadove = 0);
int search_strbuf(const char* buffer, char* str_cmp, int fromwhere = 0);
void releasePayload();
void parseHeaders();
int readUntilTerminator(Stream* client, char* buffer, size_t maxLen, char terminator, unsigned long timeoutMs, bool delCR = true);
bool readChunked();
int readStream(char* buffer, int lenbuffer, int ndati );
void triggerEvent(HTTPEventType type, const char* data);
void triggerEvent(HTTPEventType type, const String& message);
};