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

feat(http): implement http client based on libcurl #1583

Merged
merged 29 commits into from
Sep 28, 2023

Conversation

empiredan
Copy link
Contributor

@empiredan empiredan commented Aug 16, 2023

#1582

Http client is implemented based on libcurl. Both GET and POST methods
are supported.

@neverchanje
Copy link
Contributor

I recommend this library for more modern http https://github.com/yhirose/cpp-httplib

@empiredan empiredan marked this pull request as ready for review August 19, 2023 08:36
@empiredan
Copy link
Contributor Author

I recommend this library for more modern http https://github.com/yhirose/cpp-httplib

Thanks for your recommendation ! It seems that this lib provides a set of very elegant and easy-to-use APIs for http client. In addition, it also supports building http server. Later on I'll look deep into this lib.

src/http/test/http_client_test.cpp Outdated Show resolved Hide resolved
thirdparty/CMakeLists.txt Outdated Show resolved Hide resolved
src/http/http_client.h Outdated Show resolved Hide resolved
src/http/http_client.h Outdated Show resolved Hide resolved
src/http/http_client.h Outdated Show resolved Hide resolved
src/http/http_client.h Show resolved Hide resolved
@acelyc111
Copy link
Member

I recommend this library for more modern http https://github.com/yhirose/cpp-httplib

+1, it's not necessary to invent the wheel again :)

@empiredan empiredan marked this pull request as draft September 21, 2023 03:49
@empiredan empiredan changed the title feat: implement http client based on libcurl feat(http): implement http client based on libcurl Sep 24, 2023
@empiredan
Copy link
Contributor Author

Originally, referencing libcurl into pegasus shell directly would lead to hidden symbol XXX in YYY is referenced by DSO ...:
wecom-temp-65992-cd2278e076b3ba45d457ef44ef0e147c

企业微信截图_09859393-8f5b-48e6-bd91-d3c830eb70ab

wecom-temp-75125-1ea1e87155e22755a232bec4998a343c

This error could be eliminated by introducing http client. However, compilation failed for macOS:

