joshualitt | 26cc3f5 | 2016-02-25 11:09:36 -0800 | [diff] [blame^] | 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 | |
| 8 | #include "Response.h" |
| 9 | |
| 10 | #include "microhttpd.h" |
| 11 | |
| 12 | #include "Request.h" |
| 13 | |
| 14 | #include "SkCommandLineFlags.h" |
| 15 | #include "SkData.h" |
| 16 | #include "SkString.h" |
| 17 | |
| 18 | DEFINE_string(source, "https://debugger.skia.org", "Where to load the web UI from."); |
| 19 | |
| 20 | static SkString generate_template(SkString source) { |
| 21 | SkString debuggerTemplate; |
| 22 | debuggerTemplate.appendf( |
| 23 | "<!DOCTYPE html>\n" |
| 24 | "<html>\n" |
| 25 | "<head>\n" |
| 26 | " <title>SkDebugger</title>\n" |
| 27 | " <meta charset=\"utf-8\" />\n" |
| 28 | " <meta http-equiv=\"X-UA-Compatible\" content=\"IE=egde,chrome=1\">\n" |
| 29 | " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" |
| 30 | " <script src=\"%s/res/js/core.js\" type=\"text/javascript\" charset=\"utf-8\"></script>\n" |
| 31 | " <link href=\"%s/res/vul/elements.html\" rel=\"import\" />\n" |
| 32 | " <link rel='shortcut icon' href='https://debugger.skia.org/res/img/favicon.ico' type='image/x-icon'/ >" |
| 33 | "</head>\n" |
| 34 | "<body class=\"fullbleed layout vertical\">\n" |
| 35 | " <debugger-app-sk>This is the app." |
| 36 | " </debugger-app-sk>\n" |
| 37 | "</body>\n" |
| 38 | "</html>", source.c_str(), source.c_str()); |
| 39 | return debuggerTemplate; |
| 40 | } |
| 41 | |
| 42 | namespace Response { |
| 43 | // SendOK just sends an empty response with a 200 OK status code. |
| 44 | int SendOK(MHD_Connection* connection) { |
| 45 | const char* data = ""; |
| 46 | |
| 47 | MHD_Response* response = MHD_create_response_from_buffer(strlen(data), |
| 48 | (void*)data, |
| 49 | MHD_RESPMEM_PERSISTENT); |
| 50 | int ret = MHD_queue_response(connection, 200, response); |
| 51 | MHD_destroy_response(response); |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | int SendError(MHD_Connection* connection, const char* msg) { |
| 56 | MHD_Response* response = MHD_create_response_from_buffer(strlen(msg), |
| 57 | (void*) msg, |
| 58 | MHD_RESPMEM_PERSISTENT); |
| 59 | int ret = MHD_queue_response(connection, 500, response); |
| 60 | MHD_destroy_response(response); |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | int SendData(MHD_Connection* connection, const SkData* data, const char* type, |
| 65 | bool setContentDisposition, const char* dispositionString) { |
| 66 | MHD_Response* response = MHD_create_response_from_buffer(data->size(), |
| 67 | const_cast<void*>(data->data()), |
| 68 | MHD_RESPMEM_MUST_COPY); |
| 69 | MHD_add_response_header(response, "Content-Type", type); |
| 70 | |
| 71 | if (setContentDisposition) { |
| 72 | MHD_add_response_header(response, "Content-Disposition", dispositionString); |
| 73 | } |
| 74 | |
| 75 | int ret = MHD_queue_response(connection, MHD_HTTP_OK, response); |
| 76 | MHD_destroy_response(response); |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | int SendJSON(MHD_Connection* connection, Request* request, int n) { |
| 81 | SkCanvas* canvas = request->getCanvas(); |
| 82 | SkDebugCanvas* debugCanvas = request->fDebugCanvas; |
| 83 | UrlDataManager* urlDataManager = &request->fUrlDataManager; |
| 84 | Json::Value root = debugCanvas->toJSON(*urlDataManager, n, canvas); |
| 85 | root["mode"] = Json::Value(request->fGPUEnabled ? "gpu" : "cpu"); |
| 86 | SkDynamicMemoryWStream stream; |
| 87 | stream.writeText(Json::FastWriter().write(root).c_str()); |
| 88 | |
| 89 | SkAutoTUnref<SkData> data(stream.copyToData()); |
| 90 | return SendData(connection, data, "application/json"); |
| 91 | } |
| 92 | |
| 93 | int SendTemplate(MHD_Connection* connection, bool redirect, const char* redirectUrl) { |
| 94 | SkString debuggerTemplate = generate_template(SkString(FLAGS_source[0])); |
| 95 | |
| 96 | MHD_Response* response = MHD_create_response_from_buffer( |
| 97 | debuggerTemplate.size(), |
| 98 | (void*) const_cast<char*>(debuggerTemplate.c_str()), |
| 99 | MHD_RESPMEM_MUST_COPY); |
| 100 | MHD_add_response_header (response, "Access-Control-Allow-Origin", "*"); |
| 101 | |
| 102 | int status = MHD_HTTP_OK; |
| 103 | |
| 104 | if (redirect) { |
| 105 | MHD_add_response_header (response, "Location", redirectUrl); |
| 106 | status = MHD_HTTP_SEE_OTHER; |
| 107 | } |
| 108 | |
| 109 | int ret = MHD_queue_response(connection, status, response); |
| 110 | MHD_destroy_response(response); |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | } // namespace Response |