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
//! HTTP transaction components.
//!
//! ## Remarks
//! Retina currently only parses HTTP headers, and does not yet parse request/response bodies. This
//! is an upcoming feature.
use anyhow::{bail, Result};
use httparse::{Request, Response, EMPTY_HEADER};
use serde::Serialize;
/// An HTTP Request
#[derive(Debug, Default, Serialize, Clone)]
pub struct HttpRequest {
pub method: Option<String>,
pub uri: Option<String>,
pub version: Option<String>,
pub user_agent: Option<String>,
pub cookie: Option<String>,
pub host: Option<String>,
pub content_length: Option<usize>,
pub content_type: Option<String>,
pub transfer_encoding: Option<String>,
// /// `false` if request body needs continuation pub is_complete: bool, /// Actual length in
// bytes of body data transferred from the client. pub body_len: usize,
}
impl HttpRequest {
pub(crate) fn parse_from(data: &[u8]) -> Result<Self> {
let mut request = HttpRequest::default();
const NUM_OF_HEADERS: usize = 20;
let mut headers = [EMPTY_HEADER; NUM_OF_HEADERS];
let mut req = Request::new(&mut headers[..]);
let status = req.parse(data);
if status.is_err() {
bail!("error");
}
if let Some(method) = req.method {
request.method = Some(method.to_owned());
}
if let Some(uri) = req.path {
request.uri = Some(uri.to_owned());
}
if let Some(version) = req.version {
request.version = Some(format!("HTTP/1.{}", version));
}
for hdr in &headers {
let name = hdr.name.to_lowercase();
match name.as_ref() {
"user-agent" => {
let s = String::from_utf8_lossy(hdr.value).into_owned();
request.user_agent = Some(s);
}
"cookie" => {
let s = String::from_utf8_lossy(hdr.value).into_owned();
request.cookie = Some(s);
}
"host" => {
let s = String::from_utf8_lossy(hdr.value);
request.host = Some(s.to_string());
}
"content-length" => {
if let Ok(s) = std::str::from_utf8(hdr.value) {
if let Ok(length) = str::parse::<usize>(s) {
request.content_length = Some(length);
}
}
}
"content-type" => {
let s = String::from_utf8_lossy(hdr.value).into_owned();
request.content_type = Some(s);
}
"transfer-encoding" => {
let s = String::from_utf8_lossy(hdr.value).to_lowercase();
request.transfer_encoding = Some(s);
// if &s == "chunked" {if let Ok(httparse::Status::Complete(sz)) = status {start
// = sz;} else {log::warn!("Parsing response failed"); return
// ParseResult::Error;
// }
// return request.get_chunk_loop(start, data, false);
// }
}
_ => (),
}
}
Ok(request)
}
}
/// An HTTP Response
#[derive(Debug, Default, Serialize, Clone)]
pub struct HttpResponse {
pub version: Option<String>,
pub status_code: Option<u16>,
pub status_msg: Option<String>,
pub content_length: Option<usize>,
pub content_type: Option<String>,
pub transfer_encoding: Option<String>,
// /// `false` if response body needs continuation pub is_complete: bool, /// Actual length in
// bytes of body data transferred from the server. pub body_len: usize, pub chunk_length:
// Option<usize>, pub in_next_frame: bool,
}
impl HttpResponse {
pub(crate) fn parse_from(data: &[u8]) -> Result<Self> {
let mut response = HttpResponse::default();
const NUM_OF_HEADERS: usize = 20;
let mut headers = [EMPTY_HEADER; NUM_OF_HEADERS];
let mut resp = Response::new(&mut headers[..]);
let status = resp.parse(data);
if status.is_err() {
bail!("error");
}
if let Some(version) = resp.version {
response.version = Some(format!("HTTP/1.{}", version));
}
if let Some(code) = resp.code {
response.status_code = Some(code);
}
if let Some(reason) = resp.reason {
response.status_msg = Some(reason.to_owned());
}
for hdr in &headers {
let name = hdr.name.to_lowercase();
match name.as_ref() {
"content-length" => {
if let Ok(s) = std::str::from_utf8(hdr.value) {
if let Ok(length) = str::parse::<usize>(s) {
// if let Ok(httparse::Status::Complete(sz)) = status {response.body =
// data[sz..].to_vec();
// }
response.content_length = Some(length);
// if length > response.body.len() {need_more = true;
// }
// continue;
}
}
}
"content-type" => {
let s = String::from_utf8_lossy(hdr.value).into_owned();
response.content_type = Some(s);
}
"transfer-encoding" => {
let s = String::from_utf8_lossy(hdr.value).to_lowercase();
response.transfer_encoding = Some(s);
// if &s == "chunked" {if let Ok(httparse::Status::Complete(sz)) = status {start
// = sz;} else {log::warn!("Parsing response failed"); return
// ParseResult::Error;
// }
// return response.get_chunk_loop(start, data, false);
// }
}
_ => (),
}
}
Ok(response)
}
}