MCPcopy Index your code
hub / github.com/coder/coder / headerTransport

Function headerTransport

cli/root.go:1706–1745  ·  view source on GitHub ↗

HeaderTransport creates a new transport that executes `--header-command` if it is set to add headers for all outbound requests.

(ctx context.Context, serverURL *url.URL, header []string, headerCommand string)

Source from the content-addressed store, hash-verified

1704// HeaderTransport creates a new transport that executes `--header-command`
1705// if it is set to add headers for all outbound requests.
1706func headerTransport(ctx context.Context, serverURL *url.URL, header []string, headerCommand string) (*codersdk.HeaderTransport, error) {
1707 transport := &codersdk.HeaderTransport{
1708 Transport: http.DefaultTransport,
1709 Header: http.Header{},
1710 }
1711 headers := header
1712 if headerCommand != "" {
1713 shell := "sh"
1714 caller := "-c"
1715 if runtime.GOOS == "windows" {
1716 shell = "cmd.exe"
1717 caller = "/c"
1718 }
1719 var outBuf bytes.Buffer
1720 // #nosec
1721 cmd := exec.CommandContext(ctx, shell, caller, headerCommand)
1722 cmd.Env = append(os.Environ(), "CODER_URL="+serverURL.String())
1723 cmd.Stdout = &outBuf
1724 cmd.Stderr = io.Discard
1725 err := cmd.Run()
1726 if err != nil {
1727 return nil, xerrors.Errorf("failed to run %v: %w", cmd.Args, err)
1728 }
1729 scanner := bufio.NewScanner(&outBuf)
1730 for scanner.Scan() {
1731 headers = append(headers, scanner.Text())
1732 }
1733 if err := scanner.Err(); err != nil {
1734 return nil, xerrors.Errorf("scan %v: %w", cmd.Args, err)
1735 }
1736 }
1737 for _, header := range headers {
1738 parts := strings.SplitN(header, "=", 2)
1739 if len(parts) < 2 {
1740 return nil, xerrors.Errorf("split header %q had less than two parts", header)
1741 }
1742 transport.Header.Add(parts[0], parts[1])
1743 }
1744 return transport, nil
1745}
1746
1747// PrintDeprecatedOptions loops through all command options, and
1748// prints a warning for usage of deprecated options.

Callers 2

HeaderTransportMethod · 0.85
workspaceAgentFunction · 0.85

Calls 9

ErrMethod · 0.80
CommandContextMethod · 0.65
EnvironMethod · 0.65
RunMethod · 0.65
AddMethod · 0.65
StringMethod · 0.45
ErrorfMethod · 0.45
ScanMethod · 0.45
TextMethod · 0.45

Tested by

no test coverage detected