Skip to content

Commit a6537de

Browse files
committed
fix(handler): nil next
1 parent 3f691ea commit a6537de

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

go.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ func NewWasmHandlerGo(modulepath string, moduleConfig any, poolConfiguration map
102102
}
103103

104104
return NewWasmHandlerInstance(func(ctx context.Context, next Handler) Handler {
105-
mw.NewHandler(ctx, wazemmesToHTTPHandler(next))
105+
return HandlerFunc(func(rw http.ResponseWriter, req *http.Request) error {
106+
mw.NewHandler(ctx, wazemmesToHTTPHandler(next)).ServeHTTP(rw, req)
106107

107-
return nil
108+
return nil
109+
})
108110
}, poolConfiguration, logger)
109111
}

handler.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,18 @@ func (w *WasmHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, next H
5757
return errors.New("impossible to cast the borrowed object into a WASM HTTP handler")
5858
}
5959

60-
err = handler(rq.Context(), next).ServeHTTP(rw, rq)
60+
result := handler(rq.Context(), next)
61+
if result != nil {
62+
err = result.ServeHTTP(rw, rq)
63+
}
64+
6165
if err != nil {
6266
return err
6367
}
64-
65-
return next.ServeHTTP(rw, rq)
68+
69+
if next != nil {
70+
return next.ServeHTTP(rw, rq)
71+
}
72+
73+
return nil
6674
}

php.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func (h *phpWASMHandler) getScriptPath(urlPath string) string {
3232
}
3333

3434
func (h *phpWASMHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) error {
35-
fmt.Println("PHP ServeHTTP handler")
3635
scriptPath := h.getScriptPath(r.URL.Path)
3736

3837
// Create a pipe to capture PHP output

writer.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package wazemmes
33
import (
44
"bytes"
55
"net/http"
6+
"strings"
67
)
78

89
type writer struct {
9-
status int
10-
buf *bytes.Buffer
11-
res http.ResponseWriter
12-
req *http.Request
10+
written bool
11+
status int
12+
buf *bytes.Buffer
13+
res http.ResponseWriter
14+
req *http.Request
1315
}
1416

1517
func BuildWriter(res http.ResponseWriter, req *http.Request) *writer {
@@ -26,6 +28,10 @@ func (w *writer) Header() http.Header {
2628
}
2729

2830
func (w *writer) Write(b []byte) (int, error) {
31+
if strings.HasPrefix(string(b), "module closed with exit_code") {
32+
return len(b), nil
33+
}
34+
2935
w.buf.Reset()
3036

3137
return w.buf.Write(b)
@@ -36,6 +42,11 @@ func (w *writer) WriteHeader(status int) {
3642
}
3743

3844
func (w *writer) Flush() {
45+
if w.written {
46+
return
47+
}
48+
49+
w.written = true
3950
w.res.WriteHeader(w.status)
4051
_, _ = w.res.Write(w.buf.Bytes())
4152
}

0 commit comments

Comments
 (0)