url_builder.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "url_builder.h"
  2. #include <library/cpp/string_utils/quote/quote.h>
  3. #include <util/generic/yexception.h>
  4. namespace NYql {
  5. TUrlBuilder::TUrlBuilder(const TString& uri)
  6. : MainUri(uri)
  7. {
  8. }
  9. TUrlBuilder& TUrlBuilder::AddUrlParam(const TString& name, const TString& value) {
  10. Params.emplace_back(TParam {name, value});
  11. return *this;
  12. }
  13. TUrlBuilder& TUrlBuilder::AddPathComponent(const TString& value) {
  14. if (!value) {
  15. throw yexception() << "Empty path component is not allowed";
  16. }
  17. TStringBuilder res;
  18. res << MainUri;
  19. if (!MainUri.EndsWith('/')) {
  20. res << '/';
  21. }
  22. res << UrlEscapeRet(value, true);
  23. MainUri = std::move(res);
  24. return *this;
  25. }
  26. TString TUrlBuilder::Build() const {
  27. if (Params.empty()) {
  28. return MainUri;
  29. }
  30. TStringBuilder res;
  31. res << MainUri << "?";
  32. TStringBuf separator = ""sv;
  33. for (const auto& p : Params) {
  34. res << separator << p.Name;
  35. if (p.Value) {
  36. res << "=" << CGIEscapeRet(p.Value);
  37. }
  38. separator = "&"sv;
  39. }
  40. return std::move(res);
  41. }
  42. } // NYql