[Phoenix-commits] rev 17736 -
public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content
andi at wyona.com
andi at wyona.com
Fri Sep 8 20:46:44 CEST 2006
Author: andi
Date: 2006-09-08 20:46:43 +0200 (Fri, 08 Sep 2006)
New Revision: 17736
Modified:
public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/controller.js
public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/networkservice.js
public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/neutronarchive.js
public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/widget.js
Log:
Authentication rewrite part 3. (Note: breakage should be signifiantly reduced now.)
Modified: public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/controller.js
===================================================================
--- public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/controller.js 2006-09-08 18:45:46 UTC (rev 17735)
+++ public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/controller.js 2006-09-08 18:46:43 UTC (rev 17736)
@@ -156,7 +156,7 @@
gEditorController.constructor.startEditorWithDocument(gEditorController.document, null, null);
} else {
- /* DEBUG */ dump("Ulysses:controller.js:UlyssesEditController.archiveLoadFinished: NetworkService.httpFetchToFile(\"" + gEditorController.archive.loadURI.spec + "\") failed.\n");
+ /* DEBUG */ dump("Ulysses:controller.js:UlyssesEditController.archiveLoadFinished: failed to load Neutron archive \"" + gEditorController.archive.loadURI.spec + "\". \"" + aException + "\"\n");
if (aException && (aException instanceof NeutronProtocolException || aException instanceof NeutronAuthException)) {
// report error message retrieved from response
Modified: public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/networkservice.js
===================================================================
--- public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/networkservice.js 2006-09-08 18:45:46 UTC (rev 17735)
+++ public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/networkservice.js 2006-09-08 18:46:43 UTC (rev 17736)
@@ -36,26 +36,35 @@
/**
* Perform a file download over HTTP.
*
- * @param {String} aURI the URI of the file to download
- * @param {nsIFile} aFile the file where the downloaded data gets written
- * @param {Function} aCallbackFunction the function to call when the download has finished
+ * @param {String} aURI the URI of the file to download
+ * @param {nsIFile} aFile the file where the downloaded data gets written
+ * @param {Function} aCallbackFunction the function to call when the load has finished of type function(String aDocumentData, Number aResponseStatusCode, Object aContext, Array aResponseHeaders, Error aException)
+ * @param {Object} aContext a context, or null if unused by the caller
+ * @param {Boolean} aHandleAuthentication set to true if authenciation should be handled automatically upon a 401 response, or if the response should simply be passed back to the caller as other responses
* @return {Undefined} does not have a return value
*/
- httpFetchToFile: function(aURI, aFile, aCallbackFunction, aContext) {
+ httpFetchToFile: function(aURI, aFile, aCallbackFunction, aContext, aHandleAuthentication) {
var downloader = null;
var downloadObserver = null;
var channel = null;
var ioService = null;
+ var request = null;
/* DEBUG */ dump("Ulysses:networkservice.js:NetworkService.httpFetchToFile(\"" + aURI + "\", \"" + aFile + "\", \"" + /*aCallbackFunction +*/ "\", \"" + /*aContext +*/ "\") invoked\n");
- /* DEBUG */ UlyssesDebug.ASSERT(aURI != null);
- /* DEBUG */ UlyssesDebug.ASSERT(aFile != null);
- /* DEBUG */ UlyssesDebug.ASSERT(aCallbackFunction != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(aURI != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(aFile != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(aCallbackFunction != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(typeof(aCallbackFunction) == "function");
+ /* DEBUG */ UlyssesDebug.ASSERT(aHandleAuthentication != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(typeof(aHandleAuthentication) == "boolean");
ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
channel = ioService.newChannel(aURI, "", null);
+
+ request = new HTTPRequestFetchToFile(aURI, aFile, aCallbackFunction, aContext, aHandleAuthentication);
+
try {
channel.QueryInterface(Components.interfaces.nsIHttpChannel);
channel.requestMethod = "GET";
@@ -65,7 +74,7 @@
// if we're loading a local file, we don't have a nsIHttpChannel
}
- downloadObserver = new DownloadObserver(aCallbackFunction, channel, aContext);
+ downloadObserver = new DownloadObserver(request, channel);
downloader = Components.classes["@mozilla.org/network/downloader;1"].createInstance();
downloader.QueryInterface(Components.interfaces.nsIDownloader);
@@ -385,6 +394,11 @@
return;
}
+ if (aRequest instanceof HTTPRequestFetchToFile) {
+ NetworkService.httpFetchToFile(aRequest.uri, aRequest.file, aRequest.requestFinishedCallback, aRequest.context, aRequest.handleAuthentication);
+ return;
+ }
+
throw new UlyssesException("Ulysses:networkservice.js:NetworkService.performHTTPRequest: unknown request \"" + aRequest + "\".");
},
@@ -440,27 +454,42 @@
* Implements the nsIDownloadObserver interface.
*
* @constructor
- * @param {Function} aDownloadFinishedCallback the function to call when the download finished
- * @param {Object} aContext a context or null if unused
+ * @param {HTTPRequestFetchToFile} aRequest the request with which this download observer is associated
+ * @param {nsIChannel} aChannel the channel which originates the request
* @return {DownloadObserver}
*/
-function DownloadObserver(aDownloadFinishedCallback, aChannel, aContext) {
+function DownloadObserver(aRequest, aChannel) {
- /* DEBUG */ dump("Ulysses:networkservice.js:DownloadObserver(\"" + aDownloadFinishedCallback + "\", \"" + aContext + "\") invoked\n");
+ /* DEBUG */ dump("Ulysses:networkservice.js:DownloadObserver(\"" + aRequest + "\", \"" + aChannel + "\") invoked\n");
- this.downloadFinishedCallback = aDownloadFinishedCallback;
- this.context = aContext;
- this.channel = aChannel;
+ /* DEBUG */ UlyssesDebug.ASSERT(aRequest != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(aRequest instanceof HTTPRequestFetchToFile);
+ /* DEBUG */ UlyssesDebug.ASSERT(aChannel != null);
+
+ this.request = aRequest;
+ this.channel = aChannel;
}
DownloadObserver.prototype = {
- downloadFinishedCallback: null,
- context : null,
+ request: null,
+ channel: null,
- onDownloadComplete: function(aDownloader, aRequest, aContext, aStatus, aResult) {
+ /**
+ * Download has completed.
+ *
+ * @param {nsIDownloader} aDownloader
+ * @param {nsIRequest} aRequest
+ * @param {nsISupports} aContext
+ * @param {nsresult} aStatusCode
+ * @param {nsIFile} aResult
+ * @return {Undefined} does not have a return value
+ */
+ onDownloadComplete: function (aDownloader, aRequest, aContext, aStatusCode, aResult) {
var responseStatusCode = null;
+ var fileURI = null;
+ var xmlDoc = null;
- /* DEBUG */ dump("Ulysses:networkservice.js:DownloadObserver.onDownloadComplete(\"" + aDownloader + "\", \"" + aRequest + "\", \"" + aContext + "\", \"" + aStatus + "\", \"" + aResult + "\") invoked\n");
+ /* DEBUG */ dump("Ulysses:networkservice.js:DownloadObserver.onDownloadComplete(\"" + aDownloader + "\", \"" + aRequest + "\", \"" + aContext + "\", \"" + aStatusCode + "\", \"" + aResult + "\") invoked\n");
try {
this.channel.QueryInterface(Components.interfaces.nsIHttpChannel);
@@ -470,7 +499,55 @@
responseStatusCode = 200;
}
- this.downloadFinishedCallback(aResult, responseStatusCode, this.context);
+ /* DEBUG */ dump("Ulysses:networkservice.js:DownloadObserver.onDownloadComplete: status code = \"" + responseStatusCode + "\"\n");
+
+ // retrieve HTTP response headers if the response status is 401 and we do authentication
+ if (responseStatusCode == 401 && this.request.handleAuthentication) {
+ try {
+ this.channel.QueryInterface(Components.interfaces.nsIHttpChannel);
+
+ responseHeaderVisitor = new HTTPResponseHeaderVisitor();
+ this.channel.visitResponseHeaders(responseHeaderVisitor);
+
+ responseHeaders = responseHeaderVisitor.headers;
+ } catch (exception) {
+ // not a HTTP channel or not ready
+ responseHeaders = null;
+ }
+ }
+
+ // check if request was successful
+ if (Components.isSuccessCode(aStatusCode)) {
+ // check if we should do authentication
+ if (responseStatusCode == 401 && this.request.handleAuthentication) {
+ /* DEBUG */ dump("Ulysses:networkservice.js:DownloadObserver.onDownloadComplete: we have to authenticate\n");
+
+ // get an nsIURI object for the response file
+ fileURI = Components.classes["@mozilla.org/network/io-service;1"]. getService(Components.interfaces.nsIIOService).newFileURI(aResult);
+
+ xmlDoc = new XMLDocument(fileURI);
+ xmlDoc.loadDocument();
+
+ try {
+ NetworkService.authenticate(this.request, responseHeaders, xmlDoc.documentData);
+ } catch (exception) {
+ /* DEBUG */ UlyssesDebug.dumpExceptionToConsole("Ulysses:networkservice.js:DownloadObserver.onDownloadComplete", exception);
+ /* We should authenticate but the server did not tell us how, the
+ * headers were not accessible, or the specified authentication
+ * scheme is unknown. Bail out. */
+ this.request.requestFinishedCallback(null, responseStatusCode, this.request.context, exception);
+ }
+ } else {
+ /* Everything went fine (even if we received 401, but the caller did not order
+ * us to authenticate, therefore he is expecting a potential authentication failure). */
+ this.request.requestFinishedCallback(aResult, responseStatusCode, this.request.context, null);
+ }
+ } else {
+ // request failed
+ /* DEBUG */ dump("Ulysses:networkservice.js:DownloadObserver.onDownloadComplete: load failed\n");
+
+ this.request.requestFinishedCallback(null, responseStatusCode, this.request.context, new UlyssesException("Ulysses:networkservice.js:DownloadObserver.onDownloadComplete: request failed, return code is \"" + aStatusCode + "\""));
+ }
}
};
@@ -703,6 +780,7 @@
}
};
+
/**
* The base class for all HTTP requests.
*
@@ -712,89 +790,126 @@
*
* @constructor
* @param {String} aURI the target URI of the GET request
- * @param {Array} aHeaderArray a two-dimensional array, the first dimension containing the header the arrays, the second dimension containing the header name as a string in field 0, and the header value as a string in field 1
* @param {Function} aRequestFinishedCallback the function to call when the load has finished of type function(String aDocumentData, Number aResponseStatusCode, Object aContext, Array aResponseHeaders, Error aException)
* @param {Object} aContext a context, or null if unused by the caller
- * @param {Boolean} aRetrieveResponseHeaders set to true if the response headers should be passed to the callback, false otherwise
* @param {Boolean} aHandleAuthentication set to true if authenciation should be handled automatically upon a 401 response, or if the response should simply be passed back to the caller as other responses
* @return {HTTPRequest}
*/
-function HTTPRequest(aURI, aHeaderArray, aRequestFinishedCallback, aContext, aRetrieveResponseHeaders, aHandleAuthentication) {
+function HTTPRequest(aURI, aRequestFinishedCallback, aContext, aHandleAuthentication) {
/* DEBUG */ UlyssesDebug.ASSERT(aURI != null);
/* DEBUG */ UlyssesDebug.ASSERT(aRequestFinishedCallback != null);
/* DEBUG */ UlyssesDebug.ASSERT(typeof(aRequestFinishedCallback) == "function");
- /* DEBUG */ UlyssesDebug.ASSERT(aRetrieveResponseHeaders != null);
- /* DEBUG */ UlyssesDebug.ASSERT(typeof(aRetrieveResponseHeaders) == "boolean");
/* DEBUG */ UlyssesDebug.ASSERT(aHandleAuthentication != null);
/* DEBUG */ UlyssesDebug.ASSERT(typeof(aHandleAuthentication) == "boolean");
this.uri = aURI;
- this.headerArray = aHeaderArray;
this.requestFinishedCallback = aRequestFinishedCallback;
this.context = aContext;
- this.retrieveResponseHeaders = aRetrieveResponseHeaders;
this.handleAuthentication = aHandleAuthentication;
}
HTTPRequest.prototype = {
uri : null,
- headerArray : null,
requestFinishedCallback: null,
context : null,
- retrieveResponseHeaders: false,
handleAuthentication : false
};
+function HTTPRequestFetchToFile(aURI, aFile, aRequestFinishedCallback, aContext, aHandleAuthentication) {
+ /* DEBUG */ UlyssesDebug.ASSERT(aFile != null);
+
+ HTTPRequest.call(this, aURI, aRequestFinishedCallback, aContext, aHandleAuthentication);
+
+ this.file = aFile;
+}
+
+HTTPRequestFetchToFile.prototype = {
+ __proto__: HTTPRequest.prototype,
+
+ file: null
+};
+
+
function HTTPRequestGET(aURI, aHeaderArray, aRequestFinishedCallback, aContext, aRetrieveResponseHeaders, aHandleAuthentication) {
- HTTPRequest.call(this, aURI, aHeaderArray, aRequestFinishedCallback, aContext, aRetrieveResponseHeaders, aHandleAuthentication);
+ /* DEBUG */ UlyssesDebug.ASSERT(aRetrieveResponseHeaders != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(typeof(aRetrieveResponseHeaders) == "boolean");
+
+ HTTPRequest.call(this, aURI, aRequestFinishedCallback, aContext, aHandleAuthentication);
+
+ this.headerArray = aHeaderArray;
+ this.retrieveResponseHeaders = aRetrieveResponseHeaders;
}
HTTPRequestGET.prototype = {
- __proto__: HTTPRequest.prototype
+ __proto__: HTTPRequest.prototype,
+
+ headerArray : null,
+ retrieveResponseHeaders: false
};
function HTTPRequestPUT(aURI, aHeaderArray, aDocumentData, aContentType, aRequestFinishedCallback, aContext, aRetrieveResponseHeaders, aHandleAuthentication) {
- /* DEBUG */ UlyssesDebug.ASSERT(aDocumentData != null);
- /* DEBUG */ UlyssesDebug.ASSERT(aContentType != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(aDocumentData != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(aContentType != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(aRetrieveResponseHeaders != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(typeof(aRetrieveResponseHeaders) == "boolean");
- HTTPRequest.call(this, aURI, aHeaderArray, aRequestFinishedCallback, aContext, aRetrieveResponseHeaders, aHandleAuthentication);
+ HTTPRequest.call(this, aURI, aRequestFinishedCallback, aContext, aHandleAuthentication);
- this.documentData = aDocumentData;
- this.contentType = aContentType;
+ this.headerArray = aHeaderArray;
+ this.documentData = aDocumentData;
+ this.contentType = aContentType;
+ this.retrieveResponseHeaders = aRetrieveResponseHeaders;
}
HTTPRequestPUT.prototype = {
__proto__: HTTPRequest.prototype,
- documentData: null,
- contentType : null
+ headerArray : null,
+ documentData : null,
+ contentType : null,
+ retrieveResponseHeaders: false
};
function HTTPRequestPOST(aURI, aHeaderArray, aDocumentData, aContentType, aRequestFinishedCallback, aContext, aRetrieveResponseHeaders, aHandleAuthentication) {
- /* DEBUG */ UlyssesDebug.ASSERT(aDocumentData != null);
- /* DEBUG */ UlyssesDebug.ASSERT(aContentType != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(aDocumentData != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(aContentType != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(aRetrieveResponseHeaders != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(typeof(aRetrieveResponseHeaders) == "boolean");
- HTTPRequest.call(this, aURI, aHeaderArray, aRequestFinishedCallback, aContext, aRetrieveResponseHeaders, aHandleAuthentication);
+ HTTPRequest.call(this, aURI, aRequestFinishedCallback, aContext, aHandleAuthentication);
- this.documentData = aDocumentData;
- this.contentType = aContentType;
+ this.headerArray = aHeaderArray;
+ this.documentData = aDocumentData;
+ this.contentType = aContentType;
+ this.retrieveResponseHeaders = aRetrieveResponseHeaders;
}
HTTPRequestPOST.prototype = {
__proto__: HTTPRequest.prototype,
- documentData: null,
- contentType : null
+ headerArray : null,
+ documentData : null,
+ contentType : null,
+ retrieveResponseHeaders: false
};
function HTTPRequestDELETE(aURI, aHeaderArray, aRequestFinishedCallback, aContext, aRetrieveResponseHeaders, aHandleAuthentication) {
- HTTPRequest.call(this, aURI, aHeaderArray, aRequestFinishedCallback, aContext, aRetrieveResponseHeaders, aHandleAuthentication);
+ /* DEBUG */ UlyssesDebug.ASSERT(aRetrieveResponseHeaders != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(typeof(aRetrieveResponseHeaders) == "boolean");
+
+ HTTPRequest.call(this, aURI, aRequestFinishedCallback, aContext, aHandleAuthentication);
+
+ this.headerArray = aHeaderArray;
+ this.retrieveResponseHeaders = aRetrieveResponseHeaders;
}
HTTPRequestDELETE.prototype = {
- __proto__: HTTPRequest.prototype
+ __proto__: HTTPRequest.prototype,
+
+ headerArray : null,
+ retrieveResponseHeaders: false
};
Modified: public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/neutronarchive.js
===================================================================
--- public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/neutronarchive.js 2006-09-08 18:45:46 UTC (rev 17735)
+++ public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/neutronarchive.js 2006-09-08 18:46:43 UTC (rev 17736)
@@ -86,37 +86,48 @@
/* DEBUG */ dump("Ulysses:neutronarchive.js:NeutronArchive.loadNeutronArchive(): archive URI: " + this.zipArchiveURI.spec + "\n");
- NetworkService.httpFetchToFile(this.loadURI.spec, this.tmpArchive, this.requestFinishedHandler, aLoadFinishedCallback);
+ NetworkService.httpFetchToFile(this.loadURI.spec, this.tmpArchive, this.__requestFinishedHandler, aLoadFinishedCallback, true);
},
/**
*
- * @param {String} aDocumentData the data returned by the request
- * @param {Long} aResponseStatusCode the status code of the response
- * @param {Function} aRequestFinishedCallback a function with the signature function(String, Exception)
+ * @param {nsIFile} aResultFile the data returned by the request
+ * @param {Long} aResponseStatusCode the status code of the response
+ * @param {Function} aRequestFinishedCallback a function with the signature function(String, Exception)
+ * @param {Array} aResponseHeaders the response headers
+ * @param {Error} aException an exception, or null if everything went well
+ * @return {Undefined} does not have a return value
*/
- requestFinishedHandler: function (aResultFile, aResponseStatusCode, aRequestFinishedCallback) {
+ __requestFinishedHandler: function (aResultFile, aResponseStatusCode, aRequestFinishedCallback, aResponseHeaders, aException) {
var fileURI = null;
var xmlDoc = null;
- /* DEBUG */ dump("Ulysses:neutronarchive.js:NeutronArchive.requestFinishedHandler(\"" + aResultFile + "\", \"" + aResponseStatusCode + "\", \"" + aRequestFinishedCallback + "\") invoked\n");
+ /* DEBUG */ dump("Ulysses:neutronarchive.js:NeutronArchive.__requestFinishedHandler(\"" + aResultFile + "\", \"" + aResponseStatusCode + "\", \"" + aRequestFinishedCallback + "\", \"" + aResponseHeaders + "\", \"" + aException + "\") invoked\n");
+ /* DEBUG */ UlyssesDebug.ASSERT(aResponseStatusCode != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(aRequestFinishedCallback != null);
+ /* DEBUG */ UlyssesDebug.ASSERT(typeof(aRequestFinishedCallback) == "function");
+
if (aResponseStatusCode == 200) {
aRequestFinishedCallback(aResultFile, null);
} else {
- // get an nsIURI object for the response file
- fileURI = Components.classes["@mozilla.org/network/io-service;1"]. getService(Components.interfaces.nsIIOService).newFileURI(aResultFile);
+ if (aException) {
+ aRequestFinishedCallback(null, aException);
+ } else {
+ try {
+ // get an nsIURI object for the response file
+ fileURI = Components.classes["@mozilla.org/network/io-service;1"]. getService(Components.interfaces.nsIIOService).newFileURI(aResultFile);
- xmlDoc = new XMLDocument(fileURI);
- xmlDoc.loadDocument();
- try {
- // parse the neutron exception
- Neutron.response(xmlDoc.documentData);
- } catch (exception) {
- aRequestFinishedCallback(null, exception);
+ xmlDoc = new XMLDocument(fileURI);
+ xmlDoc.loadDocument();
+
+ // parse error message (throws an exeception)
+ Neutron.response(xmlDoc.documentData);
+ } catch (exception) {
+ aRequestFinishedCallback(null, exception);
+ return;
+ }
}
-
- aRequestFinishedCallback(null, null);
}
},
Modified: public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/widget.js
===================================================================
--- public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/widget.js 2006-09-08 18:45:46 UTC (rev 17735)
+++ public/ulysses/src/trunk/ulysses/prototypes/prototype1/src/chrome/content/widget.js 2006-09-08 18:46:43 UTC (rev 17736)
@@ -35,7 +35,7 @@
/**
* Instantiates a new Object of the type Widget
*
- * @return {Widget}
+ * @return {Widget}
*/
function Widget() {
/* DEBUG */ dump("Ulysses:widget.js:Widget.Widget() invoked\n");
@@ -243,7 +243,7 @@
callback: aLoadFinishedCallback
};
- NetworkService.httpFetchToFile(this.widgets[i].iconURI.spec, this.widgets[i].tmpIconFile, this.requestFinishedHandler, contextObj);
+ NetworkService.httpFetchToFile(this.widgets[i].iconURI.spec, this.widgets[i].tmpIconFile, this.requestFinishedHandler, contextObj, true);
break;
}
}
More information about the Phoenix-commits
mailing list