Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devel #23

Merged
merged 13 commits into from
Mar 3, 2021
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![MIT License](https://img.shields.io/github/license/lbussy/LCBUrl?style=plastic)](https://github.com/lbussy/LCBUrl/blob/master/LICENSE)
[![GitHub Issues](https://img.shields.io/github/issues/lbussy/LCBUrl?style=plastic)](http://github.com/lbussy/LCBUrl/issues)
[![GitHub Pull Requests](https://img.shields.io/github/issues-pr/lbussy/LCBUrl?style=plastic)](http://github.com/lbussy/LCBUrl/pulls)
[![Contributors Welsome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=plastic)](#Contributing)
[![Contributors Welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=plastic)](#Contributing)

This library was written by and for a non-programmer. If you find some use out of it, that will make me happy, but if not, I'm still using it in my projects.

Expand All @@ -28,22 +28,21 @@ If you are using mDNS in your projects, you may have discovered many microcontro
- getAfterPath() = ?foo=bar#frag
- getQuery() = foo=bar
- getFragment() = frag
- isMDNS

:exclamation: IMPORTANT: Using any of the IP-based methods in a timer will crash or hang your program. This hang is not a shortcoming of this library; it is how radio-functions (networking being one) work.

## Public Methods

### Core Methods

- `bool setUrl(String)` - Pass the URL to be handled to the class
- `bool isMDNS(String)` - Return true/false based on whether URL is mDNS-based (*.local) or not
- `String getUrl()` - Return a processed URI in the following format: `scheme:[//authority]path[?query][#fragment]`
- `String getIPUrl()` - Return a processed URI with the host replaced by the IP address in the following format: `scheme:[//authority]path[?query][#fragment]` (useful for mDNS URLs)
- `String getScheme()` - Get the scheme (currently only handles http and https)
- `String getUserInfo()` - Return username and password (if present)
- `String getUserName()` - Returns username (if present)
- `String getPassword()` - Returns password (if present)
- `String getHost()` - Return host name
- `String getIP()` - Return IP address of host (always does lookup)
- `word getPort()` - Return port (if present) if non-standard
- `String getAuthority()` - Return the authority (if present) in the following format: `[userinfo@]host[:port]`
- `String getIPAuthority()` - Return the authority (if present) in the following format: `[userinfo@]XXX.XXX.XXX.XXX[:port]` (useful for mDNS URLs, will use cached IPs if they exist)
Expand All @@ -52,6 +51,18 @@ If you are using mDNS in your projects, you may have discovered many microcontro
- `String getQuery()` - Returns query (if present)
- `String getFragment()` - Returns fragment (if present)

### Utility Methods

- `bool isMDNS()` - (deprecated)
- `bool isMDNS(const char *hostName)` - Returns true if hostname is a valid mDNS name
- `IPAddress getIP()` - (deprecated) Return IP address of class' host (always does lookup)
- `IPAddress getIP()` - (deprecated) Return IP address of class' host (always does lookup)
- `bool isValidIP(const char * hostName` - Returns true if hostName represents a valid IP address string
- `int labelCount(const char * hostName)` - Integer of the number of labels in the hostname
- `bool isANumber(const char * str)` - Returns true if string is a valid number
- `bool isValidLabel(const char *label)` - Returns true if the string is a valid DNS label
- `bool isValidHostName(const char *hostName)` - Return true if the hostname passed is a valid DNS, mDNS or IP hostname

## Progress:

- [X] Convert percent-encoded triplets to uppercase
Expand All @@ -61,10 +72,11 @@ If you are using mDNS in your projects, you may have discovered many microcontro
- [X] Convert an empty path to a "/" path
- [X] Remove the default port
- [X] ~~Add a trailing "/" to a non-empty path (may remove this)~~ (removed this after some thought)
- [X] Add validity check functions (not yet part of the initialization)

## Installation

Instalation is particular to the platform with which you are developing:
Installation is particular to the platform with which you are developing:

### PlatformIO

Expand All @@ -85,7 +97,7 @@ When installed, this library should contain the following files:
./lib/targets/libraries/LCBUrl (this library's folder)
./lib/targets/libraries/LCBUrl/examples (the examples in the "open" menu)
./lib/targets/libraries/LCBUrl/keywords.txt (the syntax coloring file)
./lib/targets/libraries/LCBUrl/library.properties (properties of this libraary)
./lib/targets/libraries/LCBUrl/library.properties (properties of this library)
./lib/targets/libraries/LCBUrl/LICENSE (the license for this library)
./lib/targets/libraries/LCBUrl/README.md (this file)
./lib/targets/libraries/LCBUrl/src/LCBUrl.cpp (the library implementation file)
Expand Down
84 changes: 53 additions & 31 deletions examples/Basic/Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,67 @@
#include <LCBUrl.h>
#include <Arduino.h>

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

// LCBUrl does not require ArduinoLog. It is used to
// facilitate the examples.
#include <ArduinoLog.h>
#define LOG_LEVEL LOG_LEVEL_VERBOSE

const char* ssid = "ssid";
const char* password = "password";

void setup() {
Serial.begin(74880);
delay(1000);
Serial.println(F("Starting test run of LCBUrl:"));
Serial.begin(BAUD);
Serial.println();
Serial.flush();
Log.begin(LOG_LEVEL, &Serial, false);
Log.notice(F("Starting test run of LCBUrl." CR CR));

String myUrl = "http://%7EFoo:%7Ep@$$wOrd@Servername.local:80/%7EthIs/is/A/./Path/test.php?foo=bar#frag";
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Log.notice(F("Establishing connection to WiFi." CR));
}
Log.notice(F("Connected, IP address: %s" CR), WiFi.localIP().toString().c_str());

MDNS.begin(WiFi.getHostname());

String myUrl = "http://%7EFoo:%7Ep@$$wOrd@brewpi.local:8000/%7EthIs/is/A/./Path/test.php?foo=bar#frag";
LCBUrl url;

if (!url.setUrl(myUrl)) {
Serial.println(F("Failure in setUrl();"));
Log.fatal(F("Failure in setUrl();" CR CR));
} else {
Serial.println(F("Return from setUrl():"));
Serial.print(F("\tgetUrl(); = "));
Serial.println(url.getUrl().c_str());
Serial.print(F("\tgetScheme(); = "));
Serial.println(url.getScheme().c_str());
Serial.print(F("\tgetUserInfo(); = "));
Serial.println(url.getUserInfo().c_str());
Serial.print(F("\tgetUserName(); = "));
Serial.println(url.getUserName().c_str());
Serial.print(F("\tgetPassword(); = "));
Serial.println(url.getPassword().c_str());
Serial.print(F("\tgetHost(); = "));
Serial.println(url.getHost().c_str());
Serial.print(F("\tgetPort(); = "));
Serial.println(url.getPort());
Serial.print(F("\tgetAuthority(); = "));
Serial.println(url.getAuthority().c_str());
Serial.print(F("\tgetPath(); = "));
Serial.println(url.getPath().c_str());
Serial.print(F("\tgetAfterPath(); = "));
Serial.println(url.getAfterPath().c_str());
Serial.print(F("\tgetQuery(); = "));
Serial.println(url.getQuery().c_str());
Serial.print(F("\tgetFragment(); = "));
Serial.println(url.getFragment().c_str());
Log.notice(F("Return from setUrl():" CR CR));

Log.notice(F("\tgetUrl(); = %s" CR), url.getUrl().c_str());
Log.notice(F("\tgetScheme(); = %s" CR), url.getScheme().c_str());
Log.notice(F("\tgetUserInfo(); = %s" CR), url.getUserInfo().c_str());
Log.notice(F("\tgetUserName(); = %s" CR), url.getUserName().c_str());
Log.notice(F("\tgetPassword(); = %s" CR), url.getPassword().c_str());
Log.notice(F("\tgetHost(); = %s" CR), url.getHost().c_str());
Log.notice(F("\tgetPort(); = %l" CR), url.getPort());
Log.notice(F("\tgetAuthority(); = %s" CR), url.getAuthority().c_str());
Log.notice(F("\tgetPath(); = %s" CR), url.getPath().c_str());
Log.notice(F("\tgetAfterPath(); = %s" CR), url.getAfterPath().c_str());
Log.notice(F("\tgetQuery(); = %s" CR), url.getQuery().c_str());
Log.notice(F("\tgetFragment(); = %s" CR CR), url.getFragment().c_str());

Log.notice(F("\tHostname isMDNS(): %T" CR), url.isMDNS(url.getHost().c_str()));
Log.notice(F("\tHostname isValidHostName(): %T" CR), url.isValidHostName(url.getHost().c_str()));
Log.notice(F("\tHostname isValidIP(): %T" CR CR), url.isValidIP(url.getHost().c_str()));
}

Serial.println(F("LCBUrl test run complete."));
Log.notice(F("LCBUrl test run complete." CR));
}

void loop() {
Expand Down
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ build_flags =
; -DPIO_FRAMEWORK_ARDUINO_LWIP2_IPV6_HIGHER_BANDWIDTH ; v2 IPv6 Higher Bandwidth
; -DPIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH ; v1.4 Higher Bandwidth
lib_deps =
ArduinoLog
extra_scripts =
build_type = debug

Expand Down
Loading