Skip to content

Commit 3d3436a

Browse files
author
S Riemersma
committed
ftp round tripper
1 parent 066b75c commit 3d3436a

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ go-ftp-protocol
33

44
Plugin for http.Transport with support for ftp:// protocol in Go.
55

6-
Limitations: only anonymous FTP servers, only file retrieval operations.
6+
Limitations: only file retrieval operations.
7+
authentication in case of the url containing the username password
78

89
Internally connections to FTP servers are cached and re-used when possible.
910

1011
Example usage:
1112

12-
import "github.com/smira/go-ftp-protocol/protocol"
13+
import "github.com/afosto/go-ftp-protocol/protocol"
1314

1415
transport := &http.Transport{}
1516
transport.RegisterProtocol("ftp", &protocol.FTPRoundTripper{})

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/afosto/go-ftp-protocol
2+
3+
go 1.17
4+
5+
require github.com/jlaffaye/ftp v0.0.0-20211117213618-11820403398b

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/jlaffaye/ftp v0.0.0-20211117213618-11820403398b h1:Ur6QAxsHCK99Quj9PaWafoV4unb0DO/HWiKExD+TN5g=
4+
github.com/jlaffaye/ftp v0.0.0-20211117213618-11820403398b/go.mod h1:2lmrmq866uF2tnje75wQHzmPXhmSWUt7Gyx2vgK1RCU=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
9+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
12+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

protocol/protocol.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (w *readCloserWrapper) Close() error {
5555
return err
5656
}
5757

58-
func (rt *FTPRoundTripper) getConnection(hostport string) (conn *ftp.ServerConn, err error) {
58+
func (rt *FTPRoundTripper) getConnection(hostport, username, password string) (conn *ftp.ServerConn, err error) {
5959
rt.lock.Lock()
6060
conns, ok := rt.idleConnections[hostport]
6161
if ok && len(conns) > 0 {
@@ -71,7 +71,7 @@ func (rt *FTPRoundTripper) getConnection(hostport string) (conn *ftp.ServerConn,
7171
return nil, err
7272
}
7373

74-
err = conn.Login("anonymous", "anonymous")
74+
err = conn.Login(username, password)
7575
if err != nil {
7676
conn.Quit()
7777
return nil, err
@@ -107,7 +107,17 @@ func (rt *FTPRoundTripper) RoundTrip(request *http.Request) (*http.Response, err
107107
hostport = hostport + ":21"
108108
}
109109

110-
connection, err := rt.getConnection(hostport)
110+
username, password := "anonymous", "anonymous"
111+
112+
if requestUserName, requestPassword, valid := request.BasicAuth(); valid {
113+
username = requestUserName
114+
password = requestPassword
115+
} else if request.URL.User != nil {
116+
username = request.URL.User.Username()
117+
password, _ = request.URL.User.Password()
118+
}
119+
120+
connection, err := rt.getConnection(hostport, username, password)
111121
if err != nil {
112122
return nil, err
113123
}

0 commit comments

Comments
 (0)