diff options
| author | bptato <nincsnevem662@gmail.com> | 2023-12-10 17:15:18 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2023-12-10 17:29:41 +0100 |
| commit | 48b197133783bf7ec7211b1e114b6ca73f36a3c6 (patch) | |
| tree | b1bbc4ddec8791022f04b11587c1aee3c7b1656a /src/loader | |
| parent | 2dec7483045c9e5696182db7b4ad842491b6c2b2 (diff) | |
| download | chawan-48b197133783bf7ec7211b1e114b6ca73f36a3c6.tar.gz | |
Separate gopher conversion from main binary
Now we use a (much simplified) gopher2html binary in libexec,
instead of converting gopher directories to HTML in loader/gopher.
This has two advantages:
* Less ugly conversion logic in the loader module; we can just
convert the file line by line. (The previous converter also had
some correctness issues, that is fixed now as well.)
* If the user desires, they can replace the gopher converter with
another binary using the mailcap mechanism.
The disadvantages are:
* For now, source display is broken. This is a problem with all
mailcap filters in general, and should be fixed in the future. (That
said, the previous version also only displayed the converted HTML
source, which was not really useful anyway.)
* The proper directory structure is required for this to work;
OTOH plenty of work has been done so that this is as frictionless as
possible, so it should not really be a problem.
Diffstat (limited to 'src/loader')
| -rw-r--r-- | src/loader/gopher.nim | 125 |
1 files changed, 10 insertions, 115 deletions
diff --git a/src/loader/gopher.nim b/src/loader/gopher.nim index fddc7f7c..5f27b213 100644 --- a/src/loader/gopher.nim +++ b/src/loader/gopher.nim @@ -1,5 +1,4 @@ import options -import strutils import bindings/curl import loader/connecterror @@ -8,7 +7,6 @@ import loader/curlwrap import loader/headers import loader/loaderhandle import loader/request -import types/opt import types/url import utils/twtstr @@ -30,9 +28,6 @@ type GopherType = enum type GopherHandle = ref object of CurlHandle t: GopherType - buffer: string - ispre: bool - surl: string func gopherType(c: char): GopherType = return case c @@ -68,7 +63,9 @@ proc onStatusLine(op: GopherHandle): bool = if not op.handle.sendStatus(cast[int](status)): return false let headers = case op.t - of DIRECTORY, SEARCH, HTML: + of DIRECTORY, SEARCH: + newHeaders({"Content-Type": "text/gopher"}) + of HTML: newHeaders({"Content-Type": "text/html"}) of GIF: newHeaders({"Content-Type": "image/gif"}) @@ -80,35 +77,20 @@ proc onStatusLine(op: GopherHandle): bool = newHeaders() if not op.handle.sendHeaders(headers): return false - if op.t in {DIRECTORY, SEARCH}: - var heads = """ -<!DOCTYPE HTML> -<HTML> -<HEAD> -<BASE HREF="""" & $op.request.url & """"> -</HEAD> -<BODY> - """ - if op.t == DIRECTORY: - heads &= "<H1>Index of " & htmlEscape(op.surl) & "</H1>" - else: # search - heads &= "<H1>Search " & htmlEscape(op.surl) & "</H1>" - if not op.handle.sendData(heads): - return false return true -proc loadSearch(op: GopherHandle) = +proc loadSearch(op: GopherHandle, surl: string) = discard op.handle.sendResult(int(CURLE_OK)) discard op.handle.sendStatus(200) # ok discard op.handle.sendHeaders(newHeaders({"Content-Type": "text/html"})) - var heads = """ + let heads = """ <!DOCTYPE HTML> <HTML> <HEAD> <BASE HREF="""" & $op.request.url & """"> </HEAD> <BODY> -<H1>Search """ & htmlEscape(op.surl) & """</H1> +<H1>Search """ & htmlEscape(surl) & """</H1> <FORM> <INPUT TYPE=SEARCH NAME="NAME"> </FORM> @@ -117,75 +99,6 @@ proc loadSearch(op: GopherHandle) = """ discard op.handle.sendData(heads) -proc flushLine(op: GopherHandle, s: string, fromi, toi: int): bool = - if toi == fromi + 1 and s[fromi] == '.': - return true #TODO this is the file end. maybe return false? - if s.len == 0: - return true # invalid - var i = fromi - let tc = s[i] - let t = gopherType(tc) - inc i - let ni = i - while i < toi and s[i] != '\t': inc i - let name = s.substr(ni, i - 1) - inc i - let fi = i - while i < toi and s[i] != '\t': inc i - let file = s.substr(fi, i - 1) - inc i - let hi = i - while i < toi and s[i] != '\t': inc i - let host = s.substr(hi, i - 1) - inc i - let pi = i - while i < toi and s[i] notin {'\t', '\r', '\n'}: inc i - let port = s.substr(pi, i - 1) - var line: string - if t == INFO: - if not op.ispre: - op.ispre = true - line = "<PRE>" - line &= htmlEscape(name) & "\n" - else: - if op.ispre: - line = "</PRE>" - op.ispre = false - let ts = $t - var names = "" - if ts != "": - names &= '[' & ts & ']' - names &= htmlEscape(name) - var ourls: string - if not file.startsWith("URL:"): - let file = if file.len > 0 and file[0] == '/': - file - else: - '/' & file - let pefile = percentEncode(file, PathPercentEncodeSet) - let iurls = "gopher://" & host & ":" & port & "/" & tc & pefile - let url = newURL(iurls) - ourls = if url.isSome: $url.get else: "" - else: - ourls = file.substr("URL:".len) - line &= "<A HREF=\"" & htmlEscape(ourls) & "\">" & names & "</A><BR>\n" - return op.handle.sendData(line) - -proc onSendChunk(op: GopherHandle, previ: int): bool = - var i = previ - var lasti = 0 - while i < op.buffer.len: - if op.buffer[i] in {'\r', '\n'}: - if not op.flushLine(op.buffer, lasti, i): - return false - while i < op.buffer.high and op.buffer[i] in {'\r', '\n'}: - inc i - lasti = i - inc i - if lasti > 0: - op.buffer.delete(0 .. lasti) - return true - # From the documentation: size is always 1. proc curlWriteBody(p: cstring, size: csize_t, nmemb: csize_t, userdata: pointer): csize_t {.cdecl.} = @@ -194,25 +107,10 @@ proc curlWriteBody(p: cstring, size: csize_t, nmemb: csize_t, op.statusline = true if not op.onStatusLine(): return 0 - if nmemb > 0: - if op.t in {DIRECTORY, SEARCH}: - let i = op.buffer.len - op.buffer.setLen(op.buffer.len + int(nmemb)) - prepareMutation(op.buffer) - copyMem(addr op.buffer[i], p, nmemb) - if not op.onSendChunk(i): - return 0 - else: - if not op.handle.sendData(p, int(nmemb)): - return 0 + if not op.handle.sendData(p, int(nmemb)): + return 0 return nmemb -proc finish(op: CurlHandle) = - let op = cast[GopherHandle](op) - if op.ispre: - discard op.handle.sendData("</PRE>\n") - discard op.handle.sendData("</BODY>\n</HTML>\n") - proc loadGopher*(handle: LoaderHandle, curlm: CURLM, request: Request): CurlHandle = let curl = curl_easy_init() @@ -232,15 +130,12 @@ proc loadGopher*(handle: LoaderHandle, curlm: CURLM, let op = curl.newGopherHandle(request, handle, t) if t == SEARCH: if url.query.isNone: - op.surl = url.serialize() - op.loadSearch() + op.loadSearch(url.serialize()) + handle.close() return nil else: url.query = some(url.query.get.after('=')) let surl = url.serialize() - if t in {DIRECTORY, SEARCH}: - op.surl = surl - op.finish = finish curl.setopt(CURLOPT_URL, surl) curl.setopt(CURLOPT_WRITEDATA, op) curl.setopt(CURLOPT_WRITEFUNCTION, curlWriteBody) |