aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-12-12 20:28:54 +0100
committerbptato <nincsnevem662@gmail.com>2023-12-12 20:49:30 +0100
commita6166c4dfc7b028761a884127cc78aeb0056ed8d (patch)
treee19cda9ba3e46fa4cf42aa383c3530d472282c10 /src
parent1dcb9cf6567145a0d3705b95717ccbf37eac6140 (diff)
downloadchawan-a6166c4dfc7b028761a884127cc78aeb0056ed8d.tar.gz
Move out file: protocol handling to adapter/
Diffstat (limited to 'src')
-rw-r--r--src/config/config.nim3
-rw-r--r--src/loader/cgi.nim1
-rw-r--r--src/loader/file.nim121
-rw-r--r--src/loader/loader.nim9
4 files changed, 4 insertions, 130 deletions
diff --git a/src/config/config.nim b/src/config/config.nim
index 3a14377f..269c37d5 100644
--- a/src/config/config.nim
+++ b/src/config/config.nim
@@ -408,10 +408,11 @@ proc getMimeTypes*(config: Config): MimeTypes =
return mimeTypes
const DefaultURIMethodMap = parseURIMethodMap("""
-finger: cgi-bin:cha-finger?%s
+finger: cgi-bin:cha-finger
gemini: cgi-bin:gmifetch?%s
about: cgi-bin:about
data: cgi-bin:data
+file: cgi-bin:file?%s
""")
proc getURIMethodMap*(config: Config): URIMethodMap =
diff --git a/src/loader/cgi.nim b/src/loader/cgi.nim
index 659047da..dbdce99c 100644
--- a/src/loader/cgi.nim
+++ b/src/loader/cgi.nim
@@ -215,7 +215,6 @@ proc loadCGI*(handle: LoaderHandle, request: Request, cgiDir: seq[string],
#TODO
discard
ps.close()
- discard handle.sendResult(0) # success
let ps = newPosixStream(pipefd[0])
let headers = newHeaders()
var status = 200
diff --git a/src/loader/file.nim b/src/loader/file.nim
deleted file mode 100644
index cdb7afc2..00000000
--- a/src/loader/file.nim
+++ /dev/null
@@ -1,121 +0,0 @@
-import algorithm
-import os
-import streams
-import times
-
-import loader/connecterror
-import loader/dirlist
-import loader/headers
-import loader/loaderhandle
-import types/url
-
-proc loadDir(handle: LoaderHandle, url: URL, path: string) =
- template t(body: untyped) =
- if not body:
- return
- var path = path
- if path[^1] != '/': #TODO dos/windows
- path &= '/'
- var base = $url
- if base[^1] != '/': #TODO dos/windows
- base &= '/'
- t handle.sendResult(0)
- t handle.sendStatus(200) # ok
- t handle.sendHeaders(newHeaders({"Content-Type": "text/html"}))
- t handle.sendData("""
-<HTML>
-<HEAD>
-<BASE HREF="""" & base & """">
-<TITLE>Directory list of """ & path & """</TITLE>
-</HEAD>
-<BODY>
-<H1>Directory list of """ & path & """</H1>
-<PRE>
-""")
- var fs: seq[(PathComponent, string)]
- for pc, file in walkDir(path, relative = true):
- fs.add((pc, file))
- fs.sort(cmp = proc(a, b: (PathComponent, string)): int = cmp(a[1], b[1]))
- var items: seq[DirlistItem]
- for (pc, file) in fs:
- let fullpath = path / file
- var info: FileInfo
- try:
- info = getFileInfo(fullpath, followSymlink = false)
- except OSError:
- continue
- let modified = $info.lastWriteTime.local().format("MMM/dd/yyyy HH:MM")
- case pc
- of pcDir:
- items.add(DirlistItem(
- t: ITEM_DIR,
- name: file,
- modified: modified
- ))
- of pcFile:
- items.add(DirlistItem(
- t: ITEM_FILE,
- name: file,
- modified: modified,
- nsize: int(info.size)
- ))
- of pcLinkToDir, pcLinkToFile:
- var target = expandSymlink(fullpath)
- if pc == pcLinkToDir:
- target &= '/'
- items.add(DirlistItem(
- t: ITEM_LINK,
- name: file,
- modified: modified,
- linkto: target
- ))
- t handle.sendData(makeDirlist(items))
- t handle.sendData("\n</PRE>\n</BODY>\n</HTML>\n")
-
-proc loadSymlink(handle: LoaderHandle, path: string) =
- template t(body: untyped) =
- if not body:
- return
- t handle.sendResult(0)
- t handle.sendStatus(200) # ok
- t handle.sendHeaders(newHeaders({"Content-Type": "text/html"}))
- let sl = expandSymlink(path)
- t handle.sendData("""
-<HTML>
-<HEAD>
-<TITLE>Symlink view<TITLE>
-</HEAD>
-<BODY>
-Symbolic link to <A HREF="""" & sl & """">""" & sl & """</A></br>
-</BODY>
-</HTML>""")
-
-proc loadFile(handle: LoaderHandle, istream: Stream) =
- template t(body: untyped) =
- if not body:
- return
- t handle.sendResult(0)
- t handle.sendStatus(200) # ok
- t handle.sendHeaders(newHeaders())
- while not istream.atEnd:
- const bufferSize = 4096
- var buffer {.noinit.}: array[bufferSize, char]
- while true:
- let n = readData(istream, addr buffer[0], bufferSize)
- if n == 0:
- break
- t handle.sendData(addr buffer[0], n)
- if n < bufferSize:
- break
-
-proc loadFilePath*(handle: LoaderHandle, url: URL, path: string) =
- let istream = newFileStream(path, fmRead)
- if istream == nil:
- if dirExists(path):
- handle.loadDir(url, path)
- elif symlinkExists(path):
- handle.loadSymlink(path)
- else:
- discard handle.sendResult(ERROR_FILE_NOT_FOUND)
- else:
- handle.loadFile(istream)
diff --git a/src/loader/loader.nim b/src/loader/loader.nim
index b6250098..28029160 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/file
import loader/ftp
import loader/gopher
import loader/headers
@@ -115,8 +114,6 @@ proc addFd(ctx: LoaderContext, fd: int, flags: int) =
const MaxRewrites = 4
func canRewriteForCGICompat(ctx: LoaderContext, path: string): bool =
- if not ctx.config.w3mCGICompat:
- return false
if path.startsWith("/cgi-bin/") or path.startsWith("/$LIB/"):
return true
for dir in ctx.config.cgiDir:
@@ -130,8 +127,7 @@ proc loadResource(ctx: LoaderContext, request: Request, handle: LoaderHandle) =
var prevurl: URL = nil
while redo and tries < MaxRewrites:
redo = false
- case request.url.scheme
- of "file":
+ if ctx.config.w3mCGICompat and request.url.scheme == "file":
let path = request.url.path.serialize_unicode()
if ctx.canRewriteForCGICompat(path):
let newURL = newURL("cgi-bin:" & path & request.url.search)
@@ -140,8 +136,7 @@ proc loadResource(ctx: LoaderContext, request: Request, handle: LoaderHandle) =
inc tries
redo = true
continue
- handle.loadFilePath(request.url, path)
- handle.close()
+ case request.url.scheme
of "http", "https":
let handleData = handle.loadHttp(ctx.curlm, request)
if handleData != nil: