2007-03-22 10:30:00 -07:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version : MPL 1.1 / GPL 2.0 / LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 ( the " License " ) ; you may not use this file except in compliance with
* the License . You may obtain a copy of the License at
* http : //www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an " AS IS " basis ,
* WITHOUT WARRANTY OF ANY KIND , either express or implied . See the License
* for the specific language governing rights and limitations under the
* License .
*
* The Original Code is mozilla . org code .
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation .
* Portions created by the Initial Developer are Copyright ( C ) 1998
* the Initial Developer . All Rights Reserved .
*
* Contributor ( s ) :
*
* Alternatively , the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later ( the " GPL " ) , or
* the GNU Lesser General Public License Version 2.1 or later ( the " LGPL " ) ,
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above . If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL , and not to allow others to
* use your version of this file under the terms of the MPL , indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL . If you do not delete
* the provisions above , a recipient may use your version of this file under
* the terms of any one of the MPL , the GPL or the LGPL .
*
* * * * * * END LICENSE BLOCK * * * * * */
2011-10-10 22:50:08 -07:00
# include "mozilla/Util.h"
2007-03-22 10:30:00 -07:00
# include "nsXMLHttpRequest.h"
# include "nsISimpleEnumerator.h"
# include "nsIXPConnect.h"
# include "nsICharsetConverterManager.h"
# include "nsLayoutCID.h"
# include "nsXPIDLString.h"
# include "nsReadableUtils.h"
# include "nsIURI.h"
# include "nsILoadGroup.h"
# include "nsNetUtil.h"
2009-09-08 16:29:41 -07:00
# include "nsStreamUtils.h"
2007-03-22 10:30:00 -07:00
# include "nsThreadUtils.h"
# include "nsIUploadChannel.h"
2009-09-08 16:29:41 -07:00
# include "nsIUploadChannel2.h"
2007-03-22 10:30:00 -07:00
# include "nsIDOMSerializer.h"
# include "nsXPCOM.h"
# include "nsISupportsPrimitives.h"
# include "nsGUIEvent.h"
# include "nsIPrivateDOMEvent.h"
# include "prprf.h"
# include "nsIDOMEventListener.h"
# include "nsIJSContextStack.h"
# include "nsIScriptSecurityManager.h"
# include "nsWeakPtr.h"
# include "nsICharsetAlias.h"
# include "nsIScriptGlobalObject.h"
2011-10-03 12:11:31 -07:00
# include "nsDOMClassInfoID.h"
2007-03-22 10:30:00 -07:00
# include "nsIDOMElement.h"
# include "nsIDOMWindow.h"
2009-09-08 16:29:41 -07:00
# include "nsIMIMEService.h"
# include "nsCExternalHandlerService.h"
2007-03-22 10:30:00 -07:00
# include "nsIVariant.h"
2011-12-24 00:27:04 -08:00
# include "nsVariant.h"
# include "nsIScriptError.h"
# include "xpcpublic.h"
2007-03-22 10:30:00 -07:00
# include "nsStringStream.h"
# include "nsIStreamConverterService.h"
# include "nsICachingChannel.h"
# include "nsContentUtils.h"
# include "nsEventDispatcher.h"
# include "nsDOMJSUtils.h"
# include "nsCOMArray.h"
# include "nsIScriptableUConv.h"
# include "nsCycleCollectionParticipant.h"
2007-03-24 22:35:39 -07:00
# include "nsIContentPolicy.h"
# include "nsContentPolicyUtils.h"
# include "nsContentErrors.h"
2007-05-16 10:05:47 -07:00
# include "nsLayoutStatics.h"
2008-09-30 17:49:30 -07:00
# include "nsCrossSiteListenerProxy.h"
2007-07-24 21:53:21 -07:00
# include "nsDOMError.h"
2007-07-26 19:49:18 -07:00
# include "nsIHTMLDocument.h"
2007-07-30 17:42:16 -07:00
# include "nsIMultiPartChannel.h"
2007-12-12 00:33:32 -08:00
# include "nsIScriptObjectPrincipal.h"
2008-03-18 12:49:20 -07:00
# include "nsIStorageStream.h"
2008-04-08 17:27:50 -07:00
# include "nsIPromptFactory.h"
# include "nsIWindowWatcher.h"
2010-05-04 07:43:48 -07:00
# include "nsCharSeparatedTokenizer.h"
2009-11-18 15:21:13 -08:00
# include "nsIConsoleService.h"
2010-04-23 12:54:09 -07:00
# include "nsIChannelPolicy.h"
# include "nsChannelPolicy.h"
# include "nsIContentSecurityPolicy.h"
2010-08-04 19:15:55 -07:00
# include "nsAsyncRedirectVerifyHelper.h"
2010-08-14 10:52:19 -07:00
# include "jstypedarray.h"
2010-11-23 08:49:12 -08:00
# include "nsStringBuffer.h"
2011-05-10 16:18:55 -07:00
# include "nsDOMFile.h"
# include "nsIFileChannel.h"
2011-11-19 10:50:17 -08:00
# include "mozilla/Telemetry.h"
2012-01-17 12:33:04 -08:00
# include "sampler.h"
2012-01-26 07:55:30 -08:00
# include "nsWrapperCacheInlines.h"
2007-03-22 10:30:00 -07:00
2011-10-10 22:50:08 -07:00
using namespace mozilla ;
2007-03-22 10:30:00 -07:00
# define LOAD_STR "load"
# define ERROR_STR "error"
2008-08-14 04:07:46 -07:00
# define ABORT_STR "abort"
2012-01-26 02:02:22 -08:00
# define TIMEOUT_STR "timeout"
2008-08-14 04:07:46 -07:00
# define LOADSTART_STR "loadstart"
2007-03-22 10:30:00 -07:00
# define PROGRESS_STR "progress"
# define UPLOADPROGRESS_STR "uploadprogress"
# define READYSTATE_STR "readystatechange"
2011-03-24 06:22:03 -07:00
# define LOADEND_STR "loadend"
2007-03-22 10:30:00 -07:00
// CIDs
// State
2011-05-10 16:18:55 -07:00
# define XML_HTTP_REQUEST_UNSENT (1 << 0) // 0 UNSENT
# define XML_HTTP_REQUEST_OPENED (1 << 1) // 1 OPENED
# define XML_HTTP_REQUEST_HEADERS_RECEIVED (1 << 2) // 2 HEADERS_RECEIVED
# define XML_HTTP_REQUEST_LOADING (1 << 3) // 3 LOADING
# define XML_HTTP_REQUEST_DONE (1 << 4) // 4 DONE
# define XML_HTTP_REQUEST_SENT (1 << 5) // Internal, OPENED in IE and external view
# define XML_HTTP_REQUEST_STOPPED (1 << 6) // Internal, LOADING in IE and external view
2007-03-22 10:30:00 -07:00
// The above states are mutually exclusive, change with ChangeState() only.
// The states below can be combined.
# define XML_HTTP_REQUEST_ABORTED (1 << 7) // Internal
# define XML_HTTP_REQUEST_ASYNC (1 << 8) // Internal
# define XML_HTTP_REQUEST_PARSEBODY (1 << 9) // Internal
2011-03-17 09:19:13 -07:00
# define XML_HTTP_REQUEST_SYNCLOOPING (1 << 10) // Internal
# define XML_HTTP_REQUEST_MULTIPART (1 << 11) // Internal
# define XML_HTTP_REQUEST_GOT_FINAL_STOP (1 << 12) // Internal
# define XML_HTTP_REQUEST_BACKGROUND (1 << 13) // Internal
2008-09-11 03:44:15 -07:00
// This is set when we've got the headers for a multipart XMLHttpRequest,
// but haven't yet started to process the first part.
2011-03-17 09:19:13 -07:00
# define XML_HTTP_REQUEST_MPART_HEADERS (1 << 14) // Internal
# define XML_HTTP_REQUEST_USE_XSITE_AC (1 << 15) // Internal
# define XML_HTTP_REQUEST_NEED_AC_PREFLIGHT (1 << 16) // Internal
# define XML_HTTP_REQUEST_AC_WITH_CREDENTIALS (1 << 17) // Internal
2012-01-26 02:02:22 -08:00
# define XML_HTTP_REQUEST_TIMED_OUT (1 << 18) // Internal
2012-02-14 10:05:43 -08:00
# define XML_HTTP_REQUEST_DELETED (1 << 19) // Internal
2007-03-22 10:30:00 -07:00
# define XML_HTTP_REQUEST_LOADSTATES \
2011-05-10 16:18:55 -07:00
( XML_HTTP_REQUEST_UNSENT | \
2007-03-22 10:30:00 -07:00
XML_HTTP_REQUEST_OPENED | \
2011-05-10 16:18:55 -07:00
XML_HTTP_REQUEST_HEADERS_RECEIVED | \
XML_HTTP_REQUEST_LOADING | \
XML_HTTP_REQUEST_DONE | \
2007-03-22 10:30:00 -07:00
XML_HTTP_REQUEST_SENT | \
XML_HTTP_REQUEST_STOPPED )
2008-03-18 17:14:38 -07:00
# define NS_BADCERTHANDLER_CONTRACTID \
" @mozilla.org/content/xmlhttprequest-bad-cert-handler;1 "
2009-01-13 09:38:01 -08:00
# define NS_PROGRESS_EVENT_INTERVAL 50
2008-10-19 14:26:37 -07:00
2011-11-15 23:38:51 -08:00
NS_IMPL_ISUPPORTS1 ( nsXHRParseEndListener , nsIDOMEventListener )
2009-03-03 12:11:14 -08:00
class nsResumeTimeoutsEvent : public nsRunnable
{
public :
nsResumeTimeoutsEvent ( nsPIDOMWindow * aWindow ) : mWindow ( aWindow ) { }
NS_IMETHOD Run ( )
{
2011-10-17 07:59:28 -07:00
mWindow - > ResumeTimeouts ( false ) ;
2009-03-03 12:11:14 -08:00
return NS_OK ;
}
private :
nsCOMPtr < nsPIDOMWindow > mWindow ;
} ;
2008-08-14 04:04:43 -07:00
2007-03-22 10:30:00 -07:00
// This helper function adds the given load flags to the request's existing
// load flags.
static void AddLoadFlags ( nsIRequest * request , nsLoadFlags newFlags )
{
nsLoadFlags flags ;
request - > GetLoadFlags ( & flags ) ;
flags | = newFlags ;
request - > SetLoadFlags ( flags ) ;
}
2011-09-28 23:19:26 -07:00
static nsresult IsCapabilityEnabled ( const char * capability , bool * enabled )
2008-03-18 17:14:38 -07:00
{
nsIScriptSecurityManager * secMan = nsContentUtils : : GetSecurityManager ( ) ;
if ( ! secMan )
return NS_ERROR_FAILURE ;
return secMan - > IsCapabilityEnabled ( capability , enabled ) ;
}
2007-03-22 10:30:00 -07:00
// Helper proxy class to be used when expecting an
// multipart/x-mixed-replace stream of XML documents.
class nsMultipartProxyListener : public nsIStreamListener
{
public :
nsMultipartProxyListener ( nsIStreamListener * dest ) ;
virtual ~ nsMultipartProxyListener ( ) ;
/* additional members */
NS_DECL_ISUPPORTS
NS_DECL_NSISTREAMLISTENER
NS_DECL_NSIREQUESTOBSERVER
private :
nsCOMPtr < nsIStreamListener > mDestListener ;
} ;
nsMultipartProxyListener : : nsMultipartProxyListener ( nsIStreamListener * dest )
: mDestListener ( dest )
{
}
nsMultipartProxyListener : : ~ nsMultipartProxyListener ( )
{
}
NS_IMPL_ISUPPORTS2 ( nsMultipartProxyListener , nsIStreamListener ,
nsIRequestObserver )
/** nsIRequestObserver methods **/
NS_IMETHODIMP
nsMultipartProxyListener : : OnStartRequest ( nsIRequest * aRequest ,
nsISupports * ctxt )
{
nsCOMPtr < nsIChannel > channel ( do_QueryInterface ( aRequest ) ) ;
NS_ENSURE_TRUE ( channel , NS_ERROR_UNEXPECTED ) ;
nsCAutoString contentType ;
nsresult rv = channel - > GetContentType ( contentType ) ;
if ( ! contentType . EqualsLiteral ( " multipart/x-mixed-replace " ) ) {
return NS_ERROR_INVALID_ARG ;
}
// If multipart/x-mixed-replace content, we'll insert a MIME
// decoder in the pipeline to handle the content and pass it along
// to our original listener.
2008-09-11 03:44:15 -07:00
nsCOMPtr < nsIXMLHttpRequest > xhr = do_QueryInterface ( mDestListener ) ;
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsIStreamConverterService > convServ =
do_GetService ( " @mozilla.org/streamConverters;1 " , & rv ) ;
if ( NS_SUCCEEDED ( rv ) ) {
nsCOMPtr < nsIStreamListener > toListener ( mDestListener ) ;
nsCOMPtr < nsIStreamListener > fromListener ;
rv = convServ - > AsyncConvertData ( " multipart/x-mixed-replace " ,
" */* " ,
toListener ,
nsnull ,
getter_AddRefs ( fromListener ) ) ;
NS_ENSURE_TRUE ( NS_SUCCEEDED ( rv ) & & fromListener , NS_ERROR_UNEXPECTED ) ;
mDestListener = fromListener ;
}
2008-09-11 03:44:15 -07:00
if ( xhr ) {
static_cast < nsXMLHttpRequest * > ( xhr . get ( ) ) - > mState | =
XML_HTTP_REQUEST_MPART_HEADERS ;
}
2007-03-22 10:30:00 -07:00
return mDestListener - > OnStartRequest ( aRequest , ctxt ) ;
}
NS_IMETHODIMP
nsMultipartProxyListener : : OnStopRequest ( nsIRequest * aRequest ,
nsISupports * ctxt ,
nsresult status )
{
return mDestListener - > OnStopRequest ( aRequest , ctxt , status ) ;
}
/** nsIStreamListener methods **/
NS_IMETHODIMP
nsMultipartProxyListener : : OnDataAvailable ( nsIRequest * aRequest ,
nsISupports * ctxt ,
nsIInputStream * inStr ,
PRUint32 sourceOffset ,
PRUint32 count )
{
return mDestListener - > OnDataAvailable ( aRequest , ctxt , inStr , sourceOffset ,
count ) ;
}
2008-08-14 04:07:46 -07:00
/////////////////////////////////////////////
NS_IMPL_CYCLE_COLLECTION_CLASS ( nsXHREventTarget )
2009-06-15 01:27:29 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED ( nsXHREventTarget ,
2012-02-07 18:53:33 -08:00
nsDOMEventTargetHelper )
2008-08-14 04:07:46 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mOnLoadListener )
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mOnErrorListener )
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mOnAbortListener )
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mOnLoadStartListener )
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mOnProgressListener )
2011-03-24 06:22:03 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mOnLoadendListener )
2012-01-26 02:02:22 -08:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mOnTimeoutListener )
2008-08-14 04:07:46 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
2009-06-15 01:27:29 -07:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED ( nsXHREventTarget ,
2012-02-07 18:53:33 -08:00
nsDOMEventTargetHelper )
2008-08-14 04:07:46 -07:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mOnLoadListener )
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mOnErrorListener )
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mOnAbortListener )
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mOnLoadStartListener )
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mOnProgressListener )
2011-03-24 06:22:03 -07:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mOnLoadendListener )
2012-01-26 02:02:22 -08:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mOnTimeoutListener )
2008-08-14 04:07:46 -07:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
2009-06-15 01:27:29 -07:00
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED ( nsXHREventTarget )
2008-08-14 04:07:46 -07:00
NS_INTERFACE_MAP_ENTRY ( nsIXMLHttpRequestEventTarget )
2012-02-07 18:53:33 -08:00
NS_INTERFACE_MAP_END_INHERITING ( nsDOMEventTargetHelper )
2008-08-14 04:07:46 -07:00
2012-02-07 18:53:33 -08:00
NS_IMPL_ADDREF_INHERITED ( nsXHREventTarget , nsDOMEventTargetHelper )
NS_IMPL_RELEASE_INHERITED ( nsXHREventTarget , nsDOMEventTargetHelper )
2008-08-14 04:07:46 -07:00
NS_IMETHODIMP
nsXHREventTarget : : GetOnload ( nsIDOMEventListener * * aOnLoad )
{
return GetInnerEventListener ( mOnLoadListener , aOnLoad ) ;
}
NS_IMETHODIMP
nsXHREventTarget : : SetOnload ( nsIDOMEventListener * aOnLoad )
{
return RemoveAddEventListener ( NS_LITERAL_STRING ( LOAD_STR ) ,
mOnLoadListener , aOnLoad ) ;
}
NS_IMETHODIMP
nsXHREventTarget : : GetOnerror ( nsIDOMEventListener * * aOnerror )
{
return GetInnerEventListener ( mOnErrorListener , aOnerror ) ;
}
NS_IMETHODIMP
nsXHREventTarget : : SetOnerror ( nsIDOMEventListener * aOnerror )
{
return RemoveAddEventListener ( NS_LITERAL_STRING ( ERROR_STR ) ,
mOnErrorListener , aOnerror ) ;
}
NS_IMETHODIMP
nsXHREventTarget : : GetOnabort ( nsIDOMEventListener * * aOnabort )
{
return GetInnerEventListener ( mOnAbortListener , aOnabort ) ;
}
NS_IMETHODIMP
nsXHREventTarget : : SetOnabort ( nsIDOMEventListener * aOnabort )
{
return RemoveAddEventListener ( NS_LITERAL_STRING ( ABORT_STR ) ,
mOnAbortListener , aOnabort ) ;
}
NS_IMETHODIMP
nsXHREventTarget : : GetOnloadstart ( nsIDOMEventListener * * aOnloadstart )
{
return GetInnerEventListener ( mOnLoadStartListener , aOnloadstart ) ;
}
NS_IMETHODIMP
nsXHREventTarget : : SetOnloadstart ( nsIDOMEventListener * aOnloadstart )
{
return RemoveAddEventListener ( NS_LITERAL_STRING ( LOADSTART_STR ) ,
mOnLoadStartListener , aOnloadstart ) ;
}
NS_IMETHODIMP
nsXHREventTarget : : GetOnprogress ( nsIDOMEventListener * * aOnprogress )
{
return GetInnerEventListener ( mOnProgressListener , aOnprogress ) ;
}
NS_IMETHODIMP
nsXHREventTarget : : SetOnprogress ( nsIDOMEventListener * aOnprogress )
{
return RemoveAddEventListener ( NS_LITERAL_STRING ( PROGRESS_STR ) ,
mOnProgressListener , aOnprogress ) ;
}
2012-01-26 02:02:22 -08:00
/* attribute nsIDOMEventListener ontimeout; */
NS_IMETHODIMP
nsXHREventTarget : : GetOntimeout ( nsIDOMEventListener * * aOntimeout )
{
return GetInnerEventListener ( mOnTimeoutListener , aOntimeout ) ;
}
NS_IMETHODIMP
nsXHREventTarget : : SetOntimeout ( nsIDOMEventListener * aOntimeout )
{
return RemoveAddEventListener ( NS_LITERAL_STRING ( TIMEOUT_STR ) ,
mOnTimeoutListener , aOntimeout ) ;
}
2011-03-24 06:22:03 -07:00
NS_IMETHODIMP
nsXHREventTarget : : GetOnloadend ( nsIDOMEventListener * * aOnLoadend )
{
return GetInnerEventListener ( mOnLoadendListener , aOnLoadend ) ;
}
NS_IMETHODIMP
nsXHREventTarget : : SetOnloadend ( nsIDOMEventListener * aOnLoadend )
{
return RemoveAddEventListener ( NS_LITERAL_STRING ( LOADEND_STR ) ,
mOnLoadendListener , aOnLoadend ) ;
}
2008-09-25 07:40:30 -07:00
/////////////////////////////////////////////
2008-08-14 04:07:46 -07:00
2010-01-12 05:08:43 -08:00
DOMCI_DATA ( XMLHttpRequestUpload , nsXMLHttpRequestUpload )
2008-09-25 07:40:30 -07:00
NS_INTERFACE_MAP_BEGIN ( nsXMLHttpRequestUpload )
2008-08-14 04:07:46 -07:00
NS_INTERFACE_MAP_ENTRY ( nsIXMLHttpRequestUpload )
2010-03-17 08:09:05 -07:00
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO ( XMLHttpRequestUpload )
2008-08-14 04:07:46 -07:00
NS_INTERFACE_MAP_END_INHERITING ( nsXHREventTarget )
NS_IMPL_ADDREF_INHERITED ( nsXMLHttpRequestUpload , nsXHREventTarget )
NS_IMPL_RELEASE_INHERITED ( nsXMLHttpRequestUpload , nsXHREventTarget )
2007-03-22 10:30:00 -07:00
/////////////////////////////////////////////
//
//
/////////////////////////////////////////////
nsXMLHttpRequest : : nsXMLHttpRequest ( )
2011-09-23 18:57:36 -07:00
: mResponseBodyDecodedPos ( 0 ) ,
mResponseType ( XML_HTTP_RESPONSE_TYPE_DEFAULT ) ,
2011-05-10 16:18:55 -07:00
mRequestObserver ( nsnull ) , mState ( XML_HTTP_REQUEST_UNSENT ) ,
2011-10-17 07:59:28 -07:00
mUploadTransferred ( 0 ) , mUploadTotal ( 0 ) , mUploadComplete ( true ) ,
mProgressSinceLastProgressEvent ( false ) ,
2008-10-19 14:26:37 -07:00
mUploadProgress ( 0 ) , mUploadProgressMax ( 0 ) ,
2012-01-26 02:02:22 -08:00
mRequestSentTime ( 0 ) , mTimeoutMilliseconds ( 0 ) ,
2012-02-16 08:45:25 -08:00
mErrorLoad ( false ) , mWaitingForOnStopRequest ( false ) ,
mProgressTimerIsActive ( false ) , mProgressEventWasDelayed ( false ) ,
2011-11-15 23:38:51 -08:00
mIsHtml ( false ) ,
mWarnAboutMultipartHtml ( false ) ,
mWarnAboutSyncHtml ( false ) ,
2012-01-26 02:02:22 -08:00
mLoadLengthComputable ( false ) , mLoadTotal ( 0 ) ,
2011-10-17 07:59:28 -07:00
mFirstStartRequestSeen ( false ) ,
mInLoadProgressEvent ( false ) ,
2011-09-23 18:57:22 -07:00
mResultJSON ( JSVAL_VOID ) ,
mResultArrayBuffer ( nsnull )
2007-03-22 10:30:00 -07:00
{
2007-05-16 10:05:47 -07:00
nsLayoutStatics : : AddRef ( ) ;
2007-03-22 10:30:00 -07:00
}
nsXMLHttpRequest : : ~ nsXMLHttpRequest ( )
{
2012-02-14 10:05:43 -08:00
mState | = XML_HTTP_REQUEST_DELETED ;
2007-03-22 10:30:00 -07:00
if ( mState & ( XML_HTTP_REQUEST_STOPPED |
XML_HTTP_REQUEST_SENT |
2011-05-10 16:18:55 -07:00
XML_HTTP_REQUEST_LOADING ) ) {
2007-03-22 10:30:00 -07:00
Abort ( ) ;
}
NS_ABORT_IF_FALSE ( ! ( mState & XML_HTTP_REQUEST_SYNCLOOPING ) , " we rather crash than hang " ) ;
mState & = ~ XML_HTTP_REQUEST_SYNCLOOPING ;
2007-05-16 10:05:47 -07:00
nsLayoutStatics : : Release ( ) ;
2007-03-22 10:30:00 -07:00
}
2011-05-23 18:09:28 -07:00
void
nsXMLHttpRequest : : RootResultArrayBuffer ( )
{
2011-06-23 19:17:58 -07:00
nsContentUtils : : PreserveWrapper (
static_cast < nsIDOMEventTarget * > (
static_cast < nsDOMEventTargetHelper * > ( this ) ) , this ) ;
2011-05-23 18:09:28 -07:00
}
2008-02-19 15:12:23 -08:00
/**
* This Init method is called from the factory constructor .
*/
2007-12-12 00:33:32 -08:00
nsresult
nsXMLHttpRequest : : Init ( )
{
// Set the original mScriptContext and mPrincipal, if available.
// Get JSContext from stack.
nsCOMPtr < nsIJSContextStack > stack =
do_GetService ( " @mozilla.org/js/xpc/ContextStack;1 " ) ;
if ( ! stack ) {
return NS_OK ;
}
JSContext * cx ;
if ( NS_FAILED ( stack - > Peek ( & cx ) ) | | ! cx ) {
return NS_OK ;
}
nsIScriptSecurityManager * secMan = nsContentUtils : : GetSecurityManager ( ) ;
nsCOMPtr < nsIPrincipal > subjectPrincipal ;
if ( secMan ) {
2011-06-21 09:52:22 -07:00
nsresult rv = secMan - > GetSubjectPrincipal ( getter_AddRefs ( subjectPrincipal ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-12-12 00:33:32 -08:00
}
NS_ENSURE_STATE ( subjectPrincipal ) ;
mPrincipal = subjectPrincipal ;
2008-02-19 15:12:23 -08:00
nsIScriptContext * context = GetScriptContextFromJSContext ( cx ) ;
if ( context ) {
mScriptContext = context ;
nsCOMPtr < nsPIDOMWindow > window =
do_QueryInterface ( context - > GetGlobalObject ( ) ) ;
if ( window ) {
mOwner = window - > GetCurrentInnerWindow ( ) ;
}
}
return NS_OK ;
}
/**
* This Init method should only be called by C + + consumers .
*/
NS_IMETHODIMP
nsXMLHttpRequest : : Init ( nsIPrincipal * aPrincipal ,
nsIScriptContext * aScriptContext ,
2008-12-30 14:24:58 -08:00
nsPIDOMWindow * aOwnerWindow ,
nsIURI * aBaseURI )
2008-02-19 15:12:23 -08:00
{
NS_ENSURE_ARG_POINTER ( aPrincipal ) ;
// This object may have already been initialized in the other Init call above
// if JS was on the stack. Clear the old values for mScriptContext and mOwner
// if new ones are not supplied here.
mPrincipal = aPrincipal ;
mScriptContext = aScriptContext ;
if ( aOwnerWindow ) {
mOwner = aOwnerWindow - > GetCurrentInnerWindow ( ) ;
}
else {
mOwner = nsnull ;
2007-12-12 00:33:32 -08:00
}
2008-12-30 14:24:58 -08:00
mBaseURI = aBaseURI ;
2007-12-12 00:33:32 -08:00
return NS_OK ;
}
2008-02-19 15:12:23 -08:00
/**
* This Initialize method is called from XPConnect via nsIJSNativeInitializer .
*/
2007-12-12 00:33:32 -08:00
NS_IMETHODIMP
nsXMLHttpRequest : : Initialize ( nsISupports * aOwner , JSContext * cx , JSObject * obj ,
PRUint32 argc , jsval * argv )
{
mOwner = do_QueryInterface ( aOwner ) ;
if ( ! mOwner ) {
NS_WARNING ( " Unexpected nsIJSNativeInitializer owner " ) ;
return NS_OK ;
}
// This XHR object is bound to a |window|,
// so re-set principal and script context.
nsCOMPtr < nsIScriptObjectPrincipal > scriptPrincipal = do_QueryInterface ( aOwner ) ;
NS_ENSURE_STATE ( scriptPrincipal ) ;
mPrincipal = scriptPrincipal - > GetPrincipal ( ) ;
nsCOMPtr < nsIScriptGlobalObject > sgo = do_QueryInterface ( aOwner ) ;
NS_ENSURE_STATE ( sgo ) ;
mScriptContext = sgo - > GetContext ( ) ;
NS_ENSURE_STATE ( mScriptContext ) ;
return NS_OK ;
}
2011-09-23 18:57:22 -07:00
void
nsXMLHttpRequest : : ResetResponse ( )
{
mResponseXML = nsnull ;
mResponseBody . Truncate ( ) ;
2011-09-23 18:57:36 -07:00
mResponseText . Truncate ( ) ;
2011-09-23 18:57:22 -07:00
mResponseBlob = nsnull ;
2012-01-30 02:33:59 -08:00
mDOMFile = nsnull ;
mBuilder = nsnull ;
2011-09-23 18:57:22 -07:00
mResultArrayBuffer = nsnull ;
mResultJSON = JSVAL_VOID ;
mLoadTransferred = 0 ;
2011-09-23 18:57:36 -07:00
mResponseBodyDecodedPos = 0 ;
2011-09-23 18:57:22 -07:00
}
2008-09-30 16:56:57 -07:00
void
nsXMLHttpRequest : : SetRequestObserver ( nsIRequestObserver * aObserver )
{
mRequestObserver = aObserver ;
}
2007-03-22 10:30:00 -07:00
NS_IMPL_CYCLE_COLLECTION_CLASS ( nsXMLHttpRequest )
2012-01-26 07:55:30 -08:00
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN ( nsXMLHttpRequest )
2012-02-16 08:45:25 -08:00
bool isBlack = tmp - > IsBlack ( ) ;
if ( isBlack | | tmp - > mWaitingForOnStopRequest ) {
2012-01-26 07:55:30 -08:00
if ( tmp - > mListenerManager ) {
tmp - > mListenerManager - > UnmarkGrayJSListeners ( ) ;
NS_UNMARK_LISTENER_WRAPPER ( Load )
NS_UNMARK_LISTENER_WRAPPER ( Error )
NS_UNMARK_LISTENER_WRAPPER ( Abort )
NS_UNMARK_LISTENER_WRAPPER ( LoadStart )
NS_UNMARK_LISTENER_WRAPPER ( Progress )
NS_UNMARK_LISTENER_WRAPPER ( Loadend )
NS_UNMARK_LISTENER_WRAPPER ( UploadProgress )
NS_UNMARK_LISTENER_WRAPPER ( Readystatechange )
}
2012-02-16 08:45:25 -08:00
if ( ! isBlack ) {
xpc_UnmarkGrayObject ( tmp - > PreservingWrapper ( ) ?
tmp - > GetWrapperPreserveColor ( ) :
tmp - > GetExpandoObjectPreserveColor ( ) ) ;
}
2012-01-26 07:55:30 -08:00
return true ;
}
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN ( nsXMLHttpRequest )
return tmp - > IsBlack ( ) ;
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN ( nsXMLHttpRequest )
return tmp - > IsBlack ( ) ;
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
2008-08-14 04:07:46 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED ( nsXMLHttpRequest ,
nsXHREventTarget )
2007-03-22 10:30:00 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mContext )
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mChannel )
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mReadRequest )
2008-10-28 11:30:10 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mResponseXML )
2011-03-28 13:18:45 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mCORSPreflightChannel )
2007-03-22 10:30:00 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mOnUploadProgressListener )
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mOnReadystatechangeListener )
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mXMLParserStreamListener )
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mChannelEventSink )
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mProgressEventSink )
2007-12-12 00:33:32 -08:00
2008-08-14 04:07:46 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS ( mUpload ,
nsIXMLHttpRequestUpload )
2007-03-22 10:30:00 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
2008-08-14 04:07:46 -07:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED ( nsXMLHttpRequest ,
nsXHREventTarget )
2011-05-23 18:09:28 -07:00
tmp - > mResultArrayBuffer = nsnull ;
2011-09-08 15:12:18 -07:00
tmp - > mResultJSON = JSVAL_VOID ;
2007-03-22 10:30:00 -07:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mContext )
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mChannel )
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mReadRequest )
2008-10-28 11:30:10 -07:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mResponseXML )
2011-03-28 13:18:45 -07:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mCORSPreflightChannel )
2007-03-22 10:30:00 -07:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mOnUploadProgressListener )
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mOnReadystatechangeListener )
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mXMLParserStreamListener )
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mChannelEventSink )
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mProgressEventSink )
2007-12-12 00:33:32 -08:00
2008-08-14 04:07:46 -07:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mUpload )
2007-03-22 10:30:00 -07:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
2011-05-23 18:09:28 -07:00
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED ( nsXMLHttpRequest ,
nsXHREventTarget )
if ( tmp - > mResultArrayBuffer ) {
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_CALLBACK ( tmp - > mResultArrayBuffer ,
" mResultArrayBuffer " )
}
2011-09-08 15:12:18 -07:00
if ( JSVAL_IS_GCTHING ( tmp - > mResultJSON ) ) {
void * gcThing = JSVAL_TO_GCTHING ( tmp - > mResultJSON ) ;
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_CALLBACK ( gcThing , " mResultJSON " )
}
2011-05-23 18:09:28 -07:00
NS_IMPL_CYCLE_COLLECTION_TRACE_END
2010-01-12 05:08:43 -08:00
DOMCI_DATA ( XMLHttpRequest , nsXMLHttpRequest )
2007-03-22 10:30:00 -07:00
// QueryInterface implementation for nsXMLHttpRequest
2008-08-14 04:07:46 -07:00
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED ( nsXMLHttpRequest )
2007-03-22 10:30:00 -07:00
NS_INTERFACE_MAP_ENTRY ( nsIXMLHttpRequest )
NS_INTERFACE_MAP_ENTRY ( nsIJSXMLHttpRequest )
NS_INTERFACE_MAP_ENTRY ( nsIRequestObserver )
NS_INTERFACE_MAP_ENTRY ( nsIStreamListener )
NS_INTERFACE_MAP_ENTRY ( nsIChannelEventSink )
NS_INTERFACE_MAP_ENTRY ( nsIProgressEventSink )
NS_INTERFACE_MAP_ENTRY ( nsIInterfaceRequestor )
NS_INTERFACE_MAP_ENTRY ( nsISupportsWeakReference )
2007-12-12 00:33:32 -08:00
NS_INTERFACE_MAP_ENTRY ( nsIJSNativeInitializer )
2008-10-19 14:26:37 -07:00
NS_INTERFACE_MAP_ENTRY ( nsITimerCallback )
2010-03-17 08:09:05 -07:00
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO ( XMLHttpRequest )
2008-08-14 04:07:46 -07:00
NS_INTERFACE_MAP_END_INHERITING ( nsXHREventTarget )
2007-03-22 10:30:00 -07:00
2008-08-14 04:07:46 -07:00
NS_IMPL_ADDREF_INHERITED ( nsXMLHttpRequest , nsXHREventTarget )
NS_IMPL_RELEASE_INHERITED ( nsXMLHttpRequest , nsXHREventTarget )
2007-03-22 10:30:00 -07:00
NS_IMETHODIMP
nsXMLHttpRequest : : GetOnreadystatechange ( nsIDOMEventListener * * aOnreadystatechange )
{
2008-08-14 04:07:46 -07:00
return
nsXHREventTarget : : GetInnerEventListener ( mOnReadystatechangeListener ,
aOnreadystatechange ) ;
2007-03-22 10:30:00 -07:00
}
NS_IMETHODIMP
nsXMLHttpRequest : : SetOnreadystatechange ( nsIDOMEventListener * aOnreadystatechange )
{
2008-08-14 04:07:46 -07:00
return
nsXHREventTarget : : RemoveAddEventListener ( NS_LITERAL_STRING ( READYSTATE_STR ) ,
mOnReadystatechangeListener ,
aOnreadystatechange ) ;
2007-03-22 10:30:00 -07:00
}
NS_IMETHODIMP
nsXMLHttpRequest : : GetOnuploadprogress ( nsIDOMEventListener * * aOnuploadprogress )
{
2008-08-14 04:07:46 -07:00
return
nsXHREventTarget : : GetInnerEventListener ( mOnUploadProgressListener ,
aOnuploadprogress ) ;
2007-03-22 10:30:00 -07:00
}
NS_IMETHODIMP
nsXMLHttpRequest : : SetOnuploadprogress ( nsIDOMEventListener * aOnuploadprogress )
{
2008-08-14 04:07:46 -07:00
return
nsXHREventTarget : : RemoveAddEventListener ( NS_LITERAL_STRING ( UPLOADPROGRESS_STR ) ,
mOnUploadProgressListener ,
aOnuploadprogress ) ;
2007-03-22 10:30:00 -07:00
}
/* readonly attribute nsIChannel channel; */
NS_IMETHODIMP
nsXMLHttpRequest : : GetChannel ( nsIChannel * * aChannel )
{
NS_ENSURE_ARG_POINTER ( aChannel ) ;
NS_IF_ADDREF ( * aChannel = mChannel ) ;
return NS_OK ;
}
2011-12-08 05:54:05 -08:00
static void LogMessage ( const char * aWarning , nsPIDOMWindow * aWindow )
{
nsCOMPtr < nsIDocument > doc ;
if ( aWindow ) {
doc = do_QueryInterface ( aWindow - > GetExtantDocument ( ) ) ;
}
2011-12-15 06:47:03 -08:00
nsContentUtils : : ReportToConsole ( nsIScriptError : : warningFlag ,
" DOM " , doc ,
nsContentUtils : : eDOM_PROPERTIES ,
aWarning ) ;
2011-12-08 05:54:05 -08:00
}
2007-03-22 10:30:00 -07:00
/* readonly attribute nsIDOMDocument responseXML; */
NS_IMETHODIMP
nsXMLHttpRequest : : GetResponseXML ( nsIDOMDocument * * aResponseXML )
{
NS_ENSURE_ARG_POINTER ( aResponseXML ) ;
* aResponseXML = nsnull ;
2011-05-10 16:18:55 -07:00
if ( mResponseType ! = XML_HTTP_RESPONSE_TYPE_DEFAULT & &
mResponseType ! = XML_HTTP_RESPONSE_TYPE_DOCUMENT ) {
return NS_ERROR_DOM_INVALID_STATE_ERR ;
}
if ( ( XML_HTTP_REQUEST_DONE & mState ) & & mResponseXML ) {
2008-10-11 23:30:14 -07:00
* aResponseXML = mResponseXML ;
2007-03-22 10:30:00 -07:00
NS_ADDREF ( * aResponseXML ) ;
}
2011-11-15 23:38:51 -08:00
if ( mWarnAboutMultipartHtml ) {
mWarnAboutMultipartHtml = false ;
2011-12-08 05:54:05 -08:00
LogMessage ( " HTMLMultipartXHRWarning " , mOwner ) ;
2011-11-15 23:38:51 -08:00
}
if ( mWarnAboutSyncHtml ) {
mWarnAboutSyncHtml = false ;
2011-12-08 05:54:05 -08:00
LogMessage ( " HTMLSyncXHRWarning " , mOwner ) ;
2011-11-15 23:38:51 -08:00
}
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
/*
* This piece copied from nsXMLDocument , we try to get the charset
* from HTTP headers .
*/
nsresult
2011-09-23 18:57:36 -07:00
nsXMLHttpRequest : : DetectCharset ( )
2007-03-22 10:30:00 -07:00
{
2011-09-23 18:57:36 -07:00
mResponseCharset . Truncate ( ) ;
mDecoder = nsnull ;
2007-03-22 10:30:00 -07:00
2011-09-23 18:57:36 -07:00
if ( mResponseType ! = XML_HTTP_RESPONSE_TYPE_DEFAULT & &
mResponseType ! = XML_HTTP_RESPONSE_TYPE_TEXT & &
2011-09-23 18:57:36 -07:00
mResponseType ! = XML_HTTP_RESPONSE_TYPE_JSON & &
mResponseType ! = XML_HTTP_RESPONSE_TYPE_CHUNKED_TEXT ) {
2007-03-22 10:30:00 -07:00
return NS_OK ;
2010-04-23 10:37:02 -07:00
}
2007-03-22 10:30:00 -07:00
2011-09-23 18:57:36 -07:00
nsCOMPtr < nsIChannel > channel = do_QueryInterface ( mReadRequest ) ;
if ( ! channel ) {
channel = mChannel ;
}
2007-03-22 10:30:00 -07:00
2011-09-23 18:57:36 -07:00
nsCAutoString charsetVal ;
nsresult rv = channel ? channel - > GetContentCharset ( charsetVal ) :
NS_ERROR_FAILURE ;
if ( NS_SUCCEEDED ( rv ) ) {
nsCOMPtr < nsICharsetAlias > calias =
do_GetService ( NS_CHARSETALIAS_CONTRACTID , & rv ) ;
if ( NS_SUCCEEDED ( rv ) & & calias ) {
rv = calias - > GetPreferred ( charsetVal , mResponseCharset ) ;
2007-03-22 10:30:00 -07:00
}
}
2011-09-23 18:57:36 -07:00
if ( NS_FAILED ( rv ) | | mResponseCharset . IsEmpty ( ) ) {
// MS documentation states UTF-8 is default for responseText
mResponseCharset . AssignLiteral ( " UTF-8 " ) ;
2010-04-23 10:37:02 -07:00
}
2007-03-22 10:30:00 -07:00
2011-12-08 05:56:14 -08:00
if ( mResponseType = = XML_HTTP_RESPONSE_TYPE_JSON & &
! mResponseCharset . EqualsLiteral ( " UTF-8 " ) ) {
// The XHR spec says only UTF-8 is supported for responseType == "json"
LogMessage ( " JSONCharsetWarning " , mOwner ) ;
mResponseCharset . AssignLiteral ( " UTF-8 " ) ;
}
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsICharsetConverterManager > ccm =
do_GetService ( NS_CHARSETCONVERTERMANAGER_CONTRACTID , & rv ) ;
2011-09-23 18:57:36 -07:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-03-22 10:30:00 -07:00
2011-09-23 18:57:36 -07:00
return ccm - > GetUnicodeDecoderRaw ( mResponseCharset . get ( ) ,
getter_AddRefs ( mDecoder ) ) ;
}
2007-03-22 10:30:00 -07:00
2011-09-23 18:57:36 -07:00
nsresult
nsXMLHttpRequest : : AppendToResponseText ( const char * aSrcBuffer ,
PRUint32 aSrcBufferLen )
{
NS_ENSURE_STATE ( mDecoder ) ;
PRInt32 destBufferLen ;
nsresult rv = mDecoder - > GetMaxLength ( aSrcBuffer , aSrcBufferLen ,
& destBufferLen ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-03-22 10:30:00 -07:00
2011-09-23 18:57:36 -07:00
if ( ! mResponseText . SetCapacity ( mResponseText . Length ( ) + destBufferLen ) ) {
2007-03-22 10:30:00 -07:00
return NS_ERROR_OUT_OF_MEMORY ;
}
2011-09-23 18:57:36 -07:00
PRUnichar * destBuffer = mResponseText . BeginWriting ( ) + mResponseText . Length ( ) ;
2010-11-23 08:49:12 -08:00
2011-09-23 18:57:36 -07:00
PRInt32 totalChars = mResponseText . Length ( ) ;
2007-03-22 10:30:00 -07:00
2011-09-23 18:57:36 -07:00
// This code here is basically a copy of a similar thing in
// nsScanner::Append(const char* aBuffer, PRUint32 aLen).
// If we get illegal characters in the input we replace
// them and don't just fail.
2007-03-22 10:30:00 -07:00
do {
2011-09-23 18:57:36 -07:00
PRInt32 srclen = ( PRInt32 ) aSrcBufferLen ;
PRInt32 destlen = ( PRInt32 ) destBufferLen ;
rv = mDecoder - > Convert ( aSrcBuffer ,
& srclen ,
destBuffer ,
& destlen ) ;
2007-03-22 10:30:00 -07:00
if ( NS_FAILED ( rv ) ) {
// We consume one byte, replace it with U+FFFD
// and try the conversion again.
2011-09-23 18:57:36 -07:00
destBuffer [ destlen ] = ( PRUnichar ) 0xFFFD ; // add replacement character
destlen + + ; // skip written replacement character
destBuffer + = destlen ;
destBufferLen - = destlen ;
2007-03-22 10:30:00 -07:00
2011-09-23 18:57:36 -07:00
if ( srclen < ( PRInt32 ) aSrcBufferLen ) {
srclen + + ; // Consume the invalid character
2007-03-22 10:30:00 -07:00
}
2011-09-23 18:57:36 -07:00
aSrcBuffer + = srclen ;
aSrcBufferLen - = srclen ;
2007-03-22 10:30:00 -07:00
2011-09-23 18:57:36 -07:00
mDecoder - > Reset ( ) ;
2007-03-22 10:30:00 -07:00
}
2011-09-23 18:57:36 -07:00
totalChars + = destlen ;
} while ( NS_FAILED ( rv ) & & aSrcBufferLen > 0 ) ;
mResponseText . SetLength ( totalChars ) ;
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
/* readonly attribute AString responseText; */
NS_IMETHODIMP nsXMLHttpRequest : : GetResponseText ( nsAString & aResponseText )
{
aResponseText . Truncate ( ) ;
2011-05-10 16:18:55 -07:00
if ( mResponseType ! = XML_HTTP_RESPONSE_TYPE_DEFAULT & &
2011-09-23 18:57:36 -07:00
mResponseType ! = XML_HTTP_RESPONSE_TYPE_TEXT & &
mResponseType ! = XML_HTTP_RESPONSE_TYPE_CHUNKED_TEXT ) {
2011-05-10 16:18:55 -07:00
return NS_ERROR_DOM_INVALID_STATE_ERR ;
}
2011-09-23 18:57:36 -07:00
if ( mResponseType = = XML_HTTP_RESPONSE_TYPE_CHUNKED_TEXT & &
! mInLoadProgressEvent ) {
2011-10-17 07:59:28 -07:00
aResponseText . SetIsVoid ( true ) ;
2011-09-23 18:57:36 -07:00
return NS_OK ;
}
2011-09-23 18:57:36 -07:00
if ( ! ( mState & ( XML_HTTP_REQUEST_DONE | XML_HTTP_REQUEST_LOADING ) ) ) {
return NS_OK ;
2007-03-22 10:30:00 -07:00
}
2011-09-23 18:57:36 -07:00
// We only decode text lazily if we're also parsing to a doc.
// Also, if we've decoded all current data already, then no need to decode
// more.
2011-11-24 07:28:12 -08:00
if ( ! mResponseXML | |
2011-09-23 18:57:36 -07:00
mResponseBodyDecodedPos = = mResponseBody . Length ( ) ) {
aResponseText = mResponseText ;
return NS_OK ;
}
nsresult rv ;
nsCOMPtr < nsIDocument > document = do_QueryInterface ( mResponseXML ) ;
if ( mResponseCharset ! = document - > GetDocumentCharacterSet ( ) ) {
2011-10-07 04:33:42 -07:00
mResponseCharset = document - > GetDocumentCharacterSet ( ) ;
2011-09-23 18:57:36 -07:00
mResponseText . Truncate ( ) ;
mResponseBodyDecodedPos = 0 ;
nsCOMPtr < nsICharsetConverterManager > ccm =
do_GetService ( NS_CHARSETCONVERTERMANAGER_CONTRACTID , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = ccm - > GetUnicodeDecoderRaw ( mResponseCharset . get ( ) ,
getter_AddRefs ( mDecoder ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
NS_ASSERTION ( mResponseBodyDecodedPos < mResponseBody . Length ( ) ,
" Unexpected mResponseBodyDecodedPos " ) ;
rv = AppendToResponseText ( mResponseBody . get ( ) + mResponseBodyDecodedPos ,
mResponseBody . Length ( ) - mResponseBodyDecodedPos ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
mResponseBodyDecodedPos = mResponseBody . Length ( ) ;
if ( mState & XML_HTTP_REQUEST_DONE ) {
// Free memory buffer which we no longer need
mResponseBody . Truncate ( ) ;
mResponseBodyDecodedPos = 0 ;
}
aResponseText = mResponseText ;
return NS_OK ;
2007-03-22 10:30:00 -07:00
}
2011-09-08 15:12:18 -07:00
nsresult
nsXMLHttpRequest : : CreateResponseParsedJSON ( JSContext * aCx )
2010-08-14 10:52:19 -07:00
{
2011-09-08 15:12:18 -07:00
if ( ! aCx ) {
return NS_ERROR_FAILURE ;
}
2011-12-08 05:56:14 -08:00
// The Unicode converter has already zapped the BOM if there was one
2011-09-08 15:12:18 -07:00
if ( ! JS_ParseJSON ( aCx ,
2011-09-23 18:57:36 -07:00
( jschar * ) mResponseText . get ( ) ,
mResponseText . Length ( ) , & mResultJSON ) ) {
2010-08-14 10:52:19 -07:00
return NS_ERROR_FAILURE ;
2011-09-08 15:12:18 -07:00
}
return NS_OK ;
}
2012-01-30 02:33:59 -08:00
nsresult
nsXMLHttpRequest : : CreatePartialBlob ( )
{
if ( mDOMFile ) {
if ( mLoadTotal = = mLoadTransferred ) {
mResponseBlob = mDOMFile ;
} else {
mResponseBlob =
mDOMFile - > CreateSlice ( 0 , mLoadTransferred , EmptyString ( ) ) ;
}
return NS_OK ;
}
nsCAutoString contentType ;
if ( mLoadTotal = = mLoadTransferred ) {
mChannel - > GetContentType ( contentType ) ;
}
return mBuilder - > GetBlobInternal ( NS_ConvertASCIItoUTF16 ( contentType ) ,
false , getter_AddRefs ( mResponseBlob ) ) ;
}
2011-05-10 16:18:55 -07:00
/* attribute AString responseType; */
2011-05-18 22:11:51 -07:00
NS_IMETHODIMP nsXMLHttpRequest : : GetResponseType ( nsAString & aResponseType )
2011-05-10 16:18:55 -07:00
{
switch ( mResponseType ) {
case XML_HTTP_RESPONSE_TYPE_DEFAULT :
aResponseType . Truncate ( ) ;
break ;
case XML_HTTP_RESPONSE_TYPE_ARRAYBUFFER :
aResponseType . AssignLiteral ( " arraybuffer " ) ;
break ;
case XML_HTTP_RESPONSE_TYPE_BLOB :
aResponseType . AssignLiteral ( " blob " ) ;
break ;
case XML_HTTP_RESPONSE_TYPE_DOCUMENT :
aResponseType . AssignLiteral ( " document " ) ;
break ;
case XML_HTTP_RESPONSE_TYPE_TEXT :
aResponseType . AssignLiteral ( " text " ) ;
break ;
2011-09-08 15:12:18 -07:00
case XML_HTTP_RESPONSE_TYPE_JSON :
2011-12-08 05:56:14 -08:00
aResponseType . AssignLiteral ( " json " ) ;
2011-09-08 15:12:18 -07:00
break ;
2011-09-23 18:57:36 -07:00
case XML_HTTP_RESPONSE_TYPE_CHUNKED_TEXT :
aResponseType . AssignLiteral ( " moz-chunked-text " ) ;
break ;
case XML_HTTP_RESPONSE_TYPE_CHUNKED_ARRAYBUFFER :
aResponseType . AssignLiteral ( " moz-chunked-arraybuffer " ) ;
break ;
2012-01-30 02:33:59 -08:00
case XML_HTTP_RESPONSE_TYPE_MOZ_BLOB :
aResponseType . AssignLiteral ( " moz-blob " ) ;
break ;
2011-05-10 16:18:55 -07:00
default :
NS_ERROR ( " Should not happen " ) ;
}
return NS_OK ;
}
/* attribute AString responseType; */
2011-05-18 22:11:51 -07:00
NS_IMETHODIMP nsXMLHttpRequest : : SetResponseType ( const nsAString & aResponseType )
2011-05-10 16:18:55 -07:00
{
// If the state is not OPENED or HEADERS_RECEIVED raise an
// INVALID_STATE_ERR exception and terminate these steps.
if ( ! ( mState & ( XML_HTTP_REQUEST_OPENED | XML_HTTP_REQUEST_SENT |
XML_HTTP_REQUEST_HEADERS_RECEIVED ) ) )
return NS_ERROR_DOM_INVALID_STATE_ERR ;
2011-12-08 05:54:05 -08:00
// sync request is not allowed setting responseType in window context
if ( mOwner & &
! ( mState & ( XML_HTTP_REQUEST_UNSENT | XML_HTTP_REQUEST_ASYNC ) ) ) {
LogMessage ( " ResponseTypeSyncXHRWarning " , mOwner ) ;
return NS_ERROR_DOM_INVALID_ACCESS_ERR ;
}
2011-05-10 16:18:55 -07:00
// Set the responseType attribute's value to the given value.
if ( aResponseType . IsEmpty ( ) ) {
mResponseType = XML_HTTP_RESPONSE_TYPE_DEFAULT ;
} else if ( aResponseType . EqualsLiteral ( " arraybuffer " ) ) {
mResponseType = XML_HTTP_RESPONSE_TYPE_ARRAYBUFFER ;
} else if ( aResponseType . EqualsLiteral ( " blob " ) ) {
mResponseType = XML_HTTP_RESPONSE_TYPE_BLOB ;
} else if ( aResponseType . EqualsLiteral ( " document " ) ) {
mResponseType = XML_HTTP_RESPONSE_TYPE_DOCUMENT ;
} else if ( aResponseType . EqualsLiteral ( " text " ) ) {
mResponseType = XML_HTTP_RESPONSE_TYPE_TEXT ;
2011-12-08 05:56:14 -08:00
} else if ( aResponseType . EqualsLiteral ( " json " ) ) {
2011-09-08 15:12:18 -07:00
mResponseType = XML_HTTP_RESPONSE_TYPE_JSON ;
2011-09-23 18:57:36 -07:00
} else if ( aResponseType . EqualsLiteral ( " moz-chunked-text " ) ) {
if ( ! ( mState & XML_HTTP_REQUEST_ASYNC ) ) {
return NS_ERROR_DOM_INVALID_STATE_ERR ;
}
mResponseType = XML_HTTP_RESPONSE_TYPE_CHUNKED_TEXT ;
} else if ( aResponseType . EqualsLiteral ( " moz-chunked-arraybuffer " ) ) {
if ( ! ( mState & XML_HTTP_REQUEST_ASYNC ) ) {
return NS_ERROR_DOM_INVALID_STATE_ERR ;
}
mResponseType = XML_HTTP_RESPONSE_TYPE_CHUNKED_ARRAYBUFFER ;
2012-01-30 02:33:59 -08:00
} else if ( aResponseType . EqualsLiteral ( " moz-blob " ) ) {
mResponseType = XML_HTTP_RESPONSE_TYPE_MOZ_BLOB ;
2011-05-10 16:18:55 -07:00
}
// If the given value is not the empty string, "arraybuffer",
// "blob", "document", or "text" terminate these steps.
// If the state is OPENED, SetCacheAsFile would have no effect here
// because the channel hasn't initialized the cache entry yet.
// SetCacheAsFile will be called from OnStartRequest.
// If the state is HEADERS_RECEIVED, however, we need to call
// it immediately because OnStartRequest is already dispatched.
if ( mState & XML_HTTP_REQUEST_HEADERS_RECEIVED ) {
nsCOMPtr < nsICachingChannel > cc ( do_QueryInterface ( mChannel ) ) ;
if ( cc ) {
2012-01-30 02:33:59 -08:00
cc - > SetCacheAsFile ( mResponseType = = XML_HTTP_RESPONSE_TYPE_BLOB | |
mResponseType = = XML_HTTP_RESPONSE_TYPE_MOZ_BLOB ) ;
2011-05-10 16:18:55 -07:00
}
}
return NS_OK ;
}
/* readonly attribute jsval response; */
2011-05-18 22:11:51 -07:00
NS_IMETHODIMP nsXMLHttpRequest : : GetResponse ( JSContext * aCx , jsval * aResult )
2011-05-10 16:18:55 -07:00
{
nsresult rv = NS_OK ;
switch ( mResponseType ) {
case XML_HTTP_RESPONSE_TYPE_DEFAULT :
case XML_HTTP_RESPONSE_TYPE_TEXT :
2011-09-23 18:57:36 -07:00
case XML_HTTP_RESPONSE_TYPE_CHUNKED_TEXT :
2011-05-10 16:18:55 -07:00
{
nsString str ;
rv = GetResponseText ( str ) ;
if ( NS_FAILED ( rv ) ) return rv ;
2011-12-24 00:27:04 -08:00
NS_ENSURE_TRUE ( xpc : : StringToJsval ( aCx , str , aResult ) ,
2011-11-15 23:50:18 -08:00
NS_ERROR_OUT_OF_MEMORY ) ;
2011-05-10 16:18:55 -07:00
}
break ;
case XML_HTTP_RESPONSE_TYPE_ARRAYBUFFER :
2011-09-23 18:57:36 -07:00
case XML_HTTP_RESPONSE_TYPE_CHUNKED_ARRAYBUFFER :
if ( ( mResponseType = = XML_HTTP_RESPONSE_TYPE_ARRAYBUFFER & &
mState & XML_HTTP_REQUEST_DONE ) | |
( mResponseType = = XML_HTTP_RESPONSE_TYPE_CHUNKED_ARRAYBUFFER & &
mInLoadProgressEvent ) ) {
2011-09-08 15:12:18 -07:00
if ( ! mResultArrayBuffer ) {
2011-12-15 15:19:01 -08:00
RootResultArrayBuffer ( ) ;
rv = nsContentUtils : : CreateArrayBuffer ( aCx , mResponseBody ,
& mResultArrayBuffer ) ;
2011-05-23 18:09:28 -07:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
* aResult = OBJECT_TO_JSVAL ( mResultArrayBuffer ) ;
2011-05-10 16:18:55 -07:00
} else {
* aResult = JSVAL_NULL ;
}
break ;
case XML_HTTP_RESPONSE_TYPE_BLOB :
2012-01-30 02:33:59 -08:00
case XML_HTTP_RESPONSE_TYPE_MOZ_BLOB :
* aResult = JSVAL_NULL ;
if ( mState & XML_HTTP_REQUEST_DONE ) {
// do nothing here
} else if ( mResponseType = = XML_HTTP_RESPONSE_TYPE_MOZ_BLOB ) {
if ( ! mResponseBlob ) {
rv = CreatePartialBlob ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
} else {
return rv ;
}
if ( mResponseBlob ) {
2011-09-28 17:57:27 -07:00
JSObject * scope = JS_GetGlobalForScopeChain ( aCx ) ;
2011-05-10 16:18:55 -07:00
rv = nsContentUtils : : WrapNative ( aCx , scope , mResponseBlob , aResult ,
2011-10-17 07:59:28 -07:00
nsnull , true ) ;
2011-05-10 16:18:55 -07:00
}
break ;
case XML_HTTP_RESPONSE_TYPE_DOCUMENT :
if ( mState & XML_HTTP_REQUEST_DONE & & mResponseXML ) {
2011-09-28 17:57:27 -07:00
JSObject * scope = JS_GetGlobalForScopeChain ( aCx ) ;
2011-05-10 16:18:55 -07:00
rv = nsContentUtils : : WrapNative ( aCx , scope , mResponseXML , aResult ,
2011-10-17 07:59:28 -07:00
nsnull , true ) ;
2011-05-10 16:18:55 -07:00
} else {
* aResult = JSVAL_NULL ;
}
break ;
2011-09-08 15:12:18 -07:00
case XML_HTTP_RESPONSE_TYPE_JSON :
if ( mState & XML_HTTP_REQUEST_DONE ) {
if ( mResultJSON = = JSVAL_VOID ) {
rv = CreateResponseParsedJSON ( aCx ) ;
2011-09-23 18:57:36 -07:00
mResponseText . Truncate ( ) ;
2011-12-08 05:56:14 -08:00
if ( NS_FAILED ( rv ) ) {
// Per spec, errors aren't propagated. null is returned instead.
rv = NS_OK ;
// It would be nice to log the error to the console. That's hard to
// do without calling window.onerror as a side effect, though.
JS_ClearPendingException ( aCx ) ;
mResultJSON = JSVAL_NULL ;
}
2011-09-08 15:12:18 -07:00
}
* aResult = mResultJSON ;
} else {
* aResult = JSVAL_NULL ;
}
break ;
2011-05-10 16:18:55 -07:00
default :
NS_ERROR ( " Should not happen " ) ;
}
return rv ;
}
2007-03-22 10:30:00 -07:00
/* readonly attribute unsigned long status; */
NS_IMETHODIMP
nsXMLHttpRequest : : GetStatus ( PRUint32 * aStatus )
{
2008-09-30 17:50:42 -07:00
* aStatus = 0 ;
if ( mState & XML_HTTP_REQUEST_USE_XSITE_AC ) {
// Make sure we don't leak status information from denied cross-site
// requests.
if ( mChannel ) {
nsresult status ;
mChannel - > GetStatus ( & status ) ;
if ( NS_FAILED ( status ) ) {
return NS_OK ;
}
}
}
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsIHttpChannel > httpChannel = GetCurrentHttpChannel ( ) ;
if ( httpChannel ) {
nsresult rv = httpChannel - > GetResponseStatus ( aStatus ) ;
if ( rv = = NS_ERROR_NOT_AVAILABLE ) {
// Someone's calling this before we got a response... Check our
// ReadyState. If we're at 3 or 4, then this means the connection
2008-03-21 22:09:41 -07:00
// errored before we got any data; return 0 in that case.
2011-05-10 16:18:55 -07:00
PRUint16 readyState ;
2007-03-22 10:30:00 -07:00
GetReadyState ( & readyState ) ;
2011-05-10 16:18:55 -07:00
if ( readyState > = LOADING ) {
2008-03-21 22:09:41 -07:00
* aStatus = 0 ;
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
}
return rv ;
}
return NS_OK ;
}
/* readonly attribute AUTF8String statusText; */
NS_IMETHODIMP
nsXMLHttpRequest : : GetStatusText ( nsACString & aStatusText )
{
nsCOMPtr < nsIHttpChannel > httpChannel = GetCurrentHttpChannel ( ) ;
aStatusText . Truncate ( ) ;
if ( httpChannel ) {
2010-03-16 16:23:45 -07:00
if ( mState & XML_HTTP_REQUEST_USE_XSITE_AC ) {
// Make sure we don't leak status information from denied cross-site
// requests.
if ( mChannel ) {
nsresult status ;
mChannel - > GetStatus ( & status ) ;
if ( NS_FAILED ( status ) ) {
2010-11-11 13:39:14 -08:00
return NS_OK ;
2010-03-16 16:23:45 -07:00
}
}
}
2010-11-11 13:39:14 -08:00
httpChannel - > GetResponseStatusText ( aStatusText ) ;
2007-03-22 10:30:00 -07:00
}
2010-11-11 13:39:14 -08:00
return NS_OK ;
2007-03-22 10:30:00 -07:00
}
2012-01-26 02:02:22 -08:00
void
nsXMLHttpRequest : : CloseRequestWithError ( const nsAString & aType ,
const PRUint32 aFlag )
2007-03-22 10:30:00 -07:00
{
if ( mReadRequest ) {
mReadRequest - > Cancel ( NS_BINDING_ABORTED ) ;
}
if ( mChannel ) {
mChannel - > Cancel ( NS_BINDING_ABORTED ) ;
}
2011-03-28 13:18:45 -07:00
if ( mCORSPreflightChannel ) {
mCORSPreflightChannel - > Cancel ( NS_BINDING_ABORTED ) ;
2008-09-30 17:49:30 -07:00
}
2012-01-26 02:02:22 -08:00
if ( mTimeoutTimer ) {
mTimeoutTimer - > Cancel ( ) ;
}
2008-10-19 14:26:37 -07:00
PRUint32 responseLength = mResponseBody . Length ( ) ;
2011-09-23 18:57:22 -07:00
ResetResponse ( ) ;
2012-01-26 02:02:22 -08:00
mState | = aFlag ;
2012-02-14 10:05:43 -08:00
// If we're in the destructor, don't risk dispatching an event.
2012-02-20 12:02:47 -08:00
if ( mState & XML_HTTP_REQUEST_DELETED ) {
mState & = ~ XML_HTTP_REQUEST_SYNCLOOPING ;
2012-02-14 10:05:43 -08:00
return ;
2012-02-20 12:02:47 -08:00
}
2012-02-14 10:05:43 -08:00
2011-05-10 16:18:55 -07:00
if ( ! ( mState & ( XML_HTTP_REQUEST_UNSENT |
2008-02-06 21:03:01 -08:00
XML_HTTP_REQUEST_OPENED |
2011-05-10 16:18:55 -07:00
XML_HTTP_REQUEST_DONE ) ) ) {
2011-10-17 07:59:28 -07:00
ChangeState ( XML_HTTP_REQUEST_DONE , true ) ;
2007-03-22 10:30:00 -07:00
2011-11-21 10:07:12 -08:00
if ( ! ( mState & XML_HTTP_REQUEST_SYNCLOOPING ) ) {
2012-01-26 02:02:22 -08:00
DispatchProgressEvent ( this , aType , mLoadLengthComputable , responseLength ,
2011-11-21 10:07:12 -08:00
mLoadTotal ) ;
if ( mUpload & & ! mUploadComplete ) {
mUploadComplete = true ;
2012-01-26 02:02:22 -08:00
DispatchProgressEvent ( mUpload , aType , true , mUploadTransferred ,
2011-11-21 10:07:12 -08:00
mUploadTotal ) ;
}
2008-08-14 04:07:46 -07:00
}
}
2007-03-22 10:30:00 -07:00
// The ChangeState call above calls onreadystatechange handlers which
2011-03-28 13:05:52 -07:00
// if they load a new url will cause nsXMLHttpRequest::Open to clear
2007-03-22 10:30:00 -07:00
// the abort state bit. If this occurs we're not uninitialized (bug 361773).
if ( mState & XML_HTTP_REQUEST_ABORTED ) {
2011-10-17 07:59:28 -07:00
ChangeState ( XML_HTTP_REQUEST_UNSENT , false ) ; // IE seems to do it
2007-03-22 10:30:00 -07:00
}
2008-08-25 10:21:28 -07:00
mState & = ~ XML_HTTP_REQUEST_SYNCLOOPING ;
2012-01-26 02:02:22 -08:00
}
2008-08-25 10:21:28 -07:00
2012-01-26 02:02:22 -08:00
/* void abort (); */
NS_IMETHODIMP
nsXMLHttpRequest : : Abort ( )
{
CloseRequestWithError ( NS_LITERAL_STRING ( ABORT_STR ) , XML_HTTP_REQUEST_ABORTED ) ;
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
2012-02-21 01:34:01 -08:00
/* DOMString getAllResponseHeaders(); */
2007-03-22 10:30:00 -07:00
NS_IMETHODIMP
2012-02-21 01:34:01 -08:00
nsXMLHttpRequest : : GetAllResponseHeaders ( nsAString & aResponseHeaders )
2007-03-22 10:30:00 -07:00
{
2012-02-21 01:34:01 -08:00
aResponseHeaders . Truncate ( ) ;
2007-03-22 10:30:00 -07:00
2012-02-19 03:58:24 -08:00
// If the state is UNSENT or OPENED,
// return the empty string and terminate these steps.
if ( mState & ( XML_HTTP_REQUEST_UNSENT |
XML_HTTP_REQUEST_OPENED | XML_HTTP_REQUEST_SENT ) ) {
return NS_OK ;
}
2008-09-30 17:49:30 -07:00
if ( mState & XML_HTTP_REQUEST_USE_XSITE_AC ) {
return NS_OK ;
}
2012-02-21 01:34:01 -08:00
if ( nsCOMPtr < nsIHttpChannel > httpChannel = GetCurrentHttpChannel ( ) ) {
2010-11-11 13:39:14 -08:00
nsRefPtr < nsHeaderVisitor > visitor = new nsHeaderVisitor ( ) ;
2012-02-21 01:34:01 -08:00
if ( NS_SUCCEEDED ( httpChannel - > VisitResponseHeaders ( visitor ) ) ) {
aResponseHeaders = NS_ConvertUTF8toUTF16 ( visitor - > Headers ( ) ) ;
2012-02-19 03:58:24 -08:00
}
2012-02-21 01:34:01 -08:00
return NS_OK ;
2010-11-11 13:39:14 -08:00
}
2012-02-19 03:58:24 -08:00
2012-02-21 01:34:01 -08:00
if ( ! mChannel ) {
return NS_OK ;
2007-03-22 10:30:00 -07:00
}
2012-02-21 01:34:01 -08:00
// Even non-http channels supply content type.
nsCAutoString value ;
if ( NS_SUCCEEDED ( mChannel - > GetContentType ( value ) ) ) {
aResponseHeaders . AppendLiteral ( " Content-Type: " ) ;
aResponseHeaders . Append ( NS_ConvertUTF8toUTF16 ( value ) ) ;
if ( NS_SUCCEEDED ( mChannel - > GetContentCharset ( value ) ) & &
! value . IsEmpty ( ) ) {
aResponseHeaders . AppendLiteral ( " ;charset= " ) ;
aResponseHeaders . Append ( NS_ConvertUTF8toUTF16 ( value ) ) ;
}
aResponseHeaders . Append ( ' \n ' ) ;
}
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
/* ACString getResponseHeader (in AUTF8String header); */
NS_IMETHODIMP
nsXMLHttpRequest : : GetResponseHeader ( const nsACString & header ,
nsACString & _retval )
{
nsresult rv = NS_OK ;
2011-10-17 07:59:28 -07:00
_retval . SetIsVoid ( true ) ;
2010-10-04 18:25:44 -07:00
nsCOMPtr < nsIHttpChannel > httpChannel = GetCurrentHttpChannel ( ) ;
if ( ! httpChannel ) {
2012-02-19 03:58:24 -08:00
// If the state is UNSENT or OPENED,
// return null and terminate these steps.
if ( mState & ( XML_HTTP_REQUEST_UNSENT |
XML_HTTP_REQUEST_OPENED | XML_HTTP_REQUEST_SENT ) ) {
return NS_OK ;
}
// Even non-http channels supply content type.
// Remember we don't leak header information from denied cross-site
// requests.
nsresult status ;
if ( ! mChannel | |
NS_FAILED ( mChannel - > GetStatus ( & status ) ) | |
NS_FAILED ( status ) | |
! header . LowerCaseEqualsASCII ( " content-type " ) ) {
return NS_OK ;
}
if ( NS_FAILED ( mChannel - > GetContentType ( _retval ) ) ) {
// Means no content type
_retval . SetIsVoid ( true ) ;
return NS_OK ;
}
nsCString value ;
if ( NS_SUCCEEDED ( mChannel - > GetContentCharset ( value ) ) & &
! value . IsEmpty ( ) ) {
_retval . Append ( " ;charset= " ) ;
_retval . Append ( value ) ;
}
2010-10-04 18:25:44 -07:00
return NS_OK ;
}
2007-03-22 10:30:00 -07:00
2008-11-13 18:18:01 -08:00
// See bug #380418. Hide "Set-Cookie" headers from non-chrome scripts.
2011-09-28 23:19:26 -07:00
bool chrome = false ; // default to false in case IsCapabilityEnabled fails
2008-11-13 18:18:01 -08:00
IsCapabilityEnabled ( " UniversalXPConnect " , & chrome ) ;
if ( ! chrome & &
( header . LowerCaseEqualsASCII ( " set-cookie " ) | |
header . LowerCaseEqualsASCII ( " set-cookie2 " ) ) ) {
NS_WARNING ( " blocked access to response header " ) ;
return NS_OK ;
}
2008-09-30 17:49:30 -07:00
// Check for dangerous headers
if ( mState & XML_HTTP_REQUEST_USE_XSITE_AC ) {
// Make sure we don't leak header information from denied cross-site
// requests.
if ( mChannel ) {
nsresult status ;
mChannel - > GetStatus ( & status ) ;
if ( NS_FAILED ( status ) ) {
return NS_OK ;
}
}
const char * kCrossOriginSafeHeaders [ ] = {
" cache-control " , " content-language " , " content-type " , " expires " ,
" last-modified " , " pragma "
} ;
2011-09-28 23:19:26 -07:00
bool safeHeader = false ;
2008-09-30 17:49:30 -07:00
PRUint32 i ;
2011-10-10 22:50:08 -07:00
for ( i = 0 ; i < ArrayLength ( kCrossOriginSafeHeaders ) ; + + i ) {
2008-09-30 17:49:30 -07:00
if ( header . LowerCaseEqualsASCII ( kCrossOriginSafeHeaders [ i ] ) ) {
2011-10-17 07:59:28 -07:00
safeHeader = true ;
2008-09-30 17:49:30 -07:00
break ;
}
}
if ( ! safeHeader ) {
2010-10-04 18:25:44 -07:00
nsCAutoString headerVal ;
// The "Access-Control-Expose-Headers" header contains a comma separated
// list of method names.
httpChannel - >
GetResponseHeader ( NS_LITERAL_CSTRING ( " Access-Control-Expose-Headers " ) ,
headerVal ) ;
nsCCharSeparatedTokenizer exposeTokens ( headerVal , ' , ' ) ;
while ( exposeTokens . hasMoreTokens ( ) ) {
const nsDependentCSubstring & token = exposeTokens . nextToken ( ) ;
if ( token . IsEmpty ( ) ) {
continue ;
}
if ( ! IsValidHTTPToken ( token ) ) {
return NS_OK ;
}
if ( header . Equals ( token , nsCaseInsensitiveCStringComparator ( ) ) ) {
2011-10-17 07:59:28 -07:00
safeHeader = true ;
2010-10-04 18:25:44 -07:00
}
}
2008-09-30 17:49:30 -07:00
}
2007-03-22 10:30:00 -07:00
2010-10-04 18:25:44 -07:00
if ( ! safeHeader ) {
return NS_OK ;
}
2007-03-22 10:30:00 -07:00
}
2010-10-04 18:25:44 -07:00
rv = httpChannel - > GetResponseHeader ( header , _retval ) ;
2007-03-22 10:30:00 -07:00
if ( rv = = NS_ERROR_NOT_AVAILABLE ) {
// Means no header
2011-10-17 07:59:28 -07:00
_retval . SetIsVoid ( true ) ;
2007-03-22 10:30:00 -07:00
rv = NS_OK ;
}
return rv ;
}
2011-08-24 10:46:53 -07:00
already_AddRefed < nsILoadGroup >
nsXMLHttpRequest : : GetLoadGroup ( ) const
2008-10-14 17:12:28 -07:00
{
if ( mState & XML_HTTP_REQUEST_BACKGROUND ) {
2011-08-24 10:46:53 -07:00
return nsnull ;
2008-10-14 17:12:28 -07:00
}
2009-06-15 01:27:29 -07:00
nsCOMPtr < nsIDocument > doc =
nsContentUtils : : GetDocumentFromScriptContext ( mScriptContext ) ;
2008-10-14 17:12:28 -07:00
if ( doc ) {
2011-08-24 10:46:53 -07:00
return doc - > GetDocumentLoadGroup ( ) ;
2008-10-14 17:12:28 -07:00
}
2011-08-24 10:46:53 -07:00
return nsnull ;
2008-10-14 17:12:28 -07:00
}
2007-03-22 10:30:00 -07:00
nsresult
2008-08-14 04:07:46 -07:00
nsXMLHttpRequest : : CreateReadystatechangeEvent ( nsIDOMEvent * * aDOMEvent )
2007-03-22 10:30:00 -07:00
{
nsresult rv = nsEventDispatcher : : CreateEvent ( nsnull , nsnull ,
NS_LITERAL_STRING ( " Events " ) ,
aDOMEvent ) ;
if ( NS_FAILED ( rv ) ) {
return rv ;
}
nsCOMPtr < nsIPrivateDOMEvent > privevent ( do_QueryInterface ( * aDOMEvent ) ) ;
if ( ! privevent ) {
NS_IF_RELEASE ( * aDOMEvent ) ;
return NS_ERROR_FAILURE ;
}
2008-08-14 04:07:46 -07:00
( * aDOMEvent ) - > InitEvent ( NS_LITERAL_STRING ( READYSTATE_STR ) ,
2011-10-17 07:59:28 -07:00
false , false ) ;
2007-03-22 10:30:00 -07:00
2008-08-14 04:07:46 -07:00
// We assume anyone who managed to call CreateReadystatechangeEvent is trusted
2011-10-17 07:59:28 -07:00
privevent - > SetTrusted ( true ) ;
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
2008-08-14 04:07:46 -07:00
void
2011-06-23 19:17:58 -07:00
nsXMLHttpRequest : : DispatchProgressEvent ( nsDOMEventTargetHelper * aTarget ,
2008-08-14 04:07:46 -07:00
const nsAString & aType ,
2011-09-28 23:19:26 -07:00
bool aUseLSEventWrapper ,
bool aLengthComputable ,
2008-08-14 04:07:46 -07:00
PRUint64 aLoaded , PRUint64 aTotal ,
PRUint64 aPosition , PRUint64 aTotalSize )
{
2008-09-30 17:51:53 -07:00
NS_ASSERTION ( aTarget , " null target " ) ;
2011-09-23 18:57:22 -07:00
NS_ASSERTION ( ! aType . IsEmpty ( ) , " missing event type " ) ;
if ( NS_FAILED ( CheckInnerWindowCorrectness ( ) ) | |
2008-09-30 17:51:53 -07:00
( ! AllowUploadProgress ( ) & &
( aTarget = = mUpload | | aType . EqualsLiteral ( UPLOADPROGRESS_STR ) ) ) ) {
2008-08-14 04:07:46 -07:00
return ;
}
2011-09-28 23:19:26 -07:00
bool dispatchLoadend = aType . EqualsLiteral ( LOAD_STR ) | |
2011-03-24 06:22:03 -07:00
aType . EqualsLiteral ( ERROR_STR ) | |
2012-01-26 02:02:22 -08:00
aType . EqualsLiteral ( TIMEOUT_STR ) | |
2011-03-24 06:22:03 -07:00
aType . EqualsLiteral ( ABORT_STR ) ;
2008-08-14 04:07:46 -07:00
nsCOMPtr < nsIDOMEvent > event ;
nsresult rv = nsEventDispatcher : : CreateEvent ( nsnull , nsnull ,
NS_LITERAL_STRING ( " ProgressEvent " ) ,
getter_AddRefs ( event ) ) ;
if ( NS_FAILED ( rv ) ) {
return ;
}
nsCOMPtr < nsIPrivateDOMEvent > privevent ( do_QueryInterface ( event ) ) ;
if ( ! privevent ) {
return ;
}
2011-10-17 07:59:28 -07:00
privevent - > SetTrusted ( true ) ;
2008-08-14 04:07:46 -07:00
nsCOMPtr < nsIDOMProgressEvent > progress = do_QueryInterface ( event ) ;
if ( ! progress ) {
return ;
}
2011-10-17 07:59:28 -07:00
progress - > InitProgressEvent ( aType , false , false , aLengthComputable ,
2008-08-14 04:07:46 -07:00
aLoaded , ( aTotal = = LL_MAXUINT ) ? 0 : aTotal ) ;
if ( aUseLSEventWrapper ) {
nsCOMPtr < nsIDOMProgressEvent > xhrprogressEvent =
2011-10-01 09:14:40 -07:00
new nsXMLHttpProgressEvent ( progress , aPosition , aTotalSize , mOwner ) ;
2008-08-14 04:07:46 -07:00
event = xhrprogressEvent ;
}
aTarget - > DispatchDOMEvent ( nsnull , event , nsnull , nsnull ) ;
2011-03-24 06:22:03 -07:00
if ( dispatchLoadend ) {
DispatchProgressEvent ( aTarget , NS_LITERAL_STRING ( LOADEND_STR ) ,
aUseLSEventWrapper , aLengthComputable ,
aLoaded , aTotal , aPosition , aTotalSize ) ;
}
2008-08-14 04:07:46 -07:00
}
2011-03-24 06:22:03 -07:00
2007-03-22 10:30:00 -07:00
already_AddRefed < nsIHttpChannel >
nsXMLHttpRequest : : GetCurrentHttpChannel ( )
{
nsIHttpChannel * httpChannel = nsnull ;
if ( mReadRequest ) {
CallQueryInterface ( mReadRequest , & httpChannel ) ;
}
if ( ! httpChannel & & mChannel ) {
CallQueryInterface ( mChannel , & httpChannel ) ;
}
return httpChannel ;
}
2011-08-11 06:29:50 -07:00
bool
nsXMLHttpRequest : : IsSystemXHR ( )
{
return ! ! nsContentUtils : : IsSystemPrincipal ( mPrincipal ) ;
}
2008-09-30 17:49:30 -07:00
nsresult
nsXMLHttpRequest : : CheckChannelForCrossSiteRequest ( nsIChannel * aChannel )
{
2011-03-17 09:19:13 -07:00
// First check if cross-site requests are enabled...
if ( IsSystemXHR ( ) ) {
2010-06-17 04:27:52 -07:00
return NS_OK ;
}
2011-03-17 09:19:13 -07:00
// ...or if this is a same-origin request.
2010-06-17 04:27:52 -07:00
if ( nsContentUtils : : CheckMayLoad ( mPrincipal , aChannel ) ) {
2008-09-30 17:49:30 -07:00
return NS_OK ;
}
2012-01-11 01:47:46 -08:00
// exempt data URIs from the same origin check.
nsCOMPtr < nsIURI > channelURI ;
bool dataScheme = false ;
if ( NS_SUCCEEDED ( NS_GetFinalChannelURI ( aChannel ,
getter_AddRefs ( channelURI ) ) ) & &
NS_SUCCEEDED ( channelURI - > SchemeIs ( " data " , & dataScheme ) ) & &
dataScheme ) {
return NS_OK ;
}
2008-09-30 17:49:30 -07:00
// This is a cross-site request
mState | = XML_HTTP_REQUEST_USE_XSITE_AC ;
2009-02-24 11:46:51 -08:00
// Check if we need to do a preflight request.
nsCOMPtr < nsIHttpChannel > httpChannel = do_QueryInterface ( aChannel ) ;
NS_ENSURE_TRUE ( httpChannel , NS_ERROR_DOM_BAD_URI ) ;
2012-01-11 01:47:46 -08:00
2009-02-24 11:46:51 -08:00
nsCAutoString method ;
httpChannel - > GetRequestMethod ( method ) ;
2011-03-28 13:18:45 -07:00
if ( ! mCORSUnsafeHeaders . IsEmpty ( ) | |
2009-02-24 11:46:51 -08:00
HasListenersFor ( NS_LITERAL_STRING ( UPLOADPROGRESS_STR ) ) | |
2010-10-04 17:41:07 -07:00
( mUpload & & mUpload - > HasListeners ( ) ) | |
( ! method . LowerCaseEqualsLiteral ( " get " ) & &
! method . LowerCaseEqualsLiteral ( " post " ) & &
! method . LowerCaseEqualsLiteral ( " head " ) ) ) {
2009-02-24 11:46:51 -08:00
mState | = XML_HTTP_REQUEST_NEED_AC_PREFLIGHT ;
}
2008-09-30 17:49:30 -07:00
return NS_OK ;
}
2007-03-22 10:30:00 -07:00
NS_IMETHODIMP
2011-03-28 13:05:52 -07:00
nsXMLHttpRequest : : Open ( const nsACString & method , const nsACString & url ,
2011-09-28 23:19:26 -07:00
bool async , const nsAString & user ,
2011-03-28 13:05:52 -07:00
const nsAString & password , PRUint8 optional_argc )
2007-03-22 10:30:00 -07:00
{
NS_ENSURE_ARG ( ! method . IsEmpty ( ) ) ;
2011-03-28 13:05:52 -07:00
if ( ! optional_argc ) {
// No optional arguments were passed in. Default async to true.
2011-10-17 07:59:28 -07:00
async = true ;
2011-03-28 13:05:52 -07:00
}
2011-11-19 10:50:17 -08:00
Telemetry : : Accumulate ( Telemetry : : XMLHTTPREQUEST_ASYNC_OR_SYNC ,
async ? 0 : 1 ) ;
2011-03-28 13:05:52 -07:00
2008-02-19 15:12:23 -08:00
NS_ENSURE_TRUE ( mPrincipal , NS_ERROR_NOT_INITIALIZED ) ;
2007-06-26 02:28:38 -07:00
// Disallow HTTP/1.1 TRACE method (see bug 302489)
// and MS IIS equivalent TRACK (see bug 381264)
2007-07-26 19:49:18 -07:00
if ( method . LowerCaseEqualsLiteral ( " trace " ) | |
method . LowerCaseEqualsLiteral ( " track " ) ) {
2007-03-22 10:30:00 -07:00
return NS_ERROR_INVALID_ARG ;
}
2011-12-08 05:54:05 -08:00
// sync request is not allowed using withCredential or responseType
// in window context
if ( ! async & & mOwner & &
( mState & XML_HTTP_REQUEST_AC_WITH_CREDENTIALS | |
2012-01-26 02:02:22 -08:00
mTimeoutMilliseconds | |
2011-12-08 05:54:05 -08:00
mResponseType ! = XML_HTTP_RESPONSE_TYPE_DEFAULT ) ) {
if ( mState & XML_HTTP_REQUEST_AC_WITH_CREDENTIALS ) {
LogMessage ( " WithCredentialsSyncXHRWarning " , mOwner ) ;
}
2012-01-26 02:02:22 -08:00
if ( mTimeoutMilliseconds ) {
LogMessage ( " TimeoutSyncXHRWarning " , mOwner ) ;
}
2011-12-08 05:54:05 -08:00
if ( mResponseType ! = XML_HTTP_RESPONSE_TYPE_DEFAULT ) {
LogMessage ( " ResponseTypeSyncXHRWarning " , mOwner ) ;
}
return NS_ERROR_DOM_INVALID_ACCESS_ERR ;
}
2007-03-22 10:30:00 -07:00
nsresult rv ;
nsCOMPtr < nsIURI > uri ;
2007-11-15 14:18:36 -08:00
if ( mState & ( XML_HTTP_REQUEST_OPENED |
2011-05-10 16:18:55 -07:00
XML_HTTP_REQUEST_HEADERS_RECEIVED |
XML_HTTP_REQUEST_LOADING |
2007-11-15 14:18:36 -08:00
XML_HTTP_REQUEST_SENT |
XML_HTTP_REQUEST_STOPPED ) ) {
2007-03-22 10:30:00 -07:00
// IE aborts as well
Abort ( ) ;
// XXX We should probably send a warning to the JS console
// that load was aborted and event listeners were cleared
// since this looks like a situation that could happen
// by accident and you could spend a lot of time wondering
// why things didn't work.
2007-11-15 14:18:36 -08:00
}
2007-03-22 10:30:00 -07:00
2012-01-26 02:02:22 -08:00
// Unset any pre-existing aborted and timed-out states.
mState & = ~ XML_HTTP_REQUEST_ABORTED & ~ XML_HTTP_REQUEST_TIMED_OUT ;
2007-03-22 10:30:00 -07:00
if ( async ) {
mState | = XML_HTTP_REQUEST_ASYNC ;
} else {
mState & = ~ XML_HTTP_REQUEST_ASYNC ;
}
2008-09-11 03:44:15 -07:00
mState & = ~ XML_HTTP_REQUEST_MPART_HEADERS ;
2009-06-15 01:27:29 -07:00
nsCOMPtr < nsIDocument > doc =
nsContentUtils : : GetDocumentFromScriptContext ( mScriptContext ) ;
2008-10-11 23:30:14 -07:00
nsCOMPtr < nsIURI > baseURI ;
2008-12-30 14:24:58 -08:00
if ( mBaseURI ) {
baseURI = mBaseURI ;
}
else if ( doc ) {
2008-10-11 23:30:14 -07:00
baseURI = doc - > GetBaseURI ( ) ;
}
rv = NS_NewURI ( getter_AddRefs ( uri ) , url , nsnull , baseURI ) ;
2007-03-22 10:30:00 -07:00
if ( NS_FAILED ( rv ) ) return rv ;
2007-03-24 22:35:39 -07:00
// mScriptContext should be initialized because of GetBaseURI() above.
// Still need to consider the case that doc is nsnull however.
2007-12-12 00:33:32 -08:00
rv = CheckInnerWindowCorrectness ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-03-24 22:35:39 -07:00
PRInt16 shouldLoad = nsIContentPolicy : : ACCEPT ;
2007-06-17 06:50:50 -07:00
rv = NS_CheckContentLoadPolicy ( nsIContentPolicy : : TYPE_XMLHTTPREQUEST ,
2007-03-24 22:35:39 -07:00
uri ,
2007-12-12 00:33:32 -08:00
mPrincipal ,
2007-03-24 22:35:39 -07:00
doc ,
EmptyCString ( ) , //mime guess
nsnull , //extra
2007-08-07 18:16:09 -07:00
& shouldLoad ,
nsContentUtils : : GetContentPolicy ( ) ,
nsContentUtils : : GetSecurityManager ( ) ) ;
2007-03-24 22:35:39 -07:00
if ( NS_FAILED ( rv ) ) return rv ;
if ( NS_CP_REJECTED ( shouldLoad ) ) {
// Disallowed by content policy
return NS_ERROR_CONTENT_BLOCKED ;
}
2007-03-22 10:30:00 -07:00
if ( ! user . IsEmpty ( ) ) {
nsCAutoString userpass ;
CopyUTF16toUTF8 ( user , userpass ) ;
if ( ! password . IsEmpty ( ) ) {
userpass . Append ( ' : ' ) ;
AppendUTF16toUTF8 ( password , userpass ) ;
}
uri - > SetUserPass ( userpass ) ;
}
2008-10-14 17:12:28 -07:00
// When we are called from JS we can find the load group for the page,
// and add ourselves to it. This way any pending requests
// will be automatically aborted if the user leaves the page.
2011-08-24 10:46:53 -07:00
nsCOMPtr < nsILoadGroup > loadGroup = GetLoadGroup ( ) ;
2007-03-22 10:30:00 -07:00
2010-04-23 12:54:09 -07:00
// get Content Security Policy from principal to pass into channel
nsCOMPtr < nsIChannelPolicy > channelPolicy ;
nsCOMPtr < nsIContentSecurityPolicy > csp ;
2010-04-26 12:14:49 -07:00
rv = mPrincipal - > GetCsp ( getter_AddRefs ( csp ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2010-04-23 12:54:09 -07:00
if ( csp ) {
channelPolicy = do_CreateInstance ( " @mozilla.org/nschannelpolicy;1 " ) ;
channelPolicy - > SetContentSecurityPolicy ( csp ) ;
channelPolicy - > SetLoadType ( nsIContentPolicy : : TYPE_XMLHTTPREQUEST ) ;
}
rv = NS_NewChannel ( getter_AddRefs ( mChannel ) ,
uri ,
nsnull , // ioService
loadGroup ,
nsnull , // callbacks
2010-09-08 10:42:08 -07:00
nsIRequest : : LOAD_BACKGROUND ,
2010-04-23 12:54:09 -07:00
channelPolicy ) ;
2008-10-14 17:12:28 -07:00
if ( NS_FAILED ( rv ) ) return rv ;
2007-03-22 10:30:00 -07:00
2009-02-24 11:46:51 -08:00
mState & = ~ ( XML_HTTP_REQUEST_USE_XSITE_AC |
XML_HTTP_REQUEST_NEED_AC_PREFLIGHT ) ;
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsIHttpChannel > httpChannel ( do_QueryInterface ( mChannel ) ) ;
if ( httpChannel ) {
rv = httpChannel - > SetRequestMethod ( method ) ;
2007-07-26 19:49:18 -07:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-03-22 10:30:00 -07:00
}
ChangeState ( XML_HTTP_REQUEST_OPENED ) ;
return rv ;
}
/*
* " Copy " from a stream .
*/
NS_METHOD
nsXMLHttpRequest : : StreamReaderFunc ( nsIInputStream * in ,
void * closure ,
const char * fromRawSegment ,
PRUint32 toOffset ,
PRUint32 count ,
PRUint32 * writeCount )
{
2007-07-08 00:08:04 -07:00
nsXMLHttpRequest * xmlHttpRequest = static_cast < nsXMLHttpRequest * > ( closure ) ;
2007-03-22 10:30:00 -07:00
if ( ! xmlHttpRequest | | ! writeCount ) {
NS_WARNING ( " XMLHttpRequest cannot read from stream: no closure or writeCount " ) ;
return NS_ERROR_FAILURE ;
}
2012-01-30 02:33:59 -08:00
nsresult rv = NS_OK ;
if ( xmlHttpRequest - > mResponseType = = XML_HTTP_RESPONSE_TYPE_BLOB | |
xmlHttpRequest - > mResponseType = = XML_HTTP_RESPONSE_TYPE_MOZ_BLOB ) {
if ( ! xmlHttpRequest - > mDOMFile ) {
if ( ! xmlHttpRequest - > mBuilder ) {
xmlHttpRequest - > mBuilder = new nsDOMBlobBuilder ( ) ;
}
rv = xmlHttpRequest - > mBuilder - > AppendVoidPtr ( fromRawSegment , count ) ;
}
// Clear the cache so that the blob size is updated.
if ( xmlHttpRequest - > mResponseType = = XML_HTTP_RESPONSE_TYPE_MOZ_BLOB ) {
xmlHttpRequest - > mResponseBlob = nsnull ;
}
if ( NS_SUCCEEDED ( rv ) ) {
* writeCount = count ;
}
return rv ;
2011-05-10 16:18:55 -07:00
}
2011-09-23 18:57:36 -07:00
if ( ( xmlHttpRequest - > mResponseType = = XML_HTTP_RESPONSE_TYPE_DEFAULT & &
xmlHttpRequest - > mResponseXML ) | |
xmlHttpRequest - > mResponseType = = XML_HTTP_RESPONSE_TYPE_ARRAYBUFFER | |
2011-09-23 18:57:36 -07:00
xmlHttpRequest - > mResponseType = = XML_HTTP_RESPONSE_TYPE_CHUNKED_ARRAYBUFFER ) {
2011-05-10 16:18:55 -07:00
// Copy for our own use
PRUint32 previousLength = xmlHttpRequest - > mResponseBody . Length ( ) ;
xmlHttpRequest - > mResponseBody . Append ( fromRawSegment , count ) ;
if ( count > 0 & & xmlHttpRequest - > mResponseBody . Length ( ) = = previousLength ) {
return NS_ERROR_OUT_OF_MEMORY ;
}
2011-09-23 18:57:36 -07:00
} else if ( xmlHttpRequest - > mResponseType = = XML_HTTP_RESPONSE_TYPE_DEFAULT | |
xmlHttpRequest - > mResponseType = = XML_HTTP_RESPONSE_TYPE_TEXT | |
xmlHttpRequest - > mResponseType = = XML_HTTP_RESPONSE_TYPE_JSON | |
xmlHttpRequest - > mResponseType = = XML_HTTP_RESPONSE_TYPE_CHUNKED_TEXT ) {
2011-09-23 18:57:36 -07:00
NS_ASSERTION ( ! xmlHttpRequest - > mResponseXML ,
" We shouldn't be parsing a doc here " ) ;
xmlHttpRequest - > AppendToResponseText ( fromRawSegment , count ) ;
2011-05-10 16:18:55 -07:00
}
2007-03-22 10:30:00 -07:00
if ( xmlHttpRequest - > mState & XML_HTTP_REQUEST_PARSEBODY ) {
// Give the same data to the parser.
// We need to wrap the data in a new lightweight stream and pass that
// to the parser, because calling ReadSegments() recursively on the same
// stream is not supported.
nsCOMPtr < nsIInputStream > copyStream ;
rv = NS_NewByteInputStream ( getter_AddRefs ( copyStream ) , fromRawSegment , count ) ;
2009-06-15 03:11:04 -07:00
if ( NS_SUCCEEDED ( rv ) & & xmlHttpRequest - > mXMLParserStreamListener ) {
2007-03-22 10:30:00 -07:00
NS_ASSERTION ( copyStream , " NS_NewByteInputStream lied " ) ;
nsresult parsingResult = xmlHttpRequest - > mXMLParserStreamListener
- > OnDataAvailable ( xmlHttpRequest - > mReadRequest ,
xmlHttpRequest - > mContext ,
copyStream , toOffset , count ) ;
// No use to continue parsing if we failed here, but we
// should still finish reading the stream
if ( NS_FAILED ( parsingResult ) ) {
xmlHttpRequest - > mState & = ~ XML_HTTP_REQUEST_PARSEBODY ;
}
}
}
if ( NS_SUCCEEDED ( rv ) ) {
* writeCount = count ;
} else {
* writeCount = 0 ;
}
return rv ;
}
2012-01-30 02:33:59 -08:00
bool nsXMLHttpRequest : : CreateDOMFile ( nsIRequest * request )
2011-05-10 16:18:55 -07:00
{
nsCOMPtr < nsIFile > file ;
nsCOMPtr < nsICachingChannel > cc ( do_QueryInterface ( request ) ) ;
if ( cc ) {
cc - > GetCacheFile ( getter_AddRefs ( file ) ) ;
} else {
nsCOMPtr < nsIFileChannel > fc = do_QueryInterface ( request ) ;
if ( fc ) {
fc - > GetFile ( getter_AddRefs ( file ) ) ;
}
}
2011-10-26 05:52:19 -07:00
bool fromFile = false ;
2011-05-10 16:18:55 -07:00
if ( file ) {
nsCAutoString contentType ;
mChannel - > GetContentType ( contentType ) ;
2011-06-30 14:41:39 -07:00
nsCOMPtr < nsISupports > cacheToken ;
if ( cc ) {
cc - > GetCacheToken ( getter_AddRefs ( cacheToken ) ) ;
2011-10-26 05:52:19 -07:00
// We need to call IsFromCache to determine whether the response is
// fully cached (i.e. whether we can skip reading the response).
cc - > IsFromCache ( & fromFile ) ;
} else {
// If the response is coming from the local resource, we can skip
// reading the response unconditionally.
fromFile = true ;
2011-06-30 14:41:39 -07:00
}
2011-06-30 14:42:15 -07:00
2012-01-30 02:33:59 -08:00
mDOMFile =
2011-07-11 19:41:29 -07:00
new nsDOMFileFile ( file , NS_ConvertASCIItoUTF16 ( contentType ) , cacheToken ) ;
2012-01-30 02:33:59 -08:00
mBuilder = nsnull ;
NS_ASSERTION ( mResponseBody . IsEmpty ( ) , " mResponseBody should be empty " ) ;
2011-05-10 16:18:55 -07:00
}
2011-10-26 05:52:19 -07:00
return fromFile ;
2011-05-10 16:18:55 -07:00
}
2007-03-22 10:30:00 -07:00
NS_IMETHODIMP
2011-09-23 18:57:22 -07:00
nsXMLHttpRequest : : OnDataAvailable ( nsIRequest * request ,
nsISupports * ctxt ,
nsIInputStream * inStr ,
PRUint32 sourceOffset ,
PRUint32 count )
2007-03-22 10:30:00 -07:00
{
NS_ENSURE_ARG_POINTER ( inStr ) ;
NS_ABORT_IF_FALSE ( mContext . get ( ) = = ctxt , " start context different from OnDataAvailable context " ) ;
2011-10-26 05:52:19 -07:00
mProgressSinceLastProgressEvent = true ;
2011-10-28 07:05:07 -07:00
bool cancelable = false ;
2012-01-30 02:33:59 -08:00
if ( ( mResponseType = = XML_HTTP_RESPONSE_TYPE_BLOB | |
mResponseType = = XML_HTTP_RESPONSE_TYPE_MOZ_BLOB ) & & ! mDOMFile ) {
cancelable = CreateDOMFile ( request ) ;
2011-10-28 07:05:07 -07:00
// The nsIStreamListener contract mandates us
// to read from the stream before returning.
2011-05-10 16:18:55 -07:00
}
2007-03-22 10:30:00 -07:00
PRUint32 totalRead ;
2011-09-23 18:57:22 -07:00
nsresult rv = inStr - > ReadSegments ( nsXMLHttpRequest : : StreamReaderFunc ,
( void * ) this , count , & totalRead ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2011-10-28 07:05:07 -07:00
if ( cancelable ) {
// We don't have to read from the local file for the blob response
2012-01-30 02:33:59 -08:00
mDOMFile - > GetSize ( & mLoadTransferred ) ;
2011-10-28 07:05:07 -07:00
ChangeState ( XML_HTTP_REQUEST_LOADING ) ;
return request - > Cancel ( NS_OK ) ;
}
2011-09-23 18:57:22 -07:00
mLoadTransferred + = totalRead ;
ChangeState ( XML_HTTP_REQUEST_LOADING ) ;
2011-10-17 07:59:28 -07:00
MaybeDispatchProgressEvents ( false ) ;
2011-09-23 18:57:22 -07:00
return NS_OK ;
2007-03-22 10:30:00 -07:00
}
2011-09-28 23:19:26 -07:00
bool
2007-09-05 20:51:56 -07:00
IsSameOrBaseChannel ( nsIRequest * aPossibleBase , nsIChannel * aChannel )
{
nsCOMPtr < nsIMultiPartChannel > mpChannel = do_QueryInterface ( aPossibleBase ) ;
if ( mpChannel ) {
nsCOMPtr < nsIChannel > baseChannel ;
nsresult rv = mpChannel - > GetBaseChannel ( getter_AddRefs ( baseChannel ) ) ;
2011-10-17 07:59:28 -07:00
NS_ENSURE_SUCCESS ( rv , false ) ;
2007-09-05 20:51:56 -07:00
return baseChannel = = aChannel ;
}
return aPossibleBase = = aChannel ;
}
2007-03-22 10:30:00 -07:00
/* void onStartRequest (in nsIRequest request, in nsISupports ctxt); */
NS_IMETHODIMP
nsXMLHttpRequest : : OnStartRequest ( nsIRequest * request , nsISupports * ctxt )
{
2012-01-18 16:53:35 -08:00
SAMPLE_LABEL ( " nsXMLHttpRequest " , " OnStartRequest " ) ;
2008-10-19 14:26:37 -07:00
nsresult rv = NS_OK ;
2008-09-30 16:56:57 -07:00
if ( ! mFirstStartRequestSeen & & mRequestObserver ) {
2011-10-17 07:59:28 -07:00
mFirstStartRequestSeen = true ;
2008-09-30 16:56:57 -07:00
mRequestObserver - > OnStartRequest ( request , ctxt ) ;
}
2007-09-05 20:51:56 -07:00
if ( ! IsSameOrBaseChannel ( request , mChannel ) ) {
return NS_OK ;
}
2007-03-22 10:30:00 -07:00
// Don't do anything if we have been aborted
2011-05-10 16:18:55 -07:00
if ( mState & XML_HTTP_REQUEST_UNSENT )
2007-03-22 10:30:00 -07:00
return NS_OK ;
2012-01-26 02:02:22 -08:00
/* Apparently, Abort() should set XML_HTTP_REQUEST_UNSENT. See bug 361773.
XHR2 spec says this is correct . */
2007-03-22 10:30:00 -07:00
if ( mState & XML_HTTP_REQUEST_ABORTED ) {
NS_ERROR ( " Ugh, still getting data on an aborted XMLHttpRequest! " ) ;
return NS_ERROR_UNEXPECTED ;
}
2012-01-26 02:02:22 -08:00
// Don't do anything if we have timed out.
if ( mState & XML_HTTP_REQUEST_TIMED_OUT ) {
return NS_OK ;
}
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsIChannel > channel ( do_QueryInterface ( request ) ) ;
NS_ENSURE_TRUE ( channel , NS_ERROR_UNEXPECTED ) ;
2011-03-17 09:19:13 -07:00
nsCOMPtr < nsIPrincipal > documentPrincipal ;
if ( IsSystemXHR ( ) ) {
2008-03-27 20:46:15 -07:00
// Don't give this document the system principal. We need to keep track of
// mPrincipal being system because we use it for various security checks
// that should be passing, but the document data shouldn't get a system
// principal.
nsresult rv ;
documentPrincipal = do_CreateInstance ( " @mozilla.org/nullprincipal;1 " , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2011-03-17 09:19:13 -07:00
} else {
documentPrincipal = mPrincipal ;
2008-03-27 20:46:15 -07:00
}
channel - > SetOwner ( documentPrincipal ) ;
2007-07-02 06:20:01 -07:00
2011-09-23 18:57:22 -07:00
nsresult status ;
request - > GetStatus ( & status ) ;
mErrorLoad = mErrorLoad | | NS_FAILED ( status ) ;
if ( mUpload & & ! mUploadComplete & & ! mErrorLoad & &
( mState & XML_HTTP_REQUEST_ASYNC ) ) {
2012-01-26 02:02:22 -08:00
if ( mProgressTimerIsActive ) {
mProgressTimerIsActive = false ;
2011-09-23 18:57:22 -07:00
mProgressNotifier - > Cancel ( ) ;
}
2011-10-17 07:59:28 -07:00
MaybeDispatchProgressEvents ( true ) ;
mUploadComplete = true ;
2011-09-23 18:57:22 -07:00
DispatchProgressEvent ( mUpload , NS_LITERAL_STRING ( LOAD_STR ) ,
2011-10-17 07:59:28 -07:00
true , mUploadTotal , mUploadTotal ) ;
2011-09-23 18:57:22 -07:00
}
2007-03-22 10:30:00 -07:00
mReadRequest = request ;
mContext = ctxt ;
mState | = XML_HTTP_REQUEST_PARSEBODY ;
2008-09-11 03:44:15 -07:00
mState & = ~ XML_HTTP_REQUEST_MPART_HEADERS ;
2011-05-10 16:18:55 -07:00
ChangeState ( XML_HTTP_REQUEST_HEADERS_RECEIVED ) ;
2012-01-30 02:33:59 -08:00
if ( mResponseType = = XML_HTTP_RESPONSE_TYPE_BLOB | |
mResponseType = = XML_HTTP_RESPONSE_TYPE_MOZ_BLOB ) {
2011-05-10 16:18:55 -07:00
nsCOMPtr < nsICachingChannel > cc ( do_QueryInterface ( mChannel ) ) ;
if ( cc ) {
2011-10-17 07:59:28 -07:00
cc - > SetCacheAsFile ( true ) ;
2011-05-10 16:18:55 -07:00
}
}
2007-03-22 10:30:00 -07:00
2011-09-23 18:57:22 -07:00
ResetResponse ( ) ;
2008-10-11 23:30:14 -07:00
2011-09-23 18:57:36 -07:00
if ( ! mOverrideMimeType . IsEmpty ( ) ) {
channel - > SetContentType ( mOverrideMimeType ) ;
}
DetectCharset ( ) ;
2008-10-11 23:30:14 -07:00
// Set up responseXML
2011-09-28 23:19:26 -07:00
bool parseBody = mResponseType = = XML_HTTP_RESPONSE_TYPE_DEFAULT | |
2011-05-10 16:18:55 -07:00
mResponseType = = XML_HTTP_RESPONSE_TYPE_DOCUMENT ;
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsIHttpChannel > httpChannel ( do_QueryInterface ( mChannel ) ) ;
2011-05-10 16:18:55 -07:00
if ( parseBody & & httpChannel ) {
2007-03-22 10:30:00 -07:00
nsCAutoString method ;
httpChannel - > GetRequestMethod ( method ) ;
parseBody = ! method . EqualsLiteral ( " HEAD " ) ;
}
2011-11-15 23:38:51 -08:00
mIsHtml = false ;
mWarnAboutMultipartHtml = false ;
mWarnAboutSyncHtml = false ;
2007-03-22 10:30:00 -07:00
if ( parseBody & & NS_SUCCEEDED ( status ) ) {
// We can gain a huge performance win by not even trying to
// parse non-XML data. This also protects us from the situation
// where we have an XML document and sink, but HTML (or other)
// parser, which can produce unreliable results.
nsCAutoString type ;
channel - > GetContentType ( type ) ;
2011-11-24 07:28:12 -08:00
if ( ( mResponseType = = XML_HTTP_RESPONSE_TYPE_DOCUMENT ) & &
type . EqualsLiteral ( " text/html " ) ) {
// HTML parsing is only supported for responseType == "document" to
// avoid running the parser and, worse, populating responseXML for
// legacy users of XHR who use responseType == "" for retrieving the
// responseText of text/html resources. This legacy case is so common
// that it's not useful to emit a warning about it.
2011-11-15 23:38:51 -08:00
if ( ! ( mState & XML_HTTP_REQUEST_ASYNC ) ) {
// We don't make cool new features available in the bad synchronous
// mode. The synchronous mode is for legacy only.
mWarnAboutSyncHtml = true ;
mState & = ~ XML_HTTP_REQUEST_PARSEBODY ;
} else if ( mState & XML_HTTP_REQUEST_MULTIPART ) {
// HTML parsing is supported only for non-multipart responses. The
// multipart implementation assumes that it's OK to start the next part
// immediately after the last part. That doesn't work with the HTML
// parser, because when OnStopRequest for one part has fired, the
// parser thread still hasn't posted back the runnables that make the
// parsing appear finished.
//
// On the other hand, multipart support seems to be a legacy feature,
// so it isn't clear that use cases justify adding support for deferring
// the multipart stream events between parts to accommodate the
// asynchronous nature of the HTML parser.
mWarnAboutMultipartHtml = true ;
mState & = ~ XML_HTTP_REQUEST_PARSEBODY ;
} else {
mIsHtml = true ;
}
} else if ( type . Find ( " xml " ) = = kNotFound ) {
2007-03-22 10:30:00 -07:00
mState & = ~ XML_HTTP_REQUEST_PARSEBODY ;
}
} else {
// The request failed, so we shouldn't be parsing anyway
mState & = ~ XML_HTTP_REQUEST_PARSEBODY ;
}
if ( mState & XML_HTTP_REQUEST_PARSEBODY ) {
2008-10-11 23:30:14 -07:00
nsCOMPtr < nsIURI > baseURI , docURI ;
2009-06-15 01:27:29 -07:00
nsCOMPtr < nsIDocument > doc =
nsContentUtils : : GetDocumentFromScriptContext ( mScriptContext ) ;
2008-10-11 23:30:14 -07:00
if ( doc ) {
docURI = doc - > GetDocumentURI ( ) ;
baseURI = doc - > GetBaseURI ( ) ;
}
// Create an empty document from it. Here we have to cheat a little bit...
// Setting the base URI to |baseURI| won't work if the document has a null
// principal, so use mPrincipal when creating the document, then reset the
// principal.
const nsAString & emptyStr = EmptyString ( ) ;
nsCOMPtr < nsIScriptGlobalObject > global = do_QueryInterface ( mOwner ) ;
rv = nsContentUtils : : CreateDocument ( emptyStr , emptyStr , nsnull , docURI ,
2011-11-15 23:38:51 -08:00
baseURI , mPrincipal , global ,
mIsHtml ? DocumentFlavorHTML :
DocumentFlavorLegacyGuess ,
2008-10-11 23:30:14 -07:00
getter_AddRefs ( mResponseXML ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
nsCOMPtr < nsIDocument > responseDoc = do_QueryInterface ( mResponseXML ) ;
responseDoc - > SetPrincipal ( documentPrincipal ) ;
2011-03-17 09:19:13 -07:00
if ( IsSystemXHR ( ) ) {
2010-08-25 13:12:28 -07:00
responseDoc - > ForceEnableXULXBL ( ) ;
}
2008-10-11 23:30:14 -07:00
if ( mState & XML_HTTP_REQUEST_USE_XSITE_AC ) {
nsCOMPtr < nsIHTMLDocument > htmlDoc = do_QueryInterface ( mResponseXML ) ;
if ( htmlDoc ) {
htmlDoc - > DisableCookieAccess ( ) ;
}
}
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsIStreamListener > listener ;
nsCOMPtr < nsILoadGroup > loadGroup ;
channel - > GetLoadGroup ( getter_AddRefs ( loadGroup ) ) ;
2008-10-11 23:30:14 -07:00
rv = responseDoc - > StartDocumentLoad ( kLoadAsData , channel , loadGroup ,
nsnull , getter_AddRefs ( listener ) ,
! ( mState & XML_HTTP_REQUEST_USE_XSITE_AC ) ) ;
2007-03-22 10:30:00 -07:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
mXMLParserStreamListener = listener ;
2008-10-11 23:30:14 -07:00
rv = mXMLParserStreamListener - > OnStartRequest ( request , ctxt ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-03-22 10:30:00 -07:00
}
2008-10-19 14:26:37 -07:00
// We won't get any progress events anyway if we didn't have progress
// events when starting the request - so maybe no need to start timer here.
if ( NS_SUCCEEDED ( rv ) & &
( mState & XML_HTTP_REQUEST_ASYNC ) & &
HasListenersFor ( NS_LITERAL_STRING ( PROGRESS_STR ) ) ) {
StartProgressEventTimer ( ) ;
}
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
/* void onStopRequest (in nsIRequest request, in nsISupports ctxt, in nsresult status, in wstring statusArg); */
NS_IMETHODIMP
nsXMLHttpRequest : : OnStopRequest ( nsIRequest * request , nsISupports * ctxt , nsresult status )
{
2012-01-17 12:33:04 -08:00
SAMPLE_LABEL ( " content " , " nsXMLHttpRequest::OnStopRequest " ) ;
2007-09-05 20:51:56 -07:00
if ( ! IsSameOrBaseChannel ( request , mChannel ) ) {
return NS_OK ;
}
2012-02-16 08:45:25 -08:00
mWaitingForOnStopRequest = false ;
2007-03-22 10:30:00 -07:00
nsresult rv = NS_OK ;
// If we're loading a multipart stream of XML documents, we'll get
// an OnStopRequest() for the last part in the stream, and then
// another one for the end of the initiating
// "multipart/x-mixed-replace" stream too. So we must check that we
// still have an xml parser stream listener before accessing it
// here.
2007-07-30 17:42:16 -07:00
nsCOMPtr < nsIMultiPartChannel > mpChannel = do_QueryInterface ( request ) ;
if ( mpChannel ) {
2011-09-28 23:19:26 -07:00
bool last ;
2007-07-30 17:42:16 -07:00
rv = mpChannel - > GetIsLastPart ( & last ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( last ) {
mState | = XML_HTTP_REQUEST_GOT_FINAL_STOP ;
}
}
else {
mState | = XML_HTTP_REQUEST_GOT_FINAL_STOP ;
}
2008-11-26 22:16:37 -08:00
if ( mRequestObserver & & mState & XML_HTTP_REQUEST_GOT_FINAL_STOP ) {
NS_ASSERTION ( mFirstStartRequestSeen , " Inconsistent state! " ) ;
2011-10-17 07:59:28 -07:00
mFirstStartRequestSeen = false ;
2008-11-26 22:16:37 -08:00
mRequestObserver - > OnStopRequest ( request , ctxt , status ) ;
}
2010-05-02 01:27:20 -07:00
// make sure to notify the listener if we were aborted
// XXX in fact, why don't we do the cleanup below in this case??
2012-01-26 02:02:22 -08:00
// XML_HTTP_REQUEST_UNSENT is for abort calls. See OnStartRequest above.
if ( ( mState & XML_HTTP_REQUEST_UNSENT ) | |
( mState & XML_HTTP_REQUEST_TIMED_OUT ) ) {
2010-05-02 01:27:20 -07:00
if ( mXMLParserStreamListener )
( void ) mXMLParserStreamListener - > OnStopRequest ( request , ctxt , status ) ;
2008-11-26 22:16:37 -08:00
return NS_OK ;
2010-05-02 01:27:20 -07:00
}
2008-11-26 22:16:37 -08:00
// Is this good enough here?
if ( mState & XML_HTTP_REQUEST_PARSEBODY & & mXMLParserStreamListener ) {
2011-06-14 00:53:08 -07:00
mXMLParserStreamListener - > OnStopRequest ( request , ctxt , status ) ;
2008-11-26 22:16:37 -08:00
}
2007-03-22 10:30:00 -07:00
mXMLParserStreamListener = nsnull ;
mReadRequest = nsnull ;
mContext = nsnull ;
2011-09-23 18:57:22 -07:00
// If we're received data since the last progress event, make sure to fire
2011-11-15 23:38:51 -08:00
// an event for it, except in the HTML case, defer the last progress event
// until the parser is done.
if ( ! mIsHtml ) {
MaybeDispatchProgressEvents ( true ) ;
}
2011-09-23 18:57:22 -07:00
2012-01-30 02:33:59 -08:00
if ( NS_SUCCEEDED ( status ) & &
( mResponseType = = XML_HTTP_RESPONSE_TYPE_BLOB | |
mResponseType = = XML_HTTP_RESPONSE_TYPE_MOZ_BLOB ) ) {
if ( ! mDOMFile ) {
CreateDOMFile ( request ) ;
2011-05-10 16:18:55 -07:00
}
2012-01-30 02:33:59 -08:00
if ( mDOMFile ) {
mResponseBlob = mDOMFile ;
mDOMFile = nsnull ;
} else {
2011-05-10 16:18:55 -07:00
// Smaller files may be written in cache map instead of separate files.
// Also, no-store response cannot be written in persistent cache.
nsCAutoString contentType ;
mChannel - > GetContentType ( contentType ) ;
2012-01-30 02:33:59 -08:00
mBuilder - > GetBlobInternal ( NS_ConvertASCIItoUTF16 ( contentType ) ,
false , getter_AddRefs ( mResponseBlob ) ) ;
mBuilder = nsnull ;
2011-05-10 16:18:55 -07:00
}
2012-01-30 02:33:59 -08:00
NS_ASSERTION ( mResponseBody . IsEmpty ( ) , " mResponseBody should be empty " ) ;
NS_ASSERTION ( mResponseText . IsEmpty ( ) , " mResponseText should be empty " ) ;
2011-05-10 16:18:55 -07:00
}
2012-01-30 02:33:59 -08:00
nsCOMPtr < nsIChannel > channel ( do_QueryInterface ( request ) ) ;
NS_ENSURE_TRUE ( channel , NS_ERROR_UNEXPECTED ) ;
2007-03-22 10:30:00 -07:00
channel - > SetNotificationCallbacks ( nsnull ) ;
mNotificationCallbacks = nsnull ;
mChannelEventSink = nsnull ;
mProgressEventSink = nsnull ;
2011-11-16 11:50:48 -08:00
mState & = ~ XML_HTTP_REQUEST_SYNCLOOPING ;
2007-03-22 10:30:00 -07:00
if ( NS_FAILED ( status ) ) {
// This can happen if the server is unreachable. Other possible
// reasons are that the user leaves the page or hits the ESC key.
2011-10-17 07:59:28 -07:00
mErrorLoad = true ;
2011-06-14 00:53:08 -07:00
mResponseXML = nsnull ;
2007-03-22 10:30:00 -07:00
}
// If we're uninitialized at this point, we encountered an error
// earlier and listeners have already been notified. Also we do
// not want to do this if we already completed.
2011-05-10 16:18:55 -07:00
if ( mState & ( XML_HTTP_REQUEST_UNSENT |
XML_HTTP_REQUEST_DONE ) ) {
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
2011-11-15 23:38:51 -08:00
if ( ! mResponseXML ) {
ChangeStateToDone ( ) ;
return NS_OK ;
}
if ( mIsHtml ) {
NS_ASSERTION ( ! ( mState & XML_HTTP_REQUEST_SYNCLOOPING ) ,
" We weren't supposed to support HTML parsing with XHR! " ) ;
nsCOMPtr < nsIDOMEventTarget > eventTarget = do_QueryInterface ( mResponseXML ) ;
nsEventListenerManager * manager = eventTarget - > GetListenerManager ( true ) ;
manager - > AddEventListenerByType ( new nsXHRParseEndListener ( this ) ,
NS_LITERAL_STRING ( " DOMContentLoaded " ) ,
NS_EVENT_FLAG_BUBBLE |
NS_EVENT_FLAG_SYSTEM_EVENT ) ;
return NS_OK ;
}
2011-11-16 11:50:48 -08:00
// We might have been sent non-XML data. If that was the case,
// we should null out the document member. The idea in this
// check here is that if there is no document element it is not
// an XML document. We might need a fancier check...
2011-11-15 23:38:51 -08:00
nsCOMPtr < nsIDOMElement > root ;
mResponseXML - > GetDocumentElement ( getter_AddRefs ( root ) ) ;
if ( ! root ) {
mResponseXML = nsnull ;
2007-03-22 10:30:00 -07:00
}
2011-11-15 23:38:51 -08:00
ChangeStateToDone ( ) ;
return NS_OK ;
}
2007-03-22 10:30:00 -07:00
2011-11-15 23:38:51 -08:00
void
nsXMLHttpRequest : : ChangeStateToDone ( )
{
if ( mIsHtml ) {
// In the HTML case, this has to be deferred, because the parser doesn't
// do it's job synchronously.
MaybeDispatchProgressEvents ( true ) ;
}
2012-01-26 02:02:22 -08:00
2011-10-17 07:59:28 -07:00
ChangeState ( XML_HTTP_REQUEST_DONE , true ) ;
2012-01-26 02:02:22 -08:00
if ( mTimeoutTimer ) {
mTimeoutTimer - > Cancel ( ) ;
}
2007-03-22 10:30:00 -07:00
2008-08-14 04:07:46 -07:00
NS_NAMED_LITERAL_STRING ( errorStr , ERROR_STR ) ;
NS_NAMED_LITERAL_STRING ( loadStr , LOAD_STR ) ;
DispatchProgressEvent ( this ,
mErrorLoad ? errorStr : loadStr ,
! mErrorLoad ,
2011-09-23 18:57:22 -07:00
mLoadTransferred ,
mErrorLoad ? 0 : mLoadTransferred ) ;
2008-08-14 04:07:46 -07:00
if ( mErrorLoad & & mUpload & & ! mUploadComplete ) {
2011-10-17 07:59:28 -07:00
DispatchProgressEvent ( mUpload , errorStr , true ,
2008-08-14 04:07:46 -07:00
mUploadTransferred , mUploadTotal ) ;
2007-03-22 10:30:00 -07:00
}
2011-06-14 00:53:08 -07:00
if ( mErrorLoad ) {
// By nulling out channel here we make it so that Send() can test
// for that and throw. Also calling the various status
// methods/members will not throw.
// This matches what IE does.
mChannel = nsnull ;
}
else if ( ! ( mState & XML_HTTP_REQUEST_GOT_FINAL_STOP ) ) {
2007-03-22 10:30:00 -07:00
// We're a multipart request, so we're not done. Reset to opened.
ChangeState ( XML_HTTP_REQUEST_OPENED ) ;
}
}
2007-07-24 21:53:21 -07:00
NS_IMETHODIMP
nsXMLHttpRequest : : SendAsBinary ( const nsAString & aBody )
{
char * data = static_cast < char * > ( NS_Alloc ( aBody . Length ( ) + 1 ) ) ;
if ( ! data )
return NS_ERROR_OUT_OF_MEMORY ;
nsAString : : const_iterator iter , end ;
aBody . BeginReading ( iter ) ;
aBody . EndReading ( end ) ;
char * p = data ;
while ( iter ! = end ) {
if ( * iter & 0xFF00 ) {
NS_Free ( data ) ;
return NS_ERROR_DOM_INVALID_CHARACTER_ERR ;
}
* p + + = static_cast < char > ( * iter + + ) ;
}
* p = ' \0 ' ;
nsCOMPtr < nsIInputStream > stream ;
nsresult rv = NS_NewByteInputStream ( getter_AddRefs ( stream ) , data ,
aBody . Length ( ) , NS_ASSIGNMENT_ADOPT ) ;
if ( NS_FAILED ( rv ) )
NS_Free ( data ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
nsCOMPtr < nsIWritableVariant > variant = new nsVariant ( ) ;
if ( ! variant ) return NS_ERROR_OUT_OF_MEMORY ;
rv = variant - > SetAsISupports ( stream ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
return Send ( variant ) ;
}
2010-02-24 21:58:17 -08:00
static nsresult
GetRequestBody ( nsIVariant * aBody , nsIInputStream * * aResult ,
nsACString & aContentType , nsACString & aCharset )
{
* aResult = nsnull ;
aContentType . AssignLiteral ( " text/plain " ) ;
aCharset . AssignLiteral ( " UTF-8 " ) ;
PRUint16 dataType ;
nsresult rv = aBody - > GetDataType ( & dataType ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( dataType = = nsIDataType : : VTYPE_INTERFACE | |
dataType = = nsIDataType : : VTYPE_INTERFACE_IS ) {
nsCOMPtr < nsISupports > supports ;
nsID * iid ;
rv = aBody - > GetAsInterface ( & iid , getter_AddRefs ( supports ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
nsMemory : : Free ( iid ) ;
// document?
nsCOMPtr < nsIDOMDocument > doc = do_QueryInterface ( supports ) ;
if ( doc ) {
aContentType . AssignLiteral ( " application/xml " ) ;
2011-03-26 09:06:27 -07:00
nsAutoString inputEncoding ;
doc - > GetInputEncoding ( inputEncoding ) ;
if ( ! DOMStringIsNull ( inputEncoding ) ) {
CopyUTF16toUTF8 ( inputEncoding , aCharset ) ;
2010-02-24 21:58:17 -08:00
}
// Serialize to a stream so that the encoding used will
// match the document's.
nsCOMPtr < nsIDOMSerializer > serializer =
do_CreateInstance ( NS_XMLSERIALIZER_CONTRACTID , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
nsCOMPtr < nsIStorageStream > storStream ;
rv = NS_NewStorageStream ( 4096 , PR_UINT32_MAX ,
getter_AddRefs ( storStream ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
nsCOMPtr < nsIOutputStream > output ;
rv = storStream - > GetOutputStream ( 0 , getter_AddRefs ( output ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// Make sure to use the encoding we'll send
rv = serializer - > SerializeToStream ( doc , output , aCharset ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
output - > Close ( ) ;
return storStream - > NewInputStream ( 0 , aResult ) ;
}
// nsISupportsString?
nsCOMPtr < nsISupportsString > wstr = do_QueryInterface ( supports ) ;
if ( wstr ) {
nsAutoString string ;
wstr - > GetData ( string ) ;
return NS_NewCStringInputStream ( aResult ,
NS_ConvertUTF16toUTF8 ( string ) ) ;
}
// nsIInputStream?
nsCOMPtr < nsIInputStream > stream = do_QueryInterface ( supports ) ;
if ( stream ) {
2011-08-24 10:46:53 -07:00
stream . forget ( aResult ) ;
2010-02-24 21:58:17 -08:00
aCharset . Truncate ( ) ;
return NS_OK ;
}
2010-02-24 21:58:18 -08:00
// nsIXHRSendable?
nsCOMPtr < nsIXHRSendable > sendable = do_QueryInterface ( supports ) ;
if ( sendable ) {
return sendable - > GetSendInfo ( aResult , aContentType , aCharset ) ;
}
2011-08-17 18:44:56 -07:00
// ArrayBuffer?
jsval realVal ;
JSObject * obj ;
nsresult rv = aBody - > GetAsJSVal ( & realVal ) ;
if ( NS_SUCCEEDED ( rv ) & & ! JSVAL_IS_PRIMITIVE ( realVal ) & &
( obj = JSVAL_TO_OBJECT ( realVal ) ) & &
( js_IsArrayBuffer ( obj ) ) ) {
2011-10-17 07:59:28 -07:00
aContentType . SetIsVoid ( true ) ;
2011-08-17 18:44:56 -07:00
PRInt32 abLength = JS_GetArrayBufferByteLength ( obj ) ;
char * data = ( char * ) JS_GetArrayBufferData ( obj ) ;
nsCOMPtr < nsIInputStream > stream ;
nsresult rv = NS_NewByteInputStream ( getter_AddRefs ( stream ) , data ,
abLength , NS_ASSIGNMENT_COPY ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
stream . forget ( aResult ) ;
aCharset . Truncate ( ) ;
return NS_OK ;
}
2010-02-24 21:58:17 -08:00
}
else if ( dataType = = nsIDataType : : VTYPE_VOID | |
dataType = = nsIDataType : : VTYPE_EMPTY ) {
// Makes us act as if !aBody, don't upload anything
return NS_OK ;
}
PRUnichar * data = nsnull ;
PRUint32 len = 0 ;
rv = aBody - > GetAsWStringWithSize ( & len , & data ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
nsString string ;
string . Adopt ( data , len ) ;
return NS_NewCStringInputStream ( aResult , NS_ConvertUTF16toUTF8 ( string ) ) ;
}
2007-03-22 10:30:00 -07:00
/* void send (in nsIVariant aBody); */
NS_IMETHODIMP
nsXMLHttpRequest : : Send ( nsIVariant * aBody )
{
2008-02-19 15:12:23 -08:00
NS_ENSURE_TRUE ( mPrincipal , NS_ERROR_NOT_INITIALIZED ) ;
2007-12-12 00:33:32 -08:00
nsresult rv = CheckInnerWindowCorrectness ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-03-22 10:30:00 -07:00
// Return error if we're already processing a request
if ( XML_HTTP_REQUEST_SENT & mState ) {
return NS_ERROR_FAILURE ;
}
// Make sure we've been opened
if ( ! mChannel | | ! ( XML_HTTP_REQUEST_OPENED & mState ) ) {
return NS_ERROR_NOT_INITIALIZED ;
}
2010-09-08 10:42:08 -07:00
// nsIRequest::LOAD_BACKGROUND prevents throbber from becoming active, which
// in turn keeps STOP button from becoming active. If the consumer passed in
// a progress event handler we must load with nsIRequest::LOAD_NORMAL or
// necko won't generate any progress notifications.
if ( HasListenersFor ( NS_LITERAL_STRING ( PROGRESS_STR ) ) | |
HasListenersFor ( NS_LITERAL_STRING ( UPLOADPROGRESS_STR ) ) | |
( mUpload & & mUpload - > HasListenersFor ( NS_LITERAL_STRING ( PROGRESS_STR ) ) ) ) {
nsLoadFlags loadFlags ;
mChannel - > GetLoadFlags ( & loadFlags ) ;
loadFlags & = ~ nsIRequest : : LOAD_BACKGROUND ;
loadFlags | = nsIRequest : : LOAD_NORMAL ;
mChannel - > SetLoadFlags ( loadFlags ) ;
}
2007-03-22 10:30:00 -07:00
// XXX We should probably send a warning to the JS console
// if there are no event listeners set and we are doing
// an asynchronous call.
// Ignore argument if method is GET, there is no point in trying to
// upload anything
nsCAutoString method ;
nsCOMPtr < nsIHttpChannel > httpChannel ( do_QueryInterface ( mChannel ) ) ;
if ( httpChannel ) {
2008-10-14 17:12:28 -07:00
httpChannel - > GetRequestMethod ( method ) ; // If GET, method name will be uppercase
2007-03-22 10:30:00 -07:00
2011-03-17 09:19:13 -07:00
if ( ! IsSystemXHR ( ) ) {
2011-01-30 13:46:34 -08:00
// Get the referrer for the request.
//
// If it weren't for history.push/replaceState, we could just use the
// principal's URI here. But since we want changes to the URI effected
// by push/replaceState to be reflected in the XHR referrer, we have to
// be more clever.
//
// If the document's original URI (before any push/replaceStates) matches
// our principal, then we use the document's current URI (after
// push/replaceStates). Otherwise (if the document is, say, a data:
// URI), we just use the principal's URI.
nsCOMPtr < nsIURI > principalURI ;
mPrincipal - > GetURI ( getter_AddRefs ( principalURI ) ) ;
nsCOMPtr < nsIDocument > doc =
nsContentUtils : : GetDocumentFromScriptContext ( mScriptContext ) ;
nsCOMPtr < nsIURI > docCurURI ;
nsCOMPtr < nsIURI > docOrigURI ;
if ( doc ) {
docCurURI = doc - > GetDocumentURI ( ) ;
docOrigURI = doc - > GetOriginalURI ( ) ;
}
nsCOMPtr < nsIURI > referrerURI ;
if ( principalURI & & docCurURI & & docOrigURI ) {
2011-09-28 23:19:26 -07:00
bool equal = false ;
2011-01-30 13:46:34 -08:00
principalURI - > Equals ( docOrigURI , & equal ) ;
if ( equal ) {
referrerURI = docCurURI ;
}
}
if ( ! referrerURI )
referrerURI = principalURI ;
2007-03-22 10:30:00 -07:00
2011-01-30 13:46:34 -08:00
httpChannel - > SetReferrer ( referrerURI ) ;
2008-03-13 04:43:06 -07:00
}
2009-11-18 15:21:13 -08:00
// Some extensions override the http protocol handler and provide their own
// implementation. The channels returned from that implementation doesn't
// seem to always implement the nsIUploadChannel2 interface, presumably
// because it's a new interface.
// Eventually we should remove this and simply require that http channels
// implement the new interface.
// See bug 529041
nsCOMPtr < nsIUploadChannel2 > uploadChannel2 =
do_QueryInterface ( httpChannel ) ;
if ( ! uploadChannel2 ) {
nsCOMPtr < nsIConsoleService > consoleService =
do_GetService ( NS_CONSOLESERVICE_CONTRACTID ) ;
if ( consoleService ) {
consoleService - > LogStringMessage ( NS_LITERAL_STRING (
2009-11-18 16:22:25 -08:00
" Http channel implementation doesn't support nsIUploadChannel2. An extension has supplied a non-functional http protocol handler. This will break behavior and in future releases not work at all. "
) . get ( ) ) ;
2009-11-18 15:21:13 -08:00
}
}
2007-03-22 10:30:00 -07:00
}
2008-08-14 04:07:46 -07:00
mUploadTransferred = 0 ;
mUploadTotal = 0 ;
// By default we don't have any upload, so mark upload complete.
2011-10-17 07:59:28 -07:00
mUploadComplete = true ;
mErrorLoad = false ;
mLoadLengthComputable = false ;
2008-10-19 14:26:37 -07:00
mLoadTotal = 0 ;
mUploadProgress = 0 ;
mUploadProgressMax = 0 ;
2007-03-22 10:30:00 -07:00
if ( aBody & & httpChannel & & ! method . EqualsLiteral ( " GET " ) ) {
2008-10-14 17:12:28 -07:00
2010-02-24 21:58:17 -08:00
nsCAutoString charset ;
nsCAutoString defaultContentType ;
nsCOMPtr < nsIInputStream > postDataStream ;
2008-10-14 17:12:28 -07:00
2010-02-24 21:58:17 -08:00
rv = GetRequestBody ( aBody , getter_AddRefs ( postDataStream ) ,
defaultContentType , charset ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-10-14 17:12:28 -07:00
if ( postDataStream ) {
// If no content type header was set by the client, we set it to
// application/xml.
nsCAutoString contentType ;
if ( NS_FAILED ( httpChannel - >
GetRequestHeader ( NS_LITERAL_CSTRING ( " Content-Type " ) ,
contentType ) ) | |
contentType . IsEmpty ( ) ) {
2009-01-13 22:53:43 -08:00
contentType = defaultContentType ;
2008-10-14 17:12:28 -07:00
}
// We don't want to set a charset for streams.
if ( ! charset . IsEmpty ( ) ) {
nsCAutoString specifiedCharset ;
2011-09-28 23:19:26 -07:00
bool haveCharset ;
2008-10-14 17:12:28 -07:00
PRInt32 charsetStart , charsetEnd ;
rv = NS_ExtractCharsetFromContentType ( contentType , specifiedCharset ,
& haveCharset , & charsetStart ,
& charsetEnd ) ;
2009-01-13 22:53:43 -08:00
if ( NS_SUCCEEDED ( rv ) ) {
2011-11-29 14:06:32 -08:00
// special case: the extracted charset is quoted with single quotes
// -- for the purpose of preserving what was set we want to handle
// them as delimiters (although they aren't really)
if ( specifiedCharset . Length ( ) > = 2 & &
specifiedCharset . First ( ) = = ' \' ' & &
specifiedCharset . Last ( ) = = ' \' ' ) {
specifiedCharset = Substring ( specifiedCharset , 1 ,
specifiedCharset . Length ( ) - 2 ) ;
}
2009-01-13 22:53:43 -08:00
// If the content-type the page set already has a charset parameter,
// and it's the same charset, up to case, as |charset|, just send the
// page-set content-type header. Apparently at least
// google-web-toolkit is broken and relies on the exact case of its
// charset parameter, which makes things break if we use |charset|
// (which is always a fully resolved charset per our charset alias
// table, hence might be differently cased).
if ( ! specifiedCharset . Equals ( charset ,
nsCaseInsensitiveCStringComparator ( ) ) ) {
nsCAutoString newCharset ( " ; charset= " ) ;
newCharset . Append ( charset ) ;
contentType . Replace ( charsetStart , charsetEnd - charsetStart ,
newCharset ) ;
}
2008-10-14 17:12:28 -07:00
}
}
2009-09-08 16:29:41 -07:00
// If necessary, wrap the stream in a buffered stream so as to guarantee
// support for our upload when calling ExplicitSetUploadStream.
if ( ! NS_InputStreamIsBuffered ( postDataStream ) ) {
nsCOMPtr < nsIInputStream > bufferedStream ;
rv = NS_NewBufferedInputStream ( getter_AddRefs ( bufferedStream ) ,
postDataStream ,
4096 ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
postDataStream = bufferedStream ;
}
2011-10-17 07:59:28 -07:00
mUploadComplete = false ;
2008-10-21 02:06:53 -07:00
PRUint32 uploadTotal = 0 ;
postDataStream - > Available ( & uploadTotal ) ;
mUploadTotal = uploadTotal ;
2009-09-08 16:29:41 -07:00
// We want to use a newer version of the upload channel that won't
// ignore the necessary headers for an empty Content-Type.
2009-11-18 15:21:13 -08:00
nsCOMPtr < nsIUploadChannel2 > uploadChannel2 ( do_QueryInterface ( httpChannel ) ) ;
// This assertion will fire if buggy extensions are installed
2010-10-04 17:41:07 -07:00
NS_ASSERTION ( uploadChannel2 , " http must support nsIUploadChannel2 " ) ;
2009-11-18 15:21:13 -08:00
if ( uploadChannel2 ) {
uploadChannel2 - > ExplicitSetUploadStream ( postDataStream , contentType ,
2011-10-17 07:59:28 -07:00
- 1 , method , false ) ;
2009-11-18 15:21:13 -08:00
}
else {
// http channel doesn't support the new nsIUploadChannel2. Emulate
// as best we can using nsIUploadChannel
if ( contentType . IsEmpty ( ) ) {
contentType . AssignLiteral ( " application/octet-stream " ) ;
}
nsCOMPtr < nsIUploadChannel > uploadChannel =
do_QueryInterface ( httpChannel ) ;
uploadChannel - > SetUploadStream ( postDataStream , contentType , - 1 ) ;
// Reset the method to its original value
httpChannel - > SetRequestMethod ( method ) ;
}
2008-10-14 17:12:28 -07:00
}
}
2008-10-14 14:05:41 -07:00
2010-10-04 17:41:07 -07:00
if ( httpChannel ) {
nsCAutoString contentTypeHeader ;
rv = httpChannel - > GetRequestHeader ( NS_LITERAL_CSTRING ( " Content-Type " ) ,
contentTypeHeader ) ;
if ( NS_SUCCEEDED ( rv ) ) {
nsCAutoString contentType , charset ;
rv = NS_ParseContentType ( contentTypeHeader , contentType , charset ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( ! contentType . LowerCaseEqualsLiteral ( " text/plain " ) & &
! contentType . LowerCaseEqualsLiteral ( " application/x-www-form-urlencoded " ) & &
! contentType . LowerCaseEqualsLiteral ( " multipart/form-data " ) ) {
2011-03-28 13:18:45 -07:00
mCORSUnsafeHeaders . AppendElement ( NS_LITERAL_CSTRING ( " Content-Type " ) ) ;
2010-10-04 17:41:07 -07:00
}
}
}
2011-09-23 18:57:22 -07:00
ResetResponse ( ) ;
2007-03-22 10:30:00 -07:00
2008-09-30 17:49:30 -07:00
rv = CheckChannelForCrossSiteRequest ( mChannel ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2011-09-28 23:19:26 -07:00
bool withCredentials = ! ! ( mState & XML_HTTP_REQUEST_AC_WITH_CREDENTIALS ) ;
2008-09-30 17:52:52 -07:00
2007-03-22 10:30:00 -07:00
// Hook us up to listen to redirects and the like
mChannel - > GetNotificationCallbacks ( getter_AddRefs ( mNotificationCallbacks ) ) ;
mChannel - > SetNotificationCallbacks ( this ) ;
2007-07-26 19:49:18 -07:00
// Create our listener
nsCOMPtr < nsIStreamListener > listener = this ;
2007-03-22 10:30:00 -07:00
if ( mState & XML_HTTP_REQUEST_MULTIPART ) {
2007-07-26 19:49:18 -07:00
listener = new nsMultipartProxyListener ( listener ) ;
2007-03-22 10:30:00 -07:00
if ( ! listener ) {
return NS_ERROR_OUT_OF_MEMORY ;
}
}
2011-03-17 09:19:13 -07:00
if ( ! IsSystemXHR ( ) ) {
2011-03-28 13:18:45 -07:00
// Always create a nsCORSListenerProxy here even if it's
2008-09-30 17:49:30 -07:00
// a same-origin request right now, since it could be redirected.
2011-03-28 13:18:45 -07:00
listener = new nsCORSListenerProxy ( listener , mPrincipal , mChannel ,
2012-01-11 01:47:46 -08:00
withCredentials , true , & rv ) ;
2008-09-30 17:49:30 -07:00
NS_ENSURE_TRUE ( listener , NS_ERROR_OUT_OF_MEMORY ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2008-10-14 17:12:28 -07:00
// Bypass the network cache in cases where it makes no sense:
// 1) Multipart responses are very large and would likely be doomed by the
// cache once they grow too large, so they are not worth caching.
// 2) POST responses are always unique, and we provide no API that would
// allow our consumers to specify a "cache key" to access old POST
// responses, so they are not worth caching.
if ( ( mState & XML_HTTP_REQUEST_MULTIPART ) | | method . EqualsLiteral ( " POST " ) ) {
AddLoadFlags ( mChannel ,
nsIRequest : : LOAD_BYPASS_CACHE | nsIRequest : : INHIBIT_CACHING ) ;
}
// When we are sync loading, we need to bypass the local cache when it would
// otherwise block us waiting for exclusive access to the cache. If we don't
// do this, then we could dead lock in some cases (see bug 309424).
else if ( ! ( mState & XML_HTTP_REQUEST_ASYNC ) ) {
AddLoadFlags ( mChannel ,
nsICachingChannel : : LOAD_BYPASS_LOCAL_CACHE_IF_BUSY ) ;
}
2007-03-22 10:30:00 -07:00
2008-10-14 17:12:28 -07:00
// Since we expect XML data, set the type hint accordingly
// This means that we always try to parse local files as XML
// ignoring return value, as this is not critical
mChannel - > SetContentType ( NS_LITERAL_CSTRING ( " application/xml " ) ) ;
2007-03-22 10:30:00 -07:00
2012-01-26 02:02:22 -08:00
// We're about to send the request. Start our timeout.
mRequestSentTime = PR_Now ( ) ;
StartTimeoutTimer ( ) ;
2011-03-28 13:11:09 -07:00
// Set up the preflight if needed
if ( mState & XML_HTTP_REQUEST_NEED_AC_PREFLIGHT ) {
// Check to see if this initial OPTIONS request has already been cached
// in our special Access Control Cache.
2008-09-30 17:49:30 -07:00
2011-03-28 13:11:09 -07:00
rv = NS_StartCORSPreflight ( mChannel , listener ,
mPrincipal , withCredentials ,
2011-03-28 13:18:45 -07:00
mCORSUnsafeHeaders ,
getter_AddRefs ( mCORSPreflightChannel ) ) ;
2011-03-28 13:11:09 -07:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-09-30 17:49:30 -07:00
}
else {
// Start reading from the channel
rv = mChannel - > AsyncOpen ( listener , nsnull ) ;
}
2007-03-22 10:30:00 -07:00
if ( NS_FAILED ( rv ) ) {
// Drop our ref to the channel to avoid cycles
mChannel = nsnull ;
2011-03-28 13:18:45 -07:00
mCORSPreflightChannel = nsnull ;
2007-03-22 10:30:00 -07:00
return rv ;
}
2012-02-16 08:45:25 -08:00
// Either AsyncOpen was called, or CORS will open the channel later.
mWaitingForOnStopRequest = true ;
2007-03-22 10:30:00 -07:00
// If we're synchronous, spin an event loop here and wait
if ( ! ( mState & XML_HTTP_REQUEST_ASYNC ) ) {
2008-10-08 04:08:01 -07:00
mState | = XML_HTTP_REQUEST_SYNCLOOPING ;
2009-02-17 12:07:39 -08:00
2009-03-03 12:11:14 -08:00
nsCOMPtr < nsIDocument > suspendedDoc ;
2009-02-17 12:07:39 -08:00
nsCOMPtr < nsIRunnable > resumeTimeoutRunnable ;
if ( mOwner ) {
nsCOMPtr < nsIDOMWindow > topWindow ;
if ( NS_SUCCEEDED ( mOwner - > GetTop ( getter_AddRefs ( topWindow ) ) ) ) {
nsCOMPtr < nsPIDOMWindow > suspendedWindow ( do_QueryInterface ( topWindow ) ) ;
2009-03-03 12:11:14 -08:00
if ( suspendedWindow & &
( suspendedWindow = suspendedWindow - > GetCurrentInnerWindow ( ) ) ) {
suspendedDoc = do_QueryInterface ( suspendedWindow - > GetExtantDocument ( ) ) ;
if ( suspendedDoc ) {
suspendedDoc - > SuppressEventHandling ( ) ;
}
2011-10-17 07:59:28 -07:00
suspendedWindow - > SuspendTimeouts ( 1 , false ) ;
2009-03-03 12:11:14 -08:00
resumeTimeoutRunnable = new nsResumeTimeoutsEvent ( suspendedWindow ) ;
2009-02-17 12:07:39 -08:00
}
}
}
2010-09-17 02:30:35 -07:00
ChangeState ( XML_HTTP_REQUEST_SENT ) ;
// Note, calling ChangeState may have cleared
// XML_HTTP_REQUEST_SYNCLOOPING flag.
2007-03-22 10:30:00 -07:00
nsIThread * thread = NS_GetCurrentThread ( ) ;
while ( mState & XML_HTTP_REQUEST_SYNCLOOPING ) {
if ( ! NS_ProcessNextEvent ( thread ) ) {
rv = NS_ERROR_UNEXPECTED ;
break ;
}
}
2009-02-17 12:07:39 -08:00
2009-03-03 12:11:14 -08:00
if ( suspendedDoc ) {
2011-10-17 07:59:28 -07:00
suspendedDoc - > UnsuppressEventHandlingAndFireEvents ( true ) ;
2009-03-03 12:11:14 -08:00
}
2009-02-17 12:07:39 -08:00
if ( resumeTimeoutRunnable ) {
NS_DispatchToCurrentThread ( resumeTimeoutRunnable ) ;
}
2008-08-14 04:07:46 -07:00
} else {
2010-09-17 02:30:35 -07:00
// Now that we've successfully opened the channel, we can change state. Note
// that this needs to come after the AsyncOpen() and rv check, because this
// can run script that would try to restart this request, and that could end
// up doing our AsyncOpen on a null channel if the reentered AsyncOpen fails.
ChangeState ( XML_HTTP_REQUEST_SENT ) ;
2011-12-17 22:00:47 -08:00
if ( ( ! mUploadComplete & &
HasListenersFor ( NS_LITERAL_STRING ( UPLOADPROGRESS_STR ) ) ) | |
2008-10-19 14:26:37 -07:00
( mUpload & & mUpload - > HasListenersFor ( NS_LITERAL_STRING ( PROGRESS_STR ) ) ) ) {
StartProgressEventTimer ( ) ;
}
2011-10-17 07:59:28 -07:00
DispatchProgressEvent ( this , NS_LITERAL_STRING ( LOADSTART_STR ) , false ,
2008-08-14 04:07:46 -07:00
0 , 0 ) ;
if ( mUpload & & ! mUploadComplete ) {
2011-10-17 07:59:28 -07:00
DispatchProgressEvent ( mUpload , NS_LITERAL_STRING ( LOADSTART_STR ) , true ,
2008-08-14 04:07:46 -07:00
0 , mUploadTotal ) ;
}
2007-03-22 10:30:00 -07:00
}
if ( ! mChannel ) {
return NS_ERROR_FAILURE ;
}
return rv ;
}
/* void setRequestHeader (in AUTF8String header, in AUTF8String value); */
NS_IMETHODIMP
nsXMLHttpRequest : : SetRequestHeader ( const nsACString & header ,
const nsACString & value )
{
2007-07-26 19:49:18 -07:00
nsresult rv ;
2011-03-28 13:18:45 -07:00
// Make sure we don't store an invalid header name in mCORSUnsafeHeaders
2008-09-30 17:50:42 -07:00
if ( ! IsValidHTTPToken ( header ) ) {
return NS_ERROR_FAILURE ;
}
2008-09-30 17:49:30 -07:00
// Check that we haven't already opened the channel. We can't rely on
// the channel throwing from mChannel->SetRequestHeader since we might
2011-03-28 13:18:45 -07:00
// still be waiting for mCORSPreflightChannel to actually open mChannel
if ( mCORSPreflightChannel ) {
2011-09-28 23:19:26 -07:00
bool pending ;
2011-03-28 13:18:45 -07:00
rv = mCORSPreflightChannel - > IsPending ( & pending ) ;
2008-09-30 17:49:30 -07:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-10-14 17:12:28 -07:00
2008-09-30 17:49:30 -07:00
if ( pending ) {
return NS_ERROR_IN_PROGRESS ;
}
}
2011-07-25 15:08:44 -07:00
if ( ! ( mState & XML_HTTP_REQUEST_OPENED ) )
2010-03-21 18:52:35 -07:00
return NS_ERROR_IN_PROGRESS ;
2007-07-30 17:42:16 -07:00
if ( ! mChannel ) // open() initializes mChannel, and open()
2007-03-22 10:30:00 -07:00
return NS_ERROR_FAILURE ; // must be called before first setRequestHeader()
2007-07-30 17:42:16 -07:00
nsCOMPtr < nsIHttpChannel > httpChannel ( do_QueryInterface ( mChannel ) ) ;
if ( ! httpChannel ) {
return NS_OK ;
}
2007-03-22 10:30:00 -07:00
// Prevent modification to certain HTTP headers (see bug 302263), unless
2011-12-30 09:35:39 -08:00
// the executing script has UniversalXPConnect.
2007-03-22 10:30:00 -07:00
2011-09-28 23:19:26 -07:00
bool privileged ;
2011-12-30 09:35:39 -08:00
rv = IsCapabilityEnabled ( " UniversalXPConnect " , & privileged ) ;
2007-03-22 10:30:00 -07:00
if ( NS_FAILED ( rv ) )
return NS_ERROR_FAILURE ;
if ( ! privileged ) {
2007-07-26 19:49:18 -07:00
// Check for dangerous headers
2007-03-22 10:30:00 -07:00
const char * kInvalidHeaders [ ] = {
2010-11-15 03:55:30 -08:00
" accept-charset " , " accept-encoding " , " access-control-request-headers " ,
" access-control-request-method " , " connection " , " content-length " ,
" cookie " , " cookie2 " , " content-transfer-encoding " , " date " , " expect " ,
" host " , " keep-alive " , " origin " , " referer " , " te " , " trailer " ,
" transfer-encoding " , " upgrade " , " user-agent " , " via "
2007-03-22 10:30:00 -07:00
} ;
2007-07-26 19:49:18 -07:00
PRUint32 i ;
2011-10-10 22:50:08 -07:00
for ( i = 0 ; i < ArrayLength ( kInvalidHeaders ) ; + + i ) {
2007-03-22 10:30:00 -07:00
if ( header . LowerCaseEqualsASCII ( kInvalidHeaders [ i ] ) ) {
NS_WARNING ( " refusing to set request header " ) ;
return NS_OK ;
}
}
2008-07-12 16:06:36 -07:00
if ( StringBeginsWith ( header , NS_LITERAL_CSTRING ( " proxy- " ) ,
nsCaseInsensitiveCStringComparator ( ) ) | |
StringBeginsWith ( header , NS_LITERAL_CSTRING ( " sec- " ) ,
nsCaseInsensitiveCStringComparator ( ) ) ) {
NS_WARNING ( " refusing to set request header " ) ;
return NS_OK ;
}
2008-09-30 17:49:30 -07:00
// Check for dangerous cross-site headers
2011-03-17 09:19:13 -07:00
bool safeHeader = IsSystemXHR ( ) ;
2008-09-30 17:49:30 -07:00
if ( ! safeHeader ) {
2010-10-04 17:41:07 -07:00
// Content-Type isn't always safe, but we'll deal with it in Send()
2008-09-30 17:49:30 -07:00
const char * kCrossOriginSafeHeaders [ ] = {
2010-10-04 17:41:07 -07:00
" accept " , " accept-language " , " content-language " , " content-type " ,
" last-event-id "
2008-09-30 17:49:30 -07:00
} ;
2011-10-10 22:50:08 -07:00
for ( i = 0 ; i < ArrayLength ( kCrossOriginSafeHeaders ) ; + + i ) {
2008-09-30 17:49:30 -07:00
if ( header . LowerCaseEqualsASCII ( kCrossOriginSafeHeaders [ i ] ) ) {
2011-03-17 09:19:13 -07:00
safeHeader = true ;
2008-09-30 17:49:30 -07:00
break ;
}
}
}
if ( ! safeHeader ) {
2011-03-28 13:18:45 -07:00
mCORSUnsafeHeaders . AppendElement ( header ) ;
2008-09-30 17:49:30 -07:00
}
2007-03-22 10:30:00 -07:00
}
2007-07-26 19:49:18 -07:00
// We need to set, not add to, the header.
2011-10-17 07:59:28 -07:00
rv = httpChannel - > SetRequestHeader ( header , value , false ) ;
2010-03-21 18:52:35 -07:00
if ( NS_SUCCEEDED ( rv ) ) {
// We'll want to duplicate this header for any replacement channels (eg. on redirect)
RequestHeader reqHeader = {
nsCString ( header ) , nsCString ( value )
} ;
mModifiedRequestHeaders . AppendElement ( reqHeader ) ;
}
return rv ;
2007-03-22 10:30:00 -07:00
}
2012-01-26 02:02:22 -08:00
/* attribute unsigned long timeout; */
NS_IMETHODIMP
nsXMLHttpRequest : : GetTimeout ( PRUint32 * aTimeout )
{
* aTimeout = mTimeoutMilliseconds ;
return NS_OK ;
}
NS_IMETHODIMP
nsXMLHttpRequest : : SetTimeout ( PRUint32 aTimeout )
{
if ( ( mState & ( XML_HTTP_REQUEST_ASYNC | XML_HTTP_REQUEST_UNSENT ) ) | | ! mOwner ) {
mTimeoutMilliseconds = aTimeout ;
if ( mRequestSentTime ) {
StartTimeoutTimer ( ) ;
}
return NS_OK ;
}
/* Timeout is not supported for synchronous requests with an owning window,
per XHR2 spec . */
LogMessage ( " TimeoutSyncXHRWarning " , mOwner ) ;
return NS_ERROR_DOM_INVALID_ACCESS_ERR ;
}
void
nsXMLHttpRequest : : StartTimeoutTimer ( )
{
NS_ABORT_IF_FALSE ( mRequestSentTime ,
" StartTimeoutTimer mustn't be called before the request was sent! " ) ;
if ( mState & XML_HTTP_REQUEST_DONE ) {
// do nothing!
return ;
}
if ( mTimeoutTimer ) {
mTimeoutTimer - > Cancel ( ) ;
}
if ( ! mTimeoutMilliseconds ) {
return ;
}
if ( ! mTimeoutTimer ) {
mTimeoutTimer = do_CreateInstance ( NS_TIMER_CONTRACTID ) ;
}
PRUint32 elapsed =
( PRUint32 ) ( ( PR_Now ( ) - mRequestSentTime ) / PR_USEC_PER_MSEC ) ;
mTimeoutTimer - > InitWithCallback (
this ,
mTimeoutMilliseconds > elapsed ? mTimeoutMilliseconds - elapsed : 0 ,
nsITimer : : TYPE_ONE_SHOT
) ;
}
2007-03-22 10:30:00 -07:00
/* readonly attribute long readyState; */
NS_IMETHODIMP
2011-05-10 16:18:55 -07:00
nsXMLHttpRequest : : GetReadyState ( PRUint16 * aState )
2007-03-22 10:30:00 -07:00
{
NS_ENSURE_ARG_POINTER ( aState ) ;
// Translate some of our internal states for external consumers
2011-05-10 16:18:55 -07:00
if ( mState & XML_HTTP_REQUEST_UNSENT ) {
* aState = UNSENT ;
2007-03-22 10:30:00 -07:00
} else if ( mState & ( XML_HTTP_REQUEST_OPENED | XML_HTTP_REQUEST_SENT ) ) {
2011-05-10 16:18:55 -07:00
* aState = OPENED ;
} else if ( mState & XML_HTTP_REQUEST_HEADERS_RECEIVED ) {
* aState = HEADERS_RECEIVED ;
} else if ( mState & ( XML_HTTP_REQUEST_LOADING | XML_HTTP_REQUEST_STOPPED ) ) {
* aState = LOADING ;
} else if ( mState & XML_HTTP_REQUEST_DONE ) {
* aState = DONE ;
2007-03-22 10:30:00 -07:00
} else {
NS_ERROR ( " Should not happen " ) ;
}
return NS_OK ;
}
/* void overrideMimeType(in AUTF8String mimetype); */
NS_IMETHODIMP
nsXMLHttpRequest : : OverrideMimeType ( const nsACString & aMimeType )
{
// XXX Should we do some validation here?
mOverrideMimeType . Assign ( aMimeType ) ;
return NS_OK ;
}
/* attribute boolean multipart; */
NS_IMETHODIMP
2011-09-28 23:19:26 -07:00
nsXMLHttpRequest : : GetMultipart ( bool * _retval )
2007-03-22 10:30:00 -07:00
{
2008-02-09 22:16:30 -08:00
* _retval = ! ! ( mState & XML_HTTP_REQUEST_MULTIPART ) ;
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
/* attribute boolean multipart; */
NS_IMETHODIMP
2011-09-28 23:19:26 -07:00
nsXMLHttpRequest : : SetMultipart ( bool aMultipart )
2007-03-22 10:30:00 -07:00
{
2011-05-10 16:18:55 -07:00
if ( ! ( mState & XML_HTTP_REQUEST_UNSENT ) ) {
2007-03-22 10:30:00 -07:00
// Can't change this while we're in the middle of something.
return NS_ERROR_IN_PROGRESS ;
}
if ( aMultipart ) {
mState | = XML_HTTP_REQUEST_MULTIPART ;
} else {
mState & = ~ XML_HTTP_REQUEST_MULTIPART ;
}
return NS_OK ;
}
2008-03-18 17:14:38 -07:00
/* attribute boolean mozBackgroundRequest; */
NS_IMETHODIMP
2011-09-28 23:19:26 -07:00
nsXMLHttpRequest : : GetMozBackgroundRequest ( bool * _retval )
2008-03-18 17:14:38 -07:00
{
* _retval = ! ! ( mState & XML_HTTP_REQUEST_BACKGROUND ) ;
return NS_OK ;
}
/* attribute boolean mozBackgroundRequest; */
NS_IMETHODIMP
2011-09-28 23:19:26 -07:00
nsXMLHttpRequest : : SetMozBackgroundRequest ( bool aMozBackgroundRequest )
2008-03-18 17:14:38 -07:00
{
2011-09-28 23:19:26 -07:00
bool privileged ;
2008-03-18 17:14:38 -07:00
nsresult rv = IsCapabilityEnabled ( " UniversalXPConnect " , & privileged ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( ! privileged )
return NS_ERROR_DOM_SECURITY_ERR ;
2011-05-10 16:18:55 -07:00
if ( ! ( mState & XML_HTTP_REQUEST_UNSENT ) ) {
2008-03-18 17:14:38 -07:00
// Can't change this while we're in the middle of something.
return NS_ERROR_IN_PROGRESS ;
}
if ( aMozBackgroundRequest ) {
mState | = XML_HTTP_REQUEST_BACKGROUND ;
} else {
mState & = ~ XML_HTTP_REQUEST_BACKGROUND ;
}
return NS_OK ;
}
2008-09-30 17:52:52 -07:00
/* attribute boolean withCredentials; */
NS_IMETHODIMP
2011-09-28 23:19:26 -07:00
nsXMLHttpRequest : : GetWithCredentials ( bool * _retval )
2008-09-30 17:52:52 -07:00
{
* _retval = ! ! ( mState & XML_HTTP_REQUEST_AC_WITH_CREDENTIALS ) ;
return NS_OK ;
}
/* attribute boolean withCredentials; */
NS_IMETHODIMP
2011-09-28 23:19:26 -07:00
nsXMLHttpRequest : : SetWithCredentials ( bool aWithCredentials )
2008-09-30 17:52:52 -07:00
{
// Return error if we're already processing a request
if ( XML_HTTP_REQUEST_SENT & mState ) {
return NS_ERROR_FAILURE ;
}
2011-12-08 05:54:05 -08:00
// sync request is not allowed setting withCredentials in window context
if ( mOwner & &
! ( mState & ( XML_HTTP_REQUEST_UNSENT | XML_HTTP_REQUEST_ASYNC ) ) ) {
LogMessage ( " WithCredentialsSyncXHRWarning " , mOwner ) ;
return NS_ERROR_DOM_INVALID_ACCESS_ERR ;
}
2008-09-30 17:52:52 -07:00
if ( aWithCredentials ) {
mState | = XML_HTTP_REQUEST_AC_WITH_CREDENTIALS ;
}
else {
mState & = ~ XML_HTTP_REQUEST_AC_WITH_CREDENTIALS ;
}
return NS_OK ;
}
2007-03-22 10:30:00 -07:00
nsresult
2011-09-28 23:19:26 -07:00
nsXMLHttpRequest : : ChangeState ( PRUint32 aState , bool aBroadcast )
2007-03-22 10:30:00 -07:00
{
// If we are setting one of the mutually exclusive states,
// unset those state bits first.
if ( aState & XML_HTTP_REQUEST_LOADSTATES ) {
mState & = ~ XML_HTTP_REQUEST_LOADSTATES ;
}
mState | = aState ;
nsresult rv = NS_OK ;
2008-10-19 14:26:37 -07:00
if ( mProgressNotifier & &
2011-05-10 16:18:55 -07:00
! ( aState & ( XML_HTTP_REQUEST_HEADERS_RECEIVED | XML_HTTP_REQUEST_LOADING ) ) ) {
2012-01-26 02:02:22 -08:00
mProgressTimerIsActive = false ;
2008-10-19 14:26:37 -07:00
mProgressNotifier - > Cancel ( ) ;
}
2010-09-17 02:30:35 -07:00
if ( ( aState & XML_HTTP_REQUEST_LOADSTATES ) & & // Broadcast load states only
aBroadcast & &
( mState & XML_HTTP_REQUEST_ASYNC | |
aState & XML_HTTP_REQUEST_OPENED | |
2011-05-10 16:18:55 -07:00
aState & XML_HTTP_REQUEST_DONE ) ) {
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsIDOMEvent > event ;
2008-08-14 04:07:46 -07:00
rv = CreateReadystatechangeEvent ( getter_AddRefs ( event ) ) ;
2007-03-22 10:30:00 -07:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-08-14 04:04:43 -07:00
DispatchDOMEvent ( nsnull , event , nsnull , nsnull ) ;
2007-03-22 10:30:00 -07:00
}
return rv ;
}
2010-08-04 19:15:55 -07:00
/*
* Simple helper class that just forwards the redirect callback back
* to the nsXMLHttpRequest .
*/
class AsyncVerifyRedirectCallbackForwarder : public nsIAsyncVerifyRedirectCallback
{
public :
AsyncVerifyRedirectCallbackForwarder ( nsXMLHttpRequest * xhr )
: mXHR ( xhr )
{
}
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS ( AsyncVerifyRedirectCallbackForwarder )
// nsIAsyncVerifyRedirectCallback implementation
NS_IMETHOD OnRedirectVerifyCallback ( nsresult result )
{
mXHR - > OnRedirectVerifyCallback ( result ) ;
return NS_OK ;
}
private :
nsRefPtr < nsXMLHttpRequest > mXHR ;
} ;
NS_IMPL_CYCLE_COLLECTION_CLASS ( AsyncVerifyRedirectCallbackForwarder )
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN ( AsyncVerifyRedirectCallbackForwarder )
2011-06-14 00:53:08 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS ( mXHR , nsIXMLHttpRequest )
2010-08-04 19:15:55 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN ( AsyncVerifyRedirectCallbackForwarder )
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mXHR )
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION ( AsyncVerifyRedirectCallbackForwarder )
NS_INTERFACE_MAP_ENTRY ( nsISupports )
NS_INTERFACE_MAP_ENTRY ( nsIAsyncVerifyRedirectCallback )
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF ( AsyncVerifyRedirectCallbackForwarder )
NS_IMPL_CYCLE_COLLECTING_RELEASE ( AsyncVerifyRedirectCallbackForwarder )
2007-03-22 10:30:00 -07:00
/////////////////////////////////////////////////////
// nsIChannelEventSink methods:
//
NS_IMETHODIMP
2010-08-04 19:15:55 -07:00
nsXMLHttpRequest : : AsyncOnChannelRedirect ( nsIChannel * aOldChannel ,
nsIChannel * aNewChannel ,
PRUint32 aFlags ,
nsIAsyncVerifyRedirectCallback * callback )
2007-03-22 10:30:00 -07:00
{
NS_PRECONDITION ( aNewChannel , " Redirect without a channel? " ) ;
2007-07-26 19:49:18 -07:00
nsresult rv ;
2008-04-04 02:01:22 -07:00
2009-01-21 14:50:28 -08:00
if ( ! NS_IsInternalSameURIRedirect ( aOldChannel , aNewChannel , aFlags ) ) {
rv = CheckChannelForCrossSiteRequest ( aNewChannel ) ;
2010-08-04 19:15:55 -07:00
if ( NS_FAILED ( rv ) ) {
NS_WARNING ( " nsXMLHttpRequest::OnChannelRedirect: "
" CheckChannelForCrossSiteRequest returned failure " ) ;
return rv ;
}
2008-04-04 02:01:22 -07:00
2009-01-21 14:50:28 -08:00
// Disable redirects for preflighted cross-site requests entirely for now
// Note, do this after the call to CheckChannelForCrossSiteRequest
// to make sure that XML_HTTP_REQUEST_USE_XSITE_AC is up-to-date
if ( ( mState & XML_HTTP_REQUEST_NEED_AC_PREFLIGHT ) ) {
return NS_ERROR_DOM_BAD_URI ;
}
2008-04-04 02:01:22 -07:00
}
2010-08-04 19:15:55 -07:00
// Prepare to receive callback
mRedirectCallback = callback ;
mNewRedirectChannel = aNewChannel ;
2007-03-22 10:30:00 -07:00
if ( mChannelEventSink ) {
2010-08-04 19:15:55 -07:00
nsRefPtr < AsyncVerifyRedirectCallbackForwarder > fwd =
new AsyncVerifyRedirectCallbackForwarder ( this ) ;
rv = mChannelEventSink - > AsyncOnChannelRedirect ( aOldChannel ,
aNewChannel ,
aFlags , fwd ) ;
2008-08-14 04:07:46 -07:00
if ( NS_FAILED ( rv ) ) {
2010-08-04 19:15:55 -07:00
mRedirectCallback = nsnull ;
mNewRedirectChannel = nsnull ;
2008-08-14 04:07:46 -07:00
}
2010-08-04 19:15:55 -07:00
return rv ;
2007-03-22 10:30:00 -07:00
}
2010-08-04 19:15:55 -07:00
OnRedirectVerifyCallback ( NS_OK ) ;
return NS_OK ;
}
2007-03-22 10:30:00 -07:00
2010-08-04 19:15:55 -07:00
void
nsXMLHttpRequest : : OnRedirectVerifyCallback ( nsresult result )
{
NS_ASSERTION ( mRedirectCallback , " mRedirectCallback not set in callback " ) ;
NS_ASSERTION ( mNewRedirectChannel , " mNewRedirectChannel not set in callback " ) ;
2007-07-26 19:49:18 -07:00
2010-03-21 18:52:35 -07:00
if ( NS_SUCCEEDED ( result ) ) {
2010-08-04 19:15:55 -07:00
mChannel = mNewRedirectChannel ;
2010-03-21 18:52:35 -07:00
nsCOMPtr < nsIHttpChannel > httpChannel ( do_QueryInterface ( mChannel ) ) ;
if ( httpChannel ) {
// Ensure all original headers are duplicated for the new channel (bug #553888)
for ( PRUint32 i = mModifiedRequestHeaders . Length ( ) ; i > 0 ; ) {
- - i ;
httpChannel - > SetRequestHeader ( mModifiedRequestHeaders [ i ] . header ,
mModifiedRequestHeaders [ i ] . value ,
2011-10-17 07:59:28 -07:00
false ) ;
2010-03-21 18:52:35 -07:00
}
}
} else {
2011-10-17 07:59:28 -07:00
mErrorLoad = true ;
2010-03-21 18:52:35 -07:00
}
2010-08-04 19:15:55 -07:00
mNewRedirectChannel = nsnull ;
mRedirectCallback - > OnRedirectVerifyCallback ( result ) ;
mRedirectCallback = nsnull ;
2007-03-22 10:30:00 -07:00
}
/////////////////////////////////////////////////////
// nsIProgressEventSink methods:
//
2011-09-23 18:57:22 -07:00
void
2011-09-28 23:19:26 -07:00
nsXMLHttpRequest : : MaybeDispatchProgressEvents ( bool aFinalProgress )
2011-09-23 18:57:22 -07:00
{
2012-01-26 02:02:22 -08:00
if ( aFinalProgress & & mProgressTimerIsActive ) {
mProgressTimerIsActive = false ;
2011-09-23 18:57:22 -07:00
mProgressNotifier - > Cancel ( ) ;
}
2012-01-26 02:02:22 -08:00
if ( mProgressTimerIsActive | |
2011-09-23 18:57:22 -07:00
! mProgressSinceLastProgressEvent | |
mErrorLoad | |
! ( mState & XML_HTTP_REQUEST_ASYNC ) ) {
return ;
}
if ( ! aFinalProgress ) {
StartProgressEventTimer ( ) ;
}
// We're uploading if our state is XML_HTTP_REQUEST_OPENED or
// XML_HTTP_REQUEST_SENT
if ( ( XML_HTTP_REQUEST_OPENED | XML_HTTP_REQUEST_SENT ) & mState ) {
if ( aFinalProgress ) {
mUploadTotal = mUploadTransferred ;
mUploadProgressMax = mUploadProgress ;
2011-10-17 07:59:28 -07:00
mUploadLengthComputable = true ;
2011-09-23 18:57:22 -07:00
}
DispatchProgressEvent ( this , NS_LITERAL_STRING ( UPLOADPROGRESS_STR ) ,
2011-10-17 07:59:28 -07:00
true , mUploadLengthComputable , mUploadTransferred ,
2011-09-23 18:57:22 -07:00
mUploadTotal , mUploadProgress ,
mUploadProgressMax ) ;
if ( mUpload & & ! mUploadComplete ) {
DispatchProgressEvent ( mUpload , NS_LITERAL_STRING ( PROGRESS_STR ) ,
2011-10-17 07:59:28 -07:00
true , mUploadLengthComputable , mUploadTransferred ,
2011-09-23 18:57:22 -07:00
mUploadTotal , mUploadProgress ,
mUploadProgressMax ) ;
}
} else {
if ( aFinalProgress ) {
mLoadTotal = mLoadTransferred ;
2011-10-17 07:59:28 -07:00
mLoadLengthComputable = true ;
2011-09-23 18:57:22 -07:00
}
2011-11-24 07:28:12 -08:00
mInLoadProgressEvent = true ;
DispatchProgressEvent ( this , NS_LITERAL_STRING ( PROGRESS_STR ) ,
true , mLoadLengthComputable , mLoadTransferred ,
mLoadTotal , mLoadTransferred , mLoadTotal ) ;
mInLoadProgressEvent = false ;
2011-09-23 18:57:36 -07:00
if ( mResponseType = = XML_HTTP_RESPONSE_TYPE_CHUNKED_TEXT | |
mResponseType = = XML_HTTP_RESPONSE_TYPE_CHUNKED_ARRAYBUFFER ) {
mResponseBody . Truncate ( ) ;
mResponseText . Truncate ( ) ;
mResultArrayBuffer = nsnull ;
}
2011-09-23 18:57:22 -07:00
}
2011-10-17 07:59:28 -07:00
mProgressSinceLastProgressEvent = false ;
2011-09-23 18:57:22 -07:00
}
2007-03-22 10:30:00 -07:00
NS_IMETHODIMP
nsXMLHttpRequest : : OnProgress ( nsIRequest * aRequest , nsISupports * aContext , PRUint64 aProgress , PRUint64 aProgressMax )
{
2008-09-11 03:44:15 -07:00
// We're in middle of processing multipart headers and we don't want to report
// any progress because upload's 'load' is dispatched when we start to load
// the first response.
if ( XML_HTTP_REQUEST_MPART_HEADERS & mState ) {
return NS_OK ;
}
2007-03-22 10:30:00 -07:00
// We're uploading if our state is XML_HTTP_REQUEST_OPENED or
// XML_HTTP_REQUEST_SENT
2011-09-28 23:19:26 -07:00
bool upload = ! ! ( ( XML_HTTP_REQUEST_OPENED | XML_HTTP_REQUEST_SENT ) & mState ) ;
2008-08-14 04:07:46 -07:00
// When uploading, OnProgress reports also headers in aProgress and aProgressMax.
// So, try to remove the headers, if possible.
2011-09-28 23:19:26 -07:00
bool lengthComputable = ( aProgressMax ! = LL_MAXUINT ) ;
2008-08-14 04:07:46 -07:00
if ( upload ) {
2011-09-23 18:57:22 -07:00
PRUint64 loaded = aProgress ;
PRUint64 total = aProgressMax ;
if ( lengthComputable ) {
2008-10-21 02:06:53 -07:00
PRUint64 headerSize = aProgressMax - mUploadTotal ;
2008-08-14 04:07:46 -07:00
loaded - = headerSize ;
total - = headerSize ;
}
2011-09-23 18:57:22 -07:00
mUploadLengthComputable = lengthComputable ;
2008-08-14 04:07:46 -07:00
mUploadTransferred = loaded ;
2008-10-19 14:26:37 -07:00
mUploadProgress = aProgress ;
mUploadProgressMax = aProgressMax ;
2011-10-17 07:59:28 -07:00
mProgressSinceLastProgressEvent = true ;
2011-09-23 18:57:22 -07:00
2011-10-17 07:59:28 -07:00
MaybeDispatchProgressEvents ( false ) ;
2008-10-19 14:26:37 -07:00
} else {
mLoadLengthComputable = lengthComputable ;
2011-09-23 18:57:22 -07:00
mLoadTotal = lengthComputable ? aProgressMax : 0 ;
// Don't dispatch progress events here. OnDataAvailable will take care
// of that.
2008-08-14 04:07:46 -07:00
}
2007-03-22 10:30:00 -07:00
if ( mProgressEventSink ) {
mProgressEventSink - > OnProgress ( aRequest , aContext , aProgress ,
aProgressMax ) ;
}
return NS_OK ;
}
NS_IMETHODIMP
nsXMLHttpRequest : : OnStatus ( nsIRequest * aRequest , nsISupports * aContext , nsresult aStatus , const PRUnichar * aStatusArg )
{
if ( mProgressEventSink ) {
mProgressEventSink - > OnStatus ( aRequest , aContext , aStatus , aStatusArg ) ;
}
return NS_OK ;
}
2011-09-28 23:19:26 -07:00
bool
2008-09-30 17:51:53 -07:00
nsXMLHttpRequest : : AllowUploadProgress ( )
{
return ! ( mState & XML_HTTP_REQUEST_USE_XSITE_AC ) | |
( mState & XML_HTTP_REQUEST_NEED_AC_PREFLIGHT ) ;
}
2007-03-22 10:30:00 -07:00
/////////////////////////////////////////////////////
// nsIInterfaceRequestor methods:
//
NS_IMETHODIMP
nsXMLHttpRequest : : GetInterface ( const nsIID & aIID , void * * aResult )
{
2008-04-08 17:27:50 -07:00
nsresult rv ;
2007-03-22 10:30:00 -07:00
// Make sure to return ourselves for the channel event sink interface and
// progress event sink interface, no matter what. We can forward these to
// mNotificationCallbacks if it wants to get notifications for them. But we
// need to see these notifications for proper functioning.
if ( aIID . Equals ( NS_GET_IID ( nsIChannelEventSink ) ) ) {
mChannelEventSink = do_GetInterface ( mNotificationCallbacks ) ;
2007-07-08 00:08:04 -07:00
* aResult = static_cast < nsIChannelEventSink * > ( this ) ;
2007-03-22 10:30:00 -07:00
NS_ADDREF_THIS ( ) ;
return NS_OK ;
} else if ( aIID . Equals ( NS_GET_IID ( nsIProgressEventSink ) ) ) {
mProgressEventSink = do_GetInterface ( mNotificationCallbacks ) ;
2007-07-08 00:08:04 -07:00
* aResult = static_cast < nsIProgressEventSink * > ( this ) ;
2007-03-22 10:30:00 -07:00
NS_ADDREF_THIS ( ) ;
return NS_OK ;
}
// Now give mNotificationCallbacks (if non-null) a chance to return the
2008-03-18 11:18:29 -07:00
// desired interface.
2007-03-22 10:30:00 -07:00
if ( mNotificationCallbacks ) {
2008-04-08 17:27:50 -07:00
rv = mNotificationCallbacks - > GetInterface ( aIID , aResult ) ;
2007-03-22 10:30:00 -07:00
if ( NS_SUCCEEDED ( rv ) ) {
NS_ASSERTION ( * aResult , " Lying nsIInterfaceRequestor implementation! " ) ;
return rv ;
}
}
2008-03-18 17:14:38 -07:00
if ( mState & XML_HTTP_REQUEST_BACKGROUND ) {
nsCOMPtr < nsIInterfaceRequestor > badCertHandler ( do_CreateInstance ( NS_BADCERTHANDLER_CONTRACTID , & rv ) ) ;
// Ignore failure to get component, we may not have all its dependencies
// available
if ( NS_SUCCEEDED ( rv ) ) {
rv = badCertHandler - > GetInterface ( aIID , aResult ) ;
if ( NS_SUCCEEDED ( rv ) )
return rv ;
}
}
2008-04-08 17:27:50 -07:00
else if ( aIID . Equals ( NS_GET_IID ( nsIAuthPrompt ) ) | |
aIID . Equals ( NS_GET_IID ( nsIAuthPrompt2 ) ) ) {
nsCOMPtr < nsIPromptFactory > wwatch =
do_GetService ( NS_WINDOWWATCHER_CONTRACTID , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// Get the an auth prompter for our window so that the parenting
// of the dialogs works as it should when using tabs.
nsCOMPtr < nsIDOMWindow > window ;
if ( mOwner ) {
window = mOwner - > GetOuterWindow ( ) ;
}
return wwatch - > GetPrompt ( window , aIID ,
reinterpret_cast < void * * > ( aResult ) ) ;
}
2008-03-18 17:14:38 -07:00
2007-03-22 10:30:00 -07:00
return QueryInterface ( aIID , aResult ) ;
}
2008-08-14 04:07:46 -07:00
NS_IMETHODIMP
nsXMLHttpRequest : : GetUpload ( nsIXMLHttpRequestUpload * * aUpload )
2008-08-14 04:04:43 -07:00
{
2008-08-14 04:07:46 -07:00
* aUpload = nsnull ;
2009-04-24 02:18:37 -07:00
nsresult rv ;
nsIScriptContext * scriptContext =
GetContextForEventHandlers ( & rv ) ;
2008-09-25 07:40:30 -07:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-08-14 04:07:46 -07:00
if ( ! mUpload ) {
2008-09-25 07:40:30 -07:00
mUpload = new nsXMLHttpRequestUpload ( mOwner , scriptContext ) ;
2008-08-14 04:07:46 -07:00
NS_ENSURE_TRUE ( mUpload , NS_ERROR_OUT_OF_MEMORY ) ;
2008-08-14 04:04:43 -07:00
}
2008-08-14 04:07:46 -07:00
NS_ADDREF ( * aUpload = mUpload ) ;
2008-08-14 04:04:43 -07:00
return NS_OK ;
}
2007-03-22 10:30:00 -07:00
2012-01-26 02:02:22 -08:00
void
nsXMLHttpRequest : : HandleTimeoutCallback ( )
{
if ( mState & XML_HTTP_REQUEST_DONE ) {
NS_NOTREACHED ( " nsXMLHttpRequest::HandleTimeoutCallback with completed request " ) ;
// do nothing!
return ;
}
CloseRequestWithError ( NS_LITERAL_STRING ( TIMEOUT_STR ) ,
XML_HTTP_REQUEST_TIMED_OUT ) ;
}
2008-10-19 14:26:37 -07:00
NS_IMETHODIMP
nsXMLHttpRequest : : Notify ( nsITimer * aTimer )
{
2012-01-26 02:02:22 -08:00
if ( mProgressNotifier = = aTimer ) {
HandleProgressTimerCallback ( ) ;
return NS_OK ;
}
if ( mTimeoutTimer = = aTimer ) {
HandleTimeoutCallback ( ) ;
return NS_OK ;
}
// Just in case some JS user wants to QI to nsITimerCallback and play with us...
NS_WARNING ( " Unexpected timer! " ) ;
return NS_ERROR_INVALID_POINTER ;
}
void
nsXMLHttpRequest : : HandleProgressTimerCallback ( )
{
mProgressTimerIsActive = false ;
2011-09-23 18:57:22 -07:00
if ( ! ( XML_HTTP_REQUEST_MPART_HEADERS & mState ) ) {
2011-10-17 07:59:28 -07:00
MaybeDispatchProgressEvents ( false ) ;
2008-10-19 14:26:37 -07:00
}
}
void
nsXMLHttpRequest : : StartProgressEventTimer ( )
{
if ( ! mProgressNotifier ) {
mProgressNotifier = do_CreateInstance ( NS_TIMER_CONTRACTID ) ;
}
if ( mProgressNotifier ) {
2011-10-17 07:59:28 -07:00
mProgressEventWasDelayed = false ;
2012-01-26 02:02:22 -08:00
mProgressTimerIsActive = true ;
2008-10-19 14:26:37 -07:00
mProgressNotifier - > Cancel ( ) ;
mProgressNotifier - > InitWithCallback ( this , NS_PROGRESS_EVENT_INTERVAL ,
nsITimer : : TYPE_ONE_SHOT ) ;
}
}
2007-03-22 10:30:00 -07:00
NS_IMPL_ISUPPORTS1 ( nsXMLHttpRequest : : nsHeaderVisitor , nsIHttpHeaderVisitor )
NS_IMETHODIMP nsXMLHttpRequest : :
nsHeaderVisitor : : VisitHeader ( const nsACString & header , const nsACString & value )
{
2008-11-13 18:18:01 -08:00
// See bug #380418. Hide "Set-Cookie" headers from non-chrome scripts.
2011-09-28 23:19:26 -07:00
bool chrome = false ; // default to false in case IsCapabilityEnabled fails
2008-11-13 18:18:01 -08:00
IsCapabilityEnabled ( " UniversalXPConnect " , & chrome ) ;
if ( ! chrome & &
( header . LowerCaseEqualsASCII ( " set-cookie " ) | |
header . LowerCaseEqualsASCII ( " set-cookie2 " ) ) ) {
NS_WARNING ( " blocked access to response header " ) ;
} else {
mHeaders . Append ( header ) ;
mHeaders . Append ( " : " ) ;
mHeaders . Append ( value ) ;
mHeaders . Append ( ' \n ' ) ;
}
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
// DOM event class to handle progress notifications
2008-08-14 04:07:46 -07:00
nsXMLHttpProgressEvent : : nsXMLHttpProgressEvent ( nsIDOMProgressEvent * aInner ,
PRUint64 aCurrentProgress ,
2011-10-01 09:14:40 -07:00
PRUint64 aMaxProgress ,
nsPIDOMWindow * aWindow )
: mWindow ( aWindow )
2007-03-22 10:30:00 -07:00
{
2008-08-14 04:07:46 -07:00
mInner = static_cast < nsDOMProgressEvent * > ( aInner ) ;
2007-03-22 10:30:00 -07:00
mCurProgress = aCurrentProgress ;
mMaxProgress = aMaxProgress ;
}
nsXMLHttpProgressEvent : : ~ nsXMLHttpProgressEvent ( )
{ }
2008-08-14 04:07:46 -07:00
NS_IMPL_CYCLE_COLLECTION_CLASS ( nsXMLHttpProgressEvent )
2010-01-12 05:08:43 -08:00
DOMCI_DATA ( XMLHttpProgressEvent , nsXMLHttpProgressEvent )
2008-08-14 04:07:46 -07:00
// QueryInterface implementation for nsXMLHttpProgressEvent
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION ( nsXMLHttpProgressEvent )
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS ( nsISupports , nsIDOMProgressEvent )
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS ( nsIDOMEvent , nsIDOMProgressEvent )
NS_INTERFACE_MAP_ENTRY ( nsIDOMNSEvent )
NS_INTERFACE_MAP_ENTRY ( nsIPrivateDOMEvent )
NS_INTERFACE_MAP_ENTRY ( nsIDOMProgressEvent )
2007-03-22 10:30:00 -07:00
NS_INTERFACE_MAP_ENTRY ( nsIDOMLSProgressEvent )
2010-03-17 08:09:05 -07:00
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO ( XMLHttpProgressEvent )
2007-03-22 10:30:00 -07:00
NS_INTERFACE_MAP_END
2008-08-14 04:07:46 -07:00
NS_IMPL_CYCLE_COLLECTING_ADDREF ( nsXMLHttpProgressEvent )
NS_IMPL_CYCLE_COLLECTING_RELEASE ( nsXMLHttpProgressEvent )
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN ( nsXMLHttpProgressEvent )
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mInner ) ;
2011-10-01 09:14:40 -07:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR ( mWindow ) ;
2008-08-14 04:07:46 -07:00
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN ( nsXMLHttpProgressEvent )
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS ( mInner ,
nsIDOMProgressEvent )
2011-10-01 09:14:40 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR ( mWindow ) ;
2008-08-14 04:07:46 -07:00
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
2007-03-22 10:30:00 -07:00
NS_IMETHODIMP nsXMLHttpProgressEvent : : GetInput ( nsIDOMLSInput * * aInput )
{
* aInput = nsnull ;
return NS_ERROR_NOT_IMPLEMENTED ;
}
2011-10-01 09:14:40 -07:00
void
nsXMLHttpProgressEvent : : WarnAboutLSProgressEvent ( nsIDocument : : DeprecatedOperations aOperation )
{
if ( ! mWindow ) {
return ;
}
nsCOMPtr < nsIDocument > document =
do_QueryInterface ( mWindow - > GetExtantDocument ( ) ) ;
if ( ! document ) {
return ;
}
document - > WarnOnceAbout ( aOperation ) ;
}
2007-03-22 10:30:00 -07:00
NS_IMETHODIMP nsXMLHttpProgressEvent : : GetPosition ( PRUint32 * aPosition )
{
2011-10-01 09:14:40 -07:00
WarnAboutLSProgressEvent ( nsIDocument : : ePosition ) ;
2007-03-22 10:30:00 -07:00
// XXX can we change the iface?
LL_L2UI ( * aPosition , mCurProgress ) ;
return NS_OK ;
}
NS_IMETHODIMP nsXMLHttpProgressEvent : : GetTotalSize ( PRUint32 * aTotalSize )
{
2011-10-01 09:14:40 -07:00
WarnAboutLSProgressEvent ( nsIDocument : : eTotalSize ) ;
2007-03-22 10:30:00 -07:00
// XXX can we change the iface?
LL_L2UI ( * aTotalSize , mMaxProgress ) ;
return NS_OK ;
}