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.

75 lines
1.7 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. class URL {
  10. public:
  11. URL() :
  12. protocol(),
  13. host(),
  14. path(),
  15. port(0)
  16. {}
  17. URL(const String& string) {
  18. _parse(string);
  19. }
  20. String protocol;
  21. String host;
  22. String path;
  23. uint16_t port;
  24. private:
  25. void _parse(String buffer) {
  26. // cut the protocol part
  27. int index = buffer.indexOf("://");
  28. if (index > 0) {
  29. this->protocol = buffer.substring(0, index);
  30. buffer.remove(0, (index + 3));
  31. }
  32. if (this->protocol == "http") {
  33. this->port = 80;
  34. } else if (this->protocol == "https") {
  35. this->port = 443;
  36. }
  37. // cut the host part
  38. String _host;
  39. index = buffer.indexOf('/');
  40. if (index >= 0) {
  41. _host = buffer.substring(0, index);
  42. } else {
  43. _host = buffer;
  44. }
  45. // store the remaining part as path
  46. if (index >= 0) {
  47. buffer.remove(0, index);
  48. this->path = buffer;
  49. } else {
  50. this->path = "/";
  51. }
  52. // separate host from port, when present
  53. index = _host.indexOf(':');
  54. if (index >= 0) {
  55. this->port = _host.substring(index + 1).toInt();
  56. this->host = _host.substring(0, index);
  57. } else {
  58. this->host = _host;
  59. }
  60. }
  61. };