///|
pub(all) struct AdaptationHeaders {
  http : Bytes
  forwarded : Array[(String, String)]
}

///|
pub fn prepare_http(
  start_line : String,
  fields : Array[(String, String)],
) -> Result[AdaptationHeaders, ProtocolError] {
  if start_line.is_empty() || !safe_value(start_line) {
    return Err(Malformed("invalid encapsulated HTTP start line", 0))
  }
  let excluded = [
    "connection", "keep-alive", "proxy-connection", "te", "trailer", "transfer-encoding",
    "upgrade",
  ]
  let nominated : Array[String] = []
  for (name, value) in fields {
    if !token(name) || !safe_value(value) {
      return Err(Malformed("invalid HTTP header", 0))
    }
    if name.to_lower() == "connection" {
      for part in value.split(",") {
        nominated.push(part.trim().to_owned().to_lower())
      }
    }
  }
  let lines = [start_line + "\r\n"]
  let forwarded : Array[(String, String)] = []
  for (name, value) in fields {
    let lower = name.to_lower()
    if lower == "proxy-authorization" || lower == "proxy-authenticate" {
      forwarded.push((name, value))
      continue
    }
    if excluded.contains(lower) || nominated.contains(lower) {
      continue
    }
    lines.push(name + ": " + value + "\r\n")
  }
  lines.push("\r\n")
  Ok({ http: raw(lines.join("")), forwarded, })
}