Permalink
Browse files

Updated response headers style

  • Loading branch information...
1 parent 1c99691 commit 49ccba11a2f740dfce51fa851b6a1443633d6b25 @chao committed Mar 26, 2012
@@ -202,4 +202,13 @@ body {
}
#window-manage-request button {
margin-left: 5px;
+}
+span.header-name {
+ color: teal;
+}
+span.header-split{
+ color: #93a1a1;
+}
+span.header-value {
+ color: #D14;
}
@@ -50,8 +50,8 @@ restclient.http = {
restclient.http.xhr = xhr;
xhr.send(requestBody);
} catch (e) {
- restclient.main.setResponseHeader("Error: Could not connect to server\n"
- + "Error:" + e.getMessage(), false);
+ restclient.main.setResponseHeader([["Error", "Could not connect to server"],
+ ["Error", e.getMessage()]], false);
}
},
onprogress: function(evt) {
@@ -62,21 +62,28 @@ restclient.http = {
restclient.main.updateProgressBar(-1, 'Sending data...');
},
onerror: function(xhr) {
- //console.log(xhr);
restclient.main.clearResult();
restclient.main.updateProgressBar(-1);
- restclient.main.setResponseHeader("Error: Could not connect to server", false);
+ restclient.main.setResponseHeader([["Error", "Could not connect to server"]], false);
},
onload: function(xhr) {
- //console.log(xhr);
restclient.main.clearResult();
xhr = xhr.target;
- var responseHeader = "";
- responseHeader += "Status Code: " + xhr.status + " " + xhr.statusText + "\n";
+ var headers = [];
+ headers.push(["Status Code", xhr.status + " " + xhr.statusText]);
+
+ var headersText = xhr.getAllResponseHeaders();
+ var responseHeaders = headersText.split("\n");
- responseHeader += xhr.getAllResponseHeaders();
- //console.log(responseHeader);
- restclient.main.setResponseHeader(responseHeader);
+ for (var i = 0, header; header = responseHeaders[i]; i++) {
+ if(header.indexOf(":") > 0) {
+ var key = header.substring(0, header.indexOf(":"));
+ var headValue = header.substr(header.indexOf(":") + 2);
+ headValue = headValue.replace(/\s$/, "");
+ headers.push([key, headValue]);
+ }
+ }
+ restclient.main.setResponseHeader(headers);
var contentType = xhr.getResponseHeader("Content-Type");
View
@@ -37,80 +37,26 @@ if (typeof (restclient) === "undefined") {
importModules : function () {
Components.utils.import("resource://restclient/modules/StringBundle.js", restclient);
Components.utils.import("resource://restclient/modules/Preferences.js", restclient);
- Components.utils.import("resource://restclient/modules/Observers.js", restclient);
- Components.utils.import("resource://restclient/modules/log4moz.js", restclient);
Components.utils.import("resource://gre/modules/NetUtil.jsm", restclient);
Components.utils.import("resource://gre/modules/FileUtils.jsm", restclient);
},
- setupLogging: function () {
- var debugLevel = restclient.getPref("extensions.restclient.log.level", 'Warn'),
- logfile = restclient.getPref("extensions.restclient.log.file", ""),
- formatter = new restclient.Log4Moz.BasicFormatter(),
- root = restclient.Log4Moz.repository.rootLogger,
- capp = new restclient.Log4Moz.ConsoleAppender(formatter),
- dapp = new restclient.Log4Moz.DumpAppender(formatter);
- if (root.appenders.length > 0) {
- return;
- }
- root.level = restclient.Log4Moz.Level[debugLevel];
-
- capp.level = restclient.Log4Moz.Level[debugLevel];
- root.addAppender(capp);
-
- dapp.level = restclient.Log4Moz.Level[debugLevel];
- root.addAppender(dapp);
-
- if (logfile !== "") {
- var logFile = restclient.getLocalDirectory();
- logFile.append("log.txt");
- var appender = new restclient.Log4Moz.RotatingFileAppender(logFile, formatter);
- appender.level = restclient.Log4Moz.Level[debugLevel];
- root.addAppender(appender);
- }
- restclient.logger = true;
- },
- getLogger : function (name) {
- if (!restclient.logger) {
- restclient.setupLogging();
- }
- return restclient.Log4Moz.repository.getLogger(name);
- },
setPref : function (name, value) {
return restclient.Preferences.set("extension.restclient." + name, value);
},
getPref : function (name, value) {
return restclient.Preferences.get("extension.restclient." + name, value);
},
- getLocalDirectory : function () {
- var directoryService =
- Components.classes["@mozilla.org/file/directory_service;1"].
- getService(Components.interfaces.nsIProperties),
- localDir = directoryService.get("ProfD", Components.interfaces.nsIFile);
-
- localDir.append("restclient");
-
- if (!localDir.exists() || !localDir.isDirectory()) {
- // read and write permissions to owner and group, read-only for others.
- localDir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 484);
- }
- return localDir;
- },
- getRecentWindow : function () {
- var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
- .getService(Components.interfaces.nsIWindowMediator);
- return wm.getMostRecentWindow("navigator:browser");
- },
+
i18n : function (n, arg) {
- var logger = restclient.getLogger('restclient.i18n'),
- i18nStrings = new restclient.StringBundle("chrome://restclient/locale/s.properties");
+ i18nStrings = new restclient.StringBundle("chrome://restclient/locale/restclient.properties");
try {
return i18nStrings.get(n, arg);
} catch (e) {
- logger.fatal(n);
- logger.fatal(JSON.stringify(arg));
- logger.fatal(i18nStrings.get(n));
- logger.fatal(e.message);
+ console.error(n);
+ console.error(JSON.stringify(arg));
+ console.error(i18nStrings.get(n));
+ console.error(e.message);
}
}
}
@@ -46,7 +46,7 @@ restclient.main = {
this.initModal();
this.updateFavoriteHeadersMenu();
- this.updateFavoriteRequest();
+ this.updateFavoriteRequestMenu();
$('#request-button').bind('click',function(){
var request = restclient.main.getRequest();
@@ -205,6 +205,12 @@ restclient.main = {
}
return false;
},
+ removeHttpRequestHeaders: function(){
+ $('#request-headers span.label').remove();
+ if( $('#request-headers span.label').length == 0 ) {
+ $('#request-headers').hide();
+ }
+ },
editHttpRequestHeader: function() {
},
@@ -306,19 +312,40 @@ restclient.main = {
request.overrideMimeType = ($('#overrideMimeType').attr('checked') == 'checked') ? $('#overrideMimeType').val() : false;
var headers = [];
$('#request-headers .label').each(function(){
- headers.push($(this).attr('header-name'), $(this).attr('header-value'));
+ headers.push([$(this).attr('header-name'), $(this).attr('header-value')]);
});
request.headers = headers;
return request;
},
setResponseHeader: function(headers, line) {
- $('#response-headers pre').text(headers);
+ console.log(headers);
+ if(!headers) {
+ $('#response-headers pre').text('');
+ return false;
+ }
if(typeof line === 'boolean' && line == false) {
- $('#response-headers pre').removeClass('linenums');
+ var text = "";
+ for(var i=0, header; header = headers[i]; i++) {
+ text += header.join(" ") + "\n";
+ }
+ $('#response-headers pre').text(text);
}
else
{
- $('#response-headers pre').addClass('linenums');
+ var ol = $('<ol class="linenums"></ol>');
+ for(var i=0, header; header = headers[i]; i++) {
+ ol.append($('<li></li>').append(
+ $('<span class="header-name"></span>').text(header[0])
+ )
+ .append(
+ $('<span class="header-split"></span>').text(': ')
+ )
+ .append(
+ $('<span class="header-value"></span>').text(header[1])
+ )
+ );
+ }
+ $('#response-headers pre').empty().append(ol);
}
},
updateProgressBar: function(idx, status) {
@@ -357,7 +384,7 @@ restclient.main = {
$("#response-body-preview div.pre").html('');
$('#response-body-raw pre').text('');
$('#response-body-highlight pre').text('');
- restclient.main.setResponseHeader('');
+ restclient.main.setResponseHeader();
$('[href="#response-headers"]').click();
},
checkMimeType: function(){
@@ -447,9 +474,9 @@ restclient.main = {
var savedRequest = restclient.getPref('savedRequest', '');
if(savedRequest != '')
{
- console.log(savedRequest);
+ //console.log(savedRequest);
savedRequest = JSON.parse(savedRequest);
- console.log(typeof savedRequest[name.val()]);
+ //console.log(typeof savedRequest[name.val()]);
if(typeof $('#modal-save-request .btnOkay').attr('request-name') == 'undefined' &&
typeof savedRequest[name.val()] != 'undefined') {
name.next().text('Name existed, you can either change a name or overwrite it.').show();
@@ -465,8 +492,10 @@ restclient.main = {
savedRequest[name.val()] = request;
restclient.setPref('savedRequest', JSON.stringify(savedRequest));
$('#modal-save-request').modal('hide');
+ this.updateFavoriteRequestMenu();
+ $('.request-menu').click();
},
- updateFavoriteRequest: function() {
+ updateFavoriteRequestMenu: function() {
$('ul.savedRequest .favorite').remove();
var savedRequest = restclient.getPref('savedRequest', '');
if(savedRequest == '')
@@ -481,7 +510,8 @@ restclient.main = {
name = name.substr(0, restclient.main.requestMenuMaxLength -3) + "...";
var a = $('<a class="favorite" href="#"></a>').text(name)
- .data('request', savedRequest[name]);
+ .data('request', savedRequest[name])
+ .data('request-name', name);
$('.savedRequest').prepend($('<li></li>').append(a));
}
if( $('.savedRequest a.favorite').length > 0 )
@@ -490,7 +520,7 @@ restclient.main = {
$('li.manage-request').hide();
$('.savedRequest a.favorite').bind('click', function(evt) {
- //restclient.main.addHttpRequestHeader($(this).attr('header-name'), $(this).attr('header-value'));
+ restclient.main.applyFavoriteRequest($(this).data('request-name'));
evt.preventDefault();
})
$('.savedRequest .favorite:last').after($('<li class="divider favorite"></li>'));
@@ -533,7 +563,7 @@ restclient.main = {
}catch(e){ alert('Cannot import the json file.'); }
});
}
- restclient.main.updateFavoriteRequest();
+ restclient.main.updateFavoriteRequestMenu();
alert('import requests succeed');
return false;
},
@@ -609,7 +639,7 @@ restclient.main = {
var result = restclient.main.removeFavoriteRequest(name);
if(result) {
$(this).parents('div.accordion-group').hide().remove();
- restclient.main.updateFavoriteRequest();
+ restclient.main.updateFavoriteRequestMenu();
}
}));
@@ -638,8 +668,39 @@ restclient.main = {
return false;
favorites = JSON.parse(favorites);
if(name in favorites) {
- request = favorites[name];
+ var request = favorites[name];
+ if(request.method) {
+ $('#request-method option[value="' + request.method + '"]').attr('selected', true);
+ }
+ else
+ $('#request-method option[value="GET"]').attr('selected', true);
+
+ if(request.url) {
+ $('#request-url').val(request.url);
+ }
+ else
+ $('#request-url').val('');
+
+ if(request.overrideMimeType) {
+ $('#overrideMimeType').attr('checked', true);
+ $('.overrideMimeType').show();
+ }
+ else
+ $('#overrideMimeType').removeAttr('checked');
+ if(request.body) {
+ $('#request-body').val(request.body);
+ }
+ else
+ $('#request-body').val('');
+
+ if(request.headers) {
+ restclient.main.removeHttpRequestHeaders();
+ //console.log(request.headers);
+ for(var i = 0, header; header = request.headers[i]; i++) {
+ restclient.main.addHttpRequestHeader(header[0], header[1]);
+ }
+ }
return true;
}
return false;
View
@@ -49,7 +49,7 @@
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">View <b class="caret"></b></a>
<ul class="dropdown-menu">
- <li><a href="#">Request</a></li>
+ <li><a href="#" class="request-menu">Request</a></li>
<li><a href="#response" onclick="restclient.main.showResponse();">Response</a></li>
</ul>
</li>
@@ -78,11 +78,11 @@
<form class="form-inline" onsubmit="return false;">
<label>Method</label>
<select id="request-method" style="width:130px;">
- <option>GET</option>
- <option>POST</option>
- <option>PUT</option>
- <option>DELETE</option>
- <option>SEND</option>
+ <option value="GET">GET</option>
+ <option value="POST">POST</option>
+ <option value="PUT">PUT</option>
+ <option value="DELETE">DELETE</option>
+ <option value="SEND">SEND</option>
</select>
<input id="request-url" type="URL" class="span9" style="margin: 0 auto;" placeholder="http://www.example.com" data-provide="typeahead" data-items="4" data-source="">
<button id="request-button" type="button" class="btn btn-inverse">SEND</button>
@@ -133,7 +133,7 @@ <h4 class="alert-heading">You've overrided MIME type</h4>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="response-headers">
- <pre class="prettyprint linenums"></pre>
+ <pre></pre>
</div>
<div class="tab-pane" id="response-body-raw">
<pre class="span12" style="margin-left:0px"></pre>
@@ -189,7 +189,7 @@ <h4 class="alert-heading">You've overrided MIME type</h4>
<h3>oAuth</h3>
</div>
<div class="modal-body">
- <form class="form-horizontal">
+ <form class="form-horizontal" onsubmit="return false;">
<fieldset>
<legend>Signing Requests</legend>
<div class="control-group">
@@ -309,7 +309,7 @@ <h4 class="alert-heading">You've overrided MIME type</h4>
<h3 class="title">Save Current Request</h3>
</div>
<div class="modal-body">
- <form class="offset1">
+ <form class="offset1" onsubmit="return false;">
<div class="control-group">
<label for="saved-request-name">Request Name</label>
<div class="controls">
Oops, something went wrong.

0 comments on commit 49ccba1

Please sign in to comment.