diff --git a/pkg/logging/console.go b/pkg/logging/console.go index c50243c..193660f 100644 --- a/pkg/logging/console.go +++ b/pkg/logging/console.go @@ -9,13 +9,11 @@ import ( ) const ( - PRIVATE_PREFIX string = "PRIVATE::" - PUBLIC_PREFIX string = "PUBLIC::" -) + PRIVATE_PREFIX = "PRIVATE::" + PUBLIC_PREFIX = "PUBLIC::" -const ( - NORMAL_LOG int = 1 - FATAL_LOG int = 2 + NORMAL_LOG = 1 + FATAL_LOG = 2 ) // Console logs a prefix, IP, and message all appeneded together to the console. diff --git a/pkg/logging/file.go b/pkg/logging/file.go index e0c8217..0c8b556 100644 --- a/pkg/logging/file.go +++ b/pkg/logging/file.go @@ -4,13 +4,11 @@ package logging import "os" const ( - PRIVATE_LOG_FOLDER string = "logs/private/" - PUBLIC_LOG_FOLDER string = "logs/public/" -) + PRIVATE_LOG_FOLDER = "logs/private/" + PUBLIC_LOG_FOLDER = "logs/public/" -const ( - ERROR string = "error.log" - WARNING string = "warning.log" + ERROR = "error.log" + WARNING = "warning.log" ) // File logs a given message to a log file diff --git a/pkg/routing/content_types.go b/pkg/routing/content_types.go index ec72284..ad6df54 100644 --- a/pkg/routing/content_types.go +++ b/pkg/routing/content_types.go @@ -3,38 +3,30 @@ package routing import ( "errors" + "mime" "path/filepath" + "strings" ) -// getContentType returns the http header safe content-type attribute for the -// requested path. If the requested path is not formatted correctly with an extension +// getContentType returns the http header safe Content-Type for the requested +// path. If the requested path is not formatted correctly with an extension // then the function will return the zero-value for a string and an error. -// BUG(george-e-shaw-iv) Cannot handle interpreted files such as .php files -// BUG(george-e-shaw-iv) Does not cover the bulk of encountered content types on the web func GetContentType(path string) (string, error) { - var contentType string - fileType := filepath.Ext(path) + var ct string - if fileType == "" { - return contentType, errors.New("Invalid path, contained no period-separated content-type") + path = filepath.Base(path) + ext := filepath.Ext(path) + fn := strings.TrimSuffix(path, ext) + + if ext == "" || fn == "" { + return "", errors.New("Invalid file path") } - switch fileType { - case "html": - contentType = "text/html" - case "css": - contentType = "text/css" - case "js": - contentType = "text/javascript" - case "png": - contentType = "image/png" - case "jpg": - fallthrough - case "jpeg": - contentType = "image/jpeg" - default: - contentType = "text/plain" + ct = mime.TypeByExtension(ext) + + if ct == "" { + return "", errors.New("Could not get content type") } - return contentType, nil + return ct, nil } diff --git a/pkg/routing/content_types_test.go b/pkg/routing/content_types_test.go new file mode 100644 index 0000000..4934028 --- /dev/null +++ b/pkg/routing/content_types_test.go @@ -0,0 +1,35 @@ +package routing + +import ( + "testing" +) + +func TestContentType(t *testing.T) { + cases := []struct { + input string + ok bool + }{ + {"/path/to/index.html", true}, + {"somepath/file.css", true}, + {"test.js", true}, + {"./relative/asdf.png", true}, + {"/something.jpg", true}, + {"/something.jpeg", true}, + {"doc.pdf", true}, + {"this/should/fail/.png", false}, + {"php/doesnt/have/a/contenttype.php", false}, + {".pdf", false}, + {"asdfpng", false}, + } + + for _, tc := range cases { + mime, err := GetContentType(tc.input) + if err != nil && tc.ok == true { + t.Fatalf("\nGetContentType(%s): Expected OK, got error: %s", tc.input, err) + } + if err == nil && tc.ok == false { + t.Fatalf("\nGetContentType(%s): Expected error, got OK: %s", tc.input, mime) + } + } + +}