Make the multipart viewer tolerant of preambles, batch responses and cut-off bodies (#821)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Gregory SchierandClaude Opus 5.5 authored and GitHub committed 2026-11-06 18:19:19 -07:00
1 parent c629d4c66c
commit f63586f9f0
5 files changed
+992 -43

No files matched your search

@@ -0,0 +1,122 @@
import type { ReactNode } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, test, vi } from "vite-plus/test";
import {
GMAIL_BATCH_BODY,
GMAIL_BATCH_BOUNDARY,
GMAIL_BATCH_CONTENT_TYPE,
} from "../../lib/multipart.fixtures";
import { MAX_MULTIPART_DEPTH, MultipartViewer } from "./MultipartViewer";
// Stand-ins that show which viewer a part went to, without the editor or the tab machinery
vi.mock("./TextViewer", () => ({
TextViewer: ({ text, language }: { text: string; language?: string }) => (
<pre data-viewer="text" data-language={language}>
{text}
</pre>
),
}));
vi.mock("./HexdumpViewer", () => ({
HexdumpViewer: ({ data }: { data: Uint8Array }) => (
<pre data-viewer="hexdump">{data.length} bytes</pre>
),
}));
vi.mock("../tabs/Tabs", () => ({
Tabs: ({ tabs, children }: { tabs: { label: string }[]; children: ReactNode }) => (
<div>
{tabs.map((t) => (
<button key={t.label} type="button">
{t.label}
</button>
))}
{children}
</div>
),
TabContent: ({ children }: { children: ReactNode }) => <section>{children}</section>,
}));
vi.mock("../HeadersTable", () => ({
HeadersTable: ({ headers }: { headers: { name: string; value: string }[] }) => (
<dl>
{headers.map((h) => (
<dt key={h.name + h.value}>
{h.name}: {h.value}
</dt>
))}
</dl>
),
}));
const bytes = (s: string) => new TextEncoder().encode(s);
describe("MultipartViewer", () => {
test("renders Gmail's batch response as a nested HTTP response", () => {
const markup = renderToStaticMarkup(
<MultipartViewer
data={bytes(GMAIL_BATCH_BODY)}
boundary={GMAIL_BATCH_CONTENT_TYPE.split("boundary=")[1]!}
/>,
);
expect(markup).not.toContain("Failed to parse");
expect(markup).toContain(">response-gtlr_43499</button>");
expect(markup).toContain("200");
expect(markup).toContain("OK");
expect(markup).toContain("Vary: X-Origin");
expect(markup).toContain('data-viewer="text" data-language="json"');
expect(markup).toContain("&quot;messagesTotal&quot;: 23545");
expect(markup).not.toContain(GMAIL_BATCH_BOUNDARY);
});
test("renders a multipart part recursively", () => {
const body = [
"--outer",
'Content-Type: multipart/alternative; boundary="inner"',
"",
"--inner",
"Content-Type: text/plain",
"",
"deep",
"--inner--",
"--outer--",
].join("\r\n");
const markup = renderToStaticMarkup(<MultipartViewer data={bytes(body)} boundary="outer" />);
expect(markup).toContain('data-viewer="text"');
expect(markup).toContain(">deep</pre>");
expect(markup).not.toContain("--inner");
});
test("shows multipart nested past the limit raw, with a note", () => {
// Six levels, each a multipart part holding the next, the innermost a text part
let body = ["--l6", "Content-Type: text/plain", "", "innermost", "--l6--"].join("\r\n");
for (let level = 5; level >= 1; level--) {
body = [
`--l${level}`,
`Content-Type: multipart/mixed; boundary=l${level + 1}`,
"",
body,
`--l${level}--`,
].join("\r\n");
}
const markup = renderToStaticMarkup(<MultipartViewer data={bytes(body)} boundary="l1" />);
expect(markup).toContain(`nested more than ${MAX_MULTIPART_DEPTH} levels deep`);
// Level five is shown as its raw text, so its own delimiters are still in it
expect(markup).toContain('data-viewer="text" data-language="text"');
expect(markup).toContain("--l5");
expect(markup).not.toContain(">innermost</pre>");
});
test("shows a binary part as a hexdump", () => {
const head = bytes("--b\r\nContent-Type: application/octet-stream\r\n\r\n");
const tail = bytes("\r\n--b--");
const data = new Uint8Array([...head, 0x00, 0x01, 0xff, 0xfe, ...tail]);
const markup = renderToStaticMarkup(<MultipartViewer data={data} boundary="b" />);
expect(markup).toContain('data-viewer="hexdump">4 bytes');
});
test("notes a body cut off before its closing delimiter", () => {
const markup = renderToStaticMarkup(
<MultipartViewer data={bytes("preamble\r\n--b\r\n\r\npartial")} boundary="b" />,
);
expect(markup).toContain("ends without its closing boundary");
expect(markup).toContain(">partial</pre>");
});
});
@@ -1,12 +1,24 @@
import { type MultipartPart, parseMultipart } from "@mjackson/multipart-parser";
import type { ReactNode } from "react";
import { useMemo } from "react";
import { languageFromContentType } from "../../lib/contentType";
import {
boundaryFromContentType,
headerValue,
type HttpMessage,
isProbablyBinary,
type MultipartParseResult,
type MultipartPart,
parseHttpMessage,
parseMultipart,
} from "../../lib/multipart";
import { Banner } from "../Banner";
import { HeadersTable } from "../HeadersTable";
import { Icon } from "../Icon";
import { HttpStatusTagRaw } from "../tags/HttpStatusTag";
import { TabContent, type TabContentProps, Tabs } from "../tabs/Tabs";
import { AudioViewer } from "./AudioViewer";
import { CsvViewer } from "./CsvViewer";
import { HexdumpViewer } from "./HexdumpViewer";
import { ImageViewer } from "./ImageViewer";
import { SvgViewer } from "./SvgViewer";
import { TextViewer, type TextViewerProps } from "./TextViewer";
@@ -15,6 +27,7 @@ import { WebPageViewer } from "./WebPageViewer";
export interface MultipartViewerProps {
data: Uint8Array;
/** The boundary, bare or as cut from a `Content-Type` header (quotes and later parameters are dropped) */
boundary: string;
idPrefix?: string;
/** Renders a PDF part, which shows as text without it */
@@ -24,35 +37,47 @@ export interface MultipartViewerProps {
ErrorBoundary?: TabContentProps["ErrorBoundary"];
}
export function MultipartViewer({
/** How many multipart levels show as tabs; a part nested deeper shows raw, with a note */
export const MAX_MULTIPART_DEPTH = 4;
type ViewerOptions = Pick<
MultipartViewerProps,
"renderPdf" | "textViewerProps" | "ErrorBoundary"
> & {
/** The multipart levels shown so far, this one included */
depth: number;
};
export function MultipartViewer(props: MultipartViewerProps) {
return <MultipartParts {...props} depth={1} />;
}
function MultipartParts({
data,
boundary,
idPrefix = "multipart",
renderPdf,
textViewerProps,
ErrorBoundary,
}: MultipartViewerProps) {
const parseResult = useMemo(() => {
...options
}: MultipartViewerProps & { depth: number }) {
// Parsed once per bytes and boundary, however often this renders
const parseResult = useMemo<
{ result: MultipartParseResult; error: null } | { result: null; error: string }
>(() => {
try {
const maxFileSize = 1024 * 1024 * 10; // 10MB
const parsed = parseMultipart(data, { boundary, maxFileSize });
const parts = Array.from(parsed);
return { parts, error: null };
return { result: parseMultipart(data, boundary), error: null };
} catch (err) {
return { parts: [], error: err instanceof Error ? err.message : String(err) };
return { result: null, error: err instanceof Error ? err.message : String(err) };
}
}, [data, boundary]);
const { parts, error } = parseResult;
if (error) {
if (parseResult.error != null) {
return (
<Banner color="danger" className="m-3">
Failed to parse multipart data: {error}
Failed to parse multipart data: {parseResult.error}
</Banner>
);
}
const { parts, complete } = parseResult.result;
if (parts.length === 0) {
return (
<Banner color="info" className="m-3">
@@ -61,20 +86,21 @@ export function MultipartViewer({
);
}
return (
const tabs = (
<Tabs
addBorders
label="Multipart"
layout="horizontal"
tabListClassName="border-r border-r-border -ml-3"
tabs={parts.map((part, i) => ({
label: part.name ?? "",
label: partLabel(part, i),
value: tabValue(part, i),
rightSlot:
part.filename && part.headers.contentType.mediaType?.startsWith("image/") ? (
part.filename && part.mediaType?.startsWith("image/") ? (
<div className="h-5 w-5 overflow-auto flex items-center justify-end">
<ImageViewer
data={part.arrayBuffer}
data={toArrayBuffer(part.body)}
mimeType={part.mediaType}
className="ml-auto w-auto rounded-sm overflow-hidden"
/>
</div>
@@ -86,59 +112,182 @@ export function MultipartViewer({
{parts.map((part, i) => (
<TabContent
// oxlint-disable-next-line react/no-array-index-key -- Nothing else to key on
key={idPrefix + part.name + i}
key={idPrefix + (part.name ?? "") + i}
value={tabValue(part, i)}
className="pl-3 pt-0!"
ErrorBoundary={ErrorBoundary}
ErrorBoundary={options.ErrorBoundary}
>
<Part part={part} renderPdf={renderPdf} textViewerProps={textViewerProps} />
<Part part={part} idPrefix={`${idPrefix}.${i}`} {...options} />
</TabContent>
))}
</Tabs>
);
if (complete) return tabs;
return (
<div className="h-full grid grid-rows-[auto_minmax(0,1fr)]">
<Banner color="warning" className="mb-2 mr-3 text-sm">
The body ends without its closing boundary; showing the parts that arrived.
</Banner>
{tabs}
</div>
);
}
function Part({
part,
idPrefix,
...options
}: { part: MultipartPart; idPrefix: string } & ViewerOptions) {
if (part.mediaType === "application/http") {
const message = parseHttpMessage(part.body);
if (message != null) {
return <HttpMessagePart message={message} idPrefix={idPrefix} {...options} />;
}
}
return (
<PartBody body={part.body} contentType={part.contentType} idPrefix={idPrefix} {...options} />
);
}
/** An `application/http` part, as in Google and OData batches: an HTTP message of its own */
function HttpMessagePart({
message,
idPrefix,
...options
}: { message: HttpMessage; idPrefix: string } & ViewerOptions) {
return (
<div className="h-full grid grid-rows-[auto_auto_minmax(0,1fr)] gap-y-2">
<div className="font-mono text-sm pt-1 truncate">
{message.status != null ? (
<HttpStatusTagRaw
status={message.status}
statusReason={message.statusReason}
showReason
/>
) : (
<span>
<span className="text-info">{message.method}</span> {message.url}
</span>
)}
</div>
<HeadersTable title="Headers" headers={message.headers} labelColor="primary" defaultOpen />
<PartBody
body={message.body}
contentType={headerValue(message.headers, "content-type")}
idPrefix={idPrefix}
{...options}
/>
</div>
);
}
/** A body by its own content type: the viewer the response pane would pick for it */
function PartBody({
body,
contentType,
idPrefix,
...options
}: {
body: Uint8Array;
contentType: string | null;
idPrefix: string;
} & ViewerOptions) {
const mediaType = contentType?.split(";")[0]?.trim().toLowerCase() || null;
if (body.length === 0) {
return <div className="text-text-subtlest text-sm italic pt-1">Empty</div>;
}
const nestedBoundary = mediaType?.startsWith("multipart/")
? boundaryFromContentType(contentType)
: null;
if (nestedBoundary != null) {
if (options.depth < MAX_MULTIPART_DEPTH) {
// The nested body is a view into the outer one's bytes, parsed only when shown
return (
<MultipartParts
{...options}
data={body}
boundary={nestedBoundary}
idPrefix={idPrefix}
depth={options.depth + 1}
/>
);
}
return (
<div className="h-full grid grid-rows-[auto_minmax(0,1fr)]">
<Banner color="info" className="mb-2 mr-3 text-sm">
Multipart nested more than {MAX_MULTIPART_DEPTH} levels deep; shown raw.
</Banner>
<LeafBody body={body} contentType={null} mediaType={null} raw {...options} />
</div>
);
}
return <LeafBody body={body} contentType={contentType} mediaType={mediaType} {...options} />;
}
/** A body that isn't multipart, decoded only here, where a viewer needs its text */
function LeafBody({
body,
contentType,
mediaType,
raw,
renderPdf,
textViewerProps,
}: { part: MultipartPart } & Pick<MultipartViewerProps, "renderPdf" | "textViewerProps">) {
const mimeType = part.headers.contentType.mediaType ?? null;
const contentTypeHeader = part.headers.get("content-type");
}: {
body: Uint8Array;
contentType: string | null;
mediaType: string | null;
/** Text or a hexdump, whatever the content type says */
raw?: boolean;
} & ViewerOptions) {
const { content, detectedLanguage, binary } = useMemo(() => {
const binary = isBinaryPart(mediaType, body);
const content = new TextDecoder().decode(body);
const detectedLanguage = raw ? "text" : languageFromContentType(contentType, content);
return { content, detectedLanguage, binary };
}, [body, contentType, mediaType, raw]);
const { uint8Array, content, detectedLanguage } = useMemo(() => {
const uint8Array = new Uint8Array(part.arrayBuffer);
const content = new TextDecoder().decode(part.arrayBuffer);
const detectedLanguage = languageFromContentType(contentTypeHeader, content);
return { uint8Array, content, detectedLanguage };
}, [part, contentTypeHeader]);
if (raw) {
return binary ? (
<HexdumpViewer data={body} renderEditor={textViewerProps?.renderEditor} />
) : (
<TextViewer {...textViewerProps} text={content} language="text" stateKey={null} />
);
}
if (mimeType?.match(/^image\/svg/i)) {
if (mediaType?.match(/^image\/svg/i)) {
return <SvgViewer text={content} className="pb-2" />;
}
if (mimeType?.match(/^image/i)) {
return <ImageViewer data={part.arrayBuffer} className="pb-2" />;
if (mediaType?.match(/^image/i)) {
return <ImageViewer data={toArrayBuffer(body)} mimeType={mediaType} className="pb-2" />;
}
if (mimeType?.match(/^audio/i)) {
return <AudioViewer data={uint8Array} />;
if (mediaType?.match(/^audio/i)) {
return <AudioViewer data={body} mimeType={mediaType} />;
}
if (mimeType?.match(/^video/i)) {
return <VideoViewer data={uint8Array} />;
if (mediaType?.match(/^video/i)) {
return <VideoViewer data={body} />;
}
if (mimeType?.match(/csv|tab-separated/i)) {
if (mediaType?.match(/csv|tab-separated/i)) {
return <CsvViewer text={content} className="bg-primary h-10 w-10" />;
}
if (mimeType?.match(/^text\/html/i) || detectedLanguage === "html") {
if (mediaType?.match(/^text\/html/i) || (!binary && detectedLanguage === "html")) {
return <WebPageViewer html={content} />;
}
if (mimeType?.match(/pdf/i) && renderPdf != null) {
return renderPdf(uint8Array);
if (mediaType?.match(/pdf/i) && renderPdf != null) {
return renderPdf(body);
}
if (binary) {
return <HexdumpViewer data={body} renderEditor={textViewerProps?.renderEditor} />;
}
return (
@@ -146,6 +295,25 @@ function Part({
);
}
/** Text types are shown as text even with odd bytes; anything else is binary if its bytes say so */
function isBinaryPart(mediaType: string | null, body: Uint8Array): boolean {
if (
mediaType != null &&
/^text\/|json|xml|javascript|yaml|csv|x-www-form-urlencoded/.test(mediaType)
) {
return false;
}
return isProbablyBinary(body);
}
function partLabel(part: MultipartPart, i: number): string {
return part.name || part.filename || part.contentId || `Part ${i + 1}`;
}
function tabValue(part: MultipartPart, i: number) {
return `${part.name ?? ""}::${i}`;
}
function toArrayBuffer(bytes: Uint8Array): ArrayBuffer {
return bytes.slice().buffer;
}