blob: 0d53f7a77e35d1f115496d977aa4d4204b3dc37d [file] [log] [blame]
Andreea Costinas054fbb52020-06-12 20:46:22 +02001// Copyright 2020 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "system-proxy/test_http_server.h"
6
7#include <netinet/in.h>
8#include <sys/socket.h>
9#include <sys/types.h>
10
11#include <base/bind.h>
12#include <base/bind_helpers.h>
13#include <base/callback_helpers.h>
14#include <base/strings/stringprintf.h>
15#include <chromeos/patchpanel/net_util.h>
16#include <chromeos/patchpanel/socket.h>
17
18namespace {
19constexpr int kMaxConn = 10;
20
21const std::string_view kConnectionEstablished =
22 "HTTP/1.1 200 Connection established\r\n\r\n";
23
24const std::string_view kProxyAuthenticationRequiredBasic =
25 "HTTP/1.1 407 Proxy Authentication Required\r\n"
26 "Proxy-Authenticate: Basic realm=\"My Proxy\"\r\n"
27 "\r\n";
28
29const std::string_view kProxyAuthenticationRequiredNegotiate =
30 "HTTP/1.1 407 Proxy Authentication Required\r\n"
31 "Proxy-Authenticate: Negotiate realm=\"My Proxy\"\r\n"
32 "\r\n";
33
34const std::string_view kHttpBadGateway = "HTTP/1.1 502 Bad Gateway\r\n\r\n";
35
36} // namespace
37namespace system_proxy {
38
39HttpTestServer::HttpTestServer()
40 : base::SimpleThread("HttpTestServer"),
41 listening_addr_(htonl(INADDR_LOOPBACK)),
42 listening_port_(0) {}
43
44HttpTestServer::~HttpTestServer() {
45 if (!HasBeenStarted()) {
46 return;
47 }
48 int fd = listening_socket_->release();
49 if (close(fd) != 0) {
50 LOG(ERROR) << "Failed to close the listening socket";
51 }
52 Join();
53}
54
55void HttpTestServer::Run() {
56 struct sockaddr_storage client_src = {};
57 socklen_t sockaddr_len = sizeof(client_src);
58 while (!expected_responses_.empty()) {
59 if (auto client_conn = listening_socket_->Accept(
60 (struct sockaddr*)&client_src, &sockaddr_len)) {
61 std::string_view server_reply =
62 GetConnectReplyString(expected_responses_.front());
63 expected_responses_.pop();
64 LOG(ERROR) << server_reply;
65 client_conn->SendTo(server_reply.data(), server_reply.size());
66 }
67 }
68}
69
70void HttpTestServer::BeforeStart() {
71 listening_socket_ =
72 std::make_unique<patchpanel::Socket>(AF_INET, SOCK_STREAM);
73
74 struct sockaddr_in addr = {0};
75 addr.sin_family = AF_INET;
76 addr.sin_port = htons(listening_port_);
77 addr.sin_addr.s_addr = listening_addr_;
78 if (!listening_socket_->Bind((const struct sockaddr*)&addr, sizeof(addr))) {
79 LOG(ERROR) << "Cannot bind source socket" << std::endl;
80 return;
81 }
82
83 if (!listening_socket_->Listen(kMaxConn)) {
84 LOG(ERROR) << "Cannot listen on source socket." << std::endl;
85 return;
86 }
87
88 socklen_t len = sizeof(addr);
89 if (getsockname(listening_socket_->fd(), (struct sockaddr*)&addr, &len)) {
90 LOG(ERROR) << "Cannot get the listening port " << std::endl;
91 return;
92 }
93 listening_port_ = ntohs(addr.sin_port);
94}
95
96std::string HttpTestServer::GetUrl() {
97 return base::StringPrintf(
98 "http://%s:%d", patchpanel::IPv4AddressToString(listening_addr_).c_str(),
99 listening_port_);
100}
101
102void HttpTestServer::AddHttpConnectReply(HttpConnectReply reply) {
103 expected_responses_.push(reply);
104}
105
106std::string_view HttpTestServer::GetConnectReplyString(HttpConnectReply reply) {
107 switch (reply) {
108 case HttpConnectReply::kOk:
109 return kConnectionEstablished;
110 case HttpConnectReply::kAuthRequiredBasic:
111 return kProxyAuthenticationRequiredBasic;
112 case HttpConnectReply::kAuthRequiredKerberos:
113 return kProxyAuthenticationRequiredNegotiate;
114 case HttpConnectReply::kBadGateway:
115 return kHttpBadGateway;
116 default:
117 return kConnectionEstablished;
118 }
119}
120
121} // namespace system_proxy