@@ -86,7 +86,7 @@ type httpSmartSubtransport struct {
86
86
httpTransport * http.Transport
87
87
}
88
88
89
- func (t * httpSmartSubtransport ) Action (targetUrl string , action git2go.SmartServiceAction ) (git2go.SmartSubtransportStream , error ) {
89
+ func (t * httpSmartSubtransport ) Action (transportAuthID string , action git2go.SmartServiceAction ) (git2go.SmartSubtransportStream , error ) {
90
90
var proxyFn func (* http.Request ) (* url.URL , error )
91
91
proxyOpts , err := t .transport .SmartProxyOptions ()
92
92
if err != nil {
@@ -109,7 +109,7 @@ func (t *httpSmartSubtransport) Action(targetUrl string, action git2go.SmartServ
109
109
t .httpTransport .Proxy = proxyFn
110
110
t .httpTransport .DisableCompression = false
111
111
112
- client , req , err := createClientRequest (targetUrl , action , t .httpTransport )
112
+ client , req , err := createClientRequest (transportAuthID , action , t .httpTransport )
113
113
if err != nil {
114
114
return nil , err
115
115
}
@@ -142,36 +142,22 @@ func (t *httpSmartSubtransport) Action(targetUrl string, action git2go.SmartServ
142
142
return stream , nil
143
143
}
144
144
145
- func createClientRequest (targetUrl string , action git2go.SmartServiceAction , t * http.Transport ) (* http.Client , * http.Request , error ) {
145
+ func createClientRequest (transportAuthID string , action git2go.SmartServiceAction , t * http.Transport ) (* http.Client , * http.Request , error ) {
146
146
var req * http.Request
147
147
var err error
148
148
149
149
if t == nil {
150
150
return nil , nil , fmt .Errorf ("failed to create client: transport cannot be nil" )
151
151
}
152
152
153
- finalUrl := targetUrl
154
- opts , found := transportOptions (targetUrl )
155
- if found {
156
- if opts .TargetURL != "" {
157
- // override target URL only if options are found and a new targetURL
158
- // is provided.
159
- finalUrl = opts .TargetURL
160
- }
153
+ opts , found := getTransportOptions (transportAuthID )
161
154
162
- // Add any provided certificate to the http transport.
163
- if len (opts .CABundle ) > 0 {
164
- cap := x509 .NewCertPool ()
165
- if ok := cap .AppendCertsFromPEM (opts .CABundle ); ! ok {
166
- return nil , nil , fmt .Errorf ("failed to use certificate from PEM" )
167
- }
168
- t .TLSClientConfig = & tls.Config {
169
- RootCAs : cap ,
170
- }
171
- }
155
+ if ! found {
156
+ return nil , nil , fmt .Errorf ("failed to create client: could not find transport options for the object: %s" , transportAuthID )
172
157
}
158
+ targetURL := opts .TargetURL
173
159
174
- if len (finalUrl ) > URLMaxLength {
160
+ if len (targetURL ) > URLMaxLength {
175
161
return nil , nil , fmt .Errorf ("URL exceeds the max length (%d)" , URLMaxLength )
176
162
}
177
163
@@ -182,20 +168,20 @@ func createClientRequest(targetUrl string, action git2go.SmartServiceAction, t *
182
168
183
169
switch action {
184
170
case git2go .SmartServiceActionUploadpackLs :
185
- req , err = http .NewRequest ("GET" , finalUrl + "/info/refs?service=git-upload-pack" , nil )
171
+ req , err = http .NewRequest ("GET" , targetURL + "/info/refs?service=git-upload-pack" , nil )
186
172
187
173
case git2go .SmartServiceActionUploadpack :
188
- req , err = http .NewRequest ("POST" , finalUrl + "/git-upload-pack" , nil )
174
+ req , err = http .NewRequest ("POST" , targetURL + "/git-upload-pack" , nil )
189
175
if err != nil {
190
176
break
191
177
}
192
178
req .Header .Set ("Content-Type" , "application/x-git-upload-pack-request" )
193
179
194
180
case git2go .SmartServiceActionReceivepackLs :
195
- req , err = http .NewRequest ("GET" , finalUrl + "/info/refs?service=git-receive-pack" , nil )
181
+ req , err = http .NewRequest ("GET" , targetURL + "/info/refs?service=git-receive-pack" , nil )
196
182
197
183
case git2go .SmartServiceActionReceivepack :
198
- req , err = http .NewRequest ("POST" , finalUrl + "/git-receive-pack" , nil )
184
+ req , err = http .NewRequest ("POST" , targetURL + "/git-receive-pack" , nil )
199
185
if err != nil {
200
186
break
201
187
}
@@ -209,6 +195,20 @@ func createClientRequest(targetUrl string, action git2go.SmartServiceAction, t *
209
195
return nil , nil , err
210
196
}
211
197
198
+ // Add any provided certificate to the http transport.
199
+ if opts .AuthOpts != nil {
200
+ req .SetBasicAuth (opts .AuthOpts .Username , opts .AuthOpts .Password )
201
+ if len (opts .AuthOpts .CAFile ) > 0 {
202
+ certPool := x509 .NewCertPool ()
203
+ if ok := certPool .AppendCertsFromPEM (opts .AuthOpts .CAFile ); ! ok {
204
+ return nil , nil , fmt .Errorf ("failed to use certificate from PEM" )
205
+ }
206
+ t .TLSClientConfig = & tls.Config {
207
+ RootCAs : certPool ,
208
+ }
209
+ }
210
+ }
211
+
212
212
req .Header .Set ("User-Agent" , "git/2.0 (flux-libgit2)" )
213
213
return client , req , nil
214
214
}
@@ -239,7 +239,6 @@ type httpSmartSubtransportStream struct {
239
239
recvReply sync.WaitGroup
240
240
httpError error
241
241
m sync.RWMutex
242
- targetURL string
243
242
}
244
243
245
244
func newManagedHttpStream (owner * httpSmartSubtransport , req * http.Request , client * http.Client ) * httpSmartSubtransportStream {
@@ -324,29 +323,8 @@ func (self *httpSmartSubtransportStream) sendRequest() error {
324
323
325
324
var resp * http.Response
326
325
var err error
327
- var userName string
328
- var password string
329
-
330
- // Obtain the credentials and use them if available.
331
- cred , err := self .owner .transport .SmartCredentials ("" , git2go .CredentialTypeUserpassPlaintext )
332
- if err != nil {
333
- // Passthrough error indicates that no credentials were provided.
334
- // Continue without credentials.
335
- if err .Error () != git2go .ErrorCodePassthrough .String () {
336
- return err
337
- }
338
- }
339
-
340
- if cred != nil {
341
- defer cred .Free ()
342
-
343
- userName , password , err = cred .GetUserpassPlaintext ()
344
- if err != nil {
345
- return err
346
- }
347
- }
348
-
349
326
var content []byte
327
+
350
328
for {
351
329
req := & http.Request {
352
330
Method : self .req .Method ,
@@ -365,7 +343,6 @@ func (self *httpSmartSubtransportStream) sendRequest() error {
365
343
req .ContentLength = - 1
366
344
}
367
345
368
- req .SetBasicAuth (userName , password )
369
346
traceLog .Info ("[http]: new request" , "method" , req .Method , "URL" , req .URL )
370
347
resp , err = self .client .Do (req )
371
348
if err != nil {
0 commit comments