Skip to content

Commit 2f6ae60

Browse files
committed
Add bounded FSIS route diagnostic
1 parent cb3d84a commit 2f6ae60

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: FSIS route diagnostic
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
paths:
7+
- .github/workflows/fsis-route-diagnostic.yml
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
probe:
14+
name: Probe official FSIS routes
15+
runs-on: ubuntu-latest
16+
timeout-minutes: 3
17+
steps:
18+
- uses: actions/checkout@v4
19+
- name: Bounded no-body route probes
20+
shell: bash
21+
run: |
22+
set +e
23+
byte_cap=1048576
24+
python - <<'PY' | while IFS=$'\t' read -r route url_host url_path url; do
25+
import json
26+
from pathlib import Path
27+
from urllib.parse import urlsplit
28+
29+
config = json.loads(Path("pipeline/sources/us/fsis/config.json").read_text())
30+
for route, key in (
31+
("directory-by-name", "directory_by_name_url"),
32+
("directory-by-number", "directory_by_number_url"),
33+
("demographics", "demographics_url"),
34+
):
35+
url = config[key]
36+
parsed = urlsplit(url)
37+
print(f"{route}\t{parsed.hostname or '-'}\t{parsed.path or '/'}\t{url}")
38+
PY
39+
case "$route" in
40+
directory-by-name) expected_path='/sites/default/files/media_file/documents/MPI_Directory_by_Establishment_Name.csv' ;;
41+
directory-by-number) expected_path='/sites/default/files/media_file/documents/MPI_Directory_by_Establishment_Number.csv' ;;
42+
demographics) expected_path='/sites/default/files/media_file/documents/Dataset_Establishment_Demographic_Data.csv' ;;
43+
*) expected_path='' ;;
44+
esac
45+
if [[ "$url" != "https://www.fsis.usda.gov${expected_path}" || "$url_host" != 'www.fsis.usda.gov' || "$url_path" != "$expected_path" ]]; then
46+
printf 'route=%s config_shape=invalid curl_exit=not-run http_status=000 content_type=unknown bytes=0 final_host=- final_path=-\n' "$route"
47+
continue
48+
fi
49+
# The process substitution consumes at most byte_cap bytes and
50+
# discards them; --max-filesize also protects Content-Length cases.
51+
result="$(curl -sS -L --max-time 30 --max-filesize "$byte_cap" -o >(head -c "$byte_cap" >/dev/null) -w '%{http_code}|%{content_type}|%{size_download}|%{url_effective}' "$url" 2>/dev/null)"
52+
curl_exit=$?
53+
IFS='|' read -r http_status content_type bytes final_url <<< "$result"
54+
final_host="-"
55+
if [[ "$final_url" == https://* || "$final_url" == http://* ]]; then
56+
final_host="$(python -c 'import sys; from urllib.parse import urlsplit; print(urlsplit(sys.argv[1]).hostname or "-")' "$final_url" 2>/dev/null)"
57+
final_host="${final_host:-unknown}"
58+
fi
59+
printf 'route=%s requested_host=%s requested_path=%s curl_exit=%s http_status=%s content_type=%s bytes=%s final_host=%s\n' \
60+
"$route" "$url_host" "$url_path" "$curl_exit" "${http_status:-000}" \
61+
"${content_type:-unknown}" "${bytes:-0}" "$final_host"
62+
done

0 commit comments

Comments
 (0)