Replacing Base64 - Is http/https communication 8 bit clean?

∥☆過路亽.° 提交于 2020-01-03 02:57:21

问题


Here is an overview of what 8 bit clean means.

In the context of web applications, why are images saved as Base64? There is a 33% overhead associated with being 8 bit clean.

If the transmission method is safe there is no need for this.

But basically, my images are saved in Base64 on the server, and transferred to the client, which as we all know can read Base64.

Here is the client side version of Base 64 in an SO Post.

How can you encode a string to Base64 in JavaScript?

Is http/https 8 bit clean?

Reference

http://www.princeton.edu/~achaney/tmve/wiki100k/docs/8-bit_clean.html

http://en.wikipedia.org/wiki/8-bit_clean


回答1:


In the context of web applications, why are images saved as Base64? There is a 33% overhead associated with being 8 bit clean.

Base64 is used to allow 8-bit binary data to be presented as printable text within the ASCII definition. This is only 7-bits, not 8 as the last 128 characters would be depending on set encoding (Latin1, UTF8 etc.) which means that the encoded data could be mangled if a different encoding type was set at client/receiver end compared to source.

As there aren't enough printable characters within ASCII to represent all 8-bit values (which has absolute values and aren't dependent on encoding itself) you need to "water out the bits" and base-64 keeps high enough numbers to enable the bytes to be represented as printable chars.

This is the 33% overhead you see as the byte values representing characters outside the printable range must be shifted to a value that becomes printable within the ASCII table; Base-64 allows this (you could also use quoted printable which was common in the past, ie. with Usenet, email etc.).

I'm thinking about writing another encoding type to remove the overhead.

Good luck :-)




回答2:


You are asking two different things.

  1. Q: Is http 8 bit clean?

    A: yes HTTP is "bit 8 clean".

  2. Q: In the context of web applications, why are images saved as Base64?

    A: images are not usually saved in Base64. In fact, they are almost never. They are usually saved or transmitted or streamed in compressed binary format (PNG or JPG or similar)

    Base64 is used to embed images inside the HTML.

So, you got an image logo.png. You include it statically in your page as <img src='logo.png'>. The image is transmitted thru HTTP in binary, no encoding in neither browser nor server side. This is the most common case.

Alternatively, you might decide to embed the contents of the image inside the HTML. It has some advantages: The browser will not need to do a second trip to the server to fetch the image, because the browser has already received it in the same HTTP GET response of the HTML file. But some disadvantages, because HTML files are text and certain character values may have special meaning for HTML (not for HTTP), you cannot just embed the binary values inside the HTML text. You have to encode them to avoid such collisions. The most usual encoding method is base64, which avoids all the collisions with only a 33% of overhead.




回答3:


RFC 2616s abstract states:

A feature of HTTP is the typing and negotiation of data representation, allowing systems to be built independently of the data being transferred.

HTTP always starts with a text-only header and in this header the content-type is specified. As long as sender and receiver agree on this contents type anything is possible.

HTTP relies on a reliable (recognize the wordplay) transport layer such as TCP. HTTPS only adds security to the transport layer (or between the transport layer and HTTP, not sure about this).

So yep, http(s) is 8 bit clean.

In addition to PAs answer and your question "But why use an encoding method that adds 33% overhead, when you don't need it?": because that's part of a different concept!

HTTP transfers data of any kind, and the http-content may be an html file with an embedded picture. But after receiving that html file a browser or some other renderer has to interpret the html content. And that follows different standards, which require arbitrary data to be encoded. html is not 8-bit clean, in fact it is not even 7-bit clean as there are many restrictions on the characters used and their order of appearance.




回答4:


Related to the query

  • Is HTTP 8-bit clean ?

HTTP protocol is not in entirety a 8-bit clean protocol.

HTTP Entity Body is 8-bit clean since there is a provision to suggest the content-type, allowing content-negotiation between the interacting entities as pointed by everyone in this thread.

However the request line , the headers and the status line are not 8-bit clean.

In order to send any binary information as part of

  • the request line, as part of query parameters / path segments

  • header

one must use one of the binary-to-text encoding to preserve the binary values.

For instance when sending a signature as part of query parameters or headers , which is the case of signed URL technique employed by CDN , the signature a binary information has to be encoded to preserve the binary value of it.



来源:https://stackoverflow.com/questions/18830083/replacing-base64-is-http-https-communication-8-bit-clean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!