forked from FKainka/NanoESP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NanoESP_HTTP.cpp
131 lines (90 loc) · 3.39 KB
/
NanoESP_HTTP.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
/*
Author: Fabian Kainka
*/
#include "Arduino.h"
#include "NanoESP_HTTP.h"
NanoESP_HTTP::NanoESP_HTTP(NanoESP& nanoesp):m_nanoesp(&nanoesp){
}
//-------------------------------------------------New RESTapi & Webserver Functions-----------------------------------------
bool NanoESP_HTTP::sendFromFlash(int client, const char *website, int len) {
if (m_nanoesp->sendCom(at+cip+"SEND=" + String(client) + "," + String(len), ">")) {
for (int i = 0; i <= len; i++)
{
m_nanoesp->print(char(pgm_read_byte_near(website + i)));
}
}
else {
return false;
}
return m_nanoesp->find("OK\r\n");
}
bool NanoESP_HTTP::recvRequest(int &id, String &method, String &ressource, String ¶meter) {
if (m_nanoesp->available()) {
if (m_nanoesp->findUntil("+IPD,", "\n") ) {
id = m_nanoesp->parseInt();
//debug(String(id));
if (id >= 0) {
m_nanoesp->findUntil(":", "\n"); //+IPD, 0, 407: GET / HTTP / 1.1
method = m_nanoesp->readStringUntil(' ');
String url = m_nanoesp->readStringUntil(' ');
m_nanoesp->find("\r\n\r\n");
int x = url.indexOf('?'); //192.168.178.41/?rgb=%2300ffff
parameter = url.substring(x);
ressource = url.substring(0, x);
if (method == "POST") {
//if (m_nanoesp->find("\r\n\r\n")) {
m_nanoesp->setTimeout(300);
parameter = m_nanoesp->readString(); //Until(' '); //+IPD,0,13:rgb=%230000ff
m_nanoesp->setTimeout(1000);
}
}
return true;
}
}
return false;
}
bool NanoESP_HTTP::recvHTTP(int id, int len, String &method, String &ressource, String ¶meter) {
if (len>=11){
if( m_nanoesp->findUntil(":", "\n")){ //+IPD, 0, 407: GET / HTTP / 1.1
method = m_nanoesp->readStringUntil(' ');
String url = m_nanoesp->readStringUntil(' ');
m_nanoesp->find("\r\n\r\n");
int x = url.indexOf('?'); //192.168.178.41/?rgb=%2300ffff
parameter = url.substring(x);
ressource = url.substring(0, x);
if (method == "POST") {
//if (m_nanoesp->find("\r\n\r\n")) {
m_nanoesp->setTimeout(300);
parameter = m_nanoesp->readString(); //Until(' '); //+IPD,0,13:rgb=%230000ff
m_nanoesp->setTimeout(1000);
}
return true;
}
}
return false;
}
bool NanoESP_HTTP::sendStreamHeader(int connectionId) {
this->sendFromFlash(connectionId, serverStreamResponse, sizeof(serverStreamResponse) - 1);
}
bool NanoESP_HTTP::sendRequest(int id, char method[5], String address, String parameter) {
String host = address.substring(0, address.indexOf('/'));
String ressource = address.substring(address.indexOf('/'));
if ((method == "GET") && (parameter != "")) {
ressource = ressource + "?" + parameter;
}
String getRequest = String(method) + " " + ressource + " HTTP/1.1\r\nHost:" + host + "\r\n";
if ((method == "POST") && (parameter != "")) {
getRequest = getRequest + "Content-Length: " + String(parameter.length()) + "\r\n\r\n";
getRequest = getRequest + parameter;
}
else {
getRequest = getRequest + "\r\n";
}
if (m_nanoesp->newConnection(id, TCP, host, 80)) {
return m_nanoesp->sendData(id, getRequest);
}
return false;
}
bool NanoESP_HTTP::sendRequest(int id, char method[5], String address) {
return sendRequest( id, method, address, "");
}