Response.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/Response.h"
  8. #include "tools/skiaserve/Request.h"
  9. #include "include/core/SkData.h"
  10. #include "include/core/SkString.h"
  11. #include "tools/flags/CommandLineFlags.h"
  12. #include "microhttpd.h"
  13. static DEFINE_string(source, "https://debugger-assets.skia.org", "Where to load the web UI from.");
  14. static SkString generate_template(SkString source) {
  15. SkString debuggerTemplate;
  16. debuggerTemplate.appendf(
  17. "<!DOCTYPE html>\n"
  18. "<html>\n"
  19. "<head>\n"
  20. " <title>SkDebugger</title>\n"
  21. " <meta charset='utf-8' />\n"
  22. " <meta http-equiv='X-UA-Compatible' content='IE=egde,chrome=1'>\n"
  23. " <meta name='viewport' content='width=device-width, initial-scale=1.0'>\n"
  24. " <script src='%s/res/js/core.js' type='text/javascript' charset='utf-8'></script>\n"
  25. " <link href='%s/res/vul/elements.html' rel='import' />\n"
  26. " <link rel='shortcut icon' href='%s/res/img/favicon.ico' type='image/x-icon'/ >"
  27. "</head>\n"
  28. "<body class='fullbleed layout vertical'>\n"
  29. " <debugger-app-sk>This is the app."
  30. " </debugger-app-sk>\n"
  31. "</body>\n"
  32. "</html>", source.c_str(), source.c_str(), source.c_str());
  33. return debuggerTemplate;
  34. }
  35. namespace Response {
  36. // SendOK just sends an empty response with a 200 OK status code.
  37. int SendOK(MHD_Connection* connection) {
  38. const char* data = "";
  39. MHD_Response* response = MHD_create_response_from_buffer(strlen(data),
  40. (void*)data,
  41. MHD_RESPMEM_PERSISTENT);
  42. int ret = MHD_queue_response(connection, 200, response);
  43. MHD_destroy_response(response);
  44. return ret;
  45. }
  46. int SendError(MHD_Connection* connection, const char* msg) {
  47. MHD_Response* response = MHD_create_response_from_buffer(strlen(msg),
  48. (void*) msg,
  49. MHD_RESPMEM_PERSISTENT);
  50. int ret = MHD_queue_response(connection, 500, response);
  51. MHD_destroy_response(response);
  52. return ret;
  53. }
  54. int SendData(MHD_Connection* connection, const SkData* data, const char* type,
  55. bool setContentDisposition, const char* dispositionString) {
  56. MHD_Response* response = MHD_create_response_from_buffer(data->size(),
  57. const_cast<void*>(data->data()),
  58. MHD_RESPMEM_MUST_COPY);
  59. MHD_add_response_header(response, "Content-Type", type);
  60. if (setContentDisposition) {
  61. MHD_add_response_header(response, "Content-Disposition", dispositionString);
  62. }
  63. int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
  64. MHD_destroy_response(response);
  65. return ret;
  66. }
  67. int SendTemplate(MHD_Connection* connection, bool redirect, const char* redirectUrl) {
  68. SkString debuggerTemplate = generate_template(SkString(FLAGS_source[0]));
  69. MHD_Response* response = MHD_create_response_from_buffer(
  70. debuggerTemplate.size(),
  71. (void*) const_cast<char*>(debuggerTemplate.c_str()),
  72. MHD_RESPMEM_MUST_COPY);
  73. MHD_add_response_header (response, "Access-Control-Allow-Origin", "*");
  74. int status = MHD_HTTP_OK;
  75. if (redirect) {
  76. MHD_add_response_header (response, "Location", redirectUrl);
  77. status = MHD_HTTP_SEE_OTHER;
  78. }
  79. int ret = MHD_queue_response(connection, status, response);
  80. MHD_destroy_response(response);
  81. return ret;
  82. }
  83. } // namespace Response