-/*
-uploader -- simplest form file uploader
-Copyright (C) 2018-2021 Sergey Matveev <stargrave@stargrave.org>
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, version 3 of the License.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
+// uploader -- simplest form file uploader
+// Copyright (C) 2018-2024 Sergey Matveev <stargrave@stargrave.org>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, version 3 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
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{}
+}
+
+type ReadResult struct {
+ n int
+ err error
+}
-func (conn *UCSPIConn) Read(b []byte) (int, error) { return os.Stdin.Read(b) }
+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) }
if err := os.Stdin.Close(); err != nil {
return err
}
- return os.Stdin.Close()
+ return os.Stdout.Close()
}
func (conn *UCSPIConn) LocalAddr() net.Addr {
}
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 {