1
0
mirror of https://github.com/lxc/incus.git synced 2026-02-05 09:46:19 +01:00

cmd/incus_agent: Replace gorilla/mux with http.ServeMux

Signed-off-by: 0xk1f0 <dev@k1f0.dev>
This commit is contained in:
0xk1f0
2025-07-07 17:30:12 +02:00
parent 7425408ede
commit 0c5d8f90cc
3 changed files with 25 additions and 38 deletions

View File

@@ -4,14 +4,11 @@ import (
"fmt"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/gorilla/mux"
incus "github.com/lxc/incus/v6/client"
"github.com/lxc/incus/v6/internal/server/daemon"
"github.com/lxc/incus/v6/internal/server/device/config"
@@ -86,8 +83,8 @@ var DevIncusConfigGet = devIncusHandler{"/1.0/config", func(d *Daemon, w http.Re
}}
var DevIncusConfigKeyGet = devIncusHandler{"/1.0/config/{key}", func(d *Daemon, w http.ResponseWriter, r *http.Request) *devIncusResponse {
key, err := url.PathUnescape(mux.Vars(r)["key"])
if err != nil {
key := r.PathValue("key")
if key == "" {
return &devIncusResponse{"bad request", http.StatusBadRequest, "raw"}
}
@@ -251,8 +248,7 @@ func hoistReq(f func(*Daemon, http.ResponseWriter, *http.Request) *devIncusRespo
}
func devIncusAPI(d *Daemon) http.Handler {
router := mux.NewRouter()
router.UseEncodedPath() // Allow encoded values in path segments.
router := http.NewServeMux()
for _, handler := range handlers {
router.HandleFunc(handler.path, hoistReq(handler.f, d))

View File

@@ -5,13 +5,10 @@ import (
"fmt"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/lxc/incus/v6/internal/jmap"
"github.com/lxc/incus/v6/internal/server/operations"
"github.com/lxc/incus/v6/internal/server/response"
@@ -44,9 +41,9 @@ var operationWait = APIEndpoint{
}
func operationDelete(d *Daemon, r *http.Request) response.Response {
id, err := url.PathUnescape(mux.Vars(r)["id"])
if err != nil {
return response.SmartError(err)
id := r.PathValue("id")
if id == "" {
return response.BadRequest(fmt.Errorf("Failed to extract operation ID from URL"))
}
// First check if the query is for a local operation from this node
@@ -64,9 +61,9 @@ func operationDelete(d *Daemon, r *http.Request) response.Response {
}
func operationGet(d *Daemon, r *http.Request) response.Response {
id, err := url.PathUnescape(mux.Vars(r)["id"])
if err != nil {
return response.SmartError(err)
id := r.PathValue("id")
if id == "" {
return response.BadRequest(fmt.Errorf("Failed to extract operation ID from URL"))
}
var body *api.Operation
@@ -153,9 +150,9 @@ func operationsGet(d *Daemon, r *http.Request) response.Response {
}
func operationWebsocketGet(d *Daemon, r *http.Request) response.Response {
id, err := url.PathUnescape(mux.Vars(r)["id"])
if err != nil {
return response.SmartError(err)
id := r.PathValue("id")
if id == "" {
return response.BadRequest(fmt.Errorf("Failed to extract operation ID from URL"))
}
// First check if the query is for a local operation from this node
@@ -168,17 +165,21 @@ func operationWebsocketGet(d *Daemon, r *http.Request) response.Response {
}
func operationWaitGet(d *Daemon, r *http.Request) response.Response {
id, err := url.PathUnescape(mux.Vars(r)["id"])
if err != nil {
return response.InternalError(fmt.Errorf("Failed to extract operation ID from URL: %w", err))
id := r.PathValue("id")
if id == "" {
return response.BadRequest(fmt.Errorf("Failed to extract operation ID from URL"))
}
timeoutSecs := -1
if r.FormValue("timeout") != "" {
timeoutSecs, err = strconv.Atoi(r.FormValue("timeout"))
var err error
var timeoutSecs int
timeout := r.FormValue("timeout")
if timeout != "" {
timeoutSecs, err = strconv.Atoi(timeout)
if err != nil {
return response.InternalError(fmt.Errorf("Failed to extract operation wait timeout from URL: %w", err))
}
} else {
timeoutSecs = -1
}
var ctx context.Context

View File

@@ -9,8 +9,6 @@ import (
"io"
"net/http"
"github.com/gorilla/mux"
internalIO "github.com/lxc/incus/v6/internal/io"
"github.com/lxc/incus/v6/internal/server/response"
localUtil "github.com/lxc/incus/v6/internal/server/util"
@@ -18,9 +16,7 @@ import (
)
func restServer(tlsConfig *tls.Config, cert *x509.Certificate, debug bool, d *Daemon) *http.Server {
router := mux.NewRouter()
router.StrictSlash(false) // Don't redirect to URL with trailing slash.
router.UseEncodedPath() // Allow encoded values in path segments.
router := http.NewServeMux()
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
@@ -34,7 +30,7 @@ func restServer(tlsConfig *tls.Config, cert *x509.Certificate, debug bool, d *Da
return &http.Server{Handler: router, TLSConfig: tlsConfig}
}
func createCmd(restAPI *mux.Router, version string, c APIEndpoint, cert *x509.Certificate, debug bool, d *Daemon) {
func createCmd(restAPI *http.ServeMux, version string, c APIEndpoint, cert *x509.Certificate, debug bool, d *Daemon) {
var uri string
if c.Path == "" {
uri = fmt.Sprintf("/%s", version)
@@ -42,7 +38,7 @@ func createCmd(restAPI *mux.Router, version string, c APIEndpoint, cert *x509.Ce
uri = fmt.Sprintf("/%s/%s", version, c.Path)
}
route := restAPI.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
restAPI.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if !authenticate(r, cert) {
@@ -101,12 +97,6 @@ func createCmd(restAPI *mux.Router, version string, c APIEndpoint, cert *x509.Ce
}
}
})
// If the endpoint has a canonical name then record it so it can be used to build URLS
// and accessed in the context of the request by the handler function.
if c.Name != "" {
route.Name(c.Name)
}
}
func authenticate(r *http.Request, cert *x509.Certificate) bool {