Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.5 KiB

  1. // -----------------------------------------------------------------------------
  2. // Parse char string as URL
  3. //
  4. // Adapted from HTTPClient::beginInternal()
  5. // https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
  6. //
  7. // -----------------------------------------------------------------------------
  8. #pragma once
  9. struct URL {
  10. String value;
  11. String protocol;
  12. String host;
  13. String path;
  14. uint16_t port;
  15. URL(const char* url) { init(url); }
  16. URL(const String& url) { init(url); }
  17. void init(String url);
  18. };
  19. void URL::init(String url) {
  20. this->value = url;
  21. // cut the protocol part
  22. int index = url.indexOf("://");
  23. if (index > 0) {
  24. this->protocol = url.substring(0, index);
  25. url.remove(0, (index + 3));
  26. }
  27. if (this->protocol == "http") {
  28. this->port = 80;
  29. } else if (this->protocol == "https") {
  30. this->port = 443;
  31. }
  32. // cut the host part
  33. String _host;
  34. index = url.indexOf('/');
  35. if (index >= 0) {
  36. _host = url.substring(0, index);
  37. } else {
  38. _host = url;
  39. }
  40. // store the remaining part as path
  41. if (index >= 0) {
  42. url.remove(0, index);
  43. this->path = url;
  44. } else {
  45. this->path = "/";
  46. }
  47. // separate host from port, when present
  48. index = _host.indexOf(':');
  49. if (index >= 0) {
  50. this->port = _host.substring(index + 1).toInt();
  51. this->host = _host.substring(0, index);
  52. } else {
  53. this->host = _host;
  54. }
  55. }