uri.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "uri.hpp"
  14. #include "jexcept.hpp"
  15. URI::URI(const char* path)
  16. {
  17. state.uri = &uri;
  18. try {
  19. if (uriParseUriA(&state, path) != URI_SUCCESS)
  20. throw MakeStringException(-1, "Invalid URI '%s'", path);
  21. populateFields(); // In a format we understand
  22. }
  23. // On parser failure, but also system exceptions (bad alloc, etc)
  24. catch (IException *)
  25. {
  26. uriFreeUriMembersA(&uri);
  27. throw;
  28. }
  29. uriFreeUriMembersA(&uri);
  30. }
  31. // Helper, to validate URI before creating object
  32. bool URI::isURI(const char *path)
  33. {
  34. UriParserStateA state;
  35. UriUriA uri;
  36. state.uri = &uri;
  37. bool match = (uriParseUriA(&state, path) == URI_SUCCESS);
  38. uriFreeUriMembersA(&uri);
  39. return match;
  40. }
  41. void URI::populateFields()
  42. {
  43. // Scheme (defines which resolver to use, see above)
  44. StringBuffer schemeStr(uri.scheme.afterLast - uri.scheme.first, uri.scheme.first);
  45. schemeStr.toLowerCase();
  46. if (strcmp(schemeStr.str(), "hpcc") == 0)
  47. scheme = URIScheme_hpcc;
  48. else if (strcmp(schemeStr.str(), "file") == 0)
  49. scheme = URIScheme_file;
  50. else
  51. scheme = URIScheme_error;
  52. // Server
  53. server.user.set(uri.userInfo.first, uri.userInfo.afterLast - uri.userInfo.first);
  54. server.host.set(uri.hostText.first, uri.hostText.afterLast - uri.hostText.first);
  55. StringAttr portStr(uri.portText.first, uri.portText.afterLast - uri.portText.first);
  56. server.port = atoi(portStr.get()); // More - use default ports?
  57. // Path
  58. UriPathSegmentA* cur = uri.pathHead;
  59. StringBuffer pathStr;
  60. if (uri.absolutePath || scheme == URIScheme_file)
  61. pathStr.append("/");
  62. bool first = true;
  63. while (cur)
  64. {
  65. if (!first)
  66. pathStr.append("/");
  67. pathStr.append(cur->text.afterLast - cur->text.first, cur->text.first);
  68. first = false;
  69. cur = cur->next;
  70. }
  71. path.path.set(pathStr.str());
  72. // Extra info
  73. if (scheme == URIScheme_hpcc)
  74. {
  75. StringBuffer query(uri.query.afterLast - uri.query.first, uri.query.first);
  76. query.toLowerCase();
  77. if (strcmp(query.str(), "super") == 0)
  78. {
  79. path.type = URIFile_super;
  80. path.subname.set(uri.fragment.first, uri.fragment.afterLast - uri.fragment.first);
  81. path.index = 0;
  82. }
  83. else if (strcmp(query.str(), "stream") == 0)
  84. {
  85. path.type = URIFile_stream;
  86. StringAttr index(uri.fragment.first, uri.fragment.afterLast - uri.fragment.first);
  87. path.index = atoi(index.get());
  88. }
  89. else
  90. {
  91. path.type = URIFile_logic;
  92. path.index = 0;
  93. }
  94. }
  95. else
  96. {
  97. path.type = URIFile_local;
  98. path.index = 0;
  99. }
  100. }
  101. void URI::appendSchemeStr(StringBuffer& buf)
  102. {
  103. switch(scheme)
  104. {
  105. case URIScheme_hpcc:
  106. buf.append("hpcc");
  107. return;
  108. case URIScheme_file:
  109. buf.append("file");
  110. return;
  111. default:
  112. buf.append("unknown");
  113. return;
  114. }
  115. }
  116. void URI::appendServerStr(StringBuffer& buf)
  117. {
  118. if (!server.user.isEmpty())
  119. buf.append(server.user.get()).append("@");
  120. buf.append(server.host.get());
  121. if (server.port)
  122. buf.append(":").append(server.port);
  123. }
  124. void URI::appendPathStr(StringBuffer& buf)
  125. {
  126. buf.append(path.path.get());
  127. switch(path.type)
  128. {
  129. case URIFile_super:
  130. buf.append("?super");
  131. break;
  132. case URIFile_stream:
  133. buf.append("?stream");
  134. break;
  135. }
  136. if (path.index)
  137. buf.append("#").append(path.index);
  138. else if (path.subname.length())
  139. buf.append("#").append(path.subname.get());
  140. }