Skip to content

Commit

Permalink
can serve jpegs (and only these :P) as binary prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Abraham committed Mar 25, 2012
1 parent 16c64e5 commit 14d733d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
18 changes: 11 additions & 7 deletions bin/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ while true do
local err = ""

if client then
client:settimeout(0)
client:settimeout( nil )

table.insert( selectlist, client )
end

readlist, _, err = socket.select( selectlist, nil, 0.1 )
readlist, _, err = socket.select( selectlist, nil, nil )

if err then print( err ) end

Expand All @@ -40,13 +41,15 @@ while true do

if key ~= nil and value ~= nil then
request[key] = value
else
break
end
if rest ~= nil then
request.post = rest
break
end
end

if request.GET ~= nil then
local getList = getRequest( request.GET, " " )
if getList[1] ~= nil then
Expand All @@ -61,10 +64,11 @@ while true do
end
end

-- Debug Information prints complete request
--for k,l in pairs( request ) do
-- print( " key: " .. k .. " value: " .. l )
--end
--[[ Debug Information prints complete request
for k,l in pairs( request ) do
print( " key: " .. k .. " value: " .. l )
end
--]]

client:close()
table.remove( selectlist, i )
Expand Down
Binary file added content/wallpaper.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/whos_awesome.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 53 additions & 16 deletions lib/answerRequest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,57 @@

function answerRequest ( request )
local fileStatus = 0;
local req
local ext

request = cutTrailingSlash( request )
local fh , err = io.open( "./content/" .. request, "r" )

if #request == 0 then
request = "index.html"
end

req, ext = splitReq( request )

local fh , err = io.open( "./content/" .. request, "rb" )

if fh == nil then
fileStatus = 1;
fh , err = io.open( "./content/error.html", "r" )
return prepareHeader( fileStatus ) .. prepareContent( fh )
else
return prepareHeader( fileStatus ) .. prepareContent( fh )
return prepareHeader( fileStatus, ext ) .. prepareContent( fh, ext )
end
end
end

function prepareHeader ( status )
function prepareHeader ( status, ext )

return prepareStatus( status )
.. prepareDate()
.. prepareServername()
.. prepareConnType()
.. prepareContentType()
.. prepareConnType( )
.. prepareContentType( ext )
end

function prepareContent ( fh )
local html = ""

while true do
local line = fh.read( fh )
if not line then break end
html = html .. line
end
function prepareContent ( fh, ext )
local html
if ext == "html" then
html = ""
while true do
local line = fh.read( fh )
if not line then break end
html = html .. line
end
elseif ext == "jpg" or ext == "jpeg" then
html = fh:read("*a")
else
html = ""
while true do
local line = fh.read( fh )
if not line then break end
html = html .. line
end

end
return html
end

Expand Down Expand Up @@ -68,7 +89,23 @@ function prepareConnType ()
return conntype
end

function prepareContentType ()
local contenttype = 'Content-Type: text/html; charset=iso-8859-1\r\n\r\n'
function prepareContentType ( ext )
local contenttype
if ext == "html" then
contenttype = 'Content-Type: text/html; charset=iso-8859-1\r\n\r\n'
elseif ext == "jpg" or ext == "jpeg" then
contenttype = 'Content-Type: image/jpeg\r\n\r\n'
else
contenttype = 'Content-Type: text/html; charset=iso-8859-1\r\n\r\n'
end
return contenttype
end

function splitReq( request )
local req
local ext
local found = request:find( '%.' )
req = request:sub( 1, found - 1 )
ext = request:sub( found + 1 )
return req, ext;
end

0 comments on commit 14d733d

Please sign in to comment.