summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/util-soc-imp-http.lua
blob: ff858b313daf68f66949d4ba8e34c341f0624beb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
-- original file : http.lua
-- for more into : see util-soc.lua

local tostring, tonumber, setmetatable, next, type = tostring, tonumber, setmetatable, next, type
local find, lower, format, gsub, match  = string.find, string.lower, string.format, string.gsub, string.match
local concat = table.concat

local socket  = socket         or require("socket")
local url     = socket.url     or require("socket.url")
local ltn12   = ltn12          or require("ltn12")
local mime    = mime           or require("mime")
local headers = socket.headers or require("socket.headers")

local normalizeheaders = headers.normalize

local parseurl         = url.parse
local buildurl         = url.build
local absoluteurl      = url.absolute
local unescapeurl      = url.unescape

local skipsocket       = socket.skip
local sinksocket       = socket.sink
local sourcesocket     = socket.source
local trysocket        = socket.try
local tcpsocket        = socket.tcp
local newtrysocket     = socket.newtry
local protectsocket    = socket.protect

local emptysource      = ltn12.source.empty
local stringsource     = ltn12.source.string
local rewindsource     = ltn12.source.rewind
local pumpstep         = ltn12.pump.step
local pumpall          = ltn12.pump.all
local sinknull         = ltn12.sink.null
local sinktable        = ltn12.sink.table

local lowerheaders     = headers.lower

local mimeb64          = mime.b64

-- todo: localize ltn12

local http  = {
    TIMEOUT   = 60,               -- connection timeout in seconds
    USERAGENT = socket._VERSION,  -- user agent field sent in request
}

socket.http = http

local PORT    = 80
local SCHEMES = {
    http = true,
}

-- Reads MIME headers from a connection, unfolding where needed

local function receiveheaders(sock, headers)
    if not headers then
        headers = { }
    end
    -- get first line
    local line, err = sock:receive("*l") -- this seems to be wrong!
    if err then
        return nil, err
    end
    -- headers go until a blank line is found
    while line ~= "" do
        -- get field-name and value
        local name, value = skipsocket(2, find(line, "^(.-):%s*(.*)"))
        if not (name and value) then
            return nil, "malformed reponse headers"
        end
        name = lower(name)
        -- get next line (value might be folded)
        line, err  = sock:receive("*l")
        if err then
            return nil, err
        end
        -- unfold any folded values
        while find(line, "^%s") do
            value = value .. line
            line  = sock:receive("*l")
            if err then
                return nil, err
            end
        end
        -- save pair in table
        local found = headers[name]
        if found then
            value = found .. ", " .. value
        end
        headers[name] = value
    end
    return headers
end

-- Extra sources and sinks

socket.sourcet["http-chunked"] = function(sock, headers)
    return setmetatable (
        {
            getfd = function() return sock:getfd() end,
            dirty = function() return sock:dirty() end,
        }, {
            __call = function()
                local line, err = sock:receive("*l")
                if err then
                    return nil, err
                end
                local size = tonumber(gsub(line, ";.*", ""), 16)
                if not size then
                    return nil, "invalid chunk size"
                end
                if size > 0 then
                    local chunk, err, part = sock:receive(size)
                    if chunk then
                        sock:receive("*a")
                    end
                    return chunk, err
                else
                    headers, err = receiveheaders(sock, headers)
                    if not headers then
                        return nil, err
                    end
                end
            end
        }
    )
end

socket.sinkt["http-chunked"] = function(sock)
    return setmetatable(
        {
            getfd = function() return sock:getfd() end,
            dirty = function() return sock:dirty() end,
        },
        {
            __call = function(self, chunk, err)
                if not chunk then
                    chunk = ""
                end
                return sock:send(format("%X\r\n%s\r\n",#chunk,chunk))
            end
    })
end

-- Low level HTTP API

local methods = { }
local mt      = { __index = methods }

local function openhttp(host, port, create)
    local c = trysocket((create or tcpsocket)())
    local h = setmetatable({ c = c }, mt)
    local try = newtrysocket(function() h:close() end)
    h.try = try
    try(c:settimeout(http.TIMEOUT))
    try(c:connect(host, port or PORT))
    return h
end

http.open = openhttp

function methods.sendrequestline(self, method, uri)
    local requestline = format("%s %s HTTP/1.1\r\n", method or "GET", uri)
    return self.try(self.c:send(requestline))
end

function methods.sendheaders(self,headers)
    self.try(self.c:send(normalizeheaders(headers)))
    return 1
end

function methods.sendbody(self, headers, source, step)
    if not source then
        source = emptysource()
    end
    if not step then
        step = pumpstep
    end
    local mode = "http-chunked"
    if headers["content-length"] then
        mode = "keep-open"
    end
    return self.try(pumpall(source, sinksocket(mode, self.c), step))
end

function methods.receivestatusline(self)
    local try    = self.try
    local status = try(self.c:receive(5))
    if status ~= "HTTP/" then
        return nil, status -- HTTP/0.9
    end
    status = try(self.c:receive("*l", status))
    local code = skipsocket(2, find(status, "HTTP/%d*%.%d* (%d%d%d)"))
    return try(tonumber(code), status)
end

function methods.receiveheaders(self)
    return self.try(receiveheaders(self.c))
end

function methods.receivebody(self, headers, sink, step)
    if not sink then
        sink = sinknull()
    end
    if not step then
        step = pumpstep
    end
    local length   = tonumber(headers["content-length"])
    local encoding = headers["transfer-encoding"] -- shortcut
    local mode     = "default" -- connection close
    if encoding and encoding ~= "identity" then
        mode = "http-chunked"
    elseif length then
        mode = "by-length"
    end
    --hh: so length can be nil
    return self.try(pumpall(sourcesocket(mode, self.c, length), sink, step))
end

function methods.receive09body(self, status, sink, step)
    local source = rewindsource(sourcesocket("until-closed", self.c))
    source(status)
    return self.try(pumpall(source, sink, step))
end

function methods.close(self)
    return self.c:close()
end

-- High level HTTP API

local function adjusturi(request)
    if not request.proxy and not http.PROXY then
        request = {
           path     = trysocket(request.path, "invalid path 'nil'"),
           params   = request.params,
           query    = request.query,
           fragment = request.fragment,
        }
    end
    return buildurl(request)
end

local function adjustheaders(request)
    local headers = {
        ["user-agent"] = http.USERAGENT,
        ["host"]       = gsub(request.authority, "^.-@", ""),
        ["connection"] = "close, TE",
        ["te"]         = "trailers"
    }
    local username = request.user
    local password = request.password
    if username and password then
        headers["authorization"] = "Basic " ..  (mimeb64(username .. ":" .. unescapeurl(password)))
    end
    local proxy = request.proxy or http.PROXY
    if proxy then
        proxy = parseurl(proxy)
        local username = proxy.user
        local password = proxy.password
        if username and password then
            headers["proxy-authorization"] = "Basic " ..  (mimeb64(username .. ":" .. password))
        end
    end
    local requestheaders = request.headers
    if requestheaders then
        headers = lowerheaders(headers,requestheaders)
    end
    return headers
end

-- default url parts

local default = {
    host   = "",
    port   = PORT,
    path   = "/",
    scheme = "http"
}

local function adjustrequest(originalrequest)
    local url     = originalrequest.url
    local request = url and parseurl(url,default) or { }
    for k, v in next, originalrequest do
        request[k] = v
    end
    local host = request.host
    local port = request.port
    local uri  = request.uri
    if not host or host == "" then
        trysocket(nil, "invalid host '" .. tostring(host) .. "'")
    end
    if port == "" then
        request.port = PORT
    end
    if not uri or uri == "" then
        request.uri = adjusturi(request)
    end
    request.headers = adjustheaders(request)
    local proxy = request.proxy or http.PROXY
    if proxy then
        proxy        = parseurl(proxy)
        request.host = proxy.host
        request.port = proxy.port or 3128
    end
    return request
end

local maxredericts   = 4
local validredirects = { [301] = true, [302] = true, [303] = true, [307] = true }
local validmethods   = { [false] = true, GET = true, HEAD = true }

local function shouldredirect(request, code, headers)
    local location = headers.location
    if not location then
        return false
    end
    location = gsub(location, "%s", "")
    if location == "" then
        return false
    end
    local scheme = match(location, "^([%w][%w%+%-%.]*)%:")
    if scheme and not SCHEMES[scheme] then
        return false
    end
    local method    = request.method
    local redirect  = request.redirect
    local redirects = request.nredirects or 0
    return redirect and validredirects[code] and validmethods[method] and redirects <= maxredericts
end

local function shouldreceivebody(request, code)
    if request.method == "HEAD" then
        return nil
    end
    if code == 204 or code == 304 then
        return nil
    end
    if code >= 100 and code < 200 then
        return nil
    end
    return 1
end

local tredirect, trequest, srequest

tredirect = function(request, location)
    local result, code, headers, status = trequest {
        url        = absoluteurl(request.url,location),
        source     = request.source,
        sink       = request.sink,
        headers    = request.headers,
        proxy      = request.proxy,
        nredirects = (request.nredirects or 0) + 1,
        create     = request.create,
    }
    if not headers then
        headers = { }
    end
    if not headers.location then
        headers.location = location
    end
    return result, code, headers, status
end

trequest = function(originalrequest)
    local request    = adjustrequest(originalrequest)
    local connection = openhttp(request.host, request.port, request.create)
    local headers    = request.headers
    connection:sendrequestline(request.method, request.uri)
    connection:sendheaders(headers)
    if request.source then
        connection:sendbody(headers, request.source, request.step)
    end
    local code, status = connection:receivestatusline()
    if not code then
        connection:receive09body(status, request.sink, request.step)
        connection:close()
        return 1, 200
    end
    while code == 100 do
        headers = connection:receiveheaders()
        code, status = connection:receivestatusline()
    end
    headers = connection:receiveheaders()
    if shouldredirect(request, code, headers) and not request.source then
        connection:close()
        return tredirect(originalrequest,headers.location)
    end
    if shouldreceivebody(request, code) then
        connection:receivebody(headers, request.sink, request.step)
    end
    connection:close()
    return 1, code, headers, status
end

-- turns an url and a body into a generic request

local function genericform(url, body)
    local buffer  = { }
    local request = {
        url    = url,
        sink   = sinktable(buffer),
        target = buffer,
    }
    if body then
        request.source  = stringsource(body)
        request.method  = "POST"
        request.headers = {
            ["content-length"] = #body,
            ["content-type"]   = "application/x-www-form-urlencoded"
        }
    end
    return request
end

http.genericform = genericform

srequest = function(url, body)
    local request = genericform(url, body)
    local _, code, headers, status = trequest(request)
    return concat(request.target), code, headers, status
end

http.request = protectsocket(function(request, body)
    if type(request) == "string" then
        return srequest(request, body)
    else
        return trequest(request)
    end
end)

package.loaded["socket.http"] = http

return http