上一次說(shuō)到怎么樣開(kāi)始把任務(wù)發(fā)送出去,也就是調(diào)用函數(shù)
BeginRequestInternal
來(lái)把
URL
請(qǐng)求發(fā)送,它的代碼如下:
#001
?
void ResourceDispatcherHost::BeginRequestInternal(URLRequest* request,
#002
???????????????????????????????????????????????????
bool mixed_content) {
獲取請(qǐng)求信息。
#003
???
ExtraRequestInfo* info = ExtraInfoForRequest(request);
生成全局
ID
,然后保存到正在下載請(qǐng)求隊(duì)列里。
#004
???
GlobalRequestID global_id(info->render_process_host_id, info->request_id);
#005
???
pending_requests_[global_id] = request;
#006
???
if (mixed_content) {
#007
?????
// We don't start the request in that case.
?
The SSLManager will potentially
#008
?????
// change the request (potentially to indicate its content should be
#009
?????
// filtered) and start it itself.
#010
?????
SSLManager::OnMixedContentRequest(this, request, ui_loop_);
#011
?????
return;
#012
???
}
這里開(kāi)始處理請(qǐng)求。
#013
???
request->Start();
#014
?
啟動(dòng)上傳狀態(tài)更新定時(shí)器。
#015
???
// Make sure we have the load state monitor running
#016
???
if (!update_load_states_timer_.IsRunning()) {
#017
?????
update_load_states_timer_.Start(
#018
?????????
TimeDelta::FromMilliseconds(kUpdateLoadStatesIntervalMsec),
#019
?????????
this, &ResourceDispatcherHost::UpdateLoadStates);
#020
???
}
#021
?
}
通過(guò)上面的函數(shù)可以看到主要調(diào)用
URLRequest::Start()
來(lái)處理下載的請(qǐng)求,它的代碼如下:
#001
?
void URLRequest::Start() {
#002
???
DCHECK(!is_pending_);
#003
???
DCHECK(!job_);
#004
?
創(chuàng)建一個(gè)下載的工作任務(wù)。
#005
???
job_ = GetJobManager()->CreateJob(this);
#006
???
job_->SetExtraRequestHeaders(extra_request_headers_);
#007
?
判斷是否有數(shù)據(jù)需要上傳。
#008
???
if (upload_.get())
#009
?????
job_->SetUpload(upload_.get());
#010
?
設(shè)置請(qǐng)下開(kāi)始下載的時(shí)間,以便后面檢查超時(shí)的狀態(tài)。
#011
???
is_pending_ = true;
#012
???
response_info_.request_time = Time::Now();
#013
?
#014
???
// Don't allow errors to be sent from within Start().
#015
???
// TODO(brettw) this may cause NotifyDone to be sent synchronously,
#016
???
// we probably don't want this: they should be sent asynchronously so
#017
???
// the caller does not get reentered.
這里把工作任務(wù)啟動(dòng)運(yùn)行。
#018
???
job_->Start();
#019
?
}
由于這里是對(duì)
URL
的
HTTP
請(qǐng)求下載數(shù)據(jù),所以這里的
job_
是類(lèi)
URLRequestHttpJob
的實(shí)例,也就是調(diào)用函數(shù)
URLRequestHttpJob::Start()
,在函數(shù)
URLRequestHttpJob::Start()
的處理過(guò)程序如下:
1.
??????
URLRequestHttpJob::StartTransaction()
2.
??????
net::HttpCache::Transaction::Start
3.
??????
net::HttpCache::Transaction::BeginNetworkRequest()
4.
??????
net::HttpTransactionWinHttp::Start
5.
??????
net::HttpTransactionWinHttp::DidResolveProxy()
6.
??????
net::HttpTransactionWinHttp::OpenRequest
7.
??????
net::HttpTransactionWinHttp::SendRequest()
8.
??????
net::WinHttpRequestThrottle::SubmitRequest
9.
??????
net::WinHttpRequestThrottle::SendRequest
通過(guò)上面
9
個(gè)函數(shù)的調(diào)用處理,然后就會(huì)通過(guò)
Windows
的
HTTP API
進(jìn)行發(fā)送請(qǐng)求和下載數(shù)據(jù)。我們來(lái)分析一下最后的函數(shù)
WinHttpRequestThrottle::SendRequest
,看看怎么樣調(diào)用
Windows HTTP API
函數(shù)來(lái)獲取數(shù)據(jù)的,它的代碼如下:
#001
?
BOOL WinHttpRequestThrottle::SendRequest(HINTERNET request_handle,
#002
??????????????????????????????????????????
DWORD total_size,
#003
??????????????????????????????????????????
DWORD_PTR context,
#004
??????????????????????????????????????????
bool report_async_error) {
下面就是調(diào)用
Windows
的
API
函數(shù)
WinHttpSendRequest
來(lái)發(fā)送請(qǐng)求,當(dāng)然在調(diào)用這個(gè)函數(shù)之前,需要調(diào)用函數(shù)
WinHttpOpenRequest
先打開(kāi)一個(gè)
TCP
連接。
#005
???
BOOL ok = WinHttpSendRequest(request_handle,
#006
????????????????????????????????
WINHTTP_NO_ADDITIONAL_HEADERS,
#007
????????????????????????????????
0,
#008
????????????????????????????????
WINHTTP_NO_REQUEST_DATA,
#009
????????????????????????????????
0,
#010
????????????????????????????????
total_size,
#011
????????????????????????????????
context);
#012
???
if (!ok && report_async_error) {
#013
?????
WINHTTP_ASYNC_RESULT async_result = { API_SEND_REQUEST, GetLastError() };
出錯(cuò)處理,就調(diào)用外面的回調(diào)函數(shù)。
#014
?????
HttpTransactionWinHttp::StatusCallback(
#015
?????????
request_handle, context,
#016
?????????
WINHTTP_CALLBACK_STATUS_REQUEST_ERROR,
#017
?????????
&async_result, sizeof(async_result));
#018
???
}
#019
???
return ok;
#020
?
}
通過(guò)前面一系列的分析學(xué)會(huì)
chrome
瀏覽器怎么樣輸入
URL
地址,以及怎么樣進(jìn)行
URL
自動(dòng)完成,然后把
URL
發(fā)送到渲染進(jìn)程去處理,最后渲染進(jìn)程又把資源下載請(qǐng)求發(fā)送到資源下載進(jìn)程里處理,最后資源下載進(jìn)程通過(guò)
Windows HTTP API
函數(shù)進(jìn)行
TCP
連接,以及
HTTP
數(shù)據(jù)的上傳和下載。瀏覽器向網(wǎng)站發(fā)送請(qǐng)求的過(guò)程已經(jīng)分析完成了,那么
HTTP API
收到網(wǎng)頁(yè)的數(shù)據(jù)后,又是怎么樣處理的呢?下一次再來(lái)分析這個(gè)問(wèn)題。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
