Vous êtes sur la page 1sur 5

10/22/2016

What happens when you type a URL in browser | EduSagar

WhathappenswhenyoutypeaURLinbrowser
30May,2014

PankajPal

10Comments

51.02K

Apartfrombeingaverycommoninterviewquestion,thisisoneoftheveryfirstquerywhichlingers
aroundinourmindeverytimewetypeaURLinabrowser.Hereisanattempttosatiatethisquest
whilewedelveintothedetailsofwhathappensinthebackgroundwhenwetypeaURLinour
browsers.

Step1.URListypedinthebrowser.
Step2.Ifrequestedobjectisinbrowsercacheandisfresh,move
ontoStep8.
Step3.DNSlookuptofindtheipaddressoftheserver
whenwewanttoconnectto google.com ,weactuallywanttoreachouttoaserver
wheregooglewebservicesarehosted.Onesuchserverishavinganipaddress
of74.125.236.65 .Now,ifyoutype" http://74.125.236.65 "inyourbrowser ,thiswill
takeyoutogooglehomepageitself.Whichmeans," http://google.com "and
"http://74.125.236.65 "arenothingbutsamestuf f.But,itisnotso.Googlehas
multipleserversinmultiplelocationstocatertothehugevolumeofrequeststhey
receivepersecond. ThusweshouldletGoogledecidewhichserverisbestsuitedto
ourneeds.Using"google.com"doesthejobforus.Whenwetype
"google.com", DNS(DomainNameSystem) servicescomesintoplayandresolves
theURLtoaproperipaddress.
FollowingisasummaryofstepshappeningwhileDNSserviceisatwork:
Checkbrowsercache :browsersmaintaincacheofDNSrecordsforsome
fixedduration.So,thisisthefirstplacetoresolveDNSqueries.
CheckOScache :ifbrowserdoesn'tcontaintherecordinitscache,itmakesa
systemcalltounderlyingOperatingSystemtofetchtherecordasOSalso
http://edusagar.com/articles/view/70/What-happens-when-you-type-a-URL-in-browser

1/5

10/22/2016

What happens when you type a URL in browser | EduSagar

maintainsacacheofrecentDNSqueries.
RouterCache :ifabovestepsfailtogetaDNSrecord,thesearchcontinuesto
yourrouterwhichhasitsowncache.
ISPcache:ifeverythingfails,thesearchmovesontoyourISP .First,ittriesin
itscache,ifnotfoundISP'sDNS recursivesearch comesintopicture.DNS
lookupisagainacomplexprocesswhichfindstheappropriateipaddressfrom
alistofmanyoptionsavailableforwebsiteslikeGoogle. Youcanreadmore
aboutthis here.
FortheDNSenthusiasts hereisagreatguideworthreading.

Step4.BrowserinitiatesaTCPconnectionwiththeserver.
Step5.BrowsersendsaHTTPrequesttotheserver.
Browsersendsa GETrequesttotheserveraccordingtothespecification
ofHTTP(Hyper TextTransferProtocol) protocol.

