about summary refs log tree commit diff
path: root/web/bubblegum/default.nix
blob: 393ac75d48030afe2434763e4d5459f40ef6f25a (plain) (blame)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
{ depot, lib, pkgs, ... }:

let

  inherit (depot.nix)
    runExecline
    getBins
    ;

  statusCodes = {
    # 1xx
    "Continue" = 100;
    "Switching Protocols" = 101;
    "Processing" = 102;
    "Early Hints" = 103;
    # 2xx
    "OK" = 200;
    "Created" = 201;
    "Accepted" = 202;
    "Non-Authoritative Information" = 203;
    "No Content" = 204;
    "Reset Content" = 205;
    "Partial Content" = 206;
    "Multi Status" = 207;
    "Already Reported" = 208;
    "IM Used" = 226;
    # 3xx
    "Multiple Choices" = 300;
    "Moved Permanently" = 301;
    "Found" = 302;
    "See Other" = 303;
    "Not Modified" = 304;
    "Use Proxy" = 305;
    "Switch Proxy" = 306;
    "Temporary Redirect" = 307;
    "Permanent Redirect" = 308;
    # 4xx
    "Bad Request" = 400;
    "Unauthorized" = 401;
    "Payment Required" = 402;
    "Forbidden" = 403;
    "Not Found" = 404;
    "Method Not Allowed" = 405;
    "Not Acceptable" = 406;
    "Proxy Authentication Required" = 407;
    "Request Timeout" = 408;
    "Conflict" = 409;
    "Gone" = 410;
    "Length Required" = 411;
    "Precondition Failed" = 412;
    "Payload Too Large" = 413;
    "URI Too Long" = 414;
    "Unsupported Media Type" = 415;
    "Range Not Satisfiable" = 416;
    "Expectation Failed" = 417;
    "I'm a teapot" = 418;
    "Misdirected Request" = 421;
    "Unprocessable Entity" = 422;
    "Locked" = 423;
    "Failed Dependency" = 424;
    "Too Early" = 425;
    "Upgrade Required" = 426;
    "Precondition Required" = 428;
    "Too Many Requests" = 429;
    "Request Header Fields Too Large" = 431;
    "Unavailable For Legal Reasons" = 451;
    # 5xx
    "Internal Server Error" = 500;
    "Not Implemented" = 501;
    "Bad Gateway" = 502;
    "Service Unavailable" = 503;
    "Gateway Timeout" = 504;
    "HTTP Version Not Supported" = 505;
    "Variant Also Negotiates" = 506;
    "Insufficient Storage" = 507;
    "Loop Detected" = 508;
    "Not Extended" = 510;
    "Network Authentication Required" = 511;
  };

  /* Generate a CGI response. Takes three arguments:

     1. Status of the response as a string which is
        the descriptive name in the protocol, e. g.
        `"OK"`, `"Not Found"` etc.
     2. Attribute set describing extra headers to
        send, keys and values should both be strings.
     3. Response content as a string.

     See the [README](./README.md) for an example.

    Type: string -> attrs string -> string -> string
  */
  respond =
    # response status as the textual representation in the
    # HTTP protocol. See `statusCodes` for a list of valid
    # options.
    statusArg:
    # headers as an attribute set of strings
    headers:
    # response body as a string
    bodyArg:
    let
      status =
        if builtins.isString statusArg then {
          code = statusCodes."${statusArg}" or null;
          line = statusArg;
        } else {
          code = null; line = null;
        };
      renderedHeaders = lib.concatStrings
        (lib.mapAttrsToList (n: v: "${n}: ${toString v}\r\n") headers);
      internalError = msg: respond 500 {
        Content-type = "text/plain";
      } "bubblegum error: ${msg}";
      body = builtins.tryEval bodyArg;
    in
      if status.code == null || status.line == null
      then internalError "Invalid status ${lib.generators.toPretty {} statusArg}."
      else if !body.success
      then internalError "Unknown evaluation error in user code"
      else lib.concatStrings [
        "Status: ${toString status.code} ${status.line}\r\n"
        renderedHeaders
        "\r\n"
        body.value
      ];

  /* Returns the value of the `SCRIPT_NAME` environment
     variable used by CGI.
  */
  scriptName = builtins.getEnv "SCRIPT_NAME";

  /* Returns the value of the `PATH_INFO` environment
     variable used by CGI. All cases that could be
     considered as the CGI script's root (i. e.
     `PATH_INFO` is empty or `/`) is mapped to `"/"`
     for convenience.
  */
  pathInfo =
    let
      p = builtins.getEnv "PATH_INFO";
    in
      if builtins.stringLength p == 0
      then "/"
      else p;

  /* Helper function which converts a path from the
     root of the CGI script (i. e. something which
     could be the content of `PATH_INFO`) to an
     absolute path from the web root by also
     utilizing `scriptName`.

     Type: string -> string
  */
  absolutePath = path:
    if builtins.substring 0 1 path == "/"
    then "${scriptName}${path}"
    else "${scriptName}/${path}";

  bins = getBins pkgs.coreutils [ "env" "tee" "cat" "printf" "chmod" ]
      // getBins depot.users.sterni.nint [ "nint" ];

  /* Type: args -> either path derivation string -> derivation
  */
  writeCGI =
    { # if given sets the `PATH` to search for `nix-instantiate`
      # Useful when using for example thttpd which unsets `PATH`
      # in the CGI environment.
      binPath ? ""
      # name of the resulting derivation. Defaults to `baseNameOf`
      # the input path or name of the input derivation.
      # Must be given if the input is a string.
    , name ? null
    }:
    input: let
      drvName =
        if name != null
        then name
        else if builtins.isPath input
        then baseNameOf input
        else if lib.isDerivation input
        then input.name
        else builtins.throw "Need name";
      script =
        if builtins.isPath input || lib.isDerivation input
        then input
        else if builtins.isString input
        then pkgs.writeText "${drvName}-source" input
        else builtins.throw "Unsupported input: ${lib.generators.toPretty {} input}";
      shebang = lib.concatStringsSep " " ([
        "#!${bins.env}"
        # use the slightly cursed /usr/bin/env -S which allows us
        # to pass any number of arguments to our interpreter
        # instead of maximum one using plain shebang which considers
        # everything after the first space as the second argument.
        "-S"
      ] ++ lib.optionals (builtins.stringLength binPath > 0) [
        "PATH=${binPath}"
      ] ++ [
        "${bins.nint}"
        # always pass depot so scripts can use this library
        "--arg depot '(import ${depot.depotPath} {})'"
      ]);
    in runExecline.local drvName {} [
      "importas" "out" "out"
      "pipeline" [
        "foreground" [
          "if" [ bins.printf "%s\n" shebang ]
        ]
        "if" [ bins.cat script ]
      ]
      "if" [ bins.tee "$out" ]
      "if" [ bins.chmod "+x" "$out" ]
      "exit" "0"
    ];

in {
  inherit
    respond
    pathInfo
    scriptName
    absolutePath
    writeCGI
    ;
}