Skip to content

Commit

Permalink
Add mDNS and name resolution support
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Jul 15, 2020
1 parent 980839f commit a302153
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
100 changes: 99 additions & 1 deletion src/LCBUrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ LCBUrl::LCBUrl()
{
rawurl = "";
url = "";
ipurl = "";
workingurl = "";
scheme = "";
stripscheme = "";
Expand All @@ -47,8 +48,10 @@ LCBUrl::LCBUrl()
username = "";
password = "";
host = "";
ipaddress = INADDR_NONE;
port = 65535;
authority = "";
ipauthority = "";
pathsegment = "";
path = "";
removedotsegments = "";
Expand All @@ -69,13 +72,13 @@ bool LCBUrl::setUrl(const String &newUrl)
}
else
{
getIP(); // We neeed to try this up front and pre-cache
return true;
}
}

String LCBUrl::getUrl()
{

if (url.length() == 0)
{
url = "";
Expand All @@ -102,6 +105,42 @@ String LCBUrl::getUrl()
return url;
}

String LCBUrl::getIPUrl()
{
if (url.length() == 0)
{
url = "";
url.concat(getScheme()); // http or https
url.concat(F("://"));
url.concat(getIPAuthority()); // Username, password, host and port
url.concat(F("/"));
url.concat(getPath()); // Path
if (getQuery() != "") // Add a query string
{
url.concat(F("?"));
url.concat(getQuery());
}
if (getFragment() != "") // Add a fragment
{
url.concat(F("#"));
url.concat(getFragment());
}
if ((getScheme() == "") || (getHost() == "")) // No idea what I was thinking here
{
return url;
}
}
return url;
}

bool LCBUrl::isMDNS()
{
return getHost().endsWith(".local");
// Can use following for C++
// return host.size() >= suffix.size() && 0 == host.compare(host.size()-suffix.size(), suffix.size(), suffix);
}


String LCBUrl::getScheme()
{ // Currrently only finds http and https as scheme
if (scheme.length() == 0)
Expand Down Expand Up @@ -204,6 +243,32 @@ String LCBUrl::getHost()
return host;
}

IPAddress LCBUrl::getIP()
{
IPAddress returnIP = INADDR_NONE;

// First try to resolve the address fresh
int err = 0;
err = WiFi.hostByName(getHost().c_str(), returnIP) ;

if(err == 1)
{
ipaddress = returnIP;
}
else
{
Serial.print(F("DEBUG: hostByName() failed for "));
Serial.print(getHost().c_str());
Serial.print(". Error code: (");
Serial.print(err);
Serial.println(F(")"));
}

// If we got a new IP address, we will use it. Otherwise
// we will use last known good (if there is one)
return ipaddress;
}

word LCBUrl::getPort()
{
// Port will be any integer between : and / in authority
Expand Down Expand Up @@ -266,6 +331,39 @@ String LCBUrl::getAuthority()
return authority;
}

String LCBUrl::getIPAuthority()
{
if (ipauthority.length() == 0)
{
ipauthority = "";
if (getUserName().length() > 0)
{
ipauthority = getUserName();
}
if (getPassword().length() > 0)
{
ipauthority.concat(F(":"));
ipauthority.concat(getPassword());
}
if (ipauthority.length() > 0)
{
ipauthority.concat(F("@"));
}
ipauthority.concat(getIP());
if (getPort() > 0)
{
if (
((getScheme() == F("http")) && (port != 80)) ||
((getScheme() == F("https")) && (port != 443)))
{
ipauthority.concat(F(":"));
ipauthority.concat(String(getPort()));
}
}
}
return ipauthority;
}

String LCBUrl::getPath()
{
if (path.length() == 0)
Expand Down
14 changes: 14 additions & 0 deletions src/LCBUrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
#ifndef _LCBURL_H
#define _LCBURL_H

#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif

#include <string.h>
#include <Arduino.h>

Expand All @@ -42,14 +49,18 @@ class LCBUrl
LCBUrl();
~LCBUrl(){};
bool setUrl(const String &);
bool isMDNS();
String getUrl();
String getIPUrl();
String getScheme();
String getUserInfo();
String getUserName();
String getPassword();
String getHost();
IPAddress getIP();
word getPort();
String getAuthority();
String getIPAuthority();
String getPath();
String getAfterPath();
String getQuery();
Expand All @@ -65,6 +76,7 @@ class LCBUrl
String getPathSegment();
String rawurl;
String url;
String ipurl;
String workingurl;
String scheme;
String stripscheme;
Expand All @@ -74,8 +86,10 @@ class LCBUrl
String username;
String password;
String host;
IPAddress ipaddress;
word port;
String authority;
String ipauthority;
String pathsegment;
String path;
String removedotsegments;
Expand Down

0 comments on commit a302153

Please sign in to comment.