blob: 6fb9dbc0fb62242281333cba5053da3037d1cd47 [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 =
Andreea Costinascc4d54e2020-10-19 15:46:25 +020035 "HTTP/1.1 502 Bad Gateway\r\n\r\nBad 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) {
Andreea-Elena Costinasfae5c152020-09-28 18:18:31 +000051 LOG(ERROR) << "Failed to close the listening socket";
Andreea Costinas054fbb52020-06-12 20:46:22 +020052 }
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)) {
Andreea-Elena Costinasfae5c152020-09-28 18:18:31 +000062 std::string_view server_reply =
63 GetConnectReplyString(expected_responses_.front());
Andreea Costinas054fbb52020-06-12 20:46:22 +020064 expected_responses_.pop();
Andreea Costinas054fbb52020-06-12 20:46:22 +020065 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
Andreea Costinas054fbb52020-06-12 20:46:22 +0200102void 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