Skip to content
Permalink
Browse files

Fixed Few Network Process elements and State changers

1) Loading, changing title and adding redirected url are added to minibrowser
2) Get data and run on main thread - fixes network process run on main thread problem
  • Loading branch information...
RAJAGOPALAN-GANGADHARAN authored and pulkomandy committed Jun 30, 2019
1 parent e1f2be2 commit ff1ed047c83c89c6a6d56a8771edda39d1eeddb5
@@ -30,21 +30,35 @@
#include <WebCore/CookieJar.h>
#include <WebCore/NetworkStorageSession.h>
#include <WebCore/ResourceError.h>
#include <WebCore/ResourceResponse.h>
#include <WebCore/SameSiteInfo.h>
#include <WebCore/SharedBuffer.h>
#include <WebCore/HTTPParsers.h>
#include <wtf/MainThread.h>
#include <wtf/text/CString.h>

#include <Url.h>
#include <UrlRequest.h>
#include <HttpRequest.h>
#include <assert.h>
#include "BeDC.h"

static const int gMaxRecursionLimit = 10;

namespace WebKit {

using namespace WebCore;

NetworkDataTaskHaiku::NetworkDataTaskHaiku(NetworkSession& session, NetworkDataTaskClient& client, const ResourceRequest& requestWithCredentials, StoredCredentialsPolicy storedCredentialsPolicy, ContentSniffingPolicy shouldContentSniff, ContentEncodingSniffingPolicy, bool shouldClearReferrerOnHTTPSToHTTPRedirect, bool dataTaskIsForMainFrameNavigation)

NetworkDataTaskHaiku::NetworkDataTaskHaiku(NetworkSession& session, NetworkDataTaskClient& client, const ResourceRequest& requestWithCredentials,
StoredCredentialsPolicy storedCredentialsPolicy, ContentSniffingPolicy shouldContentSniff, ContentEncodingSniffingPolicy,
bool shouldClearReferrerOnHTTPSToHTTPRedirect, bool dataTaskIsForMainFrameNavigation)
: NetworkDataTask(session, client, requestWithCredentials, storedCredentialsPolicy, shouldClearReferrerOnHTTPSToHTTPRedirect, dataTaskIsForMainFrameNavigation)
, m_postData(NULL)
, m_responseDataSent(false)
, m_redirected(false)
, m_redirectionTries(gMaxRecursionLimit)
, m_position(0)

{
if (m_scheduledFailureType != NoFailure)
return;
@@ -64,6 +78,10 @@ NetworkDataTaskHaiku::NetworkDataTaskHaiku(NetworkSession& session, NetworkDataT

NetworkDataTaskHaiku::~NetworkDataTaskHaiku()
{
cancel();
if (m_request)
m_request->SetListener(NULL);
delete m_request;
}

void NetworkDataTaskHaiku::createRequest(ResourceRequest&& request)
@@ -99,11 +117,22 @@ void NetworkDataTaskHaiku::createRequest(ResourceRequest&& request)
} else {
m_request->SetListener(this);
}

if (m_request->Run() < B_OK)
if (m_request->Run() < B_OK)
{
if (!m_client)
return;

ResourceError error("BUrlProtocol", 42,
m_baseUrl,
"The service kit failed to start the request.");

m_networkLoadMetrics.responseEnd = MonotonicTime::now() - m_startTime;
m_networkLoadMetrics.markComplete();
m_client->didCompleteWithError(error,m_networkLoadMetrics);
}
}

void NetworkDataTaskHaiku::cancel()
{
if(m_state == State::Canceling || m_state == State::Completed)
@@ -123,6 +152,8 @@ void NetworkDataTaskHaiku::resume()

m_state = State::Running;

if(m_request)
m_request->Resume();
}

void NetworkDataTaskHaiku::invalidateAndCancel()
@@ -135,33 +166,168 @@ NetworkDataTask::State NetworkDataTaskHaiku::state() const
return m_state;
}

void NetworkDataTaskHaiku::ConnectionOpened(BUrlRequest* caller)
void NetworkDataTaskHaiku::runOnMainThread(Function<void()>&& task)
{
if(isMainThread())
task();
else
callOnMainThreadAndWait(WTFMove(task));
}


