mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
About 57 handlers decoded request bodies with no size limit, and the few that had one each picked their own. They now go through decodeJSON with a 1 MiB default; handlers that need more keep their larger limit. An oversized body gets 413 instead of whatever the handler returned for bad JSON (MCP and plugin requests used to answer 400). Every other decode error keeps the handler's existing status and message.
157 lines
3.6 KiB
Go
157 lines
3.6 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"skillshare/internal/config"
|
|
)
|
|
|
|
type hubConfigResponse struct {
|
|
Hubs []hubEntryJSON `json:"hubs"`
|
|
Default string `json:"default"`
|
|
}
|
|
|
|
type hubEntryJSON struct {
|
|
Label string `json:"label"`
|
|
URL string `json:"url"`
|
|
BuiltIn bool `json:"builtIn,omitempty"`
|
|
}
|
|
|
|
func (s *Server) handleGetHubSaved(w http.ResponseWriter, r *http.Request) {
|
|
// Snapshot config under RLock, then release before I/O.
|
|
s.mu.RLock()
|
|
hub := s.hubConfig()
|
|
s.mu.RUnlock()
|
|
resp := hubConfigResponse{
|
|
Default: hub.Default,
|
|
Hubs: make([]hubEntryJSON, 0, len(hub.Hubs)),
|
|
}
|
|
for _, h := range hub.Hubs {
|
|
resp.Hubs = append(resp.Hubs, hubEntryJSON{
|
|
Label: h.Label,
|
|
URL: h.URL,
|
|
BuiltIn: h.BuiltIn,
|
|
})
|
|
}
|
|
writeJSON(w, resp)
|
|
}
|
|
|
|
func (s *Server) handlePutHubSaved(w http.ResponseWriter, r *http.Request) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
start := time.Now()
|
|
|
|
var req hubConfigResponse
|
|
if err := decodeJSON(w, r, &req, defaultJSONBodyLimit); err != nil {
|
|
if !errors.Is(err, errBodyTooLarge) {
|
|
writeError(w, http.StatusBadRequest, "invalid JSON: "+err.Error())
|
|
}
|
|
return
|
|
}
|
|
|
|
hubCfg := config.HubConfig{
|
|
Default: req.Default,
|
|
Hubs: make([]config.HubEntry, 0, len(req.Hubs)),
|
|
}
|
|
for _, h := range req.Hubs {
|
|
hubCfg.Hubs = append(hubCfg.Hubs, config.HubEntry{
|
|
Label: h.Label,
|
|
URL: h.URL,
|
|
BuiltIn: h.BuiltIn,
|
|
})
|
|
}
|
|
|
|
s.setHubConfig(hubCfg)
|
|
if err := s.saveAndReloadConfig(); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
s.writeOpsLog("hub-save", "ok", start, map[string]any{"count": len(hubCfg.Hubs)}, "")
|
|
writeJSON(w, map[string]bool{"success": true})
|
|
}
|
|
|
|
func (s *Server) handlePostHubSaved(w http.ResponseWriter, r *http.Request) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
start := time.Now()
|
|
|
|
var req struct {
|
|
Label string `json:"label"`
|
|
URL string `json:"url"`
|
|
}
|
|
if err := decodeJSON(w, r, &req, defaultJSONBodyLimit); err != nil {
|
|
if !errors.Is(err, errBodyTooLarge) {
|
|
writeError(w, http.StatusBadRequest, "invalid JSON: "+err.Error())
|
|
}
|
|
return
|
|
}
|
|
|
|
hubCfg := s.hubConfig()
|
|
if err := hubCfg.AddHub(config.HubEntry{Label: req.Label, URL: req.URL}); err != nil {
|
|
writeError(w, http.StatusConflict, err.Error())
|
|
return
|
|
}
|
|
|
|
s.setHubConfig(*hubCfg)
|
|
if err := s.saveAndReloadConfig(); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
s.writeOpsLog("hub-add", "ok", start, map[string]any{"label": req.Label}, "")
|
|
writeJSON(w, map[string]bool{"success": true})
|
|
}
|
|
|
|
func (s *Server) handleDeleteHubSaved(w http.ResponseWriter, r *http.Request) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
start := time.Now()
|
|
|
|
label := strings.TrimSpace(r.PathValue("label"))
|
|
if label == "" {
|
|
writeError(w, http.StatusBadRequest, "label is required")
|
|
return
|
|
}
|
|
|
|
hubCfg := s.hubConfig()
|
|
if err := hubCfg.RemoveHub(label); err != nil {
|
|
writeError(w, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
|
|
s.setHubConfig(*hubCfg)
|
|
if err := s.saveAndReloadConfig(); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
s.writeOpsLog("hub-remove", "ok", start, map[string]any{"label": label}, "")
|
|
writeJSON(w, map[string]bool{"success": true})
|
|
}
|
|
|
|
// hubConfig returns a copy of the current hub config.
|
|
func (s *Server) hubConfig() *config.HubConfig {
|
|
if s.IsProjectMode() {
|
|
h := s.projectCfg.Hub
|
|
return &h
|
|
}
|
|
h := s.cfg.Hub
|
|
return &h
|
|
}
|
|
|
|
// setHubConfig sets the hub config on the appropriate config object.
|
|
func (s *Server) setHubConfig(h config.HubConfig) {
|
|
if s.IsProjectMode() {
|
|
s.projectCfg.Hub = h
|
|
} else {
|
|
s.cfg.Hub = h
|
|
}
|
|
}
|