### Handle Specific icaparse Errors Source: https://context7.com/chills42/icaparse/llms.txt Use a match statement to handle specific error types returned by the parse function. This example demonstrates handling TooManyHeaders, HeaderName, HeaderValue, Version, Token, NewLine, Status, and MissingEncapsulated errors. ```rust use icaparse::{Request, Response, EMPTY_HEADER, Error}; // Handle specific error types let mut headers = [EMPTY_HEADER; 2]; let mut req = Request::new(&mut headers); // Too many headers error let buf = b"REQMOD / ICAP/1.0\r\n\ Header1: value1\r\n\ Header2: value2\r\n\ Header3: value3\r\n\ Encapsulated: null-body=0\r\n\ \r\n"; match req.parse(buf) { Err(Error::TooManyHeaders) => println!("Increase header buffer size"), Err(Error::HeaderName) => println!("Invalid character in header name"), Err(Error::HeaderValue) => println!("Invalid character in header value"), Err(Error::Version) => println!("Invalid ICAP version string"), Err(Error::Token) => println!("Invalid token in request line"), Err(Error::NewLine) => println!("Invalid line ending"), Err(Error::Status) => println!("Invalid status code or reason"), Err(Error::MissingEncapsulated) => println!("REQMOD/RESPMOD requires Encapsulated header"), Ok(_) => println!("Parsing succeeded"), } ``` -------------------------------- ### Parse ICAP requests with icaparse Source: https://github.com/chills42/icaparse/blob/master/README.md Demonstrates initializing a request parser and handling partial or complete data buffers. ```rust let mut headers = [icaparse::EMPTY_HEADER; 16]; let mut req = icaparse::Request::new(&mut headers); let buf = b"RESPMOD /index.html ICAP/1.0\r\nHost"; assert!(try!(req.parse(buf)).is_partial()); // a partial request, so we try again once we have more data let buf = b"RESPMOD /index.html ICAP/1.0\r\nHost: example.domain\r\nEncapsulated:null-body=0\r\n\r\n"; assert!(try!(req.parse(buf)).is_complete()); ``` -------------------------------- ### Access Encapsulated Sections Source: https://context7.com/chills42/icaparse/llms.txt Demonstrates how to parse an ICAP message and access specific encapsulated sections like request headers, response headers, or response bodies. ```rust use icaparse::{Request, EMPTY_HEADER, Status, SectionType}; let mut headers = [EMPTY_HEADER; 16]; let mut req = Request::new(&mut headers); // RESPMOD with encapsulated HTTP response let buf = b"RESPMOD icap://icap.example.org/satisf ICAP/1.0\r\n\ Host: icap.example.org\r\n\ Encapsulated: req-hdr=0, res-hdr=62, res-body=200\r\n\ \r\n\ GET /resource HTTP/1.1\r\n\ Host: www.origin-server.com\r\n\ \r\n\ HTTP/1.1 200 OK\r\n\ Content-Type: text/html\r\n\ Content-Length: 51\r\n\ \r\n\ 33\r\n\ This is the response body from origin server.\r\n\ 0\r\n\ \r\n"; if let Ok(Status::Complete(_)) = req.parse(buf) { if let Some(ref sections) = req.encapsulated_sections { // Access different section types if let Some(req_hdr) = sections.get(&SectionType::RequestHeader) { println!("HTTP Request:\n{}", String::from_utf8_lossy(req_hdr)); } if let Some(res_hdr) = sections.get(&SectionType::ResponseHeader) { println!("HTTP Response:\n{}", String::from_utf8_lossy(res_hdr)); } if let Some(res_body) = sections.get(&SectionType::ResponseBody) { println!("Response Body:\n{}", String::from_utf8_lossy(res_body)); } } } ``` -------------------------------- ### Handle Incremental Parsing Results Source: https://context7.com/chills42/icaparse/llms.txt Uses the Status enum to manage partial or complete parsing results, allowing for efficient processing of data arriving in chunks. ```rust use icaparse::{Request, EMPTY_HEADER, Status}; let mut headers = [EMPTY_HEADER; 16]; let mut req = Request::new(&mut headers); // Simulate incremental data arrival let partial_data = b"RESPMOD /index.html ICAP/1.0\r\nHost"; match req.parse(partial_data) { Ok(status) if status.is_partial() => { // Can inspect partially parsed fields println!("Method so far: {:?}", req.method); // Some("RESPMOD") println!("Path so far: {:?}", req.path); // Some("/index.html") println!("Waiting for more data..."); }, Ok(status) if status.is_complete() => { let bytes_consumed = status.unwrap(); println!("Complete! Parsed {} bytes", bytes_consumed); }, Err(e) => println!("Parse error: {}", e), _ => {} } // Later, when full data arrives, parse again let full_data = b"RESPMOD /index.html ICAP/1.0\r\n\ Host: example.domain\r\n\ Encapsulated: null-body=0\r\n\ \r\n"; let mut headers2 = [EMPTY_HEADER; 16]; let mut req2 = Request::new(&mut headers2); assert!(req2.parse(full_data).unwrap().is_complete()); ``` -------------------------------- ### Parse ICAP Request Messages Source: https://context7.com/chills42/icaparse/llms.txt Parses REQMOD, RESPMOD, and OPTIONS messages using a pre-allocated header buffer. Requires an Encapsulated header for request/response modification methods. ```rust use icaparse::{Request, EMPTY_HEADER, Status, SectionType}; // Allocate header buffer (no heap allocations during parsing) let mut headers = [EMPTY_HEADER; 16]; let mut req = Request::new(&mut headers); // Parse a REQMOD request with encapsulated HTTP request let buf = b"REQMOD icap://icap-server.net/server?arg=87 ICAP/1.0\r\n\ Host: icap-server.net\r\n\ Encapsulated: req-hdr=0, null-body=170\r\n\ \r\n\ GET / HTTP/1.1\r\n\ Host: www.origin-server.com\r\n\ Accept: text/html, text/plain\r\n\ \r\n"; match req.parse(buf) { Ok(Status::Complete(len)) => { println!("Parsed {} bytes", len); println!("Method: {}", req.method.unwrap()); // "REQMOD" println!("Path: {}", req.path.unwrap()); // "icap://icap-server.net/server?arg=87" println!("Version: ICAP/1.{}", req.version.unwrap()); // "ICAP/1.0" // Access headers for header in req.headers.iter() { println!("{}: {}", header.name, String::from_utf8_lossy(header.value)); } // Access encapsulated sections (HTTP request/response bodies) if let Some(ref sections) = req.encapsulated_sections { if let Some(http_req) = sections.get(&SectionType::RequestHeader) { println!("Encapsulated HTTP request:\n{}", String::from_utf8_lossy(http_req)); } } }, Ok(Status::Partial) => { println!("Need more data, buffer again and retry"); }, Err(e) => { println!("Parse error: {}", e); } } ``` -------------------------------- ### Status Enum Source: https://context7.com/chills42/icaparse/llms.txt Handles incremental parsing results for ICAP messages. ```APIDOC ## Status ### Description Represents the result of an incremental parsing operation. It indicates whether the parsing is complete or if more data is required. ### Methods - **is_complete()** - Returns true if the parsing operation has finished. - **is_partial()** - Returns true if more data is needed to complete the parsing. - **unwrap()** - Returns the number of bytes consumed if the parsing is complete. ``` -------------------------------- ### Parse Raw Header Data Source: https://context7.com/chills42/icaparse/llms.txt Parses raw header buffers into a slice of Header structs. Returns the number of bytes consumed and the parsed headers. ```rust use icaparse::{parse_headers, EMPTY_HEADER, Status, Header}; let buf = b"Host: example.com\r\nContent-Type: text/plain\r\nX-Custom: value\r\n\r\nBody content here"; let mut headers = [EMPTY_HEADER; 8]; match parse_headers(buf, &mut headers) { Ok(Status::Complete((bytes_consumed, parsed_headers))) => { println!("Consumed {} bytes", bytes_consumed); // 63 println!("Found {} headers", parsed_headers.len()); // 3 for header in parsed_headers { println!("{}: {}", header.name, String::from_utf8_lossy(header.value)); } // Output: // Host: example.com // Content-Type: text/plain // X-Custom: value }, Ok(Status::Partial) => println!("Incomplete headers, need more data"), Err(e) => println!("Invalid header: {}", e), } ``` -------------------------------- ### Parse ICAP Response Messages Source: https://context7.com/chills42/icaparse/llms.txt Parses ICAP response messages including status codes and headers. Supports incremental parsing for non-blocking I/O. ```rust use icaparse::{Response, EMPTY_HEADER, Status}; let mut headers = [EMPTY_HEADER; 16]; let mut res = Response::new(&mut headers); // Parse an ICAP response let buf = b"ICAP/1.0 200 OK\r\n\ ISTag: \"W3E4R7U9-L2E4-2\"\r\n\ Date: Mon, 10 Jan 2000 09:55:21 GMT\r\n\ Server: Example-ICAP-Server/1.0\r\n\ \r\n"; match res.parse(buf) { Ok(Status::Complete(len)) => { println!("Parsed {} bytes", len); println!("Version: ICAP/1.{}", res.version.unwrap()); // 0 println!("Status: {}", res.code.unwrap()); // 200 println!("Reason: {}", res.reason.unwrap()); // "OK" for header in res.headers.iter() { println!("{}: {}", header.name, String::from_utf8_lossy(header.value)); } }, Ok(Status::Partial) => println!("Need more data"), Err(e) => println!("Parse error: {}", e), } ``` -------------------------------- ### Parse Transfer-Encoding Chunk Sizes Source: https://context7.com/chills42/icaparse/llms.txt Parses hexadecimal chunk sizes from a byte buffer. Returns the position after the chunk size line and the parsed size value. ```rust use icaparse::{parse_chunk_size, Status}; // Parse various chunk size formats let examples = [ b"0\r\n".as_ref(), // Terminal chunk b"12\r\nchunk data".as_ref(), // Chunk size 18 (0x12) b"3086d\r\n".as_ref(), // Chunk size 198765 b"ff\r\n".as_ref(), // Chunk size 255 b"3735AB1;extension\r\n".as_ref(), // With chunk extension ]; for buf in &examples { match parse_chunk_size(buf) { Ok(Status::Complete((pos, size))) => { println!("Position: {}, Chunk size: {} bytes", pos, size); }, Ok(Status::Partial) => println!("Need more data"), Err(_) => println!("Invalid chunk size"), } } // Output: // Position: 3, Chunk size: 0 bytes // Position: 4, Chunk size: 18 bytes // Position: 7, Chunk size: 198765 bytes // Position: 4, Chunk size: 255 bytes // Position: 20, Chunk size: 57891505 bytes ``` -------------------------------- ### Parse ICAP Response Messages Source: https://context7.com/chills42/icaparse/llms.txt The Response struct parses ICAP response messages, extracting the version, status code, reason phrase, and headers. ```APIDOC ## Parse ICAP Response ### Description Parses an ICAP response message, supporting incremental parsing for non-blocking I/O. ### Request Body - **buf** (byte slice) - Required - The raw ICAP response message buffer. ### Response - **Status::Complete(len)** - Returns the number of bytes consumed upon successful parsing. - **Status::Partial** - Indicates that more data is required to complete the parse. - **Err** - Returns an error if the ICAP message is malformed. ``` -------------------------------- ### Parse ICAP Request Messages Source: https://context7.com/chills42/icaparse/llms.txt The Request struct parses ICAP request messages (REQMOD, RESPMOD, OPTIONS), extracting methods, paths, versions, headers, and encapsulated HTTP sections. ```APIDOC ## Parse ICAP Request ### Description Parses an ICAP request message using a pre-allocated header buffer to avoid heap allocations. ### Request Body - **buf** (byte slice) - Required - The raw ICAP request message buffer. ### Response - **Status::Complete(len)** - Returns the number of bytes consumed upon successful parsing. - **Status::Partial** - Indicates that more data is required to complete the parse. - **Err** - Returns an error if the ICAP message is malformed. ``` -------------------------------- ### Parse Raw Header Data Source: https://context7.com/chills42/icaparse/llms.txt The parse_headers function parses a buffer containing raw headers into a slice of Header structs. ```APIDOC ## parse_headers ### Description Parses raw header data from a buffer into a provided slice of Header structs. ### Request Body - **buf** (byte slice) - Required - The raw header data. - **headers** (slice of Header) - Required - Pre-allocated buffer to store parsed headers. ### Response - **Status::Complete((bytes_consumed, parsed_headers))** - Returns the number of bytes consumed and the slice of parsed headers. - **Status::Partial** - Indicates that the headers are incomplete. - **Err** - Returns an error if the header format is invalid. ``` -------------------------------- ### parse_chunk_size Source: https://context7.com/chills42/icaparse/llms.txt Parses hexadecimal chunk sizes used in chunked transfer encoding. ```APIDOC ## parse_chunk_size ### Description Parses hexadecimal chunk sizes from a byte buffer, returning the position after the chunk size line and the parsed size value. ### Parameters - **buf** (&[u8]) - Required - The byte buffer containing the chunk size string. ### Response - **Result>** - Returns a Status containing the position and the parsed size value, or an error if the chunk size is invalid. ``` -------------------------------- ### SectionType and Encapsulated Sections Source: https://context7.com/chills42/icaparse/llms.txt Represents and accesses different sections of encapsulated data in ICAP messages. ```APIDOC ## SectionType ### Description Defines the types of encapsulated sections within an ICAP message, such as RequestHeader, ResponseHeader, and ResponseBody. ### Usage - **SectionType::RequestHeader** - Represents encapsulated HTTP request headers. - **SectionType::ResponseHeader** - Represents encapsulated HTTP response headers. - **SectionType::ResponseBody** - Represents encapsulated HTTP response body data. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.