引用

HTTP协议知识点(收集)

(一)对象更新校验方式:

HTTP通过两种方式验证对象是否有更新if-non-match或者if-modified-since.通过在Request中包含上述header向服务器发起询问。当response中包含E-tag头时,浏览器应该使用if-non-match来询问;response中含有last-modified头时,浏览器应用if-modified-since来进行更新询问。HTTP1.1规范建议使用E-tag方式(当不能使用e-tag方式时使用last-modified),但事实上很多现代服务器依然使用last-modified方式。当服务器同时使用E-tag和last-modified时,浏览器应同时发送if-non-match和if-modified-since头,服务器应同时对这两个头进行确认,只有同时符合未更新条件方可返回304响应。

(二)Cache控制:

1.用在request中的cache控制头

Pragma:no-cache:兼容早起HTTP协议版本如1.0+

Cache-Control:no-cache,表示不希望得到一个缓存内容。只是希望,cache设备可能忽略。

Cache-Control:no-store,表示client与server之间的设备不能缓存响应内容,并应该删除已有缓存。

Cache-Control:only-if-cached,表示只接受是被缓存的内容

2.用在response中控制cache的头

Cache-Control:max-age=3600,用相对于接收到的时间开始可缓存多久

Cache-Control:s-maxage=3600,与上面类似,只是s-maxage一般用在cache服务器上,并只对public缓存有效

Expires:Fri,05Jul2002,05:00:00GMT基于GMT的时间,绝对时间,但该头容易受到本地错误时间影响

Cache-Control:must-revalidate该头表示内容可以被缓存但每次必须询问是否有更新。

各种cache-control头值和意义:

Cache-Controlheaderdirectives

Directive

Messagetype

Description

no-cache

Request

Donotreturnacachedcopyofthedocumentwithoutfirstrevalidatingitwiththeserver.

no-store

Request

Donotreturnacachedcopyofthedocument.Donotstoretheresponsefromtheserver.

max-age

Request

Thedocumentinthecachemustnotbeolderthanthespecifiedage.

max-stale

Request

Thedocumentmaybestalebasedontheserver-specifiedexpirationinformation,butitmustnothavebeenexpiredforlongerthanthevalueinthisdirective.

min-fresh

Request

Thedocument'sagemustnotbemorethanitsageplusthespecifiedamount.Inotherwords,theresponsemustbefreshforatleastthespecifiedamountoftime.

no-transform

Request

Thedocumentmustnotbetransformedbeforebeingsent.

only-if-cached

Request

Sendthedocumentonlyifitisinthecache,withoutcontactingtheoriginserver.

public

Response

Responsemaybecachedbyanycache.

private

Response

Responsemaybecachedsuchthatitcanbeaccessedonlybyasingleclient.

no-cache

Response

Ifthedirectiveisaccompaniedbyalistofheaderfields,thecontentmaybecachedandservedtoclients,butthelistedheaderfieldsmustfirstberemoved.Ifnoheaderfieldsarespecified,thecachedcopymustnotbeservedwithoutrevalidationwiththeserver.

no-store

Response

Responsemustnotbecached.

no-transform

Response

Responsemustnotbemodifiedinanywaybeforebeingserved.

must-revalidate

Response

Responsemustberevalidatedwiththeserverbeforebeingserved.

proxy-revalidate

Response

Sharedcachesmustrevalidatetheresponsewiththeoriginserverbeforeserving.Thisdirectivecanbeignoredbyprivatecaches.

max-age

Response

Specifiesthemaximumlengthoftimethedocumentcanbecachedandstillconsideredfresh.

s-max-age

Response

Specifiesthemaximumageofthedocumentasitappliestosharedcaches(overridingthemax-agedirective,ifoneispresent).Thisdirectivecanbeignoredbyprivatecaches.

(三)两个特殊的HTTP动作options,trace

1.Trace可用来追踪在client和Server之间存在多少个代理服务器,当然前提是代理服务器支持设置via头,用法:

执行:

trace/tttt.gifHTTP/1.1

host:www.sohu.com

服务器会返回如下头信息

HTTP/1.0200OK

Date:Mon,16Mar200911:47:52GMT

Server:Apache/1.3.37(Unix)mod_gzip/1.3.26.1a

Content-Type:message/http

X-Cache:MISSfrom19709705.29867846.28603073.sohu.com

Via:1.019709705.29867846.28603073.sohu.com:80(squid)

Connection:close

服务器返回如下内容(这个内容反应的是中间代理服务器发向OWS的头部内容)

TRACE/HTTP/1.0

Cache-Control:max-age=36288000

Connection:keep-alive

Host:www.sohu.com

Via:1.119709705.29867846.28603073.sohu.com:80(squid)

X-Forwarded-For:58.31.225.229

从上可以看出,中间经过了19709705.29867846.28603073.sohu.com代理服务器,而且该服务器只支持http1.0

