pub fn read_http_header<'a>(
    headers: impl Iterator<Item = (&'a str, &'a [u8])>,
) -> Result<Option<WebSocketContext>> {
    let mut sec_websocket_protocol_list: Vec<String<U24>, U3> = Vec::new();
    let mut is_websocket_request = false;
    let mut sec_websocket_key = String::new();

    for (name, value) in headers {
        match name {
            "Upgrade" => is_websocket_request = str::from_utf8(value)? == "websocket",
            "Sec-WebSocket-Protocol" => {
                // extract a csv list of supported sub protocols
                for item in str::from_utf8(value)?.split(", ") {
                    if sec_websocket_protocol_list.len() < sec_websocket_protocol_list.capacity() {
                        // it is safe to unwrap here because we have checked
                        // the size of the list beforehand
                        sec_websocket_protocol_list
                            .push(String::from(item))
                            .unwrap();
                    }
                }
            }
            "Sec-WebSocket-Key" => {
                sec_websocket_key = String::from(str::from_utf8(value)?);
            }
            &_ => {
                // ignore all other headers
            }
        }
    }

    if is_websocket_request {
        Ok(Some(WebSocketContext {
            sec_websocket_protocol_list,
            sec_websocket_key,
        }))
    } else {
        Ok(None)
    }
}
// NOTE: this function is re-exported
/// Reads an http header and extracts websocket specific information from it.
///
/// # Examples
/// ```
/// use embedded_websocket as ws;
/// let client_request = "GET /chat HTTP/1.1
/// Host: myserver.example.com
/// Upgrade: websocket
/// Connection: Upgrade
/// Sec-WebSocket-Key: Z7OY1UwHOx/nkSz38kfPwg==
/// Origin: http://example.com
/// Sec-WebSocket-Protocol: chat, advancedchat
/// Sec-WebSocket-Version: 13
///
/// ";
///
/// let mut headers = [httparse::EMPTY_HEADER; 16];
/// let mut request = httparse::Request::new(&mut headers);
/// request.parse(client_request.as_bytes()).unwrap();
/// let headers = request.headers.iter().map(|f| (f.name, f.value));
/// let ws_context = ws::read_http_header(headers).unwrap().unwrap();
/// assert_eq!("Z7OY1UwHOx/nkSz38kfPwg==", ws_context.sec_websocket_key);
/// assert_eq!("chat", ws_context.sec_websocket_protocol_list.get(0).unwrap().as_str());
/// assert_eq!("advancedchat", ws_context.sec_websocket_protocol_list.get(1).unwrap().as_str());
///
/// ```
this function is re-exported
Reads an http header and extracts websocket specific information from it.
this function is re - exported Reads an http header and extracts websocket specific information from it .
pub fn read_http_header<'a>(
    headers: impl Iterator<Item = (&'a str, &'a [u8])>,
) -> Result<Option<WebSocketContext>> {
    let mut sec_websocket_protocol_list: Vec<String<U24>, U3> = Vec::new();
    let mut is_websocket_request = false;
    let mut sec_websocket_key = String::new();
    for (name, value) in headers {
        match name {
            "Upgrade" => is_websocket_request = str::from_utf8(value)? == "websocket",
            "Sec-WebSocket-Protocol" => {
                // extract a csv list of supported sub protocols
                for item in str::from_utf8(value)?.split(", ") {
                    if sec_websocket_protocol_list.len() < sec_websocket_protocol_list.capacity() {
                        // it is safe to unwrap here because we have checked
                        // the size of the list beforehand
                        sec_websocket_protocol_list
                            .push(String::from(item))
                            .unwrap();
                    }
                }
            }
            "Sec-WebSocket-Key" => {
                sec_websocket_key = String::from(str::from_utf8(value)?);
            }
            &_ => {
                // ignore all other headers
            }
        }
    }
    if is_websocket_request {
        Ok(Some(WebSocketContext {
            sec_websocket_protocol_list,
            sec_websocket_key,
        }))
    } else {
        Ok(None)
    }
}
pub fn read_http_header < ' a > ( headers : impl Iterator < Item = ( & ' a str , & ' a [ u8 ] ) > , ) -> Result < Option < WebSocketContext > > { let mut sec_websocket_protocol_list : Vec < String < U24 > , U3 > = Vec :: new ( ) ; let mut is_websocket_request = false ; let mut sec_websocket_key = String :: new ( ) ; for ( name , value ) in headers { match name { " " => is_websocket_request = str :: from_utf8 ( value ) ? == " " , " " => { // extract a csv list of supported sub protocols for item in str :: from_utf8 ( value ) ? . split ( " " ) { if sec_websocket_protocol_list . len ( ) < sec_websocket_protocol_list . capacity ( ) { // it is safe to unwrap here because we have checked // the size of the list beforehand sec_websocket_protocol_list . push ( String :: from ( item ) ) . unwrap ( ) ; } } } " " => { sec_websocket_key = String :: from ( str :: from_utf8 ( value ) ? ) ; } & _ => { // ignore all other headers } } } if is_websocket_request { Ok ( Some ( WebSocketContext { sec_websocket_protocol_list , sec_websocket_key , } ) ) } else { Ok ( None ) } }
NOTE: this function is re-exported
Reads an http header and extracts websocket specific information from it.
NOTE : this function is re - exported Reads an http header and extracts websocket specific information from it .
headers
impl Iterator<Item = (&'a str, &'a [u8])>
headers
impl Iterator<Item = (&'a str, &'a [u8])>
examples




let mut headers = [httparse::EMPTY_HEADER; 16];
let mut request = httparse::Request::new(&mut headers);
request.parse(client_request.as_bytes()).unwrap();
let headers = request.headers.iter().map(|f| (f.name, f.value));
let ws_context = ws::read_http_header(headers).unwrap().unwrap();
assert_eq!("Z7OY1UwHOx/nkSz38kfPwg==", ws_context.sec_websocket_key);
assert_eq!("chat", ws_context.sec_websocket_protocol_list.get(0).unwrap().as_str());
assert_eq!("advancedchat", ws_context.sec_websocket_protocol_list.get(1).unwrap().as_str()).


let mut headers = [ httparse :: EMPTY_HEADER ; 16 ] ; let mut request = httparse :: Request :: new ( &mut headers ) ; request . parse ( client_request . as_bytes () ) . unwrap () ; let headers = request . headers . iter () . map ( |f| ( f . name f . value )) ; let ws_context = ws :: read_http_header ( headers ) . unwrap () . unwrap () ; assert_eq! ( " Z7OY1UwHOx / nkSz38kfPwg == " ws_context . sec_websocket_key ) ; assert_eq! ( " chat " ws_context . sec_websocket_protocol_list . get ( 0 ) . unwrap () . as_str () ) ; assert_eq! ( " advancedchat " ws_context . sec_websocket_protocol_list . get ( 1 ) . unwrap () . as_str () ) .