| @@ -25,9 +25,11 @@ |
| 25 | 25 | |
| 26 | 26 | use louise_rt_prims::SMALL_INT; |
| 27 | 27 | |
| 28 | +use std::sync::OnceLock; |
| 29 | + |
| 28 | 30 | use crate::abi::{Ctx, Value, int}; |
| 29 | 31 | use crate::int::saturating; |
| 30 | | −use crate::values::{bytes_of, cell, list, string, text}; |
| 32 | +use crate::values::{bytes_of, cell, immortal_string, list, string, text}; |
| 31 | 33 | |
| 32 | 34 | /// Headers a request may have; a request with more is invalid. |
| 33 | 35 | const MAX_HEADERS: usize = 64; |
| @@ -193,6 +195,171 @@ unsafe fn offset(at: Value) -> usize { |
| 193 | 195 | usize::try_from(unsafe { saturating(at) }).unwrap_or(0) |
| 194 | 196 | } |
| 195 | 197 | |
| 198 | +/// The request header names virtually every request carries, in both |
| 199 | +/// spellings seen in practice (browsers and curl capitalise; many |
| 200 | +/// clients, and HTTP/2, lowercase), sorted for the binary search in |
| 201 | +/// [`interned`]. |
| 202 | +const COMMON_HEADER_NAMES: [&str; 66] = [ |
| 203 | + "Accept", |
| 204 | + "Accept-Charset", |
| 205 | + "Accept-Encoding", |
| 206 | + "Accept-Language", |
| 207 | + "Authorization", |
| 208 | + "Cache-Control", |
| 209 | + "Connection", |
| 210 | + "Content-Length", |
| 211 | + "Content-Type", |
| 212 | + "Cookie", |
| 213 | + "DNT", |
| 214 | + "Date", |
| 215 | + "Expect", |
| 216 | + "Forwarded", |
| 217 | + "Host", |
| 218 | + "If-Modified-Since", |
| 219 | + "If-None-Match", |
| 220 | + "Last-Event-ID", |
| 221 | + "Origin", |
| 222 | + "Pragma", |
| 223 | + "Range", |
| 224 | + "Referer", |
| 225 | + "Sec-Fetch-Dest", |
| 226 | + "Sec-Fetch-Mode", |
| 227 | + "Sec-Fetch-Site", |
| 228 | + "Sec-Fetch-User", |
| 229 | + "TE", |
| 230 | + "Trailer", |
| 231 | + "Transfer-Encoding", |
| 232 | + "Upgrade-Insecure-Requests", |
| 233 | + "User-Agent", |
| 234 | + "Via", |
| 235 | + "X-Forwarded-For", |
| 236 | + "X-Forwarded-Host", |
| 237 | + "X-Forwarded-Proto", |
| 238 | + "X-Real-Ip", |
| 239 | + "X-Request-Id", |
| 240 | + "X-Requested-With", |
| 241 | + "accept", |
| 242 | + "accept-encoding", |
| 243 | + "accept-language", |
| 244 | + "authorization", |
| 245 | + "cache-control", |
| 246 | + "connection", |
| 247 | + "content-length", |
| 248 | + "content-type", |
| 249 | + "cookie", |
| 250 | + "dnt", |
| 251 | + "expect", |
| 252 | + "forwarded", |
| 253 | + "host", |
| 254 | + "if-modified-since", |
| 255 | + "if-none-match", |
| 256 | + "last-event-id", |
| 257 | + "origin", |
| 258 | + "pragma", |
| 259 | + "range", |
| 260 | + "referer", |
| 261 | + "te", |
| 262 | + "trailer", |
| 263 | + "transfer-encoding", |
| 264 | + "user-agent", |
| 265 | + "via", |
| 266 | + "x-forwarded-for", |
| 267 | + "x-request-id", |
| 268 | + "x-requested-with", |
| 269 | +]; |
| 270 | + |
| 271 | +/// The index of `name` in [`COMMON_HEADER_NAMES`], if it is one. A |
| 272 | +/// `match` compiles to a length switch and a few byte compares: cheaper |
| 273 | +/// than the allocation a hit saves. |
| 274 | +fn interned_index(name: &str) -> Option<usize> { |
| 275 | + Some(match name { |
| 276 | + "Accept" => 0, |
| 277 | + "Accept-Charset" => 1, |
| 278 | + "Accept-Encoding" => 2, |
| 279 | + "Accept-Language" => 3, |
| 280 | + "Authorization" => 4, |
| 281 | + "Cache-Control" => 5, |
| 282 | + "Connection" => 6, |
| 283 | + "Content-Length" => 7, |
| 284 | + "Content-Type" => 8, |
| 285 | + "Cookie" => 9, |
| 286 | + "DNT" => 10, |
| 287 | + "Date" => 11, |
| 288 | + "Expect" => 12, |
| 289 | + "Forwarded" => 13, |
| 290 | + "Host" => 14, |
| 291 | + "If-Modified-Since" => 15, |
| 292 | + "If-None-Match" => 16, |
| 293 | + "Last-Event-ID" => 17, |
| 294 | + "Origin" => 18, |
| 295 | + "Pragma" => 19, |
| 296 | + "Range" => 20, |
| 297 | + "Referer" => 21, |
| 298 | + "Sec-Fetch-Dest" => 22, |
| 299 | + "Sec-Fetch-Mode" => 23, |
| 300 | + "Sec-Fetch-Site" => 24, |
| 301 | + "Sec-Fetch-User" => 25, |
| 302 | + "TE" => 26, |
| 303 | + "Trailer" => 27, |
| 304 | + "Transfer-Encoding" => 28, |
| 305 | + "Upgrade-Insecure-Requests" => 29, |
| 306 | + "User-Agent" => 30, |
| 307 | + "Via" => 31, |
| 308 | + "X-Forwarded-For" => 32, |
| 309 | + "X-Forwarded-Host" => 33, |
| 310 | + "X-Forwarded-Proto" => 34, |
| 311 | + "X-Real-Ip" => 35, |
| 312 | + "X-Request-Id" => 36, |
| 313 | + "X-Requested-With" => 37, |
| 314 | + "accept" => 38, |
| 315 | + "accept-encoding" => 39, |
| 316 | + "accept-language" => 40, |
| 317 | + "authorization" => 41, |
| 318 | + "cache-control" => 42, |
| 319 | + "connection" => 43, |
| 320 | + "content-length" => 44, |
| 321 | + "content-type" => 45, |
| 322 | + "cookie" => 46, |
| 323 | + "dnt" => 47, |
| 324 | + "expect" => 48, |
| 325 | + "forwarded" => 49, |
| 326 | + "host" => 50, |
| 327 | + "if-modified-since" => 51, |
| 328 | + "if-none-match" => 52, |
| 329 | + "last-event-id" => 53, |
| 330 | + "origin" => 54, |
| 331 | + "pragma" => 55, |
| 332 | + "range" => 56, |
| 333 | + "referer" => 57, |
| 334 | + "te" => 58, |
| 335 | + "trailer" => 59, |
| 336 | + "transfer-encoding" => 60, |
| 337 | + "user-agent" => 61, |
| 338 | + "via" => 62, |
| 339 | + "x-forwarded-for" => 63, |
| 340 | + "x-request-id" => 64, |
| 341 | + "x-requested-with" => 65, |
| 342 | + _ => return None, |
| 343 | + }) |
| 344 | +} |
| 345 | + |
| 346 | +/// `name` as a Louise string: a shared immortal one when it is a common |
| 347 | +/// name spelled a common way (no allocation), else a fresh string. The |
| 348 | +/// string holds the name's bytes exactly as received either way. |
| 349 | +fn header_name(ctx: *mut Ctx, name: &str) -> Value { |
| 350 | + static NAMES: OnceLock<Vec<Value>> = OnceLock::new(); |
| 351 | + let names = NAMES.get_or_init(|| { |
| 352 | + COMMON_HEADER_NAMES |
| 353 | + .iter() |
| 354 | + .map(|n| immortal_string(n)) |
| 355 | + .collect() |
| 356 | + }); |
| 357 | + match interned_index(name) { |
| 358 | + Some(i) => names[i], |
| 359 | + None => string(ctx, name), |
| 360 | + } |
| 361 | +} |
| 362 | + |
| 196 | 363 | /// `parse_head(buffer, at)` (`std/http`): the Rust `parse_head` as a |
| 197 | 364 | /// `Head`: `Partial`, `Invalid(reason)`, or `Complete(method, path, |
| 198 | 365 | /// version, headers, end, content_length)`, where `content_length` is -1 |
| @@ -213,7 +380,7 @@ pub unsafe extern "C" fn tl_http_parse_request(ctx: *mut Ctx, buffer: Value, at: |
| 213 | 380 | ctx, |
| 214 | 381 | req.headers.iter().map(|(name, value)| { |
| 215 | 382 | let value = String::from_utf8_lossy(value); |
| 216 | | − cell(ctx, 0, &[string(ctx, name), string(ctx, &value)]) |
| 383 | + cell(ctx, 0, &[header_name(ctx, name), string(ctx, &value)]) |
| 217 | 384 | }), |
| 218 | 385 | ); |
| 219 | 386 | cell( |
| @@ -479,6 +646,16 @@ mod tests { |
| 479 | 646 | } |
| 480 | 647 | |
| 481 | 648 | #[test] |
| 649 | + fn the_common_header_names_table_is_sorted() { |
| 650 | + assert!(COMMON_HEADER_NAMES.is_sorted()); |
| 651 | + assert!( |
| 652 | + COMMON_HEADER_NAMES |
| 653 | + .iter() |
| 654 | + .all(|n| n.bytes().all(|b| b.is_ascii_graphic())) |
| 655 | + ); |
| 656 | + } |
| 657 | + |
| 658 | + #[test] |
| 482 | 659 | fn chunks_of_data_and_where_they_start() { |
| 483 | 660 | assert_eq!( |
| 484 | 661 | parse_chunk(b"5\r\nhello\r\n", 0), |
| @@ -593,7 +770,23 @@ mod tests { |
| 593 | 770 | assert_eq!(tag(invalid), CHUNK_INVALID); |
| 594 | 771 | let partial = tl_http_parse_chunk(ctx, chunks, int(99)); |
| 595 | 772 | assert_eq!(partial, int(CHUNK_PARTIAL)); |
| 596 | | − for v in [head, complete, again, bad, chunks, data, end, invalid] { |
| 773 | + // A common name is the same immortal string on every parse; |
| 774 | + // an uncommon one is fresh each time, its bytes exact. |
| 775 | + let common = bytes_from(ctx, b"GET / HTTP/1.1\r\nHost: a\r\nX-Weird: b\r\n\r\n"); |
| 776 | + let first = tl_http_parse_request(ctx, common, int(0)); |
| 777 | + let second = tl_http_parse_request(ctx, common, int(0)); |
| 778 | + let name = |v: Value, i: usize| { |
| 779 | + let mut items = list_items(field(v, 3)); |
| 780 | + let pair = items.nth(i).unwrap(); |
| 781 | + field(pair, 0) |
| 782 | + }; |
| 783 | + assert_eq!(name(first, 0), name(second, 0)); |
| 784 | + assert_eq!(text(name(first, 0)), "Host"); |
| 785 | + assert_ne!(name(first, 1), name(second, 1)); |
| 786 | + assert_eq!(text(name(first, 1)), "X-Weird"); |
| 787 | + for v in [ |
| 788 | + head, complete, again, bad, chunks, data, end, invalid, common, first, second, |
| 789 | + ] { |
| 597 | 790 | drop_value(ctx, v); |
| 598 | 791 | } |
| 599 | 792 | } |