samples/want/client.lua

changeset 0
f7d2d78eb424
equal deleted inserted replaced
-1:000000000000 0:f7d2d78eb424
1 --
2 -- Test the conn:want() function
3 --
4 -- Public domain
5 --
6 require("socket")
7 require("ssl")
8
9 local params = {
10 mode = "client",
11 protocol = "sslv3",
12 key = "../certs/clientAkey.pem",
13 certificate = "../certs/clientA.pem",
14 cafile = "../certs/rootA.pem",
15 verify = {"peer", "fail_if_no_peer_cert"},
16 options = {"all", "no_sslv2"},
17 }
18
19 -- Wait until socket is ready (for reading or writing)
20 local function wait(peer)
21 -- What event blocked us?
22 local err
23 if peer.want then -- Is it an SSL connection?
24 err = peer:want()
25 print("Want? ", err)
26 else
27 -- No, it's a normal TCP connection...
28 err = "timeout"
29 end
30
31 if err == "read" or err == "timeout" then
32 socket.select({peer}, nil)
33 elseif err == "write" then
34 socket.select(nil, {peer})
35 else
36 peer:close()
37 os.exit(1)
38 end
39 end
40
41 -- Start the TCP connection
42 local peer = socket.tcp()
43 assert( peer:connect("127.0.0.1", 8888) )
44
45 -- [[ SSL wrapper
46 peer = assert( ssl.wrap(peer, params) )
47 peer:settimeout(0.3)
48 local succ = peer:dohandshake()
49 while not succ do
50 wait(peer)
51 succ = peer:dohandshake()
52 end
53 print("** Handshake done")
54 --]]
55
56 -- If the section above is commented, the timeout is not set.
57 -- We set it again for safetiness.
58 peer:settimeout(0.3)
59
60 -- Try to receive a line
61 local str = peer:receive("*l")
62 while not str do
63 wait(peer)
64 str = peer:receive("*l")
65 end
66 peer:close()

mercurial