Skip to content

Commit 38c08de

Browse files
Doug PowersDoug Powers
Doug Powers
authored and
Doug Powers
committed
improvements to http client and using const char buffers where appropriate
1 parent b330190 commit 38c08de

26 files changed

+126
-57
lines changed

ARPHandler.cpp

100755100644
+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ bool ARPHandler::sendARPRequest(uint8_t target_protocol_addr[4]){
7272
if (!etherBuffer->write8(5,4)) return false; // IPv4 addresses are 4 bytes
7373
if (!etherBuffer->writeNet16(6,ARP_REQUEST)) return false; // 1 is request
7474
if (!etherBuffer->write(8,sender_hardware_addr,6)) return false; //sender mac
75+
7576
if (!etherBuffer->write(14,ipAddress,4)) return false; //sender ip
7677

7778
//target hw address

Buffer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <string.h>
33
#include "Buffer.h"
44

5-
bool Buffer::write(uint16_t offset, char* d){
5+
bool Buffer::write(uint16_t offset, const char* d){
66
return this->write(offset,d,strlen(d));
77
}
88

Buffer.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Buffer {
88
public:
99
virtual uint16_t size() = 0;
1010

11-
virtual bool write(uint16_t offset, void* data, uint16_t len) = 0;
11+
virtual bool write(uint16_t offset, const void* data, uint16_t len) = 0;
1212
virtual bool read(uint16_t offset, void* data, uint16_t len) = 0;
1313

1414
virtual bool copyTo(Buffer* destination, uint16_t dest_start = 0,
@@ -26,7 +26,7 @@ class Buffer {
2626
bool write8(uint16_t offset, uint8_t d);
2727
bool write16(uint16_t offset, uint16_t d);
2828
bool write32(uint16_t offset, uint32_t d);
29-
bool write(uint16_t offset, char* d);
29+
bool write(uint16_t offset, const char* d);
3030

3131
bool writeNet16(uint16_t offset, uint16_t d);
3232
bool writeNet32(uint32_t offset, uint32_t d);

DNSHandler.cpp

100755100644
+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ DNSHandler::DNSHandler(UDPHandler* udpHandler,uint8_t *dnsip,
5959
/* P R I V A T E */
6060
/* ========================================================================= */
6161
// returns false if the UDP buffer is not large enough for the request
62-
bool DNSHandler::sendDNSRequest(char* domainName, uint16_t id,
62+
bool DNSHandler::sendDNSRequest(const char* domainName, uint16_t id,
6363
uint8_t* dnsServer){
6464

6565
if (strlen(domainName) > 255){
@@ -101,7 +101,7 @@ bool DNSHandler::sendDNSRequest(char* domainName, uint16_t id,
101101
// 0 (note that we end with a 0 length for the root domain)
102102
uint16_t hostLengthOffset = DNS_HEADER_LENGTH;
103103

104-
char* nextChar = domainName;
104+
const char* nextChar = domainName;
105105
uint8_t hostLength = 0;
106106
while (*nextChar != '\0'){
107107
if (*nextChar == '.'){//toggle to the next host
@@ -155,7 +155,7 @@ bool DNSHandler::sendDNSRequest(char* domainName, uint16_t id,
155155
/* ========================================================================= */
156156
/* P U B L I C */
157157
/* ========================================================================= */
158-
uint8_t* DNSHandler::resolve(char* domainName, uint8_t* err, bool force){
158+
uint8_t* DNSHandler::resolve(const char* domainName, uint8_t* err, bool force){
159159

160160
//first, see if this domainName is in our cache
161161
for(int i=0; i<cacheSize; i++){

DNSHandler.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ class DNSHandler: TimerHandler,DatagramReceiver {
3737
uint8_t* dnsIPBackup;
3838
uint8_t timer;
3939

40-
bool sendDNSRequest(char* domainName, uint16_t id, uint8_t *dnsServer);
40+
bool sendDNSRequest(const char* domainName, uint16_t id, uint8_t *dnsServer);
4141
void checkAndSetExpiration(uint8_t i);
4242

4343
public:
4444
DNSHandler(UDPHandler* udpHandler, uint8_t* dnsip, uint8_t cacheCapcity,
4545
uint8_t* dnsipbackup = NULL);
46-
uint8_t* resolve(char* domainName, uint8_t* err, bool force = false);
46+
uint8_t* resolve(const char* domainName, uint8_t* err, bool force = false);
4747
void handleTimer(uint8_t index);
4848
void handleDatagram(uint8_t* sourceIP, uint16_t sourcePort,Buffer *packet);
4949
void printDNSCache();

ENC28J60Buffer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ uint16_t ENC28J60Buffer::size(){
2929
}
3030

3131
#include <stdio.h>
32-
bool ENC28J60Buffer::write(uint16_t offset, void* data, uint16_t len){
32+
bool ENC28J60Buffer::write(uint16_t offset, const void* data, uint16_t len){
3333

3434
uint16_t address = this->bufferStart+this->payloadPointer+offset;
3535

ENC28J60Buffer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ENC28J60Buffer: public Buffer {
2525
bool wrap = false);
2626

2727
uint16_t size();
28-
bool write(uint16_t offset, void* data, uint16_t len);
28+
bool write(uint16_t offset, const void* data, uint16_t len);
2929
bool read(uint16_t offset, void* data, uint16_t len);
3030

3131
void setPayloadPointer(uint16_t offset);

ENC28J60Driver.cpp

100755100644
File mode changed.

ENC28J60Driver.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ENC28J60Driver: public EthernetDriver {
3737

3838
public:
3939

40-
ENC28J60Driver(uint8_t* mac,uint8_t csPin = 8);
40+
ENC28J60Driver(uint8_t* mac,uint8_t csPin = 10);
4141

4242
Buffer* getSendBuffer();
4343
Buffer* getReceiveBuffer();

ENC28J60Registers.h

100755100644
File mode changed.

EtherControl.cpp

100755100644
+6-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#include <hostutil.h>
44
#include "EtherControl.h"
55

6+
#if ARDUINO >= 100
7+
#include <Arduino.h> // Arduino 1.0
8+
#else
9+
#include <Wprogram.h> // Arduino 0022
10+
#endif
11+
612
//#define EMULATE_PACKET_LOSS_PCT 25
713

814
#define MAC_SIZE 6
@@ -79,7 +85,6 @@ uint8_t EtherControl::registerTimer(TimerHandler *handler,
7985
return i+1;
8086
}
8187
}//end for
82-
8388
return 0;
8489
}
8590

@@ -209,7 +214,6 @@ bool EtherControl::sendFrame(const uint8_t *destinationMAC, uint16_t protocol,
209214
}//send frame
210215

211216
bool EtherControl::processFrame(){
212-
213217
Buffer* recvBuffer = driver->getReceiveBuffer();
214218

215219
uint16_t len = driver->receiveFrame();

EtherControl.h

100755100644
File mode changed.

EthernetDriver.h

100755100644
File mode changed.

HTTPClient.cpp

+80-22
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,108 @@
22
#include <string.h>
33
#include "HTTPClient.h"
44

5-
HTTPClient::HTTPClient(uint8_t* ip, uint16_t port, char* path)
5+
HTTPClient::HTTPClient(uint8_t* ip, uint16_t port)
66
:Socket(ip,port){
7-
this->path = path;
7+
this->sendInProgress = false;
88
}
99

10-
HTTPClient::HTTPClient(char* server, DNSHandler* dns,
11-
uint16_t port, char* path)
10+
HTTPClient::HTTPClient(const char* server, DNSHandler* dns, uint16_t port)
1211
:Socket(server,port,dns){
13-
this->path = path;
12+
this->sendInProgress = false;
1413
}
1514

1615
#include <Arduino.h>
16+
17+
bool HTTPClient::send(const char* method, const char* path,
18+
const char* contentType,const char* body) {
19+
20+
if (!this->readyToSend())
21+
return false;
22+
23+
this->sendInProgress = true;
24+
25+
this->method = method;
26+
this->path = path;
27+
this->body = body;
28+
this->contentType = contentType;
29+
30+
this->connect();
31+
32+
return true;
33+
}
34+
35+
bool HTTPClient::readyToSend(){
36+
if (this->sendInProgress) return false;
37+
if (getState() != CLOSED) return false;
38+
return true;
39+
}
40+
1741
void HTTPClient::onEstablished(){
18-
Serial.println("writing");
19-
startTime = host_millis();
2042
Buffer* buf = this->getSendDataBuffer();
21-
buf->write(0,(void*)"GET ",4);
22-
buf->write(4,path,strlen(path));
23-
buf->write(4+strlen(path),(void*)"\n\n",2);
24-
this->send(4+strlen(path)+2);
43+
uint16_t pos = 0;
44+
buf->write(pos,method,strlen(method)); pos += strlen(method);
45+
buf->write(pos," ",1); pos += 1;
46+
buf->write(pos,path,strlen(path)); pos += strlen(path);
47+
buf->write(pos,"\n",1); pos += 1;
48+
49+
if (body != NULL && strlen(body) > 0) {
50+
if (contentType != NULL && strlen(contentType) > 0){
51+
buf->write(pos, "Content-Type: "); pos += 14;
52+
buf->write(pos, contentType); pos += strlen(contentType);
53+
buf->write(pos,"\n",1); pos += 1;
54+
}
55+
56+
buf->write(pos, "Content-Length: "); pos += 16;
57+
char contentLength[5];
58+
itoa(strlen(body), contentLength, 10);
59+
buf->write(pos,contentLength,strlen(contentLength));
60+
pos += strlen(contentLength);
61+
buf->write(pos,"\n",1); pos += 1;
62+
}
63+
buf->write(pos,"\n",1); pos += 1;
64+
if (body != NULL && strlen(body) > 0) {
65+
buf->write(pos,body,strlen(body));
66+
pos += strlen(body);
67+
}
68+
Socket::send(pos);
2569
}
2670

2771
bool HTTPClient::onDataReceived(Buffer* buf){
28-
for(int i=0; i<buf->size(); i++){
29-
char c;
30-
buf->read(i,&c,1);
31-
printf("%c",c);
32-
}
72+
// this client is really just for uploading data
73+
// therefore, we don't care about the result
74+
75+
#ifdef DEBUG
76+
uint8_t c;
77+
uint16_t offset = 0;
78+
79+
while(buf->read8(offset++,&c))
80+
printf("%c",(char)c);
81+
82+
#endif
3383
return true;
3484
}
3585

3686

3787
void HTTPClient::onRemoteClosed(){
38-
printf("Connection closed by remote host.\n");
88+
// connection closed by remote host
89+
this->sendInProgress = false;
3990
close();
4091
}
4192

4293
void HTTPClient::onClosed(){
43-
printf("Closed\n");
94+
// closed locally
95+
96+
//we may be in time-wait which would prevent us from
97+
//being able to do another send despite that a new
98+
//socket connection will always allocate a different port
99+
//so let's force ourselves straight to CLOSED
100+
if (this->sendInProgress)
101+
forceClose();
102+
103+
this->sendInProgress = false;
44104
}
45105

46106
void HTTPClient::onReset(bool resetByRemoteHost){
47-
if (resetByRemoteHost)
48-
printf("Connection reset by remote host.\n");
49-
else
50-
printf("Connection reset by client.\n");
107+
this->sendInProgress = false;
108+
51109
}

HTTPClient.h

+10-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
class HTTPClient: public Socket{
77

8-
char* path;
8+
const char* path;
9+
const char* method;
10+
const char* body;
11+
const char* contentType;
912
uint32_t bytes;
10-
uint32_t startTime;
13+
bool sendInProgress;
1114

1215
protected:
1316
void onEstablished();
@@ -17,10 +20,11 @@ class HTTPClient: public Socket{
1720
void onReset(bool resetByRemoteHost);
1821

1922
public:
20-
HTTPClient(uint8_t* ip, uint16_t port = 80, char* path = "/");
21-
HTTPClient(char* server, DNSHandler* dns,
22-
uint16_t port = 80, char* path = "/");
23-
23+
HTTPClient(uint8_t* ip, uint16_t port = 80);
24+
HTTPClient(const char* server, DNSHandler* dns, uint16_t port = 80);
25+
bool send(const char* method, const char* path,
26+
const char* contentType, const char* body);
27+
bool readyToSend();
2428
};
2529

2630
#endif

IPHandler.cpp

100755100644
+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ PacketHandler* IPHandler::getProtocolHandler(uint8_t ipProtocol){
126126
/* T I M E R R E G I S T R A T I O N */
127127
/* ========================================================================= */
128128
uint8_t IPHandler::registerTimer(TimerHandler *handler, uint16_t millisDelay){
129-
etherControl->registerTimer(handler,millisDelay);
129+
return etherControl->registerTimer(handler,millisDelay);
130130
}
131131

132132
void IPHandler::unregisterTimer(uint8_t index){

MemBuffer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ uint16_t MemBuffer::size(){
2727
return this->bufferLength;
2828
}
2929

30-
bool MemBuffer::write(uint16_t offset, void* data, uint16_t len){
30+
bool MemBuffer::write(uint16_t offset, const void* data, uint16_t len){
3131
if (offset + len > this->size()) return false;
3232
memcpy(this->buffer+offset,data,len);
3333
return true;

MemBuffer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MemBuffer: public Buffer {
1616

1717
uint16_t size();
1818

19-
bool write(uint16_t offset, void* data, uint16_t len);
19+
bool write(uint16_t offset, const void* data, uint16_t len);
2020
bool read(uint16_t offset, void* data, uint16_t len);
2121

2222
uint8_t getBufferType();

OffsetBuffer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ uint16_t OffsetBuffer::getRootBufferOffset(){
5656
}
5757

5858
#include <Arduino.h>
59-
bool OffsetBuffer::write(uint16_t start, void* data, uint16_t len){
59+
bool OffsetBuffer::write(uint16_t start, const void* data, uint16_t len){
6060

6161
//make sure we are within the bounds of the offset buffer
6262
if (start+len > this->size()) return false;

OffsetBuffer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class OffsetBuffer : public Buffer {
1818
bool reinit(Buffer *buffer,uint16_t offset, uint16_t length = 0);
1919

2020
uint16_t size();
21-
bool write(uint16_t offset, void* data, uint16_t len);
21+
bool write(uint16_t offset, const void* data, uint16_t len);
2222
bool read(uint16_t offset, void* data, uint16_t len);
2323

2424
bool copyTo(Buffer* destination, uint16_t dest_start = 0,

SMTPClient.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ bool SMTPClient::email(char* from, char* to, char* subject, char* body){
4949
this->body = body;
5050

5151
this->connect();
52+
53+
return true;
5254
}
5355

5456
uint8_t SMTPClient::getStatus(){
@@ -111,14 +113,14 @@ bool SMTPClient::checkCode(char* line, uint16_t code){
111113
return false;
112114
}
113115

114-
bool SMTPClient::checkMessage(char* line, char* message){
116+
bool SMTPClient::checkMessage(const char* line, const char* message){
115117
char* start = strstr(line," ");
116118
if (start == NULL) return false;
117119
start++;
118120

119121
char* finish = strstr(start," ");
120122
if (finish == NULL)
121-
finish = line + strlen(line);
123+
finish = (char*)line + strlen(line);
122124

123125
if ((finish - start) != strlen(message)) return false;
124126

SMTPClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SMTPClient: public Socket{
2323
char recvLine[40];
2424

2525
void onLineReceived(char* line);
26-
bool checkMessage(char*,char*);
26+
bool checkMessage(const char*,const char*);
2727
bool checkCode(char*,uint16_t);
2828

2929
protected:

0 commit comments

Comments
 (0)