Bug 1638513 - P2 let nsIHttpChannel implement shouldStripRequestBodyHeader, r=valentin,necko-reviewers

We need to extract the common method into the idl since the connection between necko
and XHR is idl. Need to import the whole necko if we do something like
`#include "HttpBaseChannel.h"

Differential Revision: https://phabricator.services.mozilla.com/D78830
This commit is contained in:
Junior Hsu 2020-06-09 18:20:15 +00:00
Родитель b7d7ea4350
Коммит c967db3c67
7 изменённых файлов: 52 добавлений и 23 удалений

Просмотреть файл

@ -1432,20 +1432,13 @@ FetchDriver::AsyncOnChannelRedirect(nsIChannel* aOldChannel,
nsCOMPtr<nsIHttpChannel> oldHttpChannel = do_QueryInterface(aOldChannel); nsCOMPtr<nsIHttpChannel> oldHttpChannel = do_QueryInterface(aOldChannel);
nsCOMPtr<nsIHttpChannel> newHttpChannel = do_QueryInterface(aNewChannel); nsCOMPtr<nsIHttpChannel> newHttpChannel = do_QueryInterface(aNewChannel);
if (newHttpChannel) { if (newHttpChannel) {
uint32_t responseCode = 0; nsAutoCString method;
bool rewriteToGET = false; mRequest->GetMethod(method);
if (oldHttpChannel &&
NS_SUCCEEDED(oldHttpChannel->GetResponseStatus(&responseCode))) {
nsAutoCString method;
mRequest->GetMethod(method);
// Fetch 4.4.11 // Fetch 4.4.11
rewriteToGET = bool rewriteToGET = false;
((responseCode == 301 || responseCode == 302) && Unused << oldHttpChannel->ShouldStripRequestBodyHeader(method,
method.LowerCaseEqualsASCII("post")) || &rewriteToGET);
(responseCode == 303 && !method.LowerCaseEqualsASCII("get") &&
!method.LowerCaseEqualsASCII("head"));
}
SetRequestHeaders(newHttpChannel, rewriteToGET); SetRequestHeaders(newHttpChannel, rewriteToGET);
} }

Просмотреть файл

@ -3324,16 +3324,9 @@ nsresult XMLHttpRequestMainThread::OnRedirectVerifyCallback(nsresult result) {
if (NS_SUCCEEDED(result)) { if (NS_SUCCEEDED(result)) {
bool rewriteToGET = false; bool rewriteToGET = false;
nsCOMPtr<nsIHttpChannel> oldHttpChannel = GetCurrentHttpChannel(); nsCOMPtr<nsIHttpChannel> oldHttpChannel = GetCurrentHttpChannel();
if (uint32_t responseCode = 0; // Fetch 4.4.11
oldHttpChannel && Unused << oldHttpChannel->ShouldStripRequestBodyHeader(mRequestMethod,
NS_SUCCEEDED(oldHttpChannel->GetResponseStatus(&responseCode))) { &rewriteToGET);
// Fetch 4.4.11
rewriteToGET =
((responseCode == 301 || responseCode == 302) &&
mRequestMethod.LowerCaseEqualsASCII("post")) ||
(responseCode == 303 && !mRequestMethod.LowerCaseEqualsASCII("get") &&
!mRequestMethod.LowerCaseEqualsASCII("head"));
}
mChannel = mNewRedirectChannel; mChannel = mNewRedirectChannel;

Просмотреть файл

@ -3352,6 +3352,27 @@ bool HttpBaseChannel::ShouldRewriteRedirectToGET(
return false; return false;
} }
NS_IMETHODIMP
HttpBaseChannel::ShouldStripRequestBodyHeader(const nsACString& aMethod,
bool* aResult) {
*aResult = false;
uint32_t httpStatus = 0;
if (NS_FAILED(GetResponseStatus(&httpStatus))) {
return NS_OK;
}
nsAutoCString method(aMethod);
nsHttpRequestHead::ParsedMethodType parsedMethod;
nsHttpRequestHead::ParseMethod(method, parsedMethod);
// Fetch 4.4.11, which is slightly different than the perserved method
// algrorithm: strip request-body-header for GET->GET redirection for 303.
*aResult =
ShouldRewriteRedirectToGET(httpStatus, parsedMethod) &&
!(httpStatus == 303 && parsedMethod == nsHttpRequestHead::kMethod_Get);
return NS_OK;
}
HttpBaseChannel::ReplacementChannelConfig HttpBaseChannel::ReplacementChannelConfig
HttpBaseChannel::CloneReplacementChannelConfig(bool aPreserveMethod, HttpBaseChannel::CloneReplacementChannelConfig(bool aPreserveMethod,
uint32_t aRedirectFlags, uint32_t aRedirectFlags,

Просмотреть файл

@ -198,6 +198,8 @@ class HttpBaseChannel : public nsHashPropertyBag,
NS_IMETHOD VisitRequestHeaders(nsIHttpHeaderVisitor* visitor) override; NS_IMETHOD VisitRequestHeaders(nsIHttpHeaderVisitor* visitor) override;
NS_IMETHOD VisitNonDefaultRequestHeaders( NS_IMETHOD VisitNonDefaultRequestHeaders(
nsIHttpHeaderVisitor* visitor) override; nsIHttpHeaderVisitor* visitor) override;
NS_IMETHOD ShouldStripRequestBodyHeader(const nsACString& aMethod,
bool* aResult) override;
NS_IMETHOD GetResponseHeader(const nsACString& header, NS_IMETHOD GetResponseHeader(const nsACString& header,
nsACString& value) override; nsACString& value) override;
NS_IMETHOD SetResponseHeader(const nsACString& header, NS_IMETHOD SetResponseHeader(const nsACString& header,

Просмотреть файл

@ -231,6 +231,12 @@ NullHttpChannel::VisitOriginalResponseHeaders(nsIHttpHeaderVisitor* aVisitor) {
return NS_ERROR_NOT_IMPLEMENTED; return NS_ERROR_NOT_IMPLEMENTED;
} }
NS_IMETHODIMP
NullHttpChannel::ShouldStripRequestBodyHeader(const nsACString& aMethod,
bool* aResult) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP NS_IMETHODIMP
NullHttpChannel::IsNoStoreResponse(bool* _retval) { NullHttpChannel::IsNoStoreResponse(bool* _retval) {
return NS_ERROR_NOT_IMPLEMENTED; return NS_ERROR_NOT_IMPLEMENTED;

Просмотреть файл

@ -183,6 +183,12 @@ interface nsIHttpChannel : nsIIdentChannel
[must_use] [must_use]
void visitNonDefaultRequestHeaders(in nsIHttpHeaderVisitor aVisitor); void visitNonDefaultRequestHeaders(in nsIHttpHeaderVisitor aVisitor);
/**
* Call this method to see if we need to strip the request body headers
* for the new http channel. This should be called during redirection.
*/
[must_use] bool ShouldStripRequestBodyHeader(in ACString aMethod);
/** /**
* This attribute no longer has any effect, it remains for backwards compat * This attribute no longer has any effect, it remains for backwards compat
* *

Просмотреть файл

@ -827,6 +827,14 @@ nsViewSourceChannel::VisitNonDefaultRequestHeaders(
: mHttpChannel->VisitNonDefaultRequestHeaders(aVisitor); : mHttpChannel->VisitNonDefaultRequestHeaders(aVisitor);
} }
NS_IMETHODIMP
nsViewSourceChannel::ShouldStripRequestBodyHeader(const nsACString& aMethod,
bool* aResult) {
return !mHttpChannel
? NS_ERROR_NULL_POINTER
: mHttpChannel->ShouldStripRequestBodyHeader(aMethod, aResult);
}
NS_IMETHODIMP NS_IMETHODIMP
nsViewSourceChannel::GetAllowPipelining(bool* aAllowPipelining) { nsViewSourceChannel::GetAllowPipelining(bool* aAllowPipelining) {
return !mHttpChannel ? NS_ERROR_NULL_POINTER return !mHttpChannel ? NS_ERROR_NULL_POINTER