ReadHeaderTimeout: 10 * time.Second,
MaxHeaderBytes: 10 * (1 << 10),
}
- s.SetKeepAlivesEnabled(false)
http.HandleFunc("/upload/", upload)
if *doUCSPI {
+ s.SetKeepAlivesEnabled(false)
ln := &UCSPI{}
s.ConnState = connStater
err := s.Serve(ln)
package main
import (
+ "io"
"net"
"net/http"
"os"
"time"
)
+var aLongTimeAgo = time.Unix(1, 0)
+
type UCSPIAddr struct {
ip string
port string
func (addr *UCSPIAddr) String() string { return addr.ip + ":" + addr.port }
-type UCSPIConn struct{}
+type UCSPIConn struct {
+ eof chan struct{}
+}
-func (conn *UCSPIConn) Read(b []byte) (int, error) { return os.Stdin.Read(b) }
+type ReadResult struct {
+ n int
+ err error
+}
+
+func (conn *UCSPIConn) Read(b []byte) (int, error) {
+ c := make(chan ReadResult)
+ go func() {
+ n, err := os.Stdin.Read(b)
+ c <- ReadResult{n, err}
+ }()
+ select {
+ case res := <-c:
+ return res.n, res.err
+ case <-conn.eof:
+ return 0, io.EOF
+ }
+}
func (conn *UCSPIConn) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
}
func (conn *UCSPIConn) SetReadDeadline(t time.Time) error {
+ // An ugly hack to forcefully terminate pending read.
+ // net/http calls SetReadDeadline(aLongTimeAgo), but file
+ // descriptors are not capable to exit immediately that way.
+ if t.Equal(aLongTimeAgo) {
+ conn.eof <- struct{}{}
+ }
return os.Stdin.SetReadDeadline(t)
}
return nil, UCSPIAlreadyAccepted{}
}
ucspi.accepted = true
- return &UCSPIConn{}, nil
+ conn := UCSPIConn{eof: make(chan struct{}, 1)}
+ return &conn, nil
}
func (ucspi *UCSPI) Close() error {