[][src]Function embedded_websocket::read_http_header

pub fn read_http_header(buffer: &[u8]) -> Result<HttpHeader>

Reads a buffer and extracts the HttpHeader information from it, including websocket specific information.

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 http_header = ws::read_http_header(&client_request.as_bytes()).unwrap();
let ws_context = http_header.websocket_context.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());

Errors

  • Will return HttpHeaderNoPath if no path is specified in the http header (e.g. /chat). This is the most likely error message to receive if the input buffer does not contain any part of a valid http header at all.
  • Will return HttpHeaderIncomplete if the input buffer does not contain a complete http header. Http headers must end with \r\n\r\n to be valid. Therefore, since http headers are not framed, (i.e. not length prefixed) the idea is to read from a network stream in a loop until you no longer receive a HttpHeaderIncomplete error code and that is when you know that the entire header has been read