master
1#***************************************************************************
2# _ _ ____ _
3# Project ___| | | | _ \| |
4# / __| | | | |_) | |
5# | (__| |_| | _ <| |___
6# \___|\___/|_| \_\_____|
7#
8# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9#
10# This software is licensed as described in the file COPYING, which
11# you should have received as part of this distribution. The terms
12# are also available at https://curl.se/docs/copyright.html.
13#
14# You may opt to use, copy, modify, merge, publish, distribute and/or sell
15# copies of the Software, and permit persons to whom the Software is
16# furnished to do so, under the terms of the COPYING file.
17#
18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19# KIND, either express or implied.
20#
21# SPDX-License-Identifier: curl
22#
23###########################################################################
24#
25import logging
26import os
27import socket
28import subprocess
29import time
30from datetime import datetime, timedelta, timezone
31from json import JSONEncoder
32from typing import ClassVar, Dict
34from .curl import CurlClient
35from .env import Env
36from .ports import alloc_ports_and_do
38log = logging.getLogger(__name__)
41class Caddy:
43 PORT_SPECS: ClassVar[Dict[str, int]] = {
44 'caddy': socket.SOCK_STREAM,
45 'caddys': socket.SOCK_STREAM,
46 }
48 def __init__(self, env: Env):
49 self.env = env
50 self._caddy = os.environ.get('CADDY', env.caddy)
51 self._caddy_dir = os.path.join(env.gen_dir, 'caddy')
52 self._docs_dir = os.path.join(self._caddy_dir, 'docs')
53 self._conf_file = os.path.join(self._caddy_dir, 'Caddyfile')
54 self._error_log = os.path.join(self._caddy_dir, 'caddy.log')
55 self._tmp_dir = os.path.join(self._caddy_dir, 'tmp')
56 self._error_fd = None
57 self._process = None
58 self._http_port = 0
59 self._https_port = 0
60 self._rmf(self._error_log)
62 @property
63 def docs_dir(self):
64 return self._docs_dir
66 @property
67 def port(self) -> int:
68 return self._https_port
70 def close_log(self):
71 if self._error_fd:
72 self._error_fd.close()
73 self._error_fd = None
75 def clear_logs(self):
76 self._rmf(self._error_log)
78 def is_running(self):
79 if self._process:
80 self._process.poll()
81 return self._process.returncode is None
82 return False
84 def start_if_needed(self):
85 if not self.is_running():
86 return self.start()
87 return True
89 def initial_start(self):
91 def startup(ports: Dict[str, int]) -> bool:
92 self._http_port = ports['caddy']
93 self._https_port = ports['caddys']
94 if self.start():
95 self.env.update_ports(ports)
96 return True
97 self.stop()
98 self._http_port = 0
99 self._https_port = 0
100 return False
102 return alloc_ports_and_do(Caddy.PORT_SPECS, startup,
103 self.env.gen_root, max_tries=3)
105 def start(self, wait_live=True):
106 assert self._http_port > 0 and self._https_port > 0
107 self._mkpath(self._tmp_dir)
108 if self._process:
109 self.stop()
110 self._write_config()
111 args = [
112 self._caddy, 'run'
113 ]
114 self._error_fd = open(self._error_log, 'a') # noqa: SIM115
115 self._process = subprocess.Popen(args=args, cwd=self._caddy_dir, stderr=self._error_fd)
116 if self._process.returncode is not None:
117 return False
118 return not wait_live or self.wait_live(timeout=timedelta(seconds=Env.SERVER_TIMEOUT))
120 def stop(self, wait_dead=True):
121 self._mkpath(self._tmp_dir)
122 if self._process:
123 self._process.terminate()
124 try:
125 self._process.wait(timeout=1)
126 except subprocess.TimeoutExpired:
127 self._process.kill()
128 self._process = None
129 self.close_log()
130 return not wait_dead or self.wait_dead(timeout=timedelta(seconds=5))
131 self.close_log()
132 return True
134 def restart(self):
135 self.stop()
136 return self.start()
138 def wait_dead(self, timeout: timedelta):
139 curl = CurlClient(env=self.env, run_dir=self._tmp_dir)
140 try_until = datetime.now(timezone.utc) + timeout
141 while datetime.now(timezone.utc) < try_until:
142 check_url = f'https://{self.env.domain1}:{self.port}/'
143 r = curl.http_get(url=check_url)
144 if r.exit_code != 0:
145 return True
146 log.debug(f'waiting for caddy to stop responding: {r}')
147 time.sleep(.1)
148 log.debug(f"Server still responding after {timeout}")
149 return False
151 def wait_live(self, timeout: timedelta):
152 curl = CurlClient(env=self.env, run_dir=self._tmp_dir)
153 try_until = datetime.now(timezone.utc) + timeout
154 while datetime.now(timezone.utc) < try_until:
155 check_url = f'https://{self.env.domain1}:{self.port}/'
156 r = curl.http_get(url=check_url)
157 if r.exit_code == 0:
158 return True
159 time.sleep(.1)
160 log.error(f"Caddy still not responding after {timeout}")
161 return False
163 def _rmf(self, path):
164 if os.path.exists(path):
165 os.remove(path)
167 def _mkpath(self, path):
168 if not os.path.exists(path):
169 os.makedirs(path)
171 def _write_config(self):
172 domain1 = self.env.domain1
173 creds1 = self.env.get_credentials(domain1)
174 assert creds1 # convince pytype this is not None
175 domain2 = self.env.domain2
176 creds2 = self.env.get_credentials(domain2)
177 assert creds2 # convince pytype this is not None
178 self._mkpath(self._docs_dir)
179 self._mkpath(self._tmp_dir)
180 with open(os.path.join(self._docs_dir, 'data.json'), 'w') as fd:
181 data = {
182 'server': f'{domain1}',
183 }
184 fd.write(JSONEncoder().encode(data))
185 with open(self._conf_file, 'w') as fd:
186 conf = [ # base server config
187 '{',
188 f' http_port {self._http_port}',
189 f' https_port {self._https_port}',
190 ' log default {',
191 ' level ERROR',
192 '}',
193 f' servers :{self._https_port} {{',
194 ' protocols h3 h2 h1',
195 ' }',
196 '}',
197 f'{domain1}:{self._https_port} {{',
198 ' file_server * {',
199 f' root {self._docs_dir}',
200 ' }',
201 f' tls {creds1.cert_file} {creds1.pkey_file}',
202 '}',
203 ]
204 if self.env.http_port > 0:
205 conf.extend([
206 f'{domain2} {{',
207 f' reverse_proxy /* http://localhost:{self.env.http_port} {{',
208 ' }',
209 f' tls {creds2.cert_file} {creds2.pkey_file}',
210 '}',
211 ])
212 fd.write("\n".join(conf))
Diagnostics4 git calls · 44 ms
CallStatusSizeTime044 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit16 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=16 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2002.3 KiB · hit26 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=26 git.response.bytes=2343 git.response.cache=HIT git.response.duration_ms=26 git.fetch.wants=14f71f5bfd07484e5d2e29fb3210c8fee4d7a438 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2343 git.pkt.payload.max=2322 git.sideband.band1.bytes=2321 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2d00c392eefad
- Request
- d663aca8…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format