diff --git a/run.js b/run.js
index 187b1f1cfc09eb6474add9bd587ff734da499e00..5b5d870a7dee93a5c59862b2f81fa080b028a0f7 100755
--- a/run.js
+++ b/run.js
@@ -101,9 +101,10 @@ function substitutionMiddleware(req, resp, next) {
                             contentType.substr(0,4) === 'text'))
         {
             if (buf) {
-                var l = buf.length;
+                if (Buffer.isBuffer(buf)) buf = buf.toString('utf8');
+                var l = Buffer.byteLength(buf);
                 buf = subHostNames(buf);
-                if (l != buf.length) resp.setHeader('Content-Length', buf.length);
+                if (l != Buffer.byteLength(buf)) resp.setHeader('Content-Length', Buffer.byteLength(buf));
             }
         }
         if (buf && buf.length) {