GEThttp://google.com/HTTP/1.1
Accept:text/html,application/xhtml+xml,application/xmlq=0.9,*/*q=0.8
UserAgent:Mozilla/5.0(X11UbuntuLinuxx86_64rv:29.0)Gecko/20100101
Firefox/29.0
AcceptEncoding:gzip,deflate
Connection:KeepAlive
Host:google.com
Cookie:datr=1265876274[...]locale=en_USlsd=WW[...]c_user=2101[...]

Here,browserpassessomemetainformationintheformofheaderstotheserver
alongwiththeURL "http://google.com". UserAgent headerspecifiesthebrowser
properties, AcceptEncoding headersspecifythetypeofresponsesitwill
accept.Connection headertellstheservertokeepopenthe TCPconnection
establishedhere. Therequestalsocontains Cookies ,whicharemetainformation
storedattheclientendandcontainpreviousbrowsingsessioninformationforthe
samewebsiteintheformofkeyvaluepairse.g.theloginnameoftheuserfor
http://edusagar.com/articles/view/70/What-happens-when-you-type-a-URL-in-browser

2/5

10/22/2016

What happens when you type a URL in browser | EduSagar

Google.
AquickguidetoHTTP specificationcanbefound here.

Step6.Serverhandlestheincomingrequest
HTTPrequestmadefrombrowsersarehandledbyaspecialsoftwarerunningon
servercommonlyknownas webservers e.g.Apache ,IISetc.Webserverpasses
ontherequesttotheproperrequesthandleraprogramwrittentohandleweb
servicese.g. PHP,ASP.NET,Ruby,Servlets etc.

ForexampleURL http://edusagar .com/index.php ishandledbyaprogramwritten


inPHPfileindex.php .AssoonasGET requestforindex.phpisreceived,
Apache(ourwebserveratedusagar .com)preparestheenvironmenttoexecute
index.phpfile.Now ,thisphpprogramwillgeneratearesponseinourcaseaHTML
response. ThisresponseisthensentbacktothebrowseraccordingtoHTTP
guidelines.

Step7.BrowserreceivestheHTTPresponse
HTTP/1.1200OK
CacheControl:private,nostore,nocache,mustrevalidate,postcheck=0,pre
check=0
Expires:Thu,19Nov198108:52:00GMT
Pragma:nocache
ContentEncoding:gzip
ContentType:text/htmlcharset=utf8
Connection:KeepAlive
Contentlength:1215
Date:Fri,30May201408:10:15GMT
.........<someblob>................
HTTPresponsestartswiththereturnedstatuscodefromtheserver .Followingisa
verybriefsummaryofwhatastatuscodedenotes:
http://edusagar.com/articles/view/70/What-happens-when-you-type-a-URL-in-browser

3/5

10/22/2016

What happens when you type a URL in browser | EduSagar

1xxindicatesaninformationalmessageonly
2xxindicatessuccessofsomekind
3xxredirectstheclienttoanotherURL
4xxindicatesanerrorontheclient'spart
5xxindicatesanerrorontheserver'spart
Serversetsvariousotherheaderstohelpbrowserrendertheproper
content. ContentTypetellsthetypeofthecontentthebrowserhasto
show,Contentlength tellsthenumberofbytesoftheresponse.Usingthe Content
Encoding header'svalue,browserscandecodetheblobdatapresentattheendof
theresponse.

Step8.Browsersdisplaysthehtmlcontent
Renderingofhtmlcontentisalsodoneinphases. Thebrowserfirstrendersthebare
bonehtmlstructure,andthenitsendsmultipleGET requeststofetchotherhyper
linkedstuf fe.g.Ifthehtmlresponsecontainsanimageintheformofimgtagssuch
as<imgsrc="/assets/img/logo.png"/> ,browserwillsendaHTTP GETrequestto
theservertofetchtheimagefollowingthecompletesetofstepswhichwehaveseen
tillnow.Butthisisn'tthatbadasitlooks.Staticfileslike images,javascript,css files
areallcachedbythebrowsersothatinfutureitdoesn'thavetofetchthemagain.

Step9.Clientinteractionwithserver
Onceahtmlpageisloaded,thereareseveralwaysausercaninteractwiththe
server.Forexample,hecallfilloutaloginformtosignintothewebsite. Thisalso
followsallthestepslistedabove,theonlydif ferenceisthattheHTTP requestthis
timewouldbea POSTinsteadofGET andalongwiththatrequest,browserwillsend
theformdatatotheserverforprocessing(usernameandpasswordinthiscase).

Onceserverauthenticatestheuser ,itwillsendtheproperHTML content(maybe


user'sprofile)backtothebrowserandthususerwillseethatnewwebpageafterhis
loginrequestisprocessed.

Step10.AJAXqueries
http://edusagar.com/articles/view/70/What-happens-when-you-type-a-URL-in-browser

4/5

10/22/2016

What happens when you type a URL in browser | EduSagar

Anotherformofclientinteractionwithserveristhrough AJAX(Asynchronous
JavaScriptAndXML) requests. ThisisanasynchronousGET/POST requesttowhich
servercansendaresponsebackinavarietyofways json,xml,html etc.AJAX
requestsdoesn'thinderthecurrentviewofthewebpageandworkinthebackground.
Becauseofthis,onecandynamicallymodifythecontentofawebpagebycallingan
AJAXrequestandupdatingthewebelementsusingJavascript.

Hopefully,thatgivesyouanideaofwhathappensinthebackgroundwhenwedoareallysimple
operationwhichistypeaURLinabrowser.
courtsey:http://igoro.com/archive/whatreallyhappenswhenyounavigatetoaurl/

http://edusagar.com/articles/view/70/What-happens-when-you-type-a-URL-in-browser

5/5

Vous aimerez peut-être aussi