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/Request.h"
9 #include "tools/skiaserve/Response.h"
10
11 #include "include/core/SkGraphics.h"
12 #include "tools/flags/CommandLineFlags.h"
13
14 #include "tools/skiaserve/urlhandlers/UrlHandler.h"
15
16 #include "microhttpd.h"
17
18 #include <errno.h>
19
20 #if !defined _WIN32
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #endif
24
25 using namespace skia_private;
26 using namespace Response;
27
28 static DEFINE_int(port, 8888, "The port to listen on.");
29 static DEFINE_string(address, "127.0.0.1", "The address to bind to.");
30 static DEFINE_bool(hosted, false, "Running in hosted mode on debugger.skia.org.");
31
32 class UrlManager {
33 public:
UrlManager()34 UrlManager() {
35 // Register handlers
36 fHandlers.push_back(new RootHandler);
37 fHandlers.push_back(new PostHandler);
38 fHandlers.push_back(new ImgHandler);
39 fHandlers.push_back(new ClipAlphaHandler);
40 fHandlers.push_back(new EnableGPUHandler);
41 fHandlers.push_back(new CmdHandler);
42 fHandlers.push_back(new InfoHandler);
43 fHandlers.push_back(new DownloadHandler);
44 fHandlers.push_back(new DataHandler);
45 fHandlers.push_back(new BreakHandler);
46 fHandlers.push_back(new OpsHandler);
47 fHandlers.push_back(new OpBoundsHandler);
48 fHandlers.push_back(new ColorModeHandler);
49 fHandlers.push_back(new QuitHandler);
50 }
51
~UrlManager()52 ~UrlManager() {
53 for (int i = 0; i < fHandlers.size(); i++) { delete fHandlers[i]; }
54 }
55
56 // This is clearly not efficient for a large number of urls and handlers
invoke(Request * request,MHD_Connection * connection,const char * url,const char * method,const char * upload_data,size_t * upload_data_size) const57 int invoke(Request* request, MHD_Connection* connection, const char* url, const char* method,
58 const char* upload_data, size_t* upload_data_size) const {
59 for (int i = 0; i < fHandlers.size(); i++) {
60 if (fHandlers[i]->canHandle(method, url)) {
61 return fHandlers[i]->handle(request, connection, url, method, upload_data,
62 upload_data_size);
63 }
64 }
65 return MHD_NO;
66 }
67
68 private:
69 TArray<UrlHandler*> fHandlers;
70 };
71
72 const UrlManager kUrlManager;
73
answer_to_connection(void * cls,struct MHD_Connection * connection,const char * url,const char * method,const char * version,const char * upload_data,size_t * upload_data_size,void ** con_cls)74 int answer_to_connection(void* cls, struct MHD_Connection* connection,
75 const char* url, const char* method, const char* version,
76 const char* upload_data, size_t* upload_data_size,
77 void** con_cls) {
78 SkDebugf("New %s request for %s using version %s\n", method, url, version);
79
80 Request* request = reinterpret_cast<Request*>(cls);
81 int result = kUrlManager.invoke(request, connection, url, method, upload_data,
82 upload_data_size);
83 if (MHD_NO == result) {
84 fprintf(stderr, "Invalid method and / or url: %s %s\n", method, url);
85 }
86 return result;
87 }
88
skiaserve_main()89 int skiaserve_main() {
90 SkGraphics::Init();
91 Request request(SkString("/data")); // This simple server has one request
92
93 struct sockaddr_in address;
94 address.sin_family = AF_INET;
95 address.sin_port = htons(FLAGS_port);
96 int result = inet_pton(AF_INET, FLAGS_address[0], &address.sin_addr);
97 if (result != 1) {
98 printf("inet_pton for %s:%d failed with return %d %s\n",
99 FLAGS_address[0], FLAGS_port, result, strerror(errno));
100 return 1;
101 }
102
103 printf("Visit http://%s:%d in your browser.\n", FLAGS_address[0], FLAGS_port);
104
105 struct MHD_Daemon* daemon;
106 daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY
107 #ifdef SK_DEBUG
108 | MHD_USE_DEBUG
109 #endif
110 , FLAGS_port, nullptr, nullptr,
111 &answer_to_connection, &request,
112 MHD_OPTION_SOCK_ADDR, &address,
113 MHD_OPTION_END);
114 if (nullptr == daemon) {
115 SkDebugf("Could not initialize daemon\n");
116 return 1;
117 }
118
119 if (FLAGS_hosted) {
120 while (1) {
121 SkDebugf("loop\n");
122 #if defined(SK_BUILD_FOR_WIN)
123 Sleep(60 * 1000);
124 #else
125 sleep(60);
126 #endif
127 }
128 } else {
129 getchar();
130 }
131 MHD_stop_daemon(daemon);
132 return 0;
133 }
134
135 #if !defined SK_BUILD_FOR_IOS
main(int argc,char ** argv)136 int main(int argc, char** argv) {
137 CommandLineFlags::Parse(argc, argv);
138 return skiaserve_main();
139 }
140 #endif
141