Skip to content

[DISCUSS] Add a SBuf-based Base64 encoder/decoder - #2452

Draft
kinkie wants to merge 22 commits into
squid-cache:masterfrom
kinkie:base64encoder
Draft

[DISCUSS] Add a SBuf-based Base64 encoder/decoder#2452
kinkie wants to merge 22 commits into
squid-cache:masterfrom
kinkie:base64encoder

Conversation

@kinkie

@kinkie kinkie commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Stemming from the converstaions in PR #2447 which focuses on
addressing a problem as narrowly as possible, develop a longer-term solution,
based around SBuf

@yadij yadij added the M-ignored-by-merge-bots https://github.com/measurement-factory/anubis/blob/master/README.md#pull-request-labels label Jun 27, 2026
@yadij

yadij commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

The only issue I see is that Squid base64 encoding is only used on buffers. So we would end up adding a stream just for the purpose of using the encoder.

@rousskov rousskov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had already flagged this API direction as problematic before this PR was posted. Will find time to detail that claim.

@rousskov rousskov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, the serious problems identified in this incomplete review are enough to warrant switching to a different approach to base64-encoding. There are other, mostly secondary problems in this PR that this review does not flag. Most will probably disappear on the adjusted path.

I can suggest starting with an API like this:

namespace AnyP {

/// A base64-encoded version of the given input.
SBuf Base64Encode(const SBuf &);

} // namespace AnyP

Comment thread src/base64/Base64Encoder.h Outdated
Comment thread src/base64/Base64Encoder.cc Outdated
Comment thread src/base64/Base64Encoder.h Outdated
@kinkie kinkie changed the title [DISCUSS] Prototype a Base64Encoder [DISCUSS] Add a SBuf-based Base64 encoder/decoder Jul 21, 2026
@kinkie

kinkie commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

@rousskov, @yadij , this might be in a state where it's discussable. It has the encoder, decoder, and some users. What do you think?

@kinkie kinkie added the S-waiting-for-reviewer ready for review: Set this when requesting a (re)review using GitHub PR Reviewers box label Jul 26, 2026

@rousskov rousskov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be in a state where it's discussable. It has the encoder, decoder, and some users. What do you think?

This is shaping nicely IMO, thank you! In addition to specific change requests, please finalize PR title/description and go through the changes to fix const, AAA, and odd formatting that misses spaces around = and between function call arguments.

Comment thread src/anyp/Base64.h
#include "sbuf/forward.h"

/// Thrown by Base64Decode() when the input is not valid base64.
class DecodeException : public TextException

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not introduce a custom exception type to report decoding errors, especially when that type is effectively unused outside of unit tests. Instead, return std::optional<SBuf>, especially since the only non-test caller immediately handles decoding errors (i.e. without propagating them a lot further up the stack to some distant high-level code).

This is the second time we are discussing a recent attempt to throw from a decoding function. The first one resulted in a AnyP::Uri::Decode()-related bug fixed in commit ad365ed. I am not claiming there is a stable pattern here, but we should be mindful of that first failure. Let's drop exceptions from this code, at least as a starting point.

Comment thread src/http.cc
SBuf toEncode(username);
toEncode.append(request->peer_login + 1);
SBuf encoded = Base64Encode(toEncode);
httpHeaderPutStrf(hdr_out, header, "Basic %.*s", (int)encoded.length(), encoded.rawContent());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please upgrade this code to use SQUIDSBUFPH and friends now that it is printing an SBuf. Same for other similar cases.

@rousskov rousskov removed the S-waiting-for-reviewer ready for review: Set this when requesting a (re)review using GitHub PR Reviewers box label Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

M-ignored-by-merge-bots https://github.com/measurement-factory/anubis/blob/master/README.md#pull-request-labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants