1 // Copyright 2021 Ian Jackson and contributors to Hippotat
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 // There is NO WARRANTY.
9 pub ic: Arc<InstanceConfig>,
10 pub web: mpsc::Sender<WebRequest>,
11 pub route: mpsc::Sender<RoutedPacket>,
14 pub async fn run(global: Arc<Global>,
15 ic: Arc<InstanceConfig>,
16 mut web: mpsc::Receiver<WebRequest>,
17 mut routed: mpsc::Receiver<RoutedPacket>)
21 reply_to: oneshot::Sender<WebResponse>,
25 struct OutstandingInner {
27 target_requests_outstanding: u32,
30 let mut outstanding: VecDeque<Outstanding> = default();
31 let mut downbound: PacketQueue<RoutedPacketData> = default();
33 let try_send_response = |
34 reply_to: oneshot::Sender<WebResponse>,
37 reply_to.send(response)
38 .unwrap_or_else(|_: WebResponse| {
40 error!("unable to send response back to webserver! user={}",
46 let eff_max_batch_down = outstanding
48 .map(|o| o.oi.max_batch_down)
50 .unwrap_or(ic.max_batch_down)
54 if ! downbound.is_empty() {
55 outstanding.pop_front()
56 } else if let Some((i,_)) = outstanding.iter().enumerate().find({
57 |(_,o)| outstanding.len() > o.oi.target_requests_outstanding.sat()
58 // xxx need timeout-based return too
60 Some(outstanding.remove(i).unwrap())
65 let mut build: FrameQueueBuf = default();
68 let next = if let Some(n) = downbound.peek_front() { n }
70 // Don't add 1 for the ESC since we will strip one
71 if build.len() + next.len() >= eff_max_batch_down { break }
72 build.esc_push(downbound.pop_front().unwrap());
74 if ! build.is_empty() {
79 let response = WebResponse {
84 try_send_response(req.reply_to, response);
87 let max = usize::saturating_mul(
88 ic.max_requests_outstanding.sat(),
90 ).saturating_add(1 /* one boundary SLIP_ESC which we'll trim */);
92 while downbound.total_len() > max {
93 let _ = downbound.pop_front();
99 data = routed.recv() =>
101 let data = data.ok_or_else(|| anyhow!("routers shut down!"))?;
102 downbound.push_back(data.data);
108 initial, initial_remaining, length_hint, mut body,
110 reply_to, conn, mut warnings, may_route,
111 } = req.ok_or_else(|| anyhow!("webservers all shut down!"))?;
115 let initial_used = initial.len() - initial_remaining;
117 let whole_request = read_limited_bytes(
118 ic.max_batch_up.sat(),
122 ).await.context("read request body")?;
124 let (meta, mut comps) =
125 multipart::ComponentIterator::resume_mid_component(
126 &whole_request[initial_used..],
128 ).context("resume parsing body, after auth checks")?;
130 let mut meta = MetadataFieldIterator::new(&meta);
133 { $v:ident, ( $( $badcmp:tt )? ), $ret:expr,
134 let $server:ident, $client:ident $($code:tt)*
138 let $client $($code)*
140 if $client $badcmp $server {
141 throw!(anyhow!("mismatch: client={:?} {} server={:?}",
142 $client, stringify!($badcmp), $server));
146 })().context(stringify!($v))?;
151 target_requests_outstanding, ( != ), client,
152 let server, client: u32 = meta.need_parse()?;
155 http_timeout, ( > ), client,
156 let server, client = Duration::from_secs(meta.need_parse()?);
160 let server, client: u32 = meta.parse()?.unwrap_or(server);
163 max_batch_down, (), min(client, server),
164 let server, client: u32 = meta.parse()?.unwrap_or(server);
167 max_batch_up, ( > ), client,
168 let server, client = meta.parse()?.unwrap_or(server);
170 let _ = max_batch_up; // we don't use this further
172 while let Some(comp) = comps.next(&mut warnings, PartName::d)? {
173 if comp.name != PartName::d {
174 warnings.add(&format_args!("unexpected part {:?}", comp.name))?;
176 slip::processn(Mime2Slip, mtu, comp.payload, |header| {
177 let saddr = ip_packet_addr::<false>(header)?;
178 if saddr != ic.link.client.0 { throw!(PE::Src(saddr)) }
179 let daddr = ip_packet_addr::<true>(header)?;
181 }, |(daddr,packet)| route_packet(
182 &global, &conn, Some(&ic.link.client), daddr,
183 packet, may_route.clone(),
185 |e| Ok::<_,SlipFramesError<_>>({ warnings.add(&e)?; })
189 let deadline = Instant::now() + http_timeout;
191 let oi = OutstandingInner {
192 target_requests_outstanding,
198 Ok(oi) => outstanding.push_back(Outstanding { reply_to, oi }),
200 try_send_response(reply_to, WebResponse {