xref: /aosp_15_r20/external/skia/tools/skiaserve/urlhandlers/ImgHandler.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "tools/skiaserve/urlhandlers/UrlHandler.h"
9 
10 #include "microhttpd.h"
11 #include "src/core/SkStringUtils.h"
12 #include "tools/skiaserve/Request.h"
13 #include "tools/skiaserve/Response.h"
14 
15 using namespace skia_private;
16 using namespace Response;
17 
canHandle(const char * method,const char * url)18 bool ImgHandler::canHandle(const char* method, const char* url) {
19     static const char* kBasePath = "/img";
20     return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
21            0 == strncmp(url, kBasePath, strlen(kBasePath));
22 }
23 
handle(Request * request,MHD_Connection * connection,const char * url,const char * method,const char * upload_data,size_t * upload_data_size)24 int ImgHandler::handle(Request* request, MHD_Connection* connection,
25                        const char* url, const char* method,
26                        const char* upload_data, size_t* upload_data_size) {
27     TArray<SkString> commands;
28     SkStrSplit(url, "/", &commands);
29 
30     if (!request->hasPicture() || commands.size() > 3) {
31         return MHD_NO;
32     }
33 
34     int n, m = -1;
35     // /img or /img/N
36     if (commands.size() == 1) {
37         n = request->fDebugCanvas->getSize() - 1;
38     } else if (commands.size() == 2) {
39         sscanf(commands[1].c_str(), "%d", &n);
40     } else {
41         sscanf(commands[1].c_str(), "%d", &n);
42         sscanf(commands[2].c_str(), "%d", &m);
43     }
44 
45     sk_sp<SkData> data(request->drawToPng(n, m));
46     return SendData(connection, data.get(), "image/png");
47 }
48