File: http_server.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (97 lines) | stat: -rw-r--r-- 3,662 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_TEST_CHROMEDRIVER_SERVER_HTTP_SERVER_H_
#define CHROME_TEST_CHROMEDRIVER_SERVER_HTTP_SERVER_H_

#include "base/json/json_reader.h"
#include "base/task/single_thread_task_runner.h"
#include "chrome/test/chromedriver/server/http_handler.h"
#include "net/base/url_util.h"
#include "net/server/http_server.h"
#include "net/server/http_server_request_info.h"
#include "net/server/http_server_response_info.h"
#include "net/socket/tcp_server_socket.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"

typedef base::RepeatingCallback<void(const net::HttpServerRequestInfo&,
                                     const HttpResponseSenderFunc&)>
    HttpRequestHandlerFunc;

class HttpServerInterface {
 public:
  virtual ~HttpServerInterface() = default;
  virtual void Close(int connection_id) = 0;

  virtual void AcceptWebSocket(int connection_id,
                               const net::HttpServerRequestInfo& request) = 0;

  virtual void SendOverWebSocket(int connection_id,
                                 const std::string& data) = 0;

  virtual void SendResponse(
      int connection_id,
      const net::HttpServerResponseInfo& response,
      const net::NetworkTrafficAnnotationTag& traffic_annotation) = 0;
};

class HttpServer : public net::HttpServer::Delegate,
                   public virtual HttpServerInterface {
 public:
  explicit HttpServer(const std::string& url_base,
                      const std::vector<net::IPAddress>& whitelisted_ips,
                      const std::vector<std::string>& allowed_origins,
                      const HttpRequestHandlerFunc& handle_request_func,
                      base::WeakPtr<HttpHandler> handler,
                      scoped_refptr<base::SingleThreadTaskRunner> cmd_runner);

  ~HttpServer() override;

  int Start(uint16_t port, bool allow_remote, bool use_ipv4);

  // Overridden from net::HttpServer::Delegate:
  void OnConnect(int connection_id) override;

  void OnHttpRequest(int connection_id,
                     const net::HttpServerRequestInfo& info) override;

  void OnWebSocketRequest(int connection_id,
                          const net::HttpServerRequestInfo& info) override;

  void OnWebSocketMessage(int connection_id, std::string data) override;

  void OnClose(int connection_id) override;

  void Close(int connection_id) override;

  void AcceptWebSocket(int connection_id,
                       const net::HttpServerRequestInfo& request) override;

  void SendOverWebSocket(int connection_id, const std::string& data) override;

  void SendResponse(
      int connection_id,
      const net::HttpServerResponseInfo& response,
      const net::NetworkTrafficAnnotationTag& traffic_annotation) override;

  const net::IPEndPoint& LocalAddress() const;

 private:
  void OnResponse(int connection_id,
                  bool keep_alive,
                  std::unique_ptr<net::HttpServerResponseInfo> response);
  const std::string url_base_;
  HttpRequestHandlerFunc handle_request_func_;
  std::unique_ptr<net::HttpServer> server_;
  std::map<int, std::string> connection_to_session_map;
  bool allow_remote_;
  const std::vector<net::IPAddress> whitelisted_ips_;
  const std::vector<std::string> allowed_origins_;
  base::WeakPtr<HttpHandler> handler_;
  scoped_refptr<base::SingleThreadTaskRunner> cmd_runner_;
  net::IPEndPoint local_address_;
  base::WeakPtrFactory<HttpServer> weak_factory_{this};  // Should be last.
};

#endif  // CHROME_TEST_CHROMEDRIVER_SERVER_HTTP_SERVER_H_