TensorFlow Serving C++ API Documentation
evhttp_echo_server.cc
1 /* Copyright 2018 Google Inc. All Rights Reserved.
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 ==============================================================================*/
15 
16 // A single-threaded server to print the request details as HTML
17 // URI: /print
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstring>
22 #include <functional>
23 #include <iostream>
24 #include <memory>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
29 #include "absl/memory/memory.h"
30 #include "absl/strings/numbers.h"
31 #include "absl/strings/str_cat.h"
32 #include "absl/strings/string_view.h"
33 #include "tensorflow_serving/util/net_http/server/public/httpserver.h"
34 #include "tensorflow_serving/util/net_http/server/public/httpserver_interface.h"
35 #include "tensorflow_serving/util/net_http/server/public/server_request_interface.h"
36 
37 namespace {
38 
39 using absl::StrAppend;
40 
46 using tensorflow::serving::net_http::SetContentTypeHTML;
47 
48 void EchoHandler(ServerRequestInterface* req) {
49  std::string response;
50 
51  StrAppend(&response,
52  "<!DOCTYPE html>\n"
53  "<html>\n <head>\n"
54  " <meta charset='utf-8'>\n"
55  " <title>Request Echo HTTP Server</title>\n"
56  " </head>\n"
57  " <body>\n"
58  " <h1>Print the HTTP request detail</h1>\n"
59  " <ul>\n");
60 
61  StrAppend(&response, "HTTP Method: ", req->http_method(), " <br>\n");
62  StrAppend(&response, "Request Uri: ", req->uri_path(), " <br>\n");
63 
64  StrAppend(&response, "<br><br>====<br><br>\n");
65  for (auto header : req->request_headers()) {
66  StrAppend(&response, header, ": ", req->GetRequestHeader(header), "<br>\n");
67  }
68 
69  // read the request body
70  int64_t num_bytes;
71  auto request_chunk = req->ReadRequestBytes(&num_bytes);
72  bool print_break = false;
73  while (request_chunk != nullptr) {
74  if (!print_break) {
75  StrAppend(&response, "<br><br>====<br><br>\n");
76  print_break = true;
77  }
78  StrAppend(&response, absl::string_view(request_chunk.get(),
79  static_cast<size_t>(num_bytes)));
80  request_chunk = req->ReadRequestBytes(&num_bytes);
81  }
82 
83  req->WriteResponseString(response);
84 
85  SetContentTypeHTML(req);
86  req->Reply();
87 }
88 
89 // An executor that runs the current thread, testing only
90 class MyExcecutor final : public EventExecutor {
91  public:
92  MyExcecutor() = default;
93 
94  void Schedule(std::function<void()> fn) override { fn(); }
95 };
96 
97 // Returns the server if success, or nullptr if there is any error.
98 std::unique_ptr<HTTPServerInterface> StartServer(int port) {
99  auto options = absl::make_unique<ServerOptions>();
100  options->AddPort(port);
101  options->SetExecutor(absl::make_unique<MyExcecutor>());
102 
103  auto server = CreateEvHTTPServer(std::move(options));
104 
105  if (server == nullptr) {
106  return nullptr;
107  }
108 
109  RequestHandlerOptions handler_options;
110  server->RegisterRequestHandler("/print", EchoHandler, handler_options);
111 
112  // Blocking here with the use of MyExecutor
113  bool success = server->StartAcceptingRequests();
114 
115  if (success) {
116  return server;
117  }
118 
119  return nullptr;
120 }
121 
122 } // namespace
123 
124 int main(int argc, char** argv) {
125  if (argc < 2) {
126  std::cerr << "Usage: http-server <port:8080>" << std::endl;
127  return 1;
128  }
129 
130  int port = 8080;
131  bool port_parsed = absl::SimpleAtoi(argv[1], &port);
132  if (!port_parsed) {
133  std::cerr << "Invalid port: " << argv[1] << std::endl;
134  }
135 
136  auto server = StartServer(port);
137 
138  if (server != nullptr) {
139  server->WaitForTermination();
140  return 0;
141  } else {
142  std::cerr << "Failed to start the server." << std::endl;
143  return 1;
144  }
145 }