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 SendTemplate(MHD_Connection* connection, bool redirect, const char* redirectUrl) { 81 SkString debuggerTemplate = generate_template(SkString(FLAGS_source[0])); 82 83 MHD_Response* response = MHD_create_response_from_buffer( 84 debuggerTemplate.size(), 85 (void*) const_cast<char*>(debuggerTemplate.c_str()), 86 MHD_RESPMEM_MUST_COPY); 87 MHD_add_response_header (response, "Access-Control-Allow-Origin", "*"); 88 89 int status = MHD_HTTP_OK; 90 91 if (redirect) { 92 MHD_add_response_header (response, "Location", redirectUrl); 93 status = MHD_HTTP_SEE_OTHER; 94 } 95 96 int ret = MHD_queue_response(connection, status, response); 97 MHD_destroy_response(response); 98 return ret; 99 } 100 101 } // namespace Response 102