location.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "location.h"
  2. #include <util/string/cast.h>
  3. using namespace NNeh;
  4. TParsedLocation::TParsedLocation(TStringBuf path) {
  5. path.Split(':', Scheme, path);
  6. path.Skip(2);
  7. const size_t pos = path.find_first_of(TStringBuf("?@"));
  8. if (TStringBuf::npos != pos && '@' == path[pos]) {
  9. path.SplitAt(pos, UserInfo, path);
  10. path.Skip(1);
  11. }
  12. auto checkRange = [](size_t b, size_t e){
  13. return b != TStringBuf::npos && e != TStringBuf::npos && b < e;
  14. };
  15. size_t oBracket = path.find_first_of('[');
  16. size_t cBracket = path.find_first_of(']');
  17. size_t endEndPointPos = path.find_first_of('/');
  18. if (checkRange(oBracket, cBracket)) {
  19. endEndPointPos = path.find_first_of('/', cBracket);
  20. }
  21. EndPoint = path.SubStr(0, endEndPointPos);
  22. Host = EndPoint;
  23. size_t lastColon = EndPoint.find_last_of(':');
  24. if (checkRange(cBracket, lastColon)
  25. || (cBracket == TStringBuf::npos && lastColon != TStringBuf::npos))
  26. {
  27. Host = EndPoint.SubStr(0, lastColon);
  28. Port = EndPoint.SubStr(lastColon + 1, EndPoint.size() - lastColon + 1);
  29. }
  30. if (endEndPointPos != TStringBuf::npos) {
  31. Service = path.SubStr(endEndPointPos + 1, path.size() - endEndPointPos + 1);
  32. }
  33. }
  34. ui16 TParsedLocation::GetPort() const {
  35. if (!Port) {
  36. return TStringBuf("https") == Scheme || TStringBuf("fulls") == Scheme || TStringBuf("posts") == Scheme ? 443 : 80;
  37. }
  38. return FromString<ui16>(Port);
  39. }