void NetworkDataTaskHaiku::ConnectionOpened(BUrlRequest* caller)
{
m_responseDataSent = false;
}
void NetworkDataTaskHaiku::HeadersReceived(BUrlRequest* caller, const BUrlResult& result)
{
if (m_currentRequest.isNull())
return;

const BHttpResult* httpResult = dynamic_cast<const BHttpResult*>(&result);

WTF::String contentType = result.ContentType();
int contentLength = result.Length();
URL url;

WTF::String encoding = extractCharsetFromMediaType(contentType);
WTF::String mimeType = extractMIMETypeFromMediaType(contentType);

if (httpResult) {
url = URL(httpResult->Url());

BString location = httpResult->Headers()["Location"];
if (location.Length() > 0) {
m_redirected = true;
url = URL(url, location);
} else {
m_redirected = false;
}
} else {
url = m_baseUrl;
}

ResourceResponse response(url, mimeType, contentLength, encoding);

if (httpResult) {
int statusCode = httpResult->StatusCode();

String suggestedFilename = filenameFromHTTPContentDisposition(
httpResult->Headers()["Content-Disposition"]);

if (!suggestedFilename.isEmpty())
response.setSuggestedFilename(suggestedFilename);

response.setHTTPStatusCode(statusCode);
response.setHTTPStatusText(httpResult->StatusText());

// Add remaining headers.
const BHttpHeaders& resultHeaders = httpResult->Headers();
for (int i = 0; i < resultHeaders.CountHeaders(); i++) {
BHttpHeader& headerPair = resultHeaders.HeaderAt(i);
response.setHTTPHeaderField(headerPair.Name(), headerPair.Value());
}

if (statusCode == 401) {

//TODO

//AuthenticationNeeded((BHttpRequest*)m_request, response);
// AuthenticationNeeded may have aborted the request
// so we need to make sure we can continue.

if (m_currentRequest.isNull())
return;
}
}

if (!m_client)
return;

if (m_redirected) {
m_redirectionTries--;

if (m_redirectionTries == 0) {
ResourceError error(url.host().utf8().data(), 400, url,
"Redirection limit reached");

m_networkLoadMetrics.responseEnd = MonotonicTime::now() - m_startTime;
m_networkLoadMetrics.markComplete();
m_client->didCompleteWithError(error,m_networkLoadMetrics);
return;
}

// Notify the client that we are redirecting.
ResourceRequest request = m_currentRequest;
ResourceResponse responseCopy = response;
request.setURL(url);
fprintf(stderr,"((((((( redirecting to %s)))))))\n",request.url().string().utf8().data());
m_client->willPerformHTTPRedirection(WTFMove(responseCopy),WTFMove(request), [this](const ResourceRequest& newRequest){

if(newRequest.isNull() || m_state == State::Canceling)
return;

m_startTime = MonotonicTime::now();//network metrics

if( m_state != State::Suspended ){
m_state = State::Suspended;
resume();
}
});

} else {
ResourceResponse responseCopy = response;
m_client->didReceiveResponse(WTFMove(responseCopy),[this](WebCore::PolicyAction policyAction){
if(m_state == State::Canceling || m_state == State::Completed){
return;
}
});
}
}
void NetworkDataTaskHaiku::DataReceived(BUrlRequest* caller, const char* data, off_t position,
ssize_t size)
{
if(size < 0 )
{
return;
}
m_client->didReceiveData(SharedBuffer::create(data,size));
if (m_currentRequest.isNull())
return;

if (!m_client)
return;

// don't emit the "Document has moved here" type of HTML
if (m_redirected)
return;

if (position != m_position)
{
debugger("bad redirect");
return;
}

if (size > 0) {
m_responseDataSent = true;


runOnMainThread([this,data = data, size = size]{
m_client->didReceiveData(SharedBuffer::create(data,size));
});
}

m_position += size;
}
void NetworkDataTaskHaiku::UploadProgress(BUrlRequest* caller, ssize_t bytesSent, ssize_t bytesTotal)
{

}
void NetworkDataTaskHaiku::RequestCompleted(BUrlRequest* caller, bool success)
{

}
bool NetworkDataTaskHaiku::CertificateVerificationFailed(BUrlRequest* caller, BCertificate& certificate, const char* message)
{


}
void NetworkDataTaskHaiku::DebugMessage(BUrlRequest* caller,BUrlProtocolDebugMessage type,const char* text)
{
}

}
@@ -27,21 +27,24 @@

