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

lunarmodules / copas / 22974920245

11 Mar 2026 09:17PM UTC coverage: 84.693% (+0.06%) from 84.635%
22974920245

push

github

web-flow
fix(lock/semaphore): use proper coroutine.running with Lua 5.1 (#180)

With Lua 5.1 the modified version from coxpcall should be used, if
not there might be rare race-conditions.

7 of 9 new or added lines in 2 files covered. (77.78%)

6 existing lines in 3 files now uncovered.

1339 of 1581 relevant lines covered (84.69%)

78921.05 hits per line

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

87.55
/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")
28✔
14
local url = require("socket.url")
28✔
15
local ltn12 = require("ltn12")
28✔
16
local mime = require("mime")
28✔
17
local string = require("string")
28✔
18
local headers = require("socket.headers")
28✔
19
local base = _G
28✔
20
local table = require("table")
28✔
21
local copas = require("copas")
28✔
22
copas.http = {}
28✔
23
local _M = copas.http
28✔
24

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

35
-- Default settings for SSL
36
_M.SSLPORT = 443
28✔
37
_M.SSLPROTOCOL = "tlsv1_2"
28✔
38
_M.SSLOPTIONS  = "all"
28✔
39
_M.SSLVERIFY   = "none"
28✔
40
_M.SSLSNISTRICT = false
28✔
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 {}
168✔
49
    -- get first line
50
    line, err = sock:receive()
216✔
51
    if err then return nil, err end
168✔
52
    -- headers go until a blank line is found
53
    while line ~= "" do
1,778✔
54
        -- get field-name and value
55
        name, value = socket.skip(2, string.find(line, "^(.-):%s*(.*)"))
1,617✔
56
        if not (name and value) then return nil, "malformed reponse headers" end
1,617✔
57
        name = string.lower(name)
2,079✔
58
        -- get next line (value might be folded)
59
        line, err  = sock:receive()
2,079✔
60
        if err then return nil, err end
1,617✔
61
        -- unfold any folded values
62
        while string.find(line, "^%s") do
1,610✔
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,610✔
69
        else headers[name] = value end
1,505✔
70
    end
71
    return headers
161✔
72
end
73

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

103
socket.sinkt["http-chunked"] = function(sock)
28✔
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 = {} }
28✔
120

121
function _M.open(reqt)
28✔
122
    -- create socket with user connect function
123
    local c = socket.try(reqt:create())   -- method call, passing reqt table as self!
187✔
124
    local h = base.setmetatable({ c = c }, metat)
140✔
125
    -- create finalized try
126
    h.try = socket.newtry(function() h:close() end)
216✔
127
    -- set timeout before connecting
128
    local to = reqt.timeout or _M.TIMEOUT
140✔
129
    if type(to) == "table" then
140✔
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))
180✔
136
    end
137
    h.try(c:connect(reqt.host, reqt.port or _M.PORT))
180✔
138
    -- here everything worked
139
    return h
140✔
140
end
141

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

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

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

166
function metat.__index:receivestatusline()
56✔
167
    local status = self.try(self.c:receive(5))
180✔
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
126✔
171
    -- otherwise proceed reading a status line
172
    status = self.try(self.c:receive("*l", status))
198✔
173
    local code = socket.skip(2, string.find(status, "HTTP/%d*%.%d* (%d%d%d)"))
126✔
174
    return self.try(base.tonumber(code), status)
126✔
175
end
176

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

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

193
function metat.__index:receive09body(status, sink, step)
56✔
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()
56✔
200
    return self.c:close()
140✔
201
end
202

203
-----------------------------------------------------------------------------
204
-- High level HTTP API
205
-----------------------------------------------------------------------------
206
local function adjusturi(reqt)
207
    local u = reqt
147✔
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
147✔
210
        u = {
147✔
211
           path = socket.try(reqt.path, "invalid path 'nil'"),
189✔
212
           params = reqt.params,
147✔
213
           query = reqt.query,
147✔
214
           fragment = reqt.fragment
147✔
215
        }
147✔
216
    end
217
    return url.build(u)
147✔
218
end
219

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

230
local function adjustheaders(reqt)
231
    -- default headers
