{"record":{"id":"8ab9c69732d7a8b7","repo":"getzola/zola","slug":"could-not-build-html-response","errorCode":null,"errorMessage":"Could not build HTML response","messagePattern":"Could not build HTML response","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"src/cmd/serve.rs","lineNumber":361,"sourceCode":"\n    Response::from_parts(parts, Body::from(bytes))\n}\n\nfn in_memory_content(path: &RelativePathBuf, content: &str) -> Response {\n    let content_type = match path.extension() {\n        Some(ext) => match ext {\n            \"xml\" => \"text/xml\",\n            \"json\" => \"application/json\",\n            \"txt\" => \"text/plain\",\n            _ => \"text/html\",\n        },\n        None => \"text/html\",\n    };\n    Response::builder()\n        .header(header::CONTENT_TYPE, content_type)\n        .status(StatusCode::OK)\n        .body(Body::from(content.to_owned()))\n        .expect(\"Could not build HTML response\")\n}\n\nfn method_not_allowed() -> Response {\n    Response::builder()\n        .header(header::CONTENT_TYPE, \"text/plain\")\n        .status(StatusCode::METHOD_NOT_ALLOWED)\n        .body(Body::from(METHOD_NOT_ALLOWED_TEXT))\n        .expect(\"Could not build Method Not Allowed response\")\n}\n\nfn io_error(err: std::io::Error) -> Response {\n    match err.kind() {\n        std::io::ErrorKind::NotFound => not_found(),\n        std::io::ErrorKind::PermissionDenied => {\n            Response::builder().status(StatusCode::FORBIDDEN).body(Body::empty()).unwrap()\n        }\n        _ => panic!(\"{}\", err),\n    }","sourceCodeStart":343,"sourceCodeEnd":379,"githubUrl":"https://github.com/getzola/zola/blob/61d30828217957120309db657950224edf9707e2/src/cmd/serve.rs#L343-L379","documentation":"in_memory_content serves in-memory build output by building a Response with Response::builder().body(Body::from(content.to_owned())).expect(...). Construction fails only if the content-type HeaderValue is invalid, so the panic indicates an invalid dynamically chosen content_type string.","triggerScenarios":"Requesting a file during `zola serve` whose resolved content_type (derived from file extension/mime guess) is not a valid HTTP header value — e.g. a mime string containing control characters from a custom extension mapping.","commonSituations":"Serving files with unusual extensions after patching the MIME table; corruption in the mime-guessing dependency output.","solutions":["Sanitize the computed content_type before building the response (strip control chars)","Fall back to \"text/html\" or \"application/octet-stream\" when HeaderValue::from_str fails","Use HeaderValue::from_str(content_type).unwrap_or(HeaderValue::from_static(\"application/octet-stream\")) in the builder"],"exampleFix":"// before\nResponse::builder()\n    .header(header::CONTENT_TYPE, content_type)\n    .status(StatusCode::OK)\n    .body(Body::from(content.to_owned()))\n    .expect(\"Could not build HTML response\")\n// after\nlet content_type = HeaderValue::from_str(content_type)\n    .unwrap_or(HeaderValue::from_static(\"application/octet-stream\"));\nResponse::builder()\n    .header(header::CONTENT_TYPE, content_type)\n    .status(StatusCode::OK)\n    .body(Body::from(content.to_owned()))\n    .expect(\"Could not build HTML response\")","handlingStrategy":"validation","validationCode":"let ct = http::HeaderValue::from_str(content_type)\n    .map_err(|_| anyhow::anyhow!(\"invalid content type: {}\", content_type))?;","typeGuard":"fn is_valid_header_value(v: &str) -> bool {\n    http::HeaderValue::from_str(v).is_ok()\n}","tryCatchPattern":null,"preventionTips":["Fall back to application/octet-stream for unresolvable MIME types","Whitelist the content types the dev server may emit","Never derive header values from raw user-controlled file extensions without sanitizing"],"tags":["rust","axum","http","panic","dev-server"],"backgroundTag":"http-response-build-failure","analyzedSha":"61d30828217957120309db657950224edf9707e2","analyzedAt":"2026-09-03T14:39:09.727Z","contentChangedAt":"2026-09-03T14:39:09.727Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}