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-04-19 19:42:24 +00:00
|
|
|
#include "SkGraphics.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-03-08 21:24:33 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
2016-04-20 17:52:54 +00:00
|
|
|
|
|
|
|
#if !defined _WIN32
|
2016-01-22 19:21:43 +00:00
|
|
|
#include <sys/socket.h>
|
2016-03-02 17:03:01 +00:00
|
|
|
#include <arpa/inet.h>
|
2016-04-20 17:52:54 +00:00
|
|
|
#endif
|
2016-01-22 19:21:43 +00:00
|
|
|
|
2016-02-25 19:09:36 +00:00
|
|
|
using namespace Response;
|
|
|
|
|
2016-01-28 14:24:19 +00:00
|
|
|
DEFINE_int32(port, 8888, "The port to listen on.");
|
2016-03-08 21:24:33 +00:00
|
|
|
DEFINE_string(address, "127.0.0.1", "The address to bind to.");
|
2016-06-03 14:26:22 +00:00
|
|
|
DEFINE_bool(hosted, false, "Running in hosted mode on debugger.skia.org.");
|
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-12-20 21:48:59 +00:00
|
|
|
fHandlers.push_back(new OpsHandler);
|
|
|
|
fHandlers.push_back(new OpBoundsHandler);
|
2016-04-19 17:16:53 +00:00
|
|
|
fHandlers.push_back(new ColorModeHandler);
|
2016-05-31 19:51:31 +00:00
|
|
|
fHandlers.push_back(new QuitHandler);
|
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-04-19 19:42:24 +00:00
|
|
|
SkGraphics::Init();
|
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-03-02 17:03:01 +00:00
|
|
|
struct sockaddr_in address;
|
|
|
|
address.sin_family = AF_INET;
|
|
|
|
address.sin_port = htons(FLAGS_port);
|
2016-03-08 21:24:33 +00:00
|
|
|
int result = inet_pton(AF_INET, FLAGS_address[0], &address.sin_addr);
|
|
|
|
if (result != 1) {
|
2016-03-29 16:03:52 +00:00
|
|
|
printf("inet_pton for %s:%d failed with return %d %s\n",
|
2016-03-08 21:24:33 +00:00
|
|
|
FLAGS_address[0], FLAGS_port, result, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
2016-03-02 17:03:01 +00:00
|
|
|
|
|
|
|
printf("Visit http://%s:%d in your browser.\n", FLAGS_address[0], FLAGS_port);
|
|
|
|
|
2016-01-22 19:21:43 +00:00
|
|
|
struct MHD_Daemon* daemon;
|
2016-03-04 18:45:05 +00:00
|
|
|
daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
| MHD_USE_DEBUG
|
2016-03-29 16:03:52 +00:00
|
|
|
#endif
|
2016-03-04 18:45:05 +00:00
|
|
|
, FLAGS_port, nullptr, nullptr,
|
2016-01-27 15:03:29 +00:00
|
|
|
&answer_to_connection, &request,
|
2016-03-02 17:03:01 +00:00
|
|
|
MHD_OPTION_SOCK_ADDR, &address,
|
2016-01-27 15:03:29 +00:00
|
|
|
MHD_OPTION_END);
|
2016-01-22 19:21:43 +00:00
|
|
|
if (NULL == daemon) {
|
2016-03-04 18:45:05 +00:00
|
|
|
SkDebugf("Could not initialize daemon\n");
|
2016-01-22 19:21:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-03 14:26:22 +00:00
|
|
|
if (FLAGS_hosted) {
|
|
|
|
while (1) {
|
|
|
|
SkDebugf("loop\n");
|
|
|
|
#if defined(SK_BUILD_FOR_WIN)
|
|
|
|
Sleep(60 * 1000);
|
|
|
|
#else
|
|
|
|
sleep(60);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
getchar();
|
|
|
|
}
|
2016-01-22 19:21:43 +00:00
|
|
|
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
|