blob: 195544e83af5dcddf6c841310584c096f10e96ab [file] [log] [blame]
joshualitt7f6a1e02016-01-22 11:21:43 -08001/*
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
joshualitt24dd6872016-02-25 08:37:54 -08008#include "Request.h"
joshualitt26cc3f52016-02-25 11:09:36 -08009#include "Response.h"
joshualitt24dd6872016-02-25 08:37:54 -080010
joshualitt7f6a1e02016-01-22 11:21:43 -080011#include "SkCommandLineFlags.h"
joshualittcdad12f2016-02-08 07:08:21 -080012
joshualitt3854f112016-02-25 11:28:18 -080013#include "microhttpd.h"
14
15#include "urlhandlers/UrlHandler.h"
joshualitt7f6a1e02016-01-22 11:21:43 -080016#include <sys/socket.h>
joshualitt7f6a1e02016-01-22 11:21:43 -080017
joshualitt26cc3f52016-02-25 11:09:36 -080018using namespace Response;
19
joshualitt7f6a1e02016-01-22 11:21:43 -080020// To get image decoders linked in we have to do the below magic
21#include "SkForceLinking.h"
22#include "SkImageDecoder.h"
23__SK_FORCE_IMAGE_DECODER_LINKING;
24
jcgregorio21ab1202016-01-28 06:24:19 -080025DEFINE_int32(port, 8888, "The port to listen on.");
joshualitt7f6a1e02016-01-22 11:21:43 -080026
joshualittccfdaa52016-01-27 07:40:29 -080027class UrlManager {
28public:
29 UrlManager() {
30 // Register handlers
joshualitt483b9012016-02-02 07:16:24 -080031 fHandlers.push_back(new RootHandler);
32 fHandlers.push_back(new PostHandler);
33 fHandlers.push_back(new ImgHandler);
ethannicholas0a0520a2016-02-12 12:06:53 -080034 fHandlers.push_back(new ClipAlphaHandler);
ethannicholas85fca852016-02-19 08:40:59 -080035 fHandlers.push_back(new EnableGPUHandler);
joshualitt29e5a892016-02-04 06:08:33 -080036 fHandlers.push_back(new CmdHandler);
joshualitt483b9012016-02-02 07:16:24 -080037 fHandlers.push_back(new InfoHandler);
joshualitt792345f2016-02-02 13:02:33 -080038 fHandlers.push_back(new DownloadHandler);
joshualittcdad12f2016-02-08 07:08:21 -080039 fHandlers.push_back(new DataHandler);
ethannicholas3ca1e1a2016-02-18 10:22:34 -080040 fHandlers.push_back(new BreakHandler);
joshualitt1e5884b2016-02-26 08:22:49 -080041 fHandlers.push_back(new BatchesHandler);
joshualitt10d8fc22016-02-29 11:15:06 -080042 fHandlers.push_back(new BatchBoundsHandler);
joshualitt483b9012016-02-02 07:16:24 -080043 }
44
45 ~UrlManager() {
46 for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; }
joshualittccfdaa52016-01-27 07:40:29 -080047 }
48
49 // This is clearly not efficient for a large number of urls and handlers
50 int invoke(Request* request, MHD_Connection* connection, const char* url, const char* method,
51 const char* upload_data, size_t* upload_data_size) const {
52 for (int i = 0; i < fHandlers.count(); i++) {
joshualitt483b9012016-02-02 07:16:24 -080053 if (fHandlers[i]->canHandle(method, url)) {
joshualitt136f5172016-02-02 11:07:39 -080054 return fHandlers[i]->handle(request, connection, url, method, upload_data,
55 upload_data_size);
joshualittccfdaa52016-01-27 07:40:29 -080056 }
57 }
58 return MHD_NO;
59 }
60
61private:
joshualitt483b9012016-02-02 07:16:24 -080062 SkTArray<UrlHandler*> fHandlers;
joshualittccfdaa52016-01-27 07:40:29 -080063};
64
65const UrlManager kUrlManager;
66
joshualitt7f6a1e02016-01-22 11:21:43 -080067int answer_to_connection(void* cls, struct MHD_Connection* connection,
68 const char* url, const char* method, const char* version,
69 const char* upload_data, size_t* upload_data_size,
70 void** con_cls) {
joshualitt9a4e1882016-01-27 07:03:29 -080071 SkDebugf("New %s request for %s using version %s\n", method, url, version);
joshualitt7f6a1e02016-01-22 11:21:43 -080072
joshualitt9a4e1882016-01-27 07:03:29 -080073 Request* request = reinterpret_cast<Request*>(cls);
joshualitt483b9012016-02-02 07:16:24 -080074 int result = kUrlManager.invoke(request, connection, url, method, upload_data,
75 upload_data_size);
76 if (MHD_NO == result) {
joshualitt873d6242016-02-08 13:57:44 -080077 fprintf(stderr, "Invalid method and / or url: %s %s\n", method, url);
joshualitt483b9012016-02-02 07:16:24 -080078 }
79 return result;
joshualitt7f6a1e02016-01-22 11:21:43 -080080}
81
82int skiaserve_main() {
joshualittcdad12f2016-02-08 07:08:21 -080083 Request request(SkString("/data")); // This simple server has one request
ethannicholas85fca852016-02-19 08:40:59 -080084
joshualitt7f6a1e02016-01-22 11:21:43 -080085 struct MHD_Daemon* daemon;
jcgregorio21ab1202016-01-28 06:24:19 -080086 // TODO Add option to bind this strictly to an address, e.g. localhost, for security.
87 daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, FLAGS_port, nullptr, nullptr,
joshualitt9a4e1882016-01-27 07:03:29 -080088 &answer_to_connection, &request,
89 MHD_OPTION_END);
joshualitt7f6a1e02016-01-22 11:21:43 -080090 if (NULL == daemon) {
91 return 1;
92 }
93
94 getchar();
95 MHD_stop_daemon(daemon);
96 return 0;
97}
98
99#if !defined SK_BUILD_FOR_IOS
100int main(int argc, char** argv) {
101 SkCommandLineFlags::Parse(argc, argv);
102 return skiaserve_main();
103}
104#endif