2020-04-09 16:33:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Simple error code type defs */
|
2020-04-09 16:33:17 +00:00
|
|
|
type ErrorCode int
|
2020-04-20 14:18:35 +00:00
|
|
|
type ErrorResponseCode int
|
2020-04-09 16:33:17 +00:00
|
|
|
const (
|
|
|
|
/* Filesystem */
|
2020-05-06 17:21:21 +00:00
|
|
|
PathEnumerationErr ErrorCode = iota
|
|
|
|
IllegalPathErr ErrorCode = iota
|
|
|
|
FileStatErr ErrorCode = iota
|
|
|
|
FileOpenErr ErrorCode = iota
|
|
|
|
FileReadErr ErrorCode = iota
|
|
|
|
FileTypeErr ErrorCode = iota
|
|
|
|
DirListErr ErrorCode = iota
|
2020-05-03 21:42:51 +00:00
|
|
|
|
2020-04-09 16:33:17 +00:00
|
|
|
/* Sockets */
|
2020-05-06 17:21:21 +00:00
|
|
|
BufferedWriteErr ErrorCode = iota
|
|
|
|
BufferedWriteReadErr ErrorCode = iota
|
|
|
|
BufferedWriteFlushErr ErrorCode = iota
|
2020-04-09 16:33:17 +00:00
|
|
|
|
|
|
|
/* Parsing */
|
2020-05-06 17:21:21 +00:00
|
|
|
InvalidRequestErr ErrorCode = iota
|
|
|
|
EmptyItemTypeErr ErrorCode = iota
|
|
|
|
InvalidGophermapErr ErrorCode = iota
|
2020-04-20 14:18:35 +00:00
|
|
|
|
2020-05-03 21:42:51 +00:00
|
|
|
/* Executing */
|
2020-05-06 17:21:21 +00:00
|
|
|
CommandStartErr ErrorCode = iota
|
|
|
|
CommandExitCodeErr ErrorCode = iota
|
2020-05-07 16:08:06 +00:00
|
|
|
CgiOutputErr ErrorCode = iota
|
2020-05-06 17:21:21 +00:00
|
|
|
CgiDisabledErr ErrorCode = iota
|
|
|
|
RestrictedCommandErr ErrorCode = iota
|
2020-05-03 21:42:51 +00:00
|
|
|
|
2020-04-20 14:18:35 +00:00
|
|
|
/* Error Response Codes */
|
|
|
|
ErrorResponse200 ErrorResponseCode = iota
|
|
|
|
ErrorResponse400 ErrorResponseCode = iota
|
|
|
|
ErrorResponse401 ErrorResponseCode = iota
|
|
|
|
ErrorResponse403 ErrorResponseCode = iota
|
|
|
|
ErrorResponse404 ErrorResponseCode = iota
|
|
|
|
ErrorResponse408 ErrorResponseCode = iota
|
|
|
|
ErrorResponse410 ErrorResponseCode = iota
|
|
|
|
ErrorResponse500 ErrorResponseCode = iota
|
|
|
|
ErrorResponse501 ErrorResponseCode = iota
|
|
|
|
ErrorResponse503 ErrorResponseCode = iota
|
|
|
|
NoResponse ErrorResponseCode = iota
|
2020-04-09 16:33:17 +00:00
|
|
|
)
|
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Simple GophorError data structure to wrap another error */
|
2020-04-09 16:33:17 +00:00
|
|
|
type GophorError struct {
|
|
|
|
Code ErrorCode
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Convert error code to string */
|
2020-04-09 16:33:17 +00:00
|
|
|
func (e *GophorError) Error() string {
|
|
|
|
var str string
|
|
|
|
switch e.Code {
|
2020-04-10 15:01:07 +00:00
|
|
|
case PathEnumerationErr:
|
|
|
|
str = "path enumeration fail"
|
|
|
|
case IllegalPathErr:
|
|
|
|
str = "illegal path requested"
|
|
|
|
case FileStatErr:
|
2020-04-09 16:33:17 +00:00
|
|
|
str = "file stat fail"
|
2020-04-10 15:01:07 +00:00
|
|
|
case FileOpenErr:
|
2020-04-09 16:33:17 +00:00
|
|
|
str = "file open fail"
|
2020-04-10 15:01:07 +00:00
|
|
|
case FileReadErr:
|
2020-04-09 16:33:17 +00:00
|
|
|
str = "file read fail"
|
2020-04-10 15:01:07 +00:00
|
|
|
case FileTypeErr:
|
2020-04-09 16:33:17 +00:00
|
|
|
str = "invalid file type"
|
2020-04-10 15:01:07 +00:00
|
|
|
case DirListErr:
|
2020-04-09 16:33:17 +00:00
|
|
|
str = "directory read fail"
|
|
|
|
|
2020-05-06 17:21:21 +00:00
|
|
|
case BufferedWriteErr:
|
|
|
|
str = "buffered write error"
|
|
|
|
case BufferedWriteReadErr:
|
|
|
|
str = "buffered write readFrom error"
|
|
|
|
case BufferedWriteFlushErr:
|
|
|
|
str = "buffered write flush error"
|
2020-04-09 16:33:17 +00:00
|
|
|
|
2020-04-11 20:44:05 +00:00
|
|
|
case InvalidRequestErr:
|
|
|
|
str = "invalid request data"
|
2020-04-16 10:41:33 +00:00
|
|
|
case InvalidGophermapErr:
|
|
|
|
str = "invalid gophermap"
|
2020-04-09 16:33:17 +00:00
|
|
|
|
2020-05-03 21:42:51 +00:00
|
|
|
case CommandStartErr:
|
|
|
|
str = "command start fail"
|
2020-05-07 16:08:06 +00:00
|
|
|
case CgiOutputErr:
|
|
|
|
str = "cgi output format error"
|
2020-05-04 17:19:56 +00:00
|
|
|
case CommandExitCodeErr:
|
|
|
|
str = "command exit code non-zero"
|
2020-05-05 08:35:29 +00:00
|
|
|
case CgiDisabledErr:
|
|
|
|
str = "ignoring /cgi-bin request, CGI disabled"
|
2020-05-05 22:46:50 +00:00
|
|
|
case RestrictedCommandErr:
|
|
|
|
str = "command use restricted"
|
2020-05-03 21:42:51 +00:00
|
|
|
|
2020-04-09 16:33:17 +00:00
|
|
|
default:
|
|
|
|
str = "Unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Err != nil {
|
2020-04-11 10:42:34 +00:00
|
|
|
return fmt.Sprintf("%s (%s)", str, e.Err.Error())
|
2020-04-09 16:33:17 +00:00
|
|
|
} else {
|
2020-04-11 10:42:34 +00:00
|
|
|
return fmt.Sprintf("%s", str)
|
2020-04-09 16:33:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-20 14:18:35 +00:00
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Convert a gophor error code to appropriate error response code */
|
2020-04-20 14:18:35 +00:00
|
|
|
func gophorErrorToResponseCode(code ErrorCode) ErrorResponseCode {
|
|
|
|
switch code {
|
|
|
|
case PathEnumerationErr:
|
|
|
|
return ErrorResponse400
|
|
|
|
case IllegalPathErr:
|
|
|
|
return ErrorResponse403
|
|
|
|
case FileStatErr:
|
|
|
|
return ErrorResponse404
|
|
|
|
case FileOpenErr:
|
|
|
|
return ErrorResponse404
|
|
|
|
case FileReadErr:
|
|
|
|
return ErrorResponse404
|
|
|
|
case FileTypeErr:
|
|
|
|
/* If wrong file type, just assume file not there */
|
|
|
|
return ErrorResponse404
|
|
|
|
case DirListErr:
|
|
|
|
return ErrorResponse404
|
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* These are errors _while_ sending, no point trying to send error */
|
2020-05-06 17:21:21 +00:00
|
|
|
case BufferedWriteErr:
|
2020-04-20 14:18:35 +00:00
|
|
|
return NoResponse
|
2020-05-06 17:21:21 +00:00
|
|
|
case BufferedWriteReadErr:
|
|
|
|
return NoResponse
|
|
|
|
case BufferedWriteFlushErr:
|
2020-04-20 14:18:35 +00:00
|
|
|
return NoResponse
|
|
|
|
|
|
|
|
case InvalidRequestErr:
|
|
|
|
return ErrorResponse400
|
|
|
|
case InvalidGophermapErr:
|
|
|
|
return ErrorResponse500
|
|
|
|
|
2020-05-03 21:42:51 +00:00
|
|
|
case CommandStartErr:
|
|
|
|
return ErrorResponse500
|
2020-05-04 17:19:56 +00:00
|
|
|
case CommandExitCodeErr:
|
|
|
|
return ErrorResponse500
|
2020-05-07 16:08:06 +00:00
|
|
|
case CgiOutputErr:
|
|
|
|
return ErrorResponse500
|
2020-05-05 08:35:29 +00:00
|
|
|
case CgiDisabledErr:
|
|
|
|
return ErrorResponse404
|
2020-05-05 22:46:50 +00:00
|
|
|
case RestrictedCommandErr:
|
|
|
|
return ErrorResponse500
|
2020-05-03 21:42:51 +00:00
|
|
|
|
2020-04-20 14:18:35 +00:00
|
|
|
default:
|
|
|
|
return ErrorResponse503
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Generates gopher protocol compatible error response from our code */
|
2020-04-20 14:18:35 +00:00
|
|
|
func generateGopherErrorResponseFromCode(code ErrorCode) []byte {
|
|
|
|
responseCode := gophorErrorToResponseCode(code)
|
|
|
|
if responseCode == NoResponse {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return generateGopherErrorResponse(responseCode)
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Generates gopher protocol compatible error response for response code */
|
2020-04-20 14:18:35 +00:00
|
|
|
func generateGopherErrorResponse(code ErrorResponseCode) []byte {
|
2020-05-06 12:35:06 +00:00
|
|
|
return buildErrorLine(code.String())
|
2020-04-20 14:18:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Error response code to string */
|
2020-04-20 14:18:35 +00:00
|
|
|
func (e ErrorResponseCode) String() string {
|
|
|
|
switch e {
|
|
|
|
case ErrorResponse200:
|
2020-05-09 15:33:38 +00:00
|
|
|
/* Should not have reached here */
|
|
|
|
Config.SysLog.Fatal("", "Passed error response 200 to error handler, SHOULD NOT HAVE DONE THIS\n")
|
|
|
|
return ""
|
2020-04-20 14:18:35 +00:00
|
|
|
case ErrorResponse400:
|
|
|
|
return "400 Bad Request"
|
|
|
|
case ErrorResponse401:
|
|
|
|
return "401 Unauthorised"
|
|
|
|
case ErrorResponse403:
|
|
|
|
return "403 Forbidden"
|
|
|
|
case ErrorResponse404:
|
|
|
|
return "404 Not Found"
|
|
|
|
case ErrorResponse408:
|
|
|
|
return "408 Request Time-out"
|
|
|
|
case ErrorResponse410:
|
|
|
|
return "410 Gone"
|
|
|
|
case ErrorResponse500:
|
|
|
|
return "500 Internal Server Error"
|
|
|
|
case ErrorResponse501:
|
|
|
|
return "501 Not Implemented"
|
|
|
|
case ErrorResponse503:
|
|
|
|
return "503 Service Unavailable"
|
|
|
|
default:
|
|
|
|
/* Should not have reached here */
|
2020-05-02 21:34:49 +00:00
|
|
|
Config.SysLog.Fatal("", "Unhandled ErrorResponseCode type\n")
|
2020-04-20 14:18:35 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|