1 // GoCheese -- Python private package repository and caching proxy
2 // Copyright (C) 2019-2024 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
28 ALongTimeAgo = time.Unix(1, 0)
29 UCSPIJob sync.WaitGroup
32 type UCSPIAddr struct {
37 func (addr *UCSPIAddr) Network() string { return "tcp" }
39 func (addr *UCSPIAddr) String() string { return addr.ip + ":" + addr.port }
41 type UCSPIConn struct {
45 type ReadResult struct {
50 func (conn *UCSPIConn) Read(b []byte) (int, error) {
51 c := make(chan ReadResult)
53 n, err := os.Stdin.Read(b)
54 c <- ReadResult{n, err}
64 func (conn *UCSPIConn) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
66 func (conn *UCSPIConn) Close() error {
67 if err := os.Stdin.Close(); err != nil {
70 return os.Stdout.Close()
73 func (conn *UCSPIConn) LocalAddr() net.Addr {
74 return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
77 func (conn *UCSPIConn) RemoteAddr() net.Addr {
78 return &UCSPIAddr{ip: os.Getenv("TCPREMOTEIP"), port: os.Getenv("TCPREMOTEPORT")}
81 func (conn *UCSPIConn) SetDeadline(t time.Time) error {
82 if err := os.Stdin.SetReadDeadline(t); err != nil {
85 return os.Stdout.SetWriteDeadline(t)
88 func (conn *UCSPIConn) SetReadDeadline(t time.Time) error {
89 // An ugly hack to forcefully terminate pending read.
90 // net/http calls SetReadDeadline(aLongTimeAgo), but file
91 // descriptors are not capable to exit immediately that way.
92 if t.Equal(ALongTimeAgo) {
93 conn.eof <- struct{}{}
95 return os.Stdin.SetReadDeadline(t)
98 func (conn *UCSPIConn) SetWriteDeadline(t time.Time) error {
99 return os.Stdout.SetWriteDeadline(t)
102 type UCSPI struct{ accepted bool }
104 type UCSPIAlreadyAccepted struct{}
106 func (u UCSPIAlreadyAccepted) Error() string {
107 return "already accepted"
110 func (ucspi *UCSPI) Accept() (net.Conn, error) {
112 return nil, UCSPIAlreadyAccepted{}
114 ucspi.accepted = true
115 conn := UCSPIConn{eof: make(chan struct{}, 1)}
119 func (ucspi *UCSPI) Close() error {
123 func (ucspi *UCSPI) Addr() net.Addr {
124 return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
127 func connStater(conn net.Conn, connState http.ConnState) {
131 case http.StateClosed: