2 uploader -- simplest form file uploader
3 Copyright (C) 2018-2022 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 var aLongTimeAgo = time.Unix(1, 0)
30 type UCSPIAddr struct {
35 func (addr *UCSPIAddr) Network() string { return "tcp" }
37 func (addr *UCSPIAddr) String() string { return addr.ip + ":" + addr.port }
39 type UCSPIConn struct {
43 type ReadResult struct {
48 func (conn *UCSPIConn) Read(b []byte) (int, error) {
49 c := make(chan ReadResult)
51 n, err := os.Stdin.Read(b)
52 c <- ReadResult{n, err}
62 func (conn *UCSPIConn) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
64 func (conn *UCSPIConn) Close() error {
65 if err := os.Stdin.Close(); err != nil {
68 return os.Stdout.Close()
71 func (conn *UCSPIConn) LocalAddr() net.Addr {
72 return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
75 func (conn *UCSPIConn) RemoteAddr() net.Addr {
76 return &UCSPIAddr{ip: os.Getenv("TCPREMOTEIP"), port: os.Getenv("TCPREMOTEPORT")}
79 func (conn *UCSPIConn) SetDeadline(t time.Time) error {
80 if err := os.Stdin.SetReadDeadline(t); err != nil {
83 return os.Stdout.SetWriteDeadline(t)
86 func (conn *UCSPIConn) SetReadDeadline(t time.Time) error {
87 // An ugly hack to forcefully terminate pending read.
88 // net/http calls SetReadDeadline(aLongTimeAgo), but file
89 // descriptors are not capable to exit immediately that way.
90 if t.Equal(aLongTimeAgo) {
91 conn.eof <- struct{}{}
93 return os.Stdin.SetReadDeadline(t)
96 func (conn *UCSPIConn) SetWriteDeadline(t time.Time) error {
97 return os.Stdout.SetWriteDeadline(t)
100 type UCSPI struct{ accepted bool }
102 type UCSPIAlreadyAccepted struct{}
104 func (u UCSPIAlreadyAccepted) Error() string {
105 return "already accepted"
108 func (ucspi *UCSPI) Accept() (net.Conn, error) {
110 return nil, UCSPIAlreadyAccepted{}
112 ucspi.accepted = true
113 conn := UCSPIConn{eof: make(chan struct{}, 1)}
117 func (ucspi *UCSPI) Close() error {
121 func (ucspi *UCSPI) Addr() net.Addr {
122 return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
125 func connStater(conn net.Conn, connState http.ConnState) {
129 case http.StateClosed: