Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method SetAcceptEncoding for customized Accept-Encoding header (#683) #746

Merged
merged 1 commit into from Jun 5, 2022

Conversation

leviliangtw
Copy link
Contributor

  1. New class for managing header Accept-Encoding refers to proxies structure.cpp
  2. New method to set header Accept-Encoding
  3. New unit test to verify header Accept-Encoding

@COM8
Copy link
Member

COM8 commented May 19, 2022

 1: [----------] 5 tests from BasicAuthenticationTests
  1: [ RUN      ] BasicAuthenticationTests.BasicAuthenticationSuccessTest
  1: /home/runner/work/cpr/cpr/test/abstractServer.cpp:108:20: runtime error: left shift of 492624220 by 6 places cannot be represented in type 'int'
  1: HEADERS: Authorization: Basic dXNlcjpwYXNzd29yZA==
  1: User-Agent: curl/7.81.0
  1: Accept-Encoding: deflate, gzip
  1: Content-Type: text/html
  1: [       OK ] BasicAuthenticationTests.BasicAuthenticationSuccessTest (1 ms)
  1: [ RUN      ] BasicAuthenticationTests.BasicBearerSuccessTest
  1: HEADERS: Authorization: ***
  1: User-Agent: curl/7.81.0
  1: Accept-Encoding: deflate, gzip
  1: Content-Type: text/html

Copy link
Member

@COM8 COM8 left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this.


AcceptEncoding::AcceptEncoding(const std::initializer_list<AcceptEncodingMethods>& methods) : methods_{methods} {}

bool AcceptEncoding::isEmpty() const {
Copy link
Member

Choose a reason for hiding this comment

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

All public methods have to start with an upper case letter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would like to change its name to has, just like the implementation in proxies.cpp:

bool Proxies::has(const std::string& protocol) const {
    return hosts_.count(protocol) > 0;
}

Copy link
Member

Choose a reason for hiding this comment

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

This is also possible.


class AcceptEncoding {
public:
std::map<AcceptEncodingMethods, std::string> MethodsString {
Copy link
Member

Choose a reason for hiding this comment

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

Although I like the way you are solving this here, curl allows even more encodings: https://curl.se/libcurl/c/CURLOPT_ACCEPT_ENCODING.html

Since this is something that changes based on what is build into curl, in my eyes we need at least an additional option allowing the user to specify their own encoding via a string name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I will add an overload function for that to make it more flexible.

Copy link
Member

@COM8 COM8 left a comment

Choose a reason for hiding this comment

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

This one goes into the right direction. Only a few things to sort out. Also please rebase your changes on master: https://gist.github.com/ravibhure/a7e0918ff4937c9ea1c456698dcd58aa

@@ -105,7 +105,7 @@ std::string AbstractServer::Base64Decode(const std::string& in) {
break;
}
// NOLINTNEXTLINE (cppcoreguidelines-avoid-magic-numbers)
val = (val << 6) + T[c];
val = ((uint)val << (uint)6) + T[c];
Copy link
Member

Choose a reason for hiding this comment

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

Please don't use C-style casts

Suggested change
val = ((uint)val << (uint)6) + T[c];
val = (static_cast<uint>(val) << static_cast<uint>(6)) + T[c];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it!


class AcceptEncoding {
public:
std::map<AcceptEncodingMethods, std::string> MethodsStringMap {
Copy link
Member

Choose a reason for hiding this comment

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

Can't this map be static and const?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would investigate it and find a best practice for that

AcceptEncoding(const std::initializer_list<AcceptEncodingMethods>& methods);
AcceptEncoding(const std::initializer_list<std::string>& methods);

bool has() const;
Copy link
Member

Choose a reason for hiding this comment

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

I thought about it again and I prefer empty() here as name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!

cpr/session.cpp Outdated Show resolved Hide resolved
cpr/accept_encoding.cpp Outdated Show resolved Hide resolved
cpr/accept_encoding.cpp Outdated Show resolved Hide resolved
@leviliangtw leviliangtw force-pushed the bugfix/session branch 4 times, most recently from 49432fc to 24cbfb7 Compare May 28, 2022 01:08
@leviliangtw leviliangtw requested a review from COM8 May 28, 2022 01:09
@COM8
Copy link
Member

COM8 commented May 29, 2022

msvc on Windows reports:

  D:\a\cpr\cpr\cpr\accept_encoding.cpp(8,24): error C2039: 'back_inserter': is not a member of 'std' [D:\a\cpr\cpr\build\cpr\cpr.vcxproj]
  C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.32.31326\include\vector(24): message : see declaration of 'std' [D:\a\cpr\cpr\build\cpr\cpr.vcxproj]
  D:\a\cpr\cpr\cpr\accept_encoding.cpp(8,37): error C3861: 'back_inserter': identifier not found [D:\a\cpr\cpr\build\cpr\cpr.vcxproj]

@leviliangtw leviliangtw force-pushed the bugfix/session branch 2 times, most recently from 04b1cb8 to 1383954 Compare June 1, 2022 06:00
@leviliangtw leviliangtw requested a review from COM8 June 1, 2022 06:05
Copy link
Member

@COM8 COM8 left a comment

Choose a reason for hiding this comment

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

Looks good now, thanks!
Please create a PR for a bit of documentation here: https://github.com/libcpr/docs

@COM8
Copy link
Member

COM8 commented Jun 3, 2022

D:\a\cpr\cpr\test\abstractServer.cpp(108,28): error C2061: syntax error: identifier 'uint' [D:\a\cpr\cpr\build\test\test_server.vcxproj]

@leviliangtw
Copy link
Contributor Author

D:\a\cpr\cpr\test\abstractServer.cpp(108,28): error C2061: syntax error: identifier 'uint' [D:\a\cpr\cpr\build\test\test_server.vcxproj]

I think I should not modify this file.

@COM8 COM8 merged commit dd715f6 into libcpr:master Jun 5, 2022
@leviliangtw leviliangtw deleted the bugfix/session branch June 5, 2022 13:08
leviliangtw pushed a commit to leviliangtw/cpr that referenced this pull request Jun 5, 2022
Add method SetAcceptEncoding for customized Accept-Encoding header (libcpr#683)
@COM8 COM8 linked an issue Jun 25, 2022 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to disable post request compression
2 participants