-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The example can be run with `cargo r --example content_length_mismatch`. TLS keys will be generated at `h3i/h3i-example`, which can be used to decrypt a corresponding PCAP if one is captured separately.
- Loading branch information
1 parent
684bc41
commit 15606d2
Showing
3 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use h3i::actions::h3::Action; | ||
use h3i::client::sync_client; | ||
use h3i::config::Config; | ||
use quiche::h3::frame::Frame; | ||
use quiche::h3::Header; | ||
use quiche::h3::NameValue; | ||
|
||
/// The QUIC stream to send the frames on. See | ||
/// https://datatracker.ietf.org/doc/html/rfc9000#name-streams and | ||
/// https://datatracker.ietf.org/doc/html/rfc9114#request-streams for more. | ||
const STREAM_ID: u64 = 0; | ||
|
||
/// Send a request with a Content-Length header that specifies 5 bytes, but a | ||
/// body that is only 4 bytes long. This verifies https://datatracker.ietf.org/doc/html/rfc9114#section-4.1.2-3 for | ||
/// blog.cloudflare.com. | ||
fn main() { | ||
let config = Config::new() | ||
.with_host_port("blog.cloudflare.com".to_string()) | ||
.with_idle_timeout(2000) | ||
.build() | ||
.unwrap(); | ||
|
||
let headers = vec![ | ||
Header::new(b":method", b"POST"), | ||
Header::new(b":scheme", b"https"), | ||
Header::new(b":authority", b"blog.cloudflare.com"), | ||
Header::new(b":path", b"/"), | ||
// We say that we're going to send a body with 5 bytes... | ||
Header::new(b"content-length", b"5"), | ||
]; | ||
|
||
let header_block = encode_header_block(&headers).unwrap(); | ||
|
||
let actions = vec![ | ||
Action::SendHeadersFrame { | ||
stream_id: STREAM_ID, | ||
fin_stream: false, | ||
headers, | ||
frame: Frame::Headers { header_block }, | ||
}, | ||
Action::SendFrame { | ||
stream_id: STREAM_ID, | ||
fin_stream: true, | ||
frame: Frame::Data { | ||
// ...but, in actuality, we only send 4 bytes. This should yield a | ||
// 400 Bad Request response from an RFC-compliant | ||
// server: https://datatracker.ietf.org/doc/html/rfc9114#section-4.1.2-3 | ||
payload: b"test".to_vec(), | ||
}, | ||
}, | ||
]; | ||
|
||
let summary = | ||
sync_client::connect(config, &actions).expect("connection failed"); | ||
|
||
println!( | ||
"=== received connection summary! ===\n\n{}", | ||
serde_json::to_string_pretty(&summary).unwrap_or_else(|e| e.to_string()) | ||
); | ||
} | ||
|
||
// SendHeadersFrame requires a QPACK-encoded header block. h3i provides a | ||
// `send_headers_frame` helper function to abstract this, but for clarity, we do | ||
// it here. | ||
fn encode_header_block( | ||
headers: &[quiche::h3::Header], | ||
) -> std::result::Result<Vec<u8>, String> { | ||
let mut encoder = quiche::h3::qpack::Encoder::new(); | ||
|
||
let headers_len = headers | ||
.iter() | ||
.fold(0, |acc, h| acc + h.value().len() + h.name().len() + 32); | ||
|
||
let mut header_block = vec![0; headers_len]; | ||
let len = encoder | ||
.encode(headers, &mut header_block) | ||
.map_err(|_| "Internal Error")?; | ||
|
||
header_block.truncate(len); | ||
|
||
Ok(header_block) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters