skiaserve.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/Request.h"
  8. #include "tools/skiaserve/Response.h"
  9. #include "include/core/SkGraphics.h"
  10. #include "tools/flags/CommandLineFlags.h"
  11. #include "tools/skiaserve/urlhandlers/UrlHandler.h"
  12. #include "microhttpd.h"
  13. #include <errno.h>
  14. #if !defined _WIN32
  15. #include <sys/socket.h>
  16. #include <arpa/inet.h>
  17. #endif
  18. using namespace Response;
  19. static DEFINE_int(port, 8888, "The port to listen on.");
  20. static DEFINE_string(address, "127.0.0.1", "The address to bind to.");
  21. static DEFINE_bool(hosted, false, "Running in hosted mode on debugger.skia.org.");
  22. class UrlManager {
  23. public:
  24. UrlManager() {
  25. // Register handlers
  26. fHandlers.push_back(new RootHandler);
  27. fHandlers.push_back(new PostHandler);
  28. fHandlers.push_back(new ImgHandler);
  29. fHandlers.push_back(new ClipAlphaHandler);
  30. fHandlers.push_back(new EnableGPUHandler);
  31. fHandlers.push_back(new CmdHandler);
  32. fHandlers.push_back(new InfoHandler);
  33. fHandlers.push_back(new DownloadHandler);
  34. fHandlers.push_back(new DataHandler);
  35. fHandlers.push_back(new BreakHandler);
  36. fHandlers.push_back(new OpsHandler);
  37. fHandlers.push_back(new OpBoundsHandler);
  38. fHandlers.push_back(new ColorModeHandler);
  39. fHandlers.push_back(new QuitHandler);
  40. }
  41. ~UrlManager() {
  42. for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; }
  43. }
  44. // This is clearly not efficient for a large number of urls and handlers
  45. int invoke(Request* request, MHD_Connection* connection, const char* url, const char* method,
  46. const char* upload_data, size_t* upload_data_size) const {
  47. for (int i = 0; i < fHandlers.count(); i++) {
  48. if (fHandlers[i]->canHandle(method, url)) {
  49. return fHandlers[i]->handle(request, connection, url, method, upload_data,
  50. upload_data_size);
  51. }
  52. }
  53. return MHD_NO;
  54. }
  55. private:
  56. SkTArray<UrlHandler*> fHandlers;
  57. };
  58. const UrlManager kUrlManager;
  59. int answer_to_connection(void* cls, struct MHD_Connection* connection,
  60. const char* url, const char* method, const char* version,
  61. const char* upload_data, size_t* upload_data_size,
  62. void** con_cls) {
  63. SkDebugf("New %s request for %s using version %s\n", method, url, version);
  64. Request* request = reinterpret_cast<Request*>(cls);
  65. int result = kUrlManager.invoke(request, connection, url, method, upload_data,
  66. upload_data_size);
  67. if (MHD_NO == result) {
  68. fprintf(stderr, "Invalid method and / or url: %s %s\n", method, url);
  69. }
  70. return result;
  71. }
  72. int skiaserve_main() {
  73. SkGraphics::Init();
  74. Request request(SkString("/data")); // This simple server has one request
  75. struct sockaddr_in address;
  76. address.sin_family = AF_INET;
  77. address.sin_port = htons(FLAGS_port);
  78. int result = inet_pton(AF_INET, FLAGS_address[0], &address.sin_addr);
  79. if (result != 1) {
  80. printf("inet_pton for %s:%d failed with return %d %s\n",
  81. FLAGS_address[0], FLAGS_port, result, strerror(errno));
  82. return 1;
  83. }
  84. printf("Visit http://%s:%d in your browser.\n", FLAGS_address[0], FLAGS_port);
  85. struct MHD_Daemon* daemon;
  86. daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY
  87. #ifdef SK_DEBUG
  88. | MHD_USE_DEBUG
  89. #endif
  90. , FLAGS_port, nullptr, nullptr,
  91. &answer_to_connection, &request,
  92. MHD_OPTION_SOCK_ADDR, &address,
  93. MHD_OPTION_END);
  94. if (nullptr == daemon) {
  95. SkDebugf("Could not initialize daemon\n");
  96. return 1;
  97. }
  98. if (FLAGS_hosted) {
  99. while (1) {
  100. SkDebugf("loop\n");
  101. #if defined(SK_BUILD_FOR_WIN)
  102. Sleep(60 * 1000);
  103. #else
  104. sleep(60);
  105. #endif
  106. }
  107. } else {
  108. getchar();
  109. }
  110. MHD_stop_daemon(daemon);
  111. return 0;
  112. }
  113. #if !defined SK_BUILD_FOR_IOS
  114. int main(int argc, char** argv) {
  115. CommandLineFlags::Parse(argc, argv);
  116. return skiaserve_main();
  117. }
  118. #endif