Initialisation du projet wslsock
[wslsock.git] / wslsock.lua
1 -- Détermination de la taille d'un fichier
2 function wsls_fsize (name)
3   fso = io.open(name, "r")
4   s = fso:seek("end")
5   io.close(fso)
6   return s
7 end
8
9 -- Détermination de l'extension d'un fichier
10 function wsls_fext (name)
11   return string.match(name, "(%.%a+)$")
12 end
13
14 -- Détermination du fichier
15 function wsls_fname (tail)
16   return string.match(tail, "/?([%a%d%.%-]+)$")
17 end
18
19 -- GET simple
20 function wsls_get (base, tail)
21   http = require("socket.http")
22   ltn12 = require("ltn12")
23   file = wsls_fname(tail)
24   out = io.open(file, "w")
25   r, c, h = http.request {
26     url = "http://" .. base .. tail,
27     sink = ltn12.sink.file(out)
28   }
29   if c == 200 then
30     print ("Le fichier " .. file .. "(" .. 
31       wsls_fsize(file) .. " octets) est transféré !")
32   end
33 end
34
35
36 -- PUT simple
37 function wsls_put (base, tail)
38   http = require("socket.http")
39   ltn12 = require("ltn12")
40   file = wsls_fname(tail)
41   r, c, h = http.request {
42     method = "PUT",
43     url = "http://" .. base .. tail,
44     source = ltn12.source.file(io.open(file)),
45     headers = {
46       ["content-length"] = wsls_fsize(file)
47     }
48   }
49   if c == 200 then
50     print ("Le fichier " .. file .. "(" ..
51       wsls_fsize(file) .. " octets) est transféré !")
52   end
53 end
54
55

Licence Creative Commons Les fichiers de Syracuse sont mis à disposition (sauf mention contraire) selon les termes de la
Licence Creative Commons Attribution - Pas d’Utilisation Commerciale - Partage dans les Mêmes Conditions 4.0 International.