ssh: enable fetches using "git" user
[girocco.git] / cgi / snapshot.cgi
blob437c120d78e7d7f0b837d7c75a1ad274987d2fe0
1 #!/bin/sh
3 # snapshot.cgi -- throttle snapshot requests
4 # Copyright (C) 2015 Kyle J. McKay. All rights reserved.
5 # License GPLv2+: GNU GPL version 2 or later.
6 # www.gnu.org/licenses/gpl-2.0.html
7 # This is free software: you are free to change and redistribute it.
8 # There is NO WARRANTY, to the extent permitted by law.
10 set -e
12 . @basedir@/shlib.sh
14 # Set to non-empty to throttle if the initial throttle service connect fails
15 throttle_on_connect_fail=
17 # Supplemental message to be included in the throttle result
18 throttle_msg=\
19 'Ravenous roving robots are probably greedily chowing down on our services
20 right now.
22 We are valiantly trying to fight them off to improve service availability.'
24 errorhdrsct()
26 _ct="$1"; shift
27 printf '%s\r\n' "Status: $1 $2"
28 printf '%s\r\n' "Expires: Fri, 01 Jan 1980 00:00:00 GMT"
29 printf '%s\r\n' "Pragma: no-cache"
30 printf '%s\r\n' "Cache-Control: no-cache,max-age=0,must-revalidate"
31 [ -z "$3" ] || printf '%s\r\n' "$3"
32 printf '%s\r\n' "Content-Type: $_ct"
33 printf '\r\n'
36 errorhdrs()
38 errorhdrsct 'text/plain' "$@"
41 msglines()
43 while [ $# -gt 0 ]; do
44 printf '%s\n' "$1"
45 shift
46 done
49 methodnotallowed()
51 errorhdrs 405 "Method Not Allowed" "Allow: GET"
52 if [ $# -eq 0 ]; then
53 msglines "Method Not Allowed"
54 else
55 msglines "$@"
57 exit 0
60 forbidden()
62 errorhdrs 403 Forbidden
63 if [ $# -eq 0 ]; then
64 msglines "Forbidden"
65 else
66 msglines "$@"
68 exit 0
71 notfound()
73 errorhdrs 404 "Not Found"
74 if [ $# -eq 0 ]; then
75 msglines "Not Found"
76 else
77 msglines "$@"
79 exit 0
82 # Snapshots are too expensive to allow HEAD
83 [ "$REQUEST_METHOD" = "GET" ] || methodnotallowed
85 # The project must be valid
86 suffix="${PATH_INFO#*.git/}"
87 project="${PATH_INFO%/$suffix}"
88 project="${project#/}"
89 [ -n "$project" ] || forbidden
90 case "$suffix" in snapshot|snapshot/*) :;; *) forbidden; esac
91 suffix="${suffix#snapshot}"
92 suffix="${suffix#/}"
94 # Perform some basic sanity checking
95 if [ -z "$suffix" ]; then
96 # Must have an "h=" argument
97 case "&$QUERY_STRING&" in *[\&\;]"h="[!\&\;]*) :;; *) forbidden; esac
99 case "$suffix" in [!A-Za-z0-9_]*) forbidden; esac
100 case "/$project/" in *"/../"*|*"/./"*|*"/_"*|*"//"*) forbidden; esac
101 is_git_dir "$cfg_reporoot/$project" || notfound
103 # Off to the races
104 projname="${project%.git}"
105 "$cfg_basedir/bin/throttle" ${throttle_on_connect_fail:+-t} -c snapshot \
106 -d "$projname" -m "$throttle_msg" "$cfg_cgiroot/gitweb.cgi"
107 exit 0