#include "config.h"
#include "NetworkDataTask.h"
#include <WebCore/HaikuFormDataStream.h>

#include <WebCore/ResourceResponse.h>
#include <WebCore/ResourceRequest.h>
#include <WebCore/NetworkLoadMetrics.h>

#include <UrlProtocolAsynchronousListener.h>
#include <Referenceable.h>
#include <Path.h>

namespace WebKit {
using namespace WebCore;

class NetworkDataTaskHaiku final : public NetworkDataTask,
public BUrlProtocolAsynchronousListener
class NetworkDataTaskHaiku final : public NetworkDataTask
,public BUrlProtocolAsynchronousListener
{
public:
static Ref<NetworkDataTask> create(NetworkSession& session, NetworkDataTaskClient& client, const WebCore::ResourceRequest& request, WebCore::StoredCredentialsPolicy storedCredentialsPolicy, WebCore::ContentSniffingPolicy shouldContentSniff, WebCore::ContentEncodingSniffingPolicy shouldContentEncodingSniff, bool shouldClearReferrerOnHTTPSToHTTPRedirect, bool dataTaskIsForMainFrameNavigation)
static Ref<NetworkDataTask> create(NetworkSession& session, NetworkDataTaskClient& client, const WebCore::ResourceRequest& request, WebCore::StoredCredentialsPolicy storedCredentialsPolicy, WebCore::ContentSniffingPolicy shouldContentSniff, WebCore::ContentEncodingSniffingPolicy shouldContentEncodingSniff,
bool shouldClearReferrerOnHTTPSToHTTPRedirect, bool dataTaskIsForMainFrameNavigation)
{
return adoptRef(*new NetworkDataTaskHaiku(session, client, request, storedCredentialsPolicy, shouldContentSniff, shouldContentEncodingSniff, shouldClearReferrerOnHTTPSToHTTPRedirect, dataTaskIsForMainFrameNavigation));
}
@@ -56,21 +59,30 @@ class NetworkDataTaskHaiku final : public NetworkDataTask,
void invalidateAndCancel() override;
NetworkDataTask::State state() const override;

void runOnMainThread(Function<void()>&&);

void ConnectionOpened(BUrlRequest* caller) override;
void HeadersReceived(BUrlRequest* caller, const BUrlResult& result) override;
void DataReceived(BUrlRequest* caller, const char* data, off_t position,
ssize_t size) override;
void UploadProgress(BUrlRequest* caller, ssize_t bytesSent, ssize_t bytesTotal) override;
void RequestCompleted(BUrlRequest* caller, bool success) override;
bool CertificateVerificationFailed(BUrlRequest* caller, BCertificate& certificate, const char* message) override;
void DebugMessage(BUrlRequest* caller,BUrlProtocolDebugMessage type,const char* text) override;

WebCore::ResourceResponse m_response;
WebCore::ResourceRequest m_currentRequest;
WebCore::NetworkLoadMetrics m_networkLoadMetrics;
unsigned m_redirectCount { 0 };
unsigned m_authFailureCount { 0 };
MonotonicTime m_startTime;
BUrlRequest* m_request;
BFormDataIO* m_postData;
URL m_baseUrl;
bool m_responseDataSent;
bool m_redirected;
off_t m_position;

int m_redirectionTries;
};
}
@@ -313,7 +313,7 @@ namespace WebKit
notImplemented();
}

BView* PageClientImpl::viewWidget()
WebViewBase* PageClientImpl::viewWidget()
{
return &fWebView;
}
@@ -38,7 +38,7 @@ namespace WebKit
{
public:
PageClientImpl(WebViewBase&);
BView* viewWidget();
WebViewBase* viewWidget();
private:
//page client def's
std::unique_ptr<DrawingAreaProxy> createDrawingAreaProxy(WebProcessProxy&) override;

0 comments on commit ff1ed04

Please sign in to comment.
You can’t perform that action at this time.