about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/css/sheet.nim1
-rw-r--r--src/html/parser.nim11
-rw-r--r--src/io/buffer.nim12
-rw-r--r--src/io/loader.nim6
4 files changed, 19 insertions, 11 deletions
diff --git a/src/css/sheet.nim b/src/css/sheet.nim
index ec7780b8..aab602ec 100644
--- a/src/css/sheet.nim
+++ b/src/css/sheet.nim
@@ -194,6 +194,7 @@ proc parseStylesheet*(s: Stream): CSSStylesheet =
   for v in css.value:
     if v of CSSAtRule: result.addAtRule(CSSAtRule(v))
     else: result.addRule(CSSQualifiedRule(v))
+  s.close()
 
 proc parseStylesheet*(s: string): CSSStylesheet =
   return newStringStream(s).parseStylesheet()
diff --git a/src/html/parser.nim b/src/html/parser.nim
index 105c5d0d..cbc7daa7 100644
--- a/src/html/parser.nim
+++ b/src/html/parser.nim
@@ -471,7 +471,7 @@ proc processDocumentPart(state: var HTMLParseState, buf: string) =
       process_char(buf[at])
       inc at
 
-proc parseHtml*(inputStream: Stream): Document =
+proc parseHtml(inputStream: Stream, savesource: bool, source: var string): Document =
   let document = newDocument()
   insertNode(document, document.root)
   insertNode(document.root, document.head)
@@ -487,6 +487,8 @@ proc parseHtml*(inputStream: Stream): Document =
   var lineBuf: string
   while not inputStream.atEnd():
     lineBuf = inputStream.readLine() & '\n'
+    if savesource:
+      source &= lineBuf
     buf &= lineBuf
 
     var at = 0
@@ -507,3 +509,10 @@ proc parseHtml*(inputStream: Stream): Document =
 
   inputStream.close()
   return document
+
+proc parseHtml*(inputStream: Stream, source: var string): Document =
+  return parseHtml(inputStream, true, source)
+
+proc parseHtml*(inputStream: Stream): Document =
+  var placeholder = ""
+  return parseHtml(inputStream, false, placeholder)
diff --git a/src/io/buffer.nim b/src/io/buffer.nim
index 7356b084..8592b98f 100644
--- a/src/io/buffer.nim
+++ b/src/io/buffer.nim
@@ -736,7 +736,7 @@ proc loadResources(buffer: Buffer, document: Document) =
           if url.get.scheme == buffer.location.scheme:
             let res = buffer.loader.getPage(url.get)
             if res.s != nil and res.contenttype == "text/css":
-              let sheet = parseStylesheet(res.s.readAll())
+              let sheet = parseStylesheet(res.s)
               elem.parentElement.sheets.add(sheet)
 
     for i in countdown(elem.children.high, 0):
@@ -747,14 +747,10 @@ proc load*(buffer: Buffer) =
   case buffer.contenttype
   of "text/html":
     if not buffer.streamclosed:
-      #TODO not sure what to do with this.
-      #Ideally we could just throw away the source data after parsing but then
-      #source view won't work. Well we could still generate it... best would be a
-      #config option like a) store source b) generate source
-      buffer.source = buffer.istream.readAll()
-      buffer.istream.close()
+      buffer.document = parseHtml(buffer.istream, buffer.source)
       buffer.streamclosed = true
-    buffer.document = parseHtml(newStringStream(buffer.source))
+    else:
+      buffer.document = parseHtml(newStringStream(buffer.source))
     buffer.document.location = buffer.location
     buffer.loadResources(buffer.document)
   else:
diff --git a/src/io/loader.nim b/src/io/loader.nim
index 4a10d336..d0181262 100644
--- a/src/io/loader.nim
+++ b/src/io/loader.nim
@@ -16,7 +16,7 @@ type
 
 const DefaultHeaders = {
   "User-Agent": "chawan",
-  "Accept": "text/html", "text/*;q=0.5",
+  "Accept": "text/html,text/*;q=0.5",
   "Accept-Language": "en;q=1.0",
   "Pragma": "no-cache",
   "Cache-control": "no-cache",
@@ -35,7 +35,9 @@ proc getPage*(loader: FileLoader, url: Url, smethod: HttpMethod = HttpGet, mimet
     result.contenttype = guessContentType(path)
     result.s = newFileStream(path, fmRead)
   elif url.scheme == "http" or url.scheme == "https":
-    var requestheaders = newHttpHeaders(DefaultHeaders, true)
+    var requestheaders = newHttpHeaders(true)
+    for header in DefaultHeaders:
+      requestheaders[header[0]] = header[1]
     if mimetype != "":
       requestheaders["Content-Type"] = mimetype 
     let resp = loader.http.request(url.serialize(true), smethod, body, requestheaders, multipart)