| 139 | |
| 140 | |
| 141 | Try<mesos::URI> HDFS::parse(const string& uri) |
| 142 | { |
| 143 | size_t schemePos = uri.find("://"); |
| 144 | if (schemePos == string::npos) { |
| 145 | return Error("Missing scheme in url string"); |
| 146 | } |
| 147 | |
| 148 | const string uriPath = uri.substr(schemePos + 3); |
| 149 | |
| 150 | size_t pathPos = uriPath.find_first_of('/'); |
| 151 | if (pathPos == 0) { |
| 152 | return mesos::uri::hdfs(uriPath); |
| 153 | } |
| 154 | |
| 155 | // If path is specified in the URL, try to capture the host and path |
| 156 | // separately. |
| 157 | string host = uriPath; |
| 158 | string path = "/"; |
| 159 | if (pathPos != string::npos) { |
| 160 | host = host.substr(0, pathPos); |
| 161 | path = uriPath.substr(pathPos); |
| 162 | } |
| 163 | |
| 164 | if (host.empty()) { |
| 165 | return mesos::uri::hdfs(path); |
| 166 | } |
| 167 | |
| 168 | const vector<string> tokens = strings::tokenize(host, ":"); |
| 169 | |
| 170 | if (tokens[0].empty()) { |
| 171 | return Error("Host not found in url"); |
| 172 | } |
| 173 | |
| 174 | if (tokens.size() > 2) { |
| 175 | return Error("Found multiple ports in url"); |
| 176 | } |
| 177 | |
| 178 | Option<int> port; |
| 179 | if (tokens.size() == 2) { |
| 180 | Try<int> numifyPort = numify<int>(tokens[1]); |
| 181 | if (numifyPort.isError()) { |
| 182 | return Error("Failed to parse port: " + numifyPort.error()); |
| 183 | } |
| 184 | |
| 185 | port = numifyPort.get(); |
| 186 | } else { |
| 187 | // Default port for HDFS. |
| 188 | port = 8020; |
| 189 | } |
| 190 | |
| 191 | return mesos::uri::hdfs(path, tokens[0], port.get()); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | // An HDFS client path must be either a full URI or an absolute path. If it is |