| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrCaps.h" | 8 #include "GrCaps.h" |
| 9 #include "GrContextFactory.h" | 9 #include "GrContextFactory.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 #include <sys/socket.h> | 23 #include <sys/socket.h> |
| 24 #include <microhttpd.h> | 24 #include <microhttpd.h> |
| 25 | 25 |
| 26 // To get image decoders linked in we have to do the below magic | 26 // To get image decoders linked in we have to do the below magic |
| 27 #include "SkForceLinking.h" | 27 #include "SkForceLinking.h" |
| 28 #include "SkImageDecoder.h" | 28 #include "SkImageDecoder.h" |
| 29 __SK_FORCE_IMAGE_DECODER_LINKING; | 29 __SK_FORCE_IMAGE_DECODER_LINKING; |
| 30 | 30 |
| 31 DEFINE_string(source, "https://debugger.skia.org", "Where to load the web UI fro
m."); | 31 DEFINE_string(source, "https://debugger.skia.org", "Where to load the web UI fro
m."); |
| 32 DEFINE_string(faviconDir, "tools/skiaserve", "The directory of the favicon"); |
| 32 DEFINE_int32(port, 8888, "The port to listen on."); | 33 DEFINE_int32(port, 8888, "The port to listen on."); |
| 33 | 34 |
| 34 // TODO probably want to make this configurable | 35 // TODO probably want to make this configurable |
| 35 static const int kImageWidth = 1920; | 36 static const int kImageWidth = 1920; |
| 36 static const int kImageHeight = 1080; | 37 static const int kImageHeight = 1080; |
| 37 | 38 |
| 38 // TODO move to template file | 39 // TODO move to template file |
| 39 SkString generateTemplate(SkString source) { | 40 SkString generateTemplate(SkString source) { |
| 40 SkString debuggerTemplate; | 41 SkString debuggerTemplate; |
| 41 debuggerTemplate.appendf( | 42 debuggerTemplate.appendf( |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 SkAutoTUnref<UrlDataManager::UrlData> urlData( | 437 SkAutoTUnref<UrlDataManager::UrlData> urlData( |
| 437 SkRef(request->fUrlDataManager.getDataFromUrl(SkString(url)))); | 438 SkRef(request->fUrlDataManager.getDataFromUrl(SkString(url)))); |
| 438 | 439 |
| 439 if (urlData) { | 440 if (urlData) { |
| 440 return SendData(connection, urlData->fData.get(), urlData->fContentT
ype.c_str()); | 441 return SendData(connection, urlData->fData.get(), urlData->fContentT
ype.c_str()); |
| 441 } | 442 } |
| 442 return MHD_NO; | 443 return MHD_NO; |
| 443 } | 444 } |
| 444 }; | 445 }; |
| 445 | 446 |
| 447 class FaviconHandler : public UrlHandler { |
| 448 public: |
| 449 bool canHandle(const char* method, const char* url) override { |
| 450 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && |
| 451 0 == strcmp(url, "/favicon.ico"); |
| 452 } |
| 453 |
| 454 int handle(Request* request, MHD_Connection* connection, |
| 455 const char* url, const char* method, |
| 456 const char* upload_data, size_t* upload_data_size) override { |
| 457 SkString dir(FLAGS_faviconDir[0]); |
| 458 dir.append("/favicon.ico"); |
| 459 FILE* ico = fopen(dir.c_str(), "r"); |
| 460 |
| 461 SkAutoTUnref<SkData> data(SkData::NewFromFILE(ico)); |
| 462 int ret = SendData(connection, data, "image/vnd.microsoft.icon"); |
| 463 fclose(ico); |
| 464 return ret; |
| 465 } |
| 466 }; |
| 467 |
| 468 |
| 446 class RootHandler : public UrlHandler { | 469 class RootHandler : public UrlHandler { |
| 447 public: | 470 public: |
| 448 bool canHandle(const char* method, const char* url) override { | 471 bool canHandle(const char* method, const char* url) override { |
| 449 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && | 472 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && |
| 450 0 == strcmp(url, "/"); | 473 0 == strcmp(url, "/"); |
| 451 } | 474 } |
| 452 | 475 |
| 453 int handle(Request* request, MHD_Connection* connection, | 476 int handle(Request* request, MHD_Connection* connection, |
| 454 const char* url, const char* method, | 477 const char* url, const char* method, |
| 455 const char* upload_data, size_t* upload_data_size) override { | 478 const char* upload_data, size_t* upload_data_size) override { |
| 456 return SendTemplate(connection); | 479 return SendTemplate(connection); |
| 457 } | 480 } |
| 458 }; | 481 }; |
| 459 | 482 |
| 460 class UrlManager { | 483 class UrlManager { |
| 461 public: | 484 public: |
| 462 UrlManager() { | 485 UrlManager() { |
| 463 // Register handlers | 486 // Register handlers |
| 464 fHandlers.push_back(new RootHandler); | 487 fHandlers.push_back(new RootHandler); |
| 465 fHandlers.push_back(new PostHandler); | 488 fHandlers.push_back(new PostHandler); |
| 466 fHandlers.push_back(new ImgHandler); | 489 fHandlers.push_back(new ImgHandler); |
| 467 fHandlers.push_back(new CmdHandler); | 490 fHandlers.push_back(new CmdHandler); |
| 468 fHandlers.push_back(new InfoHandler); | 491 fHandlers.push_back(new InfoHandler); |
| 469 fHandlers.push_back(new DownloadHandler); | 492 fHandlers.push_back(new DownloadHandler); |
| 470 fHandlers.push_back(new DataHandler); | 493 fHandlers.push_back(new DataHandler); |
| 494 fHandlers.push_back(new FaviconHandler); |
| 471 } | 495 } |
| 472 | 496 |
| 473 ~UrlManager() { | 497 ~UrlManager() { |
| 474 for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; } | 498 for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; } |
| 475 } | 499 } |
| 476 | 500 |
| 477 // This is clearly not efficient for a large number of urls and handlers | 501 // This is clearly not efficient for a large number of urls and handlers |
| 478 int invoke(Request* request, MHD_Connection* connection, const char* url, co
nst char* method, | 502 int invoke(Request* request, MHD_Connection* connection, const char* url, co
nst char* method, |
| 479 const char* upload_data, size_t* upload_data_size) const { | 503 const char* upload_data, size_t* upload_data_size) const { |
| 480 for (int i = 0; i < fHandlers.count(); i++) { | 504 for (int i = 0; i < fHandlers.count(); i++) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 495 int answer_to_connection(void* cls, struct MHD_Connection* connection, | 519 int answer_to_connection(void* cls, struct MHD_Connection* connection, |
| 496 const char* url, const char* method, const char* versio
n, | 520 const char* url, const char* method, const char* versio
n, |
| 497 const char* upload_data, size_t* upload_data_size, | 521 const char* upload_data, size_t* upload_data_size, |
| 498 void** con_cls) { | 522 void** con_cls) { |
| 499 SkDebugf("New %s request for %s using version %s\n", method, url, version); | 523 SkDebugf("New %s request for %s using version %s\n", method, url, version); |
| 500 | 524 |
| 501 Request* request = reinterpret_cast<Request*>(cls); | 525 Request* request = reinterpret_cast<Request*>(cls); |
| 502 int result = kUrlManager.invoke(request, connection, url, method, upload_dat
a, | 526 int result = kUrlManager.invoke(request, connection, url, method, upload_dat
a, |
| 503 upload_data_size); | 527 upload_data_size); |
| 504 if (MHD_NO == result) { | 528 if (MHD_NO == result) { |
| 505 fprintf(stderr, "Invalid method and / or url: %s %s)\n", method, url); | 529 fprintf(stderr, "Invalid method and / or url: %s %s\n", method, url); |
| 506 } | 530 } |
| 507 return result; | 531 return result; |
| 508 } | 532 } |
| 509 | 533 |
| 510 int skiaserve_main() { | 534 int skiaserve_main() { |
| 511 Request request(SkString("/data")); // This simple server has one request | 535 Request request(SkString("/data")); // This simple server has one request |
| 512 struct MHD_Daemon* daemon; | 536 struct MHD_Daemon* daemon; |
| 513 // TODO Add option to bind this strictly to an address, e.g. localhost, for
security. | 537 // TODO Add option to bind this strictly to an address, e.g. localhost, for
security. |
| 514 daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, FLAGS_port, nullptr, nu
llptr, | 538 daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, FLAGS_port, nullptr, nu
llptr, |
| 515 &answer_to_connection, &request, | 539 &answer_to_connection, &request, |
| 516 MHD_OPTION_END); | 540 MHD_OPTION_END); |
| 517 if (NULL == daemon) { | 541 if (NULL == daemon) { |
| 518 return 1; | 542 return 1; |
| 519 } | 543 } |
| 520 | 544 |
| 521 getchar(); | 545 getchar(); |
| 522 MHD_stop_daemon(daemon); | 546 MHD_stop_daemon(daemon); |
| 523 return 0; | 547 return 0; |
| 524 } | 548 } |
| 525 | 549 |
| 526 #if !defined SK_BUILD_FOR_IOS | 550 #if !defined SK_BUILD_FOR_IOS |
| 527 int main(int argc, char** argv) { | 551 int main(int argc, char** argv) { |
| 528 SkCommandLineFlags::Parse(argc, argv); | 552 SkCommandLineFlags::Parse(argc, argv); |
| 529 return skiaserve_main(); | 553 return skiaserve_main(); |
| 530 } | 554 } |
| 531 #endif | 555 #endif |
| OLD | NEW |