Guest User

Untitled

a guest
Apr 2nd, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. #ifndef CPP_URL
  2. #define CPP_URL
  3. #include<iostream>
  4. #include<vector>
  5. #include<map>
  6. using std::string;
  7.  
  8. enum protocol {
  9. HTTP,
  10. HTTPS
  11. };
  12.  
  13. static string map(protocol scheme) {
  14. switch(scheme) {
  15. case HTTP:
  16. return "http";
  17. default:
  18. return "https";
  19. }
  20. }
  21.  
  22. static int defaultPort(protocol scheme) {
  23. switch (scheme) {
  24. case HTTP:
  25. return 80;
  26. default:
  27. return 440;
  28. }
  29. }
  30.  
  31. class url final {
  32. private:
  33. using list = std::vector<std::string>;
  34. using pairs = std::map<std::string, std::string>;
  35. const list paths;
  36. const pairs queries;
  37. const protocol scheme;
  38. const string domain;
  39. const string fragment;
  40. const string username;
  41. const string password;
  42. mutable int port;
  43. public:
  44. class builder {
  45. friend url;
  46. private:
  47. protocol scheme;
  48. pairs queries;
  49. list paths;
  50. string domain;
  51. string fragment;
  52. string username;
  53. string password;
  54. int port = -1;
  55. public:
  56. builder& setScheme(protocol);
  57. builder& setFragment(const string&);
  58. builder& setDomain(const string&);
  59. builder& setUsername(const string&);
  60. builder& setPassword(const string&);
  61. builder& setPort(int port);
  62. builder& addQuery(const string&, const string&);
  63. builder& addPath(const string&);
  64. url& build() const;
  65. };
  66. explicit url(const url::builder&);
  67. string build() const;
  68. };
  69.  
  70. using builder = url::builder;
  71.  
  72. inline builder& builder::setScheme(protocol scheme) {
  73. this->scheme = scheme;
  74. return *this;
  75. }
  76.  
  77. inline builder& builder::setUsername(const string& username) {
  78. this->username = username;
  79. return *this;
  80. }
  81.  
  82. inline builder& builder::setPassword(const string& password) {
  83. this->password = password;
  84. return *this;
  85. }
  86.  
  87. inline builder& builder::setFragment(const string& fragment) {
  88. this->fragment = fragment;
  89. return *this;
  90. }
  91.  
  92. inline builder& builder::setDomain(const string& domain) {
  93. this->domain = domain;
  94. return *this;
  95. }
  96.  
  97. inline builder& builder::setPort(int port) {
  98. this->port = port;
  99. return *this;
  100. }
  101.  
  102. inline builder& builder::addQuery(const string& key, const string& value) {
  103. queries.insert(std::pair<string, string>(key, value));
  104. return *this;
  105. }
  106.  
  107. inline builder& builder::addPath(const string& path) {
  108. paths.insert(paths.begin(), path);
  109. return *this;
  110. }
  111.  
  112. inline url& builder::build() const {
  113. return *(new url(*this));
  114. }
  115.  
  116. inline url::url(const builder& object)
  117. : paths(object.paths)
  118. , queries(object.queries)
  119. , scheme(object.scheme)
  120. , domain(object.domain)
  121. , fragment(object.fragment)
  122. , username(object.username)
  123. , password(object.password)
  124. , port(object.port)
  125. {}
  126.  
  127. string url::build() const {
  128. string url = map(scheme).append("://");
  129. if (!username.empty()) {
  130. url.append(username);
  131. if (!password.empty()) {
  132. url.append(":").append(password);
  133. }
  134. url.append("@");
  135. }
  136. url.append(domain);
  137. if (port == -1)
  138. port = defaultPort(scheme);
  139. url.append(":").append(std::to_string(port));
  140. for (string path : paths) {
  141. url.append("/");
  142. url.append(path);
  143. }
  144. auto it = queries.begin();
  145. if (it != queries.end()) {
  146. url.append("?").append(it->first)
  147. .append("=").append(it->second);
  148. for(it++; it != queries.end(); it++) {
  149. url.append("&").append(it->first)
  150. .append("=").append(it->second);
  151.  
  152. }
  153. }
  154. if (!fragment.empty()) {
  155. url.append("#").append(fragment);
  156. }
  157. return url;
  158. };
  159.  
  160. #endif
  161.  
  162. url url = url::builder()
  163. .setScheme(HTTPS)
  164. .setDomain(YOUTUBE_DOMAIN)
  165. .addPath(WATCH_PATH)
  166. .addQuery(CATEGORY, ITEM)
  167. .build();
  168. std::cout<<url.build()<<std::endl;
Add Comment
Please, Sign In to add comment