/**
 * @fileOverview XMLHttpRequest wrapper module for restartless addons
 * @author       YUKI "Piro" Hiroshi
 * @version      1
 * @description  
 *
 * @license
 *   The MIT License, Copyright (c) 2014 YUKI "Piro" Hiroshi.
 *   https://github.com/piroor/restartless/blob/master/license.txt
 * @url http://github.com/piroor/restartless
 */

var EXPORTED_SYMBOLS = [
  'get',
  'post'
];

var Deferred = require('jsdeferred').Deferred;

function get(aURI, aHeaders) {
  return sendRequest({
    method:  'GET',
    uri:     aURI,
    headers: aHeaders
  });
}

function post(aURI, aPostData, aHeaders) {
  return sendRequest({
    method:   'POST',
    uri:      aURI,
    headers:  aHeaders,
    postData: aPostData
  });
}

function sendRequest(aParams) {
  var deferred = new Deferred();

  var method   = (aParams.method || 'GET').toUpperCase();
  var uri      = aParams.uri || null;
  var headers  = aParams.headers || {};
  var postData = aParams.postData || null;

  if (!uri)
    throw new Error('no URL');

  var request;
  var listener = function(aEvent) {
    switch (aEvent.type) {
      case 'load':
      case 'error':
        break;
      default:
        return;
    }
    request.removeEventListener('load', listener, false);
    request.removeEventListener('error', listener, false);

    if (request.status == 200) {
      deferred.call(request);
    } else {
      try {
        deferred.fail(new Error(JSON.stringify({
          statusCode: request.status,
          body:       request.responseText,
          event:      {
            type:   aEvent.type,
            detail: aEvent.detail
          }
        })));
      } catch(error) {
        deferred.fail(new Error(JSON.stringify({
          error: {
            name:    error.name,
            message: error.message,
            error:   error
          },
          event: {
            type:   aEvent.type,
            detail: aEvent.detail
          }
        })));
      }
    }
  };

  Deferred.next(function() {
    request = Cc['@mozilla.org/xmlextras/xmlhttprequest;1']
                .createInstance(Ci.nsIXMLHttpRequest)
                .QueryInterface(Ci.nsIDOMEventTarget);
    request.open(method, uri, true);
    request.addEventListener('load', listener, false);
    request.addEventListener('error', listener, false);
    Object.keys(headers).forEach(function(aKey) {
      request.setRequestHeader(aKey, headers[aKey]);
    });
    request.send(postData);
  });

  return deferred;
}

@@ -121,17 +121,17 @@
 on      
-1
+2
 %0A * @des
@@ -381,10 +381,93 @@
 ost'
-%0A%5D
+,%0A  'RESPONSE_CONTENT_TYPE'%0A%5D;%0A%0Avar RESPONSE_CONTENT_TYPE = 'X-Override-Content-Type'
 ;%0A%0Av
@@ -2439,24 +2439,172 @@
 ion(aKey) %7B%0A
+      switch (aKey) %7B%0A        case RESPONSE_CONTENT_TYPE:%0A          request.overrideMimeType(headers%5BaKey%5D);%0A          break;%0A%0A        default:%0A    
       reques
@@ -2640,24 +2640,49 @@
 ers%5BaKey%5D);%0A
+          break;%0A      %7D%0A
     %7D);%0A    

Support overriding of the response content type