DataHandler.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "tools/skiaserve/urlhandlers/UrlHandler.h"
  8. #include "microhttpd.h"
  9. #include "tools/skiaserve/Request.h"
  10. #include "tools/skiaserve/Response.h"
  11. using namespace Response;
  12. bool DataHandler::canHandle(const char* method, const char* url) {
  13. static const char* kBaseUrl = "/data";
  14. return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
  15. 0 == strncmp(url, kBaseUrl, strlen(kBaseUrl));
  16. }
  17. int DataHandler::handle(Request* request, MHD_Connection* connection,
  18. const char* url, const char* method,
  19. const char* upload_data, size_t* upload_data_size) {
  20. SkTArray<SkString> commands;
  21. SkStrSplit(url, "/", &commands);
  22. if (!request->hasPicture() || commands.count() != 2) {
  23. return MHD_NO;
  24. }
  25. sk_sp<UrlDataManager::UrlData> urlData(
  26. SkRef(request->fUrlDataManager.getDataFromUrl(SkString(url))));
  27. if (urlData) {
  28. return SendData(connection, urlData->fData.get(), urlData->fContentType.c_str());
  29. }
  30. return MHD_NO;
  31. }