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.

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