2.Options可用来探测请求某个对象时,服务器能支持的HTTP动作

OPTIONS/ssss.gifHTTP/1.1

host:www.sohu.com

HTTP/1.0200OK

Date:Mon,16Mar200911:59:17GMT

Server:Apache/1.3.37(Unix)mod_gzip/1.3.26.1a

Cache-Control:max-age=5184000

Expires:Fri,15May200911:59:17GMT

Content-Length:0

Allow:GET,HEAD,OPTIONS,TRACE

X-Cache:MISSfrom32583031.43658676.41464477.sohu.com

Via:1.132583031.43658676.41464477.sohu.com:80(squid)

Connection:close

(四)HTTP连接控制:

http连接可以分为1.顺序连接2并行连接3保持连接

顺序连接:是为每个对象建立一个TCP连接,这导致了传输中增加了大量的TCP建立、拆连时间

并行连接:同时建立多个TCP通道,并行传输对象,重叠了TCP连接建立时间,因而总体延迟会减少,但并行连接对客户端及服务器性能提出了更高要求,HTTP规范并行TCP连接不应超过2个,事实上现代浏览器已经支持6-10个不等

保持连接:

通过保持TCP通道的打开,在通道内连续传输对象,可以有效减少TCP建立带来的开销或TCP慢启动带来的影响。

在HTTP1.0+版本中开始引入keep-alive概念,在HTTP1.1中改为persistent,两者的区别是HTTP1.0中,必须在header中显式说明keep-alive,而HTTP1.1中persistent是默认行为,除非使用connection:close明确指明关闭连接。

使用keep-alive或persistent需注意:

在HTTP1.0中必须显式申明keep-alive,并在一个通道的后续request中也明确包含keep-alive,否则服务器将会认为client希望关闭连接;服务器的response中可以通过包含connection头来指明是同意keep-alive还是希望关闭连接。

使用保持连接必须在response中正确包含实体内容的长度或使用chunked,否则其他HTTPrequest将无法知道前一个对象是否传输完成。

