fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

cmd/zoekt-sourcegraph-indexserver: Add default port to root URL (#559)

+105
+47
cmd/zoekt-sourcegraph-indexserver/main.go
··· 1289 1289 return nil, fmt.Errorf("url.Parse(%v): %v", conf.root, err) 1290 1290 } 1291 1291 1292 + rootURL = addDefaultPort(rootURL) 1293 + 1292 1294 // Tune GOMAXPROCS to match Linux container CPU quota. 1293 1295 _, _ = maxprocs.Set() 1294 1296 ··· 1442 1444 ctx = metadata.AppendToOutgoingContext(ctx, "X-Sourcegraph-Actor-UID", "internal") 1443 1445 return streamer(ctx, desc, cc, method, opts...) 1444 1446 } 1447 + } 1448 + 1449 + // addDefaultPort adds a default port to a URL if one is not specified. 1450 + // 1451 + // If the URL scheme is "http" and no port is specified, "80" is used. 1452 + // If the scheme is "https", "443" is used. 1453 + // 1454 + // The original URL is not mutated. A copy is modified and returned. 1455 + func addDefaultPort(original *url.URL) *url.URL { 1456 + if original == nil { 1457 + return nil // don't panic 1458 + } 1459 + 1460 + if !original.IsAbs() { 1461 + return original // don't do anything if the URL doesn't look like a remote URL 1462 + } 1463 + 1464 + if original.Scheme == "http" && original.Port() == "" { 1465 + u := cloneURL(original) 1466 + u.Host = net.JoinHostPort(u.Host, "80") 1467 + return u 1468 + } 1469 + 1470 + if original.Scheme == "https" && original.Port() == "" { 1471 + u := cloneURL(original) 1472 + u.Host = net.JoinHostPort(u.Host, "443") 1473 + return u 1474 + } 1475 + 1476 + return original 1477 + } 1478 + 1479 + // cloneURL returns a copy of the URL. It is safe to mutate the returned URL. 1480 + // This is copied from net/http/clone.go 1481 + func cloneURL(u *url.URL) *url.URL { 1482 + if u == nil { 1483 + return nil 1484 + } 1485 + u2 := new(url.URL) 1486 + *u2 = *u 1487 + if u.User != nil { 1488 + u2.User = new(url.Userinfo) 1489 + *u2.User = *u.User 1490 + } 1491 + return u2 1445 1492 } 1446 1493 1447 1494 func main() {
+58
cmd/zoekt-sourcegraph-indexserver/main_test.go
··· 286 286 t.Fatalf("default service config is invalid:\n%s", errs.String()) 287 287 } 288 288 } 289 + 290 + func TestAddDefaultPort(t *testing.T) { 291 + tests := []struct { 292 + name string 293 + input string 294 + want string 295 + }{ 296 + { 297 + name: "http no port", 298 + input: "http://example.com", 299 + want: "http://example.com:80", 300 + }, 301 + { 302 + name: "http custom port", 303 + input: "http://example.com:90", 304 + want: "http://example.com:90", 305 + }, 306 + { 307 + name: "https no port", 308 + input: "https://example.com", 309 + want: "https://example.com:443", 310 + }, 311 + { 312 + name: "https custom port", 313 + input: "https://example.com:444", 314 + want: "https://example.com:444", 315 + }, 316 + { 317 + name: "non-http scheme", 318 + input: "ftp://example.com", 319 + want: "ftp://example.com", 320 + }, 321 + { 322 + name: "empty string", 323 + input: "", 324 + want: "", 325 + }, 326 + { 327 + name: "local file path", 328 + input: "/etc/hosts", 329 + want: "/etc/hosts", 330 + }, 331 + } 332 + 333 + for _, test := range tests { 334 + t.Run(test.name, func(t *testing.T) { 335 + input, err := url.Parse(test.input) 336 + if err != nil { 337 + t.Fatalf("failed to parse test URL %q: %v", test.input, err) 338 + } 339 + 340 + got := addDefaultPort(input) 341 + if diff := cmp.Diff(test.want, got.String()); diff != "" { 342 + t.Errorf("addDefaultPort(%q) mismatch (-want +got):\n%s", test.input, diff) 343 + } 344 + }) 345 + } 346 + }