Skip to content

Commit a9ab399

Browse files
ppennaCopilot
andcommitted
nvx: name the HTTP statuses of the GitHub release client
The GitHub release client of nvx download spelled the HTTP statuses that it acts on as bare literals, and the same class in different forms: the success check was !(200..300).contains(..) in download but the range pattern 200..=299 in json, the redirect check (300..400) appeared twice, the least valid status was a bare 100, and 404 was repeated in json and hint next to 401 and 403. Name them once, in module-private constants whose doc comments cite RFC 9110 section 15: MIN_STATUS, SUCCESSFUL, REDIRECTION, UNAUTHORIZED, FORBIDDEN, and NOT_FOUND. json matches a successful status with a guard first, as 200..=299 did, so the arms keep their order. The values, requests, redirects, errors, and hints are unchanged. Tests: the 13 host::cli::download::github unit tests, which cover redirects, response parsing, error responses, and hints, pass unchanged at the base 2e30219 and with this change on Windows. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 2e30219 commit a9ab399

1 file changed

Lines changed: 40 additions & 17 deletions

File tree

‎nvx/src/host/cli/download/github.rs‎

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
use std::fs;
1212
use std::io::{self, IsTerminal, Write};
13+
use std::ops::Range;
1314
use std::path::{Path, PathBuf};
1415
use std::process::{Command, Stdio};
1516

@@ -43,6 +44,22 @@ const STALL_SECONDS: u32 = 60;
4344
const RESPONSE_NAME: &str = "response";
4445
/// The file of the scratch directory that receives the headers of each response.
4546
const HEADERS_NAME: &str = "response.headers";
47+
/// The least HTTP status: RFC 9110 (section 15) defines three-digit statuses from 100, the first
48+
/// of the informational class.
49+
const MIN_STATUS: u16 = 100;
50+
/// The successful HTTP statuses, the 2xx class of RFC 9110 (section 15.3).
51+
const SUCCESSFUL: Range<u16> = 200..300;
52+
/// The redirection HTTP statuses, the 3xx class of RFC 9110 (section 15.4), whose `location`
53+
/// [`GitHub::get`] follows.
54+
const REDIRECTION: Range<u16> = 300..400;
55+
/// HTTP 401 Unauthorized (RFC 9110, section 15.5.2): GitHub rejected the token.
56+
const UNAUTHORIZED: u16 = 401;
57+
/// HTTP 403 Forbidden (RFC 9110, section 15.5.4): the token lacks access, or the rate limit is
58+
/// exhausted.
59+
const FORBIDDEN: u16 = 403;
60+
/// HTTP 404 Not Found (RFC 9110, section 15.5.5): GitHub answers it for a missing release, and
61+
/// for a repository that the request cannot read.
62+
const NOT_FOUND: u16 = 404;
4663

4764
/// How a query of the REST API fails.
4865
#[derive(Debug)]
@@ -121,7 +138,7 @@ impl GitHub {
121138
let progress = io::stderr().is_terminal();
122139
let context = format!("the download of {}", asset.name);
123140
let response = self.get(&asset.url, BINARY, destination, progress, &context)?;
124-
if !(200..300).contains(&response.status) {
141+
if !SUCCESSFUL.contains(&response.status) {
125142
let body = fs::read(destination).unwrap_or_default();
126143
return Err(Exit::error(
127144
ErrorCode::Unavailable,
@@ -161,14 +178,16 @@ impl GitHub {
161178
Err(error) => return Err(io_error(ErrorCode::Io, &error, &body).into()),
162179
};
163180
match response.status {
164-
200..=299 => serde_json::from_slice(&bytes).map(Some).map_err(|_| {
165-
Exit::error(
166-
ErrorCode::Unavailable,
167-
"GitHub release query returned an invalid response",
168-
)
169-
.into()
170-
}),
171-
404 if missing_ok => Ok(None),
181+
status if SUCCESSFUL.contains(&status) => {
182+
serde_json::from_slice(&bytes).map(Some).map_err(|_| {
183+
Exit::error(
184+
ErrorCode::Unavailable,
185+
"GitHub release query returned an invalid response",
186+
)
187+
.into()
188+
})
189+
}
190+
NOT_FOUND if missing_ok => Ok(None),
172191
status => {
173192
let advice = match hint(status, response.rate_limited(), self.token.is_some()) {
174193
"" => String::new(),
@@ -225,7 +244,7 @@ impl GitHub {
225244
};
226245
let response = self.curl.request(&request, context)?;
227246
match response.location {
228-
Some(location) if (300..400).contains(&response.status) => current = location,
247+
Some(location) if REDIRECTION.contains(&response.status) => current = location,
229248
_ => return Ok(response),
230249
}
231250
}
@@ -429,7 +448,7 @@ fn read_response(path: &Path, url: &str) -> Result<Option<Response>> {
429448
.iter()
430449
.find(|(name, _)| name == "location")
431450
.map(|(_, location)| location.as_str())
432-
.filter(|location| (300..400).contains(&status) && !location.is_empty())
451+
.filter(|location| REDIRECTION.contains(&status) && !location.is_empty())
433452
.map(|location| resolved(url, location));
434453
Ok(Some(Response {
435454
status,
@@ -445,7 +464,7 @@ fn status_code(line: &str) -> Option<u16> {
445464
if code.len() != 3 || !code.bytes().all(|byte| byte.is_ascii_digit()) {
446465
return None;
447466
}
448-
code.parse().ok().filter(|status| *status >= 100)
467+
code.parse().ok().filter(|status| *status >= MIN_STATUS)
449468
}
450469

451470
/// Returns `value`, the `what` of a request, quoted for the configuration of `curl`, or fails for
@@ -674,14 +693,18 @@ fn detail(message: &str) -> String {
674693
/// gave it, given whether the rate limit is exhausted and whether the query carried a token.
675694
fn hint(status: u16, rate_limited: bool, authenticated: bool) -> &'static str {
676695
match status {
677-
401 | 403 | 404 if !authenticated => "set GH_TOKEN to a token that can read the repository",
678-
401 => "the configured GitHub token was rejected; refresh or replace it",
679-
403 if rate_limited => "the GitHub API rate limit is exhausted; retry later",
680-
403 => {
696+
UNAUTHORIZED | FORBIDDEN | NOT_FOUND if !authenticated => {
697+
"set GH_TOKEN to a token that can read the repository"
698+
}
699+
UNAUTHORIZED => "the configured GitHub token was rejected; refresh or replace it",
700+
FORBIDDEN if rate_limited => "the GitHub API rate limit is exhausted; retry later",
701+
FORBIDDEN => {
681702
"the configured GitHub token lacks access; grant it read access to the repository \
682703
contents and authorize it for organization single sign-on"
683704
}
684-
404 => "verify --repository and that the configured GitHub token can read that repository",
705+
NOT_FOUND => {
706+
"verify --repository and that the configured GitHub token can read that repository"
707+
}
685708
_ => "",
686709
}
687710
}

0 commit comments

Comments
 (0)