More incremental progress.

This commit is contained in:
Ben 2025-07-27 17:06:28 -07:00
parent ab3b46713f
commit ed4bbeb608
Signed by: webmaster
GPG key ID: A5FCBAF34E6E8B50
4 changed files with 107 additions and 6 deletions

View file

@ -4,8 +4,10 @@ package http
import (
"signal-cli-http/conf"
"signal-cli-http/subprocess"
"fmt"
"io"
"log"
"net/http"
)
@ -20,17 +22,43 @@ func StartWebserver(port int) {
func getRoot(w http.ResponseWriter, r *http.Request) {
// Check that Authentication header exists
authArr, ok := r.Header["Authentication"]
if (!ok) || (len(authArr) == 0) {w.WriteHeader(400); return}
if (!ok) || (len(authArr) == 0) {
w.WriteHeader(400);
w.Write([]byte("Authentication header missing\n"))
return;
}
bearer := authArr[0];
// Check that the request is allowed for the path
if !conf.GlobalConfig.ValidateBearerKey(bearer, r.URL.Path) {
w.WriteHeader(403);
w.Write([]byte("Bearer key not whitelisted for this path\n"))
return;
}
log.Default().Print("HTTP Request: ", bearer, " " , r.URL.Path)
// OK authentication wise
w.WriteHeader(200);
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(500);
w.Write([]byte("Error reading body\n"))
return;
}
// Call subprocess
status, bodyContent, err := subprocess.Run(r.URL.Path, body)
// Error
if err != nil {
w.WriteHeader(500);
w.Write([]byte("Internal server error: " + err.Error() + "\n"));
return
}
// Respond to client with status
w.WriteHeader(status);
w.Write(bodyContent);
// Log the request
log.Default().Print("HTTP Request: ", bearer, " " , r.URL.Path, " ", status)
}