<commit_before>const HTMLNS = "http://www.w3.org/1999/xhtml";

function $(elementId) {
  return document.getElementById(elementId);
}

var restclient = {
	passwordObject: null,
	_stringBundle: null,

	checkEnterKeyPress: function(evt){
		switch( evt.keyCode ){
			case 13:
				restclient.doRequest();
				break;
			default:
				break;
		}
    
    return true;
	},

	checkDeleteKeyPress: function(evt){
		switch( evt.keyCode ){
			case 46:
				restclient.deleteSelectedHeader();
				break;
			default:
				break;
		}
	},

	deleteSelectedHeader: function(){
	  var headerList = $('headerList');
    var reqHeaderChilds = $('reqHeaderChilds');
    if (headerList.view.selection.count > 0 && headerList.editingRow < 0) {
      for (var i=reqHeaderChilds.childNodes.length-1 ; i>=0 ; i--) {
         if (headerList.view.selection.isSelected(i))
            reqHeaderChilds.removeChild(reqHeaderChilds.childNodes[i]);
      }
    }
	},

	clearRequestHeader: function(){
        var reqHeaderChilds = $('reqHeaderChilds');
        for (var i=reqHeaderChilds.childNodes.length-1 ; i>=0 ; i--) {
            reqHeaderChilds.removeChild(reqHeaderChilds.childNodes[i]);
        }
	},

  init: function(){
    var controller = new TabController();
    controller.tbRequestUrl = "dupa tam";

    this._stringBundle = $("string-bundle");
	  this.updateSaveButton();
    var that = this;
    var requestMethodDropdown = $('requestMethod');
    var requestUrlDropdown = $('tbRequestUrl');
    requestMethodDropdown.addEventListener('command', function() { that.updateOAuthAuthorizationHeader(); }, false);
	},

	updateLogin: function(){
	  var login = $("login-icon");
		if( ! this.isAuthorizationHeaderSet() ){
			login.label = this._stringBundle.getString("restclient.login");
			login.checked = false;
		}else{
			login.label = this._stringBundle.getString("restclient.logout");
			login.checked = true;
		}
	},
  
  isAuthorizationHeaderSet: function() {
	  var reqHeaderChilds = $('reqHeaderChilds');
    for (var i=reqHeaderChilds.childNodes.length-1 ; i>=0 ; i--) {
      if(reqHeaderChilds.childNodes[i].childNodes[0].childNodes[0].getAttribute('label') == "Authorization") {
        return true;
      }
    }
    
    return false;
  },

	doLogin: function(){
		var login = $("login-icon");
    
    // If the login button is already checked, clear the Authorization header and uncheck the button
		if(login.checked) {
      this.oAuthPasswordObject = null;
      this.removeHttpRequestHeader("Authorization");
      this.updateLogin();
      return;
    }
    
    // If the login button is not already checked, display the login dialog
    var passwordObject       = new Object();
    passwordObject.login     = "";
    passwordObject.password  = "";
    passwordObject.returnVal = false;
    window.openDialog("chrome://restclient/content/login.xul", "login", "chrome,modal,dialog,resizable,centerscreen", passwordObject);
    
    // If the user pressed cancel on the dialog, return
    if (!passwordObject.returnVal) {
      return;
    }
    
    
    // Basic authentication
    if(passwordObject.securityScheme == 0) {
      util.mlog("login[" + passwordObject.login + "] pass[" + passwordObject.password + "]");
      var auth = "Basic " + Base64.encode(passwordObject.login + ':' + passwordObject.password);
      util.mlog(auth);
      this.setHttpRequestHeader("Authorization", auth);
      
    // oAuth authentication
		} else {
      this.oAuthPasswordObject = passwordObject;
      this.setOAuthAuthorizationHeader(passwordObject);
    }
    
		this.updateLogin();
	},
  
  updateOAuthAuthorizationHeader: function() {
    // If the Authorization header and the oAuth credentials are already set,
    // update the Authorization header.
    if( this.isAuthorizationHeaderSet() && this.oAuthPasswordObject != null ) {
      this.setOAuthAuthorizationHeader(this.oAuthPasswordObject);
    }
  },
  
  setOAuthAuthorizationHeader: function(passwordObject) {
    var parameters = { oauth_signature_method: passwordObject.signatureMethod };
    
    var nullIfEmpty = function(str) { return (str == '') ? null : str; };
    var accessor = { consumerKey:    nullIfEmpty(passwordObject.consumerKey),
                     consumerSecret: nullIfEmpty(passwordObject.consumerSecret),
                     accessorSecret: nullIfEmpty(passwordObject.accessorSecret),
                     token:          nullIfEmpty(passwordObject.token),
                     tokenSecret:    nullIfEmpty(passwordObject.tokenSecret)
    };
    
    
    var action = $('tbRequestUrl').value;
    var message = { action: action, parameters: parameters };
    message.method = $('requestMethod').value;
    
    parameters.oauth_version   = passwordObject.autoVersion   ? null : passwordObject.version;
    parameters.oauth_nonce     = passwordObject.autoNonce     ? null : passwordObject.nonce;
    parameters.oauth_timestamp = passwordObject.autoTimestamp ? null : passwordObject.timestamp;
    OAuth.completeRequest(message, accessor);
    
    // The realm is just the URL with the parameters and anchor removed
    var realm = action;
    if(realm.indexOf('?') >= 0) {
      realm = realm.substring(0, realm.indexOf('?'));
    }
    if(realm.indexOf('#') >= 0) {<commit_msg>if the content type is application/x-www-formurlencoded, decode it and send it to oauth when we sign the request, fixes bug #18<commit_after>const HTMLNS = "http://www.w3.org/1999/xhtml";

function $(elementId) {
  return document.getElementById(elementId);
}

var restclient = {
	passwordObject: null,
	_stringBundle: null,

	checkEnterKeyPress: function(evt){
		switch( evt.keyCode ){
			case 13:
				restclient.doRequest();
				break;
			default:
				break;
		}
    
    return true;
	},

	checkDeleteKeyPress: function(evt){
		switch( evt.keyCode ){
			case 46:
				restclient.deleteSelectedHeader();
				break;
			default:
				break;
		}
	},

	deleteSelectedHeader: function(){
	  var headerList = $('headerList');
    var reqHeaderChilds = $('reqHeaderChilds');
    if (headerList.view.selection.count > 0 && headerList.editingRow < 0) {
      for (var i=reqHeaderChilds.childNodes.length-1 ; i>=0 ; i--) {
         if (headerList.view.selection.isSelected(i))
            reqHeaderChilds.removeChild(reqHeaderChilds.childNodes[i]);
      }
    }
	},

	clearRequestHeader: function(){
        var reqHeaderChilds = $('reqHeaderChilds');
        for (var i=reqHeaderChilds.childNodes.length-1 ; i>=0 ; i--) {
            reqHeaderChilds.removeChild(reqHeaderChilds.childNodes[i]);
        }
	},

  init: function(){
    var controller = new TabController();
    controller.tbRequestUrl = "dupa tam";

    this._stringBundle = $("string-bundle");
	  this.updateSaveButton();
    var that = this;
    var requestMethodDropdown = $('requestMethod');
    var requestUrlDropdown = $('tbRequestUrl');
    requestMethodDropdown.addEventListener('command', function() { that.updateOAuthAuthorizationHeader(); }, false);
	},

	updateLogin: function(){
	  var login = $("login-icon");
		if( ! this.isAuthorizationHeaderSet() ){
			login.label = this._stringBundle.getString("restclient.login");
			login.checked = false;
		}else{
			login.label = this._stringBundle.getString("restclient.logout");
			login.checked = true;
		}
	},
  isContentTypeFormUrlEncoded: function()  {
    var reqHeaderChilds = $('reqHeaderChilds');
    for (var i=reqHeaderChilds.childNodes.length-1 ; i>=0 ; i--) {
      if(reqHeaderChilds.childNodes[i].childNodes[0].childNodes[0].getAttribute('label').toLowerCase() == "content-type"
        && 
        reqHeaderChilds.childNodes[i].childNodes[0].childNodes[1].getAttribute('label').toLowerCase() == "application/x-www-form-urlencoded"
      ) {
        return true;
      }
    }
    
    return false;
  }
  ,
  isAuthorizationHeaderSet: function() {
	  var reqHeaderChilds = $('reqHeaderChilds');
    for (var i=reqHeaderChilds.childNodes.length-1 ; i>=0 ; i--) {
      if(reqHeaderChilds.childNodes[i].childNodes[0].childNodes[0].getAttribute('label') == "Authorization") {
        return true;
      }
    }
    
    return false;
  },

	doLogin: function(){
		var login = $("login-icon");
    
    // If the login button is already checked, clear the Authorization header and uncheck the button
		if(login.checked) {
      this.oAuthPasswordObject = null;
      this.removeHttpRequestHeader("Authorization");
      this.updateLogin();
      return;
    }
    
    // If the login button is not already checked, display the login dialog
    var passwordObject       = new Object();
    passwordObject.login     = "";
    passwordObject.password  = "";
    passwordObject.returnVal = false;
    window.openDialog("chrome://restclient/content/login.xul", "login", "chrome,modal,dialog,resizable,centerscreen", passwordObject);
    
    // If the user pressed cancel on the dialog, return
    if (!passwordObject.returnVal) {
      return;
    }
    
    
    // Basic authentication
    if(passwordObject.securityScheme == 0) {
      util.mlog("login[" + passwordObject.login + "] pass[" + passwordObject.password + "]");
      var auth = "Basic " + Base64.encode(passwordObject.login + ':' + passwordObject.password);
      util.mlog(auth);
      this.setHttpRequestHeader("Authorization", auth);
      
    // oAuth authentication
		} else {
      this.oAuthPasswordObject = passwordObject;
      this.setOAuthAuthorizationHeader(passwordObject);
    }
    
		this.updateLogin();
	},
  
  updateOAuthAuthorizationHeader: function() {
    // If the Authorization header and the oAuth credentials are already set,
    // update the Authorization header.
    if( this.isAuthorizationHeaderSet() && this.oAuthPasswordObject != null ) {
      this.setOAuthAuthorizationHeader(this.oAuthPasswordObject);
    }
  },
  
  setOAuthAuthorizationHeader: function(passwordObject) {
    var parameters = { oauth_signature_method: passwordObject.signatureMethod };
    
    var nullIfEmpty = function(str) { return (str == '') ? null : str; };
    var accessor = { consumerKey:    nullIfEmpty(passwordObject.consumerKey),
                     consumerSecret: nullIfEmpty(passwordObject.consumerSecret),
                     accessorSecret: nullIfEmpty(passwordObject.accessorSecret),
                     token:          nullIfEmpty(passwordObject.token),
                     tokenSecret:    nullIfEmpty(passwordObject.tokenSecret)
    };
    
    
    var action = $('tbRequestUrl').value;
    var message = { action: action, parameters: parameters };
    message.method = $('requestMethod').value;
    
    parameters.oauth_version   = passwordObject.autoVersion   ? null : passwordObject.version;
    parameters.oauth_nonce     = passwordObject.autoNonce     ? null : passwordObject.nonce;
    parameters.oauth_timestamp = passwordObject.autoTimestamp ? null : passwordObject.timestamp;
    
    // if the content-type is form-encoded, we need to add it to the parameters
    if (message.method == "POST" && this.isContentTypeFormUrlEncoded()) {
      var formParams = OAuth.decodeForm($('tbRequestBody').value);
      for(var i in formParams)
      {
        if(!formParams.hasOwnProperty(i))
        {
          return;
        }
        
        var name = formParams[i][0];
        var value = formParams[i][1];
        
        parameters[name] = value;
      }
    }
    
    OAuth.completeRequest(message, accessor);
    
    // The realm is just the URL with the parameters and anchor removed
    var realm = action;
    if(realm.indexOf('?') >= 0) {
      realm = realm.substring(0, realm.indexOf('?'));
    }
    if(realm.indexOf('#') >= 0) {
{'fraction_non_alphanumeric': 0.12493551160791058, 'fraction_numerical': 0.005159071367153913, 'mean_word_length': 3.1011988716502117, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6234701', 'n_tokens_mistral': 3488, 'n_tokens_neox': 3361, 'n_words': 678}