Skip to content
Permalink
Browse files

Re-<base> serve command on the fly

  • Loading branch information...
da2x committed Aug 5, 2015
1 parent 682f80d commit 57772d08dd6de8a8ed2aed42657360f0827c6563
Showing with 28 additions and 3 deletions.
  1. +28 −3 nikola/plugins/command/serve.py
@@ -28,7 +28,9 @@

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

try:
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 = f.read().decode('utf8')
f.close()
data = re.sub(r'<base\s([^>]*)>', '<!--base \g<1>-->', data, re.IGNORECASE)
data = data.encode('utf8')
f = StringIO()
f.write(data)
f.seek(0)
filtered_bytes = f.tell()

self.send_response(200)
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]))
else:
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.
You can’t perform that action at this time.