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

Remove libuv / simpler curl http #22

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ find_package(Threads REQUIRED)
find_package(ZLIB REQUIRED)
find_package(CURL REQUIRED)
find_package(Protobuf REQUIRED)
find_package(LibUV REQUIRED)

if (NOT DEFINED Protobuf_LIBRARIES)
set(Protobuf_LIBRARIES ${PROTOBUF_LIBRARIES})
Expand All @@ -19,10 +18,10 @@ endif()
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS proto/gsf.proto proto/play_common.proto proto/play_document.proto proto/play_respone.proto proto/play_settings.proto proto/play_toc.proto proto/play_download.proto proto/play_filter_rules.proto proto/play_ownership.proto proto/play_containers.proto proto/play_link.proto proto/play_device_config.proto proto/play_search.proto proto/play_browse.proto proto/play_details.proto)

set(LIB_UTIL_SOURCE_FILES lib/playapi/util/http.cpp include/playapi/util/http.h lib/playapi/util/config.cpp include/playapi/util/config.h lib/playapi/util/rand.cpp include/playapi/util/rand.h lib/playapi/util/base64.cpp include/playapi/util/base64.h)
set(LIB_SOURCE_FILES lib/playapi/login.cpp include/playapi/login.h lib/playapi/device_info.cpp include/playapi/device_info.h lib/playapi/checkin.cpp include/playapi/checkin.h include/playapi/api.h lib/playapi/api.cpp lib/playapi/experiments.cpp include/playapi/experiments.h include/playapi/login_cache.h include/playapi/file_login_cache.h lib/playapi/file_login_cache.cpp include/playapi/task.h include/playapi/util/http_request_pool.h lib/playapi/util/http_request_pool.cpp include/playapi/http_task.h)
set(LIB_SOURCE_FILES lib/playapi/login.cpp include/playapi/login.h lib/playapi/device_info.cpp include/playapi/device_info.h lib/playapi/checkin.cpp include/playapi/checkin.h include/playapi/api.h lib/playapi/api.cpp lib/playapi/experiments.cpp include/playapi/experiments.h include/playapi/login_cache.h include/playapi/file_login_cache.h lib/playapi/file_login_cache.cpp include/playapi/task.h include/playapi/http_task.h)
add_library(gplayapi STATIC ${LIB_SOURCE_FILES} ${LIB_UTIL_SOURCE_FILES} ${PROTO_SRCS})
target_link_libraries(gplayapi ${CURL_LIBRARIES} ${ZLIB_LIBRARIES} ${Protobuf_LIBRARIES} ${LIBUV_LIBRARIES} Threads::Threads)
target_include_directories(gplayapi PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CURL_INCLUDE_DIR} ${ZLIB_INCLUDE_DIRS} ${Protobuf_INCLUDE_DIRS} ${LIBUV_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(gplayapi ${CURL_LIBRARIES} ${ZLIB_LIBRARIES} ${Protobuf_LIBRARIES} Threads::Threads)
target_include_directories(gplayapi PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CURL_INCLUDE_DIR} ${ZLIB_INCLUDE_DIRS} ${Protobuf_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})

add_executable(gplaydl src/gplaydl.cpp src/common.cpp src/config.cpp src/config.h)
target_link_libraries(gplaydl gplayapi)
Expand Down
19 changes: 2 additions & 17 deletions include/playapi/util/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <functional>
#include <sstream>
#include <curl/curl.h>
#include "http_request_pool.h"

namespace playapi {

Expand All @@ -30,14 +29,13 @@ struct http_response {

private:

CURL* curl;
CURLcode curlCode;
long statusCode;
std::string body;

public:

http_response(CURL* curl, CURLcode curlCode, long statusCode, std::string body);
http_response(CURLcode curlCode, long statusCode, std::string body);
http_response(http_response&& r);
~http_response();

Expand Down Expand Up @@ -80,18 +78,6 @@ class http_request {
static int curl_xferinfo(void* ptr, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);

CURL* build(std::stringstream& output, bool copy_body = false);

struct pool_entry : public http_request_pool::base_entry {
std::stringstream output;
std::function<void (http_response)> success;
std::function<void (std::exception_ptr)> error;
progress_callback callback_progress;
output_callback callback_output;

~pool_entry() override = default;
void done(CURL* curl, CURLcode code) override;
};

public:

http_request() {}
Expand Down Expand Up @@ -124,8 +110,7 @@ class http_request {

http_response perform();

CURL* perform(std::function<void (http_response)> success, std::function<void (std::exception_ptr)> error,
http_request_pool& pool = http_request_pool::default_instance());
void perform(std::function<void (http_response)> success, std::function<void (std::exception_ptr)> error);

};

Expand Down
77 changes: 0 additions & 77 deletions include/playapi/util/http_request_pool.h

This file was deleted.

85 changes: 45 additions & 40 deletions lib/playapi/util/http.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include <playapi/util/http.h>

#include <cassert>
#include <stdexcept>
#include <zlib.h>
#include <thread>
#include <memory>

using namespace playapi;

Expand Down Expand Up @@ -35,32 +38,27 @@ std::string url_encoded_entity::encode() const {
return std::move(ret);
}

http_response::http_response(CURL* curl, CURLcode curlCode, long statusCode, std::string body) :
curl(curl), curlCode(curlCode), statusCode(statusCode), body(std::move(body)) {
http_response::http_response(CURLcode curlCode, long statusCode, std::string body) :
curlCode(curlCode), statusCode(statusCode), body(std::move(body)) {
//
}

http_response::http_response(http_response&& r) : curl(r.curl), curlCode(r.curlCode), statusCode(r.statusCode),
http_response::http_response(http_response&& r) : curlCode(r.curlCode), statusCode(r.statusCode),
body(r.body) {
r.curl = nullptr;
r.curlCode = CURLE_FAILED_INIT;
r.statusCode = 0;
r.body = std::string();
}

http_response& http_response::operator=(http_response&& r) {
curl = r.curl;
curlCode = r.curlCode;
body = r.body;
r.curl = nullptr;
r.curlCode = CURLE_FAILED_INIT;
r.body = std::string();
return *this;
}

http_response::~http_response() {
curl_easy_cleanup(curl);
curl = nullptr;
}

void http_request::set_body(const url_encoded_entity& ent) {
Expand Down Expand Up @@ -173,36 +171,43 @@ http_response http_request::perform() {
#ifndef NDEBUG
printf("http response body: %s\n", output.str().c_str());
#endif
return http_response(curl, ret, status, output.str());
}

CURL* http_request::perform(std::function<void(http_response)> success, std::function<void(std::exception_ptr)> error,
http_request_pool& pool) {
pool_entry* ei = new pool_entry;
ei->success = success;
ei->error = error;
CURL* curl = build(ei->output, true);
if (callback_output) {
ei->callback_output = callback_output;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ei->callback_output);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_func);
}
if (callback_progress) {
ei->callback_progress = callback_progress;
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, curl_xferinfo);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &ei->callback_progress);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
}
curl_easy_setopt(curl, CURLOPT_PRIVATE, ei);
pool.add(curl);
return curl;
curl_easy_cleanup(curl);
return http_response(ret, status, output.str());
}

void http_request::perform(std::function<void(http_response)> success, std::function<void(std::exception_ptr)> error) {
auto req = std::make_shared<http_request>(*this);
std::thread([req, success, error]() {
std::stringstream output;
CURL* curl = req->build(output);
char errbuf[CURL_ERROR_SIZE];
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
if (req->callback_output) {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &req->callback_output);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_func);
}
if (req->callback_progress) {
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, curl_xferinfo);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &req->callback_progress);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
}
CURLcode curlerr = curl_easy_perform(curl);
if (curlerr == CURLE_OK) {
long status;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
#ifndef NDEBUG
printf("http response body: %s\n", output.str().c_str());
#endif
success(http_response(curlerr, status, output.str()));
} else {
std::stringstream errormsg;
errormsg << "Failed to perform http request to " << req->url << " : CURLcode " << curlerr << " Details: " << errbuf;
try {
throw std::runtime_error(errormsg.str().data());
} catch (...) {
error(std::current_exception());
}
}
curl_easy_cleanup(curl);
}).detach();
}

void http_request::pool_entry::done(CURL* curl, CURLcode code) {
long status;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
#ifndef NDEBUG
printf("http response body: %s\n", output.str().c_str());
#endif
success(http_response(curl, code, status, output.str()));
}
Loading