diff --git a/app/config/config.go b/app/config/config.go index 8bf55d0a..2b44ea6e 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -162,8 +162,6 @@ var levels = map[string]logrus.Level{ "PANIC": logrus.PanicLevel, } -// wrongLoggingLevelError is raised when the logging level defined by the user -// is not applicable. type wrongLoggingLevelError struct{} func (e *wrongLoggingLevelError) Error() string { @@ -172,7 +170,7 @@ func (e *wrongLoggingLevelError) Error() string { // getLoggingLevelFromFileConfig returns a logrus level if a matching was found // with the one defined by the user. -// If no match, throws a wrongLoggingLevelError. +// If no match, throws an error. func getLoggingLevelFromFileConfig(c *fileConfig) (logrus.Level, error) { l, ok := levels[c.Logs.Level] if !ok { @@ -189,8 +187,6 @@ var formatters = map[string]logrus.Formatter{ "json": &logrus.JSONFormatter{}, } -// wrongLoggingFormatError is raised when the logging format defined by the user -// is not applicable. type wrongLoggingFormatError struct{} func (e *wrongLoggingFormatError) Error() string { @@ -199,7 +195,7 @@ func (e *wrongLoggingFormatError) Error() string { // getLoggingLevelFromFileConfig returns a logrus Formatter if a matching was found // with the format defined by the user. -// If no match, throws a wrongLoggingFormatError. +// If no match, throws an error. func getLoggingFormatterFromFileConfig(c *fileConfig) (logrus.Formatter, error) { f, ok := formatters[c.Logs.Format] if !ok { diff --git a/app/handlers/handlers.go b/app/handlers/handlers.go index 0d3afc21..32f66f3c 100644 --- a/app/handlers/handlers.go +++ b/app/handlers/handlers.go @@ -21,8 +21,6 @@ func GetHandlersChain() http.Handler { return alice.New(enforceContentLengthHandler, enforceContentTypeHandler, convertHandler, serveHandler).ThenFunc(clearHandler) } -// requestHasNoContentError is raised when the request -// content length is 0. type requestHasNoContentError struct{} func (e *requestHasNoContentError) Error() string { diff --git a/app/handlers/http/http.go b/app/handlers/http/http.go index 0b6b0edb..87cf145a 100644 --- a/app/handlers/http/http.go +++ b/app/handlers/http/http.go @@ -1,3 +1,4 @@ +// Package http provides functions for detecting a request or a file content type. package http import ( @@ -7,22 +8,30 @@ import ( "strings" ) +// ContentType is a string which represents a content type. type ContentType string const ( - PDFContentType ContentType = "application/pdf" - HTMLContentType ContentType = "text/html" - OctetStreamContentType ContentType = "application/octet-stream" - ZipContentType ContentType = "application/zip" + // PDFContentType represents... the PDF content type. + PDFContentType ContentType = "application/pdf" + // HTMLContentType represents... the HTML content type. + HTMLContentType ContentType = "text/html" + // OctetStreamContentType represents... the octet stream content type. + OctetStreamContentType ContentType = "application/octet-stream" + // ZipContentType represents... the zip content type. + ZipContentType ContentType = "application/zip" + // MultipartFormDataContentType represents... the multipart form data content type. MultipartFormDataContentType ContentType = "multipart/form-data" ) type notAuthorizedContentTypeError struct{} func (e *notAuthorizedContentTypeError) Error() string { - return fmt.Sprintf("Accepted values for 'Content-Type': %s, %s, %s, %s", HTMLContentType, OctetStreamContentType, MultipartFormDataContentType) + return fmt.Sprintf("Accepted values for 'Content-Type': %s, %s, %s", HTMLContentType, OctetStreamContentType, MultipartFormDataContentType) } +// FindAuthorizedContentType tries to return a content type according to a request header. +// If no authorized content type found, throws an error. func FindAuthorizedContentType(h http.Header) (ContentType, error) { ct := findContentType(h.Get("Content-Type"), HTMLContentType, OctetStreamContentType, MultipartFormDataContentType) if ct == "" { @@ -35,9 +44,11 @@ func FindAuthorizedContentType(h http.Header) (ContentType, error) { type notAuthorizedFileContentTypeError struct{} func (e *notAuthorizedFileContentTypeError) Error() string { - return fmt.Sprintf("Unable to detect a file 'Content-Type'") + return fmt.Sprintf("Unable to detect a file content type") } +// SniffContentType tries to detect the content type of a file. +// If no authorized content type found, throws an error. func SniffContentType(f *os.File) (ContentType, error) { // only the first 512 bytes are used to sniff the content type. buffer := make([]byte, 512) @@ -58,6 +69,8 @@ func SniffContentType(f *os.File) (ContentType, error) { return ct, nil } +// findContentType parses a string representing a content type and tries to find +// one of the given content types. func findContentType(contentType string, contentTypes ...ContentType) ContentType { for _, ct := range contentTypes { if i := strings.IndexRune(contentType, ';'); i != -1 {