blob: efcba00b09d9635c345fbbaf90d65b022a92e5af [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
Andreea Costinasf90a4c02020-06-12 22:30:51 +020034const std::string_view kHttpBadGateway =
35 "HTTP/1.1 502 Bad Gateway\r\n\r\nBag gateway message from the server";
Andreea Costinas054fbb52020-06-12 20:46:22 +020036
37} // namespace
38namespace system_proxy {
39
40HttpTestServer::HttpTestServer()
41 : base::SimpleThread("HttpTestServer"),
42 listening_addr_(htonl(INADDR_LOOPBACK)),
43 listening_port_(0) {}
44
45HttpTestServer::~HttpTestServer() {
46 if (!HasBeenStarted()) {
47 return;
48 }
49 int fd = listening_socket_->release();
50 if (close(fd) != 0) {
51 LOG(ERROR) << "Failed to close the listening socket";
52 }
53 Join();
54}
55
56void HttpTestServer::Run() {
57 struct sockaddr_storage client_src = {};
58 socklen_t sockaddr_len = sizeof(client_src);
59 while (!expected_responses_.empty()) {
60 if (auto client_conn = listening_socket_->Accept(
61 (struct sockaddr*)&client_src, &sockaddr_len)) {
62 std::string_view server_reply =
63 GetConnectReplyString(expected_responses_.front());
64 expected_responses_.pop();
65 LOG(ERROR) << server_reply;
66 client_conn->SendTo(server_reply.data(), server_reply.size());
67 }
68 }
69}
70
71void HttpTestServer::BeforeStart() {
72 listening_socket_ =
73 std::make_unique<patchpanel::Socket>(AF_INET, SOCK_STREAM);
74
75 struct sockaddr_in addr = {0};
76 addr.sin_family = AF_INET;
77 addr.sin_port = htons(listening_port_);
78 addr.sin_addr.s_addr = listening_addr_;
79 if (!listening_socket_->Bind((const struct sockaddr*)&addr, sizeof(addr))) {
80 LOG(ERROR) << "Cannot bind source socket" << std::endl;
81 return;
82 }
83
84 if (!listening_socket_->Listen(kMaxConn)) {
85 LOG(ERROR) << "Cannot listen on source socket." << std::endl;
86 return;
87 }
88
89 socklen_t len = sizeof(addr);
90 if (getsockname(listening_socket_->fd(), (struct sockaddr*)&addr, &len)) {
91 LOG(ERROR) << "Cannot get the listening port " << std::endl;
92 return;
93 }
94 listening_port_ = ntohs(addr.sin_port);
95}
96
97std::string HttpTestServer::GetUrl() {
98 return base::StringPrintf(
99 "http://%s:%d", patchpanel::IPv4AddressToString(listening_addr_).c_str(),
100 listening_port_);
101}
102
103void HttpTestServer::AddHttpConnectReply(HttpConnectReply reply) {
104 expected_responses_.push(reply);
105}
106
107std::string_view HttpTestServer::GetConnectReplyString(HttpConnectReply reply) {
108 switch (reply) {
109 case HttpConnectReply::kOk:
110 return kConnectionEstablished;
111 case HttpConnectReply::kAuthRequiredBasic:
112 return kProxyAuthenticationRequiredBasic;
113 case HttpConnectReply::kAuthRequiredKerberos:
114 return kProxyAuthenticationRequiredNegotiate;
115 case HttpConnectReply::kBadGateway:
116 return kHttpBadGateway;
117 default:
118 return kConnectionEstablished;
119 }
120}
121
122} // namespace system_proxy