2016-01-22 19:21:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-02-25 16:37:54 +00:00
|
|
|
#include "Request.h"
|
2016-02-25 19:09:36 +00:00
|
|
|
#include "Response.h"
|
2016-02-25 16:37:54 +00:00
|
|
|
|
2016-01-22 19:21:43 +00:00
|
|
|
#include "SkCommandLineFlags.h"
|
2016-02-08 15:08:21 +00:00
|
|
|
|
2016-02-25 19:28:18 +00:00
|
|
|
#include "microhttpd.h"
|
|
|
|
|
|
|
|
#include "urlhandlers/UrlHandler.h"
|
2016-01-22 19:21:43 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2016-02-25 19:09:36 +00:00
|
|
|
using namespace Response;
|
|
|
|
|
2016-01-22 19:21:43 +00:00
|
|
|
// To get image decoders linked in we have to do the below magic
|
|
|
|
#include "SkForceLinking.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
__SK_FORCE_IMAGE_DECODER_LINKING;
|
|
|
|
|
2016-01-28 14:24:19 +00:00
|
|
|
DEFINE_int32(port, 8888, "The port to listen on.");
|
2016-01-22 19:21:43 +00:00
|
|
|
|
2016-01-27 15:40:29 +00:00
|
|
|
class UrlManager {
|
|
|
|
public:
|
|
|
|
UrlManager() {
|
|
|
|
// Register handlers
|
2016-02-02 15:16:24 +00:00
|
|
|
fHandlers.push_back(new RootHandler);
|
|
|
|
fHandlers.push_back(new PostHandler);
|
|
|
|
fHandlers.push_back(new ImgHandler);
|
2016-02-12 20:06:53 +00:00
|
|
|
fHandlers.push_back(new ClipAlphaHandler);
|
2016-02-19 16:40:59 +00:00
|
|
|
fHandlers.push_back(new EnableGPUHandler);
|
2016-02-04 14:08:33 +00:00
|
|
|
fHandlers.push_back(new CmdHandler);
|
2016-02-02 15:16:24 +00:00
|
|
|
fHandlers.push_back(new InfoHandler);
|
2016-02-02 21:02:33 +00:00
|
|
|
fHandlers.push_back(new DownloadHandler);
|
2016-02-08 15:08:21 +00:00
|
|
|
fHandlers.push_back(new DataHandler);
|
2016-02-18 18:22:34 +00:00
|
|
|
fHandlers.push_back(new BreakHandler);
|
2016-02-26 16:22:49 +00:00
|
|
|
fHandlers.push_back(new BatchesHandler);
|
2016-02-02 15:16:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~UrlManager() {
|
|
|
|
for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; }
|
2016-01-27 15:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is clearly not efficient for a large number of urls and handlers
|
|
|
|
int invoke(Request* request, MHD_Connection* connection, const char* url, const char* method,
|
|
|
|
const char* upload_data, size_t* upload_data_size) const {
|
|
|
|
for (int i = 0; i < fHandlers.count(); i++) {
|
2016-02-02 15:16:24 +00:00
|
|
|
if (fHandlers[i]->canHandle(method, url)) {
|
2016-02-02 19:07:39 +00:00
|
|
|
return fHandlers[i]->handle(request, connection, url, method, upload_data,
|
|
|
|
upload_data_size);
|
2016-01-27 15:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return MHD_NO;
|
2016-01-22 19:21:43 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 15:40:29 +00:00
|
|
|
private:
|
2016-02-02 15:16:24 +00:00
|
|
|
SkTArray<UrlHandler*> fHandlers;
|
2016-01-27 15:40:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const UrlManager kUrlManager;
|
|
|
|
|
|
|
|
int 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) {
|
|
|
|
SkDebugf("New %s request for %s using version %s\n", method, url, version);
|
|
|
|
|
|
|
|
Request* request = reinterpret_cast<Request*>(cls);
|
2016-02-02 15:16:24 +00:00
|
|
|
int result = kUrlManager.invoke(request, connection, url, method, upload_data,
|
|
|
|
upload_data_size);
|
|
|
|
if (MHD_NO == result) {
|
2016-02-08 21:57:44 +00:00
|
|
|
fprintf(stderr, "Invalid method and / or url: %s %s\n", method, url);
|
2016-02-02 15:16:24 +00:00
|
|
|
}
|
|
|
|
return result;
|
2016-01-22 19:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int skiaserve_main() {
|
2016-02-08 15:08:21 +00:00
|
|
|
Request request(SkString("/data")); // This simple server has one request
|
2016-02-19 16:40:59 +00:00
|
|
|
|
2016-01-22 19:21:43 +00:00
|
|
|
struct MHD_Daemon* daemon;
|
2016-01-28 14:24:19 +00:00
|
|
|
// TODO Add option to bind this strictly to an address, e.g. localhost, for security.
|
|
|
|
daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, FLAGS_port, nullptr, nullptr,
|
2016-01-27 15:03:29 +00:00
|
|
|
&answer_to_connection, &request,
|
|
|
|
MHD_OPTION_END);
|
2016-01-22 19:21:43 +00:00
|
|
|
if (NULL == daemon) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
getchar();
|
|
|
|
MHD_stop_daemon(daemon);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined SK_BUILD_FOR_IOS
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
SkCommandLineFlags::Parse(argc, argv);
|
|
|
|
return skiaserve_main();
|
|
|
|
}
|
|
|
|
#endif
|