MCPcopy
hub / github.com/cloudflare/cloudflared / poll

Function poll

token/transfer.go:134–166  ·  view source on GitHub ↗

poll the endpoint for the request resource, waiting for the user interaction

(client *http.Client, requestURL string, log *zerolog.Logger)

Source from the content-addressed store, hash-verified

132
133// poll the endpoint for the request resource, waiting for the user interaction
134func poll(client *http.Client, requestURL string, log *zerolog.Logger) ([]byte, string, error) {
135 req, err := http.NewRequest(http.MethodGet, requestURL, nil)
136 if err != nil {
137 return nil, "", err
138 }
139 req.Header.Set("User-Agent", userAgent)
140 resp, err := client.Do(req) // nolint: gosec
141 if err != nil {
142 return nil, "", err
143 }
144 defer func() { _ = resp.Body.Close() }()
145
146 // ignore everything other than server errors as the resource
147 // may not exist until the user does the interaction
148 if resp.StatusCode >= 500 {
149 buf := new(bytes.Buffer)
150 if _, err := io.Copy(buf, resp.Body); err != nil {
151 return nil, "", err
152 }
153
154 return nil, "", fmt.Errorf("error on request %d: %s", resp.StatusCode, buf.String())
155 }
156 if resp.StatusCode != 200 {
157 log.Info().Msg("Waiting for login...")
158 return nil, "", nil
159 }
160
161 buf := new(bytes.Buffer)
162 if _, err := io.Copy(buf, resp.Body); err != nil {
163 return nil, "", err
164 }
165 return buf.Bytes(), resp.Header.Get("service-public-key"), nil
166}

Callers 1

transferRequestFunction · 0.85

Calls 7

ErrorfMethod · 0.80
BytesMethod · 0.80
GetMethod · 0.80
CloseMethod · 0.65
StringMethod · 0.65
SetMethod · 0.45
DoMethod · 0.45

Tested by

no test coverage detected