From a283b4cd2072109e44817a4fffd9045c67985909 Mon Sep 17 00:00:00 2001 From: George Shaw Date: Wed, 25 Oct 2017 14:46:21 -0500 Subject: [PATCH] Added some needed comments, removed all literal HTTP codes and replaced them with the constants from the http package. Changed the way auth API works by returning a status code that returns the correct status of success (eventually I'm thinking we'll return a login token of sorts). --- document_roots/webhost/index.html | 2 +- pkg/api/authentication.go | 16 ++++++++++++---- pkg/public/public.go | 12 +++++++++--- pkg/webhost/webhost.go | 8 +++++--- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/document_roots/webhost/index.html b/document_roots/webhost/index.html index ffde255..2549d76 100644 --- a/document_roots/webhost/index.html +++ b/document_roots/webhost/index.html @@ -51,7 +51,7 @@ xhr.send(JSON.stringify(formData)); xhr.onloadend = function() { - console.log(xhr.response); + console.log(xhr.status); } } diff --git a/pkg/api/authentication.go b/pkg/api/authentication.go index 93aadc3..3e2a364 100644 --- a/pkg/api/authentication.go +++ b/pkg/api/authentication.go @@ -6,25 +6,33 @@ import ( "net/http" ) +// auth struct is the structure of the JSON data to be retrieved from +// the authentication API request var auth struct { User string `json:"user"` Pass string `json:"pass"` } +// Authentication function is accessed by an API call from the webhost root +// by accessing /authentication and sending it a post request with func Authentication(res http.ResponseWriter, req *http.Request) bool { + if req.Method != "POST" { + http.Error(res, req.Method+" HTTP method is unsupported for this API.", http.StatusMethodNotAllowed) + return false + } + err := json.NewDecoder(req.Body).Decode(&auth) if err != nil { - http.Error(res, err.Error(), 400) + http.Error(res, err.Error(), http.StatusBadRequest) return false } else { if auth.User == "root" && auth.Pass == "root" { - res.WriteHeader(200) - res.Write([]byte("success")) + res.WriteHeader(http.StatusNoContent) return true } else { - http.Error(res, "Authentication failed", 401) + http.Error(res, "Authentication failed", http.StatusUnauthorized) return false } diff --git a/pkg/public/public.go b/pkg/public/public.go index 362b6d2..520cde7 100644 --- a/pkg/public/public.go +++ b/pkg/public/public.go @@ -21,9 +21,15 @@ func NewPublicWeb() PublicWeb { } } +// ServeHTTP function routes all requests for the public web server. It is used in the main +// function inside of the http.ListenAndServe() function for the public host. func (pub *PublicWeb) ServeHTTP(w http.ResponseWriter, req *http.Request) { path := req.URL.Path[1:] - path = (pub.Directory + path) + if len(path) == 0 { + path = (pub.Directory + "index.html") + } else { + path = (pub.Directory + path) + } f, err := os.Open(path) @@ -37,12 +43,12 @@ func (pub *PublicWeb) ServeHTTP(w http.ResponseWriter, req *http.Request) { logging.Console(logging.PUBLIC_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" rendered a 200 success.") } else { - routing.HttpThrowStatus(404, w) + routing.HttpThrowStatus(http.StatusUnsupportedMediaType, w) logging.Console(logging.PUBLIC_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" content type could not be determined, 404 error.") } } else { - routing.HttpThrowStatus(404, w) + routing.HttpThrowStatus(http.StatusNotFound, w) logging.Console(logging.PUBLIC_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" rendered a 404 error.") } diff --git a/pkg/webhost/webhost.go b/pkg/webhost/webhost.go index 6d76454..3950369 100644 --- a/pkg/webhost/webhost.go +++ b/pkg/webhost/webhost.go @@ -24,6 +24,8 @@ func NewPrivateHost() PrivateHost { } } +// ServeHTTP function routes all requests for the private webhost server. It is used in the main +// function inside of the http.ListenAndServe() function for the private webhost host. func (priv *PrivateHost) ServeHTTP(w http.ResponseWriter, req *http.Request) { path := req.URL.Path[1:] if len(path) == 0 { @@ -33,7 +35,7 @@ func (priv *PrivateHost) ServeHTTP(w http.ResponseWriter, req *http.Request) { } if priv.Auth != 1 { - routing.HttpThrowStatus(404, w) + routing.HttpThrowStatus(http.StatusUnauthorized, w) logging.Console(logging.PRIVATE_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" rendered a 401 error.") } else { isApi, _ := api.HandleAPI(path, w, req) @@ -51,12 +53,12 @@ func (priv *PrivateHost) ServeHTTP(w http.ResponseWriter, req *http.Request) { logging.Console(logging.PRIVATE_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" rendered a 200 success.") } else { - routing.HttpThrowStatus(404, w) + routing.HttpThrowStatus(http.StatusUnsupportedMediaType, w) logging.Console(logging.PRIVATE_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" content type could not be determined, 404 error.") } } else { - routing.HttpThrowStatus(404, w) + routing.HttpThrowStatus(http.StatusNotFound, w) logging.Console(logging.PRIVATE_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" rendered a 404 error.") }