add PUT method for raw file writes, open a/m/t for writes
This commit is contained in:
parent
4338acd2df
commit
ed0cce92f1
29
main.go
29
main.go
|
|
@ -3,7 +3,7 @@
|
||||||
// Maps HTTP methods to 9P file operations:
|
// Maps HTTP methods to 9P file operations:
|
||||||
//
|
//
|
||||||
// GET /path - read file content or list directory entries
|
// GET /path - read file content or list directory entries
|
||||||
// PUT /path - write request body to file
|
// PUT /path - write request body to file (truncate)
|
||||||
// POST /path - create file (name = last path element)
|
// POST /path - create file (name = last path element)
|
||||||
// DELETE /path - remove file
|
// DELETE /path - remove file
|
||||||
// HEAD /path - stat file (metadata in headers)
|
// HEAD /path - stat file (metadata in headers)
|
||||||
|
|
@ -210,7 +210,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
// Read-only paths — only GET and HEAD allowed.
|
// Read-only paths — only GET and HEAD allowed.
|
||||||
switch first {
|
switch first {
|
||||||
case "", "a", "m", "t", "sk":
|
case "", "sk":
|
||||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||||
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "read-only"})
|
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "read-only"})
|
||||||
return
|
return
|
||||||
|
|
@ -241,6 +241,8 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
handleWrite(w, r, fsys, p)
|
handleWrite(w, r, fsys, p)
|
||||||
|
case http.MethodPut:
|
||||||
|
handlePut(w, r, fsys, p)
|
||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
handleRemove(w, fsys, p)
|
handleRemove(w, fsys, p)
|
||||||
case http.MethodPatch:
|
case http.MethodPatch:
|
||||||
|
|
@ -252,11 +254,12 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var allowedMethods = "GET, HEAD, POST, DELETE, PATCH, OPTIONS"
|
var allowedMethods = "GET, HEAD, PUT, POST, DELETE, PATCH, OPTIONS"
|
||||||
|
|
||||||
var methodDescriptions = map[string]string{
|
var methodDescriptions = map[string]string{
|
||||||
"GET": "read file content or list directory entries",
|
"GET": "read file content or list directory entries",
|
||||||
"HEAD": "stat file (metadata in response headers)",
|
"HEAD": "stat file (metadata in response headers)",
|
||||||
|
"PUT": "write raw body to file (truncate)",
|
||||||
"POST": "write to file or trigger action",
|
"POST": "write to file or trigger action",
|
||||||
"DELETE": "remove file or session",
|
"DELETE": "remove file or session",
|
||||||
"PATCH": "rename via JSON {\"name\": \"newname\"}",
|
"PATCH": "rename via JSON {\"name\": \"newname\"}",
|
||||||
|
|
@ -421,6 +424,26 @@ func handleStat(w http.ResponseWriter, fsys *client.Fsys, p string) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handlePut(w http.ResponseWriter, r *http.Request, fsys *client.Fsys, p string) {
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fid, err := fsys.Open(p, plan9.OWRITE|plan9.OTRUNC)
|
||||||
|
if err != nil {
|
||||||
|
writeJSON(w, http.StatusNotFound, map[string]string{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
n, err := fid.Write(body)
|
||||||
|
fid.Close()
|
||||||
|
if err != nil {
|
||||||
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, map[string]int{"bytes_written": n})
|
||||||
|
}
|
||||||
|
|
||||||
func handleWrite(w http.ResponseWriter, r *http.Request, fsys *client.Fsys, p string) {
|
func handleWrite(w http.ResponseWriter, r *http.Request, fsys *client.Fsys, p string) {
|
||||||
var m map[string]any
|
var m map[string]any
|
||||||
if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue