///|
/// The method of a request.
priv enum Method {
  Get
  Post
  /// The server has no path for another method.
  Other
}

///|
/// One HTTP request. Its body is not here: no request of the server needs one.
priv struct Request {
  method_ : Method
  /// The path as the client sent it.
  path : String
  /// Gives the value of a name in the query, without its encoding. A name that
  /// repeats gives its first value.
  query : (String) -> String?
  /// Gives a header, in any case. A header that repeats gives its values with
  /// commas, so a repeated `Host` or secret is not valid.
  header : (String) -> String?
}

///|
/// True when the headers say that the request has a body.
fn Request::has_body(self : Request) -> Bool {
  (self.header)("Transfer-Encoding") is Some(_) ||
  ((self.header)("Content-Length") is Some(length) && length != "0")
}

///|
/// The status of a reply.
priv enum Status {
  Success
  Accepted
  NoContent
  BadInput
  Forbidden
  NotFound
  MethodNotAllowed
  TooLarge
  /// The app does not do the command. The reply has a text for the user.
  Refused
  InternalError
  Unavailable
}

///|
/// The number of the status in HTTP.
fn Status::to_int(self : Status) -> Int32 {
  match self {
    Success => 200
    Accepted => 202
    NoContent => 204
    BadInput => 400
    Forbidden => 403
    NotFound => 404
    MethodNotAllowed => 405
    TooLarge => 413
    Refused => 422
    InternalError => 500
    Unavailable => 503
  }
}

///|
/// One HTTP reply. `Reply::Reply` adds the headers that each reply has.
priv struct Reply {
  status : Status
  headers : Array[(String, String)]
  body : Bytes
}

///|
/// The rules of each content security policy: no other address for the links of the
/// page, no form, and no page that shows this one in a frame.
let closed_rules : String = "base-uri 'none'; form-action 'none'; frame-ancestors 'none'"

///|
/// A reply that no browser keeps, whose type no browser guesses, and whose address
/// no browser gives to another page. `kind` is its content type, and `policy` is its
/// content security policy. With no `policy`, a page loads nothing.
fn Reply::Reply(
  status : Status,
  kind? : String,
  body~ : Bytes = b"",
  policy~ : String = "default-src 'none'; \{closed_rules}",
  cookie? : String,
) -> Reply {
  let headers = [
    ("Cache-Control", "no-store"),
    ("X-Content-Type-Options", "nosniff"),
    ("Referrer-Policy", "no-referrer"),
    ("Content-Security-Policy", policy),
  ]
  if kind is Some(kind) {
    headers.push(("Content-Type", kind))
  }
  if cookie is Some(cookie) {
    headers.push(("Set-Cookie", cookie))
  }
  { status, headers, body, }
}

///|
/// A reply with a JSON value.
fn Reply::json(status : Status, value : Json) -> Reply {
  Reply(status, kind="application/json", body=@utf8.encode(value.stringify()))
}

///|
/// A reply with a text for the program or the person that sent the request.
fn Reply::error(status : Status, text : String) -> Reply {
  Reply::json(status, { "error": Json::string(text) })
}