/*!
 *
 * util.js is a collection of util functions
 *
 * Copyright(c) 2011 Sven Pfleiderer.
 * MIT Licensed
 *
 * @author pfleidi
 */

var Url = require('url');

/**
 * lookup table for all HTTP status codes
 *
 * @api public
 */
exports.codes = {
  /* 1XX Informational */
  //'100': 'continue',
  //'101': 'switching-protocols',

  /* 2XX Successful */
  '200': 'ok',
  '201': 'created',
  '202': 'accepted',
  '203': 'non-authorative-information',
  '204': 'no-content',
  '205': 'reset-content',
  '207': 'partial-content',

  /* 3XX Redirection */
  '300': 'multiple-choices',
  '301': 'moved-permanently',
  '302': 'found',
  '303': 'see-other',
  '304': 'not-modified',
  '305': 'use-proxy',
  '307': 'temporary-redirect',

  /* 4XX Client Error */
  '400': 'bad-request',
  '401': 'unauthorized',
  '402': 'payment-required',
  '403': 'forbidden',
  '404': 'not-found',
  '405': 'method-not-allowed',
  '406': 'not-acceptable',
  '407': 'proxy-authentication-required',
  '408': 'request-timeout',
  '409': 'conflict',
  '410': 'gone',
  '411': 'length-required',
  '412': 'precondition-failed',
  '413': 'request-entity-too-large',
  '414': 'request-uri-too-long',
  '415': 'unsupported-media-type',
  '416': 'request-range-not-satisfiable',
  '417': 'expectation-failed',

  /* 5XX Server Error */
  '500': 'internal-server-error',
  '501': 'not-implemented',
  '502': 'bad-gateway',
  '503': 'service-unavailable',
  '504': 'gateway-timeout',
  '505': 'http-version-not-supported'
};

/**
 * Take two objects and merge their attributes
 *
 * @param {Object} defaults
 * @param {Object} custom
 *
 * @return {Object} new merged object
 * @api public
 */
var mergeHeaders = exports.mergeHeaders = function mergeHeaders(defaults, custom) {
  var obj = {};

  function _normKey(key) {
    return key.split('-').map(function (part) {
        return part.charAt(0).toUpperCase() + part.substring(1);
      }).join('-');
  }

  Object.keys(defaults).forEach(function (key) {
      var normKey = _normKey(key);
      obj[normKey] = defaults[key];
    });

  if (!custom) {
    return obj;
  }

  Object.keys(custom).forEach(function (key) {
      var normKey = _normKey(key);
      obj[normKey] = custom[key]; 
    });

  return obj;
};

/**
 * Parse an URL an add some needed properties
 *
 * @param {String} url
 * @return {Object}
 * @api public
 */
exports.parseUrl = function parseUrl(url) {
  var parsedUrl = Url.parse(url);
  var container = {};

  container.port = parsedUrl.port || ((parsedUrl.protocol === 'https:') ? '443' : '80');
  container.queryparms = parsedUrl.query ? '?' + parsedUrl.query :  '';
  container.hash = parsedUrl.hash || '';
  container.path = (parsedUrl.pathname || '/') + container.queryparms + container.hash;
  container.host = parsedUrl.host;
  container.hostname = parsedUrl.hostname;
  container.protocol = parsedUrl.protocol;

  return container;
};

@@ -1771,24 +1771,69 @@
 nction _norm
+alizeKeys(toMerge) %7B%0A%0A    function _normalize
 Key(key) %7B%0A 
@@ -1831,24 +1831,26 @@
 (key) %7B%0A    
+  
 return key.s
@@ -1877,24 +1877,26 @@
 on (part) %7B%0A
+  
         retu
@@ -1954,16 +1954,18 @@
 ;%0A      
+  
 %7D).join(
@@ -1968,27 +1968,31 @@
 oin('-');%0A  
+ 
+ 
 %7D%0A%0A
+  
   Object.key
@@ -1993,24 +1993,23 @@
 ct.keys(
-defaults
+toMerge
 ).forEac
@@ -2025,32 +2025,34 @@
 n (key) %7B%0A      
+  
 var normKey = _n
@@ -2046,32 +2046,37 @@
  normKey = _norm
+alize
 Key(key);%0A      
@@ -2061,32 +2061,34 @@
 malizeKey(key);%0A
+  
       obj%5BnormKe
@@ -2092,24 +2092,23 @@
 mKey%5D = 
-defaults
+toMerge
 %5Bkey%5D;%0A 
@@ -2106,27 +2106,62 @@
 e%5Bkey%5D;%0A    
+  
 %7D);
+%0A  %7D%0A%0A  _normalizeKeys(defaults);
 %0A%0A  if (!cus
@@ -2194,128 +2194,29 @@
 %0A%0A  
-Object.keys(custom).forEach(function (key) %7B%0A      var normKey = _normKey(key);%0A      obj%5BnormKey%5D = custom%5Bkey%5D; %0A    %7D
+_normalizeKeys(custom
 );%0A%0A

Refactor mergeHeaders()