1*6236dae4SAndroid Build Coastguard Worker--- 2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al. 3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl 4*6236dae4SAndroid Build Coastguard WorkerTitle: CURLOPT_FOLLOWLOCATION 5*6236dae4SAndroid Build Coastguard WorkerSection: 3 6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl 7*6236dae4SAndroid Build Coastguard WorkerSee-also: 8*6236dae4SAndroid Build Coastguard Worker - CURLINFO_REDIRECT_COUNT (3) 9*6236dae4SAndroid Build Coastguard Worker - CURLINFO_REDIRECT_URL (3) 10*6236dae4SAndroid Build Coastguard Worker - CURLOPT_POSTREDIR (3) 11*6236dae4SAndroid Build Coastguard Worker - CURLOPT_PROTOCOLS_STR (3) 12*6236dae4SAndroid Build Coastguard Worker - CURLOPT_REDIR_PROTOCOLS_STR (3) 13*6236dae4SAndroid Build Coastguard Worker - CURLOPT_UNRESTRICTED_AUTH (3) 14*6236dae4SAndroid Build Coastguard WorkerProtocol: 15*6236dae4SAndroid Build Coastguard Worker - HTTP 16*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.1 17*6236dae4SAndroid Build Coastguard Worker--- 18*6236dae4SAndroid Build Coastguard Worker 19*6236dae4SAndroid Build Coastguard Worker# NAME 20*6236dae4SAndroid Build Coastguard Worker 21*6236dae4SAndroid Build Coastguard WorkerCURLOPT_FOLLOWLOCATION - follow HTTP 3xx redirects 22*6236dae4SAndroid Build Coastguard Worker 23*6236dae4SAndroid Build Coastguard Worker# SYNOPSIS 24*6236dae4SAndroid Build Coastguard Worker 25*6236dae4SAndroid Build Coastguard Worker~~~c 26*6236dae4SAndroid Build Coastguard Worker#include <curl/curl.h> 27*6236dae4SAndroid Build Coastguard Worker 28*6236dae4SAndroid Build Coastguard WorkerCURLcode curl_easy_setopt(CURL *handle, CURLOPT_FOLLOWLOCATION, long enable); 29*6236dae4SAndroid Build Coastguard Worker~~~ 30*6236dae4SAndroid Build Coastguard Worker 31*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION 32*6236dae4SAndroid Build Coastguard Worker 33*6236dae4SAndroid Build Coastguard WorkerA long parameter set to 1 tells the library to follow any Location: header 34*6236dae4SAndroid Build Coastguard Workerredirects that an HTTP server sends in a 30x response. The Location: header 35*6236dae4SAndroid Build Coastguard Workercan specify a relative or an absolute URL to follow. 36*6236dae4SAndroid Build Coastguard Worker 37*6236dae4SAndroid Build Coastguard Workerlibcurl issues another request for the new URL and follows subsequent new 38*6236dae4SAndroid Build Coastguard Worker`Location:` redirects all the way until no more such headers are returned or 39*6236dae4SAndroid Build Coastguard Workerthe maximum limit is reached. CURLOPT_MAXREDIRS(3) is used to limit the number 40*6236dae4SAndroid Build Coastguard Workerof redirects libcurl follows. 41*6236dae4SAndroid Build Coastguard Worker 42*6236dae4SAndroid Build Coastguard Workerlibcurl restricts what protocols it automatically follow redirects to. The 43*6236dae4SAndroid Build Coastguard Workeraccepted target protocols are set with CURLOPT_REDIR_PROTOCOLS_STR(3). By 44*6236dae4SAndroid Build Coastguard Workerdefault libcurl allows HTTP, HTTPS, FTP and FTPS on redirects. 45*6236dae4SAndroid Build Coastguard Worker 46*6236dae4SAndroid Build Coastguard WorkerWhen following a redirect, the specific 30x response code also dictates which 47*6236dae4SAndroid Build Coastguard Workerrequest method libcurl uses in the subsequent request: For 301, 302 and 303 48*6236dae4SAndroid Build Coastguard Workerresponses libcurl switches method from POST to GET unless CURLOPT_POSTREDIR(3) 49*6236dae4SAndroid Build Coastguard Workerinstructs libcurl otherwise. All other redirect response codes make libcurl 50*6236dae4SAndroid Build Coastguard Workeruse the same method again. 51*6236dae4SAndroid Build Coastguard Worker 52*6236dae4SAndroid Build Coastguard WorkerFor users who think the existing location following is too naive, too simple 53*6236dae4SAndroid Build Coastguard Workeror just lacks features, it is easy to instead implement your own redirect 54*6236dae4SAndroid Build Coastguard Workerfollow logic with the use of curl_easy_getinfo(3)'s CURLINFO_REDIRECT_URL(3) 55*6236dae4SAndroid Build Coastguard Workeroption instead of using CURLOPT_FOLLOWLOCATION(3). 56*6236dae4SAndroid Build Coastguard Worker 57*6236dae4SAndroid Build Coastguard WorkerBy default, libcurl only sends `Authentication:` or explicitly set `Cookie:` 58*6236dae4SAndroid Build Coastguard Workerheaders to the initial host given in the original URL, to avoid leaking 59*6236dae4SAndroid Build Coastguard Workerusername + password to other sites. CURLOPT_UNRESTRICTED_AUTH(3) is provided 60*6236dae4SAndroid Build Coastguard Workerto change that behavior. 61*6236dae4SAndroid Build Coastguard Worker 62*6236dae4SAndroid Build Coastguard WorkerDue to the way HTTP works, almost any header can be made to contain data a 63*6236dae4SAndroid Build Coastguard Workerclient may not want to pass on to other servers than the initially intended 64*6236dae4SAndroid Build Coastguard Workerhost and for all other headers than the two mentioned above, there is no 65*6236dae4SAndroid Build Coastguard Workerprotection from this happening when libcurl is told to follow redirects. 66*6236dae4SAndroid Build Coastguard Worker 67*6236dae4SAndroid Build Coastguard Worker# NOTE 68*6236dae4SAndroid Build Coastguard Worker 69*6236dae4SAndroid Build Coastguard WorkerSince libcurl changes method or not based on the specific HTTP response code, 70*6236dae4SAndroid Build Coastguard Workersetting CURLOPT_CUSTOMREQUEST(3) while following redirects may change what 71*6236dae4SAndroid Build Coastguard Workerlibcurl would otherwise do and if not that carefully may even make it 72*6236dae4SAndroid Build Coastguard Workermisbehave since CURLOPT_CUSTOMREQUEST(3) overrides the method libcurl would 73*6236dae4SAndroid Build Coastguard Workerotherwise select internally. 74*6236dae4SAndroid Build Coastguard Worker 75*6236dae4SAndroid Build Coastguard Worker# DEFAULT 76*6236dae4SAndroid Build Coastguard Worker 77*6236dae4SAndroid Build Coastguard Worker0, disabled 78*6236dae4SAndroid Build Coastguard Worker 79*6236dae4SAndroid Build Coastguard Worker# %PROTOCOLS% 80*6236dae4SAndroid Build Coastguard Worker 81*6236dae4SAndroid Build Coastguard Worker# EXAMPLE 82*6236dae4SAndroid Build Coastguard Worker 83*6236dae4SAndroid Build Coastguard Worker~~~c 84*6236dae4SAndroid Build Coastguard Workerint main(void) 85*6236dae4SAndroid Build Coastguard Worker{ 86*6236dae4SAndroid Build Coastguard Worker CURL *curl = curl_easy_init(); 87*6236dae4SAndroid Build Coastguard Worker if(curl) { 88*6236dae4SAndroid Build Coastguard Worker curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); 89*6236dae4SAndroid Build Coastguard Worker 90*6236dae4SAndroid Build Coastguard Worker /* example.com is redirected, so we tell libcurl to follow redirection */ 91*6236dae4SAndroid Build Coastguard Worker curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); 92*6236dae4SAndroid Build Coastguard Worker 93*6236dae4SAndroid Build Coastguard Worker curl_easy_perform(curl); 94*6236dae4SAndroid Build Coastguard Worker } 95*6236dae4SAndroid Build Coastguard Worker} 96*6236dae4SAndroid Build Coastguard Worker~~~ 97*6236dae4SAndroid Build Coastguard Worker 98*6236dae4SAndroid Build Coastguard Worker# %AVAILABILITY% 99*6236dae4SAndroid Build Coastguard Worker 100*6236dae4SAndroid Build Coastguard Worker# RETURN VALUE 101*6236dae4SAndroid Build Coastguard Worker 102*6236dae4SAndroid Build Coastguard WorkerReturns CURLE_OK if HTTP is supported, and CURLE_UNKNOWN_OPTION if not. 103