Undefined symbols for architecture x86_64:
  "_nghttp2_is_fatal", referenced from:
      _on_frame_recv in libcurl.a(libcurl_la-http2.o)
      _Curl_http2_switched in libcurl.a(libcurl_la-http2.o)
      _http2_recv in libcurl.a(libcurl_la-http2.o)
      _http2_send in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_pack_settings_payload", referenced from:
      _Curl_http2_request_upgrade in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_priority_spec_init", referenced from:
      _h2_pri_spec in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_callbacks_del", referenced from:
      _Curl_http2_init in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_callbacks_new", referenced from:
      _Curl_http2_init in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_callbacks_set_before_frame_send_callback", referenced from:
      _Curl_http2_init in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_callbacks_set_on_begin_headers_callback", referenced from:
      _Curl_http2_init in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_callbacks_set_on_data_chunk_recv_callback", referenced from:
      _Curl_http2_init in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_callbacks_set_on_frame_not_send_callback", referenced from:
      _Curl_http2_init in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_callbacks_set_on_frame_recv_callback", referenced from:
      _Curl_http2_init in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_callbacks_set_on_frame_send_callback", referenced from:
      _Curl_http2_init in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_callbacks_set_on_header_callback", referenced from:
      _Curl_http2_init in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_callbacks_set_on_invalid_frame_recv_callback", referenced from:
      _Curl_http2_init in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_callbacks_set_on_stream_close_callback", referenced from:
      _Curl_http2_init in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_callbacks_set_send_callback", referenced from:
      _Curl_http2_init in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_client_new", referenced from:
      _Curl_http2_init in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_del", referenced from:
      _http2_disconnect in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_get_remote_settings", referenced from:
      _on_frame_recv in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_get_stream_user_data", referenced from:
      _on_frame_recv in libcurl.a(libcurl_la-http2.o)
      _on_invalid_frame_recv in libcurl.a(libcurl_la-http2.o)
      _on_data_chunk_recv in libcurl.a(libcurl_la-http2.o)
      _before_frame_send in libcurl.a(libcurl_la-http2.o)
      _on_frame_send in libcurl.a(libcurl_la-http2.o)
      _on_frame_not_send in libcurl.a(libcurl_la-http2.o)
      _on_stream_close in libcurl.a(libcurl_la-http2.o)
      ...
  "_nghttp2_session_mem_recv", referenced from:
      _Curl_http2_switched in libcurl.a(libcurl_la-http2.o)
      _http2_recv in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_resume_data", referenced from:
      _http2_send in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_send", referenced from:
      _http2_send in libcurl.a(libcurl_la-http2.o)
      _h2_session_send in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_set_stream_user_data", referenced from:
      _Curl_http_done in libcurl.a(libcurl_la-http.o)
      _on_frame_recv in libcurl.a(libcurl_la-http2.o)
      _on_stream_close in libcurl.a(libcurl_la-http2.o)
      _Curl_http2_switched in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_upgrade", referenced from:
      _Curl_http2_switched in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_want_read", referenced from:
      _http2_getsock in libcurl.a(libcurl_la-http2.o)
      _http2_perform_getsock in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_session_want_write", referenced from:
      _http2_getsock in libcurl.a(libcurl_la-http2.o)
      _http2_perform_getsock in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_strerror", referenced from:
      _Curl_http2_switched in libcurl.a(libcurl_la-http2.o)
      _http2_recv in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_submit_priority", referenced from:
      _h2_session_send in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_submit_request", referenced from:
      _http2_send in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_submit_rst_stream", referenced from:
      _on_frame_recv in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_submit_settings", referenced from:
      _Curl_http2_switched in libcurl.a(libcurl_la-http2.o)
  "_nghttp2_version", referenced from:
      _Curl_http2_ver in libcurl.a(libcurl_la-http2.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [src/http/test/dsn_http_test] Error 1
make[1]: *** [src/http/test/CMakeFiles/dsn_http_test.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

Then after libcurl was bumped to 8.3.0 from 7.47.0, compilation failed for ubuntu:

/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /data/sa_cluster/src/empiredan-pegasus-new/thirdparty/output/lib/libcurl.a(libcurl_la-content_encoding.o): undefined reference to symbol 'ZSTD_isError'
//lib/libzstd.so.1.3.7: error adding symbols: DSO missing from command line

compilation also failed for macOS:

Undefined symbols for architecture x86_64:
  "_BrotliDecoderCreateInstance", referenced from:
      _brotli_init_writer in libcurl.a(libcurl_la-content_encoding.o)
  "_BrotliDecoderDecompressStream", referenced from:
      _brotli_unencode_write in libcurl.a(libcurl_la-content_encoding.o)
  "_BrotliDecoderDestroyInstance", referenced from:
      _brotli_unencode_write in libcurl.a(libcurl_la-content_encoding.o)
      _brotli_close_writer in libcurl.a(libcurl_la-content_encoding.o)
  "_BrotliDecoderGetErrorCode", referenced from:
      _brotli_unencode_write in libcurl.a(libcurl_la-content_encoding.o)
  "_idn2_check_version", referenced from:
      _Curl_idn_decode in libcurl.a(libcurl_la-idn.o)
      _Curl_idnconvert_hostname in libcurl.a(libcurl_la-idn.o)
  "_idn2_free", referenced from:
      _Curl_idn_decode in libcurl.a(libcurl_la-idn.o)
      _Curl_idn_encode in libcurl.a(libcurl_la-idn.o)
      _Curl_free_idnconverted_hostname in libcurl.a(libcurl_la-idn.o)
      _Curl_idnconvert_hostname in libcurl.a(libcurl_la-idn.o)
  "_idn2_lookup_ul", referenced from:
      _Curl_idn_decode in libcurl.a(libcurl_la-idn.o)
      _Curl_idnconvert_hostname in libcurl.a(libcurl_la-idn.o)
  "_idn2_to_unicode_8z8z", referenced from:
      _Curl_idn_encode in libcurl.a(libcurl_la-idn.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [src/http/test/dsn_http_test] Error 1
make[1]: *** [src/http/test/CMakeFiles/dsn_http_test.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

Therefore, libcurl is built with --without-zstd, and following options especially for macOS:

--without-nghttp2
--without-libidn2
--without-brotli

@empiredan empiredan marked this pull request as ready for review September 26, 2023 07:54
const dsn::error_s &err)
{
const std::string actual_description(err.description());
std::cout << actual_description << std::endl;
Copy link
Member

Choose a reason for hiding this comment

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

Is the output for debuging? If you want to check what the actual value is when test failed, you can use the macro like this:

ASSERT_*(x,x) << actual_description;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason why this description is printed is that its postfix, i.e. msg field, as is described above, differ in various systems. I think the different message could be shown for each various system.

thirdparty/CMakeLists.txt Outdated Show resolved Hide resolved
src/utils/error_code.h Show resolved Hide resolved
src/http/http_client.cpp Outdated Show resolved Hide resolved
src/http/http_client.cpp Outdated Show resolved Hide resolved
src/http/test/http_client_test.cpp Outdated Show resolved Hide resolved
src/http/test/http_client_test.cpp Show resolved Hide resolved
src/http/test/http_client_test.cpp Show resolved Hide resolved
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.

4 participants