about summary refs log tree commit diff stats
path: root/src/loader
diff options
context:
space:
mode:
Diffstat (limited to 'src/loader')
-rw-r--r--src/loader/cgi.nim96
-rw-r--r--src/loader/connecterror.nim7
-rw-r--r--src/loader/data.nim38
-rw-r--r--src/loader/loader.nim4
4 files changed, 87 insertions, 58 deletions
diff --git a/src/loader/cgi.nim b/src/loader/cgi.nim
index 592111c9..659047da 100644
--- a/src/loader/cgi.nim
+++ b/src/loader/cgi.nim
@@ -58,6 +58,66 @@ proc setupEnv(cmd, scriptName, pathInfo, requestURI: string, request: Request,
       putEnv("HTTPS_proxy", s)
     putEnv("ALL_PROXY", s)
 
+type ControlResult = enum
+  RESULT_CONTROL_DONE, RESULT_CONTROL_CONTINUE, RESULT_ERROR
+
+proc handleFirstLine(handle: LoaderHandle, line: string, headers: Headers,
+    status: var int): ControlResult =
+  let k = line.until(':')
+  if k.len == line.len:
+    # invalid
+    discard handle.sendResult(ERROR_CGI_MALFORMED_HEADER)
+    return RESULT_ERROR
+  let v = line.substr(k.len + 1).strip()
+  if k.equalsIgnoreCase("Status"):
+    status = parseInt32(v).get(0)
+    return RESULT_CONTROL_CONTINUE
+  if k.equalsIgnoreCase("Cha-Control"):
+    if v.startsWithIgnoreCase("Connected"):
+      discard handle.sendResult(0) # success
+      return RESULT_CONTROL_CONTINUE
+    elif v.startsWithIgnoreCase("ConnectionError"):
+      let errs = v.substr("ConnectionError".len + 1).split(' ')
+      if errs.len == 0:
+        discard handle.sendResult(ERROR_CGI_INVALID_CHA_CONTROL)
+      else:
+        let fb = int32(ERROR_CGI_INVALID_CHA_CONTROL)
+        let code = int(parseInt32(errs[0]).get(fb))
+        discard handle.sendResult(code)
+      return RESULT_ERROR
+    elif v.startsWithIgnoreCase("ControlDone"):
+      return RESULT_CONTROL_DONE
+    discard handle.sendResult(ERROR_CGI_INVALID_CHA_CONTROL)
+    return RESULT_ERROR
+  headers.add(k, v)
+  return RESULT_CONTROL_DONE
+
+proc handleControlLine(handle: LoaderHandle, line: string, headers: Headers,
+    status: var int): ControlResult =
+  let k = line.until(':')
+  if k.len == line.len:
+    # invalid
+    return RESULT_ERROR
+  let v = line.substr(k.len + 1).strip()
+  if k.equalsIgnoreCase("Status"):
+    status = parseInt32(v).get(0)
+    return RESULT_CONTROL_CONTINUE
+  if k.equalsIgnoreCase("Cha-Control"):
+    if v.startsWithIgnoreCase("ControlDone"):
+      return RESULT_CONTROL_DONE
+    return RESULT_ERROR
+  headers.add(k, v)
+  return RESULT_CONTROL_DONE
+
+# returns false if transfer was interrupted
+proc handleLine(handle: LoaderHandle, line: string, headers: Headers) =
+  let k = line.until(':')
+  if k.len == line.len:
+    # invalid
+    return
+  let v = line.substr(k.len + 1).strip()
+  headers.add(k, v)
+
 proc loadCGI*(handle: LoaderHandle, request: Request, cgiDir: seq[string],
     prevURL: URL) =
   template t(body: untyped) =
@@ -159,20 +219,28 @@ proc loadCGI*(handle: LoaderHandle, request: Request, cgiDir: seq[string],
     let ps = newPosixStream(pipefd[0])
     let headers = newHeaders()
     var status = 200
-    while not ps.atEnd:
-      let line = ps.readLine()
-      if line == "": #\r\n
-        break
-      let k = line.until(':')
-      if k == line:
-        # invalid?
-        discard
-      else:
-        let v = line.substr(k.len + 1).strip()
-        if k.equalsIgnoreCase("Status"):
-          status = parseInt32(v).get(0)
-        else:
-          headers.add(k, v)
+    if ps.atEnd:
+      # no data?
+      discard handle.sendResult(ERROR_CGI_NO_DATA)
+      return
+    let line = ps.readLine()
+    if line == "": #\r\n
+      # no headers, body comes immediately
+      t handle.sendResult(0) # success
+    else:
+      var res = handle.handleFirstLine(line, headers, status)
+      if res == RESULT_ERROR:
+        return
+      while not ps.atEnd and res == RESULT_CONTROL_CONTINUE:
+        let line = ps.readLine()
+        res = handle.handleControlLine(line, headers, status)
+        if res == RESULT_ERROR:
+          return
+      while not ps.atEnd:
+        let line = ps.readLine()
+        if line == "": #\r\n
+          break
+        handle.handleLine(line, headers)
     t handle.sendStatus(status)
     t handle.sendHeaders(headers)
     var buffer: array[4096, uint8]
diff --git a/src/loader/connecterror.nim b/src/loader/connecterror.nim
index 8f2f95d2..913e007f 100644
--- a/src/loader/connecterror.nim
+++ b/src/loader/connecterror.nim
@@ -1,6 +1,9 @@
 import bindings/curl
 
 type ConnectErrorCode* = enum
+  ERROR_CGI_NO_DATA = (-17, "CGI script returned no data")
+  ERROR_CGI_MALFORMED_HEADER = (-16, "CGI script returned a malformed header")
+  ERROR_CGI_INVALID_CHA_CONTROL = (-15, "CGI got invalid Cha-Control header")
   ERROR_TOO_MANY_REWRITES = (-14, "too many URI method map rewrites")
   ERROR_INVALID_URI_METHOD_ENTRY = (-13, "invalid URI method entry")
   ERROR_CGI_FILE_NOT_FOUND = (-12, "CGI file not found")
@@ -8,8 +11,8 @@ type ConnectErrorCode* = enum
   ERROR_FAIL_SETUP_CGI = (-10, "failed to set up CGI script")
   ERROR_NO_CGI_DIR = (-9, "no local-CGI directory configured")
   ERROR_INVALID_METHOD = (-8, "invalid method")
-  ERROR_INVALID_DATA_URL = (-7, "invalid data URL")
-  ERROR_ABOUT_PAGE_NOT_FOUND = (-6, "about page not found")
+  ERROR_INVALID_URL = (-7, "invalid URL")
+  ERROR_CONNECTION_REFUSED = (-6, "connection refused")
   ERROR_FILE_NOT_FOUND = (-5, "file not found")
   ERROR_SOURCE_NOT_FOUND = (-4, "clone source could not be found")
   ERROR_LOADER_KILLED = (-3, "loader killed during transfer")
diff --git a/src/loader/data.nim b/src/loader/data.nim
deleted file mode 100644
index 832bb9b9..00000000
--- a/src/loader/data.nim
+++ /dev/null
@@ -1,38 +0,0 @@
-import base64
-import strutils
-
-import loader/connecterror
-import loader/headers
-import loader/loaderhandle
-import loader/request
-import types/url
-import utils/twtstr
-
-proc loadData*(handle: LoaderHandle, request: Request) =
-  template t(body: untyped) =
-    if not body:
-      return
-  var str = $request.url
-  let si = "data:".len # start index
-  var ct = ""
-  for i in si ..< str.len:
-    if str[i] == ',':
-      break
-    ct &= str[i]
-  let sd = si + ct.len + 1 # data start
-  let s = percentDecode(str, sd)
-  if ct.endsWith(";base64"):
-    try:
-      let d = base64.decode(s) # decode from ct end + 1
-      t handle.sendResult(0)
-      t handle.sendStatus(200)
-      ct.setLen(ct.len - ";base64".len) # remove base64 indicator
-      t handle.sendHeaders(newHeaders({"Content-Type": ct}))
-      t handle.sendData(d)
-    except ValueError:
-      discard handle.sendResult(ERROR_INVALID_DATA_URL)
-  else:
-    t handle.sendResult(0)
-    t handle.sendStatus(200)
-    t handle.sendHeaders(newHeaders({"Content-Type": ct}))
-    t handle.sendData(s)
diff --git a/src/loader/loader.nim b/src/loader/loader.nim
index 5fb49c07..b6250098 100644
--- a/src/loader/loader.nim
+++ b/src/loader/loader.nim
@@ -31,7 +31,6 @@ import js/javascript
 import loader/cgi
 import loader/connecterror
 import loader/curlhandle
-import loader/data
 import loader/file
 import loader/ftp
 import loader/gopher
@@ -147,9 +146,6 @@ proc loadResource(ctx: LoaderContext, request: Request, handle: LoaderHandle) =
       let handleData = handle.loadHttp(ctx.curlm, request)
       if handleData != nil:
         ctx.handleList.add(handleData)
-    of "data":
-      handle.loadData(request)
-      handle.close()
     of "ftp", "ftps", "sftp":
       let handleData = handle.loadFtp(ctx.curlm, request)
       if handleData != nil: