本文整理汇总了Golang中net/http.Response.Location方法的典型用法代码示例。如果您正苦于以下问题:Golang Response.Location方法的具体用法?Golang Response.Location怎么用?Golang Response.Location使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http.Response
的用法示例。
在下文中一共展示了Response.Location方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SendForTraversal
func (session *Session) SendForTraversal(url string, data string) (string, error) {
var (
resp *http.Response // http response
buf bytes.Buffer // contains http response body
err error
)
resp, err = session.request(url, data)
if err != nil {
return "", err
}
log.Println(resp)
defer func() {
if resp.Body != nil {
resp.Body.Close()
}
}()
_, err = buf.ReadFrom(resp.Body)
if err != nil {
return "", err
}
session.StatusCode = resp.StatusCode
location, err := resp.Location()
if err != nil {
return "", err
}
session.Location = location.String()
return buf.String(), nil
}
示例2: save
// save saves the body of a response corresponding to a request.
func (c *CachedRoundTrip) save(req *http.Request, resp *http.Response) error {
if resp.StatusCode == http.StatusMovedPermanently || resp.StatusCode == http.StatusTemporaryRedirect {
u, err := resp.Location()
if err != nil {
return err
}
err = c.Cache.Put(req.URL, c.newEntry([]byte("REDIRECT:"+u.String()), resp))
if err != nil {
return err
}
return nil
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
resp.Body.Close()
err = c.Cache.Put(req.URL, c.newEntry(body, resp))
if err != nil {
return err
}
resp.Body = ioutil.NopCloser(bytes.NewReader(body))
return nil
}
示例3: handleResp
// handleResp handles the responses from the etcd server
// If status code is OK, read the http body and return it as byte array
// If status code is TemporaryRedirect, update leader.
// If status code is InternalServerError, sleep for 200ms.
func (c *Client) handleResp(resp *http.Response) (bool, []byte) {
defer resp.Body.Close()
code := resp.StatusCode
if code == http.StatusTemporaryRedirect {
u, err := resp.Location()
if err != nil {
logger.Warning(err)
} else {
c.cluster.updateLeaderFromURL(u)
}
return false, nil
} else if code == http.StatusInternalServerError {
time.Sleep(time.Millisecond * 200)
} else if validHttpStatusCode[code] {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, nil
}
return true, b
}
logger.Warning("bad status code ", resp.StatusCode)
return false, nil
}
示例4: assertRedirectToLogin
// assertions
func (s *FormsAuthSuite) assertRedirectToLogin(res *http.Response, c *check.C) {
c.Assert(res.StatusCode, check.Equals, http.StatusSeeOther)
location, err := res.Location()
c.Assert(err, check.IsNil)
expectedURL := fmt.Sprintf("%s?returnURL=%s", s.loginURL, url.QueryEscape(s.protectedURL))
c.Assert(location.RequestURI(), check.Equals, expectedURL)
}
示例5: UpgradeToken
// UpgradeToken returns the assertion to upgrade the token on IAM to get the
// 'purchased' scopes
func (a *AssetsService) UpgradeToken() error {
var (
req *http.Request
res *http.Response
location *url.URL
err error
)
req, err = a.client.NewRequest("GET", "assets", "/v1.0/asset/access", nil)
if err != nil {
return err
}
req.Header.Add("No-Redirect", "true")
res, err = a.client.httpClient.Do(req)
if err != nil {
return err
}
location, err = res.Location()
if err != nil {
return err
}
req, err = a.client.NewRequest("GET", "iam", fmt.Sprintf("/v1.0/oauth/token/upgrade?%s", location.RawQuery), nil)
if err != nil {
return err
}
_, err = returnErrorHTTPSimple(a.client, req, err, 204)
return err
}
示例6: handleResponse
func (c *client) handleResponse(res *http.Response) {
if res.StatusCode != 200 {
return
}
st := res.Header.Get("ST")
if st == "" {
return
}
loc, err := res.Location()
if err != nil {
return
}
usn := res.Header.Get("USN")
if usn == "" {
usn = loc.String()
}
ev := Event{
Location: loc,
ST: st,
USN: usn,
}
select {
// events not being waited for are simply dropped
case c.eventChan <- ev:
default:
}
}
示例7: newAuthorization
func newAuthorization(authz *protocol.Authorization, resp *http.Response) (*Authorization, error) {
st := authz.Status
if st == "" {
// Missing status value means "pending". ACME spec Sec. 5.3.
st = protocol.StatusPending
}
id, err := newIdentifier(authz.Identifier)
if err != nil {
return nil, err
}
uri, err := resp.Location()
if err == http.ErrNoLocation {
// Fall back to request URI.
// TODO: Check that the request wasn't for a new authorization.
uri = resp.Request.URL
} else if err != nil {
return nil, err
}
ra, _ := retryAfter(resp.Header, 0)
return &Authorization{
Authorization: *authz,
Status: st,
Identifier: id,
URI: uri.String(),
RetryAfter: ra,
}, nil
}
示例8: followRedirects
// followRedirects is a custom HTTP redirects handler which appends
// cookie header to the request.
//
// r - The original response.
//
// Returns response from the new location.
func followRedirects(r *http.Response) (*http.Response, error) {
if location, err := r.Location(); err == nil && location != nil {
c := &http.Client{}
req, _ := http.NewRequest("GET", location.String(), nil)
req.Header.Set("X-WebRocket-Cookie", Cookie)
return c.Do(req)
}
return r, nil
}
示例9: ChangeToHttp
// ChangeToHttp On redirect change the response location from https to http.
// It makes the client come back to us over http.
func ChangeToHttp(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
if IsResponseRedirected(resp) {
location, _ := resp.Location()
if location != nil {
if location.Scheme == "https" {
location.Scheme = "http"
resp.Header.Set("Location", location.String())
println("ChangeToHttp response handler: ", ctx.Req.Host, "->", resp.Header.Get("Location"))
}
}
}
return resp
}
示例10: dumpResponse
func (t *verboseTransport) dumpResponse(resp *http.Response) {
info := fmt.Sprintf("< HTTP %d", resp.StatusCode)
location, err := resp.Location()
if err == nil {
info = fmt.Sprintf("%s\n< Location: %s", info, location.String())
}
t.verbosePrintln(info)
t.dumpHeaders(resp.Header, "<")
body := t.dumpBody(resp.Body)
if body != nil {
// reset body since it's been read
resp.Body = body
}
}
示例11: pollForAsynchronousResponse
func (request *Request) pollForAsynchronousResponse(acceptedResponse *http.Response) (*http.Response, error) {
var resp *http.Response = acceptedResponse
for {
if resp.StatusCode != http.StatusAccepted {
return resp, nil
}
if retryAfter := resp.Header.Get("Retry-After"); retryAfter != "" {
retryTime, err := strconv.Atoi(strings.TrimSpace(retryAfter))
if err != nil {
return nil, err
}
request.client.logger.Printf("[INFO] Polling pausing for %d seconds as per Retry-After header", retryTime)
time.Sleep(time.Duration(retryTime) * time.Second)
}
pollLocation, err := resp.Location()
if err != nil {
return nil, err
}
request.client.logger.Printf("[INFO] Polling %q for operation completion", pollLocation.String())
req, err := retryablehttp.NewRequest("GET", pollLocation.String(), bytes.NewReader([]byte{}))
if err != nil {
return nil, err
}
err = request.client.tokenRequester.addAuthorizationToRequest(req)
if err != nil {
return nil, err
}
resp, err := request.client.httpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusAccepted {
continue
}
return resp, err
}
}
示例12: returnErrorByHTTPStatusCode
// returnErrorByHTTPStatusCode returns the http error code or nil if it returns the
// desired error
func returnErrorByHTTPStatusCode(res *http.Response, desiredStatusCode int) (string, error) {
var (
location *url.URL
locationString string
)
location, _ = res.Location()
if location == nil {
locationString = ""
} else {
locationString = location.String()
}
if res.StatusCode == desiredStatusCode {
return locationString, nil
}
if http.StatusText(res.StatusCode) == "" {
return "", fmt.Errorf("HTTP Error %d", res.StatusCode)
}
return locationString, fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode))
}
示例13: DownloadReleaseAsset
// DowloadReleaseAsset downloads a release asset.
//
// DowloadReleaseAsset returns an io.ReadCloser that reads the contents of the
// specified release asset. It is the caller's responsibility to close the ReadCloser.
//
// GitHub API docs : http://developer.github.com/v3/repos/releases/#get-a-single-release-asset
func (s *RepositoriesService) DownloadReleaseAsset(owner, repo string, id int) (io.ReadCloser, error) {
u := fmt.Sprintf("repos/%s/%s/releases/assets/%d", owner, repo, id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", defaultMediaType)
var resp *http.Response
if s.client.client.Transport == nil {
resp, err = http.DefaultTransport.RoundTrip(req)
} else {
resp, err = s.client.client.Transport.RoundTrip(req)
}
if err != nil {
return nil, err
}
// GitHub API streamed the asset directly
if resp.StatusCode == http.StatusOK {
return resp.Body, nil
}
if resp.StatusCode != http.StatusFound {
return nil, fmt.Errorf("Expected status code 200 or 302, got %d", resp.StatusCode)
}
// GitHub API redirected to pre-signed S3 URL
downloadURL, err := resp.Location()
if err != nil {
return nil, err
}
resp, err = http.Get(downloadURL.String())
if err != nil {
return nil, err
}
return resp.Body, nil
}
示例14: waitForResponseWithTimer
// Wait for the job to finish, waiting waitTime on every loop
func (c *Client) waitForResponseWithTimer(r *http.Response,
waitTime time.Duration) (*http.Response, error) {
// Get temp resource
location, err := r.Location()
if err != nil {
return nil, err
}
for {
// Create request
req, err := http.NewRequest("GET", location.String(), nil)
if err != nil {
return nil, err
}
// Set token
err = c.setToken(req)
if err != nil {
return nil, err
}
// Wait for response
r, err = c.do(req)
if err != nil {
return nil, err
}
// Check if the request is pending
if r.Header.Get("X-Pending") == "true" {
if r.StatusCode != http.StatusOK {
return nil, utils.GetErrorFromResponse(r)
}
time.Sleep(waitTime)
} else {
return r, nil
}
}
}
示例15: next
func (ar *actionResolver) next(resp *http.Response) (*http.Request, error) {
if resp.StatusCode != http.StatusTemporaryRedirect {
return nil, nil
}
ar.redirectCount += 1
if ar.redirectCount >= redirectMax {
return nil, errors.New("too many redirects")
}
loc, err := resp.Location()
if err != nil {
return nil, err
}
req, err := ar.action.HTTPRequest()
if err != nil {
return nil, err
}
req.URL = loc
return req, nil
}