Skip to content

Commit

Permalink
Return HTTP status code accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
rafael-santiago committed Dec 8, 2024
1 parent fd87dc7 commit c01c563
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/internal/actions/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package actions
import (
"internal/vars"
"net/url"
"net/http"
)

type EutherpeActionFunc func(eutherpeInstance *vars.EutherpeVars, userData *url.Values) error
Expand Down Expand Up @@ -240,6 +241,7 @@ func GetContentTypeByActionId(userData *url.Values) string {
}

func GetVDocByActionId(userData *url.Values, eutherpeVars *vars.EutherpeVars) string {
httpStatus := http.StatusNotFound
switch userData.Get(vars.EutherpeActionId) {
case vars.EutherpeMusicRemoveId,
vars.EutherpeMusicMoveUpId,
Expand Down Expand Up @@ -288,10 +290,13 @@ func GetVDocByActionId(userData *url.Values, eutherpeVars *vars.EutherpeVars) st
vars.EutherpeSettingsSetWLANCredentialsId,
vars.EutherpeSettingsSetHostNameId,
vars.EutherpeSettingsPowerOffId,
vars.EutherpeSettingsRebootId:
vars.EutherpeSettingsRebootId,
vars.EutherpeSetCurrentConfigId:
httpStatus = http.StatusOK
return eutherpeVars.HTTPd.IndexHTML

case vars.EutherpeAuthenticateId:
httpStatus = http.StatusOK
if eutherpeVars.LastError == nil {
return "<html><script>window.location=\"{{.URL-SCHEMA}}://{{.EUTHERPE-ADDR}}/eutherpe\"</script></html>"
} else {
Expand All @@ -300,8 +305,9 @@ func GetVDocByActionId(userData *url.Values, eutherpeVars *vars.EutherpeVars) st
break

case vars.EutherpePlayerStatusId:
httpStatus = http.StatusOK
return vars.EutherpeTemplateNeedlePlayerStatus
}

eutherpeVars.HTTPd.ResponseWriter.WriteHeader(httpStatus)
return eutherpeVars.HTTPd.ErrorHTML
}
2 changes: 2 additions & 0 deletions src/internal/vars/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"fmt"
"strings"
"net"
"net/http"
"time"
"crypto/sha256"
)
Expand All @@ -48,6 +49,7 @@ type EutherpeVars struct {
ErrorHTML string
LoginHTML string
RequestedByHostName bool
ResponseWriter http.ResponseWriter
}
MDNS struct {
Hosts []mdns.MDNSHost
Expand Down
5 changes: 5 additions & 0 deletions src/internal/webui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func eutherpeHTTPd(eutherpeHTTPHandler EutherpeHTTPHandler,
func (ehh *EutherpeHTTPHandler) handler(w http.ResponseWriter, r *http.Request) {
ehh.requestTurnstile.Lock()
defer ehh.requestTurnstile.Unlock()
ehh.eutherpeVars.HTTPd.ResponseWriter = w
var templatedOutput string
if len(ehh.eutherpeVars.HostName) > 0 &&
strings.HasPrefix(r.Host, ehh.eutherpeVars.HostName) {
Expand Down Expand Up @@ -153,13 +154,15 @@ func (ehh *EutherpeHTTPHandler) handler(w http.ResponseWriter, r *http.Request)
}
contentType = actions.GetContentTypeByActionId(&r.Form)
} else {
w.WriteHeader(http.StatusNotImplemented)
templatedOutput = ehh.eutherpeVars.HTTPd.ErrorHTML
ehh.eutherpeVars.LastError = fmt.Errorf("501 Not Implemented")
}
w.Header().Set("content-type", contentType)
break
case "/eutherpe-auth":
if !ehh.eutherpeVars.HTTPd.Authenticated {
w.WriteHeader(http.StatusSeeOther)
templatedOutput = ehh.eutherpeVars.HTTPd.ErrorHTML
ehh.eutherpeVars.LastError = fmt.Errorf("303 See Other")
} else if r.Method == "GET" {
Expand All @@ -172,6 +175,7 @@ func (ehh *EutherpeHTTPHandler) handler(w http.ResponseWriter, r *http.Request)
r.URL.Path = "/eutherpe"
template = vars.EutherpeIndexTemplate
} else {
w.WriteHeader(http.StatusNotImplemented)
templatedOutput = ehh.eutherpeVars.HTTPd.ErrorHTML
ehh.eutherpeVars.LastError = fmt.Errorf("501 Not Implemented")
}
Expand Down Expand Up @@ -202,6 +206,7 @@ func (ehh *EutherpeHTTPHandler) processAction(actionHandler actions.EutherpeActi
func (ehh *EutherpeHTTPHandler) processGET(w *http.ResponseWriter, r *http.Request) string {
vdoc := r.URL.Path
if !ehh.isPubFile(vdoc) {
(*w).WriteHeader(http.StatusForbidden)
ehh.eutherpeVars.LastError = fmt.Errorf("403 Forbidden")
return ehh.eutherpeVars.HTTPd.ErrorHTML
}
Expand Down

0 comments on commit c01c563

Please sign in to comment.