-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathESP8266_multipart.cpp
95 lines (91 loc) · 2.88 KB
/
ESP8266_multipart.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
#include "ESP8266_multipart.h"
ESP8266_multipart::ESP8266_multipart() {
ESP8266_multipart::init("127.0.0.1", 80);
}
ESP8266_multipart::ESP8266_multipart(String host) {
ESP8266_multipart::init(host.c_str(), 80);
}
ESP8266_multipart::ESP8266_multipart(const char* host) {
ESP8266_multipart::init(host, 80);
}
void ESP8266_multipart::init(const char* host, int port) {
this->host = host;
this->port = port;
this->binary = false;
}
int ESP8266_multipart::sendFile(String path, File file) {
return sendFile(path.c_str(), file);
}
void ESP8266_multipart::setPort(int port) {
this->port = port;
}
void ESP8266_multipart::setHost(String host) {
ESP8266_multipart::setHost(host.c_str());
}
void ESP8266_multipart::setHost(const char* host) {
this->host = host;
}
void ESP8266_multipart::setBinary(bool binary) {
this->binary = binary;
}
int ESP8266_multipart::sendFile(const char* path, File file) {
WiFiClient client;
if(client.connect(this->host, this->port)) {
client.print(F("POST ")); client.print(path); client.print(F(" HTTP/1.1\r\n"));
client.print(F("Host: ")); client.print(this->host); client.print(F("\r\n"));
client.print(F("User-Agent: esp8266_multipart/1.0\r\n"));
client.print(F("Accept: */*\r\n"));
client.print(F("Connection: close\r\n"));
client.print(F("Content-Type: multipart/form-data; boundary=X-ESP8266_MULTIPART\r\n"));
String tmpFront = "--X-ESP8266_MULTIPART\r\nContent-Disposition: form-data; name=\"";
tmpFront += file.name();
tmpFront += "\"\r\n";
if(this->binary) {
tmpFront += "Content-Type: application/octet-stream\r\n";
}
tmpFront += "\r\n";
String tmpBack = "\r\n--X-ESP8266_MULTIPART--\r\n\r\n";
int dataLength = tmpFront.length() + tmpBack.length() + file.size();
client.print(F("Content-Length: ")); client.print(dataLength); client.print(F("\r\n\r\n"));
client.print(tmpFront);
int tempPos = file.position();
file.seek(0, SeekSet);
if(this->binary) {
uint8_t dataBuffer[128];
while(file.available()) {
while(file.position() < file.size()) {
size_t len = file.readBytes((char *)dataBuffer, sizeof(dataBuffer));
client.write(dataBuffer, len);
}
}
} else {
while(file.available()) {
while(file.position() < file.size()) {
String dataBuffer = file.readStringUntil('\n');
client.print(dataBuffer);
client.print("\n");
}
}
}
client.print(tmpBack);
file.seek(tempPos, SeekSet);
client.flush();
} else {
return 0;
}
String tmpRes = "";
while(client.connected()) {
while(client.available()) {
char c = client.read();
tmpRes += c;
}
}
int status = -1;
if(!client.connected()) {
int firstSpace = tmpRes.indexOf(" ");
tmpRes.remove(0, firstSpace +1);
status = tmpRes.substring(0,3).toInt();
client.stop();
}
return status;
}