From 26cc3f5ad0aacf80bba162bab4b23a2901ffed17 Mon Sep 17 00:00:00 2001 From: joshualitt Date: Thu, 25 Feb 2016 11:09:36 -0800 Subject: Create Response namespace for skiaserve response functions BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1739763002 Review URL: https://codereview.chromium.org/1739763002 --- tools/skiaserve/Response.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++ tools/skiaserve/Response.h | 30 +++++++++++ tools/skiaserve/skiaserve.cpp | 99 ++---------------------------------- 3 files changed, 147 insertions(+), 96 deletions(-) create mode 100644 tools/skiaserve/Response.cpp create mode 100644 tools/skiaserve/Response.h (limited to 'tools/skiaserve') diff --git a/tools/skiaserve/Response.cpp b/tools/skiaserve/Response.cpp new file mode 100644 index 0000000000..16cb3b6e3d --- /dev/null +++ b/tools/skiaserve/Response.cpp @@ -0,0 +1,114 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "Response.h" + +#include "microhttpd.h" + +#include "Request.h" + +#include "SkCommandLineFlags.h" +#include "SkData.h" +#include "SkString.h" + +DEFINE_string(source, "https://debugger.skia.org", "Where to load the web UI from."); + +static SkString generate_template(SkString source) { + SkString debuggerTemplate; + debuggerTemplate.appendf( + "\n" + "\n" + "\n" + " SkDebugger\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " " + "\n" + "\n" + " This is the app." + " \n" + "\n" + "", source.c_str(), source.c_str()); + return debuggerTemplate; +} + +namespace Response { +// SendOK just sends an empty response with a 200 OK status code. +int SendOK(MHD_Connection* connection) { + const char* data = ""; + + MHD_Response* response = MHD_create_response_from_buffer(strlen(data), + (void*)data, + MHD_RESPMEM_PERSISTENT); + int ret = MHD_queue_response(connection, 200, response); + MHD_destroy_response(response); + return ret; +} + +int SendError(MHD_Connection* connection, const char* msg) { + MHD_Response* response = MHD_create_response_from_buffer(strlen(msg), + (void*) msg, + MHD_RESPMEM_PERSISTENT); + int ret = MHD_queue_response(connection, 500, response); + MHD_destroy_response(response); + return ret; +} + +int SendData(MHD_Connection* connection, const SkData* data, const char* type, + bool setContentDisposition, const char* dispositionString) { + MHD_Response* response = MHD_create_response_from_buffer(data->size(), + const_cast(data->data()), + MHD_RESPMEM_MUST_COPY); + MHD_add_response_header(response, "Content-Type", type); + + if (setContentDisposition) { + MHD_add_response_header(response, "Content-Disposition", dispositionString); + } + + int ret = MHD_queue_response(connection, MHD_HTTP_OK, response); + MHD_destroy_response(response); + return ret; +} + +int SendJSON(MHD_Connection* connection, Request* request, int n) { + SkCanvas* canvas = request->getCanvas(); + SkDebugCanvas* debugCanvas = request->fDebugCanvas; + UrlDataManager* urlDataManager = &request->fUrlDataManager; + Json::Value root = debugCanvas->toJSON(*urlDataManager, n, canvas); + root["mode"] = Json::Value(request->fGPUEnabled ? "gpu" : "cpu"); + SkDynamicMemoryWStream stream; + stream.writeText(Json::FastWriter().write(root).c_str()); + + SkAutoTUnref data(stream.copyToData()); + return SendData(connection, data, "application/json"); +} + +int SendTemplate(MHD_Connection* connection, bool redirect, const char* redirectUrl) { + SkString debuggerTemplate = generate_template(SkString(FLAGS_source[0])); + + MHD_Response* response = MHD_create_response_from_buffer( + debuggerTemplate.size(), + (void*) const_cast(debuggerTemplate.c_str()), + MHD_RESPMEM_MUST_COPY); + MHD_add_response_header (response, "Access-Control-Allow-Origin", "*"); + + int status = MHD_HTTP_OK; + + if (redirect) { + MHD_add_response_header (response, "Location", redirectUrl); + status = MHD_HTTP_SEE_OTHER; + } + + int ret = MHD_queue_response(connection, status, response); + MHD_destroy_response(response); + return ret; +} + +} // namespace Response diff --git a/tools/skiaserve/Response.h b/tools/skiaserve/Response.h new file mode 100644 index 0000000000..ef115e9d23 --- /dev/null +++ b/tools/skiaserve/Response.h @@ -0,0 +1,30 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef Reponse_DEFINED +#define Reponse_DEFINED + +struct MHD_Connection; +struct Request; +class SkData; + +namespace Response { + // SendOK just sends an empty response with a 200 OK status code. + int SendOK(MHD_Connection* connection); + + int SendError(MHD_Connection* connection, const char* msg); + + int SendData(MHD_Connection* connection, const SkData* data, const char* type, + bool setContentDisposition = false, const char* dispositionString = nullptr); + + int SendJSON(MHD_Connection* connection, Request* request, int n); + + int SendTemplate(MHD_Connection* connection, bool redirect = false, + const char* redirectUrl = nullptr); +} + +#endif diff --git a/tools/skiaserve/skiaserve.cpp b/tools/skiaserve/skiaserve.cpp index a76e2aa537..d5a0132d0d 100644 --- a/tools/skiaserve/skiaserve.cpp +++ b/tools/skiaserve/skiaserve.cpp @@ -9,6 +9,7 @@ #include "GrContextFactory.h" #include "Request.h" +#include "Response.h" #include "SkCanvas.h" #include "SkCommandLineFlags.h" @@ -19,37 +20,15 @@ #include #include +using namespace Response; + // To get image decoders linked in we have to do the below magic #include "SkForceLinking.h" #include "SkImageDecoder.h" __SK_FORCE_IMAGE_DECODER_LINKING; -DEFINE_string(source, "https://debugger.skia.org", "Where to load the web UI from."); DEFINE_int32(port, 8888, "The port to listen on."); -SkString generateTemplate(SkString source) { - SkString debuggerTemplate; - debuggerTemplate.appendf( - "\n" - "\n" - "\n" - " SkDebugger\n" - " \n" - " \n" - " \n" - " \n" - " \n" - " " - "\n" - "\n" - " This is the app." - " \n" - "\n" - "", source.c_str(), source.c_str()); - return debuggerTemplate; - -} - static const size_t kBufferSize = 1024; static int process_upload_data(void* cls, enum MHD_ValueKind kind, @@ -64,78 +43,6 @@ static int process_upload_data(void* cls, enum MHD_ValueKind kind, return MHD_YES; } -// SendOK just sends an empty response with a 200 OK status code. -static int SendOK(MHD_Connection* connection) { - const char* data = ""; - - MHD_Response* response = MHD_create_response_from_buffer(strlen(data), - (void*)data, - MHD_RESPMEM_PERSISTENT); - int ret = MHD_queue_response(connection, 200, response); - MHD_destroy_response(response); - return ret; -} - -static int SendError(MHD_Connection* connection, const char* msg) { - MHD_Response* response = MHD_create_response_from_buffer(strlen(msg), - (void*) msg, - MHD_RESPMEM_PERSISTENT); - int ret = MHD_queue_response(connection, 500, response); - MHD_destroy_response(response); - return ret; -} - -static int SendData(MHD_Connection* connection, const SkData* data, const char* type, - bool setContentDisposition = false, const char* dispositionString = nullptr) { - MHD_Response* response = MHD_create_response_from_buffer(data->size(), - const_cast(data->data()), - MHD_RESPMEM_MUST_COPY); - MHD_add_response_header(response, "Content-Type", type); - - if (setContentDisposition) { - MHD_add_response_header(response, "Content-Disposition", dispositionString); - } - - int ret = MHD_queue_response(connection, MHD_HTTP_OK, response); - MHD_destroy_response(response); - return ret; -} - -static int SendJSON(MHD_Connection* connection, Request* request, int n) { - SkCanvas* canvas = request->getCanvas(); - SkDebugCanvas* debugCanvas = request->fDebugCanvas; - UrlDataManager* urlDataManager = &request->fUrlDataManager; - Json::Value root = debugCanvas->toJSON(*urlDataManager, n, canvas); - root["mode"] = Json::Value(request->fGPUEnabled ? "gpu" : "cpu"); - SkDynamicMemoryWStream stream; - stream.writeText(Json::FastWriter().write(root).c_str()); - - SkAutoTUnref data(stream.copyToData()); - return SendData(connection, data, "application/json"); -} - -static int SendTemplate(MHD_Connection* connection, bool redirect = false, - const char* redirectUrl = nullptr) { - SkString debuggerTemplate = generateTemplate(SkString(FLAGS_source[0])); - - MHD_Response* response = MHD_create_response_from_buffer( - debuggerTemplate.size(), - (void*) const_cast(debuggerTemplate.c_str()), - MHD_RESPMEM_MUST_COPY); - MHD_add_response_header (response, "Access-Control-Allow-Origin", "*"); - - int status = MHD_HTTP_OK; - - if (redirect) { - MHD_add_response_header (response, "Location", redirectUrl); - status = MHD_HTTP_SEE_OTHER; - } - - int ret = MHD_queue_response(connection, status, response); - MHD_destroy_response(response); - return ret; -} - class UrlHandler { public: virtual ~UrlHandler() {} -- cgit v1.2.3