pub struct ResponseWriter { /* private fields */ }Expand description
ResponseWriter is the handle through which a Handler writes its response.
Two response modes are supported:
- Buffered: call
set_status/set_headerthenwrite(or the shorthandrespond). This sends headers + body atomically and closes the stream. - Streaming: call
stream_writeone or more times, thenstream_end(orstream_closeon error). Response headers must have been sent by a prior buffered call in this case, or the stream is left headerless.
Implementations§
Source§impl ResponseWriter
impl ResponseWriter
Sourcepub fn set_status(&mut self, status: &str)
pub fn set_status(&mut self, status: &str)
set_status sets the NWEP status string for the pending buffered response.
The default status is "ok". Must be called before write.
Sourcepub fn set_header(&mut self, name: &str, value: &str)
pub fn set_header(&mut self, name: &str, value: &str)
set_header appends a response header to the pending buffered response.
Sourcepub fn write(&mut self, body: &[u8]) -> Result<(), Error>
pub fn write(&mut self, body: &[u8]) -> Result<(), Error>
write sends the buffered response headers and body, then closes the stream.
If called more than once the subsequent calls are no-ops; only the first call
transmits data. Use stream_write /
stream_end for incremental streaming.
§Errors
Returns an Error if the underlying nwep_stream_respond or nwep_stream_end
C call fails.
Sourcepub fn respond(&mut self, status: &str, body: &[u8]) -> Result<(), Error>
pub fn respond(&mut self, status: &str, body: &[u8]) -> Result<(), Error>
respond sets status and writes body in a single call.
Equivalent to calling set_status followed by
write.
§Errors
Returns an Error if the underlying send fails.
Examples found in repository?
35fn main() {
36 nwep::init().unwrap_or_else(|e| {
37 eprintln!("init: {e}");
38 process::exit(1);
39 });
40
41 let key_file = std::env::var("NWEP_KEY_FILE").unwrap_or_else(|_| DEFAULT_KEY_FILE.into());
42 let keypair = load_or_generate_keypair(&key_file).unwrap_or_else(|e| {
43 eprintln!("keypair: {e}");
44 process::exit(1);
45 });
46
47 let node_id = keypair.node_id().unwrap_or_else(|e| {
48 eprintln!("node_id: {e}");
49 process::exit(1);
50 });
51 eprintln!("node id: {node_id}");
52
53 let mut router = Router::new();
54 router.handle_func("/hello", |w: &mut ResponseWriter, _r: &Request| {
55 let _ = w.respond("ok", b"hello from nwep-rust");
56 });
57
58 let addr = std::env::args().nth(1).unwrap_or_else(|| DEFAULT_ADDR.into());
59
60 let (server, event_loop) = ServerBuilder::new(&addr, keypair)
61 .on_connect(|info| eprintln!("connected: {}", info.node_id))
62 .on_disconnect(|info, code| eprintln!("disconnected: {} (code {code})", info.node_id))
63 .build(router)
64 .unwrap_or_else(|e| {
65 eprintln!("server: {e}");
66 process::exit(1);
67 });
68
69 let url = server.url("/hello");
70 eprintln!("listening on {}", server.addr());
71 println!("{url}"); // URL printed to stdout for the client to read
72
73 if let Err(e) = event_loop.run() {
74 eprintln!("server run: {e}");
75 process::exit(1);
76 }
77}Sourcepub fn stream_write(&mut self, data: &[u8]) -> Result<isize, Error>
pub fn stream_write(&mut self, data: &[u8]) -> Result<isize, Error>
stream_write writes a chunk of body data directly to the open QUIC stream.
This is the low-level building block for streaming responses. Call
stream_end when all chunks have been written.
Returns the number of bytes written.
§Errors
Returns an Error if nwep_stream_write returns a negative result code.
Sourcepub fn stream_end(&mut self) -> Result<(), Error>
pub fn stream_end(&mut self) -> Result<(), Error>
Sourcepub fn stream_close(&mut self, err: i32)
pub fn stream_close(&mut self, err: i32)
stream_close aborts the stream with an application error code.
Use this to signal an error mid-stream without sending a complete response.
Unlike stream_end, this does not return a result
because the close is best-effort.
Sourcepub fn stream_id(&self) -> i64
pub fn stream_id(&self) -> i64
stream_id returns the QUIC stream identifier for this response stream.
Useful for correlating log output or for advanced stream management.
Sourcepub fn is_server_initiated(&self) -> bool
pub fn is_server_initiated(&self) -> bool
is_server_initiated returns true if this stream was opened by the server.
Server-initiated streams correspond to notify messages; client-initiated streams correspond to ordinary requests.