(五)HTTP规范认为:如果Request中不含Accept-Encoding:即表示接受任意编码类型(例如GZIP压缩.------------实际测试发现并不一定成立。

(六)Chunked

这是一种传输编码,正常情况下http要求先知道对象的大小才能进行传输,以便接收端正确知道传输该何时结束,但是如果服务器无法报告对象的大小(例如)时,且连接是一个保持连接,则必须使用chunked传输。设置chunked后(在response头中设置transfer-encoding:chunked),对象将被切割为多个长度来传输,每次传输均指明当次内容长度,并在最后一次设置0以指示传输结束:

(七)区间请求(rangerequest)

http容许请求一个文档的指定区间内容,如果一次http下载因为某种原因中途失败,则http可以在下次请求使用range头,这样可以实现断点续传。同时range也广泛用在P2P类下载中,同时从多个服务器上下载同一类容以实现加快下载速度。

GET/bigfile.htmlHTTP/1.1

Host:www.joes-hardware.com

Range:bytes=4000-

User-Agent:Mozilla/4.61[en](WinNT;I)

在request头中包含Range:bytes=4000-表示已经下载4000bytes,本次请求从4000bytes开始即可。

而在response中可以设置Accept-Ranges:bytes以表示服务器可以接受range请求,并求度量单位是byte。

(八)DeltaEncoding

一种减少http传输量的方法,正常情况下,如果服务器端一个文档更新后,将导致在下次客户端请求时,服务器端发送整个新文档给客户端,而如果这个文档只是更新了一小部分,重新传输完整的文档则是对资源的一种浪费。http通过deltaencoding技术实现只传输变化部分,其技术原理是:

1.服务器在第一次响应中包含一个e-tag头,表示该文档的一个唯一版本识别码

2.客户端在下一次请求时,将在request中包含if-non-match头,向服务器询问该文档是否有更新;同时在request设置A-IM(accept-instancemanipulation)头表示可以接受delta技术。

3.服务器在接到请求后发现自己拥有文档的新版本(因为文档的e-tag已经变化了),于是在响应中包含IM头,e-tag头,delta-base头向客户端表明文档是如何更新的,其中IM头的值表示的是delta的某种算法,e-tag头是新的e-tag,delta-base表示本次delta算法是基于哪个版本计算出来的(正常情况下应该等于request中的if-non-match头值)

4.客户端在接到response后启动delta算法更新本地文档,并更新本地文档的e-tag值为新的e-tag值。

在delta算法中用到的头有:

Delta-encodingheaders

Header

Description

ETag

Uniqueidentifierforeachinstanceofadocument.Sentbytheserverintheresponse;usedbyclientsinsubsequentrequestsinIf-MatchandIf-None-Matchheaders.

If-None-Match

Requestheadersentbytheclient,askingtheserverforadocumentifandonlyiftheclient'sversionofthedocumentisdifferentfromtheserver's.

A-IM

Clientrequestheaderindicatingtypesofinstancemanipulationsaccepted.

IM

Serverresponseheaderspecifyingthetypeofinstancemanipulationappliedtotheresponse.Thisheaderissentwhentheresponsecodeis226IMUsed.

Delta-Base

ServerresponseheaderthatspecifiestheETagofthebasedocumentusedforgeneratingthedelta(shouldbethesameastheETagintheclientrequest'sIf-None-Matchheader).

可以包含在A-IM和IM头中的值有(即delta可用的算法):

IANAregisteredtypesofinstancemanipulations

Type

Description

vcdiff

Deltausingthevcdiffalgorithm[14]

diffe

DeltausingtheUnixdiff-ecommand

gdiff

Deltausingthegdiffalgorithm[15]

gzip

Compressionusingthegzipalgorithm

deflate

Compressionusingthedeflatealgorithm

range

Usedinaserverresponsetoindicatethattheresponseispartialcontentastheresultofarangeselection

identity

Usedinaclientrequest'sA-IMheadertoindicatethattheclientiswillingtoacceptanidentityinstancemanipulation

(九)HTTP状态码一览表:

Statuscodes

Statuscode

Reasonphrase

Meaning

100

Continue

Aninitialpartoftherequestwasreceived,andtheclientshouldcontinue.

101

SwitchingProtocols

Theserverischangingprotocols,asspecifiedbytheclient,toonelistedintheUpgradeheader.

200

OK

Therequestisokay.

201

Created

Theresourcewascreated(forrequeststhatcreateserverobjects).

202

Accepted

Therequestwasaccepted,buttheserverhasnotyetperformedanyactionwithit.

203

Non-AuthoritativeInformation

Thetransactionwasokay,excepttheinformationcontainedintheentityheaderswasnotfromtheoriginserver,butfromacopyoftheresource.

204

NoContent

Theresponsemessagecontainsheadersandastatusline,butnoentitybody.

205

ResetContent

Anothercodeprimarilyforbrowsers;basicallymeansthatthebrowsershouldclearanyHTMLformelementsonthecurrentpage.

206

PartialContent

Apartialrequestwassuccessful.

300

MultipleChoices

AclienthasrequestedaURLthatactuallyreferstomultipleresources.Thiscodeisreturnedalongwithalistofoptions;theusercanthenselectwhichonehewants.

301

MovedPermanently

TherequestedURLhasbeenmoved.TheresponseshouldcontainaLocationURLindicatingwheretheresourcenowresides.

302

Found

Likethe301statuscode,butthemoveistemporary.TheclientshouldusetheURLgivenintheLocationheadertolocatetheresourcetemporarily.

303

SeeOther

TellstheclientthattheresourceshouldbefetchedusingadifferentURL.ThisnewURLisintheLocationheaderoftheresponsemessage.

304

NotModified

Clientscanmaketheirrequestsconditionalbytherequestheaderstheyinclude.Thiscodeindicatesthattheresourcehasnotchanged.

305

UseProxy

Theresourcemustbeaccessedthroughaproxy,thelocationoftheproxyisgivenintheLocationheader.

306

(Unused)

Thisstatuscodecurrentlyisnotused.

307

TemporaryRedirect

Likethe301statuscode;however,theclientshouldusetheURLgivenintheLocationheadertolocatetheresourcetemporarily.

400

BadRequest

Tellstheclientthatitsentamalformedrequest.

401

Unauthorized

Returnedalongwithappropriateheadersthatasktheclienttoauthenticateitselfbeforeitcangainaccesstotheresource.

402

PaymentRequired

Currentlythisstatuscodeisnotused,butithasbeensetasideforfutureuse.

403

Forbidden

Therequestwasrefusedbytheserver.

404

NotFound

TheservercannotfindtherequestedURL.

405

MethodNotAllowed

ArequestwasmadewithamethodthatisnotsupportedfortherequestedURL.TheAllowheadershouldbeincludedintheresponsetotelltheclientwhatmethodsareallowedontherequestedresource.

406

NotAcceptable

Clientscanspecifyparametersaboutwhattypesofentitiestheyarewillingtoaccept.ThiscodeisusedwhentheserverhasnoresourcematchingtheURLthatisacceptablefortheclient.

407

ProxyAuthenticationRequired

Likethe401statuscode,butusedforproxyserversthatrequireauthenticationforaresource.

408

RequestTimeout

Ifaclienttakestoolongtocompleteitsrequest,aservercansendbackthisstatuscodeandclosedowntheconnection.

409

Conflict

Therequestiscausingsomeconflictonaresource.

410

Gone

Likethe404statuscode,exceptthattheserveronceheldtheresource.

411

LengthRequired

ServersusethiscodewhentheyrequireaContent-Lengthheaderintherequestmessage.TheserverwillnotacceptrequestsfortheresourcewithouttheContent-Lengthheader.

412

PreconditionFailed

Ifaclientmakesaconditionalrequestandoneoftheconditionsfails,thisresponsecodeisreturned.

413

RequestEntityTooLarge

Theclientsentanentitybodythatislargerthantheservercanorwantstoprocess.

414

RequestURITooLong

TheclientsentarequestwitharequestURLthatislargerthanwhattheservercanorwantstoprocess.

415

UnsupportedMediaType

Theclientsentanentityofacontenttypethattheserverdoesnotunderstandorsupport.

416

RequestedRangeNotSatisfiable

Therequestmessagerequestedarangeofagivenresource,andthatrangeeitherwasinvalidorcouldnotbemet.

417

ExpectationFailed

TherequestcontainedanexpectationintheExpectrequestheaderthatcouldnotbesatisfiedbytheserver.

500

InternalServerError

Theserverencounteredanerrorthatpreventeditfromservicingtherequest.

501

NotImplemented

Theclientmadearequestthatisbeyondtheserver'scapabilities.

502

BadGateway

Aserveractingasaproxyorgatewayencounteredabogusresponsefromthenextlinkintherequestresponsechain.

503

ServiceUnavailable

Theservercannotcurrentlyservicetherequestbutwillbeabletointhefuture.

504

GatewayTimeout

Similartothe408statuscode,exceptthattheresponseiscomingfromagatewayorproxythathastimedoutwaitingforaresponsetoitsrequestfromanotherserver.

505

HTTPVersionNotSupported

Theserverreceivedarequestinaversionoftheprotocolthatitcan'torwon'tsupport.

(十)【原创】一个负载均衡与E-tag头矛盾导致缓存效果变坏的实例分析:

负载均衡服务器后端是WEB服务器,但这些服务器是异构的比如说有linux的有windows的。

linux上设置httpresponse中含last-modified头,但没有etag头:

linux.jpg(16.64KB)

2009-3-1319:02

windows服务器上设置response中既有last-modified又有etag头。

windows.jpg(12.37KB)

2009-3-1319:02

第一次打开网站,a图片是从windows服务器上下到的,b图片是从linux服务器上下到的。

第二次打开网站(第2次打开时候超过缓存时间,由于该网站响应中只含有last-modified头,因此浏览器会使用启发式机制来计算可缓存时间。启发式缓存时间控制会有一个计算系数

WA上的assembly策略中有一个50%的系数就是控制这个的)

浏览器在请求a图片时候,被分配到了linux服务器上,b图片被分配到了windows服务器上。

由于a图片在第一次下载时拥有etag和last-modified两种属性。因此在第二次请求时浏览器会同时进行带2个条件的get

if-none-match和if-modified-since,根据http规范必须这2个条件同时满足未变化才会返回304。可惜第2次请求被分配到了linux服务器上,这个服务器是没有设置etag属性的,本来可以从本地缓存的图片却变成了重新下载:

截图00.jpg(57.37KB)

2009-3-1319:02

进一步分析:

使用e-tag是一件很坏的事情:

不同的服务器对同样的e-tag算出的值是不一样的,如果用e-tag作为判断条件,在被负载均衡到不同服务器后,则很容易导致缓存失效

截图01.jpg(52.91KB)

2009-3-1319:11

上图,同一图片在不同服务器上e-tag不同导致重新下载。

从服务器选择上看,这个图片这次恰好又分配到了另一台windows服务器,这样e-tag和last-modified头都有了,可以看到时间没有变化。可惜的是由于e-tag不一致导致重新下载。

(十一)使用yahoo的Yslow评测工具分析一个站点在HTTP方面所做的优化:

YahooWEB应用开发团队是HTTP应用优化的倡导者和身体力行者,其开发团队根据多年的经验总结了数条网站优化规则,并编写成程序,该程序已经被众多的测试人员所津津乐道,并和强大的firebug工具集成,成为开发和测试人员的有利工具。

安装方法:

1.下载安装Firefox浏览器

2.下载安装Firefox浏览插件firebug

3.下载安装Yslow

使用方法很简单,类似httpwatch,打开一个网站时,该程序会自动分析并评测:

从上面可以看出,网站总体测评分较低,属于F级别(A最优),其中还列出了具体可以优化的项目,并给各个项目的测评级别,例如可以再减少一些HTTP请求书,哪些项目可以使用CDN优化,哪些项目可以使用expire头或GZIP压缩等等。从上面的结果看,前5项都有很大的优化空间。具体内容可以展开项目后的三角形箭头查看,例如CDN部分:

对象过期优化:

一些对象位置的优化:

http://www.cppblog.com/age100/archive/2010/06/25/118688.aspx