File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ local http = require (' http' )
2
+ local url = require (' url' )
3
+
4
+ responses = {}
5
+
6
+ http .createServer (function (req , res )
7
+ req .uri = url .parse (req .url )
8
+ local msize = 1024
9
+ local path = req .uri .pathname
10
+ if string.len (path ) > 1 then
11
+ msize = tonumber (string.sub (path , 2 ))
12
+ end
13
+
14
+ if not responses [msize ] then
15
+ responses [msize ] = string.rep (" X" , msize )
16
+ end
17
+
18
+ res :setHeader (" Content-Type" , " text/plain" )
19
+ res :setHeader (" Content-Length" , msize )
20
+ res :finish (responses [msize ])
21
+ end ):listen (25000 , ' 127.0.0.1' )
22
+
23
+ print (' Server running at http://127.0.0.1:25000/' )
Original file line number Diff line number Diff line change
1
+ local uv = require (' uv' )
2
+
3
+ -- Create listener socket
4
+ local server = uv .new_tcp ()
5
+ server :bind (' 127.0.0.1' , 25000 )
6
+
7
+ server :listen (128 , function (err )
8
+ -- Create socket handle for client
9
+ local client = uv .new_tcp ()
10
+
11
+ -- Accept incoming connection
12
+ server :accept (client )
13
+
14
+ -- Relay data back to client
15
+ client :read_start (function (err , data )
16
+ if err then
17
+ client :close ()
18
+ return
19
+ end
20
+
21
+ if data then
22
+ client :write (data )
23
+ else
24
+ client :close ()
25
+ end
26
+ end )
27
+ end )
You can’t perform that action at this time.
0 commit comments