Show
Ignore:
Timestamp:
03/09/09 16:02:34 (4 years ago)
Author:
Cyrus
Message:

Improved httpclient

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/libs/httpclient/luasrc/httpclient.lua

    r4286 r4313  
    2323 
    2424local type, pairs, ipairs, tonumber = type, pairs, ipairs, tonumber 
     25local unpack = unpack 
    2526 
    2627module "luci.httpclient" 
     
    161162    end 
    162163     
     164    if type(options.body) == "function" then 
     165        options.method = options.method or "POST" 
     166    end 
     167 
    163168    -- Assemble message 
    164169    local message = {(options.method or "GET") .. " " .. path .. " " .. protocol} 
     
    195200    if type(options.body) == "string" then 
    196201        sock:sendall(options.body) 
     202    elseif type(options.body) == "function" then 
     203        local res = {options.body(sock)} 
     204        if not res[1] then 
     205            sock:close() 
     206            return unpack(res) 
     207        end 
    197208    end 
    198209     
     
    202213     
    203214    if not line then 
     215        sock:close() 
    204216        return nil, code, error 
    205217    end 
    206218     
    207     local protocol, status, msg = line:match("^(HTTP/[0-9.]+) ([0-9]+) (.*)") 
     219    local protocol, status, msg = line:match("^([%w./]+) ([0-9]+) (.*)") 
    208220     
    209221    if not protocol then 
     222        sock:close() 
    210223        return nil, -3, "invalid response magic: " .. line 
    211224    end 
     
    229242     
    230243    if not line then 
     244        sock:close() 
    231245        return nil, -4, "protocol error" 
    232246    end