面試官再問你Http請求過程,懟回去!

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c8/c89060bfd022839b99dbd1fe65d2da02.jpeg","alt":"image","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"link","attrs":{"href":"#http介紹","title":null}},{"type":"text","text":"Http介紹"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"超文本傳輸協議(HTTP,HyperText Transfer Protocol)是互聯網上應用最爲廣泛的一種網絡協議。所有的WWW文件都必須遵守這個標準。設計HTTP最初的目的是爲了提供一種發佈和接收HTML頁面的方法。1960年美國人Ted Nelson構思了一種通過計算機處理文本信息的方法,並稱之爲超文本(hypertext),這成爲了HTTP超文本傳輸協議標準架構的發展根基。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上並非此次文章重點,更詳細的http介紹請移步 "},{"type":"link","attrs":{"href":"http://www.baidu.com","title":null},"content":[{"type":"text","text":"www.baidu.com"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Http是一種網絡協議,而且是無狀態的超文本協議,基於Tcp/Ip協議的應用層協議。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"link","attrs":{"href":"#我要ip","title":null}},{"type":"text","text":"我要IP"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當用戶請求某個域名的資源,比如在瀏覽器敲入http://www.qq.com的時候,瀏覽器首先會根據輸入的域名去查詢IP地址。去哪查呢?這裏就需要引入DNS的概念,可以把DNS看做是域名映射IP的賬簿。 當客戶端發送一個DNS請求的時候,首先本地的DNS服務器會接收到請求,會在本地先查詢緩存中有沒有當前域名和IP的映射關係,如果有則直接返回IP信息,如果沒有,則會詢問其他DNS服務器,這裏簡單說一下網絡上DNS服務器的結構,DNS服務器在網絡上是樹狀結構的,存在一個根服務器,根服務器的子節點是一級域名服務器(比如 .com, .cn),一級域名服務器的子節點又稱爲權威(權限)DNS服務器"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/13/1339e30ac926b87149d29480173e0145.jpeg","alt":"image","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當本地DNS服務器沒有相關查詢信息的時候會依照以上樹的順序查詢域名和IP的對應關係,查到之後會緩存到本地DNS,這個過程最終的結果就是獲取到相關域名對應的IP地址,如果客戶端輸入的是IP地址信息,則省略了以上查詢IP的過程了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"訪問互聯網的任何網站本質上都是依據IP來尋址的。"}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"link","attrs":{"href":"#建立tcp連接","title":null}},{"type":"text","text":"建立Tcp連接"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當一個http請求發出之後,並且獲取到了正確的服務器IP地址,這個時候就可以建立連接了。有一點需要明確:http協議是基於Tcp協議的。所以第一步就需要建立Tcp連接,這個過程就是很多網絡文章所說的三次握手: Client:Hi,我是 Client。 Server:您好 Client,我是 Server。 Client:您好 Server..."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏是三次握手可以以這樣的順序來表示:client的問->server的答->client的答"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有的面試官無聊會問爲什麼是三次握手而不是兩次或者四次五次呢?你可以理解,當兩個人A和B要想互相聯繫的時候,最簡單的方式就是A提問然後能收到B的回答,B提問能收到A的回答。這也是三次握手的核心。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"平時所說的Tcp是面向連接的,這裏的連接其實是雙方約定一定格式來進行通信的過程(包括髮包的順序,buffer的大小等約定),在邏輯上好像是維持了一條連接而已。"}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"link","attrs":{"href":"#我要出網關","title":null}},{"type":"text","text":"我要出網關"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一旦Tcp連接建立起來,http請求就可以組織數據發送報文了。目前http協議的版本大部分是1.1,在這個版本中有一個屬性 Keep-Alive,這個屬性標示要保持此http連接建立的TCP連接,默認是開啓的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網絡上有文章大篇幅描述http的長連接信息,其實是錯誤的說法,長連接是針對tcp連接,http連接打開keepalive選項只不過保持了tcp連接不斷開而已。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"HTTP 的報文大概分爲三大部分。第一部分是請求行,第二部分是請求頭(header),第三部分是請求體(body)。這裏具體的http協議其他概念不再展開討論,因爲內容有點多。http協議位於應用層,所以要發送的報文首先會把http協議相關的內容包含在包中,然後傳給下一層。 "}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8c/8cdb69fc66f7121c7b1036891e6b1c11.jpeg","alt":"image","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下一層是傳輸層,這一層主要有兩個協議:Tcp和Udp,http協議選擇的是tcp協議,tcp會有兩個端口信息,一個是源端口,一個是目標端口,比如http請求一般目標端口是80。傳輸層把端口信息封裝完畢,接着把請求包傳給網絡層。 "}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/90/902eb55071bebd4ef2323fc01b7fe4c7.jpeg","alt":"image","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網絡層的協議是IP協議,在這一層會把源Ip地址和目標IP地址封裝進去(目標IP就是請求的網站ip,查詢dns獲得)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/42/42698c9554c16224ae04e29d4afffb31.jpeg","alt":"image","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"操作系統知道了要發往的IP地址,會判斷這個ip是否在本地局域網內(根據子網掩碼來判斷),如果不在的話,則需要網關把這個請求發送出去(網關的ip一般是DHCP協議配置的)。操作系統怎麼獲取網關在哪呢?這個過程基本上靠的是廣播,應用的協議是ARP協議,當局域網內的所有設備接收到ARP協議的內容之後會判斷ip是否和網關ip相同,如果相同就會回覆,經過這個過程,系統找到了網關,獲取到了網關的MAC地址,並把網關的MAc地址和本機的MAc地址封裝進請求包,發給下一層MAC層,最後網卡把消息發往網關。 "}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/7c/7ccbfbe03f6a61a38e6b765efaa2bfb2.jpeg","alt":"image","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MAC地址主要用在同一個局域網內定位某個計算機,是在局域網內纔有效的地址"}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"link","attrs":{"href":"#到達目標服務器","title":null}},{"type":"text","text":"到達目標服務器"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"請求包到達網關,網關會根據消息的MAC地址來判斷是否和自己的mac相同,如果相同則把消息接收下來。接着會判斷消息中目標IP,如果目標IP未在自己的局域網中,則需要根據自己的路由規則把消息發送給下一個相連的網關。網關和網關之間是通信的,至於網關怎麼計算出最優路徑這裏不再展開。我們以常見的家庭路由器爲例,每個路由器的網關IP其實是運營商給分配的,並且網絡包發送出去一般都是採用修改IP的方式(NAT)。具體步驟:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"網關檢查目標IP是否在自己的局域網內,如果不在,則獲取要傳送的下一個網關mac和IP,把目標IP和mac修改爲下一個網關的IP和mac,並把來源IP和mac修改爲當前網關的IP(外網IP)和mac。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"下一個網關收到消息,首先檢查mac是否和自己匹配,如果匹配接會檢查目標IP是否在自己的局域網內,如果不在則重複以上步驟"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"重複以上步驟直到目標服務器所在的網關。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"目標服務器所在的網關收到消息,會判斷出來當前的目標IP在我的局域網範圍,就不會在跳躍下一個網關,而是向局域網內發ARP請求尋找目標服務器,目標服務器收到請求會響應,網關會把具體的請求發送給目標服務器。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"目標服務器收到消息會解析請求的消息,在對比完mac和IP信息之後,會得到端口的信息,目標服務器則在本機尋找監聽這個端口的程序,http服務器很有可能是nginx或者其他web服務器。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"請求通過端口傳送給具體的處理的程序,程序會解析http請求的內容,根據內容作出相應的回覆。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":7,"align":null,"origin":null},"content":[{"type":"text","text":"請求按照以上所有步驟把響應返回給請求方(網關路由器會記住來源路徑),至此一個http請求結束。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網關(路由器)之間通過路由表來決定下一個跳躍的網關地址"}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"link","attrs":{"href":"#寫在最後","title":null}},{"type":"text","text":"寫在最後"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上只是http請求的一個大概過程,其實每一步都非常複雜,沒有詳細展開。比如:路由協議、ip的分配 等等。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"link","attrs":{"href":"#領取架構師進階資料大禮包","title":null}},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s/gQKStRKyHB_KOuDjyyodUw","title":null},"content":[{"type":"text","text":"領取架構師進階資料大禮包"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章