-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.tty.lua
More file actions
176 lines (137 loc) · 4.2 KB
/
test.tty.lua
File metadata and controls
176 lines (137 loc) · 4.2 KB
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
local uv = require "uv"
local stdout = uv.tty(1)
print(stdout)
--stdout:write("bybye\n")
local stdin = uv.tty(0)
print(stdin)
--print(stdin:get_win_size())
stdin:read_start(function(tty, buffer, nread)
print("read", buffer, nread)
stdout:write(buffer)
print("pregc")
collectgarbage()
collectgarbage()
collectgarbage()
print("postgc")
end)
print("run")
uv.run()
--[[
create a server object, give it a connection callback
tell the server to listen on a host/port with a cb1
makes a Tcp
Tcp:bind(host, port)
Tcp responds to "listening" <- cb1
"listen" <-
create client Tcp
self:accept(client)
client:readStart()
client <- "connection"
Server responds to:
net.createServer = function(connectionCallback)
local s = Server:new(connectionCallback)
return s
end
function http.createServer(host, port, onConnection)
local server
server = net.createServer(function(client)
if err then
return server:emit("error", err)
end
-- Accept the client and build request and response objects
local request = Request:new(client)
local response = Response:new(client)
-- Convert tcp stream to HTTP stream
local current_field
local parser
local headers
parser = HttpParser.new("request", {
onMessageBegin = function ()
headers = {}
request.headers = headers
end,
onUrl = function (url)
request.url = url
end,
onHeaderField = function (field)
current_field = field
end,
onHeaderValue = function (value)
headers[current_field:lower()] = value
end,
onHeadersComplete = function (info)
request.method = info.method
request.upgrade = info.upgrade
request.version_major = info.version_major
request.version_minor = info.version_minor
-- Give upgrade requests access to the raw client if they want it
if info.upgrade then
request.client = client
end
-- Handle 100-continue requests
if request.headers.expect and info.version_major == 1 and info.version_minor == 1 and request.headers.expect:lower() == "100-continue" then
if server.handlers and server.handlers.check_continue then
server:emit("check_continue", request, response)
else
response:writeContinue()
onConnection(request, response)
end
else
onConnection(request, response)
end
end,
onBody = function (chunk)
request:emit('data', chunk, #chunk)
end,
onMessageComplete = function ()
request:emit('end')
end
})
client:on("data", function (chunk)
-- Ignore empty chunks
if #chunk == 0 then return end
-- Once we're in "upgrade" mode, the protocol is no longer HTTP and we
-- shouldn't send data to the HTTP parser
if request.upgrade then
request:emit("data", chunk)
return
end
-- Parse the chunk of HTTP, this will syncronously emit several of the
-- above events and return how many bytes were parsed.
local nparsed = parser:execute(chunk, 0, #chunk)
-- If it wasn't all parsed then there was an error parsing
if nparsed < #chunk then
-- If the error was caused by non-http protocol like in websockets
-- then that's ok, just emit the rest directly to the request object
if request.upgrade then
chunk = chunk:sub(nparsed + 1)
request:emit("data", chunk)
else
request:emit("error", "parse error")
end
end
end)
client:on("end", function ()
parser:finish()
end)
client:on("error", function (err)
request:emit("error", err)
-- N.B. must close(), or https://github.com/joyent/libuv/blob/master/src/unix/stream.c#L586
-- kills the appication
client:close()
parser:finish()
end)
end)
server:listen(port, host)
return server
end
http.createServer(function (req, res)
local body = "Hello world\n"
res:writeHead(200, {
["Content-Type"] = "text/plain",
["Content-Length"] = #body
})
res:finish(body)
end):listen(8080)
print("Server listening at http://localhost:8080/")
--]]