How fetch-stitch.sh Handles Google Cloud Storage Redirects
The fetch-stitch.sh script automatically follows Google Cloud Storage 302 redirects using the curl -L flag, which instructs cURL to traverse HTTP redirect chains until reaching the final resource without requiring custom logic.
The fetch-stitch.sh script in the google-labs-code/stitch-skills repository provides a lightweight Bash wrapper for downloading Stitch HTML files from remote endpoints. When retrieving assets stored in Google Cloud Storage (GCS), the script must handle temporary redirects common to signed URLs. Understanding how fetch-stitch.sh handles Google Cloud Storage redirects reveals a robust approach using standard cURL options rather than provider-specific code.
The Core cURL Implementation
Located at plugins/stitch-build/skills/react-native/scripts/fetch-stitch.sh, the script delegates all redirect handling to cURL through a specific set of command-line flags. The critical implementation appears on line 24:
curl -L -f -sS --connect-timeout 10 --compressed "$URL" -o "$OUTPUT"
The -L (or --location) flag instructs cURL to follow HTTP 3xx redirect responses until reaching the final destination. This single flag eliminates the need for manual redirect parsing or recursive request logic within the script itself.
How the -L Flag Handles GCS Redirects
When accessing Google Cloud Storage objects via signed URLs, GCS typically responds with a 302 Found redirect to an internal storage endpoint. For example, requests to storage.googleapis.com frequently redirect to storage-download.googleapis.com to serve the actual object content.
Without the -L flag, cURL would return the 302 response headers instead of the file content. The -L flag ensures cURL automatically follows the redirect chain, making an additional request to the new location specified in the Location response header. This behavior is transparent to the calling script and handles any number of successive redirects automatically.
Additional Reliability Flags
Beyond redirect handling, fetch-stitch.sh employs several other cURL flags to ensure robust downloads:
-f(fail): Returns an error exit status for HTTP 4xx and 5xx responses, allowing the script to detect server errors programmatically rather than saving error pages to disk-sS(silent/show-error): Suppresses progress meters while preserving error messages for debugging purposes--connect-timeout 10: Aborts the connection attempt if it cannot establish within 10 seconds, preventing indefinite hangs on unreachable endpoints--compressed: Requests gzip or deflate compression from the server, reducing bandwidth usage when downloading large Stitch HTML files
Error Handling and Exit Codes
The script relies on cURL's exit status to determine success or failure. When curl -L follows a redirect successfully and downloads the content to the specified output file, it returns exit code 0. The script propagates this success to the caller.
If any step fails—including connection timeouts, DNS resolution failures, or non-2xx HTTP responses after following redirects—cURL returns a non-zero exit code. The script catches these conditions, prints an error message, and exits with status 1, providing clear failure signaling for CI/CD pipelines or parent scripts.
Practical Usage Example
The following example demonstrates fetching a Stitch HTML file published to a GCS bucket using a signed URL:
# Define the GCS signed URL and output location
URL="https://storage.googleapis.com/my-bucket/stitch-output.html?X-Goog-Algorithm=GOOG4-RSA-SHA256..."
OUTPUT="tmp/stitch-output.html"
# Execute the script
./fetch-stitch.sh "$URL" "$OUTPUT"
# Verify the download succeeded
cat "$OUTPUT"
During execution, cURL automatically handles the 302 redirect from storage.googleapis.com to the temporary storage-download.googleapis.com endpoint, saving the final HTML content to the specified output path while the script waits for completion.
Summary
fetch-stitch.shusescurl -Lat line 24 to automatically follow Google Cloud Storage 302 redirects without custom parsing logic- The script at
plugins/stitch-build/skills/react-native/scripts/fetch-stitch.shrelies on standard cURL flags for reliability, compression, and timeout handling -f,-sS,--connect-timeout, and--compressedensure robust, efficient downloads across network conditions- Exit codes
0(success) and1(failure) enable reliable automation and error detection in build pipelines - This approach is provider-agnostic and handles any HTTP(S) URL using standard redirect semantics
Frequently Asked Questions
Does fetch-stitch.sh require special authentication handling for GCS signed URLs?
No special authentication handling is required within the script. Signed URLs contain embedded authentication tokens in the query parameters (such as X-Goog-Algorithm and X-Goog-Signature). The curl -L flag preserves these parameters when following the 302 redirect to storage-download.googleapis.com, allowing GCS to validate the signature at the final endpoint without additional headers.
What happens if the GCS redirect chain exceeds cURL's limit?
cURL follows up to 50 redirects by default before terminating with exit code 47. If Google Cloud Storage returns more than 50 successive redirects (highly unlikely for standard GCS operations), cURL stops and reports an error. The script would then detect the non-zero exit status and return failure code 1, indicating the download did not complete successfully.
How does the script handle temporary connectivity issues during redirects?
The --connect-timeout 10 flag applies to each TCP connection attempt in the redirect chain, including intermediate hops. If establishing a connection to any redirect target exceeds 10 seconds, cURL aborts the entire operation. Additionally, the -f flag ensures that HTTP 5xx errors encountered at any point in the redirect chain result in immediate failure rather than saving the error response to the output file.
Can fetch-stitch.sh handle redirects from cloud providers other than Google Cloud?
Yes. The curl -L implementation follows standard HTTP redirect semantics (301, 302, 303, 307, 308) defined in RFC specifications. Whether fetching from Amazon S3, Azure Blob Storage, or any other service that returns HTTP redirects, the script behaves identically. The provider-agnostic design means fetch-stitch.sh handles GCS redirects, S3 signed URL redirects, and any other HTTP-based redirection without code modifications.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →