@ -32,18 +32,18 @@ func (fs *FileSystem) Init(size int, fileSizeMax float64) {
fs . CacheFileMax = int64 ( BytesInMegaByte * fileSizeMax )
}
func ( fs * FileSystem ) HandleRequest ( re quest * Request ) * GophorError {
func ( fs * FileSystem ) HandleRequest ( re sponder * Responder ) * GophorError {
/* Check if restricted file */
if isRestrictedFile ( re quest. AbsPath ( ) ) {
if isRestrictedFile ( re sponder. Re quest. AbsPath ( ) ) {
return & GophorError { IllegalPathErr , nil }
}
/* Get filesystem stat, check it exists! */
stat , err := os . Stat ( re quest. AbsPath ( ) )
stat , err := os . Stat ( re sponder. Re quest. AbsPath ( ) )
if err != nil {
/* Check file isn't in cache before throwing in the towel */
fs . CacheMutex . RLock ( )
file := fs . CacheMap . Get ( re quest. AbsPath ( ) )
file := fs . CacheMap . Get ( re sponder. Re quest. AbsPath ( ) )
if file == nil {
fs . CacheMutex . RUnlock ( )
return & GophorError { FileStatErr , err }
@ -51,7 +51,7 @@ func (fs *FileSystem) HandleRequest(request *Request) *GophorError {
/* It's there! Get contents, unlock and return */
file . Mutex . RLock ( )
gophorErr := file . WriteContents ( re quest )
gophorErr := file . WriteContents ( re sponder )
file . Mutex . RUnlock ( )
fs . CacheMutex . RUnlock ( )
@ -63,43 +63,44 @@ func (fs *FileSystem) HandleRequest(request *Request) *GophorError {
/* Directory */
case stat . Mode ( ) & os . ModeDir != 0 :
/* Ignore anything under cgi-bin directory */
if re quest. PathHasRelPrefix ( CgiBinDirStr ) {
if re sponder. Re quest. PathHasRelPrefix ( CgiBinDirStr ) {
return & GophorError { IllegalPathErr , nil }
}
/* Check Gophermap exists */
gophermapPath := NewRequestPath ( re quest. RootDir ( ) , r equest. PathJoinRel ( GophermapFileStr ) )
gophermapPath := NewRequestPath ( re sponder. Re quest. RootDir ( ) , r esponder. R equest. PathJoinRel ( GophermapFileStr ) )
stat , err = os . Stat ( gophermapPath . Absolute ( ) )
if err == nil {
/* Gophermap exists! If executable and CGI enabled execute, else serve. */
gophermapRequest := NewRequest ( request . Host , request . Client , request . Writer , gophermapPath , request . Parameters )
gophermapRequest := & Request { gophermapPath , responder . Request . Parameters }
responder . Request = gophermapRequest
if stat . Mode ( ) . Perm ( ) & 0100 != 0 {
if Config . CgiEnabled {
return gophermapRequest. SafeFlush ( executeFile ( gophermapRequest ) )
return responder. SafeFlush ( executeFile ( responder ) )
} else {
return & GophorError { CgiDisabledErr , nil }
}
} else {
return fs . FetchFile ( gophermapRequest )
return fs . FetchFile ( responder )
}
} else {
/* No gophermap, serve directory listing */
return listDir ( re quest , map [ string ] bool { } )
return listDir ( re sponder , map [ string ] bool { } )
}
/* Regular file */
case stat . Mode ( ) & os . ModeType == 0 :
/* If cgi-bin and CGI enabled, return executed contents. Else, fetch */
if re quest. PathHasRelPrefix ( CgiBinDirStr ) {
if re sponder. Re quest. PathHasRelPrefix ( CgiBinDirStr ) {
if Config . CgiEnabled {
return re quest. SafeFlush ( executeCgi ( request ) )
return re sponder. SafeFlush ( executeCgi ( responder ) )
} else {
return & GophorError { CgiDisabledErr , nil }
}
} else {
return fs . FetchFile ( re quest )
return fs . FetchFile ( re sponder )
}
/* Unsupported type */
@ -108,10 +109,10 @@ func (fs *FileSystem) HandleRequest(request *Request) *GophorError {
}
}
func ( fs * FileSystem ) FetchFile ( re quest * Request ) * GophorError {
func ( fs * FileSystem ) FetchFile ( re sponder * Responder ) * GophorError {
/* Get cache map read lock then check if file in cache map */
fs . CacheMutex . RLock ( )
file := fs . CacheMap . Get ( re quest. AbsPath ( ) )
file := fs . CacheMap . Get ( re sponder. Re quest. AbsPath ( ) )
if file != nil {
/* File in cache -- before doing anything get file read lock */
@ -140,7 +141,7 @@ func (fs *FileSystem) FetchFile(request *Request) *GophorError {
/ * Open file here , to check it exists , ready for file stat
* and in case file is too big we pass it as a raw response
* /
fd , err := os . Open ( re quest. AbsPath ( ) )
fd , err := os . Open ( re sponder. Re quest. AbsPath ( ) )
if err != nil {
/* Error stat'ing file, unlock read mutex then return error */
fs . CacheMutex . RUnlock ( )
@ -159,22 +160,22 @@ func (fs *FileSystem) FetchFile(request *Request) *GophorError {
if stat . Size ( ) > fs . CacheFileMax {
/* Unlock the read mutex, we don't need it where we're going... returning, we're returning. */
fs . CacheMutex . RUnlock ( )
return re quest . WriteRaw ( fd )
return re sponder . WriteRaw ( fd )
}
/* Create new file contents */
var contents FileContents
if re quest. PathHasAbsSuffix ( "/" + GophermapFileStr ) {
contents = & GophermapContents { re quest. CachedRequest ( ) , nil }
if re sponder. Re quest. PathHasAbsSuffix ( "/" + GophermapFileStr ) {
contents = & GophermapContents { re sponder. Request , nil }
} else {
contents = & RegularFileContents { re quest. CachedRequest ( ) , nil }
contents = & RegularFileContents { re sponder. Request , nil }
}
/* Compare file size (in MB) to CacheFileSizeMax. If larger, just send file raw */
if stat . Size ( ) > fs . CacheFileMax {
/* Unlock the read mutex, we don't need it where we're going... returning, we're returning. */
fs . CacheMutex . RUnlock ( )
return contents . Render ( re quest )
return contents . Render ( re sponder )
}
/* Create new file wrapper around contents */
@ -193,7 +194,7 @@ func (fs *FileSystem) FetchFile(request *Request) *GophorError {
fs . CacheMutex . Lock ( )
/* Put file in the FixedMap */
fs . CacheMap . Put ( re quest. AbsPath ( ) , file )
fs . CacheMap . Put ( re sponder. Re quest. AbsPath ( ) , file )
/* Before unlocking cache mutex, lock file read for upcoming call to .Contents() */
file . Mutex . RLock ( )
@ -204,7 +205,7 @@ func (fs *FileSystem) FetchFile(request *Request) *GophorError {
}
/* Read file contents into new variable for return, then unlock file read lock */
gophorErr := file . WriteContents ( re quest )
gophorErr := file . WriteContents ( re sponder )
file . Mutex . RUnlock ( )
/* Finally we can unlock the cache map read lock, we are done :) */
@ -225,8 +226,8 @@ type File struct {
LastRefresh int64
}
func ( f * File ) WriteContents ( re quest * Request ) * GophorError {
return f . Content . Render ( re quest )
func ( f * File ) WriteContents ( re sponder * Responder ) * GophorError {
return f . Content . Render ( re sponder )
}
func ( f * File ) CacheContents ( ) * GophorError {