diff --git a/README.md b/README.md index ca88e0f9..f6b2d8b0 100644 --- a/README.md +++ b/README.md @@ -129,33 +129,33 @@ Here is what the HTTP client API looks like. Note that HTTP client support is ve // Preparation // HttpClient httpClient; -HttpRequestArgs args; +HttpRequestArgsPtr args = httpClient.createRequest(); // Custom headers can be set WebSocketHttpHeaders headers; headers["Foo"] = "bar"; -args.extraHeaders = headers; +args->extraHeaders = headers; // Timeout options -args.connectTimeout = connectTimeout; -args.transferTimeout = transferTimeout; +args->connectTimeout = connectTimeout; +args->transferTimeout = transferTimeout; // Redirect options -args.followRedirects = followRedirects; -args.maxRedirects = maxRedirects; +args->followRedirects = followRedirects; +args->maxRedirects = maxRedirects; // Misc -args.compress = compress; // Enable gzip compression -args.verbose = verbose; -args.logger = [](const std::string& msg) +args->compress = compress; // Enable gzip compression +args->verbose = verbose; +args->logger = [](const std::string& msg) { std::cout << msg; }; // -// Request +// Synchronous Request // -HttpResponse out; +HttpResponsePtr out; std::string url = "https://www.google.com"; // HEAD request @@ -175,13 +175,30 @@ out = httpClient.post(url, std::string("foo=bar"), args); // // Result // -auto statusCode = std::get<0>(out); -auto errorCode = std::get<1>(out); -auto responseHeaders = std::get<2>(out); -auto payload = std::get<3>(out); -auto errorMsg = std::get<4>(out); -auto uploadSize = std::get<5>(out); -auto downloadSize = std::get<6>(out); +auto errorCode = response->errorCode; // Can be HttpErrorCode::Ok, HttpErrorCode::UrlMalformed, etc... +auto errorCode = response->errorCode; // 200, 404, etc... +auto responseHeaders = response->headers; // All the headers in a special case-insensitive unordered_map of (string, string) +auto payload = response->payload; // All the bytes from the response as an std::string +auto errorMsg = response->errorMsg; // Descriptive error message in case of failure +auto uploadSize = response->uploadSize; // Byte count of uploaded data +auto downloadSize = response->downloadSize; // Byte count of downloaded data + +// +// Asynchronous Request +// +bool async = true; +HttpClient httpClient(async); +auto args = httpClient.createRequest(url, HttpClient::kGet); + +// Push the request to a queue, +bool ok = httpClient.performRequest(args, [](const HttpResponsePtr& response) + { + // This callback execute in a background thread. Make sure you uses appropriate protection such as mutex + auto statusCode = response->statusCode; // acess results + } +); + +// ok will be false if your httpClient is not async ``` ## Build diff --git a/docker/Dockerfile.ubuntu_bionic b/docker/Dockerfile.ubuntu_bionic new file mode 100644 index 00000000..88892cff --- /dev/null +++ b/docker/Dockerfile.ubuntu_bionic @@ -0,0 +1,23 @@ +# Build time +FROM ubuntu:bionic as build + +ENV DEBIAN_FRONTEND noninteractive +RUN apt-get update +RUN apt-get -y install wget +RUN mkdir -p /tmp/cmake +WORKDIR /tmp/cmake +RUN wget https://github.com/Kitware/CMake/releases/download/v3.14.0/cmake-3.14.0-Linux-x86_64.tar.gz +RUN tar zxf cmake-3.14.0-Linux-x86_64.tar.gz + +RUN apt-get -y install g++ +RUN apt-get -y install libssl-dev +RUN apt-get -y install libz-dev +RUN apt-get -y install make +RUN apt-get -y install python + +COPY . . + +ARG CMAKE_BIN_PATH=/tmp/cmake/cmake-3.14.0-Linux-x86_64/bin +ENV PATH="${CMAKE_BIN_PATH}:${PATH}" + +RUN ["make", "ws"] diff --git a/ixwebsocket/IXSocketMbedTLS.cpp b/ixwebsocket/IXSocketMbedTLS.cpp index a72e3549..af8f744d 100644 --- a/ixwebsocket/IXSocketMbedTLS.cpp +++ b/ixwebsocket/IXSocketMbedTLS.cpp @@ -82,7 +82,7 @@ namespace ix mbedtls_ssl_set_bio(&_ssl, &_sockfd, mbedtls_net_send, mbedtls_net_recv, NULL); int res; - do + do { res = mbedtls_ssl_handshake(&_ssl); } diff --git a/ixwebsocket/IXWebSocketMessageQueue.cpp b/ixwebsocket/IXWebSocketMessageQueue.cpp index 64e9ddb6..50aebe66 100644 --- a/ixwebsocket/IXWebSocketMessageQueue.cpp +++ b/ixwebsocket/IXWebSocketMessageQueue.cpp @@ -81,7 +81,7 @@ namespace ix { _onMessageUserCallback = std::move(callback); } - + WebSocketMessageQueue::MessagePtr WebSocketMessageQueue::popMessage() { MessagePtr message; diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 4a722eef..51544212 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -1041,7 +1041,7 @@ namespace ix _requestInitCancellation = true; if (_readyState == ReadyState::CLOSING || _readyState == ReadyState::CLOSED) return; - + { std::lock_guard lock(_closeDataMutex); _closeCode = code; diff --git a/test/IXWebSocketCloseTest.cpp b/test/IXWebSocketCloseTest.cpp index 0e3fe018..f915ce4d 100644 --- a/test/IXWebSocketCloseTest.cpp +++ b/test/IXWebSocketCloseTest.cpp @@ -210,7 +210,7 @@ namespace << closeInfo.reason << ")"; log(ss.str()); - + std::lock_guard lck(mutexWrite); receivedCloseCode = closeInfo.code;