最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Replacing Base64 - Is httphttps communication 8 bit clean? - Stack Overflow

programmeradmin0浏览0评论

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

.html

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/wiki/8-bit_clean

Share Improve this question edited May 23, 2017 at 12:07 CommunityBot 11 silver badge asked Sep 16, 2013 at 14:08 employee-0employee-0 1,0411 gold badge9 silver badges19 bronze badges 1
  • Is HTTP 8-bit clean? A fairly authoritative answer from Julian Reschke – Hawkeye Parker Commented Nov 14, 2014 at 11:03
Add a ment  | 

4 Answers 4

Reset to default 8

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 pressed 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 mon 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.

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.

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 pared 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 bees printable within the ASCII table; Base-64 allows this (you could also use quoted printable which was mon in the past, ie. with Usenet, email etc.).

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

Good luck :-)

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.

发布评论

评论列表(0)

  1. 暂无评论