/
ClientHandler.cpp
157 lines (137 loc) · 3.6 KB
/
ClientHandler.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
/*
ClientHandler.cpp - Library for create Http Requests.
Created by Joan Ortega, jomaora@gmail.com Feb. 15, 2016.
Released into the public domain.
*/
#include "Arduino.h"
#include <Ethernet.h>
#include "ClientHandler.h"
bool ClientHandler::init() {
_requestReady = false;
String result = "";
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
int beginOK = Ethernet.begin(_mac);
if (beginOK == 0) {
result = "Failed to configure Ethernet using DHCP";
Serial.println(result);
Ethernet.begin(_mac, _ip);
}
delay(1000);
result = "Ethernet Shield connected";
Serial.println(result);
return (beginOK != 0);
}
bool ClientHandler::initClientHandler(String httpMethod, char *server, int serverPort, String path) {
_server = server;
_path = path;
resetRequest();
int connected = 0;
while (!connected) {
Serial.println("Trying connection");
if (_client->connect(_server, serverPort)) {
Serial.println("connected");
connected = 1;
}
else {
Serial.println("connection failed");
_requestReady = false;
return _requestReady;
}
}
String requestLine = httpMethod + " ";
requestLine.concat(path);
requestLine.concat(" HTTP/1.1");
String hostLine = "Host: " + String(server);
_client->println(requestLine);
_client->println(hostLine);
_requestReady = true;
return _requestReady;
}
void ClientHandler::addHeader(String header) {
if (_requestReady) {
_client->println(header);
}
}
const char* ClientHandler::send() {
if (_requestReady) {
_client->println("Connection: close");
_client->println();
buildResponse();
if (_activeLogs) {
Serial.println("disconnecting.");
Serial.println("");
Serial.println("status");
Serial.println(_status);
Serial.println("");
Serial.print("headers");
Serial.print(_responseHeaders);
Serial.println("");
Serial.print("body");
Serial.println(_body);
}
_client->stop();
return _body.c_str();
}
else {
return "Request is not ready to be sent";
}
}
void ClientHandler::buildResponse() {
// values needed before reading response
_returnCharCount = 0; _readingStatus = true; _readingheaders = false; _readingBody = false;
while(!_receivedResponse) {
if (_client->available()) {
char c = _client->read();
readingResponse(c);
}
if (!_client->available() && !_client->connected()) {
_receivedResponse = true;
}
}
}
void ClientHandler::readingResponse(char c) {
if (c == '\n' || c == '\r') {
_returnCharCount++;
} else {
_returnCharCount = 0;
}
if (_returnCharCount == 2) {
_readingStatus = false; _readingheaders = true; _readingBody = false;
}
if (_returnCharCount == 4) {
_readingStatus = false; _readingheaders = false; _readingBody = true;
}
if (_readingStatus) {
_status.concat(c);
}
if (_readingBody && c != '\n') {
_body.concat(c);
}
if (_readingheaders) {
_responseHeaders.concat(c);
}
}
int ClientHandler::getResponseStatusCode() {
if (_receivedResponse) {
int blankSpaceIndex = _status.indexOf(" ") + 1;
String statusCode = _status.substring(blankSpaceIndex, blankSpaceIndex + 3);
return statusCode.toInt();
} else {
return 0;
}
}
const char* ClientHandler::getResponseBody() {
if (_receivedResponse) {
return _body.c_str();
} else {
return "";
}
}
void ClientHandler::resetRequest() {
_receivedResponse = false; // setting the flag that indicates the response recpetion to false;
_body = "";
_status = "";
_responseHeaders = "";
}