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.

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