ollie-httpgw/ninep.go

435 lines
11 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path"
"strings"
"9fans.net/go/plan9"
"9fans.net/go/plan9/client"
)
func dial() (*client.Fsys, error) {
if *addr != "" {
n := *network
if n == "" {
n = "tcp"
}
return client.Mount(n, *addr)
}
return client.MountService(*service)
}
type dirEntry struct {
Name string `json:"name"`
Size uint64 `json:"size"`
Mode uint32 `json:"mode"`
Mtime uint32 `json:"mtime"`
IsDir bool `json:"is_dir"`
}
func dirToEntry(d *plan9.Dir) dirEntry {
return dirEntry{
Name: d.Name,
Size: d.Length,
Mode: uint32(d.Mode),
Mtime: d.Mtime,
IsDir: d.Mode&plan9.DMDIR != 0,
}
}
func setStatHeaders(w http.ResponseWriter, d *plan9.Dir) {
w.Header().Set("X-9p-Name", d.Name)
w.Header().Set("X-9p-Size", fmt.Sprintf("%d", d.Length))
w.Header().Set("X-9p-Mode", fmt.Sprintf("%o", d.Mode))
w.Header().Set("X-9p-Mtime", fmt.Sprintf("%d", d.Mtime))
if d.Mode&plan9.DMDIR != 0 {
w.Header().Set("X-9p-Type", "dir")
} else {
w.Header().Set("X-9p-Type", "file")
}
}
func ninepHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/open" {
handleOpen(w, r)
return
}
fsys, err := dial()
if err != nil {
writeJSON(w, http.StatusBadGateway, map[string]string{"error": "9p connect: " + err.Error()})
return
}
defer fsys.Close()
p := strings.TrimPrefix(r.URL.Path, "/")
// Internal paths not exposed via HTTP.
first, _, _ := strings.Cut(p, "/")
switch first {
case "p", "help", "tr", "u", "x", "tmp":
writeJSON(w, http.StatusNotFound, map[string]string{"error": "not found"})
return
}
// Read-only paths — only GET and HEAD allowed.
switch first {
case "", "sk":
if r.Method != http.MethodGet && r.Method != http.MethodHead {
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "read-only"})
return
}
}
base := path.Base(p)
switch base {
case "systemprompt", "statewait":
writeJSON(w, http.StatusNotFound, map[string]string{"error": "not found"})
return
case "new":
if r.Method == http.MethodPost {
handleWrite(w, r, fsys, p)
return
}
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
return
}
switch r.Method {
case http.MethodGet:
handleRead(w, fsys, p)
case http.MethodHead:
handleStat(w, fsys, p)
case http.MethodPost:
if base == "chat" {
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "chat is read-only"})
return
}
handleWrite(w, r, fsys, p)
case http.MethodPut:
handlePut(w, r, fsys, p)
case http.MethodDelete:
handleRemove(w, fsys, p)
case http.MethodPatch:
handleWstat(w, r, fsys, p)
case http.MethodOptions:
handleNinepOptions(w, r, p)
default:
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
}
}
var allowedMethods = "GET, HEAD, PUT, POST, DELETE, PATCH, OPTIONS"
var methodDescriptions = map[string]string{
"GET": "read file content or list directory entries",
"HEAD": "stat file (metadata in response headers)",
"PUT": "write raw body to file (truncate)",
"POST": "write to file or trigger action",
"DELETE": "remove file or session",
"PATCH": "rename via JSON {\"name\": \"newname\"}",
"OPTIONS": "describe available operations for this path",
}
func handleNinepOptions(w http.ResponseWriter, _ *http.Request, p string) {
w.Header().Set("Allow", allowedMethods)
writeJSON(w, http.StatusOK, map[string]any{
"path": "/" + p,
"methods": methodDescriptions,
})
}
func handleRead(w http.ResponseWriter, fsys *client.Fsys, p string) {
d, err := fsys.Stat(p)
if err != nil {
writeJSON(w, http.StatusNotFound, map[string]string{"error": err.Error()})
return
}
setStatHeaders(w, d)
if d.Mode&plan9.DMDIR != 0 {
fid, err := fsys.Open(p, plan9.OREAD)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
defer fid.Close()
dirs, err := fid.Dirreadall()
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
if p == "s" {
var ids []string
for _, de := range dirs {
if de.Mode&plan9.DMDIR != 0 {
ids = append(ids, de.Name)
}
}
writeJSON(w, http.StatusOK, ids)
return
}
entries := make([]dirEntry, len(dirs))
for i, de := range dirs {
entries[i] = dirToEntry(de)
}
writeJSON(w, http.StatusOK, entries)
return
}
fid, err := fsys.Open(p, plan9.OREAD)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
defer fid.Close()
content, err := io.ReadAll(fid)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
if p == "models" {
writeJSON(w, http.StatusOK, parseModels(content))
return
}
if p == "s/idx" {
writeJSON(w, http.StatusOK, parseIdx(content))
return
}
if path.Base(p) == "chat" {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write(content)
return
}
if path.Base(p) == "cfg" {
m := parseContent(content, "cfg")
if mm, ok := m.(map[string]string); ok {
// Inject state from sibling file.
stateFile := path.Dir(p) + "/state"
if sf, err := fsys.Open(stateFile, plan9.OREAD); err == nil {
if sb, err := io.ReadAll(sf); err == nil {
mm["state"] = strings.TrimSpace(string(sb))
}
sf.Close()
}
}
writeJSON(w, http.StatusOK, m)
return
}
writeJSON(w, http.StatusOK, parseContent(content, path.Base(p)))
}
type idxEntry struct {
ID string `json:"id"`
State string `json:"state"`
CWD string `json:"cwd"`
Backend string `json:"backend"`
Model string `json:"model"`
}
type modelsEntry struct {
Backend string `json:"backend"`
Models []string `json:"models"`
}
func parseModels(data []byte) []modelsEntry {
groups := make(map[string][]string)
var order []string
for line := range strings.SplitSeq(strings.TrimRight(string(data), "\n"), "\n") {
if line == "" {
continue
}
b, m, ok := strings.Cut(line, "\t")
if !ok {
continue
}
if _, exists := groups[b]; !exists {
order = append(order, b)
}
groups[b] = append(groups[b], m)
}
out := make([]modelsEntry, len(order))
for i, b := range order {
out[i] = modelsEntry{Backend: b, Models: groups[b]}
}
return out
}
func parseIdx(data []byte) []idxEntry {
var entries []idxEntry
for line := range strings.SplitSeq(strings.TrimRight(string(data), "\n"), "\n") {
if line == "" {
continue
}
fields := strings.SplitN(line, "\t", 5)
if len(fields) != 5 {
continue
}
entries = append(entries, idxEntry{
ID: fields[0],
State: fields[1],
CWD: fields[2],
Backend: fields[3],
Model: fields[4],
})
}
return entries
}
// parseContent detects the wire format and returns the appropriate Go value:
// - key=value lines → map[string]string
// - tab-separated records → [][]string
// - multiple newline-separated values → []string
// - single value → map[string]string{name: value}
func parseContent(data []byte, name string) any {
s := strings.TrimRight(string(data), "\n")
if s == "" {
return map[string]string{}
}
first, _, _ := strings.Cut(s, "\n")
if strings.Contains(first, "=") {
m := make(map[string]string)
for line := range strings.SplitSeq(s, "\n") {
if line == "" {
continue
}
k, v, _ := strings.Cut(line, "=")
m[k] = v
}
return m
}
if strings.Contains(first, "\t") {
var rows [][]string
for line := range strings.SplitSeq(s, "\n") {
if line != "" {
rows = append(rows, strings.Split(line, "\t"))
}
}
return rows
}
var lines []string
for line := range strings.SplitSeq(s, "\n") {
lines = append(lines, line)
}
if len(lines) == 1 {
return map[string]string{name: lines[0]}
}
return lines
}
// serializeKV serializes a map to newline-separated key=value lines.
func serializeKV(m map[string]any) []byte {
var sb strings.Builder
for k, v := range m {
fmt.Fprintf(&sb, "%s=%v\n", k, v)
}
return []byte(sb.String())
}
func handleStat(w http.ResponseWriter, fsys *client.Fsys, p string) {
d, err := fsys.Stat(p)
if err != nil {
writeJSON(w, http.StatusNotFound, map[string]string{"error": err.Error()})
return
}
setStatHeaders(w, d)
w.Header().Set("Content-Type", "application/json")
}
func handleOpen(w http.ResponseWriter, r *http.Request) {
p := r.URL.Query().Get("path")
if p == "" {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "missing path"})
return
}
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "xdg-open"
}
if err := exec.Command(editor, p).Start(); err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
writeJSON(w, http.StatusOK, map[string]string{"opened": p})
}
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) {
var m map[string]any
if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
// If the body has a single key matching the file name, write just the value.
// Otherwise serialize as key=value lines (e.g. for "new").
var payload []byte
base := path.Base(p)
if len(m) == 1 {
if v, ok := m[base]; ok {
payload = []byte(fmt.Sprintf("%v", v))
}
}
if payload == nil {
payload = serializeKV(m)
}
fid, err := fsys.Open(p, plan9.OWRITE)
if err != nil {
writeJSON(w, http.StatusNotFound, map[string]string{"error": err.Error()})
return
}
n, err := fid.Write(payload)
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 handleRemove(w http.ResponseWriter, fsys *client.Fsys, p string) {
if err := fsys.Remove(p); err != nil {
writeJSON(w, http.StatusNotFound, map[string]string{"error": err.Error()})
return
}
w.WriteHeader(http.StatusNoContent)
}
func handleWstat(w http.ResponseWriter, r *http.Request, fsys *client.Fsys, p string) {
var req struct {
Name string `json:"name"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
d := &plan9.Dir{Name: req.Name}
if err := fsys.Wstat(p, d); err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
w.WriteHeader(http.StatusNoContent)
}