【问题标题】:Determining binary data vs. text using the HTTP Content-Type header使用 HTTP Content-Type 标头确定二进制数据与文本
【发布时间】:2019-10-31 15:39:34
【问题描述】:

我正在编写代码以在 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;
}

【问题讨论】:

    标签: typescript http mime-types content-type


    【解决方案1】:

    您可以在从 URL 获取的缓冲区上使用 textorbinary。

    例如在 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);
            }
    
    .
    .
    };
    
    

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

    【讨论】:

      猜你喜欢
      • 2019-02-28
      • 2019-05-23
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 2022-06-25
      相关资源
      最近更新 更多