Artifactspublic-v0

curl

master
ply4 git calls164 ms289 KiB2 cachedprotocol v2
160 lines5.4 KiBRaw

NAME

CURLOPT_FOLLOWLOCATION - follow HTTP 3xx redirects

SYNOPSIS

c
#include <curl/curl.h>

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_FOLLOWLOCATION, long mode);

DESCRIPTION

This option tells the library to follow Location: header redirects that an HTTP server sends in a 30x response. The Location: header can specify a relative or an absolute URL to follow. The long parameter mode instructs how libcurl should act on subsequent requests.

mode only had a single value (1L) for a long time that enables redirect following. Since 8.13.0, two additional modes are also supported. See below.

When following redirects, libcurl issues another request for the new URL and follows subsequent new Location: redirects all the way until no more such headers are returned or the maximum limit is reached. CURLOPT_MAXREDIRS(3) is used to limit the number of redirects libcurl follows.

libcurl restricts what protocols it automatically follow redirects to. The accepted target protocols are set with CURLOPT_REDIR_PROTOCOLS_STR(3). By default libcurl allows HTTP, HTTPS, FTP and FTPS on redirects.

When following a redirect, the specific 30x response code also dictates which request method libcurl uses in the subsequent request: For 301, 302 and 303 responses libcurl switches method from POST to GET unless CURLOPT_POSTREDIR(3) instructs libcurl otherwise. All other redirect response codes make libcurl use the same method again.

When libcurl switches method to GET, it then uses that method without sending any request body. If it does not change the method, it sends the subsequent request the same way as the previous one; including the request body if one was provided.

For users who think the existing location following is too naive, too simple or lacking features, it is easy to instead implement your own redirect follow logic with the use of curl_easy_getinfo(3)'s CURLINFO_REDIRECT_URL(3) option instead of using CURLOPT_FOLLOWLOCATION(3).

By default, libcurl only sends Authorization: or explicitly set Cookie: headers to the initial host given in the original URL, to avoid leaking username + password to other sites. CURLOPT_UNRESTRICTED_AUTH(3) is provided to change that behavior.

Due to the way HTTP works, almost any header can be made to contain data a client may not want to pass on to other servers than the initially intended host and for all other headers than the two mentioned above, there is no protection from this happening when libcurl is told to follow redirects.

Pick one of the following modes:

CURLFOLLOW_ALL (1)

Before 8.13.0 this bit had no name and 1L was the value to enable this option. This makes a set custom method be used in all HTTP requests, even after redirects.

CURLFOLLOW_OBEYCODE (2)

When there is a custom request method set with CURLOPT_CUSTOMREQUEST(3), that set method replaces what libcurl would otherwise use. If a 301/302/303 response code is returned to signal a redirect, the method is changed from POST to GET. For 307/308, the custom method remains set and used.

Note that only POST (or a custom post) is changed to GET on 301/302, its not change PUT etc - and therefore also not when libcurl issues a custom PUT. A 303 response makes it switch to GET independently of the original method (except for HEAD).

To control for which of the 301/302/303 status codes libcurl should not switch back to GET for when doing a custom POST (a POST transfer using a modified method), and instead keep the custom method, use CURLOPT_POSTREDIR(3).

If you prefer a custom POST method to be reset to exactly the method POST, use CURLFOLLOW_FIRSTONLY instead.

CURLFOLLOW_FIRSTONLY (3)

When there is a custom request method set with CURLOPT_CUSTOMREQUEST(3), that set method replaces what libcurl would otherwise use in the first outgoing request only. The second request is then done according to the redirect response code.

If you prefer your custom method to remain in use after a 307/308 redirect, use CURLFOLLOW_OBEYCODE instead.

NOTE

Since libcurl changes method or not based on the specific HTTP response code, setting CURLOPT_CUSTOMREQUEST(3) while following redirects may change what libcurl would otherwise do and if not that carefully may even make it misbehave since CURLOPT_CUSTOMREQUEST(3) overrides the method libcurl would otherwise select internally.

Setting the CURLFOLLOW_OBEYCODE bit makes libcurl not use the custom set method after redirects for 301, 302 and 303 responses. Unless the CURLOPT_POSTREDIR(3) bits are set for those status codes.

DEFAULT

0, disabled

%PROTOCOLS%

EXAMPLE

c
int main(void)
{
  CURL *curl = curl_easy_init();
  if(curl) {
    CURLcode result;
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");

    /* example.com is redirected, so we tell libcurl to follow redirection */
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    result = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
  }
}

%AVAILABILITY%

RETURN VALUE

curl_easy_setopt(3) returns a CURLcode indicating success or error.

CURLE_OK (0) means everything was OK, non-zero means an error occurred, see libcurl-errors(3).

