]> Sergey Matveev's repositories - uploader.git/blob - ucspi.go
Unify copyright comment format
[uploader.git] / ucspi.go
1 // uploader -- simplest form file uploader
2 // Copyright (C) 2018-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "io"
20         "net"
21         "net/http"
22         "os"
23         "time"
24 )
25
26 var aLongTimeAgo = time.Unix(1, 0)
27
28 type UCSPIAddr struct {
29         ip   string
30         port string
31 }
32
33 func (addr *UCSPIAddr) Network() string { return "tcp" }
34
35 func (addr *UCSPIAddr) String() string { return addr.ip + ":" + addr.port }
36
37 type UCSPIConn struct {
38         eof chan struct{}
39 }
40
41 type ReadResult struct {
42         n   int
43         err error
44 }
45
46 func (conn *UCSPIConn) Read(b []byte) (int, error) {
47         c := make(chan ReadResult)
48         go func() {
49                 n, err := os.Stdin.Read(b)
50                 c <- ReadResult{n, err}
51         }()
52         select {
53         case res := <-c:
54                 return res.n, res.err
55         case <-conn.eof:
56                 return 0, io.EOF
57         }
58 }
59
60 func (conn *UCSPIConn) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
61
62 func (conn *UCSPIConn) Close() error {
63         if err := os.Stdin.Close(); err != nil {
64                 return err
65         }
66         return os.Stdout.Close()
67 }
68
69 func (conn *UCSPIConn) LocalAddr() net.Addr {
70         return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
71 }
72
73 func (conn *UCSPIConn) RemoteAddr() net.Addr {
74         return &UCSPIAddr{ip: os.Getenv("TCPREMOTEIP"), port: os.Getenv("TCPREMOTEPORT")}
75 }
76
77 func (conn *UCSPIConn) SetDeadline(t time.Time) error {
78         if err := os.Stdin.SetReadDeadline(t); err != nil {
79                 return err
80         }
81         return os.Stdout.SetWriteDeadline(t)
82 }
83
84 func (conn *UCSPIConn) SetReadDeadline(t time.Time) error {
85         // An ugly hack to forcefully terminate pending read.
86         // net/http calls SetReadDeadline(aLongTimeAgo), but file
87         // descriptors are not capable to exit immediately that way.
88         if t.Equal(aLongTimeAgo) {
89                 conn.eof <- struct{}{}
90         }
91         return os.Stdin.SetReadDeadline(t)
92 }
93
94 func (conn *UCSPIConn) SetWriteDeadline(t time.Time) error {
95         return os.Stdout.SetWriteDeadline(t)
96 }
97
98 type UCSPI struct{ accepted bool }
99
100 type UCSPIAlreadyAccepted struct{}
101
102 func (u UCSPIAlreadyAccepted) Error() string {
103         return "already accepted"
104 }
105
106 func (ucspi *UCSPI) Accept() (net.Conn, error) {
107         if ucspi.accepted {
108                 return nil, UCSPIAlreadyAccepted{}
109         }
110         ucspi.accepted = true
111         conn := UCSPIConn{eof: make(chan struct{}, 1)}
112         return &conn, nil
113 }
114
115 func (ucspi *UCSPI) Close() error {
116         return nil
117 }
118
119 func (ucspi *UCSPI) Addr() net.Addr {
120         return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
121 }
122
123 func connStater(conn net.Conn, connState http.ConnState) {
124         switch connState {
125         case http.StateNew:
126                 Jobs.Add(1)
127         case http.StateClosed:
128                 Jobs.Done()
129         }
130 }