LCOV - code coverage report
Current view: directory - netwerk/protocol/file - nsFileChannel.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 181 141 77.9 %
Date: 2012-06-02 Functions: 26 24 92.3 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* vim:set ts=2 sw=2 sts=2 et cin: */
       3                 : /* ***** BEGIN LICENSE BLOCK *****
       4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       5                 :  *
       6                 :  * The contents of this file are subject to the Mozilla Public License Version
       7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       8                 :  * the License. You may obtain a copy of the License at
       9                 :  * http://www.mozilla.org/MPL/
      10                 :  *
      11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      13                 :  * for the specific language governing rights and limitations under the
      14                 :  * License.
      15                 :  *
      16                 :  * The Original Code is mozilla.org code.
      17                 :  *
      18                 :  * The Initial Developer of the Original Code is
      19                 :  * Netscape Communications Corporation.
      20                 :  * Portions created by the Initial Developer are Copyright (C) 1998
      21                 :  * the Initial Developer. All Rights Reserved.
      22                 :  *
      23                 :  * Contributor(s):
      24                 :  *  Darin Fisher <darin@meer.net>
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      28                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #include "nsIOService.h"
      41                 : #include "nsFileChannel.h"
      42                 : #include "nsBaseContentStream.h"
      43                 : #include "nsDirectoryIndexStream.h"
      44                 : #include "nsThreadUtils.h"
      45                 : #include "nsTransportUtils.h"
      46                 : #include "nsStreamUtils.h"
      47                 : #include "nsURLHelper.h"
      48                 : #include "nsMimeTypes.h"
      49                 : #include "nsNetUtil.h"
      50                 : #include "nsProxyRelease.h"
      51                 : #include "nsAutoPtr.h"
      52                 : #include "nsStandardURL.h"
      53                 : 
      54                 : #include "nsIFileURL.h"
      55                 : #include "nsIMIMEService.h"
      56                 : 
      57                 : //-----------------------------------------------------------------------------
      58                 : 
      59               4 : class nsFileCopyEvent : public nsRunnable {
      60                 : public:
      61               1 :   nsFileCopyEvent(nsIOutputStream *dest, nsIInputStream *source, PRInt64 len)
      62                 :     : mDest(dest)
      63                 :     , mSource(source)
      64                 :     , mLen(len)
      65                 :     , mStatus(NS_OK)
      66               1 :     , mInterruptStatus(NS_OK) {
      67               1 :   }
      68                 : 
      69                 :   // Read the current status of the file copy operation.
      70               1 :   nsresult Status() { return mStatus; }
      71                 :   
      72                 :   // Call this method to perform the file copy synchronously.
      73                 :   void DoCopy();
      74                 : 
      75                 :   // Call this method to perform the file copy on a background thread.  The
      76                 :   // callback is dispatched when the file copy completes.
      77                 :   nsresult Dispatch(nsIRunnable *callback,
      78                 :                     nsITransportEventSink *sink,
      79                 :                     nsIEventTarget *target);
      80                 : 
      81                 :   // Call this method to interrupt a file copy operation that is occuring on
      82                 :   // a background thread.  The status parameter passed to this function must
      83                 :   // be a failure code and is set as the status of this file copy operation.
      84                 :   void Interrupt(nsresult status) {
      85                 :     NS_ASSERTION(NS_FAILED(status), "must be a failure code");
      86                 :     mInterruptStatus = status;
      87                 :   }
      88                 : 
      89               1 :   NS_IMETHOD Run() {
      90               1 :     DoCopy();
      91               1 :     return NS_OK;
      92                 :   }
      93                 : 
      94                 : private:
      95                 :   nsCOMPtr<nsIEventTarget> mCallbackTarget;
      96                 :   nsCOMPtr<nsIRunnable> mCallback;
      97                 :   nsCOMPtr<nsITransportEventSink> mSink;
      98                 :   nsCOMPtr<nsIOutputStream> mDest;
      99                 :   nsCOMPtr<nsIInputStream> mSource;
     100                 :   PRInt64 mLen;
     101                 :   nsresult mStatus;           // modified on i/o thread only
     102                 :   nsresult mInterruptStatus;  // modified on main thread only
     103                 : };
     104                 : 
     105                 : void
     106               1 : nsFileCopyEvent::DoCopy()
     107                 : {
     108                 :   // We'll copy in chunks this large by default.  This size affects how
     109                 :   // frequently we'll check for interrupts.
     110               1 :   const PRInt32 chunk = nsIOService::gDefaultSegmentSize * nsIOService::gDefaultSegmentCount;
     111                 : 
     112               1 :   nsresult rv = NS_OK;
     113                 : 
     114               1 :   PRInt64 len = mLen, progress = 0;
     115               3 :   while (len) {
     116                 :     // If we've been interrupted, then stop copying.
     117               1 :     rv = mInterruptStatus;
     118               1 :     if (NS_FAILED(rv))
     119               0 :       break;
     120                 : 
     121               1 :     PRInt32 num = NS_MIN((PRInt32) len, chunk);
     122                 : 
     123                 :     PRUint32 result;
     124               1 :     rv = mSource->ReadSegments(NS_CopySegmentToStream, mDest, num, &result);
     125               1 :     if (NS_FAILED(rv))
     126               0 :       break;
     127               1 :     if (result != (PRUint32) num) {
     128               0 :       rv = NS_ERROR_FILE_DISK_FULL;  // stopped prematurely (out of disk space)
     129               0 :       break;
     130                 :     }
     131                 : 
     132                 :     // Dispatch progress notification
     133               1 :     if (mSink) {
     134               1 :       progress += num;
     135               1 :       mSink->OnTransportStatus(nsnull, nsITransport::STATUS_WRITING, progress,
     136               1 :                                mLen);
     137                 :     }
     138                 :                                
     139               1 :     len -= num;
     140                 :   }
     141                 : 
     142               1 :   if (NS_FAILED(rv))
     143               0 :     mStatus = rv;
     144                 : 
     145                 :   // Close the output stream before notifying our callback so that others may
     146                 :   // freely "play" with the file.
     147               1 :   mDest->Close();
     148                 : 
     149                 :   // Notify completion
     150               1 :   if (mCallback) {
     151               1 :     mCallbackTarget->Dispatch(mCallback, NS_DISPATCH_NORMAL);
     152                 : 
     153                 :     // Release the callback on the target thread to avoid destroying stuff on
     154                 :     // the wrong thread.
     155               1 :     nsIRunnable *doomed = nsnull;
     156               1 :     mCallback.swap(doomed);
     157               1 :     NS_ProxyRelease(mCallbackTarget, doomed);
     158                 :   }
     159               1 : }
     160                 : 
     161                 : nsresult
     162               1 : nsFileCopyEvent::Dispatch(nsIRunnable *callback,
     163                 :                           nsITransportEventSink *sink,
     164                 :                           nsIEventTarget *target)
     165                 : {
     166                 :   // Use the supplied event target for all asynchronous operations.
     167                 : 
     168               1 :   mCallback = callback;
     169               1 :   mCallbackTarget = target;
     170                 : 
     171                 :   // Build a coalescing proxy for progress events
     172               1 :   nsresult rv = net_NewTransportEventSinkProxy(getter_AddRefs(mSink), sink,
     173               1 :                                                target, true);
     174               1 :   if (NS_FAILED(rv))
     175               0 :     return rv;
     176                 : 
     177                 :   // Dispatch ourselves to I/O thread pool...
     178                 :   nsCOMPtr<nsIEventTarget> pool =
     179               2 :       do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
     180               1 :   if (NS_FAILED(rv))
     181               0 :     return rv;
     182                 : 
     183               1 :   return pool->Dispatch(this, NS_DISPATCH_NORMAL);
     184                 : }
     185                 : 
     186                 : //-----------------------------------------------------------------------------
     187                 : 
     188                 : // This is a dummy input stream that when read, performs the file copy.  The
     189                 : // copy happens on a background thread via mCopyEvent.
     190                 : 
     191               4 : class nsFileUploadContentStream : public nsBaseContentStream {
     192                 : public:
     193                 :   NS_DECL_ISUPPORTS_INHERITED
     194                 : 
     195               1 :   nsFileUploadContentStream(bool nonBlocking,
     196                 :                             nsIOutputStream *dest,
     197                 :                             nsIInputStream *source,
     198                 :                             PRInt64 len,
     199                 :                             nsITransportEventSink *sink)
     200                 :     : nsBaseContentStream(nonBlocking)
     201               1 :     , mCopyEvent(new nsFileCopyEvent(dest, source, len))
     202               3 :     , mSink(sink) {
     203               1 :   }
     204                 : 
     205               1 :   bool IsInitialized() {
     206               1 :     return mCopyEvent != nsnull;
     207                 :   }
     208                 : 
     209                 :   NS_IMETHODIMP ReadSegments(nsWriteSegmentFun fun, void *closure,
     210                 :                              PRUint32 count, PRUint32 *result);
     211                 :   NS_IMETHODIMP AsyncWait(nsIInputStreamCallback *callback, PRUint32 flags,
     212                 :                           PRUint32 count, nsIEventTarget *target);
     213                 : 
     214                 : private:
     215                 :   void OnCopyComplete();
     216                 : 
     217                 :   nsRefPtr<nsFileCopyEvent> mCopyEvent;
     218                 :   nsCOMPtr<nsITransportEventSink> mSink;
     219                 : };
     220                 : 
     221              23 : NS_IMPL_ISUPPORTS_INHERITED0(nsFileUploadContentStream,
     222                 :                              nsBaseContentStream)
     223                 : 
     224                 : NS_IMETHODIMP
     225               0 : nsFileUploadContentStream::ReadSegments(nsWriteSegmentFun fun, void *closure,
     226                 :                                         PRUint32 count, PRUint32 *result)
     227                 : {
     228               0 :   *result = 0;  // nothing is ever actually read from this stream
     229                 : 
     230               0 :   if (IsClosed())
     231               0 :     return NS_OK;
     232                 : 
     233               0 :   if (IsNonBlocking()) {
     234                 :     // Inform the caller that they will have to wait for the copy operation to
     235                 :     // complete asynchronously.  We'll kick of the copy operation once they
     236                 :     // call AsyncWait.
     237               0 :     return NS_BASE_STREAM_WOULD_BLOCK;  
     238                 :   }
     239                 : 
     240                 :   // Perform copy synchronously, and then close out the stream.
     241               0 :   mCopyEvent->DoCopy();
     242               0 :   nsresult status = mCopyEvent->Status();
     243               0 :   CloseWithStatus(NS_FAILED(status) ? status : NS_BASE_STREAM_CLOSED);
     244               0 :   return status;
     245                 : }
     246                 : 
     247                 : NS_IMETHODIMP
     248               1 : nsFileUploadContentStream::AsyncWait(nsIInputStreamCallback *callback,
     249                 :                                      PRUint32 flags, PRUint32 count,
     250                 :                                      nsIEventTarget *target)
     251                 : {
     252               1 :   nsresult rv = nsBaseContentStream::AsyncWait(callback, flags, count, target);
     253               1 :   if (NS_FAILED(rv) || IsClosed())
     254               0 :     return rv;
     255                 : 
     256               1 :   if (IsNonBlocking()) {
     257                 :     nsCOMPtr<nsIRunnable> callback =
     258               2 :       NS_NewRunnableMethod(this, &nsFileUploadContentStream::OnCopyComplete);
     259               1 :     mCopyEvent->Dispatch(callback, mSink, target);
     260                 :   }
     261                 : 
     262               1 :   return NS_OK;
     263                 : }
     264                 : 
     265                 : void
     266               1 : nsFileUploadContentStream::OnCopyComplete()
     267                 : {
     268                 :   // This method is being called to indicate that we are done copying.
     269               1 :   nsresult status = mCopyEvent->Status();
     270                 : 
     271               1 :   CloseWithStatus(NS_FAILED(status) ? status : NS_BASE_STREAM_CLOSED);
     272               1 : }
     273                 : 
     274                 : //-----------------------------------------------------------------------------
     275                 : 
     276           49867 : nsFileChannel::nsFileChannel(nsIURI *uri) 
     277                 : {
     278                 :   // If we have a link file, we should resolve its target right away.
     279                 :   // This is to protect against a same origin attack where the same link file
     280                 :   // can point to different resources right after the first resource is loaded.
     281           99734 :   nsCOMPtr<nsIFile> file;
     282           99734 :   nsCOMPtr <nsIURI> targetURI;
     283           99734 :   nsCAutoString fileTarget;
     284           99734 :   nsCOMPtr<nsILocalFile> resolvedFile;
     285                 :   bool symLink;
     286           99734 :   nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(uri);
     287          300732 :   if (fileURL && 
     288          199468 :       NS_SUCCEEDED(fileURL->GetFile(getter_AddRefs(file))) &&
     289           49867 :       NS_SUCCEEDED(file->IsSymlink(&symLink)) && 
     290                 :       symLink &&
     291             306 :       NS_SUCCEEDED(file->GetNativeTarget(fileTarget)) &&
     292           50479 :       NS_SUCCEEDED(NS_NewNativeLocalFile(fileTarget, PR_TRUE, 
     293                 :                                          getter_AddRefs(resolvedFile))) &&
     294           50479 :       NS_SUCCEEDED(NS_NewFileURI(getter_AddRefs(targetURI), 
     295                 :                    resolvedFile, nsnull))) {
     296             306 :     SetURI(targetURI);
     297             306 :     SetOriginalURI(uri);
     298             306 :     nsLoadFlags loadFlags = 0;
     299             306 :     GetLoadFlags(&loadFlags);
     300             306 :     SetLoadFlags(loadFlags | nsIChannel::LOAD_REPLACE);
     301                 :   } else {
     302           49561 :     SetURI(uri);
     303                 :   }
     304           49867 : }
     305                 : 
     306                 : nsresult
     307            2913 : nsFileChannel::MakeFileInputStream(nsIFile *file,
     308                 :                                    nsCOMPtr<nsIInputStream> &stream,
     309                 :                                    nsCString &contentType)
     310                 : {
     311                 :   // we accept that this might result in a disk hit to stat the file
     312                 :   bool isDir;
     313            2913 :   nsresult rv = file->IsDirectory(&isDir);
     314            2913 :   if (NS_FAILED(rv)) {
     315                 :     // canonicalize error message
     316              51 :     if (rv == NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)
     317              51 :       rv = NS_ERROR_FILE_NOT_FOUND;
     318              51 :     return rv;
     319                 :   }
     320                 : 
     321            2862 :   if (isDir) {
     322               3 :     rv = nsDirectoryIndexStream::Create(file, getter_AddRefs(stream));
     323               3 :     if (NS_SUCCEEDED(rv) && !HasContentTypeHint())
     324               2 :       contentType.AssignLiteral(APPLICATION_HTTP_INDEX_FORMAT);
     325                 :   } else {
     326            2859 :     rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), file);
     327            2859 :     if (NS_SUCCEEDED(rv) && !HasContentTypeHint()) {
     328                 :       // Use file extension to infer content type
     329             114 :       nsCOMPtr<nsIMIMEService> mime = do_GetService("@mozilla.org/mime;1", &rv);
     330              57 :       if (NS_SUCCEEDED(rv)) {
     331              57 :         mime->GetTypeFromFile(file, contentType);
     332                 :       }
     333                 :     }
     334                 :   }
     335            2862 :   return rv;
     336                 : }
     337                 : 
     338                 : nsresult
     339            2914 : nsFileChannel::OpenContentStream(bool async, nsIInputStream **result,
     340                 :                                  nsIChannel** channel)
     341                 : {
     342                 :   // NOTE: the resulting file is a clone, so it is safe to pass it to the
     343                 :   //       file input stream which will be read on a background thread.
     344            5828 :   nsCOMPtr<nsIFile> file;
     345            2914 :   nsresult rv = GetFile(getter_AddRefs(file));
     346            2914 :   if (NS_FAILED(rv))
     347               0 :     return rv;
     348                 : 
     349            5828 :   nsCOMPtr<nsIFileProtocolHandler> fileHandler;
     350            2914 :   rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler));
     351            2914 :   if (NS_FAILED(rv))
     352               0 :     return rv;
     353                 :     
     354            5828 :   nsCOMPtr<nsIURI> newURI;
     355            2914 :   rv = fileHandler->ReadURLFile(file, getter_AddRefs(newURI));
     356            2914 :   if (NS_SUCCEEDED(rv)) {
     357               0 :     nsCOMPtr<nsIChannel> newChannel;
     358               0 :     rv = NS_NewChannel(getter_AddRefs(newChannel), newURI);
     359               0 :     if (NS_FAILED(rv))
     360               0 :       return rv;
     361                 : 
     362               0 :     *result = nsnull;
     363               0 :     newChannel.forget(channel);
     364               0 :     return NS_OK;
     365                 :   }
     366                 : 
     367            5828 :   nsCOMPtr<nsIInputStream> stream;
     368                 : 
     369            2914 :   if (mUploadStream) {
     370                 :     // Pass back a nsFileUploadContentStream instance that knows how to perform
     371                 :     // the file copy when "read" (the resulting stream in this case does not
     372                 :     // actually return any data).
     373                 : 
     374               2 :     nsCOMPtr<nsIOutputStream> fileStream;
     375               1 :     rv = NS_NewLocalFileOutputStream(getter_AddRefs(fileStream), file,
     376                 :                                      PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
     377               1 :                                      PR_IRUSR | PR_IWUSR);
     378               1 :     if (NS_FAILED(rv))
     379               0 :       return rv;
     380                 : 
     381                 :     nsFileUploadContentStream *uploadStream =
     382                 :         new nsFileUploadContentStream(async, fileStream, mUploadStream,
     383               2 :                                       mUploadLength, this);
     384               1 :     if (!uploadStream || !uploadStream->IsInitialized()) {
     385               0 :       delete uploadStream;
     386               0 :       return NS_ERROR_OUT_OF_MEMORY;
     387                 :     }
     388               1 :     stream = uploadStream;
     389                 : 
     390               1 :     SetContentLength64(0);
     391                 : 
     392                 :     // Since there isn't any content to speak of we just set the content-type
     393                 :     // to something other than "unknown" to avoid triggering the content-type
     394                 :     // sniffer code in nsBaseChannel.
     395                 :     // However, don't override explicitly set types.
     396               1 :     if (!HasContentTypeHint())
     397               0 :       SetContentType(NS_LITERAL_CSTRING(APPLICATION_OCTET_STREAM));
     398                 :   } else {
     399            5826 :     nsCAutoString contentType;
     400            2913 :     rv = MakeFileInputStream(file, stream, contentType);
     401            2913 :     if (NS_FAILED(rv))
     402              51 :       return rv;
     403                 : 
     404            2862 :     EnableSynthesizedProgressEvents(true);
     405                 : 
     406                 :     // fixup content length and type
     407            2862 :     if (ContentLength64() < 0) {
     408                 :       PRInt64 size;
     409            2862 :       rv = file->GetFileSize(&size);
     410            2862 :       if (NS_FAILED(rv))
     411               0 :         return rv;
     412            2862 :       SetContentLength64(size);
     413                 :     }
     414            2862 :     if (!contentType.IsEmpty())
     415              54 :       SetContentType(contentType);
     416                 :   }
     417                 : 
     418            2863 :   *result = nsnull;
     419            2863 :   stream.swap(*result);
     420            2863 :   return NS_OK;
     421                 : }
     422                 : 
     423                 : //-----------------------------------------------------------------------------
     424                 : // nsFileChannel::nsISupports
     425                 : 
     426          274350 : NS_IMPL_ISUPPORTS_INHERITED2(nsFileChannel,
     427                 :                              nsBaseChannel,
     428                 :                              nsIUploadChannel,
     429                 :                              nsIFileChannel)
     430                 : 
     431                 : //-----------------------------------------------------------------------------
     432                 : // nsFileChannel::nsIFileChannel
     433                 : 
     434                 : NS_IMETHODIMP
     435            4802 : nsFileChannel::GetFile(nsIFile **file)
     436                 : {
     437            9604 :     nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(URI());
     438            4802 :     NS_ENSURE_STATE(fileURL);
     439                 : 
     440                 :     // This returns a cloned nsIFile
     441            4802 :     return fileURL->GetFile(file);
     442                 : }
     443                 : 
     444                 : //-----------------------------------------------------------------------------
     445                 : // nsFileChannel::nsIUploadChannel
     446                 : 
     447                 : NS_IMETHODIMP
     448               1 : nsFileChannel::SetUploadStream(nsIInputStream *stream,
     449                 :                                const nsACString &contentType,
     450                 :                                PRInt32 contentLength)
     451                 : {
     452               1 :   NS_ENSURE_TRUE(!IsPending(), NS_ERROR_IN_PROGRESS);
     453                 : 
     454               1 :   if ((mUploadStream = stream)) {
     455               1 :     mUploadLength = contentLength;
     456               1 :     if (mUploadLength < 0) {
     457                 :       // Make sure we know how much data we are uploading.
     458                 :       PRUint32 avail;
     459               0 :       nsresult rv = mUploadStream->Available(&avail);
     460               0 :       if (NS_FAILED(rv))
     461               0 :         return rv;
     462               0 :       mUploadLength = avail;
     463                 :     }
     464                 :   } else {
     465               0 :     mUploadLength = -1;
     466                 :   }
     467               1 :   return NS_OK;
     468                 : }
     469                 : 
     470                 : NS_IMETHODIMP
     471               0 : nsFileChannel::GetUploadStream(nsIInputStream **result)
     472                 : {
     473               0 :     NS_IF_ADDREF(*result = mUploadStream);
     474               0 :     return NS_OK;
     475                 : }

Generated by: LCOV version 1.7