reformat everything with clang-format
This commit is contained in:
@ -10,14 +10,13 @@
|
||||
|
||||
namespace ix
|
||||
{
|
||||
CancellationRequest makeCancellationRequestWithTimeout(int secs,
|
||||
std::atomic<bool>& requestInitCancellation)
|
||||
CancellationRequest makeCancellationRequestWithTimeout(
|
||||
int secs, std::atomic<bool>& requestInitCancellation)
|
||||
{
|
||||
auto start = std::chrono::system_clock::now();
|
||||
auto timeout = std::chrono::seconds(secs);
|
||||
|
||||
auto isCancellationRequested = [&requestInitCancellation, start, timeout]() -> bool
|
||||
{
|
||||
auto isCancellationRequested = [&requestInitCancellation, start, timeout]() -> bool {
|
||||
// Was an explicit cancellation requested ?
|
||||
if (requestInitCancellation) return true;
|
||||
|
||||
@ -30,4 +29,4 @@ namespace ix
|
||||
|
||||
return isCancellationRequested;
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -10,7 +10,8 @@ namespace ix
|
||||
{
|
||||
std::atomic<uint64_t> ConnectionState::_globalId(0);
|
||||
|
||||
ConnectionState::ConnectionState() : _terminated(false)
|
||||
ConnectionState::ConnectionState()
|
||||
: _terminated(false)
|
||||
{
|
||||
computeId();
|
||||
}
|
||||
@ -39,5 +40,4 @@ namespace ix
|
||||
{
|
||||
_terminated = true;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ix
|
||||
|
@ -5,22 +5,22 @@
|
||||
*/
|
||||
|
||||
#include "IXDNSLookup.h"
|
||||
#include "IXNetSystem.h"
|
||||
|
||||
#include <string.h>
|
||||
#include "IXNetSystem.h"
|
||||
#include <chrono>
|
||||
#include <string.h>
|
||||
#include <thread>
|
||||
|
||||
namespace ix
|
||||
{
|
||||
const int64_t DNSLookup::kDefaultWait = 1; // ms
|
||||
|
||||
DNSLookup::DNSLookup(const std::string& hostname, int port, int64_t wait) :
|
||||
_hostname(hostname),
|
||||
_port(port),
|
||||
_wait(wait),
|
||||
_res(nullptr),
|
||||
_done(false)
|
||||
DNSLookup::DNSLookup(const std::string& hostname, int port, int64_t wait)
|
||||
: _hostname(hostname)
|
||||
, _port(port)
|
||||
, _wait(wait)
|
||||
, _res(nullptr)
|
||||
, _done(false)
|
||||
{
|
||||
;
|
||||
}
|
||||
@ -38,8 +38,7 @@ namespace ix
|
||||
std::string sport = std::to_string(port);
|
||||
|
||||
struct addrinfo* res;
|
||||
int getaddrinfo_result = getaddrinfo(hostname.c_str(), sport.c_str(),
|
||||
&hints, &res);
|
||||
int getaddrinfo_result = getaddrinfo(hostname.c_str(), sport.c_str(), &hints, &res);
|
||||
if (getaddrinfo_result)
|
||||
{
|
||||
errMsg = gai_strerror(getaddrinfo_result);
|
||||
@ -56,8 +55,8 @@ namespace ix
|
||||
: resolveUnCancellable(errMsg, isCancellationRequested);
|
||||
}
|
||||
|
||||
struct addrinfo* DNSLookup::resolveUnCancellable(std::string& errMsg,
|
||||
const CancellationRequest& isCancellationRequested)
|
||||
struct addrinfo* DNSLookup::resolveUnCancellable(
|
||||
std::string& errMsg, const CancellationRequest& isCancellationRequested)
|
||||
{
|
||||
errMsg = "no error";
|
||||
|
||||
@ -71,8 +70,8 @@ namespace ix
|
||||
return getAddrInfo(_hostname, _port, errMsg);
|
||||
}
|
||||
|
||||
struct addrinfo* DNSLookup::resolveCancellable(std::string& errMsg,
|
||||
const CancellationRequest& isCancellationRequested)
|
||||
struct addrinfo* DNSLookup::resolveCancellable(
|
||||
std::string& errMsg, const CancellationRequest& isCancellationRequested)
|
||||
{
|
||||
errMsg = "no error";
|
||||
|
||||
@ -126,7 +125,9 @@ namespace ix
|
||||
return getRes();
|
||||
}
|
||||
|
||||
void DNSLookup::run(std::weak_ptr<DNSLookup> self, std::string hostname, int port) // thread runner
|
||||
void DNSLookup::run(std::weak_ptr<DNSLookup> self,
|
||||
std::string hostname,
|
||||
int port) // thread runner
|
||||
{
|
||||
// We don't want to read or write into members variables of an object that could be
|
||||
// gone, so we use temporary variables (res) or we pass in by copy everything that
|
||||
@ -167,4 +168,4 @@ namespace ix
|
||||
std::lock_guard<std::mutex> lock(_resMutex);
|
||||
return _res;
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -10,9 +10,8 @@
|
||||
|
||||
namespace ix
|
||||
{
|
||||
uint32_t calculateRetryWaitMilliseconds(
|
||||
uint32_t retry_count,
|
||||
uint32_t maxWaitBetweenReconnectionRetries)
|
||||
uint32_t calculateRetryWaitMilliseconds(uint32_t retry_count,
|
||||
uint32_t maxWaitBetweenReconnectionRetries)
|
||||
{
|
||||
uint32_t wait_time = std::pow(2, retry_count) * 100;
|
||||
|
||||
@ -23,4 +22,4 @@ namespace ix
|
||||
|
||||
return wait_time;
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -10,7 +10,6 @@
|
||||
|
||||
namespace ix
|
||||
{
|
||||
uint32_t calculateRetryWaitMilliseconds(
|
||||
uint32_t retry_count,
|
||||
uint32_t maxWaitBetweenReconnectionRetries);
|
||||
uint32_t calculateRetryWaitMilliseconds(uint32_t retry_count,
|
||||
uint32_t maxWaitBetweenReconnectionRetries);
|
||||
} // namespace ix
|
||||
|
@ -5,9 +5,9 @@
|
||||
*/
|
||||
|
||||
#include "IXHttp.h"
|
||||
|
||||
#include "IXCancellationRequest.h"
|
||||
#include "IXSocket.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
@ -57,7 +57,8 @@ namespace ix
|
||||
return std::make_pair(httpVersion, statusCode);
|
||||
}
|
||||
|
||||
std::tuple<std::string, std::string, std::string> Http::parseRequestLine(const std::string& line)
|
||||
std::tuple<std::string, std::string, std::string> Http::parseRequestLine(
|
||||
const std::string& line)
|
||||
{
|
||||
// Request-Line = Method SP Request-URI SP HTTP-Version CRLF
|
||||
std::string token;
|
||||
@ -114,8 +115,8 @@ namespace ix
|
||||
|
||||
// Parse request line (GET /foo HTTP/1.1\r\n)
|
||||
auto requestLine = Http::parseRequestLine(line);
|
||||
auto method = std::get<0>(requestLine);
|
||||
auto uri = std::get<1>(requestLine);
|
||||
auto method = std::get<0>(requestLine);
|
||||
auto uri = std::get<1>(requestLine);
|
||||
auto httpVersion = std::get<2>(requestLine);
|
||||
|
||||
// Retrieve and validate HTTP headers
|
||||
@ -161,8 +162,6 @@ namespace ix
|
||||
return false;
|
||||
}
|
||||
|
||||
return response->payload.empty()
|
||||
? true
|
||||
: socket->writeBytes(response->payload, nullptr);
|
||||
return response->payload.empty() ? true : socket->writeBytes(response->payload, nullptr);
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -115,8 +115,7 @@ namespace ix
|
||||
std::shared_ptr<Socket> socket);
|
||||
static bool sendResponse(HttpResponsePtr response, std::shared_ptr<Socket> socket);
|
||||
|
||||
static std::pair<std::string, int> parseStatusLine(
|
||||
const std::string& line);
|
||||
static std::pair<std::string, int> parseStatusLine(const std::string& line);
|
||||
static std::tuple<std::string, std::string, std::string> parseRequestLine(
|
||||
const std::string& line);
|
||||
static std::string trim(const std::string& str);
|
||||
|
@ -5,17 +5,16 @@
|
||||
*/
|
||||
|
||||
#include "IXHttpClient.h"
|
||||
|
||||
#include "IXSocketFactory.h"
|
||||
#include "IXUrlParser.h"
|
||||
#include "IXUserAgent.h"
|
||||
#include "IXWebSocketHttpHeaders.h"
|
||||
#include "IXSocketFactory.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
|
||||
#include <assert.h>
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <zlib.h>
|
||||
|
||||
namespace ix
|
||||
@ -26,7 +25,9 @@ namespace ix
|
||||
const std::string HttpClient::kDel = "DEL";
|
||||
const std::string HttpClient::kPut = "PUT";
|
||||
|
||||
HttpClient::HttpClient(bool async) : _async(async), _stop(false)
|
||||
HttpClient::HttpClient(bool async)
|
||||
: _async(async)
|
||||
, _stop(false)
|
||||
{
|
||||
if (!_async) return;
|
||||
|
||||
@ -42,8 +43,7 @@ namespace ix
|
||||
_thread.join();
|
||||
}
|
||||
|
||||
HttpRequestArgsPtr HttpClient::createRequest(const std::string& url,
|
||||
const std::string& verb)
|
||||
HttpRequestArgsPtr HttpClient::createRequest(const std::string& url, const std::string& verb)
|
||||
{
|
||||
auto request = std::make_shared<HttpRequestArgs>();
|
||||
request->url = url;
|
||||
@ -106,12 +106,11 @@ namespace ix
|
||||
}
|
||||
}
|
||||
|
||||
HttpResponsePtr HttpClient::request(
|
||||
const std::string& url,
|
||||
const std::string& verb,
|
||||
const std::string& body,
|
||||
HttpRequestArgsPtr args,
|
||||
int redirects)
|
||||
HttpResponsePtr HttpClient::request(const std::string& url,
|
||||
const std::string& verb,
|
||||
const std::string& body,
|
||||
HttpRequestArgsPtr args,
|
||||
int redirects)
|
||||
{
|
||||
// We only have one socket connection, so we cannot
|
||||
// make multiple requests concurrently.
|
||||
@ -131,9 +130,14 @@ namespace ix
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Cannot parse url: " << url;
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::UrlMalformed,
|
||||
headers, payload, ss.str(),
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::UrlMalformed,
|
||||
headers,
|
||||
payload,
|
||||
ss.str(),
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
bool tls = protocol == "https";
|
||||
@ -143,9 +147,14 @@ namespace ix
|
||||
|
||||
if (!_socket)
|
||||
{
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::CannotCreateSocket,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::CannotCreateSocket,
|
||||
headers,
|
||||
payload,
|
||||
errorMsg,
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
// Build request string
|
||||
@ -155,7 +164,8 @@ namespace ix
|
||||
|
||||
if (args->compress)
|
||||
{
|
||||
ss << "Accept-Encoding: gzip" << "\r\n";
|
||||
ss << "Accept-Encoding: gzip"
|
||||
<< "\r\n";
|
||||
}
|
||||
|
||||
// Append extra headers
|
||||
@ -167,7 +177,8 @@ namespace ix
|
||||
// Set a default Accept header if none is present
|
||||
if (headers.find("Accept") == headers.end())
|
||||
{
|
||||
ss << "Accept: */*" << "\r\n";
|
||||
ss << "Accept: */*"
|
||||
<< "\r\n";
|
||||
}
|
||||
|
||||
// Set a default User agent if none is present
|
||||
@ -183,7 +194,8 @@ namespace ix
|
||||
// Set default Content-Type if unspecified
|
||||
if (args->extraHeaders.find("Content-Type") == args->extraHeaders.end())
|
||||
{
|
||||
ss << "Content-Type: application/x-www-form-urlencoded" << "\r\n";
|
||||
ss << "Content-Type: application/x-www-form-urlencoded"
|
||||
<< "\r\n";
|
||||
}
|
||||
ss << "\r\n";
|
||||
ss << body;
|
||||
@ -206,9 +218,14 @@ namespace ix
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Cannot connect to url: " << url << " / error : " << errMsg;
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::CannotConnect,
|
||||
headers, payload, ss.str(),
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::CannotConnect,
|
||||
headers,
|
||||
payload,
|
||||
ss.str(),
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
// Make a new cancellation object dealing with transfer timeout
|
||||
@ -222,8 +239,7 @@ namespace ix
|
||||
<< "to " << host << ":" << port << std::endl
|
||||
<< "request size: " << req.size() << " bytes" << std::endl
|
||||
<< "=============" << std::endl
|
||||
<< req
|
||||
<< "=============" << std::endl
|
||||
<< req << "=============" << std::endl
|
||||
<< std::endl;
|
||||
|
||||
log(ss.str(), args);
|
||||
@ -232,9 +248,14 @@ namespace ix
|
||||
if (!_socket->writeBytes(req, isCancellationRequested))
|
||||
{
|
||||
std::string errorMsg("Cannot send request");
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::SendError,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::SendError,
|
||||
headers,
|
||||
payload,
|
||||
errorMsg,
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
uploadSize = req.size();
|
||||
@ -246,9 +267,14 @@ namespace ix
|
||||
if (!lineValid)
|
||||
{
|
||||
std::string errorMsg("Cannot retrieve status line");
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::CannotReadStatusLine,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::CannotReadStatusLine,
|
||||
headers,
|
||||
payload,
|
||||
errorMsg,
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
if (args->verbose)
|
||||
@ -261,9 +287,14 @@ namespace ix
|
||||
if (sscanf(line.c_str(), "HTTP/1.1 %d", &code) != 1)
|
||||
{
|
||||
std::string errorMsg("Cannot parse response code from status line");
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::MissingStatus,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::MissingStatus,
|
||||
headers,
|
||||
payload,
|
||||
errorMsg,
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
auto result = parseHttpHeaders(_socket, isCancellationRequested);
|
||||
@ -273,9 +304,14 @@ namespace ix
|
||||
if (!headersValid)
|
||||
{
|
||||
std::string errorMsg("Cannot parse http headers");
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::HeaderParsingError,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::HeaderParsingError,
|
||||
headers,
|
||||
payload,
|
||||
errorMsg,
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
// Redirect ?
|
||||
@ -284,30 +320,45 @@ namespace ix
|
||||
if (headers.find("Location") == headers.end())
|
||||
{
|
||||
std::string errorMsg("Missing location header for redirect");
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::MissingLocation,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::MissingLocation,
|
||||
headers,
|
||||
payload,
|
||||
errorMsg,
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
if (redirects >= args->maxRedirects)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Too many redirects: " << redirects;
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::TooManyRedirects,
|
||||
headers, payload, ss.str(),
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::TooManyRedirects,
|
||||
headers,
|
||||
payload,
|
||||
ss.str(),
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
// Recurse
|
||||
std::string location = headers["Location"];
|
||||
return request(location, verb, body, args, redirects+1);
|
||||
return request(location, verb, body, args, redirects + 1);
|
||||
}
|
||||
|
||||
if (verb == "HEAD")
|
||||
{
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::Ok,
|
||||
headers, payload, std::string(),
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::Ok,
|
||||
headers,
|
||||
payload,
|
||||
std::string(),
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
// Parse response:
|
||||
@ -320,15 +371,19 @@ namespace ix
|
||||
|
||||
payload.reserve(contentLength);
|
||||
|
||||
auto chunkResult = _socket->readBytes(contentLength,
|
||||
args->onProgressCallback,
|
||||
isCancellationRequested);
|
||||
auto chunkResult = _socket->readBytes(
|
||||
contentLength, args->onProgressCallback, isCancellationRequested);
|
||||
if (!chunkResult.first)
|
||||
{
|
||||
errorMsg = "Cannot read chunk";
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::ChunkReadError,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::ChunkReadError,
|
||||
headers,
|
||||
payload,
|
||||
errorMsg,
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
payload += chunkResult.second;
|
||||
}
|
||||
@ -344,9 +399,14 @@ namespace ix
|
||||
|
||||
if (!lineResult.first)
|
||||
{
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::ChunkReadError,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::ChunkReadError,
|
||||
headers,
|
||||
payload,
|
||||
errorMsg,
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
uint64_t chunkSize;
|
||||
@ -357,23 +417,26 @@ namespace ix
|
||||
if (args->verbose)
|
||||
{
|
||||
std::stringstream oss;
|
||||
oss << "Reading " << chunkSize << " bytes"
|
||||
<< std::endl;
|
||||
oss << "Reading " << chunkSize << " bytes" << std::endl;
|
||||
log(oss.str(), args);
|
||||
}
|
||||
|
||||
payload.reserve(payload.size() + (size_t) chunkSize);
|
||||
|
||||
// Read a chunk
|
||||
auto chunkResult = _socket->readBytes((size_t) chunkSize,
|
||||
args->onProgressCallback,
|
||||
isCancellationRequested);
|
||||
auto chunkResult = _socket->readBytes(
|
||||
(size_t) chunkSize, args->onProgressCallback, isCancellationRequested);
|
||||
if (!chunkResult.first)
|
||||
{
|
||||
errorMsg = "Cannot read chunk";
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::ChunkReadError,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::ChunkReadError,
|
||||
headers,
|
||||
payload,
|
||||
errorMsg,
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
payload += chunkResult.second;
|
||||
|
||||
@ -382,9 +445,14 @@ namespace ix
|
||||
|
||||
if (!lineResult.first)
|
||||
{
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::ChunkReadError,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::ChunkReadError,
|
||||
headers,
|
||||
payload,
|
||||
errorMsg,
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
if (chunkSize == 0) break;
|
||||
@ -397,9 +465,14 @@ namespace ix
|
||||
else
|
||||
{
|
||||
std::string errorMsg("Cannot read http body");
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::CannotReadBody,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::CannotReadBody,
|
||||
headers,
|
||||
payload,
|
||||
errorMsg,
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
downloadSize = payload.size();
|
||||
@ -411,32 +484,39 @@ namespace ix
|
||||
if (!gzipInflate(payload, decompressedPayload))
|
||||
{
|
||||
std::string errorMsg("Error decompressing payload");
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::Gzip,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::Gzip,
|
||||
headers,
|
||||
payload,
|
||||
errorMsg,
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
payload = decompressedPayload;
|
||||
}
|
||||
|
||||
return std::make_shared<HttpResponse>(code, description, HttpErrorCode::Ok,
|
||||
headers, payload, std::string(),
|
||||
uploadSize, downloadSize);
|
||||
return std::make_shared<HttpResponse>(code,
|
||||
description,
|
||||
HttpErrorCode::Ok,
|
||||
headers,
|
||||
payload,
|
||||
std::string(),
|
||||
uploadSize,
|
||||
downloadSize);
|
||||
}
|
||||
|
||||
HttpResponsePtr HttpClient::get(const std::string& url,
|
||||
HttpRequestArgsPtr args)
|
||||
HttpResponsePtr HttpClient::get(const std::string& url, HttpRequestArgsPtr args)
|
||||
{
|
||||
return request(url, kGet, std::string(), args);
|
||||
}
|
||||
|
||||
HttpResponsePtr HttpClient::head(const std::string& url,
|
||||
HttpRequestArgsPtr args)
|
||||
HttpResponsePtr HttpClient::head(const std::string& url, HttpRequestArgsPtr args)
|
||||
{
|
||||
return request(url, kHead, std::string(), args);
|
||||
}
|
||||
|
||||
HttpResponsePtr HttpClient::del(const std::string& url,
|
||||
HttpRequestArgsPtr args)
|
||||
HttpResponsePtr HttpClient::del(const std::string& url, HttpRequestArgsPtr args)
|
||||
{
|
||||
return request(url, kDel, std::string(), args);
|
||||
}
|
||||
@ -475,8 +555,7 @@ namespace ix
|
||||
escaped.fill('0');
|
||||
escaped << std::hex;
|
||||
|
||||
for (std::string::const_iterator i = value.begin(), n = value.end();
|
||||
i != n; ++i)
|
||||
for (std::string::const_iterator i = value.begin(), n = value.end(); i != n; ++i)
|
||||
{
|
||||
std::string::value_type c = (*i);
|
||||
|
||||
@ -504,21 +583,17 @@ namespace ix
|
||||
|
||||
for (auto&& it : httpParameters)
|
||||
{
|
||||
ss << urlEncode(it.first)
|
||||
<< "="
|
||||
<< urlEncode(it.second);
|
||||
ss << urlEncode(it.first) << "=" << urlEncode(it.second);
|
||||
|
||||
if (i++ < (count-1))
|
||||
if (i++ < (count - 1))
|
||||
{
|
||||
ss << "&";
|
||||
ss << "&";
|
||||
}
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
bool HttpClient::gzipInflate(
|
||||
const std::string& in,
|
||||
std::string& out)
|
||||
bool HttpClient::gzipInflate(const std::string& in, std::string& out)
|
||||
{
|
||||
z_stream inflateState;
|
||||
std::memset(&inflateState, 0, sizeof(inflateState));
|
||||
@ -529,13 +604,13 @@ namespace ix
|
||||
inflateState.avail_in = 0;
|
||||
inflateState.next_in = Z_NULL;
|
||||
|
||||
if (inflateInit2(&inflateState, 16+MAX_WBITS) != Z_OK)
|
||||
if (inflateInit2(&inflateState, 16 + MAX_WBITS) != Z_OK)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
inflateState.avail_in = (uInt) in.size();
|
||||
inflateState.next_in = (unsigned char *)(const_cast<char *>(in.data()));
|
||||
inflateState.next_in = (unsigned char*) (const_cast<char*>(in.data()));
|
||||
|
||||
const int kBufferSize = 1 << 14;
|
||||
|
||||
@ -555,22 +630,19 @@ namespace ix
|
||||
return false;
|
||||
}
|
||||
|
||||
out.append(
|
||||
reinterpret_cast<char *>(compressBuffer.get()),
|
||||
kBufferSize - inflateState.avail_out
|
||||
);
|
||||
out.append(reinterpret_cast<char*>(compressBuffer.get()),
|
||||
kBufferSize - inflateState.avail_out);
|
||||
} while (inflateState.avail_out == 0);
|
||||
|
||||
inflateEnd(&inflateState);
|
||||
return true;
|
||||
}
|
||||
|
||||
void HttpClient::log(const std::string& msg,
|
||||
HttpRequestArgsPtr args)
|
||||
void HttpClient::log(const std::string& msg, HttpRequestArgsPtr args)
|
||||
{
|
||||
if (args->logger)
|
||||
{
|
||||
args->logger(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -5,13 +5,13 @@
|
||||
*/
|
||||
|
||||
#include "IXHttpServer.h"
|
||||
|
||||
#include "IXNetSystem.h"
|
||||
#include "IXSocketConnect.h"
|
||||
#include "IXSocketFactory.h"
|
||||
#include "IXNetSystem.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
@ -28,7 +28,7 @@ namespace
|
||||
file.seekg(0, file.beg);
|
||||
|
||||
memblock.resize((size_t) size);
|
||||
file.read((char*)&memblock.front(), static_cast<std::streamsize>(size));
|
||||
file.read((char*) &memblock.front(), static_cast<std::streamsize>(size));
|
||||
|
||||
return std::make_pair(true, memblock);
|
||||
}
|
||||
@ -39,15 +39,13 @@ namespace
|
||||
auto vec = res.second;
|
||||
return std::make_pair(res.first, std::string(vec.begin(), vec.end()));
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace ix
|
||||
{
|
||||
HttpServer::HttpServer(int port,
|
||||
const std::string& host,
|
||||
int backlog,
|
||||
size_t maxConnections) : SocketServer(port, host, backlog, maxConnections),
|
||||
_connectedClientsCount(0)
|
||||
HttpServer::HttpServer(int port, const std::string& host, int backlog, size_t maxConnections)
|
||||
: SocketServer(port, host, backlog, maxConnections)
|
||||
, _connectedClientsCount(0)
|
||||
{
|
||||
setDefaultConnectionCallback();
|
||||
}
|
||||
@ -71,9 +69,7 @@ namespace ix
|
||||
_onConnectionCallback = callback;
|
||||
}
|
||||
|
||||
void HttpServer::handleConnection(
|
||||
int fd,
|
||||
std::shared_ptr<ConnectionState> connectionState)
|
||||
void HttpServer::handleConnection(int fd, std::shared_ptr<ConnectionState> connectionState)
|
||||
{
|
||||
_connectedClientsCount++;
|
||||
|
||||
@ -109,8 +105,7 @@ namespace ix
|
||||
{
|
||||
setOnConnectionCallback(
|
||||
[this](HttpRequestPtr request,
|
||||
std::shared_ptr<ConnectionState> /*connectionState*/) -> HttpResponsePtr
|
||||
{
|
||||
std::shared_ptr<ConnectionState> /*connectionState*/) -> HttpResponsePtr {
|
||||
std::string uri(request->uri);
|
||||
if (uri.empty() || uri == "/")
|
||||
{
|
||||
@ -122,23 +117,16 @@ namespace ix
|
||||
bool found = res.first;
|
||||
if (!found)
|
||||
{
|
||||
return std::make_shared<HttpResponse>(404, "Not Found",
|
||||
HttpErrorCode::Ok,
|
||||
WebSocketHttpHeaders(),
|
||||
std::string());
|
||||
return std::make_shared<HttpResponse>(
|
||||
404, "Not Found", HttpErrorCode::Ok, WebSocketHttpHeaders(), std::string());
|
||||
}
|
||||
|
||||
std::string content = res.second;
|
||||
|
||||
// Log request
|
||||
std::stringstream ss;
|
||||
ss << request->method
|
||||
<< " "
|
||||
<< request->headers["User-Agent"]
|
||||
<< " "
|
||||
<< request->uri
|
||||
<< " "
|
||||
<< content.size();
|
||||
ss << request->method << " " << request->headers["User-Agent"] << " "
|
||||
<< request->uri << " " << content.size();
|
||||
logInfo(ss.str());
|
||||
|
||||
WebSocketHttpHeaders headers;
|
||||
@ -151,11 +139,8 @@ namespace ix
|
||||
headers[it.first] = it.second;
|
||||
}
|
||||
|
||||
return std::make_shared<HttpResponse>(200, "OK",
|
||||
HttpErrorCode::Ok,
|
||||
headers,
|
||||
content);
|
||||
}
|
||||
);
|
||||
return std::make_shared<HttpResponse>(
|
||||
200, "OK", HttpErrorCode::Ok, headers, content);
|
||||
});
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -42,7 +42,7 @@ namespace ix
|
||||
//
|
||||
// So we make it a select wrapper
|
||||
//
|
||||
int poll(struct pollfd *fds, nfds_t nfds, int timeout)
|
||||
int poll(struct pollfd* fds, nfds_t nfds, int timeout)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
int maxfd = 0;
|
||||
@ -53,7 +53,7 @@ namespace ix
|
||||
|
||||
for (nfds_t i = 0; i < nfds; ++i)
|
||||
{
|
||||
struct pollfd *fd = &fds[i];
|
||||
struct pollfd* fd = &fds[i];
|
||||
|
||||
if (fd->fd > maxfd)
|
||||
{
|
||||
@ -77,8 +77,7 @@ namespace ix
|
||||
tv.tv_sec = timeout / 1000;
|
||||
tv.tv_usec = (timeout % 1000) * 1000;
|
||||
|
||||
int ret = select(maxfd + 1, &readfds, &writefds, &errorfds,
|
||||
timeout != -1 ? &tv : NULL);
|
||||
int ret = select(maxfd + 1, &readfds, &writefds, &errorfds, timeout != -1 ? &tv : NULL);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
@ -87,7 +86,7 @@ namespace ix
|
||||
|
||||
for (nfds_t i = 0; i < nfds; ++i)
|
||||
{
|
||||
struct pollfd *fd = &fds[i];
|
||||
struct pollfd* fd = &fds[i];
|
||||
fd->revents = 0;
|
||||
|
||||
if (FD_ISSET(fd->fd, &readfds))
|
||||
|
@ -42,5 +42,4 @@ namespace ix
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ix
|
||||
|
@ -26,14 +26,13 @@
|
||||
|
||||
#include "IXSelectInterruptEventFd.h"
|
||||
|
||||
#include <sys/eventfd.h>
|
||||
|
||||
#include <unistd.h> // for write
|
||||
#include <string.h> // for strerror
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sstream>
|
||||
#include <string.h> // for strerror
|
||||
#include <sys/eventfd.h>
|
||||
#include <unistd.h> // for write
|
||||
|
||||
namespace ix
|
||||
{
|
||||
@ -113,4 +112,4 @@ namespace ix
|
||||
{
|
||||
return _eventfd;
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -7,9 +7,9 @@
|
||||
#include "IXSelectInterruptFactory.h"
|
||||
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
# include <ixwebsocket/IXSelectInterruptPipe.h>
|
||||
#include <ixwebsocket/IXSelectInterruptPipe.h>
|
||||
#else
|
||||
# include <ixwebsocket/IXSelectInterrupt.h>
|
||||
#include <ixwebsocket/IXSelectInterrupt.h>
|
||||
#endif
|
||||
|
||||
namespace ix
|
||||
@ -22,4 +22,4 @@ namespace ix
|
||||
return std::make_shared<SelectInterrupt>();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -10,12 +10,12 @@
|
||||
|
||||
#include "IXSelectInterruptPipe.h"
|
||||
|
||||
#include <unistd.h> // for write
|
||||
#include <string.h> // for strerror
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sstream>
|
||||
#include <string.h> // for strerror
|
||||
#include <unistd.h> // for write
|
||||
|
||||
namespace ix
|
||||
{
|
||||
@ -143,4 +143,4 @@ namespace ix
|
||||
|
||||
return _fildes[kPipeReadIndex];
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -5,21 +5,20 @@
|
||||
*/
|
||||
|
||||
#include "IXSocket.h"
|
||||
#include "IXSocketConnect.h"
|
||||
|
||||
#include "IXNetSystem.h"
|
||||
#include "IXSelectInterrupt.h"
|
||||
#include "IXSelectInterruptFactory.h"
|
||||
|
||||
#include "IXSocketConnect.h"
|
||||
#include <algorithm>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#ifdef min
|
||||
#undef min
|
||||
#endif
|
||||
@ -32,9 +31,9 @@ namespace ix
|
||||
const uint64_t Socket::kCloseRequest = 2;
|
||||
constexpr size_t Socket::kChunkSize;
|
||||
|
||||
Socket::Socket(int fd) :
|
||||
_sockfd(fd),
|
||||
_selectInterrupt(createSelectInterrupt())
|
||||
Socket::Socket(int fd)
|
||||
: _sockfd(fd)
|
||||
, _selectInterrupt(createSelectInterrupt())
|
||||
{
|
||||
;
|
||||
}
|
||||
@ -123,8 +122,7 @@ namespace ix
|
||||
|
||||
// getsockopt() puts the errno value for connect into optval so 0
|
||||
// means no-error.
|
||||
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, &optlen) == -1 ||
|
||||
optval != 0)
|
||||
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, &optlen) == -1 || optval != 0)
|
||||
{
|
||||
pollResult = PollResultType::Error;
|
||||
|
||||
@ -201,7 +199,7 @@ namespace ix
|
||||
|
||||
ssize_t Socket::send(const std::string& buffer)
|
||||
{
|
||||
return send((char*)&buffer[0], buffer.size());
|
||||
return send((char*) &buffer[0], buffer.size());
|
||||
}
|
||||
|
||||
ssize_t Socket::recv(void* buffer, size_t length)
|
||||
@ -263,7 +261,7 @@ namespace ix
|
||||
{
|
||||
if (isCancellationRequested && isCancellationRequested()) return false;
|
||||
|
||||
ssize_t ret = send((char*)&str[offset], len);
|
||||
ssize_t ret = send((char*) &str[offset], len);
|
||||
|
||||
// We wrote some bytes, as needed, all good.
|
||||
if (ret > 0)
|
||||
@ -292,8 +290,7 @@ namespace ix
|
||||
}
|
||||
}
|
||||
|
||||
bool Socket::readByte(void* buffer,
|
||||
const CancellationRequest& isCancellationRequested)
|
||||
bool Socket::readByte(void* buffer, const CancellationRequest& isCancellationRequested)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
@ -332,7 +329,7 @@ namespace ix
|
||||
std::string line;
|
||||
line.reserve(64);
|
||||
|
||||
for (int i = 0; i < 2 || (line[i-2] != '\r' && line[i-1] != '\n'); ++i)
|
||||
for (int i = 0; i < 2 || (line[i - 2] != '\r' && line[i - 1] != '\n'); ++i)
|
||||
{
|
||||
if (!readByte(&c, isCancellationRequested))
|
||||
{
|
||||
@ -365,13 +362,11 @@ namespace ix
|
||||
}
|
||||
|
||||
size_t size = std::min(kChunkSize, length - output.size());
|
||||
ssize_t ret = recv((char*)&_readBuffer[0], size);
|
||||
ssize_t ret = recv((char*) &_readBuffer[0], size);
|
||||
|
||||
if (ret > 0)
|
||||
{
|
||||
output.insert(output.end(),
|
||||
_readBuffer.begin(),
|
||||
_readBuffer.begin() + ret);
|
||||
output.insert(output.end(), _readBuffer.begin(), _readBuffer.begin() + ret);
|
||||
}
|
||||
else if (ret <= 0 && !Socket::isWaitNeeded())
|
||||
{
|
||||
@ -388,7 +383,6 @@ namespace ix
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_pair(true, std::string(output.begin(),
|
||||
output.end()));
|
||||
return std::make_pair(true, std::string(output.begin(), output.end()));
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -6,11 +6,13 @@
|
||||
* Adapted from Satori SDK Apple SSL code.
|
||||
*/
|
||||
#include "IXSocketAppleSSL.h"
|
||||
#include "IXSocketConnect.h"
|
||||
|
||||
#include "IXSocketConnect.h"
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -18,132 +20,125 @@
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <errno.h>
|
||||
#define socketerrno errno
|
||||
|
||||
#include <Security/SecureTransport.h>
|
||||
|
||||
namespace {
|
||||
|
||||
OSStatus read_from_socket(SSLConnectionRef connection, void *data, size_t *len)
|
||||
namespace
|
||||
{
|
||||
int fd = (int) (long) connection;
|
||||
if (fd < 0)
|
||||
return errSSLInternal;
|
||||
|
||||
assert(data != nullptr);
|
||||
assert(len != nullptr);
|
||||
|
||||
size_t requested_sz = *len;
|
||||
|
||||
ssize_t status = read(fd, data, requested_sz);
|
||||
|
||||
if (status > 0)
|
||||
OSStatus read_from_socket(SSLConnectionRef connection, void* data, size_t* len)
|
||||
{
|
||||
*len = (size_t) status;
|
||||
if (requested_sz > *len)
|
||||
return errSSLWouldBlock;
|
||||
else
|
||||
return noErr;
|
||||
}
|
||||
else if (0 == status)
|
||||
{
|
||||
*len = 0;
|
||||
return errSSLClosedGraceful;
|
||||
}
|
||||
else
|
||||
{
|
||||
*len = 0;
|
||||
switch (errno) {
|
||||
case ENOENT:
|
||||
return errSSLClosedGraceful;
|
||||
int fd = (int) (long) connection;
|
||||
if (fd < 0) return errSSLInternal;
|
||||
|
||||
case EAGAIN:
|
||||
assert(data != nullptr);
|
||||
assert(len != nullptr);
|
||||
|
||||
size_t requested_sz = *len;
|
||||
|
||||
ssize_t status = read(fd, data, requested_sz);
|
||||
|
||||
if (status > 0)
|
||||
{
|
||||
*len = (size_t) status;
|
||||
if (requested_sz > *len)
|
||||
return errSSLWouldBlock;
|
||||
|
||||
case ECONNRESET:
|
||||
return errSSLClosedAbort;
|
||||
|
||||
default:
|
||||
return errSecIO;
|
||||
else
|
||||
return noErr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OSStatus write_to_socket(SSLConnectionRef connection, const void *data, size_t *len)
|
||||
{
|
||||
int fd = (int) (long) connection;
|
||||
if (fd < 0)
|
||||
return errSSLInternal;
|
||||
|
||||
assert(data != nullptr);
|
||||
assert(len != nullptr);
|
||||
|
||||
size_t to_write_sz = *len;
|
||||
ssize_t status = write(fd, data, to_write_sz);
|
||||
|
||||
if (status > 0)
|
||||
{
|
||||
*len = (size_t) status;
|
||||
if (to_write_sz > *len)
|
||||
return errSSLWouldBlock;
|
||||
else
|
||||
return noErr;
|
||||
}
|
||||
else if (0 == status)
|
||||
{
|
||||
*len = 0;
|
||||
return errSSLClosedGraceful;
|
||||
}
|
||||
else
|
||||
{
|
||||
*len = 0;
|
||||
if (EAGAIN == errno)
|
||||
else if (0 == status)
|
||||
{
|
||||
return errSSLWouldBlock;
|
||||
*len = 0;
|
||||
return errSSLClosedGraceful;
|
||||
}
|
||||
else
|
||||
{
|
||||
return errSecIO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string getSSLErrorDescription(OSStatus status)
|
||||
{
|
||||
std::string errMsg("Unknown SSL error.");
|
||||
|
||||
CFErrorRef error = CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainOSStatus, status, NULL);
|
||||
if (error)
|
||||
{
|
||||
CFStringRef message = CFErrorCopyDescription(error);
|
||||
if (message)
|
||||
{
|
||||
char localBuffer[128];
|
||||
Boolean success;
|
||||
success = CFStringGetCString(message, localBuffer, 128,
|
||||
CFStringGetSystemEncoding());
|
||||
if (success)
|
||||
*len = 0;
|
||||
switch (errno)
|
||||
{
|
||||
errMsg = localBuffer;
|
||||
case ENOENT: return errSSLClosedGraceful;
|
||||
|
||||
case EAGAIN: return errSSLWouldBlock;
|
||||
|
||||
case ECONNRESET: return errSSLClosedAbort;
|
||||
|
||||
default: return errSecIO;
|
||||
}
|
||||
CFRelease(message);
|
||||
}
|
||||
CFRelease(error);
|
||||
}
|
||||
|
||||
return errMsg;
|
||||
}
|
||||
OSStatus write_to_socket(SSLConnectionRef connection, const void* data, size_t* len)
|
||||
{
|
||||
int fd = (int) (long) connection;
|
||||
if (fd < 0) return errSSLInternal;
|
||||
|
||||
assert(data != nullptr);
|
||||
assert(len != nullptr);
|
||||
|
||||
size_t to_write_sz = *len;
|
||||
ssize_t status = write(fd, data, to_write_sz);
|
||||
|
||||
if (status > 0)
|
||||
{
|
||||
*len = (size_t) status;
|
||||
if (to_write_sz > *len)
|
||||
return errSSLWouldBlock;
|
||||
else
|
||||
return noErr;
|
||||
}
|
||||
else if (0 == status)
|
||||
{
|
||||
*len = 0;
|
||||
return errSSLClosedGraceful;
|
||||
}
|
||||
else
|
||||
{
|
||||
*len = 0;
|
||||
if (EAGAIN == errno)
|
||||
{
|
||||
return errSSLWouldBlock;
|
||||
}
|
||||
else
|
||||
{
|
||||
return errSecIO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string getSSLErrorDescription(OSStatus status)
|
||||
{
|
||||
std::string errMsg("Unknown SSL error.");
|
||||
|
||||
CFErrorRef error = CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainOSStatus, status, NULL);
|
||||
if (error)
|
||||
{
|
||||
CFStringRef message = CFErrorCopyDescription(error);
|
||||
if (message)
|
||||
{
|
||||
char localBuffer[128];
|
||||
Boolean success;
|
||||
success =
|
||||
CFStringGetCString(message, localBuffer, 128, CFStringGetSystemEncoding());
|
||||
if (success)
|
||||
{
|
||||
errMsg = localBuffer;
|
||||
}
|
||||
CFRelease(message);
|
||||
}
|
||||
CFRelease(error);
|
||||
}
|
||||
|
||||
return errMsg;
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
namespace ix
|
||||
{
|
||||
SocketAppleSSL::SocketAppleSSL(const SocketTLSOptions& tlsOptions, int fd) : Socket(fd),
|
||||
_sslContext(nullptr),
|
||||
_tlsOptions(tlsOptions)
|
||||
SocketAppleSSL::SocketAppleSSL(const SocketTLSOptions& tlsOptions, int fd)
|
||||
: Socket(fd)
|
||||
, _sslContext(nullptr)
|
||||
, _tlsOptions(tlsOptions)
|
||||
{
|
||||
;
|
||||
}
|
||||
@ -169,14 +164,14 @@ namespace ix
|
||||
_sslContext = SSLCreateContext(kCFAllocatorDefault, kSSLClientSide, kSSLStreamType);
|
||||
|
||||
SSLSetIOFuncs(_sslContext, read_from_socket, write_to_socket);
|
||||
SSLSetConnection(_sslContext, (SSLConnectionRef) (long) _sockfd);
|
||||
SSLSetConnection(_sslContext, (SSLConnectionRef)(long) _sockfd);
|
||||
SSLSetProtocolVersionMin(_sslContext, kTLSProtocol12);
|
||||
SSLSetPeerDomainName(_sslContext, host.c_str(), host.size());
|
||||
|
||||
do {
|
||||
do
|
||||
{
|
||||
status = SSLHandshake(_sslContext);
|
||||
} while (errSSLWouldBlock == status ||
|
||||
errSSLServerAuthCompleted == status);
|
||||
} while (errSSLWouldBlock == status || errSSLServerAuthCompleted == status);
|
||||
}
|
||||
|
||||
if (noErr != status)
|
||||
@ -206,7 +201,8 @@ namespace ix
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
OSStatus status;
|
||||
do {
|
||||
do
|
||||
{
|
||||
size_t processed = 0;
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
status = SSLWrite(_sslContext, buf, nbyte, &processed);
|
||||
@ -215,14 +211,13 @@ namespace ix
|
||||
nbyte -= processed;
|
||||
} while (nbyte > 0 && errSSLWouldBlock == status);
|
||||
|
||||
if (ret == 0 && errSSLClosedAbort != status)
|
||||
ret = -1;
|
||||
if (ret == 0 && errSSLClosedAbort != status) ret = -1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t SocketAppleSSL::send(const std::string& buffer)
|
||||
{
|
||||
return send((char*)&buffer[0], buffer.size());
|
||||
return send((char*) &buffer[0], buffer.size());
|
||||
}
|
||||
|
||||
// No wait support
|
||||
@ -235,13 +230,11 @@ namespace ix
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
status = SSLRead(_sslContext, buf, nbyte, &processed);
|
||||
|
||||
if (processed > 0)
|
||||
return (ssize_t) processed;
|
||||
if (processed > 0) return (ssize_t) processed;
|
||||
|
||||
// The connection was reset, inform the caller that this
|
||||
// Socket should close
|
||||
if (status == errSSLClosedGraceful ||
|
||||
status == errSSLClosedNoNotify ||
|
||||
if (status == errSSLClosedGraceful || status == errSSLClosedNoNotify ||
|
||||
status == errSSLClosedAbort)
|
||||
{
|
||||
errno = ECONNRESET;
|
||||
@ -257,4 +250,4 @@ namespace ix
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -5,36 +5,34 @@
|
||||
*/
|
||||
|
||||
#include "IXSocketConnect.h"
|
||||
|
||||
#include "IXDNSLookup.h"
|
||||
#include "IXNetSystem.h"
|
||||
#include "IXSocket.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
// Android needs extra headers for TCP_NODELAY and IPPROTO_TCP
|
||||
#ifdef ANDROID
|
||||
# include <linux/in.h>
|
||||
# include <linux/tcp.h>
|
||||
#include <linux/in.h>
|
||||
#include <linux/tcp.h>
|
||||
#endif
|
||||
|
||||
namespace ix
|
||||
{
|
||||
//
|
||||
// This function can be cancelled every 50 ms
|
||||
// This is important so that we don't block the main UI thread when shutting down a connection which is
|
||||
// already trying to reconnect, and can be blocked waiting for ::connect to respond.
|
||||
// This is important so that we don't block the main UI thread when shutting down a connection
|
||||
// which is already trying to reconnect, and can be blocked waiting for ::connect to respond.
|
||||
//
|
||||
int SocketConnect::connectToAddress(const struct addrinfo *address,
|
||||
int SocketConnect::connectToAddress(const struct addrinfo* address,
|
||||
std::string& errMsg,
|
||||
const CancellationRequest& isCancellationRequested)
|
||||
{
|
||||
errMsg = "no error";
|
||||
|
||||
int fd = socket(address->ai_family,
|
||||
address->ai_socktype,
|
||||
address->ai_protocol);
|
||||
int fd = socket(address->ai_family, address->ai_socktype, address->ai_protocol);
|
||||
if (fd < 0)
|
||||
{
|
||||
errMsg = "Cannot create a socket";
|
||||
@ -74,8 +72,7 @@ namespace ix
|
||||
else if (pollResult == PollResultType::Error)
|
||||
{
|
||||
Socket::closeSocket(fd);
|
||||
errMsg = std::string("Connect error: ") +
|
||||
strerror(Socket::getErrno());
|
||||
errMsg = std::string("Connect error: ") + strerror(Socket::getErrno());
|
||||
return -1;
|
||||
}
|
||||
else if (pollResult == PollResultType::ReadyForWrite)
|
||||
@ -85,8 +82,7 @@ namespace ix
|
||||
else
|
||||
{
|
||||
Socket::closeSocket(fd);
|
||||
errMsg = std::string("Connect error: ") +
|
||||
strerror(Socket::getErrno());
|
||||
errMsg = std::string("Connect error: ") + strerror(Socket::getErrno());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@ -105,7 +101,7 @@ namespace ix
|
||||
// First do DNS resolution
|
||||
//
|
||||
auto dnsLookup = std::make_shared<DNSLookup>(hostname, port);
|
||||
struct addrinfo *res = dnsLookup->resolve(errMsg, isCancellationRequested);
|
||||
struct addrinfo* res = dnsLookup->resolve(errMsg, isCancellationRequested);
|
||||
if (res == nullptr)
|
||||
{
|
||||
return -1;
|
||||
@ -114,7 +110,7 @@ namespace ix
|
||||
int sockfd = -1;
|
||||
|
||||
// iterate through the records to find a working peer
|
||||
struct addrinfo *address;
|
||||
struct addrinfo* address;
|
||||
for (address = res; address != nullptr; address = address->ai_next)
|
||||
{
|
||||
//
|
||||
@ -149,8 +145,7 @@ namespace ix
|
||||
// 3. (apple) prevent SIGPIPE from being emitted when the remote end disconnect
|
||||
#ifdef SO_NOSIGPIPE
|
||||
int value = 1;
|
||||
setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE,
|
||||
(void *)&value, sizeof(value));
|
||||
setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void*) &value, sizeof(value));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -7,15 +7,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "IXSocketTLSOptions.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "IXSocketTLSOptions.h"
|
||||
|
||||
namespace ix
|
||||
{
|
||||
class Socket;
|
||||
std::shared_ptr<Socket> createSocket(bool tls, std::string& errorMsg, const SocketTLSOptions& tlsOptions);
|
||||
std::shared_ptr<Socket> createSocket(bool tls,
|
||||
std::string& errorMsg,
|
||||
const SocketTLSOptions& tlsOptions);
|
||||
|
||||
std::shared_ptr<Socket> createSocket(int fd, std::string& errorMsg);
|
||||
} // namespace ix
|
||||
|
@ -9,16 +9,16 @@
|
||||
*/
|
||||
|
||||
#include "IXSocketMbedTLS.h"
|
||||
#include "IXSocketConnect.h"
|
||||
|
||||
#include "IXNetSystem.h"
|
||||
#include "IXSocket.h"
|
||||
|
||||
#include "IXSocketConnect.h"
|
||||
#include <string.h>
|
||||
|
||||
namespace ix
|
||||
{
|
||||
SocketMbedTLS::SocketMbedTLS(const SocketTLSOptions& tlsOptions) :
|
||||
_tlsOptions(tlsOptions)
|
||||
SocketMbedTLS::SocketMbedTLS(const SocketTLSOptions& tlsOptions)
|
||||
: _tlsOptions(tlsOptions)
|
||||
{
|
||||
;
|
||||
}
|
||||
@ -36,13 +36,13 @@ namespace ix
|
||||
mbedtls_ssl_config_init(&_conf);
|
||||
mbedtls_ctr_drbg_init(&_ctr_drbg);
|
||||
|
||||
const char *pers = "IXSocketMbedTLS";
|
||||
const char* pers = "IXSocketMbedTLS";
|
||||
|
||||
mbedtls_entropy_init(&_entropy);
|
||||
if (mbedtls_ctr_drbg_seed(&_ctr_drbg,
|
||||
mbedtls_entropy_func,
|
||||
&_entropy,
|
||||
(const unsigned char *) pers,
|
||||
(const unsigned char*) pers,
|
||||
strlen(pers)) != 0)
|
||||
{
|
||||
errMsg = "Setting entropy seed failed";
|
||||
@ -52,7 +52,7 @@ namespace ix
|
||||
if (mbedtls_ssl_config_defaults(&_conf,
|
||||
MBEDTLS_SSL_IS_CLIENT,
|
||||
MBEDTLS_SSL_TRANSPORT_STREAM,
|
||||
MBEDTLS_SSL_PRESET_DEFAULT ) != 0)
|
||||
MBEDTLS_SSL_PRESET_DEFAULT) != 0)
|
||||
{
|
||||
errMsg = "Setting config default failed";
|
||||
return false;
|
||||
@ -102,8 +102,7 @@ namespace ix
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
res = mbedtls_ssl_handshake(&_ssl);
|
||||
}
|
||||
while (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE);
|
||||
} while (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE);
|
||||
|
||||
if (res != 0)
|
||||
{
|
||||
@ -142,13 +141,18 @@ namespace ix
|
||||
|
||||
ssize_t res = mbedtls_ssl_write(&_ssl, (unsigned char*) buf, nbyte);
|
||||
|
||||
if (res > 0) {
|
||||
if (res > 0)
|
||||
{
|
||||
nbyte -= res;
|
||||
sent += res;
|
||||
} else if (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
||||
}
|
||||
else if (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE)
|
||||
{
|
||||
errno = EWOULDBLOCK;
|
||||
return -1;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@ -157,7 +161,7 @@ namespace ix
|
||||
|
||||
ssize_t SocketMbedTLS::send(const std::string& buffer)
|
||||
{
|
||||
return send((char*)&buffer[0], buffer.size());
|
||||
return send((char*) &buffer[0], buffer.size());
|
||||
}
|
||||
|
||||
ssize_t SocketMbedTLS::recv(void* buf, size_t nbyte)
|
||||
@ -181,4 +185,4 @@ namespace ix
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -121,7 +121,9 @@ namespace ix
|
||||
});
|
||||
|
||||
SSL_CTX_set_verify_depth(ctx, 4);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, nullptr);
|
||||
}
|
||||
|
||||
|
@ -13,12 +13,12 @@
|
||||
#include "IXSocketSChannel.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <basetsd.h>
|
||||
# include <WinSock2.h>
|
||||
# include <ws2def.h>
|
||||
# include <WS2tcpip.h>
|
||||
# include <schannel.h>
|
||||
# include <io.h>
|
||||
#include <WS2tcpip.h>
|
||||
#include <WinSock2.h>
|
||||
#include <basetsd.h>
|
||||
#include <io.h>
|
||||
#include <schannel.h>
|
||||
#include <ws2def.h>
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
|
||||
@ -26,14 +26,15 @@
|
||||
#define UNICODE
|
||||
#endif
|
||||
|
||||
#include <windows.h>
|
||||
#include <winsock2.h>
|
||||
#include <mstcpip.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <rpc.h>
|
||||
#include <ntdsapi.h>
|
||||
#include <rpc.h>
|
||||
#include <stdio.h>
|
||||
#include <tchar.h>
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#define RECV_DATA_BUF_SIZE 256
|
||||
|
||||
@ -50,12 +51,8 @@
|
||||
// has already been initialized
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#else
|
||||
# error("This file should only be built on Windows")
|
||||
#error("This file should only be built on Windows")
|
||||
#endif
|
||||
|
||||
namespace ix
|
||||
@ -67,12 +64,9 @@ namespace ix
|
||||
|
||||
SocketSChannel::~SocketSChannel()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool SocketSChannel::connect(const std::string& host,
|
||||
int port,
|
||||
std::string& errMsg)
|
||||
bool SocketSChannel::connect(const std::string& host, int port, std::string& errMsg)
|
||||
{
|
||||
return Socket::connect(host, port, errMsg, nullptr);
|
||||
}
|
||||
@ -103,4 +97,4 @@ namespace ix
|
||||
return Socket::recv(buf, nbyte);
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -5,14 +5,14 @@
|
||||
*/
|
||||
|
||||
#include "IXSocketServer.h"
|
||||
|
||||
#include "IXNetSystem.h"
|
||||
#include "IXSocket.h"
|
||||
#include "IXSocketConnect.h"
|
||||
#include "IXNetSystem.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
namespace ix
|
||||
{
|
||||
@ -24,17 +24,16 @@ namespace ix
|
||||
SocketServer::SocketServer(int port,
|
||||
const std::string& host,
|
||||
int backlog,
|
||||
size_t maxConnections) :
|
||||
_port(port),
|
||||
_host(host),
|
||||
_backlog(backlog),
|
||||
_maxConnections(maxConnections),
|
||||
_serverFd(-1),
|
||||
_stop(false),
|
||||
_stopGc(false),
|
||||
_connectionStateFactory(&ConnectionState::createConnectionState)
|
||||
size_t maxConnections)
|
||||
: _port(port)
|
||||
, _host(host)
|
||||
, _backlog(backlog)
|
||||
, _maxConnections(maxConnections)
|
||||
, _serverFd(-1)
|
||||
, _stop(false)
|
||||
, _stopGc(false)
|
||||
, _connectionStateFactory(&ConnectionState::createConnectionState)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
SocketServer::~SocketServer()
|
||||
@ -62,21 +61,18 @@ namespace ix
|
||||
if ((_serverFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "SocketServer::listen() error creating socket): "
|
||||
<< strerror(Socket::getErrno());
|
||||
ss << "SocketServer::listen() error creating socket): " << strerror(Socket::getErrno());
|
||||
|
||||
return std::make_pair(false, ss.str());
|
||||
}
|
||||
|
||||
// Make that socket reusable. (allow restarting this server at will)
|
||||
int enable = 1;
|
||||
if (setsockopt(_serverFd, SOL_SOCKET, SO_REUSEADDR,
|
||||
(char*) &enable, sizeof(enable)) < 0)
|
||||
if (setsockopt(_serverFd, SOL_SOCKET, SO_REUSEADDR, (char*) &enable, sizeof(enable)) < 0)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "SocketServer::listen() error calling setsockopt(SO_REUSEADDR) "
|
||||
<< "at address " << _host << ":" << _port
|
||||
<< " : " << strerror(Socket::getErrno());
|
||||
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno());
|
||||
|
||||
Socket::closeSocket(_serverFd);
|
||||
return std::make_pair(false, ss.str());
|
||||
@ -84,7 +80,7 @@ namespace ix
|
||||
|
||||
// Bind the socket to the server address.
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(_port);
|
||||
server.sin_port = htons(_port);
|
||||
|
||||
// Using INADDR_ANY trigger a pop-up box as binding to any address is detected
|
||||
// by the osx firewall. We need to codesign the binary with a self-signed cert
|
||||
@ -95,12 +91,11 @@ namespace ix
|
||||
//
|
||||
server.sin_addr.s_addr = inet_addr(_host.c_str());
|
||||
|
||||
if (bind(_serverFd, (struct sockaddr *)&server, sizeof(server)) < 0)
|
||||
if (bind(_serverFd, (struct sockaddr*) &server, sizeof(server)) < 0)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "SocketServer::listen() error calling bind "
|
||||
<< "at address " << _host << ":" << _port
|
||||
<< " : " << strerror(Socket::getErrno());
|
||||
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno());
|
||||
|
||||
Socket::closeSocket(_serverFd);
|
||||
return std::make_pair(false, ss.str());
|
||||
@ -113,8 +108,7 @@ namespace ix
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "SocketServer::listen() error calling listen "
|
||||
<< "at address " << _host << ":" << _port
|
||||
<< " : " << strerror(Socket::getErrno());
|
||||
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno());
|
||||
|
||||
Socket::closeSocket(_serverFd);
|
||||
return std::make_pair(false, ss.str());
|
||||
@ -186,7 +180,7 @@ namespace ix
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_connectionsThreadsMutex);
|
||||
auto it = _connectionsThreads.begin();
|
||||
auto itEnd = _connectionsThreads.end();
|
||||
auto itEnd = _connectionsThreads.end();
|
||||
|
||||
while (it != itEnd)
|
||||
{
|
||||
@ -221,8 +215,7 @@ namespace ix
|
||||
if (pollResult == PollResultType::Error)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "SocketServer::run() error in select: "
|
||||
<< strerror(Socket::getErrno());
|
||||
ss << "SocketServer::run() error in select: " << strerror(Socket::getErrno());
|
||||
logError(ss.str());
|
||||
continue;
|
||||
}
|
||||
@ -238,15 +231,15 @@ namespace ix
|
||||
socklen_t addressLen = sizeof(client);
|
||||
memset(&client, 0, sizeof(client));
|
||||
|
||||
if ((clientFd = accept(_serverFd, (struct sockaddr *)&client, &addressLen)) < 0)
|
||||
if ((clientFd = accept(_serverFd, (struct sockaddr*) &client, &addressLen)) < 0)
|
||||
{
|
||||
if (!Socket::isWaitNeeded())
|
||||
{
|
||||
// FIXME: that error should be propagated
|
||||
int err = Socket::getErrno();
|
||||
std::stringstream ss;
|
||||
ss << "SocketServer::run() error accepting connection: "
|
||||
<< err << ", " << strerror(err);
|
||||
ss << "SocketServer::run() error accepting connection: " << err << ", "
|
||||
<< strerror(err);
|
||||
logError(ss.str());
|
||||
}
|
||||
continue;
|
||||
@ -255,8 +248,7 @@ namespace ix
|
||||
if (getConnectedClientsCount() >= _maxConnections)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "SocketServer::run() reached max connections = "
|
||||
<< _maxConnections << ". "
|
||||
ss << "SocketServer::run() reached max connections = " << _maxConnections << ". "
|
||||
<< "Not accepting connection";
|
||||
logError(ss.str());
|
||||
|
||||
@ -276,11 +268,8 @@ namespace ix
|
||||
// Launch the handleConnection work asynchronously in its own thread.
|
||||
std::lock_guard<std::mutex> lock(_connectionsThreadsMutex);
|
||||
_connectionsThreads.push_back(std::make_pair(
|
||||
connectionState,
|
||||
std::thread(&SocketServer::handleConnection,
|
||||
this,
|
||||
clientFd,
|
||||
connectionState)));
|
||||
connectionState,
|
||||
std::thread(&SocketServer::handleConnection, this, clientFd, connectionState)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -308,5 +297,4 @@ namespace ix
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ix
|
||||
|
@ -4,16 +4,17 @@
|
||||
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include "IXSocketTLSOptions.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
namespace ix
|
||||
{
|
||||
|
||||
SocketTLSOptions::SocketTLSOptions() {
|
||||
SocketTLSOptions::SocketTLSOptions()
|
||||
{
|
||||
#ifndef IXWEBSOCKET_USE_TLS
|
||||
assert(false && "To use TLS features the library must be compiled with USE_TLS");
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
bool SocketTLSOptions::isUsingClientCert() const
|
||||
|
@ -18,7 +18,7 @@ namespace ix
|
||||
std::string certFile;
|
||||
// the key used for signing/encryption
|
||||
std::string keyFile;
|
||||
// the ca certificate (or certificate bundle) file containing
|
||||
// the ca certificate (or certificate bundle) file containing
|
||||
// certificates to be trusted by peers; use 'SYSTEM' to
|
||||
// leverage the system defaults, use 'NONE' to disable peer verification
|
||||
std::string caFile = "SYSTEM";
|
||||
|
@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
#include "IXUrlParser.h"
|
||||
|
||||
#include "LUrlParser.h"
|
||||
|
||||
namespace ix
|
||||
@ -24,9 +25,9 @@ namespace ix
|
||||
}
|
||||
|
||||
protocol = res.m_Scheme;
|
||||
host = res.m_Host;
|
||||
path = res.m_Path;
|
||||
query = res.m_Query;
|
||||
host = res.m_Host;
|
||||
path = res.m_Path;
|
||||
query = res.m_Query;
|
||||
|
||||
if (!res.GetPort(&port))
|
||||
{
|
||||
@ -64,4 +65,4 @@ namespace ix
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -5,44 +5,44 @@
|
||||
*/
|
||||
|
||||
#include "IXUserAgent.h"
|
||||
#include "IXWebSocketVersion.h"
|
||||
|
||||
#include "IXWebSocketVersion.h"
|
||||
#include <sstream>
|
||||
#include <zlib.h>
|
||||
|
||||
// Platform name
|
||||
#if defined(_WIN32)
|
||||
#define PLATFORM_NAME "windows" // Windows
|
||||
#define PLATFORM_NAME "windows" // Windows
|
||||
#elif defined(_WIN64)
|
||||
#define PLATFORM_NAME "windows" // Windows
|
||||
#define PLATFORM_NAME "windows" // Windows
|
||||
#elif defined(__CYGWIN__) && !defined(_WIN32)
|
||||
#define PLATFORM_NAME "windows" // Windows (Cygwin POSIX under Microsoft Window)
|
||||
#define PLATFORM_NAME "windows" // Windows (Cygwin POSIX under Microsoft Window)
|
||||
#elif defined(__ANDROID__)
|
||||
#define PLATFORM_NAME "android" // Android (implies Linux, so it must come first)
|
||||
#define PLATFORM_NAME "android" // Android (implies Linux, so it must come first)
|
||||
#elif defined(__linux__)
|
||||
#define PLATFORM_NAME "linux" // Debian, Ubuntu, Gentoo, Fedora, openSUSE, RedHat, Centos and other
|
||||
#define PLATFORM_NAME "linux" // Debian, Ubuntu, Gentoo, Fedora, openSUSE, RedHat, Centos and other
|
||||
#elif defined(__unix__) || !defined(__APPLE__) && defined(__MACH__)
|
||||
#include <sys/param.h>
|
||||
#if defined(BSD)
|
||||
#define PLATFORM_NAME "bsd" // FreeBSD, NetBSD, OpenBSD, DragonFly BSD
|
||||
#endif
|
||||
#include <sys/param.h>
|
||||
#if defined(BSD)
|
||||
#define PLATFORM_NAME "bsd" // FreeBSD, NetBSD, OpenBSD, DragonFly BSD
|
||||
#endif
|
||||
#elif defined(__hpux)
|
||||
#define PLATFORM_NAME "hp-ux" // HP-UX
|
||||
#define PLATFORM_NAME "hp-ux" // HP-UX
|
||||
#elif defined(_AIX)
|
||||
#define PLATFORM_NAME "aix" // IBM AIX
|
||||
#define PLATFORM_NAME "aix" // IBM AIX
|
||||
#elif defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin)
|
||||
#include <TargetConditionals.h>
|
||||
#if TARGET_IPHONE_SIMULATOR == 1
|
||||
#define PLATFORM_NAME "ios" // Apple iOS
|
||||
#elif TARGET_OS_IPHONE == 1
|
||||
#define PLATFORM_NAME "ios" // Apple iOS
|
||||
#elif TARGET_OS_MAC == 1
|
||||
#define PLATFORM_NAME "macos" // Apple OSX
|
||||
#endif
|
||||
#include <TargetConditionals.h>
|
||||
#if TARGET_IPHONE_SIMULATOR == 1
|
||||
#define PLATFORM_NAME "ios" // Apple iOS
|
||||
#elif TARGET_OS_IPHONE == 1
|
||||
#define PLATFORM_NAME "ios" // Apple iOS
|
||||
#elif TARGET_OS_MAC == 1
|
||||
#define PLATFORM_NAME "macos" // Apple OSX
|
||||
#endif
|
||||
#elif defined(__sun) && defined(__SVR4)
|
||||
#define PLATFORM_NAME "solaris" // Oracle Solaris, Open Indiana
|
||||
#define PLATFORM_NAME "solaris" // Oracle Solaris, Open Indiana
|
||||
#else
|
||||
#define PLATFORM_NAME "unknown platform"
|
||||
#define PLATFORM_NAME "unknown platform"
|
||||
#endif
|
||||
|
||||
// SSL
|
||||
@ -80,4 +80,4 @@ namespace ix
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -24,7 +24,7 @@
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
*/
|
||||
|
||||
/*
|
||||
* IXUtf8Validator.h
|
||||
@ -48,20 +48,31 @@ namespace ix
|
||||
|
||||
/// Lookup table for the UTF8 decode state machine
|
||||
static uint8_t const utf8d[] = {
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 00..1f
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 20..3f
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 40..5f
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 60..7f
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, // 80..9f
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, // a0..bf
|
||||
8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // c0..df
|
||||
0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef
|
||||
0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff
|
||||
0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1, // s1..s2
|
||||
1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1, // s3..s4
|
||||
1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1, // s5..s6
|
||||
1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00..1f
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20..3f
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40..5f
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60..7f
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 80..9f
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // a0..bf
|
||||
8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // c0..df
|
||||
0xa, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // e0..ef
|
||||
0xb, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // f0..ff
|
||||
0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, // s1..s2
|
||||
1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // s3..s4
|
||||
1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, // s5..s6
|
||||
1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1,
|
||||
1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // s7..s8
|
||||
};
|
||||
|
||||
/// Decode the next byte of a UTF8 sequence
|
||||
@ -71,16 +82,14 @@ namespace ix
|
||||
* @param [in] byte The byte to input
|
||||
* @return The ending state of the decode operation
|
||||
*/
|
||||
inline uint32_t decodeNextByte(uint32_t * state, uint32_t * codep, uint8_t byte)
|
||||
inline uint32_t decodeNextByte(uint32_t* state, uint32_t* codep, uint8_t byte)
|
||||
{
|
||||
uint32_t type = utf8d[byte];
|
||||
uint32_t type = utf8d[byte];
|
||||
|
||||
*codep = (*state != utf8_accept) ?
|
||||
(byte & 0x3fu) | (*codep << 6) :
|
||||
(0xff >> type) & (byte);
|
||||
*codep = (*state != utf8_accept) ? (byte & 0x3fu) | (*codep << 6) : (0xff >> type) & (byte);
|
||||
|
||||
*state = utf8d[256 + *state*16 + type];
|
||||
return *state;
|
||||
*state = utf8d[256 + *state * 16 + type];
|
||||
return *state;
|
||||
}
|
||||
|
||||
/// Provides streaming UTF8 validation functionality
|
||||
@ -88,7 +97,11 @@ namespace ix
|
||||
{
|
||||
public:
|
||||
/// Construct and initialize the validator
|
||||
Utf8Validator() : m_state(utf8_accept),m_codepoint(0) {}
|
||||
Utf8Validator()
|
||||
: m_state(utf8_accept)
|
||||
, m_codepoint(0)
|
||||
{
|
||||
}
|
||||
|
||||
/// Advance the state of the validator with the next input byte
|
||||
/**
|
||||
@ -97,7 +110,7 @@ namespace ix
|
||||
*/
|
||||
bool consume(uint8_t byte)
|
||||
{
|
||||
if (decodeNextByte(&m_state,&m_codepoint,byte) == utf8_reject)
|
||||
if (decodeNextByte(&m_state, &m_codepoint, byte) == utf8_reject)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -110,16 +123,13 @@ namespace ix
|
||||
* @param end Input iterator to the end of the input range
|
||||
* @return Whether or not decoding the bytes resulted in a validation error.
|
||||
*/
|
||||
template <typename iterator_type>
|
||||
template<typename iterator_type>
|
||||
bool decode(iterator_type begin, iterator_type end)
|
||||
{
|
||||
for (iterator_type it = begin; it != end; ++it)
|
||||
{
|
||||
unsigned int result = decodeNextByte(
|
||||
&m_state,
|
||||
&m_codepoint,
|
||||
static_cast<uint8_t>(*it)
|
||||
);
|
||||
unsigned int result =
|
||||
decodeNextByte(&m_state, &m_codepoint, static_cast<uint8_t>(*it));
|
||||
|
||||
if (result == utf8_reject)
|
||||
{
|
||||
@ -144,9 +154,10 @@ namespace ix
|
||||
m_state = utf8_accept;
|
||||
m_codepoint = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
uint32_t m_state;
|
||||
uint32_t m_codepoint;
|
||||
uint32_t m_state;
|
||||
uint32_t m_codepoint;
|
||||
};
|
||||
|
||||
/// Validate a UTF8 string
|
||||
@ -154,10 +165,10 @@ namespace ix
|
||||
* convenience function that creates a Validator, validates a complete string
|
||||
* and returns the result.
|
||||
*/
|
||||
inline bool validateUtf8(std::string const & s)
|
||||
inline bool validateUtf8(std::string const& s)
|
||||
{
|
||||
Utf8Validator v;
|
||||
if (!v.decode(s.begin(),s.end()))
|
||||
if (!v.decode(s.begin(), s.end()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -5,13 +5,13 @@
|
||||
*/
|
||||
|
||||
#include "IXWebSocket.h"
|
||||
#include "IXSetThreadName.h"
|
||||
#include "IXWebSocketHandshake.h"
|
||||
#include "IXExponentialBackoff.h"
|
||||
#include "IXUtf8Validator.h"
|
||||
|
||||
#include <cmath>
|
||||
#include "IXExponentialBackoff.h"
|
||||
#include "IXSetThreadName.h"
|
||||
#include "IXUtf8Validator.h"
|
||||
#include "IXWebSocketHandshake.h"
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
|
||||
|
||||
namespace ix
|
||||
@ -23,26 +23,26 @@ namespace ix
|
||||
const bool WebSocket::kDefaultEnablePong(true);
|
||||
const uint32_t WebSocket::kDefaultMaxWaitBetweenReconnectionRetries(10 * 1000); // 10s
|
||||
|
||||
WebSocket::WebSocket() :
|
||||
_onMessageCallback(OnMessageCallback()),
|
||||
_stop(false),
|
||||
_automaticReconnection(true),
|
||||
_maxWaitBetweenReconnectionRetries(kDefaultMaxWaitBetweenReconnectionRetries),
|
||||
_handshakeTimeoutSecs(kDefaultHandShakeTimeoutSecs),
|
||||
_enablePong(kDefaultEnablePong),
|
||||
_pingIntervalSecs(kDefaultPingIntervalSecs),
|
||||
_pingTimeoutSecs(kDefaultPingTimeoutSecs)
|
||||
WebSocket::WebSocket()
|
||||
: _onMessageCallback(OnMessageCallback())
|
||||
, _stop(false)
|
||||
, _automaticReconnection(true)
|
||||
, _maxWaitBetweenReconnectionRetries(kDefaultMaxWaitBetweenReconnectionRetries)
|
||||
, _handshakeTimeoutSecs(kDefaultHandShakeTimeoutSecs)
|
||||
, _enablePong(kDefaultEnablePong)
|
||||
, _pingIntervalSecs(kDefaultPingIntervalSecs)
|
||||
, _pingTimeoutSecs(kDefaultPingTimeoutSecs)
|
||||
{
|
||||
_ws.setOnCloseCallback(
|
||||
[this](uint16_t code, const std::string& reason, size_t wireSize, bool remote)
|
||||
{
|
||||
[this](uint16_t code, const std::string& reason, size_t wireSize, bool remote) {
|
||||
_onMessageCallback(
|
||||
std::make_shared<WebSocketMessage>(
|
||||
WebSocketMessageType::Close, "", wireSize,
|
||||
WebSocketErrorInfo(), WebSocketOpenInfo(),
|
||||
WebSocketCloseInfo(code, reason, remote)));
|
||||
}
|
||||
);
|
||||
std::make_shared<WebSocketMessage>(WebSocketMessageType::Close,
|
||||
"",
|
||||
wireSize,
|
||||
WebSocketErrorInfo(),
|
||||
WebSocketOpenInfo(),
|
||||
WebSocketCloseInfo(code, reason, remote)));
|
||||
});
|
||||
}
|
||||
|
||||
WebSocket::~WebSocket()
|
||||
@ -67,7 +67,8 @@ namespace ix
|
||||
return _url;
|
||||
}
|
||||
|
||||
void WebSocket::setPerMessageDeflateOptions(const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions)
|
||||
void WebSocket::setPerMessageDeflateOptions(
|
||||
const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_configMutex);
|
||||
_perMessageDeflateOptions = perMessageDeflateOptions;
|
||||
@ -159,8 +160,7 @@ namespace ix
|
||||
_thread = std::thread(&WebSocket::run, this);
|
||||
}
|
||||
|
||||
void WebSocket::stop(uint16_t code,
|
||||
const std::string& reason)
|
||||
void WebSocket::stop(uint16_t code, const std::string& reason)
|
||||
{
|
||||
close(code, reason);
|
||||
|
||||
@ -192,11 +192,12 @@ namespace ix
|
||||
}
|
||||
|
||||
_onMessageCallback(
|
||||
std::make_shared<WebSocketMessage>(
|
||||
WebSocketMessageType::Open, "", 0,
|
||||
WebSocketErrorInfo(),
|
||||
WebSocketOpenInfo(status.uri, status.headers),
|
||||
WebSocketCloseInfo()));
|
||||
std::make_shared<WebSocketMessage>(WebSocketMessageType::Open,
|
||||
"",
|
||||
0,
|
||||
WebSocketErrorInfo(),
|
||||
WebSocketOpenInfo(status.uri, status.headers),
|
||||
WebSocketCloseInfo()));
|
||||
return status;
|
||||
}
|
||||
|
||||
@ -218,11 +219,12 @@ namespace ix
|
||||
}
|
||||
|
||||
_onMessageCallback(
|
||||
std::make_shared<WebSocketMessage>(
|
||||
WebSocketMessageType::Open, "", 0,
|
||||
WebSocketErrorInfo(),
|
||||
WebSocketOpenInfo(status.uri, status.headers),
|
||||
WebSocketCloseInfo()));
|
||||
std::make_shared<WebSocketMessage>(WebSocketMessageType::Open,
|
||||
"",
|
||||
0,
|
||||
WebSocketErrorInfo(),
|
||||
WebSocketOpenInfo(status.uri, status.headers),
|
||||
WebSocketCloseInfo()));
|
||||
return status;
|
||||
}
|
||||
|
||||
@ -236,8 +238,7 @@ namespace ix
|
||||
return getReadyState() == ReadyState::Closing;
|
||||
}
|
||||
|
||||
void WebSocket::close(uint16_t code,
|
||||
const std::string& reason)
|
||||
void WebSocket::close(uint16_t code, const std::string& reason)
|
||||
{
|
||||
_ws.close(code, reason);
|
||||
}
|
||||
@ -281,20 +282,22 @@ namespace ix
|
||||
|
||||
if (_automaticReconnection)
|
||||
{
|
||||
duration = millis(calculateRetryWaitMilliseconds(retries++, _maxWaitBetweenReconnectionRetries));
|
||||
duration = millis(calculateRetryWaitMilliseconds(
|
||||
retries++, _maxWaitBetweenReconnectionRetries));
|
||||
|
||||
connectErr.wait_time = duration.count();
|
||||
connectErr.retries = retries;
|
||||
}
|
||||
|
||||
connectErr.reason = status.errorStr;
|
||||
connectErr.reason = status.errorStr;
|
||||
connectErr.http_status = status.http_status;
|
||||
|
||||
_onMessageCallback(
|
||||
std::make_shared<WebSocketMessage>(
|
||||
WebSocketMessageType::Error, "", 0,
|
||||
connectErr, WebSocketOpenInfo(),
|
||||
WebSocketCloseInfo()));
|
||||
_onMessageCallback(std::make_shared<WebSocketMessage>(WebSocketMessageType::Error,
|
||||
"",
|
||||
0,
|
||||
connectErr,
|
||||
WebSocketOpenInfo(),
|
||||
WebSocketCloseInfo()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -330,8 +333,7 @@ namespace ix
|
||||
[this](const std::string& msg,
|
||||
size_t wireSize,
|
||||
bool decompressionError,
|
||||
WebSocketTransport::MessageKind messageKind)
|
||||
{
|
||||
WebSocketTransport::MessageKind messageKind) {
|
||||
WebSocketMessageType webSocketMessageType;
|
||||
switch (messageKind)
|
||||
{
|
||||
@ -339,22 +341,26 @@ namespace ix
|
||||
case WebSocketTransport::MessageKind::MSG_BINARY:
|
||||
{
|
||||
webSocketMessageType = WebSocketMessageType::Message;
|
||||
} break;
|
||||
}
|
||||
break;
|
||||
|
||||
case WebSocketTransport::MessageKind::PING:
|
||||
{
|
||||
webSocketMessageType = WebSocketMessageType::Ping;
|
||||
} break;
|
||||
}
|
||||
break;
|
||||
|
||||
case WebSocketTransport::MessageKind::PONG:
|
||||
{
|
||||
webSocketMessageType = WebSocketMessageType::Pong;
|
||||
} break;
|
||||
}
|
||||
break;
|
||||
|
||||
case WebSocketTransport::MessageKind::FRAGMENT:
|
||||
{
|
||||
webSocketMessageType = WebSocketMessageType::Fragment;
|
||||
} break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
WebSocketErrorInfo webSocketErrorInfo;
|
||||
@ -362,11 +368,13 @@ namespace ix
|
||||
|
||||
bool binary = messageKind == WebSocketTransport::MessageKind::MSG_BINARY;
|
||||
|
||||
_onMessageCallback(
|
||||
std::make_shared<WebSocketMessage>(
|
||||
webSocketMessageType, msg, wireSize,
|
||||
webSocketErrorInfo, WebSocketOpenInfo(),
|
||||
WebSocketCloseInfo(), binary));
|
||||
_onMessageCallback(std::make_shared<WebSocketMessage>(webSocketMessageType,
|
||||
msg,
|
||||
wireSize,
|
||||
webSocketErrorInfo,
|
||||
WebSocketOpenInfo(),
|
||||
WebSocketCloseInfo(),
|
||||
binary));
|
||||
|
||||
WebSocket::invokeTrafficTrackerCallback(msg.size(), true);
|
||||
});
|
||||
@ -453,17 +461,20 @@ namespace ix
|
||||
case SendMessageKind::Text:
|
||||
{
|
||||
webSocketSendInfo = _ws.sendText(text, onProgressCallback);
|
||||
} break;
|
||||
}
|
||||
break;
|
||||
|
||||
case SendMessageKind::Binary:
|
||||
{
|
||||
webSocketSendInfo = _ws.sendBinary(text, onProgressCallback);
|
||||
} break;
|
||||
}
|
||||
break;
|
||||
|
||||
case SendMessageKind::Ping:
|
||||
{
|
||||
webSocketSendInfo = _ws.sendPing(text);
|
||||
} break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
WebSocket::invokeTrafficTrackerCallback(webSocketSendInfo.wireSize, false);
|
||||
@ -475,10 +486,10 @@ namespace ix
|
||||
{
|
||||
switch (_ws.getReadyState())
|
||||
{
|
||||
case ix::WebSocketTransport::ReadyState::OPEN : return ReadyState::Open;
|
||||
case ix::WebSocketTransport::ReadyState::OPEN: return ReadyState::Open;
|
||||
case ix::WebSocketTransport::ReadyState::CONNECTING: return ReadyState::Connecting;
|
||||
case ix::WebSocketTransport::ReadyState::CLOSING : return ReadyState::Closing;
|
||||
case ix::WebSocketTransport::ReadyState::CLOSED : return ReadyState::Closed;
|
||||
case ix::WebSocketTransport::ReadyState::CLOSING: return ReadyState::Closing;
|
||||
case ix::WebSocketTransport::ReadyState::CLOSED: return ReadyState::Closed;
|
||||
default: return ReadyState::Closed;
|
||||
}
|
||||
}
|
||||
@ -487,10 +498,10 @@ namespace ix
|
||||
{
|
||||
switch (readyState)
|
||||
{
|
||||
case ReadyState::Open : return "OPEN";
|
||||
case ReadyState::Open: return "OPEN";
|
||||
case ReadyState::Connecting: return "CONNECTING";
|
||||
case ReadyState::Closing : return "CLOSING";
|
||||
case ReadyState::Closed : return "CLOSED";
|
||||
case ReadyState::Closing: return "CLOSING";
|
||||
case ReadyState::Closed: return "CLOSED";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
@ -514,4 +525,4 @@ namespace ix
|
||||
{
|
||||
return _ws.bufferedAmount();
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -22,10 +22,15 @@ namespace ix
|
||||
const std::string WebSocketCloseConstants::kProtocolErrorMessage("Protocol error");
|
||||
const std::string WebSocketCloseConstants::kNoStatusCodeErrorMessage("No status code");
|
||||
const std::string WebSocketCloseConstants::kProtocolErrorReservedBitUsed("Reserved bit used");
|
||||
const std::string WebSocketCloseConstants::kProtocolErrorPingPayloadOversized("Ping reason control frame with payload length > 125 octets");
|
||||
const std::string WebSocketCloseConstants::kProtocolErrorCodeControlMessageFragmented("Control message fragmented");
|
||||
const std::string WebSocketCloseConstants::kProtocolErrorCodeDataOpcodeOutOfSequence("Fragmentation: data message out of sequence");
|
||||
const std::string WebSocketCloseConstants::kProtocolErrorCodeContinuationOpCodeOutOfSequence("Fragmentation: continuation opcode out of sequence");
|
||||
const std::string WebSocketCloseConstants::kInvalidFramePayloadDataMessage("Invalid frame payload data");
|
||||
const std::string WebSocketCloseConstants::kProtocolErrorPingPayloadOversized(
|
||||
"Ping reason control frame with payload length > 125 octets");
|
||||
const std::string WebSocketCloseConstants::kProtocolErrorCodeControlMessageFragmented(
|
||||
"Control message fragmented");
|
||||
const std::string WebSocketCloseConstants::kProtocolErrorCodeDataOpcodeOutOfSequence(
|
||||
"Fragmentation: data message out of sequence");
|
||||
const std::string WebSocketCloseConstants::kProtocolErrorCodeContinuationOpCodeOutOfSequence(
|
||||
"Fragmentation: continuation opcode out of sequence");
|
||||
const std::string WebSocketCloseConstants::kInvalidFramePayloadDataMessage(
|
||||
"Invalid frame payload data");
|
||||
const std::string WebSocketCloseConstants::kInvalidCloseCodeMessage("Invalid close code");
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -5,50 +5,45 @@
|
||||
*/
|
||||
|
||||
#include "IXWebSocketHandshake.h"
|
||||
|
||||
#include "IXHttp.h"
|
||||
#include "IXSocketConnect.h"
|
||||
#include "IXUrlParser.h"
|
||||
#include "IXHttp.h"
|
||||
#include "IXUserAgent.h"
|
||||
|
||||
#include "libwshandshake.hpp"
|
||||
|
||||
#include <sstream>
|
||||
#include <random>
|
||||
#include <algorithm>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
namespace ix
|
||||
{
|
||||
WebSocketHandshake::WebSocketHandshake(std::atomic<bool>& requestInitCancellation,
|
||||
std::shared_ptr<Socket> socket,
|
||||
WebSocketPerMessageDeflate& perMessageDeflate,
|
||||
WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
|
||||
std::atomic<bool>& enablePerMessageDeflate) :
|
||||
_requestInitCancellation(requestInitCancellation),
|
||||
_socket(socket),
|
||||
_perMessageDeflate(perMessageDeflate),
|
||||
_perMessageDeflateOptions(perMessageDeflateOptions),
|
||||
_enablePerMessageDeflate(enablePerMessageDeflate)
|
||||
WebSocketHandshake::WebSocketHandshake(
|
||||
std::atomic<bool>& requestInitCancellation,
|
||||
std::shared_ptr<Socket> socket,
|
||||
WebSocketPerMessageDeflate& perMessageDeflate,
|
||||
WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
|
||||
std::atomic<bool>& enablePerMessageDeflate)
|
||||
: _requestInitCancellation(requestInitCancellation)
|
||||
, _socket(socket)
|
||||
, _perMessageDeflate(perMessageDeflate)
|
||||
, _perMessageDeflateOptions(perMessageDeflateOptions)
|
||||
, _enablePerMessageDeflate(enablePerMessageDeflate)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool WebSocketHandshake::insensitiveStringCompare(const std::string& a, const std::string& b)
|
||||
{
|
||||
return std::equal(a.begin(), a.end(),
|
||||
b.begin(), b.end(),
|
||||
[](char a, char b)
|
||||
{
|
||||
return tolower(a) == tolower(b);
|
||||
});
|
||||
return std::equal(a.begin(), a.end(), b.begin(), b.end(), [](char a, char b) {
|
||||
return tolower(a) == tolower(b);
|
||||
});
|
||||
}
|
||||
|
||||
std::string WebSocketHandshake::genRandomString(const int len)
|
||||
{
|
||||
std::string alphanum =
|
||||
"0123456789"
|
||||
"ABCDEFGH"
|
||||
"abcdefgh";
|
||||
std::string alphanum = "0123456789"
|
||||
"ABCDEFGH"
|
||||
"abcdefgh";
|
||||
|
||||
std::random_device r;
|
||||
std::default_random_engine e1(r());
|
||||
@ -88,12 +83,13 @@ namespace ix
|
||||
return WebSocketInitResult(false, code, reason);
|
||||
}
|
||||
|
||||
WebSocketInitResult WebSocketHandshake::clientHandshake(const std::string& url,
|
||||
const WebSocketHttpHeaders& extraHeaders,
|
||||
const std::string& host,
|
||||
const std::string& path,
|
||||
int port,
|
||||
int timeoutSecs)
|
||||
WebSocketInitResult WebSocketHandshake::clientHandshake(
|
||||
const std::string& url,
|
||||
const WebSocketHttpHeaders& extraHeaders,
|
||||
const std::string& host,
|
||||
const std::string& path,
|
||||
int port,
|
||||
int timeoutSecs)
|
||||
{
|
||||
_requestInitCancellation = false;
|
||||
|
||||
@ -105,9 +101,7 @@ namespace ix
|
||||
if (!success)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Unable to connect to " << host
|
||||
<< " on port " << port
|
||||
<< ", error: " << errMsg;
|
||||
ss << "Unable to connect to " << host << " on port " << port << ", error: " << errMsg;
|
||||
return WebSocketInitResult(false, 0, ss.str());
|
||||
}
|
||||
|
||||
@ -123,7 +117,7 @@ namespace ix
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "GET " << path << " HTTP/1.1\r\n";
|
||||
ss << "Host: "<< host << ":" << port << "\r\n";
|
||||
ss << "Host: " << host << ":" << port << "\r\n";
|
||||
ss << "Upgrade: websocket\r\n";
|
||||
ss << "Connection: Upgrade\r\n";
|
||||
ss << "Sec-WebSocket-Version: 13\r\n";
|
||||
@ -149,7 +143,8 @@ namespace ix
|
||||
|
||||
if (!_socket->writeBytes(ss.str(), isCancellationRequested))
|
||||
{
|
||||
return WebSocketInitResult(false, 0, std::string("Failed sending GET request to ") + url);
|
||||
return WebSocketInitResult(
|
||||
false, 0, std::string("Failed sending GET request to ") + url);
|
||||
}
|
||||
|
||||
// Read HTTP status line
|
||||
@ -159,8 +154,8 @@ namespace ix
|
||||
|
||||
if (!lineValid)
|
||||
{
|
||||
return WebSocketInitResult(false, 0,
|
||||
std::string("Failed reading HTTP status line from ") + url);
|
||||
return WebSocketInitResult(
|
||||
false, 0, std::string("Failed reading HTTP status line from ") + url);
|
||||
}
|
||||
|
||||
// Validate status
|
||||
@ -173,8 +168,7 @@ namespace ix
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Expecting HTTP/1.1, got " << httpVersion << ". "
|
||||
<< "Rejecting connection to " << host << ":" << port
|
||||
<< ", status: " << status
|
||||
<< "Rejecting connection to " << host << ":" << port << ", status: " << status
|
||||
<< ", HTTP Status line: " << line;
|
||||
return WebSocketInitResult(false, status, ss.str());
|
||||
}
|
||||
@ -183,8 +177,7 @@ namespace ix
|
||||
if (status != 101)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Got bad status connecting to " << host << ":" << port
|
||||
<< ", status: " << status
|
||||
ss << "Got bad status connecting to " << host << ":" << port << ", status: " << status
|
||||
<< ", HTTP Status line: " << line;
|
||||
return WebSocketInitResult(false, status, ss.str());
|
||||
}
|
||||
@ -269,8 +262,8 @@ namespace ix
|
||||
|
||||
// Validate request line (GET /foo HTTP/1.1\r\n)
|
||||
auto requestLine = Http::parseRequestLine(line);
|
||||
auto method = std::get<0>(requestLine);
|
||||
auto uri = std::get<1>(requestLine);
|
||||
auto method = std::get<0>(requestLine);
|
||||
auto uri = std::get<1>(requestLine);
|
||||
auto httpVersion = std::get<2>(requestLine);
|
||||
|
||||
if (method != "GET")
|
||||
@ -280,7 +273,8 @@ namespace ix
|
||||
|
||||
if (httpVersion != "HTTP/1.1")
|
||||
{
|
||||
return sendErrorResponse(400, "Invalid HTTP version, need HTTP/1.1, got: " + httpVersion);
|
||||
return sendErrorResponse(400,
|
||||
"Invalid HTTP version, need HTTP/1.1, got: " + httpVersion);
|
||||
}
|
||||
|
||||
// Retrieve and validate HTTP headers
|
||||
@ -305,8 +299,10 @@ namespace ix
|
||||
|
||||
if (!insensitiveStringCompare(headers["upgrade"], "WebSocket"))
|
||||
{
|
||||
return sendErrorResponse(400, "Invalid Upgrade header, "
|
||||
"need WebSocket, got " + headers["upgrade"]);
|
||||
return sendErrorResponse(400,
|
||||
"Invalid Upgrade header, "
|
||||
"need WebSocket, got " +
|
||||
headers["upgrade"]);
|
||||
}
|
||||
|
||||
if (headers.find("sec-websocket-version") == headers.end())
|
||||
@ -322,8 +318,10 @@ namespace ix
|
||||
|
||||
if (version != 13)
|
||||
{
|
||||
return sendErrorResponse(400, "Invalid Sec-WebSocket-Version, "
|
||||
"need 13, got " + ss.str());
|
||||
return sendErrorResponse(400,
|
||||
"Invalid Sec-WebSocket-Version, "
|
||||
"need 13, got " +
|
||||
ss.str());
|
||||
}
|
||||
}
|
||||
|
||||
@ -349,7 +347,7 @@ namespace ix
|
||||
if (!_perMessageDeflate.init(webSocketPerMessageDeflateOptions))
|
||||
{
|
||||
return WebSocketInitResult(
|
||||
false, 0,"Failed to initialize per message deflate engine");
|
||||
false, 0, "Failed to initialize per message deflate engine");
|
||||
}
|
||||
ss << webSocketPerMessageDeflateOptions.generateHeader();
|
||||
}
|
||||
@ -358,9 +356,10 @@ namespace ix
|
||||
|
||||
if (!_socket->writeBytes(ss.str(), isCancellationRequested))
|
||||
{
|
||||
return WebSocketInitResult(false, 0, std::string("Failed sending response to ") + remote);
|
||||
return WebSocketInitResult(
|
||||
false, 0, std::string("Failed sending response to ") + remote);
|
||||
}
|
||||
|
||||
return WebSocketInitResult(true, 200, "", headers, uri);
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -49,13 +49,12 @@ namespace ix
|
||||
WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
|
||||
std::atomic<bool>& enablePerMessageDeflate);
|
||||
|
||||
WebSocketInitResult clientHandshake(
|
||||
const std::string& url,
|
||||
const WebSocketHttpHeaders& extraHeaders,
|
||||
const std::string& host,
|
||||
const std::string& path,
|
||||
int port,
|
||||
int timeoutSecs);
|
||||
WebSocketInitResult clientHandshake(const std::string& url,
|
||||
const WebSocketHttpHeaders& extraHeaders,
|
||||
const std::string& host,
|
||||
const std::string& path,
|
||||
int port,
|
||||
int timeoutSecs);
|
||||
|
||||
WebSocketInitResult serverHandshake(int fd, int timeoutSecs);
|
||||
|
||||
|
@ -5,13 +5,15 @@
|
||||
*/
|
||||
|
||||
#include "IXWebSocketHttpHeaders.h"
|
||||
|
||||
#include "IXSocket.h"
|
||||
#include <algorithm>
|
||||
#include <locale>
|
||||
|
||||
namespace ix
|
||||
{
|
||||
bool CaseInsensitiveLess::NocaseCompare::operator()(const unsigned char & c1, const unsigned char & c2) const
|
||||
bool CaseInsensitiveLess::NocaseCompare::operator()(const unsigned char& c1,
|
||||
const unsigned char& c2) const
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return std::tolower(c1, std::locale()) < std::tolower(c2, std::locale());
|
||||
@ -20,17 +22,17 @@ namespace ix
|
||||
#endif
|
||||
}
|
||||
|
||||
bool CaseInsensitiveLess::operator()(const std::string & s1, const std::string & s2) const
|
||||
bool CaseInsensitiveLess::operator()(const std::string& s1, const std::string& s2) const
|
||||
{
|
||||
return std::lexicographical_compare
|
||||
(s1.begin(), s1.end(), // source range
|
||||
s2.begin(), s2.end(), // dest range
|
||||
NocaseCompare()); // comparison
|
||||
return std::lexicographical_compare(s1.begin(),
|
||||
s1.end(), // source range
|
||||
s2.begin(),
|
||||
s2.end(), // dest range
|
||||
NocaseCompare()); // comparison
|
||||
}
|
||||
|
||||
std::pair<bool, WebSocketHttpHeaders> parseHttpHeaders(
|
||||
std::shared_ptr<Socket> socket,
|
||||
const CancellationRequest& isCancellationRequested)
|
||||
std::shared_ptr<Socket> socket, const CancellationRequest& isCancellationRequested)
|
||||
{
|
||||
WebSocketHttpHeaders headers;
|
||||
|
||||
@ -41,11 +43,9 @@ namespace ix
|
||||
{
|
||||
int colon = 0;
|
||||
|
||||
for (i = 0;
|
||||
i < 2 || (i < 1023 && line[i-2] != '\r' && line[i-1] != '\n');
|
||||
++i)
|
||||
for (i = 0; i < 2 || (i < 1023 && line[i - 2] != '\r' && line[i - 1] != '\n'); ++i)
|
||||
{
|
||||
if (!socket->readByte(line+i, isCancellationRequested))
|
||||
if (!socket->readByte(line + i, isCancellationRequested))
|
||||
{
|
||||
return std::make_pair(false, headers);
|
||||
}
|
||||
@ -79,4 +79,4 @@ namespace ix
|
||||
|
||||
return std::make_pair(true, headers);
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -8,7 +8,6 @@
|
||||
|
||||
namespace ix
|
||||
{
|
||||
|
||||
WebSocketMessageQueue::WebSocketMessageQueue(WebSocket* websocket)
|
||||
{
|
||||
bindWebsocket(websocket);
|
||||
@ -24,7 +23,7 @@ namespace ix
|
||||
bindWebsocket(nullptr);
|
||||
}
|
||||
|
||||
void WebSocketMessageQueue::bindWebsocket(WebSocket * websocket)
|
||||
void WebSocketMessageQueue::bindWebsocket(WebSocket* websocket)
|
||||
{
|
||||
if (_websocket == websocket) return;
|
||||
|
||||
@ -40,8 +39,7 @@ namespace ix
|
||||
// bind new
|
||||
if (_websocket)
|
||||
{
|
||||
_websocket->setOnMessageCallback([this](const WebSocketMessagePtr& msg)
|
||||
{
|
||||
_websocket->setOnMessageCallback([this](const WebSocketMessagePtr& msg) {
|
||||
std::lock_guard<std::mutex> lock(_messagesMutex);
|
||||
_messages.emplace_back(std::move(msg));
|
||||
});
|
||||
@ -74,8 +72,7 @@ namespace ix
|
||||
|
||||
void WebSocketMessageQueue::poll(int count)
|
||||
{
|
||||
if (!_onMessageUserCallback)
|
||||
return;
|
||||
if (!_onMessageUserCallback) return;
|
||||
|
||||
WebSocketMessagePtr message;
|
||||
|
||||
@ -86,4 +83,4 @@ namespace ix
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -41,19 +41,21 @@
|
||||
* - Added more documentation.
|
||||
*
|
||||
* Per message Deflate RFC: https://tools.ietf.org/html/rfc7692
|
||||
* Chrome websocket -> https://github.com/chromium/chromium/tree/2ca8c5037021c9d2ecc00b787d58a31ed8fc8bcb/net/websockets
|
||||
* Chrome websocket ->
|
||||
* https://github.com/chromium/chromium/tree/2ca8c5037021c9d2ecc00b787d58a31ed8fc8bcb/net/websockets
|
||||
*
|
||||
*/
|
||||
|
||||
#include "IXWebSocketPerMessageDeflate.h"
|
||||
#include "IXWebSocketPerMessageDeflateOptions.h"
|
||||
|
||||
#include "IXWebSocketPerMessageDeflateCodec.h"
|
||||
#include "IXWebSocketPerMessageDeflateOptions.h"
|
||||
|
||||
namespace ix
|
||||
{
|
||||
WebSocketPerMessageDeflate::WebSocketPerMessageDeflate() :
|
||||
_compressor(std::make_unique<WebSocketPerMessageDeflateCompressor>()),
|
||||
_decompressor(std::make_unique<WebSocketPerMessageDeflateDecompressor>())
|
||||
WebSocketPerMessageDeflate::WebSocketPerMessageDeflate()
|
||||
: _compressor(std::make_unique<WebSocketPerMessageDeflateCompressor>())
|
||||
, _decompressor(std::make_unique<WebSocketPerMessageDeflateDecompressor>())
|
||||
{
|
||||
;
|
||||
}
|
||||
@ -63,10 +65,10 @@ namespace ix
|
||||
;
|
||||
}
|
||||
|
||||
bool WebSocketPerMessageDeflate::init(const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions)
|
||||
bool WebSocketPerMessageDeflate::init(
|
||||
const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions)
|
||||
{
|
||||
bool clientNoContextTakeover =
|
||||
perMessageDeflateOptions.getClientNoContextTakeover();
|
||||
bool clientNoContextTakeover = perMessageDeflateOptions.getClientNoContextTakeover();
|
||||
|
||||
uint8_t deflateBits = perMessageDeflateOptions.getClientMaxWindowBits();
|
||||
uint8_t inflateBits = perMessageDeflateOptions.getServerMaxWindowBits();
|
||||
@ -75,16 +77,14 @@ namespace ix
|
||||
_decompressor->init(inflateBits, clientNoContextTakeover);
|
||||
}
|
||||
|
||||
bool WebSocketPerMessageDeflate::compress(const std::string& in,
|
||||
std::string& out)
|
||||
bool WebSocketPerMessageDeflate::compress(const std::string& in, std::string& out)
|
||||
{
|
||||
return _compressor->compress(in, out);
|
||||
}
|
||||
|
||||
bool WebSocketPerMessageDeflate::decompress(const std::string& in,
|
||||
std::string &out)
|
||||
bool WebSocketPerMessageDeflate::decompress(const std::string& in, std::string& out)
|
||||
{
|
||||
return _decompressor->decompress(in, out);
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -5,8 +5,8 @@
|
||||
*/
|
||||
|
||||
#include "IXWebSocketPerMessageDeflateCodec.h"
|
||||
#include "IXWebSocketPerMessageDeflateOptions.h"
|
||||
|
||||
#include "IXWebSocketPerMessageDeflateOptions.h"
|
||||
#include <cassert>
|
||||
#include <string.h>
|
||||
|
||||
@ -18,7 +18,7 @@ namespace
|
||||
const std::string kEmptyUncompressedBlock = std::string("\x00\x00\xff\xff", 4);
|
||||
|
||||
const int kBufferSize = 1 << 14;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace ix
|
||||
{
|
||||
@ -26,7 +26,7 @@ namespace ix
|
||||
// Compressor
|
||||
//
|
||||
WebSocketPerMessageDeflateCompressor::WebSocketPerMessageDeflateCompressor()
|
||||
: _compressBufferSize(kBufferSize)
|
||||
: _compressBufferSize(kBufferSize)
|
||||
{
|
||||
memset(&_deflateState, 0, sizeof(_deflateState));
|
||||
|
||||
@ -43,22 +43,18 @@ namespace ix
|
||||
bool WebSocketPerMessageDeflateCompressor::init(uint8_t deflateBits,
|
||||
bool clientNoContextTakeOver)
|
||||
{
|
||||
int ret = deflateInit2(
|
||||
&_deflateState,
|
||||
Z_DEFAULT_COMPRESSION,
|
||||
Z_DEFLATED,
|
||||
-1*deflateBits,
|
||||
4, // memory level 1-9
|
||||
Z_DEFAULT_STRATEGY
|
||||
);
|
||||
int ret = deflateInit2(&_deflateState,
|
||||
Z_DEFAULT_COMPRESSION,
|
||||
Z_DEFLATED,
|
||||
-1 * deflateBits,
|
||||
4, // memory level 1-9
|
||||
Z_DEFAULT_STRATEGY);
|
||||
|
||||
if (ret != Z_OK) return false;
|
||||
|
||||
_compressBuffer = std::make_unique<unsigned char[]>(_compressBufferSize);
|
||||
|
||||
_flush = (clientNoContextTakeOver)
|
||||
? Z_FULL_FLUSH
|
||||
: Z_SYNC_FLUSH;
|
||||
_flush = (clientNoContextTakeOver) ? Z_FULL_FLUSH : Z_SYNC_FLUSH;
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -70,8 +66,7 @@ namespace ix
|
||||
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
|
||||
}
|
||||
|
||||
bool WebSocketPerMessageDeflateCompressor::compress(const std::string& in,
|
||||
std::string& out)
|
||||
bool WebSocketPerMessageDeflateCompressor::compress(const std::string& in, std::string& out)
|
||||
{
|
||||
//
|
||||
// 7.2.1. Compression
|
||||
@ -95,7 +90,7 @@ namespace ix
|
||||
if (in.empty())
|
||||
{
|
||||
uint8_t buf[6] = {0x02, 0x00, 0x00, 0x00, 0xff, 0xff};
|
||||
out.append((char *)(buf), 6);
|
||||
out.append((char*) (buf), 6);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -112,7 +107,7 @@ namespace ix
|
||||
|
||||
output = _compressBufferSize - _deflateState.avail_out;
|
||||
|
||||
out.append((char *)(_compressBuffer.get()),output);
|
||||
out.append((char*) (_compressBuffer.get()), output);
|
||||
} while (_deflateState.avail_out == 0);
|
||||
|
||||
if (endsWith(out, kEmptyUncompressedBlock))
|
||||
@ -127,7 +122,7 @@ namespace ix
|
||||
// Decompressor
|
||||
//
|
||||
WebSocketPerMessageDeflateDecompressor::WebSocketPerMessageDeflateDecompressor()
|
||||
: _compressBufferSize(kBufferSize)
|
||||
: _compressBufferSize(kBufferSize)
|
||||
{
|
||||
memset(&_inflateState, 0, sizeof(_inflateState));
|
||||
|
||||
@ -146,24 +141,18 @@ namespace ix
|
||||
bool WebSocketPerMessageDeflateDecompressor::init(uint8_t inflateBits,
|
||||
bool clientNoContextTakeOver)
|
||||
{
|
||||
int ret = inflateInit2(
|
||||
&_inflateState,
|
||||
-1*inflateBits
|
||||
);
|
||||
int ret = inflateInit2(&_inflateState, -1 * inflateBits);
|
||||
|
||||
if (ret != Z_OK) return false;
|
||||
|
||||
_compressBuffer = std::make_unique<unsigned char[]>(_compressBufferSize);
|
||||
|
||||
_flush = (clientNoContextTakeOver)
|
||||
? Z_FULL_FLUSH
|
||||
: Z_SYNC_FLUSH;
|
||||
_flush = (clientNoContextTakeOver) ? Z_FULL_FLUSH : Z_SYNC_FLUSH;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WebSocketPerMessageDeflateDecompressor::decompress(const std::string& in,
|
||||
std::string& out)
|
||||
bool WebSocketPerMessageDeflateDecompressor::decompress(const std::string& in, std::string& out)
|
||||
{
|
||||
//
|
||||
// 7.2.2. Decompression
|
||||
@ -179,7 +168,7 @@ namespace ix
|
||||
inFixed += kEmptyUncompressedBlock;
|
||||
|
||||
_inflateState.avail_in = (uInt) inFixed.size();
|
||||
_inflateState.next_in = (unsigned char *)(const_cast<char *>(inFixed.data()));
|
||||
_inflateState.next_in = (unsigned char*) (const_cast<char*>(inFixed.data()));
|
||||
|
||||
do
|
||||
{
|
||||
@ -193,13 +182,10 @@ namespace ix
|
||||
return false; // zlib error
|
||||
}
|
||||
|
||||
out.append(
|
||||
reinterpret_cast<char *>(_compressBuffer.get()),
|
||||
_compressBufferSize - _inflateState.avail_out
|
||||
);
|
||||
out.append(reinterpret_cast<char*>(_compressBuffer.get()),
|
||||
_compressBufferSize - _inflateState.avail_out);
|
||||
} while (_inflateState.avail_out == 0);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ix
|
||||
|
@ -6,9 +6,9 @@
|
||||
|
||||
#include "IXWebSocketPerMessageDeflateOptions.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
|
||||
namespace ix
|
||||
{
|
||||
@ -48,7 +48,8 @@ namespace ix
|
||||
//
|
||||
// Server response could look like that:
|
||||
//
|
||||
// Sec-WebSocket-Extensions: permessage-deflate; client_no_context_takeover; server_no_context_takeover
|
||||
// Sec-WebSocket-Extensions: permessage-deflate; client_no_context_takeover;
|
||||
// server_no_context_takeover
|
||||
//
|
||||
WebSocketPerMessageDeflateOptions::WebSocketPerMessageDeflateOptions(std::string extension)
|
||||
{
|
||||
@ -92,8 +93,7 @@ namespace ix
|
||||
// Sanitize values to be in the proper range [8, 15] in
|
||||
// case a server would give us bogus values
|
||||
_serverMaxWindowBits =
|
||||
std::min(maxServerMaxWindowBits,
|
||||
std::max(x, minServerMaxWindowBits));
|
||||
std::min(maxServerMaxWindowBits, std::max(x, minServerMaxWindowBits));
|
||||
}
|
||||
|
||||
if (startsWith(token, "client_max_window_bits="))
|
||||
@ -107,8 +107,7 @@ namespace ix
|
||||
// Sanitize values to be in the proper range [8, 15] in
|
||||
// case a server would give us bogus values
|
||||
_clientMaxWindowBits =
|
||||
std::min(maxClientMaxWindowBits,
|
||||
std::max(x, minClientMaxWindowBits));
|
||||
std::min(maxClientMaxWindowBits, std::max(x, minClientMaxWindowBits));
|
||||
|
||||
sanitizeClientMaxWindowBits();
|
||||
}
|
||||
@ -175,11 +174,10 @@ namespace ix
|
||||
std::string WebSocketPerMessageDeflateOptions::removeSpaces(const std::string& str)
|
||||
{
|
||||
std::string out(str);
|
||||
out.erase(std::remove_if(out.begin(),
|
||||
out.end(),
|
||||
[](unsigned char x){ return std::isspace(x); }),
|
||||
out.end());
|
||||
out.erase(
|
||||
std::remove_if(out.begin(), out.end(), [](unsigned char x) { return std::isspace(x); }),
|
||||
out.end());
|
||||
|
||||
return out;
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -5,13 +5,13 @@
|
||||
*/
|
||||
|
||||
#include "IXWebSocketServer.h"
|
||||
#include "IXWebSocketTransport.h"
|
||||
#include "IXWebSocket.h"
|
||||
#include "IXSocketConnect.h"
|
||||
#include "IXNetSystem.h"
|
||||
|
||||
#include <sstream>
|
||||
#include "IXNetSystem.h"
|
||||
#include "IXSocketConnect.h"
|
||||
#include "IXWebSocket.h"
|
||||
#include "IXWebSocketTransport.h"
|
||||
#include <future>
|
||||
#include <sstream>
|
||||
#include <string.h>
|
||||
|
||||
namespace ix
|
||||
@ -23,11 +23,11 @@ namespace ix
|
||||
const std::string& host,
|
||||
int backlog,
|
||||
size_t maxConnections,
|
||||
int handshakeTimeoutSecs) : SocketServer(port, host, backlog, maxConnections),
|
||||
_handshakeTimeoutSecs(handshakeTimeoutSecs),
|
||||
_enablePong(kDefaultEnablePong)
|
||||
int handshakeTimeoutSecs)
|
||||
: SocketServer(port, host, backlog, maxConnections)
|
||||
, _handshakeTimeoutSecs(handshakeTimeoutSecs)
|
||||
, _enablePong(kDefaultEnablePong)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
WebSocketServer::~WebSocketServer()
|
||||
@ -63,9 +63,7 @@ namespace ix
|
||||
_onConnectionCallback = callback;
|
||||
}
|
||||
|
||||
void WebSocketServer::handleConnection(
|
||||
int fd,
|
||||
std::shared_ptr<ConnectionState> connectionState)
|
||||
void WebSocketServer::handleConnection(int fd, std::shared_ptr<ConnectionState> connectionState)
|
||||
{
|
||||
auto webSocket = std::make_shared<WebSocket>();
|
||||
_onConnectionCallback(webSocket, connectionState);
|
||||
@ -93,10 +91,8 @@ namespace ix
|
||||
else
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "WebSocketServer::handleConnection() HTTP status: "
|
||||
<< status.http_status
|
||||
<< " error: "
|
||||
<< status.errorStr;
|
||||
ss << "WebSocketServer::handleConnection() HTTP status: " << status.http_status
|
||||
<< " error: " << status.errorStr;
|
||||
logError(ss.str());
|
||||
}
|
||||
|
||||
@ -126,4 +122,4 @@ namespace ix
|
||||
std::lock_guard<std::mutex> lock(_clientsMutex);
|
||||
return _clients.size();
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -32,24 +32,23 @@
|
||||
// Adapted from https://github.com/dhbaird/easywsclient
|
||||
//
|
||||
|
||||
#include "IXSocketTLSOptions.h"
|
||||
#include "IXWebSocketTransport.h"
|
||||
|
||||
#include "IXSocketFactory.h"
|
||||
#include "IXSocketTLSOptions.h"
|
||||
#include "IXUrlParser.h"
|
||||
#include "IXUtf8Validator.h"
|
||||
#include "IXWebSocketHandshake.h"
|
||||
#include "IXWebSocketHttpHeaders.h"
|
||||
#include "IXUrlParser.h"
|
||||
#include "IXSocketFactory.h"
|
||||
#include "IXUtf8Validator.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <cstdarg>
|
||||
#include <sstream>
|
||||
#include <chrono>
|
||||
#include <cstdarg>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace
|
||||
@ -65,7 +64,7 @@ namespace
|
||||
|
||||
return a;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace ix
|
||||
{
|
||||
@ -76,24 +75,24 @@ namespace ix
|
||||
const int WebSocketTransport::kClosingMaximumWaitingDelayInMs(300);
|
||||
constexpr size_t WebSocketTransport::kChunkSize;
|
||||
|
||||
WebSocketTransport::WebSocketTransport() :
|
||||
_useMask(true),
|
||||
_compressedMessage(false),
|
||||
_readyState(ReadyState::CLOSED),
|
||||
_closeCode(WebSocketCloseConstants::kInternalErrorCode),
|
||||
_closeReason(WebSocketCloseConstants::kInternalErrorMessage),
|
||||
_closeWireSize(0),
|
||||
_closeRemote(false),
|
||||
_enablePerMessageDeflate(false),
|
||||
_requestInitCancellation(false),
|
||||
_closingTimePoint(std::chrono::steady_clock::now()),
|
||||
_enablePong(kDefaultEnablePong),
|
||||
_pingIntervalSecs(kDefaultPingIntervalSecs),
|
||||
_pingTimeoutSecs(kDefaultPingTimeoutSecs),
|
||||
_pingIntervalOrTimeoutGCDSecs(-1),
|
||||
_nextGCDTimePoint(std::chrono::steady_clock::now()),
|
||||
_lastSendPingTimePoint(std::chrono::steady_clock::now()),
|
||||
_lastReceivePongTimePoint(std::chrono::steady_clock::now())
|
||||
WebSocketTransport::WebSocketTransport()
|
||||
: _useMask(true)
|
||||
, _compressedMessage(false)
|
||||
, _readyState(ReadyState::CLOSED)
|
||||
, _closeCode(WebSocketCloseConstants::kInternalErrorCode)
|
||||
, _closeReason(WebSocketCloseConstants::kInternalErrorMessage)
|
||||
, _closeWireSize(0)
|
||||
, _closeRemote(false)
|
||||
, _enablePerMessageDeflate(false)
|
||||
, _requestInitCancellation(false)
|
||||
, _closingTimePoint(std::chrono::steady_clock::now())
|
||||
, _enablePong(kDefaultEnablePong)
|
||||
, _pingIntervalSecs(kDefaultPingIntervalSecs)
|
||||
, _pingTimeoutSecs(kDefaultPingTimeoutSecs)
|
||||
, _pingIntervalOrTimeoutGCDSecs(-1)
|
||||
, _nextGCDTimePoint(std::chrono::steady_clock::now())
|
||||
, _lastSendPingTimePoint(std::chrono::steady_clock::now())
|
||||
, _lastReceivePongTimePoint(std::chrono::steady_clock::now())
|
||||
{
|
||||
_readbuf.resize(kChunkSize);
|
||||
}
|
||||
@ -103,11 +102,12 @@ namespace ix
|
||||
;
|
||||
}
|
||||
|
||||
void WebSocketTransport::configure(const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
|
||||
const SocketTLSOptions& socketTLSOptions,
|
||||
bool enablePong,
|
||||
int pingIntervalSecs,
|
||||
int pingTimeoutSecs)
|
||||
void WebSocketTransport::configure(
|
||||
const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
|
||||
const SocketTLSOptions& socketTLSOptions,
|
||||
bool enablePong,
|
||||
int pingIntervalSecs,
|
||||
int pingTimeoutSecs)
|
||||
{
|
||||
_perMessageDeflateOptions = perMessageDeflateOptions;
|
||||
_enablePerMessageDeflate = _perMessageDeflateOptions.enabled();
|
||||
@ -118,8 +118,8 @@ namespace ix
|
||||
|
||||
if (pingIntervalSecs > 0 && pingTimeoutSecs > 0)
|
||||
{
|
||||
_pingIntervalOrTimeoutGCDSecs = greatestCommonDivisor(pingIntervalSecs,
|
||||
pingTimeoutSecs);
|
||||
_pingIntervalOrTimeoutGCDSecs =
|
||||
greatestCommonDivisor(pingIntervalSecs, pingTimeoutSecs);
|
||||
}
|
||||
else if (_pingTimeoutSecs > 0)
|
||||
{
|
||||
@ -132,10 +132,9 @@ namespace ix
|
||||
}
|
||||
|
||||
// Client
|
||||
WebSocketInitResult WebSocketTransport::connectToUrl(
|
||||
const std::string& url,
|
||||
const WebSocketHttpHeaders& headers,
|
||||
int timeoutSecs)
|
||||
WebSocketInitResult WebSocketTransport::connectToUrl(const std::string& url,
|
||||
const WebSocketHttpHeaders& headers,
|
||||
int timeoutSecs)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_socketMutex);
|
||||
|
||||
@ -144,8 +143,7 @@ namespace ix
|
||||
|
||||
if (!UrlParser::parse(url, protocol, host, path, query, port))
|
||||
{
|
||||
return WebSocketInitResult(false, 0,
|
||||
std::string("Could not parse URL ") + url);
|
||||
return WebSocketInitResult(false, 0, std::string("Could not parse URL ") + url);
|
||||
}
|
||||
|
||||
std::string errorMsg;
|
||||
@ -163,8 +161,8 @@ namespace ix
|
||||
_perMessageDeflateOptions,
|
||||
_enablePerMessageDeflate);
|
||||
|
||||
auto result = webSocketHandshake.clientHandshake(url, headers, host, path,
|
||||
port, timeoutSecs);
|
||||
auto result =
|
||||
webSocketHandshake.clientHandshake(url, headers, host, path, port, timeoutSecs);
|
||||
if (result.success)
|
||||
{
|
||||
setReadyState(ReadyState::OPEN);
|
||||
@ -247,15 +245,15 @@ namespace ix
|
||||
|
||||
if (_pingIntervalOrTimeoutGCDSecs > 0)
|
||||
{
|
||||
_nextGCDTimePoint = std::chrono::steady_clock::now() + std::chrono::seconds(_pingIntervalOrTimeoutGCDSecs);
|
||||
_nextGCDTimePoint = std::chrono::steady_clock::now() +
|
||||
std::chrono::seconds(_pingIntervalOrTimeoutGCDSecs);
|
||||
}
|
||||
}
|
||||
|
||||
// Only consider send PING time points for that computation.
|
||||
bool WebSocketTransport::pingIntervalExceeded()
|
||||
{
|
||||
if (_pingIntervalSecs <= 0)
|
||||
return false;
|
||||
if (_pingIntervalSecs <= 0) return false;
|
||||
|
||||
std::lock_guard<std::mutex> lock(_lastSendPingTimePointMutex);
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
@ -264,8 +262,7 @@ namespace ix
|
||||
|
||||
bool WebSocketTransport::pingTimeoutExceeded()
|
||||
{
|
||||
if (_pingTimeoutSecs <= 0)
|
||||
return false;
|
||||
if (_pingTimeoutSecs <= 0) return false;
|
||||
|
||||
std::lock_guard<std::mutex> lock(_lastReceivePongTimePointMutex);
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
@ -302,7 +299,8 @@ namespace ix
|
||||
|
||||
// No timeout if state is not OPEN, otherwise computed
|
||||
// pingIntervalOrTimeoutGCD (equals to -1 if no ping and no ping timeout are set)
|
||||
int lastingTimeoutDelayInMs = (_readyState != ReadyState::OPEN) ? 0 : _pingIntervalOrTimeoutGCDSecs;
|
||||
int lastingTimeoutDelayInMs =
|
||||
(_readyState != ReadyState::OPEN) ? 0 : _pingIntervalOrTimeoutGCDSecs;
|
||||
|
||||
if (_pingIntervalOrTimeoutGCDSecs > 0)
|
||||
{
|
||||
@ -317,7 +315,10 @@ namespace ix
|
||||
}
|
||||
else
|
||||
{
|
||||
lastingTimeoutDelayInMs = (int)std::chrono::duration_cast<std::chrono::milliseconds>(_nextGCDTimePoint - now).count();
|
||||
lastingTimeoutDelayInMs =
|
||||
(int) std::chrono::duration_cast<std::chrono::milliseconds>(_nextGCDTimePoint -
|
||||
now)
|
||||
.count();
|
||||
}
|
||||
}
|
||||
|
||||
@ -365,7 +366,7 @@ namespace ix
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
ssize_t ret = _socket->recv((char*)&_readbuf[0], _readbuf.size());
|
||||
ssize_t ret = _socket->recv((char*) &_readbuf[0], _readbuf.size());
|
||||
|
||||
if (ret < 0 && Socket::isWaitNeeded())
|
||||
{
|
||||
@ -373,8 +374,9 @@ namespace ix
|
||||
}
|
||||
else if (ret <= 0)
|
||||
{
|
||||
// if there are received data pending to be processed, then delay the abnormal closure
|
||||
// to after dispatch (other close code/reason could be read from the buffer)
|
||||
// if there are received data pending to be processed, then delay the abnormal
|
||||
// closure to after dispatch (other close code/reason could be read from the
|
||||
// buffer)
|
||||
|
||||
closeSocket();
|
||||
|
||||
@ -382,9 +384,7 @@ namespace ix
|
||||
}
|
||||
else
|
||||
{
|
||||
_rxbuf.insert(_rxbuf.end(),
|
||||
_readbuf.begin(),
|
||||
_readbuf.begin() + ret);
|
||||
_rxbuf.insert(_rxbuf.end(), _readbuf.begin(), _readbuf.begin() + ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -429,7 +429,7 @@ namespace ix
|
||||
{
|
||||
for (size_t i = 0; i != (size_t) message_size; ++i)
|
||||
{
|
||||
*(_txbuf.end() - (size_t) message_size + i) ^= masking_key[i&0x3];
|
||||
*(_txbuf.end() - (size_t) message_size + i) ^= masking_key[i & 0x3];
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -440,7 +440,7 @@ namespace ix
|
||||
{
|
||||
for (size_t j = 0; j != ws.N; ++j)
|
||||
{
|
||||
_rxbuf[j+ws.header_size] ^= ws.masking_key[j&0x3];
|
||||
_rxbuf[j + ws.header_size] ^= ws.masking_key[j & 0x3];
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -473,16 +473,17 @@ namespace ix
|
||||
while (true)
|
||||
{
|
||||
wsheader_type ws;
|
||||
if (_rxbuf.size() < 2) break; /* Need at least 2 */
|
||||
const uint8_t * data = (uint8_t *) &_rxbuf[0]; // peek, but don't consume
|
||||
if (_rxbuf.size() < 2) break; /* Need at least 2 */
|
||||
const uint8_t* data = (uint8_t*) &_rxbuf[0]; // peek, but don't consume
|
||||
ws.fin = (data[0] & 0x80) == 0x80;
|
||||
ws.rsv1 = (data[0] & 0x40) == 0x40;
|
||||
ws.rsv2 = (data[0] & 0x20) == 0x20;
|
||||
ws.rsv3 = (data[0] & 0x10) == 0x10;
|
||||
ws.opcode = (wsheader_type::opcode_type) (data[0] & 0x0f);
|
||||
ws.opcode = (wsheader_type::opcode_type)(data[0] & 0x0f);
|
||||
ws.mask = (data[1] & 0x80) == 0x80;
|
||||
ws.N0 = (data[1] & 0x7f);
|
||||
ws.header_size = 2 + (ws.N0 == 126? 2 : 0) + (ws.N0 == 127? 8 : 0) + (ws.mask? 4 : 0);
|
||||
ws.header_size =
|
||||
2 + (ws.N0 == 126 ? 2 : 0) + (ws.N0 == 127 ? 8 : 0) + (ws.mask ? 4 : 0);
|
||||
if (_rxbuf.size() < ws.header_size) break; /* Need: ws.header_size - _rxbuf.size() */
|
||||
|
||||
if ((ws.rsv1 && !_enablePerMessageDeflate) || ws.rsv2 || ws.rsv3)
|
||||
@ -533,10 +534,10 @@ namespace ix
|
||||
|
||||
if (ws.mask)
|
||||
{
|
||||
ws.masking_key[0] = ((uint8_t) data[i+0]) << 0;
|
||||
ws.masking_key[1] = ((uint8_t) data[i+1]) << 0;
|
||||
ws.masking_key[2] = ((uint8_t) data[i+2]) << 0;
|
||||
ws.masking_key[3] = ((uint8_t) data[i+3]) << 0;
|
||||
ws.masking_key[0] = ((uint8_t) data[i + 0]) << 0;
|
||||
ws.masking_key[1] = ((uint8_t) data[i + 1]) << 0;
|
||||
ws.masking_key[2] = ((uint8_t) data[i + 2]) << 0;
|
||||
ws.masking_key[3] = ((uint8_t) data[i + 3]) << 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -546,16 +547,14 @@ namespace ix
|
||||
ws.masking_key[3] = 0;
|
||||
}
|
||||
|
||||
if (_rxbuf.size() < ws.header_size+ws.N)
|
||||
if (_rxbuf.size() < ws.header_size + ws.N)
|
||||
{
|
||||
return; /* Need: ws.header_size+ws.N - _rxbuf.size() */
|
||||
}
|
||||
|
||||
if (!ws.fin && (
|
||||
ws.opcode == wsheader_type::PING
|
||||
|| ws.opcode == wsheader_type::PONG
|
||||
|| ws.opcode == wsheader_type::CLOSE
|
||||
)){
|
||||
if (!ws.fin && (ws.opcode == wsheader_type::PING || ws.opcode == wsheader_type::PONG ||
|
||||
ws.opcode == wsheader_type::CLOSE))
|
||||
{
|
||||
// Control messages should not be fragmented
|
||||
close(WebSocketCloseConstants::kProtocolErrorCode,
|
||||
WebSocketCloseConstants::kProtocolErrorCodeControlMessageFragmented);
|
||||
@ -563,22 +562,19 @@ namespace ix
|
||||
}
|
||||
|
||||
unmaskReceiveBuffer(ws);
|
||||
std::string frameData(_rxbuf.begin()+ws.header_size,
|
||||
_rxbuf.begin()+ws.header_size+(size_t) ws.N);
|
||||
std::string frameData(_rxbuf.begin() + ws.header_size,
|
||||
_rxbuf.begin() + ws.header_size + (size_t) ws.N);
|
||||
|
||||
// We got a whole message, now do something with it:
|
||||
if (
|
||||
ws.opcode == wsheader_type::TEXT_FRAME
|
||||
|| ws.opcode == wsheader_type::BINARY_FRAME
|
||||
|| ws.opcode == wsheader_type::CONTINUATION
|
||||
) {
|
||||
|
||||
if (ws.opcode == wsheader_type::TEXT_FRAME ||
|
||||
ws.opcode == wsheader_type::BINARY_FRAME ||
|
||||
ws.opcode == wsheader_type::CONTINUATION)
|
||||
{
|
||||
if (ws.opcode != wsheader_type::CONTINUATION)
|
||||
{
|
||||
_fragmentedMessageKind =
|
||||
(ws.opcode == wsheader_type::TEXT_FRAME)
|
||||
? MessageKind::MSG_TEXT
|
||||
: MessageKind::MSG_BINARY;
|
||||
_fragmentedMessageKind = (ws.opcode == wsheader_type::TEXT_FRAME)
|
||||
? MessageKind::MSG_TEXT
|
||||
: MessageKind::MSG_BINARY;
|
||||
|
||||
_compressedMessage = _enablePerMessageDeflate && ws.rsv1;
|
||||
|
||||
@ -592,8 +588,9 @@ namespace ix
|
||||
else if (_chunks.empty())
|
||||
{
|
||||
// Continuation message need to follow a non-fin TEXT or BINARY message
|
||||
close(WebSocketCloseConstants::kProtocolErrorCode,
|
||||
WebSocketCloseConstants::kProtocolErrorCodeContinuationOpCodeOutOfSequence);
|
||||
close(
|
||||
WebSocketCloseConstants::kProtocolErrorCode,
|
||||
WebSocketCloseConstants::kProtocolErrorCodeContinuationOpCodeOutOfSequence);
|
||||
}
|
||||
|
||||
//
|
||||
@ -601,10 +598,8 @@ namespace ix
|
||||
//
|
||||
if (ws.fin && _chunks.empty())
|
||||
{
|
||||
emitMessage(_fragmentedMessageKind,
|
||||
frameData,
|
||||
_compressedMessage,
|
||||
onMessageCallback);
|
||||
emitMessage(
|
||||
_fragmentedMessageKind, frameData, _compressedMessage, onMessageCallback);
|
||||
|
||||
_compressedMessage = false;
|
||||
}
|
||||
@ -621,8 +616,10 @@ namespace ix
|
||||
|
||||
if (ws.fin)
|
||||
{
|
||||
emitMessage(_fragmentedMessageKind, getMergedChunks(),
|
||||
_compressedMessage, onMessageCallback);
|
||||
emitMessage(_fragmentedMessageKind,
|
||||
getMergedChunks(),
|
||||
_compressedMessage,
|
||||
onMessageCallback);
|
||||
|
||||
_chunks.clear();
|
||||
_compressedMessage = false;
|
||||
@ -668,8 +665,8 @@ namespace ix
|
||||
if (ws.N >= 2)
|
||||
{
|
||||
// Extract the close code first, available as the first 2 bytes
|
||||
code |= ((uint64_t) _rxbuf[ws.header_size]) << 8;
|
||||
code |= ((uint64_t) _rxbuf[ws.header_size+1]) << 0;
|
||||
code |= ((uint64_t) _rxbuf[ws.header_size]) << 8;
|
||||
code |= ((uint64_t) _rxbuf[ws.header_size + 1]) << 0;
|
||||
|
||||
// Get the reason.
|
||||
if (ws.N > 2)
|
||||
@ -690,13 +687,11 @@ namespace ix
|
||||
// Full list of status code and status range is defined in the dedicated
|
||||
// RFC section at https://tools.ietf.org/html/rfc6455#page-45
|
||||
//
|
||||
if (code < 1000 || code == 1004 || code == 1006 ||
|
||||
(code > 1013 && code < 3000))
|
||||
if (code < 1000 || code == 1004 || code == 1006 || (code > 1013 && code < 3000))
|
||||
{
|
||||
// build up an error message containing the bad error code
|
||||
std::stringstream ss;
|
||||
ss << WebSocketCloseConstants::kInvalidCloseCodeMessage
|
||||
<< ": " << code;
|
||||
ss << WebSocketCloseConstants::kInvalidCloseCodeMessage << ": " << code;
|
||||
reason = ss.str();
|
||||
|
||||
code = WebSocketCloseConstants::kProtocolErrorCode;
|
||||
@ -722,8 +717,8 @@ namespace ix
|
||||
}
|
||||
else
|
||||
{
|
||||
// we got the CLOSE frame answer from our close, so we can close the connection if
|
||||
// the code/reason are the same
|
||||
// we got the CLOSE frame answer from our close, so we can close the connection
|
||||
// if the code/reason are the same
|
||||
bool identicalReason;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_closeDataMutex);
|
||||
@ -746,8 +741,7 @@ namespace ix
|
||||
}
|
||||
|
||||
// Erase the message that has been processed from the input/read buffer
|
||||
_rxbuf.erase(_rxbuf.begin(),
|
||||
_rxbuf.begin() + ws.header_size + (size_t) ws.N);
|
||||
_rxbuf.erase(_rxbuf.begin(), _rxbuf.begin() + ws.header_size + (size_t) ws.N);
|
||||
}
|
||||
|
||||
// if an abnormal closure was raised in poll, and nothing else triggered a CLOSED state in
|
||||
@ -756,7 +750,8 @@ namespace ix
|
||||
{
|
||||
_rxbuf.clear();
|
||||
|
||||
// if we previously closed the connection (CLOSING state), then set state to CLOSED (code/reason were set before)
|
||||
// if we previously closed the connection (CLOSING state), then set state to CLOSED
|
||||
// (code/reason were set before)
|
||||
if (_readyState == ReadyState::CLOSING)
|
||||
{
|
||||
closeSocket();
|
||||
@ -767,7 +762,8 @@ namespace ix
|
||||
{
|
||||
closeSocketAndSwitchToClosedState(WebSocketCloseConstants::kAbnormalCloseCode,
|
||||
WebSocketCloseConstants::kAbnormalCloseMessage,
|
||||
0, false);
|
||||
0,
|
||||
false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -832,16 +828,14 @@ namespace ix
|
||||
{
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto seconds =
|
||||
std::chrono::duration_cast<std::chrono::seconds>(
|
||||
now.time_since_epoch()).count();
|
||||
std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
|
||||
return static_cast<unsigned>(seconds);
|
||||
}
|
||||
|
||||
WebSocketSendInfo WebSocketTransport::sendData(
|
||||
wsheader_type::opcode_type type,
|
||||
const std::string& message,
|
||||
bool compress,
|
||||
const OnProgressCallback& onProgressCallback)
|
||||
WebSocketSendInfo WebSocketTransport::sendData(wsheader_type::opcode_type type,
|
||||
const std::string& message,
|
||||
bool compress,
|
||||
const OnProgressCallback& onProgressCallback)
|
||||
{
|
||||
if (_readyState != ReadyState::OPEN && _readyState != ReadyState::CLOSING)
|
||||
{
|
||||
@ -895,10 +889,10 @@ namespace ix
|
||||
std::string::const_iterator begin = message_begin;
|
||||
std::string::const_iterator end = message_end;
|
||||
|
||||
for (uint64_t i = 0 ; i < steps; ++i)
|
||||
for (uint64_t i = 0; i < steps; ++i)
|
||||
{
|
||||
bool firstStep = i == 0;
|
||||
bool lastStep = (i+1) == steps;
|
||||
bool lastStep = (i + 1) == steps;
|
||||
bool fin = lastStep;
|
||||
|
||||
end = begin + kChunkSize;
|
||||
@ -916,7 +910,7 @@ namespace ix
|
||||
// Send message
|
||||
sendFragment(opcodeType, fin, begin, end, compress);
|
||||
|
||||
if (onProgressCallback && !onProgressCallback((int)i, (int) steps))
|
||||
if (onProgressCallback && !onProgressCallback((int) i, (int) steps))
|
||||
{
|
||||
break;
|
||||
}
|
||||
@ -946,14 +940,13 @@ namespace ix
|
||||
uint8_t masking_key[4] = {};
|
||||
masking_key[0] = (x >> 24);
|
||||
masking_key[1] = (x >> 16) & 0xff;
|
||||
masking_key[2] = (x >> 8) & 0xff;
|
||||
masking_key[3] = (x) & 0xff;
|
||||
masking_key[2] = (x >> 8) & 0xff;
|
||||
masking_key[3] = (x) &0xff;
|
||||
|
||||
std::vector<uint8_t> header;
|
||||
header.assign(2 +
|
||||
(message_size >= 126 ? 2 : 0) +
|
||||
(message_size >= 65536 ? 6 : 0) +
|
||||
(_useMask ? 4 : 0), 0);
|
||||
header.assign(2 + (message_size >= 126 ? 2 : 0) + (message_size >= 65536 ? 6 : 0) +
|
||||
(_useMask ? 4 : 0),
|
||||
0);
|
||||
header[0] = type;
|
||||
|
||||
// The fin bit indicate that this is the last fragment. Fin is French for end.
|
||||
@ -1004,8 +997,8 @@ namespace ix
|
||||
header[5] = (message_size >> 32) & 0xff;
|
||||
header[6] = (message_size >> 24) & 0xff;
|
||||
header[7] = (message_size >> 16) & 0xff;
|
||||
header[8] = (message_size >> 8) & 0xff;
|
||||
header[9] = (message_size >> 0) & 0xff;
|
||||
header[8] = (message_size >> 8) & 0xff;
|
||||
header[9] = (message_size >> 0) & 0xff;
|
||||
|
||||
if (_useMask)
|
||||
{
|
||||
@ -1017,8 +1010,7 @@ namespace ix
|
||||
}
|
||||
|
||||
// _txbuf will keep growing until it can be transmitted over the socket:
|
||||
appendToSendBuffer(header, message_begin, message_end,
|
||||
message_size, masking_key);
|
||||
appendToSendBuffer(header, message_begin, message_end, message_size, masking_key);
|
||||
|
||||
// Now actually send this data
|
||||
sendOnSocket();
|
||||
@ -1038,28 +1030,26 @@ namespace ix
|
||||
return info;
|
||||
}
|
||||
|
||||
WebSocketSendInfo WebSocketTransport::sendBinary(
|
||||
const std::string& message,
|
||||
const OnProgressCallback& onProgressCallback)
|
||||
WebSocketSendInfo WebSocketTransport::sendBinary(const std::string& message,
|
||||
const OnProgressCallback& onProgressCallback)
|
||||
|
||||
{
|
||||
return sendData(wsheader_type::BINARY_FRAME, message,
|
||||
_enablePerMessageDeflate, onProgressCallback);
|
||||
return sendData(
|
||||
wsheader_type::BINARY_FRAME, message, _enablePerMessageDeflate, onProgressCallback);
|
||||
}
|
||||
|
||||
WebSocketSendInfo WebSocketTransport::sendText(
|
||||
const std::string& message,
|
||||
const OnProgressCallback& onProgressCallback)
|
||||
WebSocketSendInfo WebSocketTransport::sendText(const std::string& message,
|
||||
const OnProgressCallback& onProgressCallback)
|
||||
|
||||
{
|
||||
return sendData(wsheader_type::TEXT_FRAME, message,
|
||||
_enablePerMessageDeflate, onProgressCallback);
|
||||
return sendData(
|
||||
wsheader_type::TEXT_FRAME, message, _enablePerMessageDeflate, onProgressCallback);
|
||||
}
|
||||
|
||||
ssize_t WebSocketTransport::send()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_socketMutex);
|
||||
return _socket->send((char*)&_txbuf[0], _txbuf.size());
|
||||
return _socket->send((char*) &_txbuf[0], _txbuf.size());
|
||||
}
|
||||
|
||||
void WebSocketTransport::sendOnSocket()
|
||||
@ -1096,7 +1086,7 @@ namespace ix
|
||||
{
|
||||
// See list of close events here:
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
|
||||
std::string closure{(char)(code >> 8), (char)(code & 0xff)};
|
||||
std::string closure {(char) (code >> 8), (char) (code & 0xff)};
|
||||
|
||||
// copy reason after code
|
||||
closure.append(reason);
|
||||
@ -1116,8 +1106,10 @@ namespace ix
|
||||
_socket->close();
|
||||
}
|
||||
|
||||
void WebSocketTransport::closeSocketAndSwitchToClosedState(
|
||||
uint16_t code, const std::string& reason, size_t closeWireSize, bool remote)
|
||||
void WebSocketTransport::closeSocketAndSwitchToClosedState(uint16_t code,
|
||||
const std::string& reason,
|
||||
size_t closeWireSize,
|
||||
bool remote)
|
||||
{
|
||||
closeSocket();
|
||||
|
||||
@ -1133,8 +1125,10 @@ namespace ix
|
||||
_requestInitCancellation = false;
|
||||
}
|
||||
|
||||
void WebSocketTransport::close(
|
||||
uint16_t code, const std::string& reason, size_t closeWireSize, bool remote)
|
||||
void WebSocketTransport::close(uint16_t code,
|
||||
const std::string& reason,
|
||||
size_t closeWireSize,
|
||||
bool remote)
|
||||
{
|
||||
_requestInitCancellation = true;
|
||||
|
||||
|
@ -25,7 +25,6 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace ix
|
||||
|
@ -32,232 +32,249 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
// check if the scheme name is valid
|
||||
static bool IsSchemeValid( const std::string& SchemeName )
|
||||
static bool IsSchemeValid(const std::string& SchemeName)
|
||||
{
|
||||
for ( auto c : SchemeName )
|
||||
{
|
||||
if ( !isalpha( c ) && c != '+' && c != '-' && c != '.' ) return false;
|
||||
}
|
||||
for (auto c : SchemeName)
|
||||
{
|
||||
if (!isalpha(c) && c != '+' && c != '-' && c != '.') return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LUrlParser::clParseURL::GetPort( int* OutPort ) const
|
||||
bool LUrlParser::clParseURL::GetPort(int* OutPort) const
|
||||
{
|
||||
if ( !IsValid() ) { return false; }
|
||||
if (!IsValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int Port = atoi( m_Port.c_str() );
|
||||
int Port = atoi(m_Port.c_str());
|
||||
|
||||
if ( Port <= 0 || Port > 65535 ) { return false; }
|
||||
if (Port <= 0 || Port > 65535)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( OutPort ) { *OutPort = Port; }
|
||||
if (OutPort)
|
||||
{
|
||||
*OutPort = Port;
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
// based on RFC 1738 and RFC 3986
|
||||
LUrlParser::clParseURL LUrlParser::clParseURL::ParseURL( const std::string& URL )
|
||||
LUrlParser::clParseURL LUrlParser::clParseURL::ParseURL(const std::string& URL)
|
||||
{
|
||||
LUrlParser::clParseURL Result;
|
||||
LUrlParser::clParseURL Result;
|
||||
|
||||
const char* CurrentString = URL.c_str();
|
||||
const char* CurrentString = URL.c_str();
|
||||
|
||||
/*
|
||||
* <scheme>:<scheme-specific-part>
|
||||
* <scheme> := [a-z\+\-\.]+
|
||||
* For resiliency, programs interpreting URLs should treat upper case letters as equivalent to lower case in scheme names
|
||||
*/
|
||||
/*
|
||||
* <scheme>:<scheme-specific-part>
|
||||
* <scheme> := [a-z\+\-\.]+
|
||||
* For resiliency, programs interpreting URLs should treat upper case letters as equivalent to
|
||||
*lower case in scheme names
|
||||
*/
|
||||
|
||||
// try to read scheme
|
||||
{
|
||||
const char* LocalString = strchr( CurrentString, ':' );
|
||||
// try to read scheme
|
||||
{
|
||||
const char* LocalString = strchr(CurrentString, ':');
|
||||
|
||||
if ( !LocalString )
|
||||
{
|
||||
return clParseURL( LUrlParserError_NoUrlCharacter );
|
||||
}
|
||||
if (!LocalString)
|
||||
{
|
||||
return clParseURL(LUrlParserError_NoUrlCharacter);
|
||||
}
|
||||
|
||||
// save the scheme name
|
||||
Result.m_Scheme = std::string( CurrentString, LocalString - CurrentString );
|
||||
// save the scheme name
|
||||
Result.m_Scheme = std::string(CurrentString, LocalString - CurrentString);
|
||||
|
||||
if ( !IsSchemeValid( Result.m_Scheme ) )
|
||||
{
|
||||
return clParseURL( LUrlParserError_InvalidSchemeName );
|
||||
}
|
||||
if (!IsSchemeValid(Result.m_Scheme))
|
||||
{
|
||||
return clParseURL(LUrlParserError_InvalidSchemeName);
|
||||
}
|
||||
|
||||
// scheme should be lowercase
|
||||
std::transform( Result.m_Scheme.begin(), Result.m_Scheme.end(), Result.m_Scheme.begin(), ::tolower );
|
||||
// scheme should be lowercase
|
||||
std::transform(
|
||||
Result.m_Scheme.begin(), Result.m_Scheme.end(), Result.m_Scheme.begin(), ::tolower);
|
||||
|
||||
// skip ':'
|
||||
CurrentString = LocalString+1;
|
||||
}
|
||||
// skip ':'
|
||||
CurrentString = LocalString + 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* //<user>:<password>@<host>:<port>/<url-path>
|
||||
* any ":", "@" and "/" must be normalized
|
||||
*/
|
||||
/*
|
||||
* //<user>:<password>@<host>:<port>/<url-path>
|
||||
* any ":", "@" and "/" must be normalized
|
||||
*/
|
||||
|
||||
// skip "//"
|
||||
if ( *CurrentString++ != '/' ) return clParseURL( LUrlParserError_NoDoubleSlash );
|
||||
if ( *CurrentString++ != '/' ) return clParseURL( LUrlParserError_NoDoubleSlash );
|
||||
// skip "//"
|
||||
if (*CurrentString++ != '/') return clParseURL(LUrlParserError_NoDoubleSlash);
|
||||
if (*CurrentString++ != '/') return clParseURL(LUrlParserError_NoDoubleSlash);
|
||||
|
||||
// check if the user name and password are specified
|
||||
bool bHasUserName = false;
|
||||
// check if the user name and password are specified
|
||||
bool bHasUserName = false;
|
||||
|
||||
const char* LocalString = CurrentString;
|
||||
const char* LocalString = CurrentString;
|
||||
|
||||
while ( *LocalString )
|
||||
{
|
||||
if ( *LocalString == '@' )
|
||||
{
|
||||
// user name and password are specified
|
||||
bHasUserName = true;
|
||||
break;
|
||||
}
|
||||
else if ( *LocalString == '/' )
|
||||
{
|
||||
// end of <host>:<port> specification
|
||||
bHasUserName = false;
|
||||
break;
|
||||
}
|
||||
while (*LocalString)
|
||||
{
|
||||
if (*LocalString == '@')
|
||||
{
|
||||
// user name and password are specified
|
||||
bHasUserName = true;
|
||||
break;
|
||||
}
|
||||
else if (*LocalString == '/')
|
||||
{
|
||||
// end of <host>:<port> specification
|
||||
bHasUserName = false;
|
||||
break;
|
||||
}
|
||||
|
||||
LocalString++;
|
||||
}
|
||||
LocalString++;
|
||||
}
|
||||
|
||||
// user name and password
|
||||
LocalString = CurrentString;
|
||||
// user name and password
|
||||
LocalString = CurrentString;
|
||||
|
||||
if ( bHasUserName )
|
||||
{
|
||||
// read user name
|
||||
while ( *LocalString && *LocalString != ':' && *LocalString != '@' ) LocalString++;
|
||||
if (bHasUserName)
|
||||
{
|
||||
// read user name
|
||||
while (*LocalString && *LocalString != ':' && *LocalString != '@')
|
||||
LocalString++;
|
||||
|
||||
Result.m_UserName = std::string( CurrentString, LocalString - CurrentString );
|
||||
Result.m_UserName = std::string(CurrentString, LocalString - CurrentString);
|
||||
|
||||
// proceed with the current pointer
|
||||
CurrentString = LocalString;
|
||||
// proceed with the current pointer
|
||||
CurrentString = LocalString;
|
||||
|
||||
if ( *CurrentString == ':' )
|
||||
{
|
||||
// skip ':'
|
||||
CurrentString++;
|
||||
if (*CurrentString == ':')
|
||||
{
|
||||
// skip ':'
|
||||
CurrentString++;
|
||||
|
||||
// read password
|
||||
LocalString = CurrentString;
|
||||
// read password
|
||||
LocalString = CurrentString;
|
||||
|
||||
while ( *LocalString && *LocalString != '@' ) LocalString++;
|
||||
while (*LocalString && *LocalString != '@')
|
||||
LocalString++;
|
||||
|
||||
Result.m_Password = std::string( CurrentString, LocalString - CurrentString );
|
||||
Result.m_Password = std::string(CurrentString, LocalString - CurrentString);
|
||||
|
||||
CurrentString = LocalString;
|
||||
}
|
||||
CurrentString = LocalString;
|
||||
}
|
||||
|
||||
// skip '@'
|
||||
if ( *CurrentString != '@' )
|
||||
{
|
||||
return clParseURL( LUrlParserError_NoAtSign );
|
||||
}
|
||||
// skip '@'
|
||||
if (*CurrentString != '@')
|
||||
{
|
||||
return clParseURL(LUrlParserError_NoAtSign);
|
||||
}
|
||||
|
||||
CurrentString++;
|
||||
}
|
||||
CurrentString++;
|
||||
}
|
||||
|
||||
bool bHasBracket = ( *CurrentString == '[' );
|
||||
bool bHasBracket = (*CurrentString == '[');
|
||||
|
||||
// go ahead, read the host name
|
||||
LocalString = CurrentString;
|
||||
// go ahead, read the host name
|
||||
LocalString = CurrentString;
|
||||
|
||||
while ( *LocalString )
|
||||
{
|
||||
if ( bHasBracket && *LocalString == ']' )
|
||||
{
|
||||
// end of IPv6 address
|
||||
LocalString++;
|
||||
break;
|
||||
}
|
||||
else if ( !bHasBracket && ( *LocalString == ':' || *LocalString == '/' ) )
|
||||
{
|
||||
// port number is specified
|
||||
break;
|
||||
}
|
||||
while (*LocalString)
|
||||
{
|
||||
if (bHasBracket && *LocalString == ']')
|
||||
{
|
||||
// end of IPv6 address
|
||||
LocalString++;
|
||||
break;
|
||||
}
|
||||
else if (!bHasBracket && (*LocalString == ':' || *LocalString == '/'))
|
||||
{
|
||||
// port number is specified
|
||||
break;
|
||||
}
|
||||
|
||||
LocalString++;
|
||||
}
|
||||
LocalString++;
|
||||
}
|
||||
|
||||
Result.m_Host = std::string( CurrentString, LocalString - CurrentString );
|
||||
Result.m_Host = std::string(CurrentString, LocalString - CurrentString);
|
||||
|
||||
CurrentString = LocalString;
|
||||
CurrentString = LocalString;
|
||||
|
||||
// is port number specified?
|
||||
if ( *CurrentString == ':' )
|
||||
{
|
||||
CurrentString++;
|
||||
// is port number specified?
|
||||
if (*CurrentString == ':')
|
||||
{
|
||||
CurrentString++;
|
||||
|
||||
// read port number
|
||||
LocalString = CurrentString;
|
||||
// read port number
|
||||
LocalString = CurrentString;
|
||||
|
||||
while ( *LocalString && *LocalString != '/' ) LocalString++;
|
||||
while (*LocalString && *LocalString != '/')
|
||||
LocalString++;
|
||||
|
||||
Result.m_Port = std::string( CurrentString, LocalString - CurrentString );
|
||||
Result.m_Port = std::string(CurrentString, LocalString - CurrentString);
|
||||
|
||||
CurrentString = LocalString;
|
||||
}
|
||||
CurrentString = LocalString;
|
||||
}
|
||||
|
||||
// end of string
|
||||
if ( !*CurrentString )
|
||||
{
|
||||
Result.m_ErrorCode = LUrlParserError_Ok;
|
||||
// end of string
|
||||
if (!*CurrentString)
|
||||
{
|
||||
Result.m_ErrorCode = LUrlParserError_Ok;
|
||||
|
||||
return Result;
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
||||
// skip '/'
|
||||
if ( *CurrentString != '/' )
|
||||
{
|
||||
return clParseURL( LUrlParserError_NoSlash );
|
||||
}
|
||||
// skip '/'
|
||||
if (*CurrentString != '/')
|
||||
{
|
||||
return clParseURL(LUrlParserError_NoSlash);
|
||||
}
|
||||
|
||||
CurrentString++;
|
||||
CurrentString++;
|
||||
|
||||
// parse the path
|
||||
LocalString = CurrentString;
|
||||
// parse the path
|
||||
LocalString = CurrentString;
|
||||
|
||||
while ( *LocalString && *LocalString != '#' && *LocalString != '?' ) LocalString++;
|
||||
while (*LocalString && *LocalString != '#' && *LocalString != '?')
|
||||
LocalString++;
|
||||
|
||||
Result.m_Path = std::string( CurrentString, LocalString - CurrentString );
|
||||
Result.m_Path = std::string(CurrentString, LocalString - CurrentString);
|
||||
|
||||
CurrentString = LocalString;
|
||||
CurrentString = LocalString;
|
||||
|
||||
// check for query
|
||||
if ( *CurrentString == '?' )
|
||||
{
|
||||
// skip '?'
|
||||
CurrentString++;
|
||||
// check for query
|
||||
if (*CurrentString == '?')
|
||||
{
|
||||
// skip '?'
|
||||
CurrentString++;
|
||||
|
||||
// read query
|
||||
LocalString = CurrentString;
|
||||
// read query
|
||||
LocalString = CurrentString;
|
||||
|
||||
while ( *LocalString && *LocalString != '#' ) LocalString++;
|
||||
while (*LocalString && *LocalString != '#')
|
||||
LocalString++;
|
||||
|
||||
Result.m_Query = std::string( CurrentString, LocalString - CurrentString );
|
||||
Result.m_Query = std::string(CurrentString, LocalString - CurrentString);
|
||||
|
||||
CurrentString = LocalString;
|
||||
}
|
||||
CurrentString = LocalString;
|
||||
}
|
||||
|
||||
// check for fragment
|
||||
if ( *CurrentString == '#' )
|
||||
{
|
||||
// skip '#'
|
||||
CurrentString++;
|
||||
// check for fragment
|
||||
if (*CurrentString == '#')
|
||||
{
|
||||
// skip '#'
|
||||
CurrentString++;
|
||||
|
||||
// read fragment
|
||||
LocalString = CurrentString;
|
||||
// read fragment
|
||||
LocalString = CurrentString;
|
||||
|
||||
while ( *LocalString ) LocalString++;
|
||||
while (*LocalString)
|
||||
LocalString++;
|
||||
|
||||
Result.m_Fragment = std::string( CurrentString, LocalString - CurrentString );
|
||||
}
|
||||
Result.m_Fragment = std::string(CurrentString, LocalString - CurrentString);
|
||||
}
|
||||
|
||||
Result.m_ErrorCode = LUrlParserError_Ok;
|
||||
Result.m_ErrorCode = LUrlParserError_Ok;
|
||||
|
||||
return Result;
|
||||
return Result;
|
||||
}
|
||||
|
@ -62,7 +62,10 @@ namespace LUrlParser
|
||||
}
|
||||
|
||||
/// return 'true' if the parsing was successful
|
||||
bool IsValid() const { return m_ErrorCode == LUrlParserError_Ok; }
|
||||
bool IsValid() const
|
||||
{
|
||||
return m_ErrorCode == LUrlParserError_Ok;
|
||||
}
|
||||
|
||||
/// helper to convert the port number to int, return 'true' if the port is valid (within the
|
||||
/// 0..65535 range)
|
||||
|
@ -17,4 +17,4 @@ namespace ix
|
||||
//
|
||||
pthread_setname_np(name.substr(0, 63).c_str());
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -15,7 +15,6 @@ namespace ix
|
||||
// See prctl and PR_SET_NAME property in
|
||||
// http://man7.org/linux/man-pages/man2/prctl.2.html
|
||||
//
|
||||
pthread_setname_np(pthread_self(),
|
||||
name.substr(0, 15).c_str());
|
||||
pthread_setname_np(pthread_self(), name.substr(0, 15).c_str());
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -4,13 +4,14 @@
|
||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
||||
*/
|
||||
#include "../IXSetThreadName.h"
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
namespace ix
|
||||
{
|
||||
const DWORD MS_VC_EXCEPTION = 0x406D1388;
|
||||
|
||||
#pragma pack(push,8)
|
||||
#pragma pack(push, 8)
|
||||
typedef struct tagTHREADNAME_INFO
|
||||
{
|
||||
DWORD dwType; // Must be 0x1000.
|
||||
@ -30,7 +31,8 @@ namespace ix
|
||||
|
||||
__try
|
||||
{
|
||||
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)& info);
|
||||
RaiseException(
|
||||
MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*) &info);
|
||||
}
|
||||
__except (EXCEPTION_EXECUTE_HANDLER)
|
||||
{
|
||||
@ -41,4 +43,4 @@ namespace ix
|
||||
{
|
||||
SetThreadName(-1, name.c_str());
|
||||
}
|
||||
}
|
||||
} // namespace ix
|
||||
|
Reference in New Issue
Block a user