使用HTTP Content-Type头确定二进制数据与文本的区别

3
我正在编写代码以在Node.js环境中通过HTTP/HTTPS读取任意Web资源,并希望在文本数据时将内容作为字符串返回,在二进制数据时将其作为缓冲区返回。
明显的是,任何以"text"开头的内容,例如"text/html",都是最好以字符串形式返回的文本数据 - 当然,需要使用适当的字符编码,这可能会被显式定义(例如"text/html; charset=utf-8"),或者可能不会。此外,任何对"charset"的显式定义都表示内容是文本而不是二进制,而与MIME类型无关。
就我所知,几乎所有其他内容都是二进制数据。我知道的所有音频和视频格式都是二进制的,几乎每种图像类型都是如此,除了"image/svg+xml"之外。似乎大多数"application/..."类型都是二进制的,但有一些值得注意的常见例外,例如"application/json"。
以下函数是否足以确定问题?如果不是,我可能忽略了哪些显著的例外情况?
function isBinary(contentType: string): boolean {
  let $: string[];

  if (/;\s*charset\s*=/i.test(contentType))
    return false;

  // Remove anything other than MIME type.
  contentType = contentType.replace(/;.*$/, '').trim();

  if (/^text\//i.test(contentType) || /\+xml$/i.test(contentType))
    return false;
  else if (($ = /^application\/(.+)/i.exec(contentType)))
    return !/^(javascript|ecmascript|json|ld\+json|rtf)$/i.test($[1]);
  else
    return true;
}
1个回答

0

你可以在从URL获取到的缓冲区上使用 istextorbinary 方法。

例如,在 lambda 中,您可以这样做:

const fetch = require('node-fetch');
const { isText, isBinary, getEncoding } = require('istextorbinary');
module.exports.handler = async (event, context, callback) => {
.
.
        const customUrl = 'www.abc.net.au';
        const url = `https://${customUrl}${event.path}`;

        // Request with GET/HEAD method cannot have body | undefined
        // Set fetch parameters in params
        var params = {};
        if (event.httpMethod === 'GET' || event.httpMethod === 'HEAD') {
            params = {
                method: event.httpMethod,
                headers: customRequestHeader
            };
        } else {
            params = {
                method: event.httpMethod,
                headers: customRequestHeader,
                body: JSON.stringify(parsedbody)
            };
        }

        console.debug('request params: ' + JSON.stringify(params));

        // Fetch URL with params
        const response = await fetch(url, params);
        var textResponse = await response.buffer();

        var isBase64EncodedValue = false;  
        if ( isBinary(null, textResponse) ) {
            console.log('textResponse is a binary blob - setting isBase64Encoded to true on returnResponse');
            isBase64EncodedValue = true;
            console.log('isBase64EncodedValue in returnResponse is: ' + isBase64EncodedValue);
            // We need to return binary base64 encoded data for binary files - otherwise APIGW will throw 500 error back to the user
            textResponse = textResponse.toString('base64');
            // If you need to convert it back you could do something like - textResponse.toString('binary')
            console.log('When isBase64EncodedValue is true, textResponse is: ' + textResponse);
        } else {
            console.log('textResponse is not a binary blob - setting isBase64Encoded to false on returnResponse');
            isBase64EncodedValue = false;
            console.log('isBase64EncodedValue in returnResponse is: ' + isBase64EncodedValue);
            textResponse = textResponse.toString('utf8');
            console.log('When isBase64EncodedValue is false, textResponse is: ' + textResponse);
        }

.
.
};

你最终使用了你的函数吗? 想分享一下你的最终代码吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接