Skip to content
Browse files
Re-<base> serve command on the fly
  • Loading branch information
da2x committed Aug 5, 2015
1 parent 682f80d commit 57772d08dd6de8a8ed2aed42657360f0827c6563
Showing 1 changed file with 28 additions and 3 deletions.
@@ -28,7 +28,9 @@

from __future__ import print_function
import os
import re
import socket
import tempfile
import webbrowser
from BaseHTTPServer import HTTPServer
@@ -37,6 +39,12 @@
from http.server import HTTPServer # NOQA
from http.server import SimpleHTTPRequestHandler # NOQA

from StringIO import StringIO
except ImportError:
from io import BytesIO as StringIO # NOQA

from nikola.plugin_categories import Command
from nikola.utils import get_logger, STDERR_HANDLER

@@ -221,14 +229,31 @@ def send_head(self):
except IOError:
self.send_error(404, "File not found")
return None

filtered_bytes = None
if ctype == 'text/html':
"""Comment out any <base> to allow local resolution of relative URLs."""
data ='utf8')
data = re.sub(r'<base\s([^>]*)>', '<!--base \g<1>-->', data, re.IGNORECASE)
data = data.encode('utf8')
f = StringIO()
filtered_bytes = f.tell()

self.send_header("Content-type", ctype)
if os.path.splitext(path)[1] == '.svgz':
# Special handling for svgz to make it work nice with browsers.
self.send_header("Content-Encoding", 'gzip')
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))

if filtered_bytes is None:
fs = os.fstat(f.fileno())
self.send_header('Content-Length', str(fs[6]))
self.send_header('Content-Length', filtered_bytes)

# begin no-cache patch
# For standard requests.
self.send_header("Cache-Control", "no-cache, no-store, "

0 comments on commit 57772d0

Please sign in to comment.