232
    local host = string.gsub(reqt.authority, "^.-@", "")
147✔
233
    local lower = {
147✔
234
        ["user-agent"] = _M.USERAGENT,
147✔
235
        ["host"] = host,
147✔
236
        ["connection"] = "close, TE",
105✔
UNCOV
237
        ["te"] = "trailers"
105✔
238
    }
239
    -- if we have authentication information, pass it along
240
    if reqt.user and reqt.password then
147✔
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
840✔
246
        lower[string.lower(i)] = v
891✔
247
    end
248
    return lower
147✔
249
end
250

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

259
local function adjustrequest(reqt)
260
    -- parse url if provided
261
    local nreqt = reqt.url and url.parse(reqt.url, default) or {}
189✔
262
    -- explicit components override url
263
    for i,v in base.pairs(reqt) do nreqt[i] = v end
847✔
264
    if nreqt.port == "" then nreqt.port = 80 end
147✔
265
    socket.try(nreqt.host and nreqt.host ~= "",
294✔
266
        "invalid host '" .. base.tostring(nreqt.host) .. "'")
147✔
267
    -- compute uri if user hasn't overriden
268
    nreqt.uri = reqt.uri or adjusturi(nreqt)
189✔
269
    -- ajust host and port if there is a proxy
270
    nreqt.host, nreqt.port = adjustproxy(nreqt)
189✔
271
    -- adjust headers in request
272
    nreqt.headers = adjustheaders(nreqt)
189✔
273
    return nreqt
147✔
274
end
275

276
local function shouldredirect(reqt, code, headers)
277
    return headers.location and
119✔
278
           string.gsub(headers.location, "%s", "") ~= "" and
63✔
279
           (reqt.redirect ~= false) and
63✔
280
           (code == 301 or code == 302 or code == 303 or code == 307) and
63✔
281
           (not reqt.method or reqt.method == "GET" or reqt.method == "HEAD")
63✔
282
           and (not reqt.nredirects or reqt.nredirects < 5)
119✔
283
end
284

285
local function shouldreceivebody(reqt, code)
286
    if reqt.method == "HEAD" then return nil end
56✔
287
    if code == 204 or code == 304 then return nil end
56✔
288
    if code >= 100 and code < 200 then return nil end
56✔
289
    return 1
56✔
290
end
291

292
-- forward declarations
293
local trequest, tredirect
294

295
--[[local]] function tredirect(reqt, location)
24✔
296
    local result, code, headers, status = trequest {
126✔
297
        -- the RFC says the redirect URL has to be absolute, but some
298
        -- servers do not respect that
299
        url = url.absolute(reqt.url, location),
81✔
300
        source = reqt.source,
63✔
301
        sink = reqt.sink,
63✔
302
        headers = reqt.headers,
63✔
303
        proxy = reqt.proxy,
63✔
304
        nredirects = (reqt.nredirects or 0) + 1,
63✔
305
        create = reqt.create,
63✔
306
        timeout = reqt.timeout,
63✔
307
    }
308
    -- pass location header back as a hint we redirected
309
    headers = headers or {}
56✔
310
    headers.location = headers.location or location
56✔
311
    return result, code, headers, status
56✔
312
end
313

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

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

368
   -- Default settings
369
   ssl_params.wrap.protocol = ssl_params.wrap.protocol or _M.SSLPROTOCOL
84✔
370
   ssl_params.wrap.options = ssl_params.wrap.options or _M.SSLOPTIONS
84✔
371
   if ssl_params.wrap.verify == nil then
84✔
372
      ssl_params.wrap.verify = _M.SSLVERIFY
84✔
373
   end
374
   ssl_params.wrap.mode = "client"   -- Force client mode
84✔
375

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

388
   -- upvalue to track https -> http redirection
389
   local washttps = false
84✔
390

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

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

436
_M.request = socket.protect(function(reqt, body)
56✔
437
    if base.type(reqt) == "string" then
98✔
438
        reqt = _M.parseRequest(reqt, body)
18✔
439
        local ok, code, headers, status = _M.request(reqt)
14✔
440

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

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

© 2026 Coveralls, Inc