summaryrefslogtreecommitdiff
path: root/http/cookie.ts
blob: e78d4823899fb5b4de130f7c3312f06f81c9ff0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { ServerRequest } from "./server.ts";

export interface Cookie {
  [key: string]: string;
}

/* Parse the cookie of the Server Request */
export function getCookie(rq: ServerRequest): Cookie {
  if (rq.headers.has("Cookie")) {
    const out: Cookie = {};
    const c = rq.headers.get("Cookie").split(";");
    for (const kv of c) {
      const cookieVal = kv.split("=");
      const key = cookieVal.shift().trim();
      out[key] = cookieVal.join("");
    }
    return out;
  }
  return {};
}