Skip to main content

Module send

Module send 

Source
Expand description

I/O-free coroutine sending an HTTP/1.0 request and receiving its response (RFC 1945).

Same shape as crate::rfc9112::send::Http11Send but with the HTTP/1.0 wire format: no chunked transfer coding, connections close by default unless the server returns Connection: keep-alive.

StrategyTrigger
Fixed-lengthContent-Length: <n>
Read-to-EOFHeader absent

§Example

use std::{io::{Read, Write}, net::TcpStream};

use io_http::{
    coroutine::*,
    rfc1945::send::Http10Send,
    rfc9110::{request::HttpRequest, send::HttpSendYield},
};
use url::Url;

let url = Url::parse("http://example.com/").unwrap();
let request = HttpRequest::get(url.clone())
    .header("Host", url.host_str().unwrap());

let mut stream = TcpStream::connect("example.com:80").unwrap();
let mut send = Http10Send::new(request);
let mut arg: Option<&[u8]> = None;
let mut buf = [0u8; 4096];

let response = loop {
    match send.resume(arg.take()) {
        HttpCoroutineState::Complete(Ok(out)) => break out.response,
        HttpCoroutineState::Complete(Err(err)) => panic!("{err}"),
        HttpCoroutineState::Yielded(HttpSendYield::WantsWrite(bytes)) => {
            stream.write_all(&bytes).unwrap();
        }
        HttpCoroutineState::Yielded(HttpSendYield::WantsRead) => {
            let n = stream.read(&mut buf).unwrap();
            arg = Some(&buf[..n]);
        }
        HttpCoroutineState::Yielded(HttpSendYield::WantsRedirect { .. }) => unimplemented!(),
    }
};

println!("{}", *response.status);

Structs§

Http10Send
I/O-free coroutine to send an HTTP/1.0 request and receive its response.

Enums§

Http10SendError
Failure causes during the HTTP/1.0 send flow.