• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lunarmodules / copas / 30209458911

26 Jul 2026 04:01PM UTC coverage: 85.229% (+0.06%) from 85.17%
30209458911

push

github

web-flow
fix(future): remove 9999-waiter cap on future completion (#204)

1 of 1 new or added line in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

1431 of 1679 relevant lines covered (85.23%)

56237.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.79
/src/copas/http.lua
1
-----------------------------------------------------------------------------
2
-- Full copy of the LuaSocket code, modified to include
3
-- https and http/https redirects, and Copas async enabled.
4
-----------------------------------------------------------------------------
5
-- HTTP/1.1 client support for the Lua language.
6
-- LuaSocket toolkit.
7
-- Author: Diego Nehab
8
-----------------------------------------------------------------------------
9

10
-----------------------------------------------------------------------------
11
-- Declare module and import dependencies
12
-------------------------------------------------------------------------------
13
local socket = require("socket")
30✔
14
local url = require("socket.url")
30✔
15
local ltn12 = require("ltn12")
30✔
16
local mime = require("mime")
30✔
17
local string = require("string")
30✔
18
local headers = require("socket.headers")
30✔
19
local base = _G
30✔
20
local table = require("table")
30✔
21
local copas = require("copas")
30✔
22
copas.http = {}
30✔
23
local _M = copas.http
30✔
24

25
-----------------------------------------------------------------------------
26
-- Program constants
27
-----------------------------------------------------------------------------
28
-- connection timeout in seconds
29
_M.TIMEOUT = 60
30✔
30
-- default port for document retrieval
31
_M.PORT = 80
30✔
32
-- user agent field sent in request
33
_M.USERAGENT = socket._VERSION
30✔
34

35
-- Default settings for SSL
36
_M.SSLPORT = 443
30✔
37
_M.SSLPROTOCOL = "tlsv1_2"
30✔
38
_M.SSLOPTIONS  = "all"
30✔
39
_M.SSLVERIFY   = "none"
30✔
40
_M.SSLSNISTRICT = false
30✔
41

42

43
-----------------------------------------------------------------------------
44
-- Reads MIME headers from a connection, unfolding where needed
45
-----------------------------------------------------------------------------
46
local function receiveheaders(sock, headers)
47
    local line, name, value, err
48
    headers = headers or {}
144✔
49
    -- get first line
50
    line, err = sock:receive()
168✔
51
    if err then return nil, err end
144✔
52
    -- headers go until a blank line is found
53
    while line ~= "" do
1,500✔
54
        -- get field-name and value
55
        name, value = socket.skip(2, string.find(line, "^(.-):%s*(.*)"))
1,362✔
56
        if not (name and value) then return nil, "malformed reponse headers" end
1,362✔
57
        name = string.lower(name)
1,589✔
58
        -- get next line (value might be folded)
59
        line, err  = sock:receive()
1,589✔
60
        if err then return nil, err end
1,362✔
61
        -- unfold any folded values
62
        while string.find(line, "^%s") do
1,356✔
63
            value = value .. line
×
64
            line, err = sock:receive()
×
65
            if err then return nil, err end
×
66
        end
67
        -- save pair in table
68
        if headers[name] then headers[name] = headers[name] .. ", " .. value
1,356✔
69
        else headers[name] = value end
1,272✔
70
    end
71
    return headers
138✔
72
end
73

74
-----------------------------------------------------------------------------
75
-- Extra sources and sinks
76
-----------------------------------------------------------------------------
77
socket.sourcet["http-chunked"] = function(sock, headers)
30✔
78
    return base.setmetatable({
66✔
79
        getfd = function() return sock:getfd() end,
36✔
80
        dirty = function() return sock:dirty() end
36✔
81
    }, {
36✔
82
        __call = function()
83
            -- get chunk size, skip extention
84
            local line, err = sock:receive()
194✔
85
            if err then return nil, err end
194✔
86
            local size = base.tonumber(string.gsub(line, ";.*", ""), 16)
194✔
87
            if not size then return nil, "invalid chunk size" end
194✔
88
            -- was it the last chunk?
89
            if size > 0 then
194✔
90
                -- if not, get chunk and skip terminating CRLF
91
                local chunk, err = sock:receive(size)
158✔
92
                if chunk then sock:receive() end
158✔
93
                return chunk, err
158✔
94
            else
95
                -- if it was, read trailers into headers table
96
                headers, err = receiveheaders(sock, headers)
42✔
97
                if not headers then return nil, err end
36✔
98
            end
99
        end
100
    })
24✔
101
end
102

103
socket.sinkt["http-chunked"] = function(sock)
30✔
104
    return base.setmetatable({
×
105
        getfd = function() return sock:getfd() end,
106
        dirty = function() return sock:dirty() end
×
107
    }, {
×
108
        __call = function(self, chunk, err)
109
            if not chunk then return sock:send("0\r\n\r\n") end
×
110
            local size = string.format("%X\r\n", string.len(chunk))
×
111
            return sock:send(size ..  chunk .. "\r\n")
×
112
        end
113
    })
114
end
115

116
-----------------------------------------------------------------------------
117
-- Low level HTTP API
118
-----------------------------------------------------------------------------
119
local metat = { __index = {} }
30✔
120

121
function _M.open(reqt)
30✔
122
    -- create socket with user connect function
123
    local c = socket.try(reqt:create())   -- method call, passing reqt table as self!
160✔
124
    local h = base.setmetatable({ c = c }, metat)
120✔
125
    -- create finalized try
126
    h.try = socket.newtry(function() h:close() end)
168✔
127
    -- set timeout before connecting
128
    local to = reqt.timeout or _M.TIMEOUT
120✔
129
    if type(to) == "table" then
120✔
130
      h.try(c:settimeouts(
×
131
        to.connect or _M.TIMEOUT,
×
132
        to.send or _M.TIMEOUT,
×
133
        to.receive or _M.TIMEOUT))
×
134
    else
135
      h.try(c:settimeout(to))
140✔
136
    end
137
    h.try(c:connect(reqt.host, reqt.port or _M.PORT))
140✔
138
    -- here everything worked
139
    return h
120✔
140
end
141

142
function metat.__index:sendrequestline(method, uri)
60✔
143
    local reqline = string.format("%s %s HTTP/1.1\r\n", method or "GET", uri)
120✔
144
    return self.try(self.c:send(reqline))
140✔
145
end
146

147
function metat.__index:sendheaders(tosend)
60✔
148
    local canonic = headers.canonic
120✔
149
    local h = "\r\n"
120✔
150
    for f, v in base.pairs(tosend) do
810✔
151
        h = (canonic[f] or f) .. ": " .. v .. "\r\n" .. h
690✔
152
    end
153
    self.try(self.c:send(h))
140✔
154
    return 1
120✔
155
end
156

157
function metat.__index:sendbody(headers, source, step)
60✔
158
    source = source or ltn12.source.empty()
30✔
159
    step = step or ltn12.pump.step
30✔
160
    -- if we don't know the size in advance, send chunked and hope for the best
161
    local mode = "http-chunked"
30✔
162
    if headers["content-length"] then mode = "keep-open" end
30✔
163
    return self.try(ltn12.pump.all(source, socket.sink(mode, self.c), step))
40✔
164
end
165

166
function metat.__index:receivestatusline()
60✔
167
    local status = self.try(self.c:receive(5))
140✔
168
    -- identify HTTP/0.9 responses, which do not contain a status line
169
    -- this is just a heuristic, but is what the RFC recommends
170
    if status ~= "HTTP/" then return nil, status end
108✔
171
    -- otherwise proceed reading a status line
172
    status = self.try(self.c:receive("*l", status))
144✔
173
    local code = socket.skip(2, string.find(status, "HTTP/%d*%.%d* (%d%d%d)"))
108✔
174
    return self.try(base.tonumber(code), status)
108✔
175
end
176

177
function metat.__index:receiveheaders()
60✔
178
    return self.try(receiveheaders(self.c))
126✔
179
end
180

181
function metat.__index:receivebody(headers, sink, step)
60✔
182
    sink = sink or ltn12.sink.null()
48✔
183
    step = step or ltn12.pump.step
48✔
184
    local length = base.tonumber(headers["content-length"])
48✔
185
    local t = headers["transfer-encoding"] -- shortcut
48✔
186
    local mode = "default" -- connection close
48✔
187
    if t and t ~= "identity" then mode = "http-chunked"
48✔
188
    elseif base.tonumber(headers["content-length"]) then mode = "by-length" end
12✔
189
    return self.try(ltn12.pump.all(socket.source(mode, self.c, length),
96✔
190
        sink, step))
56✔
191
end
192

193
function metat.__index:receive09body(status, sink, step)
60✔
194
    local source = ltn12.source.rewind(socket.source("until-closed", self.c))
×
195
    source(status)
×
196
    return self.try(ltn12.pump.all(source, sink, step))
×
197
end
198

199
function metat.__index:close()
60✔
200
    return self.c:close()
120✔
201
end
202

203
-----------------------------------------------------------------------------
204
-- High level HTTP API
205
-----------------------------------------------------------------------------
206
local function adjusturi(reqt)
207
    local u = reqt
138✔
208
    -- if there is a proxy, we need the full url. otherwise, just a part.
209
    if not reqt.proxy and not _M.PROXY then
138✔
210
        u = {
138✔
211
           path = socket.try(reqt.path, "invalid path 'nil'"),
161✔
212
           params = reqt.params,
138✔
213
           query = reqt.query,
138✔
214
           fragment = reqt.fragment
138✔
215
        }
138✔
216
    end
217
    return url.build(u)
138✔
218
end
219

220
local function adjustproxy(reqt)
221
    local proxy = reqt.proxy or _M.PROXY
138✔
222
    if proxy then
138✔
223
        proxy = url.parse(proxy)
×
224
        return proxy.host, proxy.port or 3128
×
225
    else
226
        return reqt.host, reqt.port
138✔
227
    end
228
end
229

230
local function adjustheaders(reqt)
231
    -- default headers
232
    local host = string.gsub(reqt.authority, "^.-@", "")
138✔
233
    local lower = {
138✔
234
        ["user-agent"] = _M.USERAGENT,
138✔
235
        ["host"] = host,
138✔
236
        ["connection"] = "close, TE",
115✔
237
        ["te"] = "trailers"
115✔
238
    }
239
    -- if we have authentication information, pass it along
240
    if reqt.user and reqt.password then
138✔
241
        lower["authorization"] =
×
242
            "Basic " ..  (mime.b64(reqt.user .. ":" .. reqt.password))
×
243
    end
244
    -- override with user headers
245
    for i,v in base.pairs(reqt.headers or lower) do
780✔
246
        lower[string.lower(i)] = v
749✔
247
    end
248
    return lower
138✔
249
end
250

251
-- default url parts
252
local default = {
30✔
253
    host = "",
25✔
254
    port = _M.PORT,
30✔
255
    path ="/",
25✔
256
    scheme = "http"
25✔
257
}
258

259
local function adjustrequest(reqt)
260
    -- parse url if provided
261
    local nreqt = reqt.url and url.parse(reqt.url, default) or {}
175✔
262
    -- explicit components override url
263
    for i,v in base.pairs(reqt) do nreqt[i] = v end
858✔
264
    socket.try(base.type(nreqt.scheme) == "string",
300✔
265
        "invalid scheme '" .. base.tostring(nreqt.scheme) .. "'")
150✔
266
    nreqt.scheme = string.lower(nreqt.scheme)
168✔
267
    socket.try(nreqt.scheme == "http" or nreqt.scheme == "https",
288✔
268
        "unsupported scheme '" .. nreqt.scheme .. "'")
144✔
269
    if nreqt.port == "" then nreqt.port = 80 end
138✔
270
    socket.try(nreqt.host and nreqt.host ~= "",
276✔
271
        "invalid host '" .. base.tostring(nreqt.host) .. "'")
138✔
272
    -- compute uri if user hasn't overriden
273
    nreqt.uri = reqt.uri or adjusturi(nreqt)
161✔
274
    -- ajust host and port if there is a proxy
275
    nreqt.host, nreqt.port = adjustproxy(nreqt)
161✔
276
    -- adjust headers in request
277
    nreqt.headers = adjustheaders(nreqt)
161✔
278
    return nreqt
138✔
279
end
280

281
local function shouldredirect(reqt, code, headers)
282
    return headers.location and
102✔
283
           string.gsub(headers.location, "%s", "") ~= "" and
54✔
284
           (reqt.redirect ~= false) and
54✔
285
           (code == 301 or code == 302 or code == 303 or code == 307) and
54✔
286
           (not reqt.method or reqt.method == "GET" or reqt.method == "HEAD")
54✔
287
           and (not reqt.nredirects or reqt.nredirects < 5)
102✔
288
end
289

290
local function shouldreceivebody(reqt, code)
291
    if reqt.method == "HEAD" then return nil end
48✔
292
    if code == 204 or code == 304 then return nil end
48✔
293
    if code >= 100 and code < 200 then return nil end
48✔
294
    return 1
48✔
295
end
296

297
-- forward declarations
298
local trequest, tredirect
299

UNCOV
300
--[[local]] function tredirect(reqt, location)
25✔
301
    local result, code, headers, status = trequest {
108✔
302
        -- the RFC says the redirect URL has to be absolute, but some
303
        -- servers do not respect that
304
        url = url.absolute(reqt.url, location),
63✔
305
        source = reqt.source,
54✔
306
        sink = reqt.sink,
54✔
307
        headers = reqt.headers,
54✔
308
        proxy = reqt.proxy,
54✔
309
        nredirects = (reqt.nredirects or 0) + 1,
54✔
310
        create = reqt.create,
54✔
311
        timeout = reqt.timeout,
54✔
312
    }
313
    -- pass location header back as a hint we redirected
314
    headers = headers or {}
48✔
315
    headers.location = headers.location or location
48✔
316
    return result, code, headers, status
48✔
317
end
318

UNCOV
319
--[[local]] function trequest(reqt)
25✔
320
    -- we loop until we get what we want, or
321
    -- until we are sure there is no way to get it
322
    local nreqt = adjustrequest(reqt)
150✔
323
    local h = _M.open(nreqt)
138✔
324
    -- send request line and headers
325
    h:sendrequestline(nreqt.method, nreqt.uri)
120✔
326
    h:sendheaders(nreqt.headers)
120✔
327
    -- if there is a body, send it
328
    if nreqt.source then
120✔
329
        h:sendbody(nreqt.headers, nreqt.source, nreqt.step)
30✔
330
    end
331
    local code, status = h:receivestatusline()
120✔
332
    -- if it is an HTTP/0.9 server, simply get the body and we are done
333
    if not code then
108✔
334
        h:receive09body(status, nreqt.sink, nreqt.step)
×
335
        return 1, 200
×
336
    end
337
    local headers
338
    -- ignore any 100-continue messages
339
    while code == 100 do
108✔
340
        h:receiveheaders()
×
341
        code, status = h:receivestatusline()
×
342
    end
343
    headers = h:receiveheaders()
125✔
344
    -- at this point we should have a honest reply from the server
345
    -- we can't redirect if we already used the source, so we report the error
346
    if shouldredirect(nreqt, code, headers) and not nreqt.source then
119✔
347
        h:close()
54✔
348
        return tredirect(reqt, headers.location)
54✔
349
    end
350
    -- here we are finally done
351
    if shouldreceivebody(nreqt, code) then
56✔
352
        h:receivebody(headers, nreqt.sink, nreqt.step)
48✔
353
    end
354
    h:close()
42✔
355
    return 1, code, headers, status
42✔
356
end
357

358
-- Return a function which creates a tcp socket that will
359
-- include the optional SSL/TLS connection, and unsafe redirect checks
360
function _M.getcreatefunc(params)
30✔
361
   params = params or {}
84✔
362
   local ssl_params = params.sslparams or {}
84✔
363
   ssl_params.wrap = ssl_params.wrap or {
84✔
364
      -- backward compatibility
365
      protocol = params.protocol,
84✔
366
      options = params.options,
84✔
367
      verify = params.verify,
84✔
368
   }
84✔
369
   ssl_params.sni = ssl_params.sni or {
84✔
370
      strict = _M.SSLSNISTRICT
84✔
371
   }
84✔
372

373
   -- Default settings
374
   ssl_params.wrap.protocol = ssl_params.wrap.protocol or _M.SSLPROTOCOL
84✔
375
   ssl_params.wrap.options = ssl_params.wrap.options or _M.SSLOPTIONS
84✔
376
   if ssl_params.wrap.verify == nil then
84✔
377
      ssl_params.wrap.verify = _M.SSLVERIFY
84✔
378
   end
379
   ssl_params.wrap.mode = "client"   -- Force client mode
84✔
380

381
   if not ssl_params.sni.names then
84✔
382
      -- names haven't been set, and hence will be set below. Since this alters
383
      -- the table, we must make a copy. Otherwise the altered table might be
384
      -- reused if a redirect is encountered.
385
      local old_params = ssl_params
84✔
386
      ssl_params = {}
84✔
387
      for k,v in pairs(old_params) do
252✔
388
        ssl_params[k] = v
168✔
389
      end
390
      ssl_params.sni = { strict = old_params.sni.strict }
84✔
391
   end
392

393
   -- upvalue to track https -> http redirection
394
   local washttps = false
84✔
395

396
   -- 'create' function for LuaSocket
397
   return function (reqt)
398
      local u = url.parse(reqt.url)
138✔
399
      if (reqt.scheme or u.scheme) == "https" then
138✔
400
        -- set SNI name to host if not given
401
        ssl_params.sni.names = ssl_params.sni.names or u.host
66✔
402
        -- https, provide an ssl wrapped socket
403
        local conn = copas.wrap(socket.tcp(), ssl_params)
68✔
404
        -- insert https default port, overriding http port inserted by LuaSocket
405
        if not u.port then
66✔
406
           u.port = _M.SSLPORT
66✔
407
           reqt.url = url.build(u)
77✔
408
           reqt.port = _M.SSLPORT
66✔
409
        end
410
        washttps = true
66✔
411
        return conn
66✔
412
      else
413
        -- regular http, needs just a socket...
414
        if washttps and params.redirect ~= "all" then
72✔
415
          socket.try(nil, "Unallowed insecure redirect https to http")
6✔
416
        end
417
        return copas.wrap(socket.tcp())
66✔
418
      end
419
   end
420
end
421

422
-- parses a shorthand form into the advanced table form.
423
-- adds field `target` to the table. This will hold the return values.
424
_M.parseRequest = function(u, b)
425
    local reqt = {
12✔
426
        url = u,
12✔
427
        target = {},
12✔
428
    }
429
    reqt.sink = ltn12.sink.table(reqt.target)
14✔
430
    if b then
12✔
431
        reqt.source = ltn12.source.string(b)
×
432
        reqt.headers = {
×
433
            ["content-length"] = string.len(b),
434
            ["content-type"] = "application/x-www-form-urlencoded"
×
435
        }
436
        reqt.method = "POST"
×
437
    end
438
    return reqt
12✔
439
end
440

441
_M.request = socket.protect(function(reqt, body)
60✔
442
    if base.type(reqt) == "string" then
108✔
443
        reqt = _M.parseRequest(reqt, body)
14✔
444
        local ok, code, headers, status = _M.request(reqt)
12✔
445

446
        if ok then
12✔
447
            return table.concat(reqt.target), code, headers, status
12✔
448
        else
449
            return nil, code
×
450
        end
451
    else
452
        -- strict check on timeout table to prevent typo's from going unnoticed
453
        if type(reqt.timeout) == "table" then
96✔
454
          local allowed = { connect = true, send = true, receive = true }
×
455
          for k in pairs(reqt.timeout) do
×
456
            assert(allowed[k], "'"..tostring(k).."' is not a valid timeout option. Valid: 'connect', 'send', 'receive'")
×
457
          end
458
        end
459
        reqt.create = reqt.create or _M.getcreatefunc(reqt)
108✔
460
        return trequest(reqt)
96✔
461
    end
462
end)
463

464
return _M
30✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc