many changes
This commit is contained in:
33
net/HTTP.h
33
net/HTTP.h
@@ -1,7 +1,11 @@
|
||||
#ifndef HTTP_H
|
||||
#define HTTP_H
|
||||
|
||||
|
||||
#include "../Platforms.h"
|
||||
|
||||
#include "TCP.h"
|
||||
#include "DNS.h"
|
||||
#include "../Debug.h"
|
||||
|
||||
class HTTPClient;
|
||||
@@ -10,13 +14,13 @@ typedef void (*HTTPCBodyDataCallback)(HTTPClient* httpc, const uint8_t* data, ui
|
||||
typedef void (*HTTPCConnectCallback)(HTTPClient* httpc);
|
||||
typedef void (*HTTPCDisconnectCallback)(HTTPClient* httpc);
|
||||
|
||||
|
||||
class HTTPClient {
|
||||
|
||||
static constexpr const char* NAME = "HTTPC";
|
||||
TCP tcp;
|
||||
|
||||
espconn dns;
|
||||
DNS dns;
|
||||
|
||||
ip_addr_t ip;
|
||||
char host[64];
|
||||
char query[128];
|
||||
@@ -70,7 +74,7 @@ public:
|
||||
|
||||
debugMod3(NAME, "connect(%s) -> host: '%s' query: '%s'", url, this->host, this->query);
|
||||
|
||||
resolveHost(this->host);
|
||||
dns.resolveHost(this->host, _onHostResolved, (void*)this);
|
||||
|
||||
}
|
||||
|
||||
@@ -80,6 +84,10 @@ public:
|
||||
numBreaks = 0;
|
||||
}
|
||||
|
||||
void consumed(uint16_t len) {
|
||||
tcp.consumed(len);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/** from TCP: connected */
|
||||
@@ -130,26 +138,23 @@ private:
|
||||
|
||||
private:
|
||||
|
||||
/** resolve hostname. creates callback */
|
||||
void resolveHost(const char* c) {
|
||||
debugMod1(NAME, "dns lookup: %s", c);
|
||||
dns.reverse = (void*) this;
|
||||
espconn_gethostbyname(&dns, c, &ip, _onHostResolved);
|
||||
}
|
||||
|
||||
|
||||
/** send the request header */
|
||||
void sendRequest() {
|
||||
char buf[128];
|
||||
const int len = os_sprintf(buf, "GET %s HTTP/1.1\r\nicy-metadata:1\r\n\r\n", this->query);
|
||||
debugMod(NAME, buf);
|
||||
const int len = sprintf(buf, "GET %s HTTP/1.1\r\n\r\n", this->query);
|
||||
//const int len = sprintf(buf, "GET %s HTTP/1.1\r\nicy-metadata:1\r\n\r\n", this->query);
|
||||
printf(buf);
|
||||
//debugMod(NAME, (const char*) buf);
|
||||
tcp.send((const uint8_t*)buf, len);
|
||||
}
|
||||
|
||||
static void _onHostResolved(const char* name, ip_addr_t* ipaddr, void* arg ) {
|
||||
HTTPClient* http = (HTTPClient*) ((espconn*)arg)->reverse;
|
||||
static void _onHostResolved(const char* name, const ip_addr_t* ipaddr, void* arg ) {
|
||||
HTTPClient* http = (HTTPClient*) arg;
|
||||
if (ipaddr) {
|
||||
//debugMod1(NAME, "dns lookup returned: %s", tmp.toString());
|
||||
http->tcp.connect(IP(*ipaddr), 80);
|
||||
http->tcp.connect(IP4(*ipaddr), 80);
|
||||
} else {
|
||||
debugMod1(NAME, "dns lookup failed: %s", name);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user