Debian Codegraph @

third_party/rust/neqo-http3/src/server_connection_events.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 use std::{cell::RefCell, collections::VecDeque, rc::Rc};
8
9 use neqo_common::Header;
10 use neqo_transport::{AppError, StreamId};
11
12 use crate::{
13 connection::Http3State,
14 features::extended_connect::{ExtendedConnectEvents, ExtendedConnectType, SessionCloseReason},
15 CloseType, Http3StreamInfo, HttpRecvStreamEvents, Priority, RecvStreamEvents, Res,
16 SendStreamEvents,
17 };
18
19 #[derive(Debug, PartialEq, Eq, Clone)]
20 pub enum Http3ServerConnEvent {
21 /// Headers are ready.
22 Headers {
23 stream_info: Http3StreamInfo,
24 headers: Vec<Header>,
25 fin: bool,
26 },
27 PriorityUpdate {
28 stream_id: StreamId,
29 priority: Priority,
30 },
31 /// Request data is ready.
32 DataReadable {
33 stream_info: Http3StreamInfo,
34 },
35 DataWritable {
36 stream_info: Http3StreamInfo,
37 },
38 StreamReset {
39 stream_info: Http3StreamInfo,
40 error: AppError,
41 },
42 StreamStopSending {
43 stream_info: Http3StreamInfo,
44 error: AppError,
45 },
46 /// Connection state change.
47 StateChange(Http3State),
48 ExtendedConnect {
49 stream_id: StreamId,
50 headers: Vec<Header>,
51 },
52 ExtendedConnectClosed {
53 connect_type: ExtendedConnectType,
54 stream_id: StreamId,
55 reason: SessionCloseReason,
56 headers: Option<Vec<Header>>,
57 },
58 ExtendedConnectNewStream(Http3StreamInfo),
59 ExtendedConnectDatagram {
60 session_id: StreamId,
61 datagram: Vec<u8>,
62 },
63 }
64
65 #[derive(Debug, Default, Clone)]
66 pub struct Http3ServerConnEvents {
67 events: Rc<RefCell<VecDeque<Http3ServerConnEvent>>>,
68 }
69
70 impl SendStreamEvents for Http3ServerConnEvents {
71 fn send_closed(&self, stream_info: Http3StreamInfo, close_type: CloseType) {
72 if close_type != CloseType::Done {
73 if let Some(error) = close_type.error() {
74 self.insert(Http3ServerConnEvent::StreamStopSending { stream_info, error });
75 }
76 }
77 }
78
79 fn data_writable(&self, stream_info: Http3StreamInfo) {
80 self.insert(Http3ServerConnEvent::DataWritable { stream_info });
81 }
82 }
83
84 impl RecvStreamEvents for Http3ServerConnEvents {
85 /// Add a new `DataReadable` event
86 fn data_readable(&self, stream_info: Http3StreamInfo) {
87 self.insert(Http3ServerConnEvent::DataReadable { stream_info });
88 }
89
90 fn recv_closed(&self, stream_info: Http3StreamInfo, close_type: CloseType) {
91 if close_type != CloseType::Done {
92 self.remove_events_for_stream_id(stream_info);
93 if let Some(error) = close_type.error() {
94 self.insert(Http3ServerConnEvent::StreamReset { stream_info, error });
95 }
96 }
97 }
98 }
99
100 impl HttpRecvStreamEvents for Http3ServerConnEvents {
101 /// Add a new `HeaderReady` event.
102 fn header_ready(
103 &self,
104 stream_info: Http3StreamInfo,
105 headers: Vec<Header>,
106 _interim: bool,
107 fin: bool,
108 ) {
109 self.insert(Http3ServerConnEvent::Headers {
110 stream_info,
111 headers,
112 fin,
113 });
114 }
115
116 fn extended_connect_new_session(&self, stream_id: StreamId, headers: Vec<Header>) {
117 self.insert(Http3ServerConnEvent::ExtendedConnect { stream_id, headers });
118 }
119 }
120
121 impl ExtendedConnectEvents for Http3ServerConnEvents {
122 fn session_start(
123 &self,
124 _connect_type: ExtendedConnectType,
125 _stream_id: StreamId,
126 _status: u16,
127 _headers: Vec<Header>,
128 ) {
129 }
130
131 fn session_end(
132 &self,
133 connect_type: ExtendedConnectType,
134 stream_id: StreamId,
135 reason: SessionCloseReason,
136 headers: Option<Vec<Header>>,
137 ) {
138 self.insert(Http3ServerConnEvent::ExtendedConnectClosed {
139 connect_type,
140 stream_id,
141 reason,
142 headers,
143 });
144 }
145
146 fn extended_connect_new_stream(&self, stream_info: Http3StreamInfo) -> Res<()> {
147 self.insert(Http3ServerConnEvent::ExtendedConnectNewStream(stream_info));
148 Ok(())
149 }
150
151 fn new_datagram(&self, session_id: StreamId, datagram: Vec<u8>) {
152 self.insert(Http3ServerConnEvent::ExtendedConnectDatagram {
153 session_id,
154 datagram,
155 });
156 }
157 }
158
159 impl Http3ServerConnEvents {
160 fn insert(&self, event: Http3ServerConnEvent) {
161 self.events.borrow_mut().push_back(event);
162 }
163
164 fn remove<F>(&self, f: F)
165 where
166 F: Fn(&Http3ServerConnEvent) -> bool,
167 {
168 self.events.borrow_mut().retain(|evt| !f(evt));
169 }
170
171 pub fn has_events(&self) -> bool {
172 !self.events.borrow().is_empty()
173 }
174
175 pub fn next_event(&self) -> Option<Http3ServerConnEvent> {
176 self.events.borrow_mut().pop_front()
177 }
178
179 pub fn connection_state_change(&self, state: Http3State) {
180 self.insert(Http3ServerConnEvent::StateChange(state));
181 }
182
183 pub fn priority_update(&self, stream_id: StreamId, priority: Priority) {
184 self.insert(Http3ServerConnEvent::PriorityUpdate {
185 stream_id,
186 priority,
187 });
188 }
189
190 fn remove_events_for_stream_id(&self, stream_info: Http3StreamInfo) {
191 self.remove(|evt| {
192 matches!(evt,
193 Http3ServerConnEvent::Headers { stream_info: x, .. } | Http3ServerConnEvent::DataReadable { stream_info: x, .. } if *x == stream_info)
194 });
195 }
196 }