add skeleton and broken http client code.
GET returns "Resource temporarily unavailable" errors...
This commit is contained in:
parent
49077f8f44
commit
2a17cad1bf
@ -28,6 +28,7 @@ set( IXWEBSOCKET_SOURCES
|
|||||||
ixwebsocket/IXWebSocketPerMessageDeflate.cpp
|
ixwebsocket/IXWebSocketPerMessageDeflate.cpp
|
||||||
ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp
|
ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp
|
||||||
ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp
|
ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp
|
||||||
|
ixwebsocket/IXHttpClient.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set( IXWEBSOCKET_HEADERS
|
set( IXWEBSOCKET_HEADERS
|
||||||
@ -49,6 +50,7 @@ set( IXWEBSOCKET_HEADERS
|
|||||||
ixwebsocket/IXWebSocketPerMessageDeflateOptions.h
|
ixwebsocket/IXWebSocketPerMessageDeflateOptions.h
|
||||||
ixwebsocket/IXWebSocketHttpHeaders.h
|
ixwebsocket/IXWebSocketHttpHeaders.h
|
||||||
ixwebsocket/libwshandshake.hpp
|
ixwebsocket/libwshandshake.hpp
|
||||||
|
ixwebsocket/IXHttpClient.h
|
||||||
)
|
)
|
||||||
|
|
||||||
# Platform specific code
|
# Platform specific code
|
||||||
|
1
examples/http_client/.gitignore
vendored
Normal file
1
examples/http_client/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
build
|
22
examples/http_client/CMakeLists.txt
Normal file
22
examples/http_client/CMakeLists.txt
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#
|
||||||
|
# Author: Benjamin Sergeant
|
||||||
|
# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
#
|
||||||
|
|
||||||
|
cmake_minimum_required (VERSION 3.4.1)
|
||||||
|
project (http_client)
|
||||||
|
|
||||||
|
set (CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
|
option(USE_TLS "Add TLS support" ON)
|
||||||
|
|
||||||
|
add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket)
|
||||||
|
|
||||||
|
add_executable(http_client http_client.cpp)
|
||||||
|
|
||||||
|
if (APPLE AND USE_TLS)
|
||||||
|
target_link_libraries(http_client "-framework foundation" "-framework security")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_link_libraries(http_client ixwebsocket)
|
||||||
|
install(TARGETS http_client DESTINATION bin)
|
32
examples/http_client/http_client.cpp
Normal file
32
examples/http_client/http_client.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* http_client.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <ixwebsocket/IXHttpClient.h>
|
||||||
|
|
||||||
|
using namespace ix;
|
||||||
|
|
||||||
|
void run(const std::string& url)
|
||||||
|
{
|
||||||
|
HttpClient httpClient;
|
||||||
|
httpClient.get(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
std::cerr << "Usage: ws_connect <url>" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
std::string url = argv[1];
|
||||||
|
|
||||||
|
Socket::init();
|
||||||
|
run(url);
|
||||||
|
return 0;
|
||||||
|
}
|
80
ixwebsocket/IXHttpClient.cpp
Normal file
80
ixwebsocket/IXHttpClient.cpp
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* IXHttpClient.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "IXHttpClient.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace ix
|
||||||
|
{
|
||||||
|
HttpClient::HttpClient()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpClient::~HttpClient()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpResponse HttpClient::get(const std::string& url)
|
||||||
|
{
|
||||||
|
int code = 0;
|
||||||
|
WebSocketHttpHeaders headers;
|
||||||
|
std::string payload;
|
||||||
|
|
||||||
|
// FIXME: missing url parsing
|
||||||
|
|
||||||
|
std::string host("www.cnn.com");
|
||||||
|
int port = 80;
|
||||||
|
std::string request("GET / HTTP/1.1\r\n\r\n");
|
||||||
|
int expectedStatus = 200;
|
||||||
|
int timeoutSecs = 3;
|
||||||
|
|
||||||
|
std::string errMsg;
|
||||||
|
static std::atomic<bool> requestInitCancellation(false);
|
||||||
|
auto isCancellationRequested =
|
||||||
|
makeCancellationRequestWithTimeout(timeoutSecs, requestInitCancellation);
|
||||||
|
|
||||||
|
bool success = _socket.connect(host, port, errMsg, isCancellationRequested);
|
||||||
|
if (!success)
|
||||||
|
{
|
||||||
|
int code = 0; // 0 ?
|
||||||
|
return std::make_tuple(code, headers, payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Sending request: " << request
|
||||||
|
<< "to " << host << ":" << port
|
||||||
|
<< std::endl;
|
||||||
|
if (!_socket.writeBytes(request, isCancellationRequested))
|
||||||
|
{
|
||||||
|
int code = 0; // 0 ?
|
||||||
|
return std::make_tuple(code, headers, payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto lineResult = _socket.readLine(isCancellationRequested);
|
||||||
|
auto lineValid = lineResult.first;
|
||||||
|
auto line = lineResult.second;
|
||||||
|
|
||||||
|
std::cout << "first line: " << line << std::endl;
|
||||||
|
|
||||||
|
std::cout << "read error: " << strerror(Socket::getErrno()) << std::endl;
|
||||||
|
|
||||||
|
int status = -1;
|
||||||
|
sscanf(line.c_str(), "HTTP/1.1 %d", &status) == 1;
|
||||||
|
|
||||||
|
return std::make_tuple(code, headers, payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpResponse HttpClient::post(const std::string& url)
|
||||||
|
{
|
||||||
|
int code = 0;
|
||||||
|
WebSocketHttpHeaders headers;
|
||||||
|
std::string payload;
|
||||||
|
|
||||||
|
return std::make_tuple(code, headers, payload);
|
||||||
|
}
|
||||||
|
}
|
34
ixwebsocket/IXHttpClient.h
Normal file
34
ixwebsocket/IXHttpClient.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* IXHttpClient.h
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <functional>
|
||||||
|
#include <mutex>
|
||||||
|
#include <atomic>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
|
#include "IXSocket.h"
|
||||||
|
#include "IXWebSocketHttpHeaders.h"
|
||||||
|
|
||||||
|
namespace ix
|
||||||
|
{
|
||||||
|
using HttpResponse = std::tuple<int, WebSocketHttpHeaders, std::string>;
|
||||||
|
|
||||||
|
class HttpClient {
|
||||||
|
public:
|
||||||
|
HttpClient();
|
||||||
|
~HttpClient();
|
||||||
|
|
||||||
|
// Static methods ?
|
||||||
|
HttpResponse get(const std::string& url);
|
||||||
|
HttpResponse post(const std::string& url);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Socket _socket;
|
||||||
|
};
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user