diff options
-rw-r--r-- | src/local/container.nim | 34 | ||||
-rw-r--r-- | src/local/pager.nim | 28 | ||||
-rw-r--r-- | src/server/buffer.nim | 22 |
3 files changed, 40 insertions, 44 deletions
diff --git a/src/local/container.nim b/src/local/container.nim index e61d4fbf..4114566b 100644 --- a/src/local/container.nim +++ b/src/local/container.nim @@ -101,6 +101,9 @@ type hovertext: array[HoverType, string] lastpeek: HoverType source*: BufferSource + # if set, this *overrides* any content type received from the network. (this + # is because it stores the content type from the -T flag.) + contentType* {.jsget.}: Option[string] pos: CursorPosition bpos: seq[CursorPosition] highlights: seq[Highlight] @@ -138,7 +141,8 @@ jsDestructor(Container) proc newBuffer*(forkserver: ForkServer, config: BufferConfig, source: BufferSource, attrs: WindowAttributes, title = "", - redirectdepth = 0, canreinterpret = true, fd = FileHandle(-1)): Container = + redirectdepth = 0, canreinterpret = true, fd = FileHandle(-1), + contentType: Option[string]): Container = let (process, loaderPid) = forkserver.forkBuffer(source, config, attrs) if fd != -1: loaderPid.passFd(source.request.url.host, fd) @@ -152,6 +156,7 @@ proc newBuffer*(forkserver: ForkServer, config: BufferConfig, process: process, loaderPid: loaderPid, source: source, + contentType: contentType, width: attrs.width, height: attrs.height - 1, title: title, @@ -165,8 +170,8 @@ proc newBuffer*(forkserver: ForkServer, config: BufferConfig, proc newBufferFrom*(forkserver: ForkServer, attrs: WindowAttributes, container: Container, contentTypeOverride: string): Container = + container.contentType = some(contentTypeOverride) var source = container.source - source.contentType = some(contentTypeOverride) source.request = newRequest(source.request.url, fromcache = true) let config = container.config let loaderPid = container.loaderPid @@ -236,9 +241,6 @@ proc clone*(container: Container, newurl: URL): Promise[Container] = func charset*(container: Container): Charset = return container.source.charset -func contentType*(container: Container): Option[string] {.jsfget.} = - return container.source.contentType - func lineLoaded(container: Container, y: int): bool = return y - container.lineshift in 0..container.lines.high @@ -1396,15 +1398,17 @@ proc load(container: Container) = if res.redirect != nil: container.triggerEvent(ContainerEvent(t: REDIRECT, request: res.redirect)) container.source.charset = res.charset - container.ishtml = res.contentType == "text/html" - if res.contentType == "application/octet-stream": - let contentType = guessContentType(container.location.pathname, - "application/octet-stream", container.config.mimeTypes) - if contentType != "application/octet-stream": - container.iface.setContentType(contentType) - container.source.contentType = some(contentType) - else: - container.source.contentType = some(res.contentType) + if container.contentType.isNone: + if res.contentType == "application/octet-stream": + let contentType = guessContentType(container.location.pathname, + "application/octet-stream", container.config.mimeTypes) + if contentType != "application/octet-stream": + container.contentType = some(contentType) + else: + container.contentType = some(res.contentType) + else: + container.contentType = some(res.contentType) + container.ishtml = container.contentType.get == "text/html" container.triggerEvent(CHECK_MAILCAP) else: if res.errorMessage != "": @@ -1423,7 +1427,7 @@ proc startload*(container: Container) = container.onload(res)) proc connect2*(container: Container): EmptyPromise = - return container.iface.connect2() + return container.iface.connect2(container.ishtml) proc redirectToFd*(container: Container, fdin: FileHandle, wait, cache: bool): EmptyPromise = diff --git a/src/local/pager.nim b/src/local/pager.nim index 5d84aed5..37d6ada8 100644 --- a/src/local/pager.nim +++ b/src/local/pager.nim @@ -452,8 +452,8 @@ proc addContainer*(pager: Pager, container: Container) = pager.setContainer(container) proc newBuffer(pager: Pager, bufferConfig: BufferConfig, source: BufferSource, - title = "", redirectdepth = 0, canreinterpret = true, fd = FileHandle(-1)): - Container = + title = "", redirectdepth = 0, canreinterpret = true, fd = FileHandle(-1), + contentType = none(string)): Container = return newBuffer( pager.forkserver, bufferConfig, @@ -462,7 +462,8 @@ proc newBuffer(pager: Pager, bufferConfig: BufferConfig, source: BufferSource, title, redirectdepth, canreinterpret, - fd + fd, + contentType ) proc dupeBuffer(pager: Pager, container: Container, location: URL) = @@ -683,7 +684,7 @@ proc applySiteconf(pager: Pager, url: var URL): BufferConfig = # Load request in a new buffer. proc gotoURL(pager: Pager, request: Request, prevurl = none(URL), - ctype = none(string), cs = CHARSET_UNKNOWN, replace: Container = nil, + contentType = none(string), cs = CHARSET_UNKNOWN, replace: Container = nil, redirectdepth = 0, referrer: Container = nil) = if referrer != nil and referrer.config.referer_from: request.referer = referrer.location @@ -698,7 +699,6 @@ proc gotoURL(pager: Pager, request: Request, prevurl = none(URL), # feedback on what is actually going to happen when typing a URL; TODO. let source = BufferSource( request: request, - contentType: ctype, charset: cs ) if referrer != nil: @@ -706,7 +706,8 @@ proc gotoURL(pager: Pager, request: Request, prevurl = none(URL), let container = pager.newBuffer( bufferconfig, source, - redirectdepth = redirectdepth + redirectdepth = redirectdepth, + contentType = contentType ) if replace != nil: container.replace = replace @@ -759,7 +760,7 @@ proc loadURL*(pager: Pager, url: string, ctype = none(string), pager.alert("Invalid URL " & url) else: let prevc = pager.container - pager.gotoURL(newRequest(urls.pop()), ctype = ctype, cs = cs) + pager.gotoURL(newRequest(urls.pop()), contentType = ctype, cs = cs) if pager.container != prevc: pager.container.retry = urls @@ -770,11 +771,16 @@ proc readPipe0*(pager: Pager, ctype: Option[string], cs: Charset, let bufferconfig = pager.applySiteconf(location) let source = BufferSource( request: newRequest(location), - contentType: some(ctype.get("text/plain")), charset: cs ) - return pager.newBuffer(bufferconfig, source, title = title, - canreinterpret = canreinterpret, fd = fd) + return pager.newBuffer( + bufferconfig, + source, + title = title, + canreinterpret = canreinterpret, + fd = fd, + contentType = some(ctype.get("text/plain")) + ) proc readPipe*(pager: Pager, ctype: Option[string], cs: Charset, fd: FileHandle, title: string) = @@ -1189,7 +1195,7 @@ proc handleEvent0(pager: Pager, container: Container, event: ContainerEvent): bo pager.deleteContainer(container) if container.retry.len > 0: pager.gotoURL(newRequest(container.retry.pop()), - ctype = container.contentType) + contentType = container.contentType) else: pager.alert("Can't load " & $container.location & " (" & container.errorMessage & ")") diff --git a/src/server/buffer.nim b/src/server/buffer.nim index 788c0aea..45949b90 100644 --- a/src/server/buffer.nim +++ b/src/server/buffer.nim @@ -60,7 +60,6 @@ import chame/tags type BufferSource* = object - contentType*: Option[string] # override charset*: Charset # fallback request*: Request @@ -69,8 +68,7 @@ type CLICK, FIND_NEXT_LINK, FIND_PREV_LINK, FIND_NTH_LINK, FIND_REV_NTH_LINK, FIND_NEXT_MATCH, FIND_PREV_MATCH, GET_LINES, UPDATE_HOVER, CONNECT, CONNECT2, GOTO_ANCHOR, CANCEL, GET_TITLE, SELECT, REDIRECT_TO_FD, - READ_FROM_FD, SET_CONTENT_TYPE, CLONE, FIND_PREV_PARAGRAPH, - FIND_NEXT_PARAGRAPH + READ_FROM_FD, CLONE, FIND_PREV_PARAGRAPH, FIND_NEXT_PARAGRAPH # LOADING_PAGE: istream open # LOADING_RESOURCES: istream closed, resources open @@ -877,8 +875,6 @@ proc connect*(buffer: Buffer): ConnectResult {.proxy.} = ) if response.charset != CHARSET_UNKNOWN: charset = charset - if buffer.source.contentType.isNone: - buffer.source.contentType = some(response.contentType) buffer.istream = response.body buffer.fd = response.body.fd needsAuth = response.status == 401 # Unauthorized @@ -893,24 +889,23 @@ proc connect*(buffer: Buffer): ConnectResult {.proxy.} = if referrerpolicy.isSome: buffer.loader.setReferrerPolicy(referrerpolicy.get) buffer.connected = true - let contentType = buffer.source.contentType.get("") - buffer.setHTML(contentType == "text/html") return ConnectResult( charset: charset, needsAuth: needsAuth, redirect: redirect, cookies: cookies, - contentType: contentType + contentType: response.contentType ) # After connect, pager will call one of the following: # * connect2, telling loader to load at last (we block loader until then) # * redirectToFd, telling loader to load into the passed fd -proc connect2*(buffer: Buffer) {.proxy.} = +proc connect2*(buffer: Buffer, ishtml: bool) {.proxy.} = if buffer.source.request.canredir: # Notify loader that we can proceed with loading the input stream. buffer.istream.swrite(false) buffer.istream.swrite(true) + buffer.setHTML(ishtml) buffer.istream.setBlocking(false) buffer.selector.registerHandle(buffer.fd, {Read}, 0) @@ -931,14 +926,9 @@ proc redirectToFd*(buffer: Buffer, fd: FileHandle, wait, cache: bool) buffer.istream.close() proc readFromFd*(buffer: Buffer, url: URL, ishtml: bool) {.proxy.} = - let contentType = if ishtml: - "text/html" - else: - "text/plain" let request = newRequest(url) buffer.source = BufferSource( request: request, - contentType: some(contentType), charset: buffer.source.charset ) buffer.setHTML(ishtml) @@ -948,10 +938,6 @@ proc readFromFd*(buffer: Buffer, url: URL, ishtml: bool) {.proxy.} = buffer.fd = response.body.fd buffer.selector.registerHandle(buffer.fd, {Read}, 0) -proc setContentType*(buffer: Buffer, contentType: string) {.proxy.} = - buffer.source.contentType = some(contentType) - buffer.setHTML(contentType == "text/html") - # As defined in std/selectors: this determines whether kqueue is being used. # On these platforms, we must not close the selector after fork, since kqueue # fds are not inherited after a fork. |