Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

provider: an upstream refusal with an empty body or a web page says what it was

yetone4ed6b42f6b0e
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index aeb11763b..6255264a3 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -161,7 +161,8 @@ recorded as a prediction failure (`noPrediction`).
- A usage record carries its prompt's cache beside `in` and `out` (#1839): `cache_read` (tokens read from the vendor's cache) and `cache_write` (cache creation, Anthropic's `cache_creation_input_tokens`; `cache_write_1h` the part written for an hour). `in` holds neither. Both are written on every record, 0 too, so a prompt that missed the cache reads as that. A record from before then has neither and reads as 0. Every route fills them: Anthropic's messages, Chat (OpenAI's `prompt_tokens_details.cached_tokens`, DeepSeek's `prompt_cache_hit_tokens`), Responses (`input_tokens_details.cached_tokens`) and Gemini (`cachedContentTokenCount`), passed through or translated, whole or streamed, a plugin's fetch, and the Claude subscription bridge. System One's decision calls report no cache. `TestUsageRecordsCacheTokensOnEveryRoute`, `TestPluginUsageRecordsCacheTokens`, `TestClaudeSubscriptionUsageKeepsCacheTokens`, `TestRecordAlwaysCarriesCacheTokens`.
- What the vendor read from its cache reaches the agent in the names its SDK reads, not only the record. A relayed Chat reply whose usage tells the cache in another vendor's names (a relay's Anthropic `cache_read_input_tokens` and `cache_creation_input_tokens`, DeepSeek's `prompt_cache_hit_tokens` or Moonshot's top-level `cached_tokens` beside a 0 in `prompt_tokens_details`) is told in OpenAI's too, streamed (`chatTidy`) and whole (`chatWhole`), by `openAIUsage` in [`chattidy.go`](../../internal/gateway/chattidy.go): `prompt_tokens_details.cached_tokens` (and `cache_write_tokens`), with `prompt_tokens` and `total_tokens` made to hold the cached part where the vendor left it out. Every other field stays, and a usage already in OpenAI's names, or with no cache, goes byte for byte; one in OpenAI's names alone isn't even decoded (`mayMendUsage`). pi-ai, which DeepSeek Harness and Pi use, reads `prompt_tokens_details.cached_tokens` first, a 0 there too, and maps no Anthropic names at all, so such a relay showed DSH 缓存命中 0% while magpie had counted the hit (B3TT3R on Discord asked; DeepSeek's own API sends both names and was already read right). Relayed Responses and Messages replies, translated replies and a plugin's need nothing: their usage is written in the agent's own names. `TestAgentReadsCacheTokensOnEveryRoute`, `TestPluginAgentReadsCacheTokens`, `TestOpenAIUsageLeavesOpenAIsAlone`, `TestMayMendUsageOnlyOtherNames` in [`usage_cache_agent_test.go`](../../internal/gateway/usage_cache_agent_test.go).
- A request whose key is a Claude sign-in (`claudeSignIn`: `Authorization: Bearer sk-ant-oat…`, Claude Code signed in to claude.ai with magpie as its base URL) is never told a 401, nor a 403 saying "OAuth token has been revoked". magpie never passes that sign-in on, so such an answer is a provider refusing magpie's credential, or one of magpie's own Claude accounts lapsing; told it, Claude Code renews its own sign-in before each of its retries (ten for one request) and then asks to `/login`. Any error whose message mentions x-api-key, whatever its status, Claude Code tells as "Not logged in · Please run /login" too. It is told as a 502 in magpie's words, an `api_error` saying "<provider> refused magpie's credential (HTTP 401): …" with x-api-key said as "API key", and any other error such a request is told keeps its status with x-api-key said as "API key", as does a stream's error after its 200 (`keepsSignIn`, `claude_signin.go`). Recent calls and the usage log keep the provider's status and message. Any other 403, and a request with magpie's key or an API key, is answered as before. The sign-in's beta (`oauth-…` in `anthropic-beta`) isn't passed on either (`askedBetas`), so a provider gets the request as Claude Code with magpie's key sends it; a provider's own `header.anthropic-beta` still goes.
-- A 2xx that can't be an API's answer is the 502 it stands for (`notAnAPIReply`, #1012): a web page (`text/html`, or a body that begins as one: a sign-in page, Cloudflare's challenge), a body with nothing in it, or one sent as JSON that doesn't begin as JSON. Every upstream request goes through it (`forwardOnce`, and the built-in clients' own: the ChatGPT backend's `/responses`), so it fails over, rests and is logged as a 502 does, its reason naming the page's title. Only the first bytes are peeked, which a stream waits for anyway, and a compressed body is left as it came.
+- A 2xx that can't be an API's answer is the 502 it stands for (`notAnAPIReply`, #1012): a web page (`text/html`, or a body that begins as one: a sign-in page, Cloudflare's challenge), a body with nothing in it, or one sent as JSON that doesn't begin as JSON. Every upstream request goes through it (`forwardOnce`, and the built-in clients' own: the ChatGPT backend's `/responses`), so it fails over, rests and is logged as a 502 does, its reason naming the page's title.
+- An error status whose body holds no message of its own says what it was (`provider.Unsaid`, through `APIError` and the plan-quota and drawing readers): a web page by its title as plain words, a firewall's block page (`EdgeBlocked`: Alibaba Cloud's, Cloudflare's "you have been blocked", "used Cloudflare to restrict access" and `error code: 1005`–`1020`) with `BlockedHint` after it, and an empty body under an HTTP status as refused with no details, often a firewall or proxy in front of the API (`NoDetails`; an empty `response.failed` event, whose fallback is no status, still reads "upstream stream failed"). A bare "403 Forbidden" told a relay's user nothing (maralica on Discord). A JSON error still reads as its message. `TestAPIErrorUnsaid`, `TestRelayRefusalSaysWhatItWas`. Only the first bytes are peeked, which a stream waits for anyway, and a compressed body is left as it came.
- A 2xx from Zhipu or Z.ai holding the platform's refusal envelope (`{"code":1000,"msg":…,"success":false}`, which their Responses API at `/api/v1` answers a refused key) is the refusal it stands for (`provider.ZhipuEnveloped` in `forwardOnce`): 401 for a key refused, 429 for a limit or no balance, as Zhipu's error codes give, so it fails over and rests as those do and the agent is told why. `TestZhipuResponsesRefusalIsRefusal`.
- A streamed request is never left silent while its vendor says nothing. `holdWriter.watch` runs beside each try: past `keepHeldAfter` (15s) it sends a held try's agent the stream's 200 and SSE comments (`keepAlive`), none of the try, so another candidate may still answer in the same stream; once the reply passes, it puts a comment between two of its lines after `keepaliveEvery` of quiet. It stops after `keepaliveLongest` (5 minutes) with nothing from the vendor. A try waiting for its key's or account's slot or for room in its minute (`lanes.take`, `rpms.wait`) is kept alive the same way (`keepQueued`), for as long as it waits; once the 200 is out, the try is held, and its failure or its queue turning it away reaches the agent as the stream's error (`failTo`), not a status (coeo91 on Discord). A wait shorter than `keepHeldAfter` sends nothing, so its 429 keeps its `Retry-After`. A proxy in front such as Cloudflare (125s) otherwise ends the request (#947). Gemini streams get no comments (#934). A vendor that then ignores `stream:true` and answers 200 with its reply whole has it sent as the agent's stream (`wholeAsStream`: Chat, Responses, Anthropic), not as the stream's error, while a reply under a type that says neither JSON nor a stream is held as a stream when its first bytes are one (`sseStart`), read event by event like one that says so, so an error among its frames still goes to the next candidate (#1418: it went through at once); a 200 that is no reply of the protocol's fails as a translated reply that didn't stream does (502, "… did not stream"), for the next candidate to answer, and the last one's reaches the agent as the stream's error. A held stream that has sent nothing but the frames before a reply (`response.created`, `response.in_progress`, `codex.rate_limits`, pings, an empty part begun, and any event `streamEvent` doesn't know to carry text, reasoning, a call or a reply's end (`replyEvent`), or an item not known that isn't a call or a tool's (`streamPart.said`): `eventLead`; Aliceonly on #1418: an unknown event was taken for content and let a frames-only 200 through, and its `server_is_overloaded` reached Codex with five candidates left) stays held as long as its agent is kept alive, up to `holdLead` (4 minutes, under the 300s Codex waits for an event; `keptQuiet`), so a `server_is_overloaded` that comes after it is a failure the next candidate answers (#1418: let through at 15s, the ChatGPT backend's failure 36s in ended Codex's turn with sub2api, next in the group, never asked). A stream whose agent can't be kept alive (Gemini's) is let through after `holdLongest` (15s), as before. Codex counts only events toward that 300s, not comments, so the bound is the request's, not each try's: once Codex on Responses has heard no event for `agentQuietMost` (4 minutes) since the request began or its last event (`keptAlive.tooQuiet`), the try in hand is let through (`scan`, `keepQuiet`), and no group replan or same-one retry is taken whose pause would end past it (fadenoob on Discord: held tries and replan pauses added up past 300s and Codex reconnected on "idle timeout waiting for SSE"). A translated reply whose upstream stream stops before it said its reply had — an Anthropic one with no stop_reason and no message_stop, a Responses one with no `response.completed`, the same two a relayed one is told by (`sniff.whole`; a Chat stream may end without `[DONE]` and be whole), a Gemini or Code Assist one whose last chunk has no candidate's `finishReason` (`geminiEnds`, #1643: `usageMetadata` is no end, Vertex AI sends it on every chunk; a chunk with no candidate at all is the empty reply's to answer, #667), and a relayed Gemini one told the same way (#1644; it ends in Gemini's own `{"error":{"code":502,…,"status":"UNAVAILABLE"}}` data line from `streamFailure`, `TestGeminiRelayEndedShort`; whatever a relayed stream gets appended, that error or a filled-in `finish_reason`, begins after a blank line, two newlines when the upstream's last write stopped inside a line, so it is an event of its own rather than more of the half-written one, #2446, `TestPassthroughStreamCutMidLine`, `TestGeminiCompatFinishAfterPartialLine`), and a Chat one from Gemini's OpenAI-compatible API (`geminiCompat`) with no `finish_reason`, relayed or translated (`saysItsEnd`; Linka on Discord: the upstream hung up between a whole function call and its `finish_reason`, and Pi threw the call away as "Stream ended without finish_reason"; that API counts the usage only once the generation is done, so when the last chunk carried it only the `finish_reason` was lost and it is filled in, `tool_calls` after a call and `stop` otherwise, as a relayed chunk before `[DONE]` or a translated reply's end, and the reply counts as whole (`chatTail`); only one with no usage at its end is a cut, `TestGeminiCompatRelayEndedShort`, `TestGeminiCompatTranslatedEndedShort`) — ends the turn with "the reply ended before it was complete" (`errEndedShort`, `TestResponsesUpstreamEndedShort`, `TestGeminiUpstreamEndedShort`), asked again in place where the upstream goes on from a prefill (`continuation.go`). A client that asked for no stream is told the same 502 of such a cut (`translate`, `TestNonStreamEndedShort`, `TestGeminiNonStreamEndedShort`), rather than handed what came as its whole reply. A translated reply from an Anthropic or Responses upstream that ended but said nothing (no text, call, search or image) and counted no token at all is no reply either, whatever its end says (`repliesCounted`, #2086: an Anthropic-compatible proxy's 200 ended a Codex turn as `response.completed` with no output, no usage and no served model, and Codex didn't ask again). It is the empty reply's to answer (#667): asked again in place, then failed with "an empty reply", streamed or not, for every client (`TestUncountedEmptyReplyFails`, `TestUncountedEmptyReplyFailsEveryClient`). Claude's own empty `end_turn` counts its prompt and stays a whole reply (`TestWellFormedStreamsStillComplete`). A reply passed through in its own protocol isn't read this way.
- A vendor's error keeps its `code` and `param` on its way to an OpenAI-shaped client (`writeVendorError`), where `writeError` used to set both to null: a status error relayed or translated, and a request refused inside a Responses stream. A Responses stream's `error` event or failed response whose error is of type `invalid_request_error` is the request at fault (`requestRefused`, read by its type, not its words): its `Event` carries status 400 and the vendor's error (`Event.Said`), so a client answered whole gets the vendor's 400 with its code and param (the whole-translated path wrote 502 `api_error`), a reply cut by it isn't resumed (`continuation.again`), and a translated stream's error says the vendor's type, code and param (`saidError`), so `holdWriter` reads it as the 400 it is. Before, the hold read magpie's `api_error` as a 502: the same request was sent three times, then to the other accounts, and every one rested (magpie-releases#18: the ChatGPT backend answers `access_programs` naming a program the workspace hasn't got with a 200 stream whose error is `access_program_not_enabled`). Asked once, nobody rests ([`codex_access_programs_test.go`](../../internal/gateway/codex_access_programs_test.go)).
diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.go
index 6bb026739..e7d7e8e63 100644
--- a/internal/gateway/codex_backend.go
+++ b/internal/gateway/codex_backend.go
@@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
- "html"
"io"
"log"
"net/http"
@@ -1619,10 +1618,8 @@ func codexSaidNothing(rest, model, who string, res *http.Response, msg []byte) (
said := " and said nothing more"
if page {
said = " with a web page in place of an API error"
- if m := pageTitle.FindSubmatch(msg); m != nil {
- if title := strings.Join(strings.Fields(html.UnescapeString(string(m[1]))), " "); title != "" {
- said += " (“" + clipRunes(title, 120) + "”)"
- }
+ if title := provider.PageTitle(msg); title != "" {
+ said += " (“" + title + "”)"
}
}
return who + " answered " + what + " for " + model + " " + res.Status + said +
diff --git a/internal/gateway/draw.go b/internal/gateway/draw.go
index fa8de26e5..9195ef2ef 100644
--- a/internal/gateway/draw.go
+++ b/internal/gateway/draw.go
@@ -1099,7 +1099,7 @@ func (s *Server) sendWith(ctx context.Context, p provider.Provider, method, url,
if drawsCodex(p) && res.StatusCode == http.StatusForbidden {
hint = fmt.Sprintf(" — ChatGPT turned %s's images away: its plan or workspace may not draw, or not with this model (Codex CLI draws with %s)", p.Name, codexDrawer)
}
- return b, res.StatusCode, fmt.Errorf("%s: %d %s%s%s", provider.HostOf(url), res.StatusCode, http.StatusText(res.StatusCode), vendorSaid(vendorMessage(b)), hint)
+ return b, res.StatusCode, fmt.Errorf("%s: %d %s%s%s", provider.HostOf(url), res.StatusCode, http.StatusText(res.StatusCode), errorSaid(b), hint)
}
return b, res.StatusCode, nil
}
@@ -1175,9 +1175,23 @@ func vendorMessage(b []byte) string {
return s
}
}
+ // a web page is said for what it is, not as markup
+ if said, ok := provider.WebPage(b); ok {
+ return said
+ }
return string(b)
}
+// errorSaid is what an HTTP error's body said, for after its status: the
+// vendor's message, a web page by its title, or, for an empty body, that
+// it was refused with no details.
+func errorSaid(b []byte) string {
+ if said, ok := provider.Unsaid(b); ok {
+ return ": " + said
+ }
+ return vendorSaid(vendorMessage(b))
+}
+
// drawImages asks an images API: generations, or edits with the images
// sent along.
func (s *Server) drawImages(ctx context.Context, p provider.Provider, model string, d drawing) (drawn, int, error) {
diff --git a/internal/gateway/draw_workbuddy.go b/internal/gateway/draw_workbuddy.go
index fd5503e87..1a40809e1 100644
--- a/internal/gateway/draw_workbuddy.go
+++ b/internal/gateway/draw_workbuddy.go
@@ -183,7 +183,7 @@ func wbFetchImageModels(ctx context.Context, client *http.Client, p provider.Pro
defer res.Body.Close()
b, _ := io.ReadAll(io.LimitReader(res.Body, 8<<20))
if res.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("%s's config: %d%s", p.Name, res.StatusCode, vendorSaid(vendorMessage(b)))
+ return nil, fmt.Errorf("%s's config: %d%s", p.Name, res.StatusCode, errorSaid(b))
}
var env struct {
Code json.RawMessage `json:"code"`
diff --git a/internal/gateway/embeddings.go b/internal/gateway/embeddings.go
index d5b85af3a..71bb8b2ac 100644
--- a/internal/gateway/embeddings.go
+++ b/internal/gateway/embeddings.go
@@ -326,7 +326,7 @@ func (s *Server) retrieveAs(ctx context.Context, p provider.Provider, url, ctype
if res.StatusCode == http.StatusNotFound || res.StatusCode == http.StatusMethodNotAllowed {
hint = " — " + p.Name + " may not serve this API"
}
- return b, got, res.StatusCode, fmt.Errorf("%s: %d %s%s%s", provider.HostOf(url), res.StatusCode, http.StatusText(res.StatusCode), vendorSaid(vendorMessage(b)), hint)
+ return b, got, res.StatusCode, fmt.Errorf("%s: %d %s%s%s", provider.HostOf(url), res.StatusCode, http.StatusText(res.StatusCode), errorSaid(b), hint)
}
return b, got, res.StatusCode, nil
}
diff --git a/internal/gateway/notapi.go b/internal/gateway/notapi.go
index c4f4f6a99..6f0c73bc2 100644
--- a/internal/gateway/notapi.go
+++ b/internal/gateway/notapi.go
@@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"encoding/json"
- "html"
"io"
"net/http"
"regexp"
@@ -59,9 +58,6 @@ func notAnAPIReply(res *http.Response, name string) *http.Response {
// htmlStart is how a web page begins.
var htmlStart = regexp.MustCompile(`(?i)^<(!doctype\s+html|html[\s>]|head[\s>]|body[\s>])`)
-// pageTitle is a web page's title.
-var pageTitle = regexp.MustCompile(`(?is)<title[^>]*>(.*?)</title>`)
-
// webPage says what a page served in place of a reply was, by its title,
// and for a firewall's block page what to do about it.
func webPage(res *http.Response, ct string) string {
@@ -70,10 +66,8 @@ func webPage(res *http.Response, ct string) string {
ct = "no Content-Type"
}
why := "answered " + res.Status + " with a web page (" + strings.TrimSpace(strings.SplitN(ct, ";", 2)[0]) + "), not an API reply"
- if m := pageTitle.FindSubmatch(b); m != nil {
- if t := strings.Join(strings.Fields(html.UnescapeString(string(m[1]))), " "); t != "" {
- why += ": “" + clipRunes(t, 120) + "”"
- }
+ if t := provider.PageTitle(b); t != "" {
+ why += ": “" + t + "”"
}
if provider.EdgeBlocked(b) {
why += " — " + provider.BlockedHint
diff --git a/internal/gateway/video.go b/internal/gateway/video.go
index 827a753e9..800202286 100644
--- a/internal/gateway/video.go
+++ b/internal/gateway/video.go
@@ -1139,7 +1139,11 @@ func (s *Server) remoteVideoContent(ctx context.Context, w http.ResponseWriter,
defer res.Body.Close()
if res.StatusCode != 200 {
b, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
- writeError(w, provider.Chat, res.StatusCode, p.Name+": "+vendorMessage(b))
+ said, ok := provider.Unsaid(b)
+ if !ok {
+ said = vendorMessage(b)
+ }
+ writeError(w, provider.Chat, res.StatusCode, p.Name+": "+said)
return
}
w.Header().Set("Content-Type", cmp.Or(res.Header.Get("Content-Type"), "video/mp4"))
diff --git a/internal/gateway/zcode_blocked_test.go b/internal/gateway/zcode_blocked_test.go
index 3974daf6f..6ea45a9a3 100644
--- a/internal/gateway/zcode_blocked_test.go
+++ b/internal/gateway/zcode_blocked_test.go
@@ -133,6 +133,62 @@ func TestEdgeBlockedSaidPlainly(t *testing.T) {
}
}
+// A custom Responses relay refusing with 403 and no message of its own
+// tells the agent what it was, not a bare "403 Forbidden" (maralica on
+// Discord): Cloudflare's block page as a firewall's, another page by its
+// title, an empty body as a refusal with no details. Its JSON error still
+// reads as its message.
+func TestRelayRefusalSaysWhatItWas(t *testing.T) {
+ cf := `<!DOCTYPE html>
+<html class="no-js" lang="en-US"><head><title>Attention Required! | Cloudflare</title></head>
+<body><div id="cf-error-details"><h1 data-translate="block_headline">Sorry, you have been blocked</h1>
+<span class="cf-footer-item">Cloudflare Ray ID: <strong>8d1c2f3a4b5c6d7e</strong></span></div></body></html>`
+ for _, c := range []struct {
+ name, ct, body string
+ want []string
+ }{
+ {"cloudflare", "text/html; charset=UTF-8", cf, []string{"403 Forbidden: a web page, not an API reply: “Attention Required! | Cloudflare”", provider.BlockedHint}},
+ {"page", "text/html", "<html><head><title>403 Forbidden</title></head><body><center><h1>403 Forbidden</h1></center><hr><center>nginx</center></body></html>", []string{"403 Forbidden: a web page, not an API reply: “403 Forbidden”"}},
+ {"empty", "", "", []string{"403 Forbidden: " + provider.NoDetails}},
+ {"json", "application/json", `{"error":{"message":"this key may not use gpt-5.5","type":"forbidden"}}`, []string{"this key may not use gpt-5.5"}},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ fresh(t)
+ relay := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if c.ct != "" {
+ w.Header().Set("Content-Type", c.ct)
+ }
+ w.WriteHeader(http.StatusForbidden)
+ io.WriteString(w, c.body)
+ }))
+ t.Cleanup(relay.Close)
+ if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Models: []string{"gpt-5.5"}, Responses: relay.URL + "/v1"}); err != nil {
+ t.Fatal(err)
+ }
+ code, reply := postTo(t, New(), "/v1/responses", `{"model":"relay/gpt-5.5","input":"hi"}`)
+ if code != http.StatusForbidden || strings.Contains(reply, "<") || strings.Contains(reply, "&lt;") {
+ t.Fatalf("%d %s", code, reply)
+ }
+ var v struct {
+ Error struct {
+ Message string `json:"message"`
+ } `json:"error"`
+ }
+ if err := json.Unmarshal([]byte(reply), &v); err != nil {
+ t.Fatalf("%v: %s", err, reply)
+ }
+ if strings.Contains(v.Error.Message, "<") {
+ t.Fatalf("markup in %q", v.Error.Message)
+ }
+ for _, w := range c.want {
+ if !strings.Contains(v.Error.Message, w) {
+ t.Errorf("missing %q in %q", w, v.Error.Message)
+ }
+ }
+ })
+ }
+}
+
// The ZCode plugin's answer to the Start Plan's 405 code 3012 (0.1.27 on,
// plugins#348) is an account ZCode's risk control blocked: it rests two
// hours as restricted, the account rather than one model, and is lifted
diff --git a/internal/provider/decide_test.go b/internal/provider/decide_test.go
index d888a998b..7d8981f72 100644
--- a/internal/provider/decide_test.go
+++ b/internal/provider/decide_test.go
@@ -515,10 +515,11 @@ func TestAPIErrorShapes(t *testing.T) {
if !strings.HasPrefix(got, `400 Bad Request: {"code":400`) || !strings.HasSuffix(got, "…") || len([]rune(got)) > 320 {
t.Fatal(got)
}
- if got := APIError([]byte("<html><body>bad</body></html>"), "400 Bad Request"); got != "400 Bad Request" {
+ if got := APIError([]byte("<html><body>bad</body></html>"), "400 Bad Request"); got != "400 Bad Request: a web page, not an API reply" {
t.Fatal(got)
}
- if got := APIError(nil, "400 Bad Request"); got != "400 Bad Request" {
+ // other markup still says nothing worth showing
+ if got := APIError([]byte("<Error><Code>AccessDenied</Code></Error>"), "403 Forbidden"); got != "403 Forbidden" {
t.Fatal(got)
}
// Command Code's refusal of a Go plan's key says where it works (#969)
@@ -528,6 +529,80 @@ func TestAPIErrorShapes(t *testing.T) {
}
}
+// An upstream refusal with no message of its own says what it was, not a
+// bare "403 Forbidden" (maralica's relay on Discord): Cloudflare's block
+// page as a firewall's, with its title; any other page by its title, as
+// plain words; an empty body as a refusal with no details. A JSON error
+// still reads as its message.
+func TestAPIErrorUnsaid(t *testing.T) {
+ cf := `<!DOCTYPE html>
+<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
+<!--[if gt IE 8]><!--> <html class="no-js" lang="en-US"> <!--<![endif]-->
+<head>
+<title>Attention Required! | Cloudflare</title>
+<meta charset="UTF-8" />
+</head>
+<body>
+ <div id="cf-wrapper">
+ <div id="cf-error-details" class="cf-error-details-wrapper">
+ <div class="cf-wrapper cf-header cf-error-overview">
+ <h1 data-translate="block_headline">Sorry, you have been blocked</h1>
+ <h2 class="cf-subheadline"><span data-translate="unable_to_access">You are unable to access</span> relay.example.com</h2>
+ </div>
+ <span class="cf-footer-item sm:block sm:mb-1">Cloudflare Ray ID: <strong class="font-semibold">8d1c2f3a4b5c6d7e</strong></span>
+ </div>
+ </div>
+</body>
+</html>`
+ got := APIError([]byte(cf), "403 Forbidden")
+ if got != "403 Forbidden: a web page, not an API reply: “Attention Required! | Cloudflare” — "+BlockedHint {
+ t.Fatal(got)
+ }
+ if !EdgeBlocked([]byte(cf)) || !EdgeBlocked([]byte("error code: 1020")) {
+ t.Fatal("Cloudflare's block page isn't a firewall's")
+ }
+ if got := APIError([]byte("error code: 1020"), "403 Forbidden"); got != "403 Forbidden — "+BlockedHint {
+ t.Fatal(got)
+ }
+
+ page := "<!doctype html>\n<html><head><title>\n 403 <b>Forbidden</b> &amp; denied by nginx\n</title></head><body><h1>403</h1></body></html>"
+ if got := APIError([]byte(page), "403 Forbidden"); got != "403 Forbidden: a web page, not an API reply: “403 Forbidden & denied by nginx”" {
+ t.Fatal(got)
+ }
+ long := "<html><title>" + strings.Repeat("页", 200) + "</title></html>"
+ if got := APIError([]byte(long), "403 Forbidden"); !strings.HasSuffix(got, strings.Repeat("页", 120)+"…”") {
+ t.Fatal(got)
+ }
+ if EdgeBlocked([]byte(page)) {
+ t.Fatal("nginx's 403 page isn't a firewall's block")
+ }
+
+ for _, b := range []string{"", " \r\n "} {
+ for _, status := range []string{"403 Forbidden", "Forbidden", "502"} {
+ if got := APIError([]byte(b), status); got != status+": "+NoDetails {
+ t.Fatalf("%q, %s: %s", b, status, got)
+ }
+ }
+ }
+ // an empty stream event, or a reader's own words, is no HTTP refusal
+ for _, fallback := range []string{"upstream stream failed", "unexpected reply", ""} {
+ if got := APIError(nil, fallback); got != fallback {
+ t.Fatalf("%q: %s", fallback, got)
+ }
+ }
+
+ if got := APIError([]byte(`{"error":{"message":"Your IP is not allowed","type":"forbidden"}}`), "403 Forbidden"); got != "Your IP is not allowed" {
+ t.Fatal(got)
+ }
+ // the plan-quota reader says the same of a body with no message
+ if got := vendorSaid([]byte(cf), "403 Forbidden"); got != "403 Forbidden: a web page, not an API reply: “Attention Required! | Cloudflare”" {
+ t.Fatal(got)
+ }
+ if got := vendorSaid(nil, "403 Forbidden"); got != "403 Forbidden: "+NoDetails {
+ t.Fatal(got)
+ }
+}
+
// A backend that only streams is tested with a streamed request: WorkBuddy
// refuses any other with 400 (#124).
func TestTinyStreamsStreamOnly(t *testing.T) {
diff --git a/internal/provider/planquota.go b/internal/provider/planquota.go
index f2ae827c5..ea512a32f 100644
--- a/internal/provider/planquota.go
+++ b/internal/provider/planquota.go
@@ -581,6 +581,9 @@ func vendorSaid(b []byte, status string) string {
Message string `json:"message"`
}
if json.Unmarshal(b, &e) != nil {
+ if said, ok := Unsaid(b); ok {
+ return status + ": " + said
+ }
return status
}
msg := e.Message
diff --git a/internal/provider/test.go b/internal/provider/test.go
index e4e704b84..e401a18ad 100644
--- a/internal/provider/test.go
+++ b/internal/provider/test.go
@@ -7,6 +7,7 @@ import (
"context"
"encoding/json"
"fmt"
+ "html"
"io"
"net/http"
"net/url"
@@ -637,8 +638,13 @@ func streamAnswer(r io.Reader) (msg string, failed bool) {
// at fault and magpie changes nothing about it: the address is.
const BlockedHint = "the provider's network firewall blocked requests from this IP; wait a while, or switch to another network or proxy"
-// edgeBlocked matches such a block page, whoever's firewall served it.
-var edgeBlocked = regexp.MustCompile(`(?i)request has been blocked|errors\.aliyun\.com`)
+// edgeBlocked matches such a block page, whoever's firewall served it:
+// Alibaba Cloud's, and Cloudflare's — its WAF's "Sorry, you have been
+// blocked" (titled "Attention Required! | Cloudflare"), its "Access denied
+// | … used Cloudflare to restrict access" page, and the plain-text
+// "error code: 1020" (and the other access-denied codes, 1005 to 1012) it
+// answers a client that takes no HTML.
+var edgeBlocked = regexp.MustCompile(`(?i)request has been blocked|errors\.aliyun\.com|you have been blocked|used cloudflare to restrict access|error code: 10(0[5-9]|1[0-2]|20)\b`)
// EdgeBlocked says whether an error body is a firewall's block page rather
// than the vendor's API answering, or one already put in plain words.
@@ -652,6 +658,9 @@ func EdgeBlocked(b []byte) bool {
// link it gave.
func APIError(b []byte, fallback string) string {
if edgeBlocked.Match(b) {
+ if said, ok := WebPage(b); ok && fallback != "" {
+ return fallback + ": " + said + " — " + BlockedHint
+ }
if fallback == "" {
return BlockedHint
}
@@ -817,10 +826,26 @@ func apiError(b []byte, fallback string) string {
return m
}
}
+ // a web page in place of the API's answer is said for what it is, and
+ // an HTTP error with an empty body as refused with no details (a bare
+ // "403 Forbidden" told the user nothing of a relay's firewall,
+ // maralica on Discord); an empty stream event is no such refusal
+ if said, ok := WebPage(b); ok {
+ if fallback == "" {
+ return said
+ }
+ return fallback + ": " + said
+ }
+ if len(bytes.TrimSpace(b)) == 0 {
+ if statusLine(fallback) {
+ return fallback + ": " + NoDetails
+ }
+ return fallback
+ }
// a body in no shape known is shown as it is, cut short, so what the
- // vendor said isn't lost; an HTML page says nothing worth showing
+ // vendor said isn't lost; other markup says nothing worth showing
s := strings.Join(strings.Fields(string(b)), " ")
- if s == "" || strings.HasPrefix(s, "<") {
+ if strings.HasPrefix(s, "<") {
return fallback
}
if r := []rune(s); len(r) > 300 {
@@ -828,3 +853,71 @@ func apiError(b []byte, fallback string) string {
}
return fallback + ": " + s
}
+
+// NoDetails is what an error with an empty body is said as: the upstream
+// refused and gave no reason, which an API seldom does and a firewall or
+// proxy in front of it often does.
+const NoDetails = "refused with no details (an empty reply, often from a firewall or proxy in front of the API rather than the API itself)"
+
+// pageStart is how a web page begins, or a fragment of one that a proxy
+// serves as its error.
+var pageStart = regexp.MustCompile(`(?i)^<(!doctype\s+html|html[\s>]|head[\s>]|body[\s>]|title[\s>]|meta[\s>])`)
+
+// pageTitle is a web page's title.
+var pageTitle = regexp.MustCompile(`(?is)<title[^>]*>(.*?)</title>`)
+
+// PageTitle is a web page's title as plain words, cut to 120 runes: no
+// markup, entities read, whitespace folded. "" when there is none.
+func PageTitle(b []byte) string {
+ m := pageTitle.FindSubmatch(b)
+ if m == nil {
+ return ""
+ }
+ t := strings.Join(strings.Fields(html.UnescapeString(tagRe.ReplaceAllString(string(m[1]), " "))), " ")
+ if r := []rune(t); len(r) > 120 {
+ t = string(r[:120]) + "…"
+ }
+ return t
+}
+
+// tagRe is a markup tag, taken out of a title that holds one.
+var tagRe = regexp.MustCompile(`<[^>]*>`)
+
+// statusLine says a fallback is an HTTP error's status ("403 Forbidden",
+// or its text alone, "Forbidden"), what an error body was answered with,
+// not a stream event's or another reader's words.
+func statusLine(s string) bool {
+ if len(s) >= 3 && s[0] >= '1' && s[0] <= '5' && s[1] >= '0' && s[1] <= '9' && s[2] >= '0' && s[2] <= '9' && (len(s) == 3 || s[3] == ' ') {
+ return true
+ }
+ for c := 400; c < 600; c++ {
+ if t := http.StatusText(c); t != "" && t == s {
+ return true
+ }
+ }
+ return false
+}
+
+// Unsaid describes an HTTP error's body that holds no message of its
+// own: an empty one (NoDetails), or a web page (WebPage). ok is false for
+// any other body, whose message is the vendor's to give.
+func Unsaid(b []byte) (string, bool) {
+ if len(bytes.TrimSpace(b)) == 0 {
+ return NoDetails, true
+ }
+ return WebPage(b)
+}
+
+// WebPage describes a web page served in place of the API's answer — a
+// firewall's, a CDN's, a relay's own — by its title. ok is false for a
+// body that isn't one.
+func WebPage(b []byte) (string, bool) {
+ t := bytes.TrimLeft(bytes.TrimSpace(b), "\ufeff")
+ if !pageStart.Match(t) {
+ return "", false
+ }
+ if title := PageTitle(t); title != "" {
+ return "a web page, not an API reply: “" + title + "”", true
+ }
+ return "a web page, not an API reply", true
+}