1. John Wright
  2. Frontfax

Commits

John Wright  committed babd2dc

Proxy logging and better replacements.

  • Participants
  • Parent commits 39651b0
  • Branches master

Comments (0)

Files changed (5)

File package.json

View file
     "module-index": "3.0.2",
     "request": "2.12.0",
     "socket.io": "~0.9.13",
-    "gaze": "~0.3.2"
+    "gaze": "~0.3.2",
+    "bytes": "~0.2.0",
+    "colors": "~0.6.0-1"
   },
   "devDependencies": {
     "coffee-script": "1.4.0",

File src/app/controllers/proxy.coffee

View file
 exports.request = (url)->
 	(req, res)->
 		requestUrl = url + req.originalUrl
+		match      = path.extname(req.path).match /html|js|css$/
 
-		if req.method is 'GET' and path.extname(req.path) in ['.html', '']
+		if req.method is 'GET' and match?
 			req.headers['accept-encoding'] = ''
+			res.header 'proxied', true
 			req.pipe request requestUrl, (err, proxyRes, body)->
 				body = socket.addClientCode body
-				res.type 'text/html'
+				res.type match.toString()
 				res.send body
 
 		else

File src/app/controllers/socket.coffee

View file
 	(chunk, encoding)->
 		html = /html/.test @get('Content-Type')
 		if chunk and html
-			chunk = chunk.toString encoding
-			if chunk.indexOf('</body>') >= 0
-				newChunk = socket.addClientCode chunk, encoding
+			newChunk = chunk.toString encoding
+			if newChunk.indexOf('</body>') >= 0
+				newChunk = socket.addClientCode newChunk, encoding
 				try
-					@set 'Content-Length', newChunk.length
+					if @get 'content-length'
+						@set 'content-length', newChunk.length
 					chunk = newChunk
 				catch e
 					# The response is from a proxy

File src/app/controllers/util.coffee

View file
+util    = require '../../lib/util'
+colors  = require 'colors'
+express = require 'express'
+
 exports.extractPort = ->
 	(req, res, next)->
-		reg   = /:(\d+)/
-		match = req.headers.host.match reg
-
-		if match
-			req.port = parseInt match[1]
-		else
-			req.port = 80
-
+		req.port = res.app.get 'port'
 		next()
 
-exports.replaceInResponse = (app, replacements)->
-	res    = app.response
-	writer = res.write
-
-	res.write = (chunk, encoding)->
+contentReplacer = (replacements, method)->
+	(chunk, encoding)->
 		isText = /text\//.test @get('Content-Type')
-		if chunk and isText
+		if chunk? and isText
 			newChunk = chunk.toString encoding
-			for own key, value of replacements
-				newChunk = newChunk.replace key, value
+			for own key, replacement of replacements
+				newChunk = newChunk.replace replacement.reg, replacement.value
 			try
-				@set 'Content-Length', newChunk.length
-				chunk = newChunk
-			catch e
-				# The response is from a proxy
-		writer.call this, chunk, encoding
+				chunk = new Buffer newChunk, encoding
+				if @get 'content-length'
+					@set 'content-length', chunk.length
+		method.call @, chunk, encoding
+
+exports.replaceInResponse = (app, replacements)->
+	for own key, value of replacements
+		console.log "Will be replacing \"#{key}\" with \"#{value}\""
+		replacements[key] =
+			reg   : new RegExp util.escapeRegExp(key), 'g'
+			value : value
+	
+	res       = app.response
+	res.end   = contentReplacer replacements, res.end
+	res.write = contentReplacer replacements, res.write
+
+exports.loggerFormat = (tokens, req, res)->
+	proxy   = res.getHeader 'proxied'
+	output  = if proxy then 'PROXY '.grey else ''
+	output += express.logger.dev tokens, req, res
 

File src/app/index.coffee

View file
 # Basic configuration
 app.set 'port', process.env.PORT or 8080
 app.set 'views', staticDir
-app.use express.logger 'dev'
+express.logger.format 'frontfax', controllers.util.loggerFormat
+app.use express.logger 'frontfax'
 app.use controllers.util.extractPort()
 app.use express.methodOverride()
 controllers.socket.refreshClient app
-if config.replacements
-	controllers.util.replaceInResponse app, config.replacements
+config.replacements = {} unless config.replacements
+config.replacements[config.proxy] = "" if config.proxy
+controllers.util.replaceInResponse app, config.replacements
 app.use app.router
 app.use express.errorHandler()