Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/lib-httpd/http-429.sh # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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/// 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: /// 19 path_info="${PATH_INFO#/}" # Remove leading slash 20 test_context="${path_info%%/*}" # Get first component (test context) 21 remaining="${path_info#*/}" # Get rest 22 retry_after="${remaining%%/*}" # Get second component (retry-after value) 23 repo_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 "/" 27 repo_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 32 safe_name=$(echo "${test_context}-${retry_after}-${repo_name}" | tr '/' '_' | tr -cd 'a-zA-Z0-9_-') 33 state="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). 38 if test "$retry_after" != permanent && ! mkdir "$state" 2>/dev/null 39 then 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" 55 fi 56 57 # Output HTTP 429 response 58 printf "Status: 429 Too Many Requests\r\n" 59 60 # Set Retry-After header based on retry_after value 61 case "$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 ;; 92 esac 93 94 printf "Content-Type: text/plain\r\n" 95 printf "\r\n" 96 printf "Rate limited\n"