add skeleton and broken http client code.

GET returns "Resource temporarily unavailable" errors...
This commit is contained in:
Benjamin Sergeant
2019-02-14 10:14:57 -08:00
parent 49077f8f44
commit 2a17cad1bf
6 changed files with 171 additions and 0 deletions

View 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);
}
}

View 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;
};
}