can GET some pages
This commit is contained in:
@@ -5,8 +5,20 @@
|
||||
*/
|
||||
|
||||
#include "IXHttpClient.h"
|
||||
#include "IXUrlParser.h"
|
||||
#include "IXWebSocketHttpHeaders.h"
|
||||
|
||||
#if defined(__APPLE__) or defined(__linux__)
|
||||
# ifdef __APPLE__
|
||||
# include <ixwebsocket/IXSocketAppleSSL.h>
|
||||
# else
|
||||
# include <ixwebsocket/IXSocketOpenSSL.h>
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
namespace ix
|
||||
{
|
||||
@@ -20,18 +32,52 @@ namespace ix
|
||||
|
||||
}
|
||||
|
||||
HttpResponse HttpClient::get(const std::string& url)
|
||||
HttpResponse HttpClient::get(const std::string& url,
|
||||
bool verbose)
|
||||
{
|
||||
int code = 0;
|
||||
WebSocketHttpHeaders headers;
|
||||
std::string payload;
|
||||
|
||||
std::string protocol, host, path, query;
|
||||
int port;
|
||||
|
||||
if (!parseUrl(url, protocol, host, path, query, port))
|
||||
{
|
||||
code = 0; // 0 ?
|
||||
std::string errorMsg("Cannot parse url");
|
||||
return std::make_tuple(code, headers, payload, errorMsg);
|
||||
}
|
||||
|
||||
if (protocol == "http")
|
||||
{
|
||||
_socket = std::make_shared<Socket>();
|
||||
}
|
||||
else if (protocol == "https")
|
||||
{
|
||||
# ifdef __APPLE__
|
||||
_socket = std::make_shared<SocketAppleSSL>();
|
||||
# else
|
||||
_socket = std::make_shared<SocketOpenSSL>();
|
||||
# endif
|
||||
}
|
||||
else
|
||||
{
|
||||
code = 0; // 0 ?
|
||||
std::string errorMsg("Bad protocol");
|
||||
return std::make_tuple(code, headers, payload, errorMsg);
|
||||
}
|
||||
|
||||
// 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;
|
||||
std::stringstream ss;
|
||||
ss << "GET " << path << " HTTP/1.1\r\n";
|
||||
ss << "Host: " << host << "\r\n";
|
||||
ss << "User-Agent: ixwebsocket/1.0.0" << "\r\n";
|
||||
ss << "Accept: */*" << "\r\n";
|
||||
ss << "\r\n";
|
||||
std::string request(ss.str());
|
||||
|
||||
int timeoutSecs = 3;
|
||||
|
||||
std::string errMsg;
|
||||
@@ -39,40 +85,138 @@ namespace ix
|
||||
auto isCancellationRequested =
|
||||
makeCancellationRequestWithTimeout(timeoutSecs, requestInitCancellation);
|
||||
|
||||
bool success = _socket.connect(host, port, errMsg, isCancellationRequested);
|
||||
bool success = _socket->connect(host, port, errMsg, isCancellationRequested);
|
||||
if (!success)
|
||||
{
|
||||
int code = 0; // 0 ?
|
||||
return std::make_tuple(code, headers, payload);
|
||||
code = 0; // 0 ?
|
||||
std::string errorMsg("Cannot connect to url");
|
||||
return std::make_tuple(code, headers, payload, errorMsg);
|
||||
}
|
||||
|
||||
std::cout << "Sending request: " << request
|
||||
<< "to " << host << ":" << port
|
||||
<< std::endl;
|
||||
if (!_socket.writeBytes(request, isCancellationRequested))
|
||||
if (verbose)
|
||||
{
|
||||
int code = 0; // 0 ?
|
||||
return std::make_tuple(code, headers, payload);
|
||||
std::cout << "Sending request: " << request
|
||||
<< "to " << host << ":" << port
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
auto lineResult = _socket.readLine(isCancellationRequested);
|
||||
if (!_socket->writeBytes(request, isCancellationRequested))
|
||||
{
|
||||
code = 0; // 0 ?
|
||||
std::string errorMsg("Cannot send request");
|
||||
return std::make_tuple(code, headers, payload, errorMsg);
|
||||
}
|
||||
|
||||
auto lineResult = _socket->readLine(isCancellationRequested);
|
||||
auto lineValid = lineResult.first;
|
||||
auto line = lineResult.second;
|
||||
|
||||
std::cout << "first line: " << line << std::endl;
|
||||
if (!lineValid)
|
||||
{
|
||||
code = 0; // 0 ?
|
||||
std::string errorMsg("Cannot retrieve status line");
|
||||
return std::make_tuple(code, headers, payload, errorMsg);
|
||||
}
|
||||
|
||||
int status = -1;
|
||||
sscanf(line.c_str(), "HTTP/1.1 %d", &status) == 1;
|
||||
if (verbose)
|
||||
{
|
||||
std::cout << "first line: " << line << std::endl;
|
||||
}
|
||||
|
||||
return std::make_tuple(code, headers, payload);
|
||||
}
|
||||
code = -1;
|
||||
if (sscanf(line.c_str(), "HTTP/1.1 %d", &code) != 1)
|
||||
{
|
||||
code = 0; // 0 ?
|
||||
std::string errorMsg("Cannot parse response code from status line");
|
||||
return std::make_tuple(code, headers, payload, errorMsg);
|
||||
}
|
||||
|
||||
HttpResponse HttpClient::post(const std::string& url)
|
||||
{
|
||||
int code = 0;
|
||||
WebSocketHttpHeaders headers;
|
||||
std::string payload;
|
||||
auto result = parseHttpHeaders(_socket, isCancellationRequested);
|
||||
auto headersValid = result.first;
|
||||
headers = result.second;
|
||||
|
||||
return std::make_tuple(code, headers, payload);
|
||||
if (!headersValid)
|
||||
{
|
||||
code = 0; // 0 ?
|
||||
std::string errorMsg("Cannot parse http headers");
|
||||
return std::make_tuple(code, headers, payload, errorMsg);
|
||||
}
|
||||
|
||||
// Parse response:
|
||||
// http://bryce-thomas.blogspot.com/2012/01/technical-parsing-http-to-extract.html
|
||||
if (headers.find("content-length") == headers.end())
|
||||
{
|
||||
code = 0; // 0 ?
|
||||
std::string errorMsg("No content length header");
|
||||
return std::make_tuple(code, headers, payload, errorMsg);
|
||||
}
|
||||
|
||||
ssize_t contentLength = -1;
|
||||
ss.str("");
|
||||
ss << headers["content-length"];
|
||||
ss >> contentLength;
|
||||
|
||||
payload.reserve(contentLength);
|
||||
|
||||
// very inefficient way to read bytes, but it works...
|
||||
for (int i = 0; i < contentLength; ++i)
|
||||
{
|
||||
char c;
|
||||
if (!_socket->readByte(&c, isCancellationRequested))
|
||||
{
|
||||
ss.str("");
|
||||
ss << "Cannot read byte";
|
||||
return std::make_tuple(-1, headers, payload, ss.str());
|
||||
}
|
||||
|
||||
payload += c;
|
||||
}
|
||||
|
||||
return std::make_tuple(code, headers, payload, "");
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
std::vector<uint8_t> rxbuf;
|
||||
|
||||
while (true)
|
||||
{
|
||||
int N = (int) _rxbuf.size();
|
||||
|
||||
_rxbuf.resize(N + 1500);
|
||||
ssize_t ret = _socket->recv((char*)&_rxbuf[0] + N, 1500);
|
||||
|
||||
if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK ||
|
||||
_socket->getErrno() == EAGAIN)) {
|
||||
_rxbuf.resize(N);
|
||||
break;
|
||||
}
|
||||
else if (ret <= 0)
|
||||
{
|
||||
_rxbuf.resize(N);
|
||||
|
||||
_socket->close();
|
||||
setReadyState(CLOSED);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
_rxbuf.resize(N + ret);
|
||||
}
|
||||
}
|
||||
ssize_t ret = _socket->recv((char*)&rxbuf[0], contentLength);
|
||||
payload = std::string(rxbuf.begin(), rxbuf.end());
|
||||
std::cerr << "socket->recv: " << ret << std::endl;
|
||||
|
||||
if (ret != contentLength)
|
||||
{
|
||||
ss.str("");
|
||||
ss << "Cannot retrieve all bytes"
|
||||
<< " want: " << contentLength
|
||||
<< ", got: " << ret;
|
||||
std::cerr << "adscasdcadcasdc" << std::endl;
|
||||
std::cerr << ss.str() << std::endl;
|
||||
|
||||
return std::make_tuple(-1, headers, payload, ss.str());
|
||||
}
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user