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