Skip to content

Commit

Permalink
Updating http and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhollander committed Sep 30, 2011
1 parent 11fa760 commit 6b65d38
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
3 changes: 2 additions & 1 deletion ox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ function ox.fill(src, cn)
else
src.h = 0
src.len = i
stop_read(src)
return cn(src)
end
end)
Expand Down Expand Up @@ -289,7 +290,7 @@ function ox.read(src, n, cb)
end

function ox.write(des, str, cn)
local buff = vla_char(#str, str)
local buff = vla_char(#str+1, str)
local n = 0
return on_write(des, function()
local m = C.write(des.fd, buff+n, #str - n)
Expand Down
10 changes: 10 additions & 0 deletions ox/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ function http.readreq(c, cb)
return ox.readln(c, 2048, readreq_status)
end

--[[
function http.readreq2(c, cb)
return ox.scan(c, 8, ' ', function(c, method)
if methods[method] then end
end)
end]]


--
--
function http.folder(dir)
local dir=dir:match('^(.+)/?$')
return function(c, path)
Expand Down
10 changes: 10 additions & 0 deletions tests/helloworld.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local ox = require'ox'
local PORT = 9094
print(PORT)
print(ox.tcpserv(PORT, function(c)
return ox.readln(c, 2048, function(c, line)
return ox.write(c, 'HTTP/1.1 200 OK\r\n\r\nHello World\r\n', ox.close)
end)
end))

ox.start()
32 changes: 26 additions & 6 deletions tests/http1.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
print 'this test requires apache bench!'

local ox = require 'ox'
local http = require 'ox.http'
local PORT = ... or 8092
local http = require'ox.http'
local PORT = ... or 8093
local N = 10000
local test = 'ab -c 1000 -n '..N..' http://localhost:'..PORT..'/'
local testline = 'GET / HTTP/1.0'

local tc = table.concat
http.route '*' '*' '*' (function(c, host, method, path)
--print(host, method, path)
local body = ('Host: %s\nMethod: %s\nPath: %s\n'):format(host, method, path)
return http.reply(c, 200, body)
end)

print(ox.tcpserv(PORT, http.accept))
print(PORT)
ox.start()
assert(ox.tcpserv(PORT, http.accept))

local p
ox.at(ox.time + 3, function()
local results = p:read(480)
local n = results:match 'Complete requests:%s+(%d+)'
assert(n, 'Could not parse complete requests from apache bench')
assert(tonumber(n) == N, 'Complete requests not equal to N')
p:close()
ox.stop()
end)

ox.start(function()
p = assert(io.popen(test), 'Could not open apache bench')
end)

print 'pass'
29 changes: 29 additions & 0 deletions tests/http2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local ox = require 'ox'
local http = require 'ox.http'
local PORT = 9090


print('1 readln, no parsing', PORT)
print(ox.tcpserv(PORT, function(c)
return ox.readln(c, 2048, function(c, line)
return ox.write(c, 'HTTP/1.1 200 OK\r\n\r\nHello World\r\n', ox.close)
end)
end))

print('full parsing', PORT+1)
print(ox.tcpserv(PORT+2, function(c)
return http.readreq(c, function(c)
c.res = {jar={},head={}}
return http.reply(c, 200, 'Hello World')
end)
end))


print('fill parsing + routing', PORT+2)
http.route '*' '*' '*' (function(c)
return c:reply(200, 'Hello World')
end)

print(ox.tcpserv(PORT+2, http.accept))

ox.start()

0 comments on commit 6b65d38

Please sign in to comment.