File: http.liq

package info (click to toggle)
liquidsoap 1.1.1-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,536 kB
  • ctags: 4,416
  • sloc: ml: 34,126; python: 956; makefile: 633; sh: 487; perl: 258; lisp: 62; ansic: 43; ruby: 8
file content (37 lines) | stat: -rw-r--r-- 1,031 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Set of HTTP utils.

%include "http_codes.liq"

# Create a HTTP response string
# @category Interaction
# @param ~protocol HTTP protocol used.
# @param ~code Response code.
# @param ~headers Response headers.
# @param ~data Response data
def http_response(~protocol="HTTP/1.1",
                  ~code=200,
                  ~headers=[],
                  ~data="") = 
  status = http_codes[string_of(code)]
  # Set content-length and connection: close
  headers = 
    list.append(headers, 
                [("Content-Length", "#{string.length(data)}"),
                 ("Connection", "close")])

  headers = list.map(fun (x) -> "#{fst(x)}: #{snd(x)}",headers)
  headers = string.concat(separator="\r\n",headers)
  # If no headers are provided, we should avoid
  # having an empty line for them. Therefore, we also
  # conditionally add the final \r\n here.
  headers =
    if headers != "" then
      "#{headers}\r\n"
    else
      headers
    end
  "#{protocol} #{code} #{status}\r\n\
   #{headers}\
   \r\n\
   #{data}"
end