11
All checks were successful
Build Multi-Platform Binaries / build-frontend (push) Successful in 32s
Build Multi-Platform Binaries / build-binaries (amd64, linux, client, true) (push) Successful in 1m22s
Build Multi-Platform Binaries / build-binaries (amd64, darwin, server, false) (push) Successful in 1m32s
Build Multi-Platform Binaries / build-binaries (amd64, windows, client, true) (push) Successful in 1m19s
Build Multi-Platform Binaries / build-binaries (amd64, linux, server, true) (push) Successful in 1m33s
Build Multi-Platform Binaries / build-binaries (arm, 7, linux, client, true) (push) Successful in 1m59s
Build Multi-Platform Binaries / build-binaries (amd64, windows, server, true) (push) Successful in 2m35s
Build Multi-Platform Binaries / build-binaries (arm64, darwin, server, false) (push) Successful in 1m40s
Build Multi-Platform Binaries / build-binaries (arm, 7, linux, server, true) (push) Successful in 2m23s
Build Multi-Platform Binaries / build-binaries (arm64, linux, client, true) (push) Successful in 1m48s
Build Multi-Platform Binaries / build-binaries (arm64, linux, server, true) (push) Successful in 1m58s
Build Multi-Platform Binaries / build-binaries (arm64, windows, server, false) (push) Successful in 1m16s

This commit is contained in:
2026-01-11 23:27:48 +08:00
parent 431780774f
commit 3fe3686e29

View File

@@ -1,6 +1,7 @@
package script
import (
"bufio"
"encoding/json"
"fmt"
"io"
@@ -8,6 +9,7 @@ import (
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
@@ -396,15 +398,63 @@ func (p *JSPlugin) createHttpAPI() map[string]interface{} {
func (p *JSPlugin) httpServe(conn net.Conn, handler goja.Callable) {
// 注意不要在这里关闭连接HandleConn 会负责关闭
buf := make([]byte, 4096)
n, err := conn.Read(buf)
// Use bufio to read the request properly
reader := bufio.NewReader(conn)
// 1. Read Request Line
line, err := reader.ReadString('\n')
if err != nil {
fmt.Printf("[JS:%s] httpServe read error: %v\n", p.name, err)
return
}
fmt.Printf("[JS:%s] httpServe read %d bytes\n", p.name, n)
line = strings.TrimSpace(line)
parts := strings.Split(line, " ")
if len(parts) < 2 {
fmt.Printf("[JS:%s] Invalid request line: %s\n", p.name, line)
return
}
method := parts[0]
path := parts[1]
req := parseHTTPRequest(buf[:n])
fmt.Printf("[JS:%s] Request: %s %s\n", p.name, method, path)
// 2. Read Headers
headers := make(map[string]string)
contentLength := 0
for {
hLine, err := reader.ReadString('\n')
if err != nil {
break
}
hLine = strings.TrimSpace(hLine)
if hLine == "" {
break
}
if idx := strings.Index(hLine, ":"); idx > 0 {
key := strings.TrimSpace(hLine[:idx])
val := strings.TrimSpace(hLine[idx+1:])
headers[strings.ToLower(key)] = val
if strings.ToLower(key) == "content-length" {
contentLength, _ = strconv.Atoi(val)
}
}
}
// 3. Read Body
body := ""
if contentLength > 0 {
bodyBuf := make([]byte, contentLength)
if _, err := io.ReadFull(reader, bodyBuf); err == nil {
body = string(bodyBuf)
}
}
req := map[string]interface{}{
"method": method,
"path": path,
"headers": headers,
"body": body,
}
// 调用 JS handler 函数
result, err := handler(goja.Undefined(), p.vm.ToValue(req))
@@ -452,30 +502,9 @@ func (p *JSPlugin) httpSendFile(conn net.Conn, filePath string) {
io.Copy(conn, f)
}
// parseHTTPRequest 解析 HTTP 请求
// parseHTTPRequest is deprecated, logic moved to httpServe
func parseHTTPRequest(data []byte) map[string]interface{} {
lines := string(data)
req := map[string]interface{}{
"method": "GET",
"path": "/",
"body": "",
}
// 解析请求行
if idx := indexOf(lines, " "); idx > 0 {
req["method"] = lines[:idx]
rest := lines[idx+1:]
if idx2 := indexOf(rest, " "); idx2 > 0 {
req["path"] = rest[:idx2]
}
}
// 解析 body
if idx := indexOf(lines, "\r\n\r\n"); idx > 0 {
req["body"] = lines[idx+4:]
}
return req
return nil
}
// writeHTTPResponse 写入 HTTP 响应