2 GoCheese -- Python private package repository and caching proxy
3 Copyright (C) 2019-2021 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/>.
30 aLongTimeAgo = time.Unix(1, 0)
31 UCSPIJob sync.WaitGroup
34 type UCSPIAddr struct {
39 func (addr *UCSPIAddr) Network() string { return "tcp" }
41 func (addr *UCSPIAddr) String() string { return addr.ip + ":" + addr.port }
43 type UCSPIConn struct {
47 type ReadResult struct {
52 func (conn *UCSPIConn) Read(b []byte) (int, error) {
53 c := make(chan ReadResult)
55 n, err := os.Stdin.Read(b)
56 c <- ReadResult{n, err}
66 func (conn *UCSPIConn) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
68 func (conn *UCSPIConn) Close() error {
69 if err := os.Stdin.Close(); err != nil {
72 return os.Stdout.Close()
75 func (conn *UCSPIConn) LocalAddr() net.Addr {
76 return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
79 func (conn *UCSPIConn) RemoteAddr() net.Addr {
80 return &UCSPIAddr{ip: os.Getenv("TCPREMOTEIP"), port: os.Getenv("TCPREMOTEPORT")}
83 func (conn *UCSPIConn) SetDeadline(t time.Time) error {
84 if err := os.Stdin.SetReadDeadline(t); err != nil {
87 return os.Stdout.SetWriteDeadline(t)
90 func (conn *UCSPIConn) SetReadDeadline(t time.Time) error {
91 // An ugly hack to forcefully terminate pending read.
92 // net/http calls SetReadDeadline(aLongTimeAgo), but file
93 // descriptors are not capable to exit immediately that way.
94 if t.Equal(aLongTimeAgo) {
95 conn.eof <- struct{}{}
97 return os.Stdin.SetReadDeadline(t)
100 func (conn *UCSPIConn) SetWriteDeadline(t time.Time) error {
101 return os.Stdout.SetWriteDeadline(t)
104 type UCSPI struct{ accepted bool }
106 type UCSPIAlreadyAccepted struct{}
108 func (u UCSPIAlreadyAccepted) Error() string {
109 return "already accepted"
112 func (ucspi *UCSPI) Accept() (net.Conn, error) {
114 return nil, UCSPIAlreadyAccepted{}
116 ucspi.accepted = true
117 conn := UCSPIConn{eof: make(chan struct{}, 1)}
121 func (ucspi *UCSPI) Close() error {
125 func (ucspi *UCSPI) Addr() net.Addr {
126 return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
129 func connStater(conn net.Conn, connState http.ConnState) {
133 case http.StateClosed: