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

lunarmodules / copas / 3874175729

pending completion
3874175729

push

github

Thijs Schreijer
feat(cli) add a runtime script to run code in a copas environment

1284 of 1497 relevant lines covered (85.77%)

14626.21 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

292
-- forward declarations
293
local trequest, tredirect
294

295
--[[local]] function tredirect(reqt, location)
4✔
296
    local result, code, headers, status = trequest {
12✔
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),
12✔
300
        source = reqt.source,
6✔
301
        sink = reqt.sink,
6✔
302
        headers = reqt.headers,
6✔
303
        proxy = reqt.proxy,
6✔
304
        nredirects = (reqt.nredirects or 0) + 1,
6✔
305
        create = reqt.create,
6✔
306
        timeout = reqt.timeout,
6✔
307
    }
308
    -- pass location header back as a hint we redirected
309
    headers = headers or {}
5✔
310
    headers.location = headers.location or location
5✔
311
    return result, code, headers, status
5✔
312
end
313

314
--[[local]] function trequest(reqt)
4✔
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)
18✔
318
    local h = _M.open(nreqt)
18✔
319
    -- send request line and headers
320
    h:sendrequestline(nreqt.method, nreqt.uri)
17✔
321
    h:sendheaders(nreqt.headers)
17✔
322
    -- if there is a body, send it
323
    if nreqt.source then
17✔
324
        h:sendbody(nreqt.headers, nreqt.source, nreqt.step)
5✔
325
    end
326
    local code, status = h:receivestatusline()
17✔
327
    -- if it is an HTTP/0.9 server, simply get the body and we are done
328
    if not code then
15✔
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
15✔
335
        h:receiveheaders()
×
336
        code, status = h:receivestatusline()
×
337
    end
338
    headers = h:receiveheaders()
29✔
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
28✔
342
        h:close()
6✔
343
        return tredirect(reqt, headers.location)
6✔
344
    end
345
    -- here we are finally done
346
    if shouldreceivebody(nreqt, code) then
16✔
347
        h:receivebody(headers, nreqt.sink, nreqt.step)
8✔
348
    end
349
    h:close()
7✔
350
    return 1, code, headers, status
7✔
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)
4✔
356
   params = params or {}
12✔
357
   local ssl_params = params.sslparams or {}
12✔
358
   ssl_params.wrap = ssl_params.wrap or {
12✔
359
      -- backward compatibility
360
      protocol = params.protocol,
12✔
361
      options = params.options,
12✔
362
      verify = params.verify,
12✔
363
   }
12✔
364
   ssl_params.sni = ssl_params.sni or {
12✔
365
      strict = _M.SSLSNISTRICT
12✔
366
   }
12✔
367

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

376
   if not ssl_params.sni.names then
12✔
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
12✔
381
      ssl_params = {}
12✔
382
      for k,v in pairs(old_params) do
36✔
383
        ssl_params[k] = v
24✔
384
      end
385
      ssl_params.sni = { strict = old_params.sni.strict }
12✔
386
   end
387

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

391
   -- 'create' function for LuaSocket
392
   return function (reqt)
393
      local u = url.parse(reqt.url)
18✔
394
      if (reqt.scheme or u.scheme) == "https" then
18✔
395
        -- set SNI name to host if not given
396
        ssl_params.sni.names = ssl_params.sni.names or u.host
7✔
397
        -- https, provide an ssl wrapped socket
398
        local conn = copas.wrap(socket.tcp(), ssl_params)
8✔
399
        -- insert https default port, overriding http port inserted by LuaSocket
400
        if not u.port then
7✔
401
           u.port = _M.SSLPORT
7✔
402
           reqt.url = url.build(u)
14✔
403
           reqt.port = _M.SSLPORT
7✔
404
        end
405
        washttps = true
7✔
406
        return conn
7✔
407
      else
408
        -- regular http, needs just a socket...
409
        if washttps and params.redirect ~= "all" then
11✔
410
          socket.try(nil, "Unallowed insecure redirect https to http")
1✔
411
        end
412
        return copas.wrap(socket.tcp())
10✔
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 = {
2✔
421
        url = u,
2✔
422
        target = {},
2✔
423
    }
424
    reqt.sink = ltn12.sink.table(reqt.target)
4✔
425
    if b then
2✔
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
2✔
434
end
435

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

441
        if ok then
2✔
442
            return table.concat(reqt.target), code, headers, status
2✔
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
12✔
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)
24✔
455
        return trequest(reqt)
12✔
456
    end
457
end)
458

459
return _M
4✔
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