-
Notifications
You must be signed in to change notification settings - Fork 4
/
Response.cpp
169 lines (142 loc) · 3.92 KB
/
Response.cpp
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
#include "Response.h"
using namespace OTF;
#if defined(ARDUINO)
void Response::writeStatus(uint16_t statusCode, const String &statusMessage) {
if (responseStatus > CREATED) {
valid = false;
return;
}
responseStatus = STATUS_WRITTEN;
bprintf(F("HTTP/1.1 %d %s\r\n"), statusCode, statusMessage.c_str());
}
void Response::writeStatus(uint16_t statusCode, const __FlashStringHelper *const statusMessage) {
if (responseStatus > CREATED) {
valid = false;
return;
}
responseStatus = STATUS_WRITTEN;
bprintf(F("HTTP/1.1 %d %s\r\n"), statusCode, statusMessage);
}
#else
void Response::writeStatus(uint16_t statusCode, const char *statusMessage) {
if (responseStatus > CREATED) {
valid = false;
return;
}
responseStatus = STATUS_WRITTEN;
bprintf(F("HTTP/1.1 %d %s\r\n"), statusCode, statusMessage);
}
#endif
void Response::writeStatus(uint16_t statusCode) {
if (responseStatus > CREATED) {
valid = false;
return;
}
responseStatus = STATUS_WRITTEN;
writeStatus(statusCode, F("No message"));
}
/*void Response::writeHeader(char *const name, char *const value) {
if (responseStatus < STATUS_WRITTEN || responseStatus > HEADERS_WRITTEN) {
valid = false;
return;
}
responseStatus = HEADERS_WRITTEN;
bprintf(F("%s: %s\r\n"), name, value);
}
void Response::writeHeader(const __FlashStringHelper *const name, char *const value) {
if (responseStatus < STATUS_WRITTEN || responseStatus > HEADERS_WRITTEN) {
valid = false;
return;
}
responseStatus = HEADERS_WRITTEN;
bprintf(F("%s: %s\r\n"), name, value);
}*/
#if defined(ARDUINO)
void Response::writeHeader(const __FlashStringHelper *const name, int value) {
if (responseStatus < STATUS_WRITTEN || responseStatus > HEADERS_WRITTEN) {
valid = false;
return;
}
responseStatus = HEADERS_WRITTEN;
bprintf(F("%s: %d\r\n"), name, value);
}
void Response::writeHeader(const __FlashStringHelper *const name, const __FlashStringHelper *const value) {
if (responseStatus < STATUS_WRITTEN || responseStatus > HEADERS_WRITTEN) {
valid = false;
return;
}
responseStatus = HEADERS_WRITTEN;
bprintf(F("%s: %s\r\n"), name, value);
}
#else
void Response::writeHeader(const char *name, int value) {
if (responseStatus < STATUS_WRITTEN || responseStatus > HEADERS_WRITTEN) {
valid = false;
return;
}
responseStatus = HEADERS_WRITTEN;
bprintf(F("%s: %d\r\n"), name, value);
}
void Response::writeHeader(const char *name, const char *value) {
if (responseStatus < STATUS_WRITTEN || responseStatus > HEADERS_WRITTEN) {
valid = false;
return;
}
responseStatus = HEADERS_WRITTEN;
bprintf(F("%s: %s\r\n"), name, value);
}
#endif
void Response::writeBodyChunk(const char *const format, ...) {
if (responseStatus < STATUS_WRITTEN) {
valid = false;
return;
}
if (responseStatus != BODY_WRITTEN) {
bprintf("\r\n");
responseStatus = BODY_WRITTEN;
}
va_list args;
va_start(args, format);
bprintf(format, args);
va_end(args);
}
#if defined(ARDUINO)
void Response::writeBodyChunk(const __FlashStringHelper *const format, ...) {
if (responseStatus < STATUS_WRITTEN) {
valid = false;
return;
}
if (responseStatus != BODY_WRITTEN) {
bprintf((char *) "\r\n");
responseStatus = BODY_WRITTEN;
}
va_list args;
va_start(args, format);
bprintf(format, args);
va_end(args);
}
#endif
void Response::writeBodyData(const char *data, size_t length) {
if (responseStatus < STATUS_WRITTEN) {
valid = false;
return;
}
if (responseStatus != BODY_WRITTEN) {
bprintf((char *) "\r\n");
responseStatus = BODY_WRITTEN;
}
write(data, length);
}
#if defined(ARDUINO)
void Response::writeBodyData(const __FlashStringHelper *const data, size_t length) {
if (responseStatus < STATUS_WRITTEN) {
valid = false;
return;
}
if (responseStatus != BODY_WRITTEN) {
bprintf((char *) "\r\n");
responseStatus = BODY_WRITTEN;
}
write_P(data, length);
}
#endif