1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_FOLLOWLOCATION
5Section: 3
6Source: libcurl
7See-also:
8 - CURLINFO_REDIRECT_COUNT (3)
9 - CURLINFO_REDIRECT_URL (3)
10 - CURLOPT_POSTREDIR (3)
11 - CURLOPT_PROTOCOLS_STR (3)
12 - CURLOPT_REDIR_PROTOCOLS_STR (3)
13 - CURLOPT_UNRESTRICTED_AUTH (3)
14Protocol:
15 - HTTP
16Added-in: 7.1
17---
19# NAME
21CURLOPT_FOLLOWLOCATION - follow HTTP 3xx redirects
23# SYNOPSIS
25~~~c
26#include <curl/curl.h>
28CURLcode curl_easy_setopt(CURL *handle, CURLOPT_FOLLOWLOCATION, long mode);
29~~~
31# DESCRIPTION
33This option tells the library to follow `Location:` header redirects that an
34HTTP server sends in a 30x response. The `Location:` header can specify a
35relative or an absolute URL to follow. The long parameter *mode* instructs how
36libcurl should act on subsequent requests.
38*mode* only had a single value (1L) for a long time that enables redirect
39following. Since 8.13.0, two additional modes are also supported. See below.
41When following redirects, libcurl issues another request for the new URL and
42follows subsequent new `Location:` redirects all the way until no more such
43headers are returned or the maximum limit is reached. CURLOPT_MAXREDIRS(3) is
44used to limit the number of redirects libcurl follows.
46libcurl restricts what protocols it automatically follow redirects to. The
47accepted target protocols are set with CURLOPT_REDIR_PROTOCOLS_STR(3). By
48default libcurl allows HTTP, HTTPS, FTP and FTPS on redirects.
50When following a redirect, the specific 30x response code also dictates which
51request method libcurl uses in the subsequent request: For 301, 302 and 303
52responses libcurl switches method from POST to GET unless CURLOPT_POSTREDIR(3)
53instructs libcurl otherwise. All other redirect response codes make libcurl
54use the same method again.
56When libcurl switches method to GET, it then uses that method without sending
57any request body. If it does not change the method, it sends the subsequent
58request the same way as the previous one; including the request body if one
59was provided.
61For users who think the existing location following is too naive, too simple
62or lacking features, it is easy to instead implement your own redirect follow
63logic with the use of curl_easy_getinfo(3)'s CURLINFO_REDIRECT_URL(3) option
64instead of using CURLOPT_FOLLOWLOCATION(3).
66By default, libcurl only sends `Authorization:` or explicitly set `Cookie:`
67headers to the initial host given in the original URL, to avoid leaking
68username + password to other sites. CURLOPT_UNRESTRICTED_AUTH(3) is provided
69to change that behavior.
71Due to the way HTTP works, almost any header can be made to contain data a
72client may not want to pass on to other servers than the initially intended
73host and for all other headers than the two mentioned above, there is no
74protection from this happening when libcurl is told to follow redirects.
76Pick one of the following modes:
78## CURLFOLLOW_ALL (1)
80Before 8.13.0 this bit had no name and 1L was the value to enable this option.
81This makes a set custom method be used in all HTTP requests, even after
82redirects.
84## CURLFOLLOW_OBEYCODE (2)
86When there is a custom request method set with CURLOPT_CUSTOMREQUEST(3), that
87set method replaces what libcurl would otherwise use. If a 301/302/303
88response code is returned to signal a redirect, the method is changed from
89POST to `GET`. For 307/308, the custom method remains set and used.
91Note that only POST (or a custom post) is changed to GET on 301/302, its not
92change PUT etc - and therefore also not when libcurl issues a custom PUT. A
93303 response makes it switch to GET independently of the original method
94(except for HEAD).
96To control for which of the 301/302/303 status codes libcurl should *not*
97switch back to GET for when doing a custom POST (a POST transfer using a
98modified method), and instead keep the custom method, use
99CURLOPT_POSTREDIR(3).
101If you prefer a custom POST method to be reset to exactly the method `POST`,
102use CURLFOLLOW_FIRSTONLY instead.
104## CURLFOLLOW_FIRSTONLY (3)
106When there is a custom request method set with CURLOPT_CUSTOMREQUEST(3), that
107set method replaces what libcurl would otherwise use in the first outgoing
108request only. The second request is then done according to the redirect
109response code.
111If you prefer your custom method to remain in use after a 307/308 redirect,
112use CURLFOLLOW_OBEYCODE instead.
114##
116# NOTE
118Since libcurl changes method or not based on the specific HTTP response code,
119setting CURLOPT_CUSTOMREQUEST(3) while following redirects may change what
120libcurl would otherwise do and if not that carefully may even make it
121misbehave since CURLOPT_CUSTOMREQUEST(3) overrides the method libcurl would
122otherwise select internally.
124Setting the CURLFOLLOW_OBEYCODE bit makes libcurl *not* use the custom set
125method after redirects for 301, 302 and 303 responses. Unless the
126CURLOPT_POSTREDIR(3) bits are set for those status codes.
128# DEFAULT
1300, disabled
132# %PROTOCOLS%
134# EXAMPLE
136~~~c
137int main(void)
139 CURL *curl = curl_easy_init();
140 if(curl) {
141 CURLcode result;
142 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
144 /* example.com is redirected, so we tell libcurl to follow redirection */
145 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
147 result = curl_easy_perform(curl);
148 curl_easy_cleanup(curl);
149 }
151~~~
153# %AVAILABILITY%
155# RETURN VALUE
157curl_easy_setopt(3) returns a CURLcode indicating success or error.
159CURLE_OK (0) means everything was OK, non-zero means an error occurred, see
160libcurl-errors(3).
Diagnostics4 git calls · 164 ms
CallStatusSizeTime0164 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=159
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch2002.5 KiB160 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2546
git.response.cache=MISS
git.response.duration_ms=160
git.fetch.wants=cc704576c4124789383198581c65f5ec60495304
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2546
git.pkt.payload.max=2525
git.sideband.band1.bytes=2524
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e3352cf9adf485
Request
f2d69d33…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format