about summary refs log tree commit diff stats
path: root/adapter/protocol/data.nim
blob: e6c7318a08848506aec2065cdba842a47db227f8 (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
when NimMajor >= 2:
  import std/envvars
else:
  import std/os
import std/base64
import std/strutils

import loader/connecterror
import utils/twtstr

proc main() =
  let str = getEnv("MAPPED_URI_PATH")
  const iu = $int(ERROR_INVALID_URL)
  var ct = str.until(',')
  if AllChars - Ascii + Controls - {'\t', ' '} in ct:
    stdout.write("Cha-Control: ConnectionError " & iu  & " invalid data URL")
    return
  let sd = ct.len + 1 # data start
  let body = percentDecode(str, sd)
  if ct.endsWith(";base64"):
    try:
      let d = base64.decode(body) # decode from ct end + 1
      ct.setLen(ct.len - ";base64".len) # remove base64 indicator
      stdout.write("Content-Type: " & ct & "\n\n")
      stdout.write(d)
    except ValueError:
      stdout.write("Cha-Control: ConnectionError " & iu  & " invalid data URL")
  else:
    stdout.write("Content-Type: " & ct & "\n\n")
    stdout.write(body)

main()