var sprintf = require('sprintf').sprintf;

var httpConstants = require('http/constants');
var bencode = require('util/bencode');

function returnError(res, code, msg) {
  var headers;
  code = code || 900;
  msg = msg || '';

  headers = {
    'Content-Length': msg.length,
    'Connection': 'close'
  };

  res.writeHead(code, headers);
  res.end(msg);
}

/**
 * Return a bencoded response
 */
function returnResponse(res, code, obj) {
  var headers, bencoded;
  code = code || 200;

  bencoded = bencode.benc(obj);
  res.writeHead(code, headers);
  res.end(bencoded);
}

function extractParams(req, paramsObj) {
  var paramName, paramRules, paramValue;
  var paramValues = {};

  for (paramName in paramsObj) {
    if (paramsObj.hasOwnProperty(paramName)) {
      paramRules = paramsObj[paramName];
      paramValue = req.param(paramName, paramRules['default_value']);

      if (paramValue !== null) {
        if (paramRules['type'] === 'number') {
          paramValue = parseInt(paramValue, 10);
        }
      }

      paramValues[paramName] = paramValue;
    }
  }

  return paramValues;
}

exports.returnError = returnError;
exports.returnResponse = returnResponse;
exports.extractParams = extractParams;

@@ -138,21 +138,24 @@
 n return
-Error
+Response
 (res, co
@@ -162,90 +162,111 @@
 de, 
-msg) %7B%0A  var headers;%0A  code = code
+headers, data) %7B%0A  data = data
  %7C%7C 
-900
+''
 ;%0A  
-msg = msg
+headers = headers
  %7C%7C 
-''
+%7B%7D
 ;%0A%0A  
-headers = %7B%0A    
+if (data.lenght %3E 0) %7B%0A    headers%5B
 'Con
@@ -281,26 +281,39 @@
 gth'
-: msg
+%5D = data
 .length
-,%0A    
+;%0A  %7D%0A  headers%5B
 'Con
@@ -324,17 +324,19 @@
 ion'
-:
+%5D =
  'close'
 %0A  %7D
@@ -331,20 +331,16 @@
  'close'
-%0A  %7D
 ;%0A%0A  res
@@ -376,16 +376,157 @@
 res.end(
+data);%0A%7D%0A%0A%0Afunction returnError(res, code, msg) %7B%0A  var headers;%0A  code = code %7C%7C 900;%0A  msg = msg %7C%7C '';%0A%0A  returnResponse(res, code, null, 
 msg);%0A%7D%0A
@@ -571,32 +571,40 @@
 %0Afunction return
+Bencoded
 Response(res, co
@@ -702,54 +702,137 @@
   re
-s.writeHead(code, headers);%0A  res.end(bencoded
+turnResponse(res, code, null, bencoded);%0A%7D%0A%0Afunction returnJson(res, code, obj) %7B%0A  returnResponse(res, code, JSON.stringify(obj)
 );%0A%7D

Add more utility functions.