get_favicon_service.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2014 Baidu, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Authors: Zhangyi Chen (chenzhangyi01@baidu.com)
  15. #include "butil/macros.h" // ARRAY_SIZE
  16. #include "butil/iobuf.h" // butil::IOBuf
  17. #include "brpc/controller.h" // Controller
  18. #include "brpc/builtin/get_favicon_service.h"
  19. namespace brpc {
  20. static unsigned char s_favicon_array[] = {
  21. 71, 73, 70, 56, 57, 97, 16, 0, 16, 0, 241, 0, 0, 0, 0, 0, 153, 153, 153, 255,
  22. 255, 255, 0, 0, 0, 33, 249, 4, 9, 50, 0, 3, 0, 33, 255, 11, 78, 69, 84, 83, 67,
  23. 65, 80, 69, 50, 46, 48, 3, 1, 0, 0, 0, 44, 0, 0, 0, 0, 16, 0, 16, 0, 0, 2, 231,
  24. 4, 0, 0, 0, 0, 0, 0, 0, 0, 132, 16, 66, 8, 33, 132, 16, 4, 65, 0, 0, 1, 0, 0, 0,
  25. 0, 0, 0, 0, 0, 64, 0, 0, 1, 32, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 64, 0, 0, 16, 0,
  26. 0, 0, 0, 0, 0, 0, 0, 2, 0, 64, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 2, 0, 64, 0, 0,
  27. 16, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 64, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0,
  28. 0, 16, 0, 0,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 0, 0, 16, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 16,
  42. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 59
  44. };
  45. static pthread_once_t s_favicon_buf_once = PTHREAD_ONCE_INIT;
  46. static butil::IOBuf* s_favicon_buf = NULL;
  47. static void InitFavIcon() {
  48. s_favicon_buf = new butil::IOBuf;
  49. s_favicon_buf->append((const void *)s_favicon_array,
  50. arraysize(s_favicon_array));
  51. }
  52. static const butil::IOBuf& GetFavIcon() {
  53. pthread_once(&s_favicon_buf_once, InitFavIcon);
  54. return *s_favicon_buf;
  55. }
  56. void GetFaviconService::default_method(
  57. ::google::protobuf::RpcController* controller,
  58. const GetFaviconRequest* /*request*/,
  59. GetFaviconResponse* /*response*/,
  60. ::google::protobuf::Closure* done) {
  61. Controller *cntl = (Controller*)controller;
  62. cntl->http_response().set_content_type("image/x-icon");
  63. cntl->response_attachment().clear();
  64. cntl->response_attachment().append(GetFavIcon());
  65. if (done) {
  66. done->Run();
  67. }
  68. }
  69. } // namespace brpc