diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/config/config.nim | 71 | ||||
| -rw-r--r-- | src/config/toml.nim | 6 | ||||
| -rw-r--r-- | src/loader/loader.nim | 3 | ||||
| -rw-r--r-- | src/main.nim | 3 | ||||
| -rw-r--r-- | src/server/forkserver.nim | 3 |
5 files changed, 39 insertions, 47 deletions
diff --git a/src/config/config.nim b/src/config/config.nim index 63d2311b..ad9510fb 100644 --- a/src/config/config.nim +++ b/src/config/config.nim @@ -162,7 +162,7 @@ type Config* = ref object jsctx: JSContext jsvfns*: seq[JSValueFunction] - configdir {.jsget.}: string + configdir* {.jsget.}: string `include` {.jsget.}: seq[ChaPathResolved] start* {.jsget.}: StartConfig buffer* {.jsget.}: BufferSectionConfig @@ -611,7 +611,7 @@ proc parseConfigValue(ctx: var ConfigParser; x: var MimeTypes; v: TomlValue; ctx.parseConfigValue(paths, v, k) x = default(MimeTypes) for p in paths: - let f = openFileExpand(ctx.config.configdir, p) + let f = openFileExpand(ctx.dir, p) if f != nil: x.parseMimeTypes(f) @@ -625,7 +625,7 @@ proc parseConfigValue(ctx: var ConfigParser; x: var Mailcap; v: TomlValue; ctx.parseConfigValue(paths, v, k) x = default(Mailcap) for p in paths: - let f = openFileExpand(ctx.config.configdir, p) + let f = openFileExpand(ctx.dir, p) if f != nil: let res = parseMailcap(f) if res.isSome: @@ -642,7 +642,7 @@ proc parseConfigValue(ctx: var ConfigParser; x: var URIMethodMap; v: TomlValue; ctx.parseConfigValue(paths, v, k) x = default(URIMethodMap) for p in paths: - let f = openFileExpand(ctx.config.configdir, p) + let f = openFileExpand(ctx.dir, p) if f != nil: x.parseURIMethodMap(f.readAll()) x.append(DefaultURIMethodMap) @@ -673,12 +673,14 @@ type ParseConfigResult* = object warnings*: seq[string] errorMsg*: string -proc parseConfig(config: Config; dir: string; stream: Stream; name = "<input>"; - laxnames = false): ParseConfigResult +proc parseConfig*(config: Config; dir, buf: string; name = "<input>"; + laxnames = false; setdir = true): ParseConfigResult -proc parseConfig(config: Config; dir: string; t: TomlValue): ParseConfigResult = +proc parseConfig(config: Config; dir: string; t: TomlValue; setdir: bool): + ParseConfigResult = var ctx = ConfigParser(config: config, dir: dir) - config.configdir = dir + if setdir: + config.configdir = dir try: var myRes = ParseConfigResult(success: true) ctx.parseConfigValue(config[], t, "") @@ -695,7 +697,7 @@ proc parseConfig(config: Config; dir: string; t: TomlValue): ParseConfigResult = warnings: ctx.warnings, errorMsg: "include file not found: " & s ) - let res = config.parseConfig(dir, fs) + let res = config.parseConfig(dir, fs.readAll()) if not res.success: return res myRes.warnings.add(res.warnings) @@ -708,66 +710,53 @@ proc parseConfig(config: Config; dir: string; t: TomlValue): ParseConfigResult = errorMsg: e.msg ) -proc parseConfig(config: Config; dir: string; stream: Stream; name = "<input>"; - laxnames = false): ParseConfigResult = - let toml = parseToml(stream, dir / name, laxnames) +proc parseConfig*(config: Config; dir, buf: string; name = "<input>"; + laxnames = false; setdir = true): ParseConfigResult = + let toml = parseToml(buf, dir / name, laxnames) if toml.isSome: - return config.parseConfig(dir, toml.get) + return config.parseConfig(dir, toml.get, setdir) else: return ParseConfigResult( success: false, errorMsg: "Fatal error: failed to parse config\n" & toml.error & '\n' ) -proc parseConfig*(config: Config; dir, s: string; name = "<input>"; - laxnames = false): ParseConfigResult = - return config.parseConfig(dir, newStringStream(s), name, laxnames) +proc getNormalAction*(config: Config; s: string): string = + return config.page.getOrDefault(s) + +proc getLinedAction*(config: Config; s: string): string = + return config.line.getOrDefault(s) + +type ReadConfigResult = tuple + config: Config + res: ParseConfigResult const defaultConfig = staticRead"res/config.toml" -proc readConfig(config: Config; dir, name: string): ParseConfigResult = +proc readConfig0(config: Config; dir, name: string): ParseConfigResult = let path = if name.len > 0 and name[0] == '/': name else: dir / name let fs = newFileStream(path) if fs != nil: - return config.parseConfig(parentDir(path), fs) + return config.parseConfig(parentDir(path), fs.readAll()) return ParseConfigResult(success: true) -proc loadConfig*(config: Config; s: string) {.jsfunc.} = - let s = if s.len > 0 and s[0] == '/': - s - else: - getCurrentDir() / s - if not fileExists(s): - return - discard config.parseConfig(parentDir(s), newFileStream(s)) - -proc getNormalAction*(config: Config; s: string): string = - return config.page.getOrDefault(s) - -proc getLinedAction*(config: Config; s: string): string = - return config.line.getOrDefault(s) - -type ReadConfigResult = tuple - config: Config - res: ParseConfigResult - proc readConfig*(pathOverride: Option[string]; jsctx: JSContext): ReadConfigResult = let config = Config(jsctx: jsctx) - var res = config.parseConfig("res", newStringStream(defaultConfig)) + var res = config.parseConfig("res", defaultConfig) if not res.success: return (nil, res) if pathOverride.isNone: when defined(debug): - res = config.readConfig(getCurrentDir() / "res", "config.toml") + res = config.readConfig0(getCurrentDir() / "res", "config.toml") if not res.success: return (nil, res) - res = config.readConfig(getConfigDir() / "chawan", "config.toml") + res = config.readConfig0(getConfigDir() / "chawan", "config.toml") else: - res = config.readConfig(getCurrentDir(), pathOverride.get) + res = config.readConfig0(getCurrentDir(), pathOverride.get) if not res.success: return (nil, res) return (config, res) diff --git a/src/config/toml.nim b/src/config/toml.nim index 20dbfbe5..c196c6f2 100644 --- a/src/config/toml.nim +++ b/src/config/toml.nim @@ -1,5 +1,4 @@ import std/options -import std/streams import std/tables import std/times @@ -563,10 +562,10 @@ proc consumeValue(state: var TomlParser): TomlResult = return ok(TomlValue(t: tvtString, s: "")) return state.err("unexpected end of file") -proc parseToml*(inputStream: Stream; filename = "<input>"; laxnames = false): +proc parseToml*(buf: string; filename = "<input>"; laxnames = false): TomlResult = var state = TomlParser( - buf: inputStream.readAll(), + buf: buf, line: 1, root: TomlTable(), filename: filename, @@ -588,5 +587,4 @@ proc parseToml*(inputStream: Stream; filename = "<input>"; laxnames = false): of '\t', ' ': discard else: return state.err("invalid character after value: " & c) ?state.flushLine() - inputStream.close() return ok(TomlValue(t: tvtTable, tab: state.root)) diff --git a/src/loader/loader.nim b/src/loader/loader.nim index 9067253f..305d22b4 100644 --- a/src/loader/loader.nim +++ b/src/loader/loader.nim @@ -79,6 +79,7 @@ type w3mCGICompat*: bool tmpdir*: string sockdir*: string + configdir*: string func isPrivileged(ctx: LoaderContext; client: ClientData): bool = return ctx.pagerClient == client @@ -1220,6 +1221,8 @@ proc initLoaderContext(fd: cint; config: LoaderConfig): LoaderContext = putEnv("REMOTE_ADDR", "127.0.0.1") putEnv("GATEWAY_INTERFACE", "CGI/1.1") putEnv("CHA_INSECURE_SSL_NO_VERIFY", "0") + putEnv("CHA_TMP_DIR", config.tmpdir) + putEnv("CHA_CONFIG_DIR", config.configdir) return ctx # This is only called when an OutputHandle could not read enough of one (or diff --git a/src/main.nim b/src/main.nim index c53950a3..9d946033 100644 --- a/src/main.nim +++ b/src/main.nim @@ -215,7 +215,8 @@ proc main() = quit(1) warnings.add(res.warnings) for opt in ctx.opts: - let res = config.parseConfig(getCurrentDir(), opt, laxnames = true) + let res = config.parseConfig(getCurrentDir(), opt, laxnames = true, + setdir = false) if not res.success: stderr.writeLine(res.errorMsg) quit(1) diff --git a/src/server/forkserver.nim b/src/server/forkserver.nim index 7c6997a8..06c930f6 100644 --- a/src/server/forkserver.nim +++ b/src/server/forkserver.nim @@ -47,7 +47,8 @@ proc loadConfig*(forkserver: ForkServer; config: Config): int = w3mCGICompat: config.external.w3m_cgi_compat, cgiDir: seq[string](config.external.cgi_dir), tmpdir: config.external.tmpdir, - sockdir: config.external.sockdir + sockdir: config.external.sockdir, + configdir: config.configdir )) var r = forkserver.istream.initPacketReader() var process: int |