Skip to content

Commit

Permalink
WIP CGI Streaming, needs testing. A looot of testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
vifino committed Jan 9, 2016
1 parent fe39618 commit d37fe86
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 35 deletions.
64 changes: 32 additions & 32 deletions modules/glue/generated_lua.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 40 additions & 3 deletions modules/middleware/cgi.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
package middleware

import (
"fmt"
"github.com/gin-gonic/gin"
"io"
"net/http"
"net/http/cgi"
)

// To allow streaming.
type flushfields struct {
f http.Flusher
orig_writer io.Writer
}
type flushWriter struct {
http.ResponseWriter
flushfields
}

func (fw flushWriter) Write(p []byte) (n int, err error) {
n, err = fw.orig_writer.Write(p)
fmt.Println("Flushing or nah?")
if fw.f != nil {
fmt.Println("Flushin!")
fw.f.Flush()
}
return
}

func CGI(path, dir string, args, env []string) func(*gin.Context) {
handler := cgi.Handler{
Path: path,
Expand All @@ -13,7 +36,11 @@ func CGI(path, dir string, args, env []string) func(*gin.Context) {
Env: env,
}
return func(c *gin.Context) {
handler.ServeHTTP(c.Writer, c.Request)
fw := flushWriter{c.Writer, flushfields{orig_writer: c.Writer}}
if f, ok := c.Writer.(http.Flusher); ok {
fw.f = f
}
handler.ServeHTTP(fw, c.Request)
}
}

Expand All @@ -26,7 +53,12 @@ func CGI_Dynamic(path, dir string, args, env []string) func(*gin.Context) {
Args: append(args, c.Request.URL.Path),
Env: append(append(env, "SCRIPT_FILENAME="+dir+c.Request.URL.Path), "SCRIPT_NAME="+c.Request.URL.Path),
}
handler.ServeHTTP(c.Writer, c.Request)
fw := flushWriter{c.Writer, flushfields{orig_writer: c.Writer}}
if f, ok := c.Writer.(http.Flusher); ok {

fw.f = f
}
handler.ServeHTTP(fw, c.Request)
}
} else {
return func(c *gin.Context) {
Expand All @@ -36,7 +68,12 @@ func CGI_Dynamic(path, dir string, args, env []string) func(*gin.Context) {
Args: append(args, c.Request.URL.Path),
Env: append(append(env, "SCRIPT_FILENAME="+dir+c.Request.URL.Path), "SCRIPT_NAME="+c.Request.URL.Path),
}
handler.ServeHTTP(c.Writer, c.Request)
fw := flushWriter{c.Writer, flushfields{orig_writer: c.Writer}}
if f, ok := c.Writer.(http.Flusher); ok {
fmt.Println("Flusher found! Yay!")
fw.f = f
}
handler.ServeHTTP(fw, c.Request)
}
}
}

0 comments on commit d37fe86

Please sign in to comment.