Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
publictext/publictext
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
149 lines (115 sloc)
3.82 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env lua | |
| -- | |
| -- publictext | |
| -- ucspi-tcp text://protocol server | |
| -- | |
| local assert = assert | |
| local io = require( 'io' ) | |
| local ipairs = ipairs | |
| local os = require( 'os' ) | |
| local pcall = pcall | |
| local table = require( 'table' ) | |
| local tonumber = tonumber | |
| local tostring = tostring | |
| _ENV = nil | |
| local EOL = '\r\n' | |
| local SP = ' ' | |
| local _request = nil | |
| local function REQUEST( aRequest ) | |
| _request = aRequest or _request or io.stdin:read() or os.exit( false ) | |
| return _request | |
| end | |
| local function LOG( aStatus, aSize ) | |
| local aBuffer = {} | |
| aBuffer[ #aBuffer + 1 ] = os.getenv( 'TCPREMOTEHOST' ) or os.getenv( 'TCPREMOTEIP' ) or '-' -- remotehost | |
| aBuffer[ #aBuffer + 1 ] = os.getenv( 'TCPREMOTEINFO' ) or '-' -- rfc931 | |
| aBuffer[ #aBuffer + 1 ] = '-' -- authuser | |
| aBuffer[ #aBuffer + 1 ] = ( '[%s]' ):format( os.date( '!%Y-%m-%dT%TZ' ) ) -- [date] | |
| aBuffer[ #aBuffer + 1 ] = ( '%q' ):format( REQUEST() or '' ) -- "request" | |
| aBuffer[ #aBuffer + 1 ] = tostring( aStatus or '-' ) -- status | |
| aBuffer[ #aBuffer + 1 ] = tostring( aSize or '0' ) -- bytes | |
| io.stderr:write( table.concat( aBuffer, ' ' ), '\n' ) | |
| end | |
| local function STATUS( aStatus, aDescription ) | |
| local aStatus = assert( tonumber( aStatus ) ) | |
| assert( aDescription ) | |
| assert( 0 ~= aDescription:len() ) | |
| assert( 1024 >= aDescription:len() ) | |
| assert( not aDescription:find( '%c' ) ) | |
| io.stdout:write( aStatus, SP, aDescription, EOL ) | |
| io.stdout:flush() | |
| end | |
| local function CHUNK( aFile ) | |
| return function() return aFile:read( 4096 ) end | |
| end | |
| local function OK( aFile, aType, aSize ) | |
| STATUS( 20, assert( aType ) ) | |
| LOG( 20, assert( aSize ) ) | |
| for aChunk in CHUNK( assert( aFile ) ) do | |
| io.stdout:write( aChunk ) | |
| io.stdout:flush() | |
| end | |
| aFile:close() | |
| os.exit( true ) | |
| end | |
| local function REDIRECT( aLocation ) | |
| STATUS( 30, aLocation ) | |
| LOG( 30 ) | |
| os.exit( true ) | |
| end | |
| local function NOK() | |
| STATUS( 40, 'NOK' ) | |
| LOG( 40 ) | |
| os.exit( false ) | |
| end | |
| local function LINK( aFile, aType ) | |
| if aType ~= '.lnk' then return end | |
| local aLocation = assert( aFile:read():match( '^(%C+)' ) ) | |
| assert( aFile:close() ) | |
| REDIRECT( aLocation ) | |
| end | |
| local function QUOTE( anArgument ) | |
| return ( "'%s'" ):format( assert( anArgument ):gsub( "'", "'\\''" ) ) | |
| end | |
| local function TYPE( aFileName ) | |
| local aCommand = ( 'file --brief --mime-type --mime-encoding %s 2>/dev/null' ):format( assert( QUOTE( aFileName ) ) ) | |
| local aHandle = assert( io.popen( aCommand, 'r' ) ) | |
| local aType = assert( aHandle:read() ) | |
| assert( aHandle:close() ) | |
| return aType | |
| end | |
| local function FILE( aName, anExtension ) | |
| local someTypes = { '.lnk', '' } | |
| for _, aType in ipairs( someTypes ) do | |
| local aFileName = ( '%s.%s%s' ):format( aName, anExtension, aType ) | |
| local aFile = io.open( aFileName, 'rb' ) | |
| if aFile then | |
| local aSize = assert( aFile:seek( 'end' ) ) | |
| assert( aFile:seek( 'set' ) ) | |
| LINK( aFile, aType ) | |
| return aFile, TYPE( aFileName ), aSize | |
| end | |
| end | |
| end | |
| local function HANDLE() | |
| local aRequest = REQUEST() | |
| assert( aRequest ) | |
| aRequest = assert( REQUEST( aRequest:match( '^(%C+)' ) ) ) | |
| assert( 0 ~= aRequest:len() ) | |
| assert( 1024 >= aRequest:len() ) | |
| assert( not aRequest:find( '%s' ) ) | |
| assert( aRequest == aRequest:lower() ) | |
| assert( 'text://' == aRequest:sub( 1, 7 ) ) | |
| local aName, anExtension = aRequest:match( '/([%w-]+)%.(%w+)$' ) | |
| if '/' == aRequest:sub( -1, -1 ) then aName = 'index' anExtension = 'txt' end | |
| if not aName then REDIRECT( ( '%s/' ):format( aRequest ) ) end | |
| assert( aName ) | |
| assert( 0 ~= aName:len() ) | |
| assert( 128 >= aName:len() ) | |
| assert( anExtension ) | |
| assert( 0 ~= anExtension:len() ) | |
| assert( 3 >= anExtension:len() ) | |
| local aFile, aType, aSize = FILE( aName, anExtension ) | |
| OK( aFile, aType, aSize ) | |
| end | |
| if not pcall( HANDLE ) then NOK() end |