This library was written by and for a non-professional 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.
This library will parse a URL, and normalize it according to the information provided in RFC3986. Take for example this terrifying URL:
http://%7EFoo:%7Ep@[email protected]:80/%7Ethis/is/a/./path/test.php?foo=bar#frag
LCBUrl will allow you to parse and access the hostname, user name, password, path, query string, fragment, etc.:
getUrl()
returns:http://~Foo:~p@[email protected]/~this/is/a/./path/test.php?foo=bar#frag
getIPUrl()
returns:http://~Foo:~p@[email protected]/~this/is/a/./path/test.php?foo=bar#frag
getScheme()
returns:http
getUserInfo()
returns:~Foo:~p@$$word
getUserName()
returns:~Foo
getPassword()
returns:~p@$$word
getHost()
returns:servername.local
getIP()
returns:XXX.XXX.XXX.XXX
getPort()
returns:80
getAuthority()
returns:~Foo:~p@[email protected]
getIPAuthority()
returns:~Foo:~p@[email protected]
getPath()
returns:~this/is/a/./path/test.php
getAfterPath()
returns:?foo=bar#frag
getQuery()
returns:foo=bar
getFragment()
returns:frag
If you are using mDNS in your projects, you may have discovered many microcontroller libraries do not support mDNS in all areas. This library will additionally (but conditionally) re-parse a URL and replace the *.local
hostname with the resolved IP address.
❗ IMPORTANT: Using any of the network-based methods in a timer (i.e. Ticker) will crash or hang your program. This hang is not a shortcoming of this library; it is how radio-functions (networking being one) work.
bool setUrl(String)
- Pass the URL to be handled to the classString getUrl()
- Return a processed/normalized 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 nameword getPort()
- Return port (if present) if non-standardString 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)String getPath()
- Returns the path segment (if present) with any query or fragment removedString getAfterPath()
- Returns query and fragment segments (if present)String getQuery()
- Returns query (if present)String getFragment()
- Returns fragment (if present)
❗ These methods are deprecated and slated to be removed.
These utility methods are dependant upon the class' instantiation and current URL string.
bool isMDNS()
- Return true or false determination of whether the class' host is a valid mDNS nameIPAddress getIP()
- Return IP address of class' host (always does lookup)
These methods are intended to extend functionality of the TCP libraries, and may be leveraged independent of a specific instance declaration.
bool isMDNS(const char *hostName)
- Returns true ifhostName
is a valid mDNS nameIPAddress getIP(const char * hostName)
- Return IP address ofhostName
(always does lookup)bool isValidIP(const char * hostName)
- Returns true ifhostName
represents a valid IP address stringint labelCount(const char * hostName)
- Integer of the number of labels in thehostName
bool isANumber(const char * str)
- Returns true ifstr
is a valid numberbool isValidLabel(const char *label)
- Returns true if thelabel
is a valid DNS labelbool isValidHostName(const char *hostName)
- Return true if thehostName
passed is a valid DNS, mDNS or IP hostname
- Convert percent-encoded triplets to uppercase
- Convert the scheme and host to lowercase
- Decode percent-encoded triplets of unreserved characters
- Remove dot-segments
- Convert an empty path to a "/" path
- Remove the default port
-
Add a trailing "/" to a non-empty path (may remove this)(removed this after some thought) - Add validity check functions (not yet part of the initialization)
Installation is particular to the platform with which you are developing:
This library is available as lbussy/LCBUrl
in PlatformIO's library manager.
LCBUrl is published via the Arduino Library Manager. You can include this library in your project by navigating to Sketch -> Include Libraries -> Manage Libraries (or Ctrl - Shift - I). Type LCBUrl
in the search bar:
To install this library, place this entire folder as a subfolder in your
./lib/targets/libraries
folder.
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 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)
./lib/targets/libraries/LCBUrl/src/LCBUrl.h (the library description file)
Ensure that you add a corresponding line to the top of your sketch:
#include <LCBUrl.h>
To stop using this library, delete that line from your sketch.
To enable mDNS support, define the macro before including the library:
#define LCBURL_MDNS
#include <LCBUrl.h>
Or, define it globally in the platformio.ini or compiler definitions:
-DLCBURL_MDNS
Without this definition, the mDNS functions will return failures.
After initial release of this library, the upstream WiFi.hostByName()
stopped resolving mDNS queries. I am exploring other options to provide name resolution for mDNS on the ESP8266.
If you would like to contribute to this project:
- Report bugs and errors
- Ask for enhancements
- Create issues and pull requests
- Tell other people about this library
The library is licensed under the MIT License.
LCBUrl is Copyright © 2019-2022 by Lee C Bussy.