config: prefer core.compression=5
[girocco.git] / cgi / snapshot.cgi
blobc16bedd5725e50505c7bf371fbf455a4938820dd
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 hdrout()
26 _kw="$1"
27 shift
28 printf '%s: %s\r\n' "$_kw" "$*"
31 errorhdrsct()
33 _ct="$1"; shift
34 printf '%s\r\n' "Status: $1 $2"
35 printf '%s\r\n' "Expires: Fri, 01 Jan 1980 00:00:00 GMT"
36 printf '%s\r\n' "Pragma: no-cache"
37 printf '%s\r\n' "Cache-Control: no-cache,max-age=0,must-revalidate"
38 [ -z "$3" ] || printf '%s\r\n' "$3"
39 printf '%s\r\n' "Content-Type: $_ct"
40 printf '\r\n'
43 errorhdrs()
45 errorhdrsct 'text/plain' "$@"
48 msglines()
50 while [ $# -gt 0 ]; do
51 printf '%s\n' "$1"
52 shift
53 done
56 methodnotallowed()
58 errorhdrs 405 "Method Not Allowed" "Allow: GET"
59 if [ $# -eq 0 ]; then
60 msglines "Method Not Allowed"
61 else
62 msglines "$@"
64 exit 0
67 forbidden()
69 errorhdrs 403 Forbidden
70 if [ $# -eq 0 ]; then
71 msglines "Forbidden"
72 else
73 msglines "$@"
75 exit 0
78 notfound()
80 errorhdrs 404 "Not Found"
81 if [ $# -eq 0 ]; then
82 msglines "Not Found"
83 else
84 msglines "$@"
86 exit 0
89 # Snapshots are too expensive to allow HEAD
90 [ "$REQUEST_METHOD" = "GET" ] || methodnotallowed
92 # The project must be valid
93 suffix="${PATH_INFO#*.git/}"
94 project="${PATH_INFO%/"$suffix"}"
95 project="${project#/}"
96 [ -n "$project" ] || forbidden
97 case "$suffix" in snapshot|snapshot/*) :;; *) forbidden; esac
98 suffix="${suffix#snapshot}"
99 suffix="${suffix#/}"
101 # Perform some basic sanity checking
102 if [ -z "$suffix" ]; then
103 # Must have an "h=" argument
104 case "&$QUERY_STRING&" in *[\&\;]"h="[!\&\;]*) :;; *) forbidden; esac
106 case "$suffix" in [!A-Za-z0-9_]*) forbidden; esac
107 case "/$project/" in *"/../"*|*"/./"*|*"/_"*|*"//"*) forbidden; esac
108 is_git_dir "$cfg_reporoot/$project" || notfound
110 # Give the 'bots indigestion
111 sleep 5
113 # Attempt to trigger a SIGPIPE if the connection has already been closed
114 hdrout "X-Project" "${project%.git}"
115 sleep 1
116 hdrout "X-Snapshot" "${suffix:-$QUERY_STRING}"
117 sleep 1
119 # Off to the races
120 projname="${project%.git}"
121 "$cfg_basedir/bin/throttle" ${throttle_on_connect_fail:+-t} -c snapshot \
122 -d "$projname" -m "$throttle_msg" "$cfg_cgiroot/gitweb.cgi"
123 exit 0