-
Notifications
You must be signed in to change notification settings - Fork 12
/
HTTPPrinter.cpp
146 lines (101 loc) · 3.07 KB
/
HTTPPrinter.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
#include "HTTPPrinter.h"
HTTPPrinter::HTTPPrinter() {
_bufferCount = 0;
_buffer = NULL;
_size = 0;
_headerSent = false;
_CountMode = false;
}
void HTTPPrinter::Begin(WiFiClient &c) {
_bufferCount = 0;
_buffer = (uint8_t *)malloc(HTTPPrinterSize);
_client = c;
_size = 0;
_headerSent = false;
_CountMode = false;
}
void HTTPPrinter::Begin(WiFiClient &c, size_t memory) {
_bufferCount = 0;
_buffer = (uint8_t *)malloc(memory);
_client = c;
_size = 0;
_headerSent = false;
_CountMode = false;
}
void HTTPPrinter::End() {
free(_buffer);
}
void HTTPPrinter::Setsize(size_t s) {
_size = s;
}
size_t HTTPPrinter::SetCountMode(bool mode) {
if (mode) {
_CountMode = true;
_size = 0;
} else {
_CountMode = false;
_sizeTotal = _size;
}
return _size;
}
size_t HTTPPrinter::GetSize() {
return _sizeTotal;
}
size_t HTTPPrinter::write(uint8_t data) {
if (_buffer == NULL) return 0;
switch(_CountMode) {
case true:
_size++;
break;
case false:
_buffer[_bufferCount++] = data; // put byte into buffer
if(_bufferCount == HTTPPrinterSize || _size == _bufferCount ) { // send it if full, or remaining bytes = buffer
if (!_headerSent) Send_Header(200, "text/json");
_client.write(_buffer + 0, _bufferCount);
_size -= _bufferCount; // keep track of remaining bytes..
_bufferCount = 0; // reset the buffer to begining..
}
break;
} // end of switch
return true;
} // end of write
void HTTPPrinter::SetHeader(int code, const char* content) {
if (code < 0) code = 0;
_code = code;
_headerContent = content;
}
size_t HTTPPrinter::Send(WiFiClient client, int code, const char* content, printer_callback fn ){
Begin(client);
SetHeader(code, content);
SetCountMode(true);
fn();
size_t size = SetCountMode(false);
fn();
End();
client.stop();
while(client.connected()) yield();
return size;
}
void HTTPPrinter::Send_Header () {
Send_Header(_code, _headerContent);
SetCountMode(false);
}
size_t HTTPPrinter::Send_Buffer(int code, const char* content) {
_size = _bufferCount;
Send_Header(code, content);
_client.write(_buffer + 0, _size);
//Serial.write(_buffer+0, _size);
return _size;
End();
}
void HTTPPrinter::Send_Header (int code, const char * content) {
if (_headerSent) return;
uint8_t *headerBuff = (uint8_t*)malloc(128);
sprintf((char*)headerBuff, "HTTP/1.1 %u OK\r\nContent-Type: %s\r\nContent-Length: %u\r\nConnection: close\r\nAccess-Control-Allow-Origin: *\r\n\r\n", code, content,_size);
// Serial.println();
// Serial.println((char*)headerBuff);
size_t headerLen = strlen((const char*)headerBuff);
_client.write((const uint8_t*)headerBuff, headerLen);
free(headerBuff);
_headerSent = true;
}