skynet源码分析(14)--skynet中http之internal

作者:shihuaping0918@163.com,转载请注明作者

skynet的http相关的代码中有一个叫internal.lua的文件,这个文件的功能是读取http头部,解析http头部。还有一个功能是读取chunk方式传输的消息体。

http协议有三部分,这三部分是这样组织的:
start-line \r\n
http-head \r\n
http-body

start-line就是HTTP 版本号那一行
http-head的格式是
key:value \r\n
key:value \r\n
这个大家可以直接在浏览器开发者工具里看到。

http-head和http-body之间是用\r\n分隔的。\r\n就是回车换行。

而http-body的组织形式就要看具体情况了。在传比较大的数据块的时候,有可能会使用chunked这种编码方式。它是在http头部,使用transfer-coding来指定的。下面是chunk的格式定义说明。

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)

第个chunk由chunk-size和chunk-data组成。chunk-size和chunk-data间用\r\n分隔。而比较坑的是,chunk部分可以带chunk-extension,这个东西格式和http头一样。

有了这些准备以后,下面的代码就不再难理解。

local table = table
local type = type

local M = {}

local LIMIT = 8192

--取chunk大小
--每个chunk后都带有\r\n
local function chunksize(readbytes, body)
    while true do
        local f,e = body:find("\r\n",1,true)
        if f then
            return tonumber(body:sub(1,f-1),16), body:sub(e+1)
        end
        if #body > 128 then --防止炸弹,这个数字有点小
            -- pervent the attacker send very long stream without \r\n
            return
        end
        body = body .. readbytes()
    end
end

--过滤\r\n
local function readcrln(readbytes, body)
    if #body >= 2 then
        if body:sub(1,2) ~= "\r\n" then
            return
        end
        return body:sub(3)
    else
        body = body .. readbytes(2-#body)
        if body ~= "\r\n" then
            return
        end
        return ""
    end
end

--取消息头
--参数readbytes是个函数
--lines用来存消息头
--header实际上是socket读到的数据块
function M.recvheader(readbytes, lines, header)
    if #header >= 2 then
        if header:find "^\r\n" then --如果是以\r\n开头,丢掉\r\n
            return header:sub(3)
        end
    end
    local result
    --以\r\n\r\n结尾
    local e = header:find("\r\n\r\n", 1, true)
    if e then
      --消息头取出来,带上\r\n\r\n
        result = header:sub(e+4)
    else --如果找不到\r\n\r\n,证明消息头没有读完
        while true do
            local bytes = readbytes() --继续读数据
            header = header .. bytes
            if #header > LIMIT then --如果消息头超大
                return
            end
            --从最新读取的数据里找\r\n\r\n
            e = header:find("\r\n\r\n", -#bytes-3, true)
            if e then
                result = header:sub(e+4) --取出head
                break
            end
            if header:find "^\r\n" then --如果是以\r\n开头,丢掉\r\n
                return header:sub(3)
            end
        end
    end
    --取消息头,消息头是一行一行的,以\r\n结尾
    for v in header:gmatch("(.-)\r\n") do
        if v == "" then
            break
        end
        table.insert(lines, v) --取一个消息头就放进lines表格中
    end
    return result
end

--解析消息头
--lines是key:value的字符串,key在http中称为field name
--header是个表格,用于设置key/value
--from是开始位置,从lines哪一个地方开始
function M.parseheader(lines, from, header)
    local name, value
    for i=from,#lines do
        local line = lines[i]
        if line:byte(1) == 9 then   -- tab, append last line
            if name == nil then
                return
            end
            header[name] = header[name] .. line:sub(2)
        else
          --把key:value解析出来
            name, value = line:match "^(.-):%s*(.*)"
            if name == nil or value == nil then
                return
            end
            --把key全转为小写,field_name是大小写不敏感的
            name = name:lower()
            if header[name] then
                local v = header[name]
                if type(v) == "table" then
                    table.insert(v, value)
                else
                    header[name] = { v , value } --把value赋进去
                end
            else
                header[name] = value
            end
        end
    end
    return header
end

--读取chunk消息体
--chunk size\r\n
--chunk data\r\n
function M.recvchunkedbody(readbytes, bodylimit, header, body)
    local result = ""
    local size = 0

    while true do
        local sz
        sz , body = chunksize(readbytes, body)
        if not sz then --chunk不合法
            return
        end
        if sz == 0 then --chunk合法,但是是空的
            break
        end
        size = size + sz --每个chunk长度累加
        if bodylimit and size > bodylimit then --chunk总长度超长
            return
        end
        if #body >= sz then --数据分割
            result = result .. body:sub(1,sz)
            body = body:sub(sz+1)
        else
            result = result .. body .. readbytes(sz - #body)
            body = ""
        end
        body = readcrln(readbytes, body) --移除\r\n
        if not body then
            return
        end
    end

  --chunk可以带entity头
    local tmpline = {}
    body = M.recvheader(readbytes, tmpline, body)
    if not body then
        return
    end

    header = M.parseheader(tmpline,1,header)

    return result, header
end

return M

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,527评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,314评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,535评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,006评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,961评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,220评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,664评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,351评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,481评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,397评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,443评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,123评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,713评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,801评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,010评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,494评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,075评论 2 341

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,566评论 18 139
  • 组织:中国互动出版网(http://www.china-pub.com/) RFC文档中文翻译计划(http://...
    Palomar阅读 1,555评论 0 6
  • 如果我剖开自己的心脏我会得到什么?几分干枯的梦想写了名字的玫瑰泡满甘果的啤酒还是透明的水晶?被阳光打在身上的日子我...
    0969fa688661阅读 142评论 0 1
  • 登录友盟官网 找到你的APP 2.选择错误列表 3.点击编辑选择你的APP发布版本就能看到你的崩溃列表:1-1 ...
    超_iOS阅读 353评论 0 0
  • 十年前的士兵突击3 在草原五班,还有2个难忘的细节,第一个是在军报记者面前,大家都为了留住班长,把修路的功劳推到班...
    布衣观天下阅读 467评论 0 1