1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
//! A non-blocking HTTP client
use std::{
fmt::Debug,
io::{self, Error, ErrorKind, Read},
mem::swap,
str::FromStr,
};
use hyperium_http::{
header::{CONTENT_LENGTH, HOST, TRANSFER_ENCODING},
Response,
};
use tcp_stream::{OwnedTLSConfig, TLSConfig, TcpStream};
use crate::{
buffer::GrowableCircleBuf,
frame::{DeserializeFrame, FrameDuplex, SerializeFrame, SizedFrame},
tcp::TcpSession,
DriveOutcome, Flush, Publish, PublishOutcome, Receive, Session, SessionStatus,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Scheme {
Http,
Https,
}
impl Scheme {
pub fn default_port(&self) -> u16 {
match self {
Self::Http => 80,
Self::Https => 443,
}
}
}
/// An HTTP Request, which can be represented by a [`hyperium_http::Request`] or a serialized HTTP payload.
///
/// [`HttpRequest::Serialized`] is utilized to prevent needing to serialize a payload more than one time in
/// the event it needs to be retried due to back-pressure.
pub enum HttpRequest {
Request(hyperium_http::Request<Vec<u8>>),
Serialized(Vec<u8>),
}
impl<I: IntoBody> From<hyperium_http::Request<I>> for HttpRequest {
fn from(value: hyperium_http::Request<I>) -> Self {
let (parts, body) = value.into_parts();
HttpRequest::Request(hyperium_http::Request::from_parts(parts, body.into_body()))
}
}
/// A simple non-blocking HTTP 1.x client
///
/// Calling `connect(..)` or `request(..)` will return a [`HttpClientSession`], which encapsulates a [`FrameDuplex`] utilizing an HTTP [`FramingStrategy`].
/// The framing strategy utilizes Hyperium's [`http`] lib for [`hyperium_http::Request`] and [`hyperium_http::Response`] structs.
///
/// The returned [`HttpClientSession`] will have pre-setup TLS for `https` URLs, and will pre-buffer the serialized request.
/// Calls to `drive(..)` will perform the TLS handshake and flush the pending request.
/// Calls to `read(..)` will buffer the response, and return a deserialized [`hyperium_http::Response`].
///
/// For now, this only supports HTTP 1.x.
///
/// ## Functions
///
/// [`HttpClient::request`] will open a [`HttpClientSession`] for the given [`hyperium_http::Request`], buffering the request, which can be driven and read to completion.
/// To open a connection without an immeidate pending [`hyperium_http::Request`], use [`HttpClient::connect`], which simply opens a persistent connection.
///
/// [`HttpClient::connect`] will open a persistent [`HttpClientSession`] to the given domain. This connection must call [`Session::drive()`] until [`Session::status`]
/// returns [`SessionStatus::Connected`], at which point the session can send multiple [`hyperium_http::Request`] payloads and receive [`hyperium_http::Response`] payloads utilizing HTTP "keep-alive".
///
/// ## Example
///
/// ```no_run
/// use nbio::{Receive, ReceiveOutcome, Session};
/// use nbio::http::HttpClient;
/// use nbio::hyperium_http::Request;
/// use nbio::tcp_stream::OwnedTLSConfig;
///
/// // create the client and make the request
/// let mut client = HttpClient::new();
/// let mut conn = client
/// .request(Request::get("http://icanhazip.com").body(()).unwrap())
/// .unwrap();
///
/// // drive and read the conn until a full response is received
/// loop {
/// conn.drive().unwrap();
/// if let ReceiveOutcome::Payload(r) = conn.receive().unwrap() {
/// // validate the response
/// println!("Response Body: {}", String::from_utf8_lossy(r.body()));
/// break;
/// }
/// }
/// ```
pub struct HttpClient {
tls_config: OwnedTLSConfig,
}
impl HttpClient {
/// Create a new HttpClient
pub fn new() -> Self {
Self {
tls_config: OwnedTLSConfig::default(),
}
}
/// Override the default TLS config
pub fn with_tls_config(mut self, tls_config: OwnedTLSConfig) -> Self {
self.tls_config = tls_config;
self
}
/// Initiate a new HTTP connection that is ready for a new request.
///
/// This will return a [`HttpClientSession`], which encapsulates a [`FrameDuplex`] utilizing an HTTP [`FramingStrategy`].
/// The framing strategy utilizes Hyperium's [`http`] lib for [`hyperium_http::Request`] and [`hyperium_http::Response`] structs.
///
/// The returned [`HttpClientSession`] will have pre-setup TLS for `https` URLs, and will have pre-buffered the serialized request request.
/// Before calling `read`/`write`, call [`Session::drive()`] to finish connecting and complete any pending TLS handshakes until [`Session::status`]
/// returns [`SessionStatus::Connected`].
///
/// For now, this only supports HTTP 1.x.
pub fn connect(
&mut self,
host: &str,
port: u16,
scheme: Scheme,
) -> Result<HttpClientSession, io::Error> {
let mut conn = TcpSession::connect(format!("{host}:{port}"))?;
if scheme == Scheme::Https {
conn = conn.into_tls(&host, self.tls_config.as_ref())?;
}
Ok(HttpClientSession::new(FrameDuplex::new(
conn,
Http1ResponseDeserializer::new(),
Http1RequestSerializer::new(),
0,
)))
}
/// Initiate a new HTTP connection that will send the given request.
///
/// This will return a [`HttpClientSession`], which encapsulates a [`FrameDuplex`] utilizing an HTTP [`FramingStrategy`].
/// The framing strategy utilizes Hyperium's [`http`] lib for [`hyperium_http::Request`] and [`hyperium_http::Response`] structs.
///
/// The returned [`HttpClientSession`] will have pre-setup TLS for `https` URLs, and will have pre-buffered the serialized request request.
/// Calling `drive(..)` and `read(..)` will perform the TLS handshake, flush the pending request, buffer the response, and return a deserialized [`http::Response`].
///
/// For now, this only supports HTTP 1.x.
pub fn request<I: IntoBody>(
&mut self,
request: hyperium_http::Request<I>,
) -> Result<HttpClientSession, io::Error> {
let (parts, body) = request.into_parts();
let request = hyperium_http::Request::from_parts(parts, body.into_body());
let scheme = match request.uri().scheme_str() {
None => Scheme::Http,
Some("http") => Scheme::Http,
Some("https") => Scheme::Https,
_ => {
return Err(io::Error::new(
ErrorKind::InvalidData,
"bad http uri scheme",
))
}
};
let stream = connect_stream(
scheme,
request.uri().host(),
request.uri().port().map(|x| x.as_u16()),
self.tls_config.as_ref(),
)?;
let mut conn = HttpClientSession::new(FrameDuplex::new(
TcpSession::new(stream)?,
Http1ResponseDeserializer::new(),
Http1RequestSerializer::new(),
0,
));
conn.pending_initial_request = Some(request.into());
Ok(conn)
}
}
impl Default for HttpClient {
fn default() -> Self {
Self::new()
}
}
pub(crate) fn connect_stream(
scheme: Scheme,
host: Option<&str>,
port: Option<u16>,
tls_config: TLSConfig<'_, '_, '_>,
) -> Result<TcpStream, Error> {
let host = match host {
Some(x) => x.to_owned(),
None => return Err(io::Error::new(ErrorKind::InvalidData, "missing host")),
};
let port = match port {
Some(x) => x,
None => scheme.default_port(),
};
let mut conn = TcpStream::connect(format!("{host}:{port}"))?;
if scheme == Scheme::Https {
conn = conn
.into_tls(&host, tls_config)
.map_err(|err| Error::new(ErrorKind::ConnectionRefused, err))?;
}
Ok(conn)
}
/// A [`Session`] created by the [`HttpClient`].
///
/// This encapsulates a [`FrameDuplex<TcpSession, Http1ResponseDeserializer, Http1RequestSerializer>`] and allows a single
/// [`hyperium_http::Request`] to be enqueued prior to a successful connection, supporting the [`HttpClient::request`] function.
pub struct HttpClientSession {
session: FrameDuplex<TcpSession, Http1ResponseDeserializer, Http1RequestSerializer>,
pending_initial_request: Option<HttpRequest>,
}
impl HttpClientSession {
pub fn new(
session: FrameDuplex<TcpSession, Http1ResponseDeserializer, Http1RequestSerializer>,
) -> Self {
Self {
session,
pending_initial_request: None,
}
}
}
impl Session for HttpClientSession {
fn status(&self) -> crate::SessionStatus {
self.session.status()
}
fn drive(&mut self) -> Result<DriveOutcome, Error> {
let mut result: crate::DriveOutcome = self.session.drive()?;
if self.session.status() == SessionStatus::Established
&& self.pending_initial_request.is_some()
{
let wrote = match self.session.publish(
self.pending_initial_request
.take()
.expect("checked pending_request"),
)? {
PublishOutcome::Published => true,
PublishOutcome::Incomplete(x) => {
self.pending_initial_request = Some(x);
false
}
};
if wrote {
self.pending_initial_request = None;
result = DriveOutcome::Active;
}
}
Ok(result)
}
}
impl Receive for HttpClientSession {
type ReceivePayload<'a> = hyperium_http::Response<Vec<u8>>;
fn receive<'a>(&'a mut self) -> Result<crate::ReceiveOutcome<Self::ReceivePayload<'a>>, Error> {
if self.pending_initial_request.is_none() && self.status() == SessionStatus::Established {
// make the request/response model more straightforward by not requiring checks to `status()` before calling `read`.
// `self.pending_initial_request.is_none()`: only do this when an initial request is pending, otherwise revert to default `read` behavior for persistent streams.
self.session.receive()
} else {
Ok(crate::ReceiveOutcome::Idle)
}
}
}
impl Publish for HttpClientSession {
type PublishPayload<'a> = HttpRequest;
fn publish<'a>(
&mut self,
data: Self::PublishPayload<'a>,
) -> Result<PublishOutcome<Self::PublishPayload<'a>>, Error> {
self.session.publish(data)
}
}
impl Flush for HttpClientSession {
fn flush(&mut self) -> Result<(), Error> {
self.session.flush()
}
}
impl Debug for HttpClientSession {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HttpClientSession")
.field("session", &self.session)
.finish()
}
}
/// Extensible public trait to support serializing a variety of body types.
pub trait IntoBody {
fn into_body(self) -> Vec<u8>;
}
impl IntoBody for String {
fn into_body(self) -> Vec<u8> {
self.into_bytes()
}
}
impl IntoBody for &str {
fn into_body(self) -> Vec<u8> {
self.as_bytes().to_vec()
}
}
impl IntoBody for Vec<u8> {
fn into_body(self) -> Vec<u8> {
self
}
}
impl IntoBody for &[u8] {
fn into_body(self) -> Vec<u8> {
self.to_vec()
}
}
impl IntoBody for () {
fn into_body(self) -> Vec<u8> {
Vec::new()
}
}
enum BodyType {
ContentLength(usize),
ChunkedTransfer,
OnClose,
None,
}
struct BodyInfo {
offset: usize,
ty: BodyType,
}
impl BodyInfo {
pub fn new(offset: usize, ty: BodyType) -> Self {
Self { offset, ty }
}
}
/// A [`DeserializeFrame`] impl for HTTP 1.x where [`DeserializeFrame::DeserializedFrame`] is an [`http::Response`].
pub struct Http1ResponseDeserializer {
deserialized_response: Option<hyperium_http::Response<Vec<u8>>>,
deserialized_size: usize,
body_info: Option<BodyInfo>,
}
impl Http1ResponseDeserializer {
pub fn new() -> Self {
Self {
deserialized_response: None,
deserialized_size: 0,
body_info: None,
}
}
}
impl DeserializeFrame for Http1ResponseDeserializer {
type DeserializedFrame<'a> = hyperium_http::Response<Vec<u8>>;
fn check_deserialize_frame(&mut self, data: &[u8], eof: bool) -> Result<bool, Error> {
if self.deserialized_response.is_none() {
self.deserialized_response = Some(Response::new(Vec::new()));
}
let deserialized_response = self
.deserialized_response
.as_mut()
.expect("checked deserialized_response value");
let header_count: usize = count_max_headers(data);
let mut headers = Vec::new();
headers.resize(header_count, httparse::EMPTY_HEADER);
// if they have not already been parsed, attempt to parse the response headers and find the body type.
if self.body_info.is_none() {
let mut parsed = httparse::Response::new(&mut headers);
match parsed.parse(data).map_err(|err| {
Error::new(
ErrorKind::InvalidData,
format!("http response parse failed: {err:?}").as_str(),
)
})? {
httparse::Status::Complete(size) => {
// determine the body type
if parse_is_chunked(&parsed.headers) {
self.body_info = Some(BodyInfo::new(size, BodyType::ChunkedTransfer));
} else if let Some(content_length) = parse_content_length(&parsed.headers)? {
self.body_info =
Some(BodyInfo::new(size, BodyType::ContentLength(content_length)));
} else if parsed.version.is_none() || parsed.version == Some(1) {
self.body_info = Some(BodyInfo::new(size, BodyType::OnClose));
} else {
self.body_info = Some(BodyInfo::new(size, BodyType::None));
}
// parse into cached deserialized_response
parsed_into_response(parsed, deserialized_response)?;
}
httparse::Status::Partial => return Ok(false),
}
}
// use parsed BodyInfo to check if entire body has been received
let parsed_body = match &self.body_info {
None => None,
Some(body_info) => {
match body_info.ty {
BodyType::ChunkedTransfer => {
// TODO: determine better way to see if all chunks have been received
// TODO: cache offset of last read chunk to avoid re-parsing entire body every time
if body_info.offset < data.len() && ends_with_ascii(data, "\r\n\r\n") {
let mut body = Vec::new();
let mut decoder =
chunked_transfer::Decoder::new(&data[body_info.offset..]);
decoder.read_to_end(&mut body)?;
match decoder.remaining_chunks_size() {
None => Some(body),
Some(_) => None,
}
} else {
None
}
}
BodyType::ContentLength(content_length) => {
// read to given length
let total_length = body_info.offset + content_length;
if data.len() >= total_length {
Some(data[body_info.offset..total_length].to_vec())
} else {
None
}
}
BodyType::OnClose => {
if eof {
Some(data[body_info.offset..].to_vec())
} else {
None
}
}
BodyType::None => Some(Vec::new()),
}
}
};
// if entire body has been received, insert into deserialized_response and return true
match parsed_body {
None => {
if eof {
Err(Error::new(
ErrorKind::UnexpectedEof,
"http connection terminated before receiving full response",
))
} else {
Ok(false)
}
}
Some(mut body) => {
swap(deserialized_response.body_mut(), &mut body);
// reset parsed body info for next response parsing iteration, allowing for use of keep-alive
self.body_info = None;
Ok(true)
}
}
}
fn deserialize_frame<'a>(
&'a mut self,
_data: &'a [u8],
) -> Result<crate::frame::SizedFrame<Self::DeserializedFrame<'a>>, Error> {
// return response that was deserialized in `check_deserialize_frame(..)`
Ok(SizedFrame::new(
self.deserialized_response
.take()
.ok_or_else(|| Error::new(ErrorKind::Other, "no deserialized frame"))?,
self.deserialized_size,
))
}
}
/// A [`SerializeFrame`] impl for HTTP 1.x where [`SerializeFrame::SerializedFrame`] is an [`HttpRequest`].
pub struct Http1RequestSerializer {}
impl Http1RequestSerializer {
pub fn new() -> Self {
Self {}
}
}
impl SerializeFrame for Http1RequestSerializer {
type SerializedFrame<'a> = HttpRequest;
fn serialize_frame<'a>(
&mut self,
request: Self::SerializedFrame<'a>,
buffer: &mut GrowableCircleBuf,
) -> Result<PublishOutcome<Self::SerializedFrame<'a>>, Error> {
let serialized_request = match request {
HttpRequest::Request(request) => {
// check version
match request.version() {
hyperium_http::Version::HTTP_10 | hyperium_http::Version::HTTP_11 => {}
version => {
return Err(Error::new(
ErrorKind::InvalidData,
format!("unsupported http request version {version:?}").as_str(),
))
}
}
// parse uri
let host = match request.uri().host() {
Some(x) => x.to_owned(),
None => return Err(io::Error::new(ErrorKind::InvalidData, "missing host")),
};
// calculate content-length
let body = request.body();
let content_length = body.len().to_string();
// construct HTTP/1.x payload
let mut serialized_request = Vec::new();
serialized_request.extend_from_slice(request.method().as_str().as_bytes());
serialized_request.extend_from_slice(" ".as_bytes());
serialized_request.extend_from_slice(request.uri().path().as_bytes());
if let Some(query) = request.uri().query() {
serialized_request.extend_from_slice("?".as_bytes());
serialized_request.extend_from_slice(query.as_bytes());
}
serialized_request
.extend_from_slice(format!(" {:?}", request.version()).as_bytes());
serialized_request.extend_from_slice(LINE_BREAK.as_bytes());
{
// host header
serialized_request.extend_from_slice(HOST.as_str().as_bytes());
serialized_request.extend_from_slice(": ".as_bytes());
serialized_request.extend_from_slice(host.as_bytes());
serialized_request.extend_from_slice(LINE_BREAK.as_bytes());
}
for (n, v) in request.headers().iter() {
// request headers
serialized_request.extend_from_slice(n.as_str().as_bytes());
serialized_request.extend_from_slice(": ".as_bytes());
serialized_request.extend_from_slice(
v.to_str()
.map_err(|_| {
Error::new(
ErrorKind::InvalidData,
format!("could not convert header '{}' to string", n.as_str())
.as_str(),
)
})?
.as_bytes(),
);
serialized_request.extend_from_slice(LINE_BREAK.as_bytes());
}
if body.len() > 0 {
// content length header
serialized_request.extend_from_slice(CONTENT_LENGTH.as_str().as_bytes());
serialized_request.extend_from_slice(": ".as_bytes());
serialized_request.extend_from_slice(content_length.as_bytes());
serialized_request.extend_from_slice(LINE_BREAK.as_bytes());
}
serialized_request.extend_from_slice(LINE_BREAK.as_bytes());
serialized_request.extend_from_slice(body);
serialized_request
}
HttpRequest::Serialized(serialized) => serialized,
};
// returned pending request
if buffer.try_write(&vec![&serialized_request])? {
Ok(PublishOutcome::Published)
} else {
Ok(PublishOutcome::Incomplete(HttpRequest::Serialized(
serialized_request,
)))
}
}
}
fn parsed_into_response(
parsed: httparse::Response,
resp: &mut http::Response<Vec<u8>>,
) -> Result<(), Error> {
// status code
if let Some(code) = parsed.code {
let status_code = hyperium_http::StatusCode::try_from(code).map_err(|_| {
Error::new(
ErrorKind::InvalidData,
format!("response invalid status code '{code}'").as_str(),
)
})?;
*resp.status_mut() = status_code;
}
// version
if let Some(version) = parsed.version {
*resp.version_mut() = match version {
0 => hyperium_http::Version::HTTP_10,
1 => hyperium_http::Version::HTTP_11,
_ => {
return Err(Error::new(
ErrorKind::InvalidData,
format!("response invalid version '{version}'").as_str(),
))
}
};
}
// headers
for h in parsed.headers.iter() {
let name = http::HeaderName::from_str(h.name).map_err(|_| {
Error::new(
ErrorKind::InvalidData,
format!("response invalid header name '{}'", h.name).as_str(),
)
})?;
let value = http::HeaderValue::from_bytes(h.value).map_err(|_| {
Error::new(
ErrorKind::InvalidData,
format!("response invalid header value '{:?}'", h.value).as_str(),
)
})?;
resp.headers_mut().insert(name, value);
}
Ok(())
}
const LINE_BREAK: &str = "\r\n";
fn count_max_headers(payload: &[u8]) -> usize {
if payload.is_empty() {
return 0;
}
let mut count = 0;
for i in 0..payload.len() - 1 {
if payload[i] == b'\r' && payload[i + 1] == b'\n' {
count += 1;
}
}
count
}
fn ends_with_ascii(buf: &[u8], ends_with: &str) -> bool {
if buf.len() < ends_with.len() {
return false;
}
let ends_with = ends_with.as_bytes();
for i in 0..ends_with.len() {
if buf[buf.len() - i - 1] != ends_with[ends_with.len() - i - 1] {
return false;
}
}
true
}
fn parse_is_chunked(headers: &[httparse::Header]) -> bool {
match find_header(&headers, TRANSFER_ENCODING.as_str()) {
Some(v) => String::from_utf8_lossy(v).eq_ignore_ascii_case("chunked"),
None => false,
}
}
fn parse_content_length(headers: &[httparse::Header]) -> Result<Option<usize>, Error> {
if let Some(v) = find_header(headers, CONTENT_LENGTH.as_str()) {
let v = String::from_utf8_lossy(v);
return Ok(Some(v.parse().map_err(|_| {
Error::new(ErrorKind::InvalidData, "content-length not a number")
})?));
}
Ok(None)
}
fn find_header<'a>(headers: &'a [httparse::Header], name: &str) -> Option<&'a [u8]> {
for h in headers.iter() {
if h.name.eq_ignore_ascii_case(name) {
return Some(h.value);
}
}
None
}