http chunked

11 篇文章 2 订阅
5 篇文章 0 订阅

最近关注到一个HTTP/1.1chunked, 说的是分块传输,这篇做个总结吧

  • 前言
    一般在HTTP API设计时,返回的参数或jsonxml亦或是一个静态文件, 他们的Content-Length响应头总是确定的,所以浏览器接受到API返回后,接收完Content-Length中定义的长度字节后开始解析页面,针对大文件比如超过1个G的,此种方式肯定不可取,因为接受完整个返回的时间可能超过用户的无感时间,又或者是一个不能预先确定报文体的长度的返回,不可能在头中包含Content-Length域来指明报文体长度,那么解决方式就是Transfer-Encoding: chunked

  • chunked原理
    浏览器不需要等到内容字节全部下载完成,只要接收到一个chunked块就可解析页面
    chunked编码将大块数据分解成多块小数据,每块指定长度。
    以BNF文法描述其格式如下 from RFC2616

    Chunked-Body   = *chunk
                     last-chunk
                     trailer
                     CRLF
    
    chunk          = chunk-size [ chunk-extension ] CRLF
                     chunk-data CRLF
    chunk-size     = 1*HEX
    last-chunk     = 1*("0") [ chunk-extension ] CRLF
    
    chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
    chunk-ext-name = token
    chunk-ext-val  = token | quoted-string
    chunk-data     = chunk-size(OCTET)
    trailer        = *(entity-header CRLF)
    

    承上-简单格式上形如: [Chunk大小][回车][Chunk数据体][回车][Chunk大小][回车][Chunk数据体][回车][0][回车]

  • chunked报文范例

    找了一个网址支持chunked的文章末尾取地址wireshark报文如下

    GET /yrwHcjSl0MgCo2Kml5_Y_D3/api/customsearch/apiaccept?sid=10742016945123576423&v=2.0&callback=csdn.afterBaiduSearchInit HTTP/1.1
    Host: gsp0.baidu.com
    Connection: keep-alive
    Cache-Control: max-age=0
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
    Accept-Encoding: gzip, deflate
    Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
    Cookie: PSTM=1646097748; BIDUPSID=5832D3D4FD5494065CD8EACE5E75B23D; BAIDUID=B9A20FFE4D774186F1D279C2B39B0C59:FG=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=36465_36455_34812_36420_36165_36487_36074_36055_36265_26350_36299_36469_22159_36447; BA_HECTOR=0h20a18ga1042181a01h8u5o514; delPer=0; PSINO=6
    
    HTTP/1.1 200 OK
    Content-Type: application/javascript
    Date: Thu, 26 May 2022 06:05:34 GMT
    Server: Apache
    Tracecode: 03346757942724521226052614
    Transfer-Encoding: chunked
    
    f36
    window.BCSE_KEY = '', window.BCSE_VS = '2.0', window.BCSE_TK = '5ff687f1302e609d08e92386af91fb15';!function(){window.BCse={};var e=function(e){"use strict";var t,r,o,a,n,s,i,c,u;return r=function(e,t,r){e.addEventListener?e.addEventListener(t,r,!1):e.attachEvent?e.attachEvent("on"+t,r):e["on"+t]=r},o=function(r,o){t.log("Garbage collecting!"),o.parentNode.removeChild(o),e[r]=void 0;try{delete e[r]}catch(a){}},a=function(e,t){var r,o,a="";for(r in e)e.hasOwnProperty(r)&&(r=t?encodeURIComponent(r):r,o=t?encodeURIComponent(e[r]):e[r],a+=r+"="+o+"&");return a.replace(/&$/,"")},n=function(){var e="",t=[],r="0123456789ABCDEF",o=0;for(o=0;32>o;o+=1)t[o]=r.substr(Math.floor(16*Math.random()),1);return t[12]="4",t[16]=r.substr(3&t[16]|8,1),e="flyjsonp_"+t.join("")},s=function(e,r){t.log(r),"undefined"!=typeof e&&e(r)},i=function(e,r){t.log("GET success"),"undefined"!=typeof e&&e(r),t.log(r)},c=function(e,r){t.log("POST success"),"undefined"!=typeof e&&e(r),t.log(r)},u=function(e){t.log("Request complete"),"undefined"!=typeof e&&e()},t={},t.options={debug:!1},t.init=function(e){var r;t.log("Initializing!");for(r in e)e.hasOwnProperty(r)&&(t.options[r]=e[r]);return t.log("Initialization options"),t.log(t.options),!0},t.log=function(r){e.console&&t.options.debug&&e.console.log(r)},t.get=function(m){m=m||{};var d=m.url,p=m.callbackParameter||"callback",l=m.parameters||{},y=e.document.createElement("script"),h=n(),f="?";if(!d)throw new Error("URL must be specified!");l[p]=h,d.indexOf("?")>=0&&(f="&"),d+=f+a(l,!0),e[h]=function(e){"undefined"==typeof e?s(m.error,"Invalid JSON data returned"):"post"===m.httpMethod?(e=e.query.results,e&&e.postresult?(e=e.postresult.json?e.postresult.json:e.postresult,c(m.success,e)):s(m.error,"Invalid JSON data returned")):i(m.success,e),o(h,y),u(m.complete)},t.log("Getting JSONP data"),y.setAttribute("src",d),y.setAttribute("charset","utf-8"),e.document.getElementsByTagName("head")[0].appendChild(y),r(y,"error",function(){o(h,y),u(m.complete),s(m.error,"Error while trying to access the URL")})},t.post=function(e){e=e||{};var r,o,n=e.url,s=e.parameters||{},i={};if(!n)throw new Error("URL must be specified!");r=encodeURIComponent('select * from jsonpost where url="'+n+'" and postdata="'+a(s,!1)+'"'),o="http://query.yahooapis.com/v1/public/yql?q="+r+"&format=json&env="+encodeURIComponent("store://datatables.org/alltableswithkeys"),i.url=o,i.httpMethod="post","undefined"!=typeof e.success&&(i.success=e.success),"undefined"!=typeof e.error&&(i.error=e.error),"undefined"!=typeof e.complete&&(i.complete=e.complete),t.get(i)},t}(window),t=function(){function e(){f=1}function t(){f=0}function o(e){e&&r.count(e)>0&&(e.erroStatus&&0===e.erroStatus.erroNo?(p=0,e.searchInfo&&(l=e.searchInfo,e.searchInfo.facets&&(y=e.searchInfo.facets)),e.blockData&&(d=e.blockData)):p=e.erroStatus.erroNo)}function a(e){e&&r.count(e)>0&&(0===e.error?(p=0,h=e):p=6)}function n(){var e=[];if(0===p&&d.length>0)for(var t=0;t<d.length;t++)e[t]={},e[t].title=d[t].title?d[t].title:"",0===f&&(e[t].title=e[t].title.replace(/&lt;em&gt;/g,"").replace(/<em>/g,""),e[t].title=e[t].title.replace(/&lt;\/em&gt;/g,"").replace(/<\/em>/g,"")),e[t].linkUrl=d[t].linkurl?d[t].linkurl:"",e[t]["abstract"]=d[t]["abstract"]?d[t]["abstract"]:"",e[t].image=d[t].image?d[t].image:"",e[t].dispUrl=d[t].dispurl?d[t].dispurl:"",e[t].dispTime=d[t].timeshow?d[t].timeshow:"",d[t].summarywords&&r.count(d[t].summarywords)>0&&(5===l.tplId?(e[t].forumName=d[t].summarywords.forumName?d[t].summarywords.forumName:"",e[t].author=d[t].summarywords.author?d[t].summarywords.author:"",e[t].createdTime=d[t].summarywords.createdTime?d[t].summarywords.createdTime:"",e[t].lastReplyTime=d[t].summarywords.lastReplyTime?d[t].summarywords.lastReplyTime:"",e[t].replyCount=d[t].summarywords.replyCount?d[t].summarywords.replyCount:"",e[t].viewCount=d[t].summarywords.viewCount?d[t].summarywords.vie
    2890
    wCount:"",e[t].threadClassify=d[t].summarywords.threadClassify?d[t].summarywords.threadClassify:""):3===l.tplId?(e[t].genre=d[t].summarywords.genre?d[t].summarywords.genre:"",e[t].downloadCount=d[t].summarywords.downloadCount?d[t].summarywords.downloadCount:"",e[t].dateModified=d[t].summarywords.dateModified?d[t].summarywords.dateModified:"",e[t].gameSubCategory=d[t].summarywords.gameSubCategory?d[t].summarywords.gameSubCategory:"",e[t].datePublished=d[t].summarywords.datePublished?d[t].summarywords.datePublished:"",e[t].version=d[t].summarywords.version?d[t].summarywords.version:"",e[t].downloadUrl=d[t].summarywords.downloadUrl?d[t].summarywords.downloadUrl:"",e[t].gameCategory=d[t].summarywords.gameCategory?d[t].summarywords.gameCategory:"",e[t].typicalAgeRange=d[t].summarywords.typicalAgeRange?d[t].summarywords.typicalAgeRange:"",e[t].tag=d[t].summarywords.tag?d[t].summarywords.tag:"",e[t].device=d[t].summarywords.device?d[t].summarywords.device:"",e[t].inLanguage=d[t].summarywords.inLanguage?d[t].summarywords.inLanguage:""):2===l.tplId?(e[t].genre=d[t].summarywords.genre?d[t].summarywords.genre:"",e[t].wordCount=d[t].summarywords.wordCount?d[t].summarywords.wordCount:"",e[t].author=d[t].summarywords.author?d[t].summarywords.author:"",e[t].trialStatus=d[t].summarywords.trialStatus?d[t].summarywords.trialStatus:"",e[t].updateStatus=d[t].summarywords.updateStatus?d[t].summarywords.updateStatus:"",e[t].totalClick=d[t].summarywords.totalClick?d[t].summarywords.totalClick:"",e[t].weeklyClick=d[t].summarywords.weeklyClick?d[t].summarywords.weeklyClick:"",e[t].monthlyClick=d[t].summarywords.monthlyClick?d[t].summarywords.monthlyClick:"",e[t].dateModified=d[t].summarywords.dateModified?d[t].summarywords.dateModified:""):1===l.tplId?(e[t].genre=d[t].summarywords.genre?d[t].summarywords.genre:"",e[t].contentLocation=d[t].summarywords.contentLocation?d[t].summarywords.contentLocation:"",e[t].description=d[t].summarywords.description?d[t].summarywords.description:"",e[t].host=d[t].summarywords.host?d[t].summarywords.host:"",e[t].provider=d[t].summarywords.provider?d[t].summarywords.provider:"",e[t].actorName=d[t].summarywords.actorName?d[t].summarywords.actorName:""):6===l.tplId?(e[t].tag=d[t].summarywords.tag?d[t].summarywords.tag:"",e[t].author_nickname=d[t].summarywords.author_nickname?d[t].summarywords.author_nickname:"",e[t].author_url=d[t].summarywords.author_url?d[t].summarywords.author_url:"",e[t].replyCount=d[t].summarywords.replyCount?d[t].summarywords.replyCount:"",e[t].property=d[t].summarywords.property?d[t].summarywords.property:"",e[t].breadCrumb_url=d[t].summarywords.breadCrumb_url?d[t].summarywords.breadCrumb_url:"",e[t].breadCrumb_title=d[t].summarywords.breadCrumb_title?d[t].summarywords.breadCrumb_title:"",e[t].price_new=d[t].summarywords.price_new?d[t].summarywords.price_new:"",e[t].price_old=d[t].summarywords.price_old?d[t].summarywords.price_old:"",e[t].discount_value=d[t].summarywords.discount_value?d[t].summarywords.discount_value:"",e[t].discount_description=d[t].summarywords.discount_description?d[t].summarywords.discount_description:"",e[t].review_rating=d[t].summarywords.review_rating?d[t].summarywords.review_rating:"",e[t].review_count=d[t].summarywords.review_count?d[t].summarywords.review_count:"",e[t].location_address=d[t].summarywords.location_address?d[t].summarywords.location_address:"",e[t].location_tel=d[t].summarywords.location_tel?d[t].summarywords.location_tel:"",e[t].location_area=d[t].summarywords.location_area?d[t].summarywords.location_area:"",e[t].time_startDate=d[t].summarywords.time_startDate?d[t].summarywords.time_startDate:"",e[t].time_endDate=d[t].summarywords.time_endDate?d[t].summarywords.time_endDate:""):8===l.tplId&&(e[t].actor_name=d[t].summarywords.actor_name?d[t].summarywords.actor_name:"",e[t].genre=d[t].summarywords.genre?d[t].summarywords.genre:"",e[t].contentLocation=d[t].summarywords.contentLocation?d[t].summarywords.contentLocation:"",e[t].director_name=d[t].summarywords.director_name?d[t].summarywords.director_name:"",e[t].inLanguage=d[t].summarywords.inLanguage?d[t].summarywords.inLanguage:"",e[t].commentNum=d[t].summarywords.commentNum?d[t].summarywords.commentNum:""));return e}function s(){var e=[];if(0===p&&d.length>0)for(var t=0;t<d.length;t++)e[t]=d[t];return e}function i(){var e={};return 0===p&&r.count(l)>0&&(e.totalNum=l.totalNum?l.totalNum:"",e.tplId=l.tplId?l.tplId:"",e.curPage=l.curPage?l.curPage:"",e.searchTime=l.searchTime?l.searchTime:"",e.orgQuery=l.jcInfo&&l.jcInfo.org_query?l.jcInfo.org_query:"",e.jcQuery=l.jcInfo&&l.jcInfo.jc_query?l.jcInfo.jc_query:"",e.jcStatus=l.jcInfo&&l.jcInfo.search_status?l.jcInfo.search_status:""),e}function c(){var e={};return 0===p&&r.count(y)>0&&(e=y),e}function u(){var e=[],t=[];if(h.data&&h.data.sug&&(t=h.data.sug),0===p&&t.length>0)for(var r=0;r<t.length;r++)e[r]={},e[r].title=t[r].title?t[r].title:"",!h.type||"music"!==h.type&&"novel"!==h.type||(e[r].author=t[r].author?t[r].author:""),h.type&&"movie"===h.type&&(e[r].type=t[r].type?t[r].type:"");return e}function m(){var e={};switch(e.status=p,p){case 0:e.reason="Success!";break;case 1:e.reason="Datas are wrong! Try again later.";break;case 2:e.reason="Token is illegal! Try again later.";break;case 3:e.reason="Params are wrong! Try again later.";break;case 4:e.reason="No data.";break;case 5:e.reason="Sid is illegal! Try again later";break;case 6:e.reason="Try again later.";break;default:e.reason="Unknown error. Try again later."}return e}var d=[],p=0,l={},y={},h={},f=1;return{openHiLight:e,closeHiLight:t,initData:o,initSugData:a,returnResult:n,returnOsResult:s,returnErr:m,returnInfo:i,returnFacet:c,returnSug:u,resultData:d,infoData:l,errData:p,sugData:h}}(),r=function(){function e(e,t){var r,o=e||{};for(r in t)t.hasOwnProperty(r)&&(o[r]=t[r]);return o}function t(e,r){var o,a=e||{};for(o in r)r.hasOwnProperty(o)&&("[object Object]"===Object.prototype.toString.call(r[o])?(e[o]=e[o]||{},a[o]=t(e[o],r[o])):a[o]=r[o]);return a}function r(e,t){if(t=t||document,t.getElementsByClassName)return t.getElementsByClassName(e);for(var r=[],o=t.getElementsByTagName("*"),a=new RegExp("(^|\\s)"+e.replace(/\-/g,"\\-")+"(\\s|$)"),n=0,s=o.length;s>n;n++)a.test(o[n].className)&&r.push(o[n]);return r}function o(e){return String(e).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#39;")}function a(e){var t=String(e).replace(/&quot;/g,'"').replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&");return t.replace(/&#([\d]+);/g,function(e,t){return String.fromCharCode(parseInt(t,10))})}function n(e){var t=new RegExp("(^| )"+e+"=([^;]*)(;|$)"),r=t.exec(document.cookie);return r?r[2]:null}function s(e,t,r){document.addEventListener?e.addEventListener(t,r,!1):document.attachEvent&&e.attachEvent("on"+t,function(t){t.preventDefault=function(){t.returnValue=!1},t.stopPropagation=function(){t.cancelBubble=!0},r.call(e,t)})}function i(e,t,r,o){s(e,t,function(t){for(var a=t.target||t.srcElement;!_hasClass(a,r)&&a!=e;)a=a.parentNode;_hasClass(a,r)&&o.call(a,t)})}function c(e){return""==e||e.match(/^\s+$/)?!0:!1}function u(e,t){var r=document.createElement("script");r.readyState?r.onreadystatechange=function(){("loaded"===r.readyState||"complete"===r.readyState)&&(r.onreadystatechange=null,t())}:r.onload=function(){t()},r.src=e,r.charset="utf-8",r.async=!0,document.getElementsByTagName("head")[0].appendChild(r)}function m(e){var t=typeof e;if("string"==t)return e.length;if("object"==t){var r=0;for(var o in e)r++;return r}return!1}function d(e,t,r){var o,a=Array.prototype.indexOf,n=e.length;if(0===n)return-1;if(r=+r||0,o=0>r?Math.max(0,r+n):r,a)return a.call(e,t,o);for(;n>o;o++)if(o in e&&e[o]===t)return o;return-1}function p(e){return e?e.replace(/^\s+|\s+$/g,""):void 0}return{extend:e,extendDeep:t,getElementsByClassName:r,encodeHTML:o,decodeHTML:a,getCookie:n,addEvent:s,count:m,delegateEvent:i,isEmptyOrSpacing:c,loadScript:u,indexOf:d,trim:p}}(),o=function(){function o(e,t){this.numReg=new RegExp("^[0-9]*$"),this.searchReady=1,this.initReady=1,this.sugReady=1;var r=window.location.protocol;return this.searchUrl="https:"===r?"https://gsp0.baidu.com/yrwHcjSl0MgCo2Kml5_Y_D3/api/customsearch/apisearch":"http://zhannei.baidu.com/api/customsearch/apisearch",this.sugUrl="http://znsv.baidu.com/customer_search/api/sug",this.param={nojc:1},e&&""!==e?(t&&2===t?(this.sid=e,this.os=1,this.cse=2,this.queryReady=0,this.facetReady=0):(this.sid=e,this.cse=1),this.token=BCSE_TK,void(this.version=BCSE_VS)):!1}return o.prototype.openTitleFilter=function(){this.closeTitleFilter();var e={stp:1};r.extend(this.param,e)},o.prototype.closeTitleFilter=function(){this.param.hasOwnProperty("stp")&&delete this.param.stp},o.prototype.openTimeFilter=function(e){if(e&&this.numReg.test(e)){this.closeTimeFilter();var t={sti:e};r.extend(this.param,t)}},o.prototype.closeTimeFilter=function(){this.param.hasOwnProperty("sti")&&delete this.param.sti},o.prototype.openGBK=function(){this.closeGBK();var e={ie:"gb2312"};r.extend(this.param,e)},o.prototype.closeGBK=function(){this.param.hasOwnProperty("ie")&&delete this.param.ie},o.prototype.openWap=function(){this.closeWap();var e={adapt:1};r.extend(this.param,e)},o.prototype.closeWap=function(){this.param.hasOwnProperty("adapt")&&delete this.param.adapt},o.prototype.closeJc=function(){this.openJc();var e={nojc:1};r.extend(this.param,e)},o.prototype.openJc=function(){this.param.hasOwnProperty("nojc")&&delete this.param.nojc},o.prototype.openSq=function(){this.closeSq();var e={sq:1};r.extend(this.param,e)},o.prototype.closeSq=function(){this.param.hasOwnProperty("sq")&&delete this.param.sq},o.prototype.openFieldFilter=function(e){if("[object Object]"===Object.prototype.toString.call(e)&&r.count(e)>0){this.closeFieldFilter();var t="",o=r.count(e),a=1;for(var n in e){if(a===o)if("[object Array]"===Object.prototype.toString.call(e[n])&&e[n].length>0)for(var s=0;s<e[n].length;s++)t=s===e[n].length-1?t+encodeURIComponent(n)+"[]="+encodeURIComponent(e[n][s]):t+encodeURIComponent(n)+"[]="+encodeURIComponent(e[n][s])+"&";else t=t+encodeURIComponent(n)+"="+encodeURIComponent(e[n]);else if("[object Array]"===Object.prototype.toString.call(e[n])&&e[n].length>0)for(var s=0;s<e[n].length;s++)t=t+encodeURIComponent(n)+"[]="+encodeURIComponent(e[n][s])+"&";else t=t+encodeURIComponent(n)+"="+encodeURIComponent(e[n])+"
    b50
    &";a++}a=1;var i={flt:t};r.extend(this.param,i)}},o.prototype.closeFieldFilter=function(){this.param.hasOwnProperty("flt")&&delete this.param.flt},o.prototype.openFeildFilter=function(e){if("[object Object]"===Object.prototype.toString.call(e)&&r.count(e)>0){this.closeFieldFilter();var t="",o=r.count(e),a=1;for(var n in e){if(a===o)if("[object Array]"===Object.prototype.toString.call(e[n])&&e[n].length>0)for(var s=0;s<e[n].length;s++)t=s===e[n].length-1?t+encodeURIComponent(n)+"[]="+encodeURIComponent(e[n][s]):t+encodeURIComponent(n)+"[]="+encodeURIComponent(e[n][s])+"&";else t=t+encodeURIComponent(n)+"="+encodeURIComponent(e[n]);else if("[object Array]"===Object.prototype.toString.call(e[n])&&e[n].length>0)for(var s=0;s<e[n].length;s++)t=t+encodeURIComponent(n)+"[]="+encodeURIComponent(e[n][s])+"&";else t=t+encodeURIComponent(n)+"="+encodeURIComponent(e[n])+"&";a++}a=1;var i={flt:t};r.extend(this.param,i)}},o.prototype.closeFeildFilter=function(){this.param.hasOwnProperty("flt")&&delete this.param.flt},o.prototype.openTimeOrder=function(){this.closeOrder();var e={srt:"lds"};r.extend(this.param,e)},o.prototype.openFieldOrder=function(e){if("[object Object]"===Object.prototype.toString.call(e)&&r.count(e)>0){this.closeOrder();var t="";if(2===this.cse){var o=1;for(var a in e)t+=1===e[a]?1===o?"-"+a:"&-"+a:1===o?a:"&"+a,o++;o=1}else{var o=1;for(var a in e)1===e[a]?1===o&&(t+="-"+a):1===o&&(t+=a),o++;o=1}var n={srt:t};r.extend(this.param,n)}},o.prototype.closeOrder=function(){this.param.hasOwnProperty("srt")&&delete this.param.srt},o.prototype.openFeildOrder=function(e){if("[object Object]"===Object.prototype.toString.call(e)&&r.count(e)>0){this.closeOrder();var t="";if(2===this.cse){var o=1;for(var a in e)t+=1===e[a]?1===o?"-"+a:"&-"+a:1===o?a:"&"+a,o++;o=1}else{var o=1;for(var a in e)1===e[a]?1===o&&(t+="-"+a):1===o&&(t+=a),o++;o=1}var n={srt:t};r.extend(this.param,n)}},o.prototype.closeOrder=function(){this.param.hasOwnProperty("srt")&&delete this.param.srt},o.prototype.setPageNum=function(e){if(e&&this.numReg.test(e)){if(10===e)return void(this.param.hasOwnProperty("pn")&&delete this.param.pn);e>100&&(e=100);var t={pn:e};r.extend(this.param,t)}},o.prototype.setResultType=function(e){if(0===e||1===e||2===e||3===e||5===e||6===e||8===e){if(0===e)return void(this.param.hasOwnProperty("rt")&&delete this.param.rt);var t={rt:e};r.extend(this.param,t)}},o.prototype.openHiLight=function(){t.openHiLight()},o.prototype.closeHiLight=function(){t.closeHiLight()},o.prototype.setSearchRange=function(e,t){if(2!==this.cse&&(1===e||2===e||3===e||4===e)){if(4===e)return this.param.hasOwnProperty("ct")&&delete this.param.ct,void(this.param.hasOwnProperty("cc")&&delete this.param.cc);var o="",a={ct:e};r.extend(this.param,a);var o="";2===e?t.length>0&&(o+=t.join("&")):t.length>0&&(o=t[0]);var n={cc:encodeURIComponent(o)};r.extend(this.param,n)}},o.prototype.setQuery
    ed7
    List=function(e){if("[object Object]"===Object.prototype.toString.call(e)&&r.count(e)>0&&2===this.cse){this.param.hasOwnProperty("qf")&&delete this.param.qf;var t="",o=r.count(e),a=1;for(var n in e)t=a===o?t+encodeURIComponent(n)+"="+encodeURIComponent(e[n]):t+encodeURIComponent(n)+"="+encodeURIComponent(e[n])+"&",a++;a=1;var s={qf:t};r.extend(this.param,s),this.queryReady=1}},o.prototype.setDispList=function(e){if("[object Array]"===Object.prototype.toString.call(e)&&e.length>0&&2===this.cse){this.param.hasOwnProperty("fl")&&delete this.param.fl;var t="";t=e.join("&");var o={fl:t};r.extend(this.param,o)}},o.prototype.setFacet=function(e){if("[object Object]"===Object.prototype.toString.call(e)&&r.count(e)>0&&2===this.cse){var t="";if(e.fields&&"[object Array]"===Object.prototype.toString.call(e.fields)&&e.fields.length>0&&(t=t+"fields[]="+e.fields.join("&fields[]=")),e.size&&(""!==t&&(t+="&"),t=t+"size="+encodeURIComponent(e.size)),e.mincount&&(""!==t&&(t+="&"),t=t+"mincount="+encodeURIComponent(e.mincount)),e.pivot&&"[object Array]"===Object.prototype.toString.call(e.pivot)&&e.pivot.length>0&&(""!==t&&(t+="&"),t=t+"pivot[]="+e.pivot.join("&pivot[]=")),""!==t){var o={facet:t};r.extend(this.param,o),this.facetReady=1}}},o.prototype.setQueryWeight=function(e){if("string"==typeof e&&2===this.cse){this.param.hasOwnProperty("pf")&&delete this.param.pf;var t={pf:encodeURIComponent(e)};r.extend(this.param,t)}},o.prototype.getJSONP=function(o,a,n){if(1===this.searchReady&&this.token&&""!==this.token){this.searchReady=0;var s={tk:this.token,v:this.version};r.extend(this.param,s);var i=this;e.get({url:o,parameters:i.param,success:function(e){i.searchReady=1,t.initData(e),1===n?a(t.returnResult()):2===n?a(t.returnInfo()):3===n?a(t.returnOsResult()):4===n&&a(t.returnFacet())},complete:function(){i.searchReady=1}})}},o.prototype.getResult=function(e,t){if(arguments[0]&&""!==arguments[0]&&arguments[1]&&""!==arguments[1]){if(arguments[2]&&this.numReg.test(arguments[2])&&arguments[2]>0){var o={p:arguments[2]-1};r.extend(this.param,o)}else this.param.hasOwnProperty("p")&&delete this.param.p;var a;2===this.cse?1===this.queryReady&&(a=this.searchUrl+"?s="+this.sid+"&os=1&q="+encodeURIComponent(e),this.getJSONP(a,t,3)):(a=this.searchUrl+"?s="+this.sid+"&q="+encodeURIComponent(e),this.getJSONP(a,t,1))}},o.prototype.getSearchInfo=function(e,o){if(arguments[0]&&""!==arguments[0]&&arguments[1]&&""!==arguments[1])if(r.count(t.returnInfo())>0)o(t.returnInfo());else{var a;2===this.cse?1===this.queryReady&&(a=this.searchUrl+"?s="+this.sid+"&os=1&q="+encodeURIComponent(e),this.getJSONP(a,o,2)):(a=this.searchUrl+"?s="+this.sid+"&q="+encodeURIComponent(e),this.getJSONP(a,o,2))}},o.prototype.getFacet=function(e,o){if(arguments[0]&&""!==arguments[0]&&arguments[1]&&""!==arguments[1]&&2===this.cse&&1===this.facetReady)if(r.count(t.returnFacet())>0)o(t.returnFacet());else{var a;2===this.cse&&1===this.queryReady&&(a=this.searchUrl+"?s="+this.sid+"&os=1&q="+encodeURIComponent(e),this.getJSONP(a,o,4))}},o.prototype.getSug=function(r,o){if(arguments[0]&&""!==arguments[0]&&arguments[1]&&""!==arguments[1]){var a="normal";if(arguments[2]&&this.numReg.test(arguments[2]))switch(arguments[2]){case 0:a="normal";break;case 1:a="movie";break;case 2:a="music";break;case 3:a="novel";break;case 4:a="health";break;case 5:a="game";break;case 6:a="soft";break;default:a="normal"}var n=this.sugUrl+"?sid="+this.sid+"&wd="+encodeURIComponent(r)+"&ch="+encodeURIComponent(a);if(1===this.sugReady&&this.token&&""!==this.token){this.sugReady=0;var s=this;e.get({url:n,success:function(e){t.initSugData(e),o(t.returnSug())},complete:function(){s.sugReady=1}})}}},o.prototype.getError=function(e){arguments[0]&&""!==arguments[0]&&e(t.returnErr())},o}();BCse.Search=o}();csdn.afterBaiduSearchInit();
    0
    

    可以看到报文中的f36 2890 b50 ed7 0 这几个16进制数字, 代表的就是chunked的长度, 格式上是16进制长度+换行符\n, 0表示结尾,这样浏览器或者其他终端可以接受一块解析一块


    http://gsp0.baidu.com/yrwHcjSl0MgCo2Kml5_Y_D3/api/customsearch/apiaccept?sid=10742016945123576423&v=2.0&callback=csdn.afterBaiduSearchInit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值