aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-09-19 17:46:27 +0200
committerbptato <nincsnevem662@gmail.com>2024-09-22 22:44:53 +0200
commit080493c058f52a5c20638f1b975d032af45f4d3f (patch)
tree60e6ba6b3cb967d29d349018b3f315e7637b4b9e
parente23fa780cf2fff7146efcd64b2806ce428858b80 (diff)
downloadchawan-080493c058f52a5c20638f1b975d032af45f4d3f.tar.gz
loader: mmap intermediate image files, misc refactoring
* refactor parseHeader * optimize response blob() * add direct "to cache" mode for loader requests which sets stdout to a file, and use it for image processing * move image resizing into a separate process * mmap cache files in between processing steps when possible At last, resize is no longer a part of image decoding. Also, it feels much nicer to keep encoded image data in the same cache as everything else. The mmap operations *should* be more efficient than copying the whole RGBA data through a pipe. In practice, it only makes a difference for loading (well, now just mmapping) the encoded image into the pager, where it singlehandedly speeds up image display by 10x on my test image. For the other steps, the unfortunate fact that "tocache" must delay the next fork/exec in the pipeline until the entire image is processed seems to equal out any wins we might have gotten from skipping a single raw RGBA copy. I have tried moving the delay before the exec (it's possible with yet another pipe), but it didn't help much and made the code much uglier. (Not that tocache didn't, but I can live with this...)
-rw-r--r--Makefile8
-rw-r--r--adapter/img/jebp.c3
-rw-r--r--adapter/img/jebp.nim18
-rw-r--r--adapter/img/resize.nim60
-rw-r--r--adapter/img/sixel.nim22
-rw-r--r--adapter/img/stb_image.c2
-rw-r--r--adapter/img/stb_image_resize.c2
-rw-r--r--adapter/img/stbi.nim55
-rw-r--r--doc/image.md78
-rw-r--r--src/io/dynstream.nim82
-rw-r--r--src/io/tempfile.nim2
-rw-r--r--src/loader/loader.nim136
-rw-r--r--src/loader/loaderhandle.nim3
-rw-r--r--src/loader/loaderiface.nim20
-rw-r--r--src/loader/request.nim6
-rw-r--r--src/loader/response.nim35
-rw-r--r--src/local/client.nim6
-rw-r--r--src/local/container.nim8
-rw-r--r--src/local/pager.nim101
-rw-r--r--src/types/url.nim10
20 files changed, 455 insertions, 202 deletions
diff --git a/Makefile b/Makefile
index eb9ddf08..80a59e4b 100644
--- a/Makefile
+++ b/Makefile
@@ -51,8 +51,8 @@ all: $(OUTDIR_BIN)/cha $(OUTDIR_BIN)/mancha $(OUTDIR_CGI_BIN)/http \
$(OUTDIR_CGI_BIN)/cha-finger $(OUTDIR_CGI_BIN)/about \
$(OUTDIR_CGI_BIN)/file $(OUTDIR_CGI_BIN)/ftp \
$(OUTDIR_CGI_BIN)/man $(OUTDIR_CGI_BIN)/spartan \
- $(OUTDIR_CGI_BIN)/stbi $(OUTDIR_CGI_BIN)/jebp \
- $(OUTDIR_CGI_BIN)/canvas $(OUTDIR_CGI_BIN)/sixel \
+ $(OUTDIR_CGI_BIN)/stbi $(OUTDIR_CGI_BIN)/jebp $(OUTDIR_CGI_BIN)/canvas \
+ $(OUTDIR_CGI_BIN)/sixel $(OUTDIR_CGI_BIN)/resize \
$(OUTDIR_LIBEXEC)/urldec $(OUTDIR_LIBEXEC)/urlenc \
$(OUTDIR_LIBEXEC)/md2html $(OUTDIR_LIBEXEC)/ansi2html
ln -sf "$(OUTDIR)/$(TARGET)/bin/cha" cha
@@ -115,6 +115,8 @@ $(OUTDIR_CGI_BIN)/sixel: src/types/color.nim src/utils/sandbox.nim $(twtstr) $(d
$(OUTDIR_CGI_BIN)/canvas: src/types/canvastypes.nim src/types/path.nim \
src/io/bufreader.nim src/types/color.nim src/types/line.nim \
src/utils/sandbox.nim $(dynstream) $(twtstr)
+$(OUTDIR_CGI_BIN)/resize: adapter/img/stb_image_resize.h adapter/img/stb_image_resize.c \
+ src/utils/sandbox.nim $(dynstream) $(twtstr)
$(OUTDIR_LIBEXEC)/urlenc: $(twtstr)
$(OUTDIR_LIBEXEC)/gopher2html: adapter/gophertypes.nim $(twtstr)
$(OUTDIR_LIBEXEC)/ansi2html: src/types/color.nim $(twtstr)
@@ -172,7 +174,7 @@ manpages = $(manpages1) $(manpages5)
.PHONY: manpage
manpage: $(manpages:%=doc/%)
-protocols = http about file ftp gopher gmifetch cha-finger man spartan stbi jebp sixel canvas
+protocols = http about file ftp gopher gmifetch cha-finger man spartan stbi jebp sixel canvas resize
converters = gopher2html md2html ansi2html gmi2html
tools = urlenc
diff --git a/adapter/img/jebp.c b/adapter/img/jebp.c
index d6dfb904..e48ed43f 100644
--- a/adapter/img/jebp.c
+++ b/adapter/img/jebp.c
@@ -2,6 +2,3 @@
/* #define JEBP_NO_STDIO */
#define JEBP_IMPLEMENTATION
#include "jebp.h"
-#define STB_IMAGE_RESIZE_IMPLEMENTATION
-#include "stb_image_resize.h"
-/**/
diff --git a/adapter/img/jebp.nim b/adapter/img/jebp.nim
index afeb283e..7a10a27e 100644
--- a/adapter/img/jebp.nim
+++ b/adapter/img/jebp.nim
@@ -58,13 +58,6 @@ proc myRead(data: pointer; size: csize_t; user: pointer): csize_t {.cdecl.} =
n += csize_t(i)
return n
-{.push header: "stb_image_resize.h".}
-proc stbir_resize_uint8(input_pixels: ptr uint8;
- input_w, input_h, input_stride_in_bytes: cint; output_pixels: ptr uint8;
- output_w, output_h, output_stride_in_bytes, num_channels: cint): cint
- {.importc.}
-{.pop.}
-
proc writeAll(data: pointer; size: int) =
var n = 0
while n < size:
@@ -123,17 +116,6 @@ proc main() =
if res != 0:
die("Cha-Control: ConnectionError 1 jebp error " &
$jebp_error_string(res))
- elif targetWidth != -1 and targetHeight != -1:
- let hdr = "Cha-Image-Dimensions: " & $targetWidth & "x" & $targetHeight &
- "\n\n"
- let p2 = cast[ptr UncheckedArray[uint8]](alloc(hdr.len +
- targetWidth * targetHeight * 4))
- copyMem(addr p2[0], unsafeAddr hdr[0], hdr.len)
- doAssert stbir_resize_uint8(cast[ptr uint8](image.pixels), image.width,
- image.height, 0, addr p2[hdr.len], targetWidth, targetHeight, 0, 4) == 1
- writeAll(p2, hdr.len + targetWidth * targetHeight * 4)
- dealloc(p2)
- jebp_free_image(addr image)
else:
puts("Cha-Image-Dimensions: " & $image.width & "x" & $image.height &
"\n\n")
diff --git a/adapter/img/resize.nim b/adapter/img/resize.nim
new file mode 100644
index 00000000..5bd29b63
--- /dev/null
+++ b/adapter/img/resize.nim
@@ -0,0 +1,60 @@
+import std/options
+import std/os
+import std/posix
+import std/strutils
+
+import io/dynstream
+import utils/sandbox
+import utils/twtstr
+
+{.compile("stb_image_resize.c", "-O3").}
+
+{.push header: "stb_image_resize.h".}
+proc stbir_resize_uint8(input_pixels: ptr uint8;
+ input_w, input_h, input_stride_in_bytes: cint; output_pixels: ptr uint8;
+ output_w, output_h, output_stride_in_bytes, num_channels: cint): cint
+ {.importc.}
+{.pop.}
+
+proc die(s: string) {.noreturn.} =
+ let os = newPosixStream(STDOUT_FILENO)
+ os.sendDataLoop(s)
+ quit(1)
+
+proc main() =
+ var srcWidth = cint(-1)
+ var srcHeight = cint(-1)
+ var dstWidth = cint(-1)
+ var dstHeight = cint(-1)
+ for hdr in getEnv("REQUEST_HEADERS").split('\n'):
+ let k = hdr.until(':')
+ if k == "Cha-Image-Target-Dimensions" or k == "Cha-Image-Dimensions":
+ let v = hdr.after(':').strip()
+ let s = v.split('x')
+ if s.len != 2:
+ die("Cha-Control: ConnectionError 1 wrong dimensions\n")
+ let w = parseUInt32(s[0], allowSign = false)
+ let h = parseUInt32(s[1], allowSign = false)
+ if w.isNone or w.isNone:
+ die("Cha-Control: ConnectionError 1 wrong dimensions\n")
+ if k == "Cha-Image-Target-Dimensions":
+ dstWidth = cint(w.get)
+ dstHeight = cint(h.get)
+ else:
+ srcWidth = cint(w.get)
+ srcHeight = cint(h.get)
+ let ps = newPosixStream(STDIN_FILENO)
+ let os = newPosixStream(STDOUT_FILENO)
+ let src = ps.recvDataLoopOrMmap(int(srcWidth * srcHeight * 4))
+ let dst = os.maybeMmapForSend(int(dstWidth * dstHeight * 4 + 1))
+ if src == nil or dst == nil:
+ die("Cha-Control: ConnectionError 1 failed to open i/o\n")
+ dst.p[0] = uint8('\n') # for CGI
+ enterNetworkSandbox()
+ doAssert stbir_resize_uint8(addr src.p[0], srcWidth, srcHeight, 0,
+ addr dst.p[1], dstWidth, dstHeight, 0, 4) == 1
+ os.sendDataLoop(dst)
+ dealloc(src)
+ dealloc(dst)
+
+main()
diff --git a/adapter/img/sixel.nim b/adapter/img/sixel.nim
index e503dbbd..19c3782d 100644
--- a/adapter/img/sixel.nim
+++ b/adapter/img/sixel.nim
@@ -157,7 +157,7 @@ proc trim(trimMap: var TrimMap; K: var uint) =
)
K = k
-proc getPixel(img: seq[RGBAColorBE]; m: int; bgcolor: ARGBColor): RGBColor
+proc getPixel(img: openArray[RGBAColorBE]; m: int; bgcolor: ARGBColor): RGBColor
{.inline.} =
let c0 = img[m].toARGBColor()
if c0.a != 255:
@@ -165,7 +165,7 @@ proc getPixel(img: seq[RGBAColorBE]; m: int; bgcolor: ARGBColor): RGBColor
return RGBColor(uint32(c1).fastmul(100))
return RGBColor(uint32(c0).fastmul(100))
-proc quantize(img: seq[RGBAColorBE]; bgcolor: ARGBColor; outk: var uint):
+proc quantize(img: openArray[RGBAColorBE]; bgcolor: ARGBColor; outk: var uint):
NodeChildren =
var root = default(NodeChildren)
if outk <= 2: # monochrome; not much we can do with an octree...
@@ -366,7 +366,7 @@ proc createBands(bands: var seq[SixelBand]; activeChunks: seq[ptr SixelChunk]) =
if not found:
bands.add(SixelBand(head: chunk, tail: chunk))
-proc encode(img: seq[RGBAColorBE]; width, height, offx, offy, cropw: int;
+proc encode(img: openArray[RGBAColorBE]; width, height, offx, offy, cropw: int;
halfdump: bool; bgcolor: ARGBColor; palette: int) =
var palette = uint(palette)
var root = img.quantize(bgcolor, palette)
@@ -478,7 +478,6 @@ proc parseDimensions(s: string): (int, int) =
return (int(w.get), int(h.get))
proc main() =
- enterNetworkSandbox()
let scheme = getEnv("MAPPED_URI_SCHEME")
let f = scheme.after('+')
if f != "x-sixel":
@@ -487,7 +486,6 @@ proc main() =
of "decode":
die("Cha-Control: ConnectionError 1 not implemented\n")
of "encode":
- let headers = getEnv("REQUEST_HEADERS")
var width = 0
var height = 0
var offx = 0
@@ -497,7 +495,7 @@ proc main() =
var bgcolor = rgb(0, 0, 0)
var cropw = -1
var quality = -1
- for hdr in headers.split('\n'):
+ for hdr in getEnv("REQUEST_HEADERS").split('\n'):
let s = hdr.after(':').strip()
case hdr.until(':')
of "Cha-Image-Dimensions":
@@ -537,9 +535,15 @@ proc main() =
os.sendDataLoop("Cha-Image-Dimensions: 0x0\n")
quit(0) # done...
let n = width * height
- var img = cast[seq[RGBAColorBE]](newSeqUninitialized[uint32](n))
+ let L = n * 4
let ps = newPosixStream(STDIN_FILENO)
- ps.recvDataLoop(addr img[0], n * 4)
- img.encode(width, height, offx, offy, cropw, halfdump, bgcolor, palette)
+ let src = ps.recvDataLoopOrMmap(L)
+ if src == nil:
+ die("Cha-Control: ConnectionError 1 failed to read input\n")
+ enterNetworkSandbox() # don't swallow stat
+ let p = cast[ptr UncheckedArray[RGBAColorBE]](src.p)
+ p.toOpenArray(0, n - 1).encode(width, height, offx, offy, cropw, halfdump,
+ bgcolor, palette)
+ dealloc(src)
main()
diff --git a/adapter/img/stb_image.c b/adapter/img/stb_image.c
index 2c2bac34..b5afab43 100644
--- a/adapter/img/stb_image.c
+++ b/adapter/img/stb_image.c
@@ -15,5 +15,3 @@
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define STIBW_NO_STDIO
#include "stb_image_write.h"
-#define STB_IMAGE_RESIZE_IMPLEMENTATION
-#include "stb_image_resize.h"
diff --git a/adapter/img/stb_image_resize.c b/adapter/img/stb_image_resize.c
new file mode 100644
index 00000000..b2d47755
--- /dev/null
+++ b/adapter/img/stb_image_resize.c
@@ -0,0 +1,2 @@
+#define STB_IMAGE_RESIZE_IMPLEMENTATION
+#include "stb_image_resize.h"
diff --git a/adapter/img/stbi.nim b/adapter/img/stbi.nim
index f0cfe2bb..dec221f1 100644
--- a/adapter/img/stbi.nim
+++ b/adapter/img/stbi.nim
@@ -84,13 +84,6 @@ proc writeAll(data: pointer; size: int) =
proc myWriteFunc(context, data: pointer; size: cint) {.cdecl.} =
writeAll(data, int(size))
-{.push header: "stb_image_resize.h".}
-proc stbir_resize_uint8(input_pixels: ptr uint8;
- input_w, input_h, input_stride_in_bytes: cint; output_pixels: ptr uint8;
- output_w, output_h, output_stride_in_bytes, num_channels: cint): cint
- {.importc.}
-{.pop.}
-
proc puts(s: string) =
if s.len > 0:
writeAll(unsafeAddr s[0], s.len)
@@ -100,13 +93,12 @@ proc die(s: string) {.noreturn.} =
quit(1)
proc main() =
- enterNetworkSandbox()
- let scheme = getEnv("MAPPED_URI_SCHEME")
- let f = scheme.after('+')
+ let f = getEnv("MAPPED_URI_SCHEME").after('+')
case getEnv("MAPPED_URI_PATH")
of "decode":
if f notin ["jpeg", "gif", "bmp", "png", "x-unknown"]:
die("Cha-Control: ConnectionError 1 unknown format " & f)
+ enterNetworkSandbox()
var user = StbiUser()
var x: cint
var y: cint
@@ -116,25 +108,12 @@ proc main() =
skip: mySkip,
eof: myEof
)
- let headers = getEnv("REQUEST_HEADERS")
- var targetWidth = cint(-1)
- var targetHeight = cint(-1)
var infoOnly = false
- for hdr in headers.split('\n'):
+ for hdr in getEnv("REQUEST_HEADERS").split('\n'):
let v = hdr.after(':').strip()
- case hdr.until(':')
- of "Cha-Image-Info-Only":
+ if hdr.until(':') == "Cha-Image-Info-Only":
infoOnly = v == "1"
- of "Cha-Image-Target-Dimensions":
- let s = v.split('x')
- if s.len != 2:
- die("Cha-Control: ConnectionError 1 wrong dimensions\n")
- let w = parseUInt32(s[0], allowSign = false)
- let h = parseUInt32(s[1], allowSign = false)
- if w.isNone or w.isNone:
- die("Cha-Control: ConnectionError 1 wrong dimensions\n")
- targetWidth = cint(w.get)
- targetHeight = cint(h.get)
+ break
if infoOnly:
if stbi_info_from_callbacks(addr clbk, addr user, x, y,
channels_in_file) == 1:
@@ -148,22 +127,13 @@ proc main() =
if p == nil:
die("Cha-Control: ConnectionError 1 stbi error " &
$stbi_failure_reason())
- elif targetWidth != -1 and targetHeight != -1:
- let hdr = "Cha-Image-Dimensions: " & $targetWidth & "x" &
- $targetHeight & "\n\n"
- let p2 = cast[ptr UncheckedArray[uint8]](alloc(hdr.len +
- targetWidth * targetHeight * 4))
- copyMem(addr p2[0], unsafeAddr hdr[0], hdr.len)
- doAssert stbir_resize_uint8(p, x, y, 0, addr p2[hdr.len], targetWidth,
- targetHeight, 0, 4) == 1
- writeAll(p2, hdr.len + targetWidth * targetHeight * 4)
- dealloc(p2)
- stbi_image_free(p)
else:
puts("Cha-Image-Dimensions: " & $x & "x" & $y & "\n\n")
writeAll(p, x * y * 4)
stbi_image_free(p)
of "encode":
+ if f notin ["png", "bmp", "jpeg"]:
+ die("Cha-Control: ConnectionError 1 unknown format " & f)
let headers = getEnv("REQUEST_HEADERS")
var quality = cint(50)
var width = cint(0)
@@ -185,10 +155,12 @@ proc main() =
die("Cha-Control: ConnectionError 1 wrong quality")
quality = cint(q)
let ps = newPosixStream(STDIN_FILENO)
- var s = newSeqUninitialized[uint8](width * height * 4)
- ps.recvDataLoop(s)
+ let src = ps.recvDataLoopOrMmap(width * height * 4)
+ if src == nil:
+ die("Cha-Control: ConnectionError 1 failed to read input\n")
+ enterNetworkSandbox() # don't swallow stat
puts("Cha-Image-Dimensions: " & $width & 'x' & $height & "\n\n")
- let p = unsafeAddr s[0]
+ let p = src.p
case f
of "png":
stbi_write_png_to_func(myWriteFunc, nil, cint(width), cint(height), 4, p,
@@ -198,7 +170,6 @@ proc main() =
of "jpeg":
stbi_write_jpg_to_func(myWriteFunc, nil, cint(width), cint(height), 4, p,
quality)
- else:
- die("Cha-Control: ConnectionError 1 unknown format " & f)
+ dealloc(src)
main()
diff --git a/doc/image.md b/doc/image.md
index a0f8766f..e9232686 100644
--- a/doc/image.md
+++ b/doc/image.md
@@ -7,9 +7,6 @@ MANOFF -->
On terminals that support images, Chawan can display various bit-mapped
image formats.
-Warning: both this document and the implementation is very much WIP.
-Anything described in this document may change in the near future.
-
## Enabling images
There are actually two switches for images in the config:
@@ -69,11 +66,13 @@ Known quirks and implementation details:
### Kitty
-On terminals that support it, Kitty is preferred over Sixel. The Kitty
-protocol has the benefit that it can reuse images we have sent at least once,
-so it is normally more efficient than the former.
+On terminals that support it, Kitty's protocol is preferred over
+Sixel. Its main benefit is that images do not have to be sent again
+every time they move on the screen (i.e. on scroll), but the initial
+transfer should also be faster (because PNG's compression tends to
+outperform Sixel's RLE).
-To minimize transfer size, we encode images to PNG before displaying them.
+Unlike Sixel, the Kitty protocol fully supports transparency.
## Input formats
@@ -91,15 +90,18 @@ like stbi is OK to vendor.)
All image codec implementations are specified by the URL scheme
"img-codec+name:", where "name" is the MIME subtype. e.g. for image/png,
-it is "img-codec+png:". Like all schemes, these are defined (and
-overridable) in the urimethodmap file, and are implemented as local CGI
-programs. These programs take an encoded image on stdin, and dump the
-decoded RGBA data to stdout - when encoding, vice versa.
+it is "img-codec+png:". (This indeed means that only "image" MIME types
+can be used.)
+
+Like all schemes, these are defined (and overridable) in the
+urimethodmap file, and are implemented as local CGI programs. These
+programs take an encoded image on stdin, and dump the decoded RGBA data
+to stdout - when encoding, vice versa.
-This means that it is possible (although rarely practical) for users
-to define image decoders for their preferred formats, or even override
-the built-in ones. (If you actually end up doing this for some reason,
-please shoot me a mail so I can add it to the bonus directory.)
+This means that it is possible for users to define image decoders for
+their preferred formats, or even override the built-in ones. (If you
+actually end up doing this for some reason, please shoot me a mail so I
+can add it to the bonus directory.)
A codec can have one of, or both, "decode" and "encode" instructions;
these are set in the path name. So "img-codec+png:decode" is called for
@@ -116,8 +118,6 @@ stream of an encoded image on its standard input and print the
equivalent binary stream of big-endian 8-bit (per component) RGBA values
to stdout.
-If specified, it also has to resize said image first.
-
Input headers:
* Cha-Image-Info-Only: 1
@@ -126,30 +126,14 @@ This tells the image decoder to only send image metadata (i.e. size).
Technically, the decoder is free to actually decode the image too, but
the browser will ignore any output after headers.
-* Cha-Image-Target-Dimensions: {width}x{height}
-
-Mutually exclusive with Cha-Image-Info-Only; this instructs the decoder
-to also resize the output image. The dimension format is such that for
-e.g. 123x456, 123 is width and 456 is height.
-
-(Readers of good taste might consider this header to be a questionable
-design decision, but remember that both the decoder and encoder
-effectively require copying the output image (thru stdio). Combined with
-the current file loader implementation, this means that in-browser image
-resizing would require at least two unnecessary copies.
-
-A future design might solve this problem better through shared memory.)
-
Output headers:
* Cha-Image-Dimensions: {width}x{height}
-The final size of the decoded image. If the image was resized through
-Cha-Image-Target-Dimensions, then this header's value will match the
-value specified there.
+The size of the decoded image.
-Again, the dimension format is such that e.g. for 123x456, 123 is width
-and 456 is height.
+The dimension format is such that e.g. for 123x456, 123 is width and 456
+is height.
#### encoding
@@ -177,3 +161,25 @@ The requested encoding quality, ranging from 1 to 100 inclusive
Output headers:
Currently, no output headers are defined for encoders.
+
+### Skipping copies with mmap
+
+The naive implementation of the above system would have to copy the
+output at least twice when an image is resized. To skip these copies,
+stdin and/or stdout is (currently) a file in the tmp directory for:
+
+* decode stdin, when the image is already downloaded
+* decode stdout, always
+* encode stdin, always
+
+This makes it possible to mmap(2) stdin/stdout instead of streaming
+through them with read(2) and write(2). When doing this, mind the
+following:
+
+* When reading, you must check your initial position in the file with
+ lseek(2).
+* When writing, your headers are part of the output. At the very least,
+ you must place a newline at the file's beginning.
+* This *is* an implementation detail, and might change at any time in
+ the future (e.g. if we add a "no cache files" mode). Always check
+ for S_ISREG to ensure that you are actually dealing with a file.
diff --git a/src/io/dynstream.nim b/src/io/dynstream.nim
index 2bfa02f5..129ecf05 100644
--- a/src/io/dynstream.nim
+++ b/src/io/dynstream.nim
@@ -169,6 +169,88 @@ proc newPosixStream*(path: string; flags, mode: cint): PosixStream =
return nil
return newPosixStream(fd)
+type
+ MaybeMappedMemory* = ptr MaybeMappedMemoryObj
+
+ MaybeMappedMemoryObj = object
+ p0: pointer
+ p0len: int
+ fromMmap: bool
+ p*: ptr UncheckedArray[uint8]
+ len*: int
+
+# Read data of size "len", or mmap it if the stream is a file.
+proc recvDataLoopOrMmap*(ps: PosixStream; len = -1): MaybeMappedMemory =
+ var stats: Stat
+ if fstat(ps.fd, stats) != -1 and S_ISREG(stats.st_mode):
+ let srcOff = lseek(ps.fd, 0, SEEK_CUR) # skip headers
+ if len != -1:
+ doAssert len == stats.st_size - srcOff
+ let len = int(stats.st_size) - srcOff
+ let p0 = mmap(nil, len, PROT_READ, MAP_SHARED, ps.fd, 0)
+ if p0 == MAP_FAILED:
+ return nil
+ let p1 = addr cast[ptr UncheckedArray[uint8]](p0)[srcOff]
+ let res = create(MaybeMappedMemoryObj)
+ res[] = MaybeMappedMemoryObj(
+ p0: p0,
+ p0len: int(stats.st_size),
+ p: cast[ptr UncheckedArray[uint8]](p1),
+ len: len,
+ fromMmap: true
+ )
+ return res
+ let p = cast[ptr UncheckedArray[uint8]](alloc(len))
+ ps.recvDataLoop(p, len)
+ let res = create(MaybeMappedMemoryObj)
+ res[] = MaybeMappedMemoryObj(
+ p0: p,
+ p0len: len,
+ p: p,
+ len: len,
+ fromMmap: false
+ )
+ return res
+
+proc maybeMmapForSend*(ps: PosixStream; len: int): MaybeMappedMemory =
+ var stats: Stat
+ if fstat(0, stats) != -1 and S_ISREG(stats.st_mode):
+ let p0 = mmap(nil, len, PROT_WRITE, MAP_SHARED, ps.fd, 0)
+ if p0 == MAP_FAILED:
+ return nil
+ ps.seek(len - 1)
+ ps.sendDataLoop([char(0)])
+ let res = create(MaybeMappedMemoryObj)
+ res[] = MaybeMappedMemoryObj(
+ p0: p0,
+ p0len: int(stats.st_size),
+ p: cast[ptr UncheckedArray[uint8]](p0),
+ len: len,
+ fromMmap: true
+ )
+ return res
+ let p = cast[ptr UncheckedArray[uint8]](alloc(len))
+ let res = create(MaybeMappedMemoryObj)
+ res[] = MaybeMappedMemoryObj(
+ p0: p,
+ p0len: len,
+ p: p,
+ len: len,
+ fromMmap: false
+ )
+ return res
+
+proc sendDataLoop*(ps: PosixStream; mem: MaybeMappedMemory) =
+ # only send if not mmapped; otherwise everything is already where it should be
+ if not mem.fromMmap:
+ ps.sendDataLoop(mem.p, mem.len)
+
+proc dealloc*(mem: MaybeMappedMemory) =
+ if mem.fromMmap:
+ discard munmap(mem.p0, mem.p0len)
+ else:
+ dealloc(mem.p0)
+
type SocketStream* = ref object of PosixStream
source*: Socket
diff --git a/src/io/tempfile.nim b/src/io/tempfile.nim
index 44c63297..132a12b1 100644
--- a/src/io/tempfile.nim
+++ b/src/io/tempfile.nim
@@ -1,6 +1,6 @@
import std/os
-var tmpf_seq: int
+var tmpf_seq = 0
proc getTempFile*(tmpdir: string; ext = ""): string =
if not dirExists(tmpdir):
createDir(tmpdir)
diff --git a/src/loader/loader.nim b/src/loader/loader.nim
index e7a45833..b5a6b3ba 100644
--- a/src/loader/loader.nim
+++ b/src/loader/loader.nim
@@ -58,8 +58,9 @@ export response
type
CachedItem = ref object
id: int
- path: string
refc: int
+ offset: int
+ path: string
ClientData = ref object
pid: int
@@ -263,10 +264,41 @@ proc addCacheFile(ctx: LoaderContext; client: ClientData; output: OutputHandle):
return cacheId
return -1
+proc findOffset(ps: PosixStream): int =
+ try:
+ var buffer = default(array[512, char])
+ var off = 0
+ var lf = 1u # we start at EOL
+ while true:
+ let n = ps.recvData(buffer)
+ if n == 0:
+ return off
+ for i in 0 ..< n:
+ let c = buffer[i]
+ if c == '\n':
+ inc lf
+ if lf == 2:
+ return off + i + 1
+ elif c != '\r':
+ lf = 0
+ off += n
+ except IOError:
+ discard
+ return -1
+
proc openCachedItem(client: ClientData; id: int): (PosixStream, int) =
let n = client.cacheMap.find(id)
if n != -1:
- return (newPosixStream(client.cacheMap[n].path, O_RDONLY, 0), n)
+ let item = client.cacheMap[n]
+ let ps = newPosixStream(client.cacheMap[n].path, O_RDONLY, 0)
+ if item.offset == -1:
+ let offset = ps.findOffset()
+ if offset == -1:
+ client.cacheMap.del(n)
+ return (nil, -1)
+ item.offset = offset
+ ps.seek(item.offset)
+ return (ps, n)
return (nil, -1)
proc put(ctx: LoaderContext; handle: LoaderHandle) =
@@ -357,18 +389,13 @@ proc handleLine(handle: InputHandle; line: string; headers: Headers) =
let v = line.substr(k.len + 1).strip()
headers.add(k, v)
-proc parseHeaders0(handle: InputHandle; buffer: LoaderBuffer): int =
+proc parseHeaders0(handle: InputHandle; data: openArray[char]): int =
let parser = handle.parser
var s = parser.lineBuffer
- let L = if buffer == nil: 1 else: buffer.len
- for i in 0 ..< L:
+ for i, c in data:
template die =
handle.parser = nil
return -1
- let c = if buffer != nil:
- char(buffer.page[i])
- else:
- '\n'
if parser.crSeen and c != '\n':
die
parser.crSeen = false
@@ -402,11 +429,15 @@ proc parseHeaders0(handle: InputHandle; buffer: LoaderBuffer): int =
s &= c
if s != "":
parser.lineBuffer = s
- return L
+ return data.len
proc parseHeaders(handle: InputHandle; buffer: LoaderBuffer): int =
try:
- return handle.parseHeaders0(buffer)
+ if buffer == nil:
+ return handle.parseHeaders0(['\n'])
+ assert buffer.page != nil
+ let p = cast[ptr UncheckedArray[char]](buffer.page)
+ return handle.parseHeaders0(p.toOpenArray(0, buffer.len - 1))
except ErrorBrokenPipe:
handle.parser = nil
return -1
@@ -457,9 +488,10 @@ proc handleRead(ctx: LoaderContext; handle: InputHandle;
hrrDone
# stream is a regular file, so we can't select on it.
-# cachedHandle is used for attaching the output handle to a different
-# InputHandle when loadFromCache is called while a download is still ongoing
-# (and thus some parts of the document are not cached yet).
+#
+# cachedHandle is used for attaching the output handle to another
+# InputHandle when loadFromCache is called while a download is still
+# ongoing (and thus some parts of the document are not cached yet).
proc loadStreamRegular(ctx: LoaderContext; handle, cachedHandle: InputHandle) =
assert handle.parser == nil # parser is only used with CGI
var unregWrite: seq[OutputHandle] = @[]
@@ -482,7 +514,7 @@ proc loadStreamRegular(ctx: LoaderContext; handle, cachedHandle: InputHandle) =
output.istreamAtEnd = true
ctx.put(output)
else:
- assert output.stream.fd < ctx.handleMap.len or
+ assert output.stream.fd >= ctx.handleMap.len or
ctx.handleMap[output.stream.fd] == nil
output.oclose()
handle.outputs.setLen(0)
@@ -506,7 +538,7 @@ type CGIPath = object
myDir: string
proc setupEnv(cpath: CGIPath; request: Request; contentLen: int; prevURL: URL;
- insecureSSLNoVerify: bool) =
+ config: LoaderClientConfig) =
let url = request.url
putEnv("SCRIPT_NAME", cpath.scriptName)
putEnv("SCRIPT_FILENAME", cpath.cmd)
@@ -532,9 +564,9 @@ proc setupEnv(cpath: CGIPath; request: Request; contentLen: int; prevURL: URL;
putEnv("HTTP_COOKIE", request.headers["Cookie"])
if request.referrer != nil:
putEnv("HTTP_REFERER", $request.referrer)
- if request.proxy != nil:
- putEnv("ALL_PROXY", $request.proxy)
- if insecureSSLNoVerify:
+ if config.proxy != nil:
+ putEnv("ALL_PROXY", $config.proxy)
+ if config.insecureSSLNoVerify:
putEnv("CHA_INSECURE_SSL_NO_VERIFY", "1")
setCurrentDir(cpath.myDir)
@@ -572,7 +604,7 @@ proc parseCGIPath(ctx: LoaderContext; request: Request): CGIPath =
return cpath
proc loadCGI(ctx: LoaderContext; client: ClientData; handle: InputHandle;
- request: Request; prevURL: URL; insecureSSLNoVerify: bool) =
+ request: Request; prevURL: URL; config: LoaderClientConfig) =
if ctx.config.cgiDir.len == 0:
handle.sendResult(ERROR_NO_CGI_DIR)
return
@@ -584,10 +616,31 @@ proc loadCGI(ctx: LoaderContext; client: ClientData; handle: InputHandle;
if not fileExists(cpath.cmd):
handle.sendResult(ERROR_CGI_FILE_NOT_FOUND)
return
- var pipefd: array[0..1, cint] # child -> parent
+ # Pipe the response body as stdout.
+ var pipefd: array[2, cint] # child -> parent
if pipe(pipefd) == -1:
handle.sendResult(ERROR_FAIL_SETUP_CGI)
return
+ let istreamOut = newPosixStream(pipefd[0]) # read by loader
+ var ostreamOut = newPosixStream(pipefd[1]) # written by child
+ var ostreamOut2: PosixStream = nil
+ if request.tocache:
+ # Set stdout to a file, and repurpose the pipe as a dummy to detect when
+ # the process ends. outputId is the cache id.
+ let tmpf = getTempFile(ctx.config.tmpdir)
+ ostreamOut2 = ostreamOut
+ # RDWR, otherwise mmap won't work
+ ostreamOut = newPosixStream(tmpf, O_CREAT or O_RDWR, 0o600)
+ if ostreamOut == nil:
+ handle.sendResult(ERROR_FAIL_SETUP_CGI)
+ return
+ let cacheId = handle.output.outputId # welp
+ client.cacheMap.add(CachedItem(
+ id: cacheId,
+ path: tmpf,
+ refc: 1,
+ offset: -1
+ ))
# Pipe the request body as stdin for POST.
var istream: PosixStream = nil # child end (read)
var ostream: PosixStream = nil # parent end (write)
@@ -617,15 +670,14 @@ proc loadCGI(ctx: LoaderContext; client: ClientData; handle: InputHandle;
istream = newPosixStream(pipefdRead[0])
ostream = newPosixStream(pipefdRead[1])
let contentLen = request.body.contentLength()
- stdout.flushFile()
stderr.flushFile()
let pid = fork()
if pid == -1:
handle.sendResult(ERROR_FAIL_SETUP_CGI)
elif pid == 0:
- discard close(pipefd[0]) # close read
- discard dup2(pipefd[1], 1) # dup stdout
- discard close(pipefd[1])
+ istreamOut.sclose() # close read
+ discard dup2(ostreamOut.fd, 1) # dup stdout
+ ostreamOut.sclose()
if ostream != nil:
ostream.sclose() # close write
if istream2 != nil:
@@ -637,7 +689,7 @@ proc loadCGI(ctx: LoaderContext; client: ClientData; handle: InputHandle;
else:
closeStdin()
# we leave stderr open, so it can be seen in the browser console
- setupEnv(cpath, request, contentLen, prevURL, insecureSSLNoVerify)
+ setupEnv(cpath, request, contentLen, prevURL, config)
# reset SIGCHLD to the default handler. this is useful if the child process
# expects SIGCHLD to be untouched. (e.g. git dies a horrible death with
# SIGCHLD as SIG_IGN)
@@ -652,11 +704,13 @@ proc loadCGI(ctx: LoaderContext; client: ClientData; handle: InputHandle;
($strerror(errno)).deleteChars({'\n', '\r'}))
quit(1)
else:
- discard close(pipefd[1]) # close write
+ ostreamOut.sclose() # close write
+ if ostreamOut2 != nil:
+ ostreamOut2.sclose() # close write
if request.body.t != rbtNone:
istream.sclose() # close read
handle.parser = HeaderParser(headers: newHeaders())
- handle.stream = newPosixStream(pipefd[0])
+ handle.stream = istreamOut
case request.body.t
of rbtString:
ostream.write(request.body.s)
@@ -709,10 +763,7 @@ proc loadStream(ctx: LoaderContext; client: ClientData; handle: InputHandle;
proc loadFromCache(ctx: LoaderContext; client: ClientData; handle: InputHandle;
request: Request) =
let id = parseInt32(request.url.pathname).get(-1)
- let startFrom = if request.url.query.isSome:
- parseInt32(request.url.query.get).get(0)
- else:
- 0
+ let startFrom = parseInt32(request.url.query.get("")).get(0)
let (ps, n) = client.openCachedItem(id)
if ps != nil:
if startFrom != 0:
@@ -798,7 +849,7 @@ proc loadResource(ctx: LoaderContext; client: ClientData;
redo = true
continue
if request.url.scheme == "cgi-bin":
- ctx.loadCGI(client, handle, request, prevurl, config.insecureSSLNoVerify)
+ ctx.loadCGI(client, handle, request, prevurl, config)
if handle.stream != nil:
ctx.addFd(handle)
else:
@@ -852,8 +903,6 @@ proc load(ctx: LoaderContext; stream: SocketStream; request: Request;
handle.rejectHandle(ERROR_DISALLOWED_URL)
else:
request.setupRequestDefaults(config)
- if request.proxy == nil or not ctx.isPrivileged(client):
- request.proxy = config.proxy
ctx.loadResource(client, config, request, handle)
proc load(ctx: LoaderContext; stream: SocketStream; client: ClientData;
@@ -969,6 +1018,18 @@ proc shareCachedItem(ctx: LoaderContext; stream: SocketStream;
targetClient.cacheMap.add(item)
stream.sclose()
+proc openCachedItem(ctx: LoaderContext; stream: SocketStream;
+ client: ClientData; r: var BufferedReader) =
+ # open a cached item
+ var id: int
+ r.sread(id)
+ let (ps, _) = client.openCachedItem(id)
+ stream.withPacketWriter w:
+ w.swrite(ps != nil)
+ if ps != nil:
+ w.sendAux.add(FileHandle(ps.fd))
+ stream.sclose()
+
proc passFd(ctx: LoaderContext; stream: SocketStream; client: ClientData;
r: var BufferedReader) =
var id: string
@@ -1076,6 +1137,9 @@ proc acceptConnection(ctx: LoaderContext) =
of lcShareCachedItem:
privileged_command
ctx.shareCachedItem(stream, r)
+ of lcOpenCachedItem:
+ privileged_command
+ ctx.openCachedItem(stream, client, r)
of lcRedirectToFile:
privileged_command
ctx.redirectToFile(stream, r)
@@ -1123,7 +1187,7 @@ proc initLoaderContext(fd: cint; config: LoaderConfig): LoaderContext =
ctx.ssock = initServerSocket(config.sockdir, -1, myPid, blocking = true)
let sfd = int(ctx.ssock.sock.getFd())
ctx.selector.registerHandle(sfd, {Read}, 0)
- if ctx.handleMap.len <= sfd:
+ if sfd >= ctx.handleMap.len:
ctx.handleMap.setLen(sfd + 1)
ctx.handleMap[sfd] = LoaderHandle() # pseudo handle
# The server has been initialized, so the main process can resume execution.
diff --git a/src/loader/loaderhandle.nim b/src/loader/loaderhandle.nim
index b43149fb..23ddff30 100644
--- a/src/loader/loaderhandle.nim
+++ b/src/loader/loaderhandle.nim
@@ -42,6 +42,9 @@ type
suspended*: bool
dead*: bool
+ SigchldHandle* = ref object of LoaderHandle
+ notifyMap*: seq[tuple[pid: int; input: InputHandle]]
+
HandleParserState* = enum
hpsBeforeLines, hpsAfterFirstLine, hpsControlDone
diff --git a/src/loader/loaderiface.nim b/src/loader/loaderiface.nim
index c8635c6f..8cc5ff42 100644
--- a/src/loader/loaderiface.nim
+++ b/src/loader/loaderiface.nim
@@ -67,6 +67,7 @@ type
lcShareCachedItem
lcSuspend
lcTee
+ lcOpenCachedItem
ClientKey* = array[32, uint8]
@@ -78,8 +79,6 @@ type
referrerPolicy*: ReferrerPolicy
insecureSSLNoVerify*: bool
- FetchPromise* = Promise[JSResult[Response]]
-
proc getRedirect*(response: Response; request: Request): Request =
if "Location" in response.headers.table:
if response.status in 301u16..303u16 or response.status in 307u16..308u16:
@@ -378,6 +377,23 @@ proc shareCachedItem*(loader: FileLoader; id, targetPid: int; sourcePid = -1) =
w.swrite(id)
stream.sclose()
+proc openCachedItem*(loader: FileLoader; cacheId: int): PosixStream =
+ let stream = loader.connect()
+ if stream != nil:
+ stream.withLoaderPacketWriter loader, w:
+ w.swrite(lcOpenCachedItem)
+ w.swrite(cacheId)
+ var fd = FileHandle(-1)
+ stream.withPacketReader r:
+ var success: bool
+ r.sread(success)
+ if success:
+ fd = r.recvAux.pop()
+ stream.sclose()
+ if fd != -1:
+ return newPosixStream(fd)
+ return nil
+
proc passFd*(loader: FileLoader; id: string; fd: FileHandle) =
let stream = loader.connect()
if stream != nil:
diff --git a/src/loader/request.nim b/src/loader/request.nim
index bd102093..69daaf79 100644
--- a/src/loader/request.nim
+++ b/src/loader/request.nim
@@ -81,7 +81,7 @@ type
headers*: Headers
body*: RequestBody
referrer*: URL
- proxy*: URL #TODO do something with this
+ tocache*: bool
JSRequest* = ref object
request*: Request
@@ -143,14 +143,14 @@ iterator pairs*(headers: Headers): (string, string) =
yield (k, v)
func newRequest*(url: URL; httpMethod = hmGet; headers = newHeaders();
- body = RequestBody(); proxy: URL = nil; referrer: URL = nil): Request =
+ body = RequestBody(); referrer: URL = nil; tocache = false): Request =
return Request(
url: url,
httpMethod: httpMethod,
headers: headers,
body: body,
referrer: referrer,
- proxy: proxy
+ tocache: tocache
)
func createPotentialCORSRequest*(url: URL; destination: RequestDestination;
diff --git a/src/loader/response.nim b/src/loader/response.nim
index 1d7307cd..23c386ef 100644
--- a/src/loader/response.nim
+++ b/src/loader/response.nim
@@ -47,6 +47,8 @@ type
opaque*: RootRef
flags*: set[ResponseFlag]
+ FetchPromise* = Promise[JSResult[Response]]
+
jsDestructor(Response)
proc newResponse*(res: int; request: Request; stream: SocketStream;
@@ -59,6 +61,9 @@ proc newResponse*(res: int; request: Request; stream: SocketStream;
status: status
)
+proc newFetchPromise*(): FetchPromise =
+ return newPromise[JSResult[Response]]()
+
func makeNetworkError*(): Response {.jsstfunc: "Response.error".} =
#TODO use "create" function
return Response(
@@ -115,6 +120,10 @@ func getReferrerPolicy*(this: Response): Option[ReferrerPolicy] =
this.headers.table.withValue("Referrer-Policy", p):
return strictParseEnum[ReferrerPolicy](p[][0])
+proc resume*(response: Response) =
+ response.resumeFun(response.outputId)
+ response.resumeFun = nil
+
type TextOpaque = ref object of RootObj
buf: string
bodyRead: Promise[JSResult[string]]
@@ -145,15 +154,9 @@ proc onFinishText(response: Response; success: bool) =
let err = newTypeError("NetworkError when attempting to fetch resource")
bodyRead.resolve(JSResult[string].err(err))
-proc resume*(response: Response) =
- response.resumeFun(response.outputId)
- response.resumeFun = nil
-
proc text*(response: Response): Promise[JSResult[string]] {.jsfunc.} =
if response.body == nil:
- let p = newPromise[JSResult[string]]()
- p.resolve(JSResult[string].ok(""))
- return p
+ return newResolvedPromise(JSResult[string].ok(""))
if response.bodyUsed:
let p = newPromise[JSResult[string]]()
let err = JSResult[string]
@@ -179,12 +182,12 @@ proc onReadBlob(response: Response) =
let opaque = BlobOpaque(response.opaque)
while true:
try:
- let targetLen = opaque.len + BufferSize
- if targetLen > opaque.size:
- opaque.size = targetLen
- opaque.p = realloc(opaque.p, targetLen)
+ if opaque.len + BufferSize > opaque.size:
+ opaque.size *= 2
+ opaque.p = realloc(opaque.p, opaque.size)
let p = cast[ptr UncheckedArray[uint8]](opaque.p)
- let n = response.body.recvData(addr p[opaque.len], BufferSize)
+ let diff = opaque.size - opaque.len
+ let n = response.body.recvData(addr p[opaque.len], diff)
opaque.len += n
if n == 0:
break
@@ -211,13 +214,13 @@ proc onFinishBlob(response: Response; success: bool) =
proc blob*(response: Response): Promise[JSResult[Blob]] {.jsfunc.} =
if response.bodyUsed:
- let p = newPromise[JSResult[Blob]]()
let err = JSResult[Blob].err(newTypeError("Body has already been consumed"))
- p.resolve(err)
- return p
+ return newResolvedPromise(err)
let opaque = BlobOpaque(
bodyRead: newPromise[JSResult[Blob]](),
- contentType: response.getContentType()
+ contentType: response.getContentType(),
+ p: alloc(BufferSize),
+ size: BufferSize
)
response.opaque = opaque
response.onRead = onReadBlob
diff --git a/src/local/client.nim b/src/local/client.nim
index c8bf9b24..c20a460b 100644
--- a/src/local/client.nim
+++ b/src/local/client.nim
@@ -356,7 +356,7 @@ proc input(client: Client): EmptyPromise =
p.resolve()
return p
-when not defined(android):
+when ioselSupportedPlatform:
let SIGWINCH {.importc, header: "<signal.h>", nodecl.}: cint
proc showConsole(client: Client) {.jsfunc.} =
@@ -542,7 +542,7 @@ proc handleError(client: Client; fd: int) =
proc inputLoop(client: Client) =
let selector = client.selector
selector.registerHandle(int(client.pager.term.istream.fd), {Read}, 0)
- when not defined(android):
+ when ioselSupportedPlatform:
let sigwinch = selector.registerSignal(int(SIGWINCH), 0)
var keys: array[64, ReadyKey]
while true:
@@ -554,7 +554,7 @@ proc inputLoop(client: Client) =
client.handleWrite(event.fd)
if Error in event.events:
client.handleError(event.fd)
- when not defined(android):
+ when ioselSupportedPlatform:
if Signal in event.events:
assert event.fd == sigwinch
client.pager.windowChange()
diff --git a/src/local/container.nim b/src/local/container.nim
index 8f295ad9..e22f0f4e 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -101,11 +101,15 @@ type
ContainerFlag* = enum
cfCloned, cfUserRequested, cfHasStart, cfCanReinterpret, cfSave, cfIsHTML
+ CachedImageState* = enum
+ cisLoading, cisCanceled, cisLoaded
+
CachedImage* = ref object
- loaded*: bool
+ state*: CachedImageState
width*: int
height*: int
- data*: Blob
+ data*: Blob # mmapped blob of image data
+ cacheId*: int # cache id of the file backing "data"
bmp*: NetworkBitmap
# Following variables are always 0 in kitty mode; they exist to support
# sixel cropping.
diff --git a/src/local/pager.nim b/src/local/pager.nim
index fa205596..d6e31e2a 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -214,6 +214,10 @@ proc clearStatus(pager: Pager) =
proc setContainer*(pager: Pager; c: Container) {.jsfunc.} =
if pager.term.imageMode != imNone and pager.container != nil:
+ for cachedImage in pager.container.cachedImages:
+ if cachedImage.state == cisLoaded:
+ pager.loader.removeCachedItem(cachedImage.cacheId)
+ cachedImage.state = cisCanceled
pager.container.cachedImages.setLen(0)
pager.container = c
if c != nil:
@@ -503,6 +507,9 @@ proc redraw(pager: Pager) {.jsfunc.} =
if pager.container.select != nil:
pager.container.select.redraw = true
+proc getTempFile(pager: Pager; ext = ""): string =
+ return getTempFile(pager.config.external.tmpdir, ext)
+
proc loadCachedImage(pager: Pager; container: Container; image: PosBitmap;
offx, erry, dispw: int) =
let bmp = image.bmp
@@ -517,20 +524,53 @@ proc loadCachedImage(pager: Pager; container: Container; image: PosBitmap;
pager.loader.shareCachedItem(bmp.cacheId, pager.loader.clientPid,
container.process)
let imageMode = pager.term.imageMode
- let headers = newHeaders()
- if image.width != bmp.width or image.height != bmp.height:
- headers.add("Cha-Image-Target-Dimensions", $image.width & 'x' &
- $image.height)
pager.loader.fetch(newRequest(
newURL("img-codec+" & bmp.contentType.after('/') & ":decode").get,
httpMethod = hmPost,
- headers = headers,
body = RequestBody(t: rbtCache, cacheId: bmp.cacheId),
- )).then(proc(res: JSResult[Response]) =
+ tocache = true
+ )).then(proc(res: JSResult[Response]): FetchPromise =
+ # remove previous step
+ pager.loader.removeCachedItem(bmp.cacheId)
+ if res.isNone:
+ return nil
+ let response = res.get
+ let cacheId = response.outputId # set by loader in tocache
+ if cachedImage.state == cisCanceled: # container is no longer visible
+ pager.loader.removeCachedItem(cacheId)
+ return nil
+ if image.width == bmp.width and image.height == bmp.height:
+ # skip resize
+ return newResolvedPromise(res)
+ # resize
+ # use a temp file, so that img-resize can mmap its output
+ let headers = newHeaders({
+ "Cha-Image-Dimensions": $bmp.width & 'x' & $bmp.height,
+ "Cha-Image-Target-Dimensions": $image.width & 'x' & $image.height
+ })
+ let p = pager.loader.fetch(newRequest(
+ newURL("cgi-bin:resize").get,
+ httpMethod = hmPost,
+ headers = headers,
+ body = RequestBody(t: rbtCache, cacheId: cacheId),
+ tocache = true
+ )).then(proc(res: JSResult[Response]): FetchPromise =
+ # ugh. I must remove the previous cached item, but only after
+ # resize is done...
+ pager.loader.removeCachedItem(cacheId)
+ return newResolvedPromise(res)
+ )
+ response.resume()
+ response.close()
+ return p
+ ).then(proc(res: JSResult[Response]) =
if res.isNone:
- pager.loader.removeCachedItem(bmp.cacheId)
return
let response = res.get
+ let cacheId = response.outputId
+ if cachedImage.state == cisCanceled:
+ pager.loader.removeCachedItem(cacheId)
+ return
let headers = newHeaders({
"Cha-Image-Dimensions": $image.width & 'x' & $image.height
})
@@ -550,23 +590,40 @@ proc loadCachedImage(pager: Pager; container: Container; image: PosBitmap;
url,
httpMethod = hmPost,
headers = headers,
- body = RequestBody(t: rbtOutput, outputId: response.outputId),
+ body = RequestBody(t: rbtCache, cacheId: cacheId),
+ tocache = true
)
let r = pager.loader.fetch(request)
response.resume()
response.close()
- r.then(proc(res: JSResult[Response]): Promise[JSResult[Blob]] =
+ r.then(proc(res: JSResult[Response]) =
+ # remove previous step
+ pager.loader.removeCachedItem(cacheId)
if res.isNone:
- let p = newPromise[JSResult[Blob]]()
- p.resolve(JSResult[Blob].err(res.error))
- return p
- return res.get.blob()
- ).then(proc(res: JSResult[Blob]) =
- if res.isSome:
- container.redraw = true
- cachedImage.data = res.get
- cachedImage.loaded = true
- pager.loader.removeCachedItem(bmp.cacheId)
+ return
+ let cacheId = res.get.outputId
+ if cachedImage.state == cisCanceled:
+ pager.loader.removeCachedItem(cacheId)
+ return
+ let ps = pager.loader.openCachedItem(cacheId)
+ if ps == nil:
+ pager.loader.removeCachedItem(cacheId)
+ return
+ let mem = ps.recvDataLoopOrMmap()
+ ps.sclose()
+ if mem == nil:
+ pager.loader.removeCachedItem(cacheId)
+ return
+ let blob = newBlob(mem.p, mem.len, "image/x-sixel",
+ (proc(opaque, p: pointer) =
+ let mem = cast[MaybeMappedMemory](opaque)
+ dealloc(mem)
+ ), mem
+ )
+ container.redraw = true
+ cachedImage.data = blob
+ cachedImage.state = cisLoaded
+ cachedImage.cacheId = cacheId
)
)
container.cachedImages.add(cachedImage)
@@ -592,7 +649,7 @@ proc initImages(pager: Pager; container: Container) =
if cached == nil:
pager.loadCachedImage(container, image, offx, erry, dispw)
continue
- if not cached.loaded:
+ if cached.state != cisLoaded:
continue # loading
let canvasImage = pager.term.loadImage(cached.data, container.process,
imageId, image.x - container.fromx, image.y - container.fromy,
@@ -1110,7 +1167,7 @@ proc getEditorCommand(pager: Pager; file: string; line = 1): string {.jsfunc.} =
proc openInEditor(pager: Pager; input: var string): bool =
try:
- let tmpf = getTempFile(pager.config.external.tmpdir)
+ let tmpf = pager.getTempFile()
if input != "":
writeFile(tmpf, input)
let cmd = pager.getEditorCommand(tmpf)
@@ -1815,7 +1872,7 @@ proc checkMailcap(pager: Pager; container: Container; stream: SocketStream;
if entry == nil:
return CheckMailcapResult(connect: true, fdout: stream.fd, found: false)
let ext = url.pathname.afterLast('.')
- let tempfile = getTempFile(pager.config.external.tmpdir, ext)
+ let tempfile = pager.getTempFile(ext)
let outpath = if entry.nametemplate != "":
unquoteCommand(entry.nametemplate, contentType, tempfile, url)
else:
diff --git a/src/types/url.nim b/src/types/url.nim
index ab2aa9c9..61f051ac 100644
--- a/src/types/url.nim
+++ b/src/types/url.nim
@@ -1169,11 +1169,11 @@ proc update(params: URLSearchParams) =
else:
params.url.query = some(serializedQuery)
-proc append*(params: URLSearchParams; name, value: string) {.jsfunc.} =
+proc append(params: URLSearchParams; name, value: string) {.jsfunc.} =
params.list.add((name, value))
params.update()
-proc delete*(params: URLSearchParams; name: string) {.jsfunc.} =
+proc delete(params: URLSearchParams; name: string) {.jsfunc.} =
for i in countdown(params.list.high, 0):
if params.list[i][0] == name:
params.list.delete(i)
@@ -1182,13 +1182,15 @@ proc get*(params: URLSearchParams; name: string): Option[string] {.jsfunc.} =
for it in params.list:
if it[0] == name:
return some(it[1])
+ return none(string)
-proc getAll*(params: URLSearchParams; name: string): seq[string] {.jsfunc.} =
+proc getAll(params: URLSearchParams; name: string): seq[string] {.jsfunc.} =
+ result = @[]
for it in params.list:
if it[0] == name:
result.add(it[1])
-proc set*(params: URLSearchParams; name, value: string) {.jsfunc.} =
+proc set(params: URLSearchParams; name, value: string) {.jsfunc.} =
var first = true
for i in 0..params.list.high:
if params.list[i][0] == name: