interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / lib-httpd / http-429.sh

96 lines · 3630 bytes Raw Blame History
1#!/bin/sh
2
3# Script to return HTTP 429 Too Many Requests responses for testing retry logic.
4# Usage: /http_429/<test-context>/<retry-after-value>/<repo-path>
5#
6# The test-context is a unique identifier for each test to isolate state directories.
7# The retry-after-value can be:
8# - A number (e.g., "1", "2", "100") - sets Retry-After header to that many seconds
9# - "none" - no Retry-After header
10# - "invalid" - invalid Retry-After format
11# - "permanent" - always return 429 (never succeed)
12# - An HTTP-date string (RFC 2822 format) - sets Retry-After to that date
13#
14# On first call, returns 429. On subsequent calls (after retry), forwards to git-http-backend
15# unless retry-after-value is "permanent".
16
17# Extract test context, retry-after value and repo path from PATH_INFO
18# PATH_INFO format: /<test-context>/<retry-after-value>/<repo-path>
19path_info="${PATH_INFO#/}" # Remove leading slash
20test_context="${path_info%%/*}" # Get first component (test context)
21remaining="${path_info#*/}" # Get rest
22retry_after="${remaining%%/*}" # Get second component (retry-after value)
23repo_path="${remaining#*/}" # Get rest (repo path)
24
25# Extract repository name from repo_path (e.g., "repo.git" from "repo.git/info/refs")
26# The repo name is the first component before any "/"
27repo_name="${repo_path%%/*}"
28
29# Use current directory (HTTPD_ROOT_PATH) to hold state directory
30# Create a safe directory name from test_context, retry_after and repo_name
31# This ensures all requests for the same test context share the same state directory
32safe_name=$(echo "${test_context}-${retry_after}-${repo_name}" | tr '/' '_' | tr -cd 'a-zA-Z0-9_-')
33state="http-429-state-${safe_name}"
34
35# Check if this is the first call (no state directory exists), or if
36# the retry-after-value is "permanent", which indicates a 429 must be
37# returned for every request (even if the state directory exists).
38if test "$retry_after" != permanent && ! mkdir "$state" 2>/dev/null
39then
40 # Already returned 429 once, forward to git-http-backend
41 # Set PATH_INFO to just the repo path (without retry-after value)
42 # Set GIT_PROJECT_ROOT so git-http-backend can find the repository
43 # Use exec to replace this process so git-http-backend gets the updated environment
44 PATH_INFO="/$repo_path"
45 export PATH_INFO
46 # GIT_PROJECT_ROOT points to the document root where repositories are stored
47 # The script runs from HTTPD_ROOT_PATH, and www/ is the document root
48 if test -z "$GIT_PROJECT_ROOT"
49 then
50 # Construct path: current directory (HTTPD_ROOT_PATH) + /www
51 GIT_PROJECT_ROOT="$(pwd)/www"
52 export GIT_PROJECT_ROOT
53 fi
54 exec "$GIT_EXEC_PATH/git-http-backend"
55fi
56
57# Output HTTP 429 response
58printf "Status: 429 Too Many Requests\r\n"
59
60# Set Retry-After header based on retry_after value
61case "$retry_after" in
62 none)
63 # No Retry-After header
64 ;;
65 invalid)
66 printf "Retry-After: invalid-format-123abc\r\n"
67 ;;
68 permanent)
69 # Always return 429
70 printf "Retry-After: 1\r\n"
71 printf "Content-Type: text/plain\r\n"
72 printf "\r\n"
73 printf "Permanently rate limited\n"
74 exit 0
75 ;;
76 *)
77 # Check if it's a number
78 case "$retry_after" in
79 [0-9]*)
80 # Numeric value
81 printf "Retry-After: %s\r\n" "$retry_after"
82 ;;
83 *)
84 # Assume it's an HTTP-date format (passed as-is, URL decoded)
85 # Apache may URL-encode the path, so decode common URL-encoded characters
86 # %20 = space, %2C = comma, %3A = colon
87 retry_value=$(echo "$retry_after" | sed -e 's/%20/ /g' -e 's/%2C/,/g' -e 's/%3A/:/g')
88 printf "Retry-After: %s\r\n" "$retry_value"
89 ;;
90 esac
91 ;;
92esac
93
94printf "Content-Type: text/plain\r\n"
95printf "\r\n"
96printf "Rate limited\n"