Beispiel #1
0
ErrorCode WdtUri::process(const string& url) {
  if (url.size() < WDT_URL_PREFIX.size()) {
    LOG(ERROR) << "Url doesn't specify wdt protocol";
    return URI_PARSE_ERROR;
  }
  StringPiece urlPiece(url, 0, WDT_URL_PREFIX.size());
  StringPiece wdtPrefix(WDT_URL_PREFIX);
  if (urlPiece != wdtPrefix) {
    LOG(ERROR) << "Url does not specify wdt protocol " << url;
    return URI_PARSE_ERROR;
  }
  urlPiece = StringPiece(url, WDT_URL_PREFIX.size());
  size_t paramsIndex = urlPiece.find("?");
  if (paramsIndex == string::npos) {
    paramsIndex = urlPiece.size();
  }
  ErrorCode status = OK;
  hostName_.assign(urlPiece.data(), paramsIndex);
  if (hostName_.size() == 0) {
    LOG(ERROR) << "URL doesn't have a valid host name " << url;
    status = URI_PARSE_ERROR;
  }
  urlPiece.advance(paramsIndex + (paramsIndex < urlPiece.size()));
  while (!urlPiece.empty()) {
    StringPiece keyValuePair = urlPiece.split_step('&');
    if (keyValuePair.empty()) {
      // Last key value pair
      keyValuePair = urlPiece;
      urlPiece.advance(urlPiece.size());
    }
    StringPiece key = keyValuePair.split_step('=');
    StringPiece value = keyValuePair;
    if (key.empty()) {
      // Value can be empty but key can't be empty
      LOG(ERROR) << "Errors parsing params, url = " << url;
      status = URI_PARSE_ERROR;
      break;
    }
    queryParams_[key.toString()] = value.toString();
  }
  return status;
}
std::unique_ptr<ZipFileCollection> ZipFileCollection::create(const StringPiece& path,
                                                             std::string* outError) {
    constexpr static const int32_t kEmptyArchive = -6;

    std::unique_ptr<ZipFileCollection> collection = std::unique_ptr<ZipFileCollection>(
            new ZipFileCollection());

    int32_t result = OpenArchive(path.data(), &collection->mHandle);
    if (result != 0) {
        // If a zip is empty, result will be an error code. This is fine and we should
        // return an empty ZipFileCollection.
        if (result == kEmptyArchive) {
            return collection;
        }

        if (outError) *outError = ErrorCodeString(result);
        return {};
    }

    void* cookie = nullptr;
    result = StartIteration(collection->mHandle, &cookie, nullptr, nullptr);
    if (result != 0) {
        if (outError) *outError = ErrorCodeString(result);
        return {};
    }

    using IterationEnder = std::unique_ptr<void, decltype(EndIteration)*>;
    IterationEnder iterationEnder(cookie, EndIteration);

    ZipString zipEntryName;
    ZipEntry zipData;
    while ((result = Next(cookie, &zipData, &zipEntryName)) == 0) {
        std::string zipEntryPath = std::string(reinterpret_cast<const char*>(zipEntryName.name),
                                               zipEntryName.name_length);
        std::string nestedPath = path.toString() + "@" + zipEntryPath;
        collection->mFiles[zipEntryPath] = util::make_unique<ZipFile>(collection->mHandle,
                                                                      zipData,
                                                                      Source(nestedPath));
    }

    if (result != -1) {
        if (outError) *outError = ErrorCodeString(result);
        return {};
    }
    return collection;
}
shared_ptr<SwitchState> applyThriftConfigFile(
    const shared_ptr<SwitchState>& state,
    StringPiece path,
    const Platform* platform) {
    // Parse the JSON config.
    //
    // This is basically what configerator's getConfigAndParse() code does,
    // except that we manually read the file from disk for now.
    // We may not be able to rely on the configerator infrastructure for
    // distributing the config files.
    cfg::SwitchConfig config;
    std::string contents;
    if (!folly::readFile(path.toString().c_str(), contents)) {
        throw FbossError("unable to read ", path);
    }
    config.readFromJson(contents.c_str());

    return ThriftConfigApplier(state, &config, platform).run();
}