Advertisement
Guest User

redirectTo API plus unit tests

a guest
Dec 5th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 12.97 KB | None | 0 0
  1. diff -r ee134c770558 netwerk/protocol/http/HttpChannelChild.cpp
  2. --- a/netwerk/protocol/http/HttpChannelChild.cpp    Wed Dec 05 12:32:09 2012 -0800
  3. +++ b/netwerk/protocol/http/HttpChannelChild.cpp    Wed Dec 05 21:51:49 2012 +0000
  4. @@ -1054,15 +1054,16 @@
  5.    URIParams uri;
  6.    SerializeURI(mURI, uri);
  7.  
  8. -  OptionalURIParams originalURI, documentURI, referrer;
  9. +  OptionalURIParams originalURI, documentURI, referrer, redirectURI;
  10.    SerializeURI(mOriginalURI, originalURI);
  11.    SerializeURI(mDocumentURI, documentURI);
  12.    SerializeURI(mReferrer, referrer);
  13. +  SerializeURI(mInternalRedirectURI, redirectURI);
  14.  
  15.    OptionalInputStreamParams uploadStream;
  16.    SerializeInputStream(mUploadStream, uploadStream);
  17.  
  18. -  SendAsyncOpen(uri, originalURI, documentURI, referrer, mLoadFlags,
  19. +  SendAsyncOpen(uri, originalURI, documentURI, referrer, redirectURI, mLoadFlags,
  20.                  mClientSetRequestHeaders, mRequestHead.Method(), uploadStream,
  21.                  mUploadStreamHasHeaders, mPriority, mRedirectionLimit,
  22.                  mAllowPipelining, mForceAllowThirdPartyCookie, mSendResumeAt,
  23. @@ -1105,6 +1106,18 @@
  24.    DROP_DEAD();
  25.  }
  26.  
  27. +NS_IMETHODIMP
  28. +HttpChannelChild::RedirectTo(nsIURI *uri)
  29. +{
  30. +  // We can only redirect unopened channels
  31. +  NS_ENSURE_TRUE(!mIPCOpen, NS_ERROR_ALREADY_OPENED);
  32. +
  33. +  // The redirect is stored internally for use in AsyncOpen
  34. +  mInternalRedirectURI = uri;
  35. +
  36. +  return NS_OK;
  37. +}
  38. +
  39.  // The next four _should_ be implemented, but we need to figure out how
  40.  // to transfer the data from the chrome process first.
  41.  
  42. diff -r ee134c770558 netwerk/protocol/http/HttpChannelChild.h
  43. --- a/netwerk/protocol/http/HttpChannelChild.h  Wed Dec 05 12:32:09 2012 -0800
  44. +++ b/netwerk/protocol/http/HttpChannelChild.h  Wed Dec 05 21:51:49 2012 +0000
  45. @@ -75,6 +75,7 @@
  46.    NS_IMETHOD GetLocalPort(int32_t* port);
  47.    NS_IMETHOD GetRemoteAddress(nsACString& addr);
  48.    NS_IMETHOD GetRemotePort(int32_t* port);
  49. +  NS_IMETHOD RedirectTo(nsIURI *uri);
  50.    // nsISupportsPriority
  51.    NS_IMETHOD SetPriority(int32_t value);
  52.    // nsIResumableChannel
  53. @@ -125,6 +126,7 @@
  54.    RequestHeaderTuples mClientSetRequestHeaders;
  55.    nsCOMPtr<nsIChildChannel> mRedirectChannelChild;
  56.    nsCOMPtr<nsISupports> mSecurityInfo;
  57. +  nsCOMPtr<nsIURI>      mInternalRedirectURI;
  58.  
  59.    bool mIsFromCache;
  60.    bool mCacheEntryAvailable;
  61. diff -r ee134c770558 netwerk/protocol/http/HttpChannelParent.cpp
  62. --- a/netwerk/protocol/http/HttpChannelParent.cpp   Wed Dec 05 12:32:09 2012 -0800
  63. +++ b/netwerk/protocol/http/HttpChannelParent.cpp   Wed Dec 05 21:51:49 2012 +0000
  64. @@ -124,6 +124,7 @@
  65.                                   const OptionalURIParams&   aOriginalURI,
  66.                                   const OptionalURIParams&   aDocURI,
  67.                                   const OptionalURIParams&   aReferrerURI,
  68. +                                 const OptionalURIParams&   aInternalRedirectURI,
  69.                                   const uint32_t&            loadFlags,
  70.                                   const RequestHeaderTuples& requestHeaders,
  71.                                   const nsHttpAtom&          requestMethod,
  72. @@ -144,6 +145,7 @@
  73.    nsCOMPtr<nsIURI> originalUri = DeserializeURI(aOriginalURI);
  74.    nsCOMPtr<nsIURI> docUri = DeserializeURI(aDocURI);
  75.    nsCOMPtr<nsIURI> referrerUri = DeserializeURI(aReferrerURI);
  76. +  nsCOMPtr<nsIURI> internalRedirectUri = DeserializeURI(aInternalRedirectURI);
  77.  
  78.    nsCString uriSpec;
  79.    uri->GetSpec(uriSpec);
  80. @@ -174,6 +176,8 @@
  81.      httpChan->SetDocumentURI(docUri);
  82.    if (referrerUri)
  83.      httpChan->SetReferrerInternal(referrerUri);
  84. +  if (internalRedirectUri)
  85. +    httpChan->RedirectTo(internalRedirectUri);
  86.    if (loadFlags != nsIRequest::LOAD_NORMAL)
  87.      httpChan->SetLoadFlags(loadFlags);
  88.  
  89. diff -r ee134c770558 netwerk/protocol/http/HttpChannelParent.h
  90. --- a/netwerk/protocol/http/HttpChannelParent.h Wed Dec 05 12:32:09 2012 -0800
  91. +++ b/netwerk/protocol/http/HttpChannelParent.h Wed Dec 05 21:51:49 2012 +0000
  92. @@ -52,6 +52,7 @@
  93.                               const OptionalURIParams&   originalUri,
  94.                               const OptionalURIParams&   docUri,
  95.                               const OptionalURIParams&   referrerUri,
  96. +                             const OptionalURIParams&   internalRedirectUri,
  97.                               const uint32_t&            loadFlags,
  98.                               const RequestHeaderTuples& requestHeaders,
  99.                               const nsHttpAtom&          requestMethod,
  100. diff -r ee134c770558 netwerk/protocol/http/PHttpChannel.ipdl
  101. --- a/netwerk/protocol/http/PHttpChannel.ipdl   Wed Dec 05 12:32:09 2012 -0800
  102. +++ b/netwerk/protocol/http/PHttpChannel.ipdl   Wed Dec 05 21:51:49 2012 +0000
  103. @@ -37,6 +37,7 @@
  104.              OptionalURIParams   original,
  105.              OptionalURIParams   doc,
  106.              OptionalURIParams   referrer,
  107. +            OptionalURIParams   internalRedirect,
  108.              uint32_t            loadFlags,
  109.              RequestHeaderTuples requestHeaders,
  110.              nsHttpAtom          requestMethod,
  111. diff -r ee134c770558 netwerk/protocol/http/nsHttpChannel.cpp
  112. --- a/netwerk/protocol/http/nsHttpChannel.cpp   Wed Dec 05 12:32:09 2012 -0800
  113. +++ b/netwerk/protocol/http/nsHttpChannel.cpp   Wed Dec 05 21:51:49 2012 +0000
  114. @@ -35,6 +35,7 @@
  115.  #include "base/compiler_specific.h"
  116.  #include "NullHttpTransaction.h"
  117.  #include "mozilla/Attributes.h"
  118. +#include "mozilla/net/HttpBaseChannel.h"
  119.  
  120.  namespace mozilla { namespace net {
  121.  
  122. @@ -1545,18 +1546,17 @@
  123.          return;
  124.      }
  125.  
  126. -    nsresult rv = AsyncRedirectChannelToHttps();
  127. +    nsresult rv = InternalRedirectChannelToHttps();
  128.      if (NS_FAILED(rv))
  129. -        ContinueAsyncRedirectChannelToHttps(rv);
  130. +        ContinueInternalRedirectChannelToURI(rv);
  131.  }
  132.  
  133.  nsresult
  134. -nsHttpChannel::AsyncRedirectChannelToHttps()
  135. +nsHttpChannel::InternalRedirectChannelToHttps()
  136.  {
  137.      nsresult rv = NS_OK;
  138.      LOG(("nsHttpChannel::HandleAsyncRedirectChannelToHttps() [STS]\n"));
  139.  
  140. -    nsCOMPtr<nsIChannel> newChannel;
  141.      nsCOMPtr<nsIURI> upgradedURI;
  142.  
  143.      rv = mURI->Clone(getter_AddRefs(upgradedURI));
  144. @@ -1578,6 +1578,48 @@
  145.      else
  146.          upgradedURI->SetPort(oldPort);
  147.  
  148. +    return InternalRedirectChannelToURI(upgradedURI);
  149. +}
  150. +
  151. +NS_IMETHODIMP
  152. +nsHttpChannel::RedirectTo(nsIURI *newURI)
  153. +{
  154. +    // We can only redirect unopened channels
  155. +    NS_ENSURE_TRUE(!mRequestObserversCalled, NS_ERROR_ALREADY_OPENED);
  156. +
  157. +    // The redirect is stored internally for use in AsyncOpen
  158. +    mInternalRedirectURI = newURI;
  159. +
  160. +    return NS_OK;
  161. +}
  162. +
  163. +void
  164. +nsHttpChannel::HandleAsyncInternalRedirect()
  165. +{
  166. +    NS_PRECONDITION(!mCallOnResume, "How did that happen?");
  167. +    NS_PRECONDITION(mInternalRedirectURI, "How did that happen?");
  168. +
  169. +    if (mSuspendCount) {
  170. +        LOG(("Waiting until resume to do async API redirect [this=%p]\n", this));
  171. +        mCallOnResume = &nsHttpChannel::HandleAsyncInternalRedirect;
  172. +        return;
  173. +    }
  174. +
  175. +    nsresult rv = InternalRedirectChannelToURI(mInternalRedirectURI);
  176. +    if (NS_FAILED(rv))
  177. +        ContinueInternalRedirectChannelToURI(rv);
  178. +
  179. +    return;
  180. +}
  181. +
  182. +nsresult
  183. +nsHttpChannel::InternalRedirectChannelToURI(nsIURI *upgradedURI)
  184. +{
  185. +    nsresult rv = NS_OK;
  186. +    LOG(("nsHttpChannel::InternalRedirectChannelToURI()\n"));
  187. +
  188. +    nsCOMPtr<nsIChannel> newChannel;
  189. +
  190.      nsCOMPtr<nsIIOService> ioService;
  191.      rv = gHttpHandler->GetIOService(getter_AddRefs(ioService));
  192.      NS_ENSURE_SUCCESS(rv, rv);
  193. @@ -1593,7 +1635,7 @@
  194.      uint32_t flags = nsIChannelEventSink::REDIRECT_PERMANENT;
  195.  
  196.      PushRedirectAsyncFunc(
  197. -        &nsHttpChannel::ContinueAsyncRedirectChannelToHttps);
  198. +        &nsHttpChannel::ContinueInternalRedirectChannelToURI);
  199.      rv = gHttpHandler->AsyncOnChannelRedirect(this, newChannel, flags);
  200.  
  201.      if (NS_SUCCEEDED(rv))
  202. @@ -1601,15 +1643,18 @@
  203.  
  204.      if (NS_FAILED(rv)) {
  205.          AutoRedirectVetoNotifier notifier(this);
  206. +
  207. +        /* Remove the async call to ContinueInternalRedirectChannelToURI().
  208. +         * It is called directly by our callers upon return (to clean up
  209. +         * the failed redirect). */
  210.          PopRedirectAsyncFunc(
  211. -            &nsHttpChannel::ContinueAsyncRedirectChannelToHttps);
  212. +            &nsHttpChannel::ContinueInternalRedirectChannelToURI);
  213.      }
  214.  
  215.      return rv;
  216.  }
  217. -
  218.  nsresult
  219. -nsHttpChannel::ContinueAsyncRedirectChannelToHttps(nsresult rv)
  220. +nsHttpChannel::ContinueInternalRedirectChannelToURI(nsresult rv)
  221.  {
  222.      if (NS_SUCCEEDED(rv))
  223.          rv = OpenRedirectChannel(rv);
  224. @@ -4337,6 +4382,12 @@
  225.      if (mLoadGroup)
  226.          mLoadGroup->AddRequest(this, nullptr);
  227.  
  228. +    // Check to see if we should redirect this channel elsewhere by
  229. +    // nsIHttpChannel.redirectTo API request
  230. +    if (mInternalRedirectURI) {
  231. +        return AsyncCall(&nsHttpChannel::HandleAsyncInternalRedirect);
  232. +    }
  233. +
  234.      // record asyncopen time unconditionally and clear it if we
  235.      // don't want it after OnModifyRequest() weighs in. But waiting for
  236.      // that to complete would mean we don't include proxy resolution in the
  237. diff -r ee134c770558 netwerk/protocol/http/nsHttpChannel.h
  238. --- a/netwerk/protocol/http/nsHttpChannel.h Wed Dec 05 12:32:09 2012 -0800
  239. +++ b/netwerk/protocol/http/nsHttpChannel.h Wed Dec 05 21:51:49 2012 +0000
  240. @@ -108,6 +108,8 @@
  241.      // nsIChannel
  242.      NS_IMETHOD GetSecurityInfo(nsISupports **aSecurityInfo);
  243.      NS_IMETHOD AsyncOpen(nsIStreamListener *listener, nsISupports *aContext);
  244. +    // nsIHttpChannel
  245. +    NS_IMETHOD RedirectTo(nsIURI *newURI);
  246.      // nsIHttpChannelInternal
  247.      NS_IMETHOD SetupFallbackChannel(const char *aFallbackKey);
  248.      // nsISupportsPriority
  249. @@ -182,12 +184,14 @@
  250.  
  251.      // redirection specific methods
  252.      void     HandleAsyncRedirect();
  253. +    void     HandleAsyncInternalRedirect();
  254.      nsresult ContinueHandleAsyncRedirect(nsresult);
  255.      void     HandleAsyncNotModified();
  256.      void     HandleAsyncFallback();
  257.      nsresult ContinueHandleAsyncFallback(nsresult);
  258.      nsresult PromptTempRedirect();
  259. -    virtual nsresult SetupReplacementChannel(nsIURI *, nsIChannel *, bool preserveMethod);
  260. +    nsresult InternalRedirectChannelToURI(nsIURI *);
  261. +    virtual  nsresult SetupReplacementChannel(nsIURI *, nsIChannel *, bool preserveMethod);
  262.  
  263.      // proxy specific methods
  264.      nsresult ProxyFailover();
  265. @@ -242,8 +246,8 @@
  266.      nsresult DoAuthRetry(nsAHttpConnection *);
  267.  
  268.      void     HandleAsyncRedirectChannelToHttps();
  269. -    nsresult AsyncRedirectChannelToHttps();
  270. -    nsresult ContinueAsyncRedirectChannelToHttps(nsresult rv);
  271. +    nsresult InternalRedirectChannelToHttps();
  272. +    nsresult ContinueInternalRedirectChannelToURI(nsresult rv);
  273.      nsresult OpenRedirectChannel(nsresult rv);
  274.  
  275.      /**
  276. @@ -323,6 +327,7 @@
  277.      friend class HttpCacheQuery;
  278.  
  279.      nsCOMPtr<nsIURI>                  mRedirectURI;
  280.      uint32_t                          mRedirectType;
  281.  
  282. diff -r ee134c770558 netwerk/protocol/http/nsIHttpChannel.idl
  283. --- a/netwerk/protocol/http/nsIHttpChannel.idl  Wed Dec 05 12:32:09 2012 -0800
  284. +++ b/netwerk/protocol/http/nsIHttpChannel.idl  Wed Dec 05 21:51:49 2012 +0000
  285. @@ -257,4 +257,16 @@
  286.       *         has been received (before onStartRequest).
  287.       */
  288.      boolean isNoCacheResponse();
  289. +
  290. +    /**
  291. +     * Instructs the channel to immediately redirect to a new destination.
  292. +     * Can only be called on channels not yet opened.
  293. +     *
  294. +     * This method provides no explicit conflict resolution. The last
  295. +     * caller to call it wins.
  296. +     *
  297. +     * @throws NS_ERROR_ALREADY_OPENED if called after the channel
  298. +     *         has been opened.
  299. +     */
  300. +    void redirectTo(in nsIURI aNewURI);
  301.  };
  302. diff -r ee134c770558 netwerk/protocol/viewsource/nsViewSourceChannel.cpp
  303. --- a/netwerk/protocol/viewsource/nsViewSourceChannel.cpp   Wed Dec 05 12:32:09 2012 -0800
  304. +++ b/netwerk/protocol/viewsource/nsViewSourceChannel.cpp   Wed Dec 05 21:51:49 2012 +0000
  305. @@ -680,4 +680,12 @@
  306.  {
  307.      return !mHttpChannel ? NS_ERROR_NULL_POINTER :
  308.          mHttpChannel->IsNoCacheResponse(_retval);
  309. -}
  310. +}
  311. +
  312. +NS_IMETHODIMP
  313. +nsViewSourceChannel::RedirectTo(nsIURI *uri)
  314. +{
  315. +    return !mHttpChannel ? NS_ERROR_NULL_POINTER :
  316. +        mHttpChannel->RedirectTo(uri);
  317. +}
  318. +
  319. diff -r ee134c770558 netwerk/test/unit/xpcshell.ini
  320. --- a/netwerk/test/unit/xpcshell.ini    Wed Dec 05 12:32:09 2012 -0800
  321. +++ b/netwerk/test/unit/xpcshell.ini    Wed Dec 05 21:51:49 2012 +0000
  322. @@ -174,6 +174,7 @@
  323.  [test_redirect-caching_passing.js]
  324.  [test_redirect_canceled.js]
  325.  [test_redirect_failure.js]
  326. +[test_redirect_from_script.js]
  327.  [test_redirect_passing.js]
  328.  [test_redirect_loop.js]
  329.  [test_redirect_baduri.js]
  330. diff -r ee134c770558 netwerk/test/unit_ipc/xpcshell.ini
  331. --- a/netwerk/test/unit_ipc/xpcshell.ini    Wed Dec 05 12:32:09 2012 -0800
  332. +++ b/netwerk/test/unit_ipc/xpcshell.ini    Wed Dec 05 21:51:49 2012 +0000
  333. @@ -19,6 +19,7 @@
  334.  [test_redirect-caching_passing_wrap.js]
  335.  [test_redirect_canceled_wrap.js]
  336.  [test_redirect_failure_wrap.js]
  337. +[test_redirect_from_script_wrap.js]
  338.  [test_redirect_passing_wrap.js]
  339.  [test_reentrancy_wrap.js]
  340.  [test_resumable_channel_wrap.js]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement