Files
poco/Net/testsuite/src/HTTPTestServer.cpp
Matej Kenda 8a4a2955d5 Use nullptr in C++ code (solves #4348) (#5043)
* chore(CppParser): 0, NULL --> nullptr

* chore(Crypto): 0, NULL --> nullptr

* chore(DNSSD): 0, NULL --> nullptr

* chore(Encodings): 0, NULL --> nullptr

* chore(CppUnit): Correct indentation.

* chore(Foundation): 0, NULL --> nullptr

* chore(CMake): Always warn about wrong nullptr usage when compiling with GCC or CLang

* chore(Net): 0, NULL --> nullptr

* chore(Foundation): 0, NULL --> nullptr

* chore(Data): 0, NULL --> nullptr

* chore(macOS): 0, NULL --> nullptr

* chore(XML): 0, NULL --> nullptr

* chore(Zip): 0, NULL --> nullptr

* chore(Util): 0, NULL --> nullptr

* chore(Net/NetSSL): 0, NULL --> nullptr

* chore(Bonjour): 0, NULL --> nullptr

* chore(MongoDB, Redis): 0, NULL --> nullptr

* chore(Poco): 0, NULL --> nullptr

* chore(Win32): 0, NULL --> nullptr

* chore(CMake): Only warn about nullptr when verbose warnings are enabled.

* Potential fix for code scanning alert no. 1634: Guarded Free

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* chore(Net): Fix warning reported by gitlab.

* chore(gitlab CI): attempt to clean to gain disk space on the runner.

* chore(gitlab CI): Run build with  --parallel 4, correct docker cleanup.

---------

Co-authored-by: Aleksandar Fabijanic <aleks-f@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
2025-10-30 15:20:53 +01:00

279 lines
7.7 KiB
C++

//
// HTTPTestServer.cpp
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "HTTPTestServer.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Timespan.h"
#include "Poco/NumberFormatter.h"
#include <iostream>
using Poco::Net::Socket;
using Poco::Net::StreamSocket;
using Poco::Net::SocketAddress;
using Poco::NumberFormatter;
const std::string HTTPTestServer::SMALL_BODY("This is some random text data returned by the server");
const std::string HTTPTestServer::LARGE_BODY(4000, 'x');
HTTPTestServer::HTTPTestServer():
_socket(SocketAddress()),
_thread("HTTPTestServer"),
_stop(false)
{
_thread.start(*this);
_ready.wait();
_lastRequest.reserve(4000);
}
HTTPTestServer::HTTPTestServer(const std::string& addr) :
_socket(SocketAddress(addr)),
_thread("HTTPTestServer"),
_stop(false)
{
_thread.start(*this);
_ready.wait();
_lastRequest.reserve(4000);
}
HTTPTestServer::~HTTPTestServer()
{
_stop = true;
_thread.join();
}
Poco::UInt16 HTTPTestServer::port() const
{
return _socket.address().port();
}
const std::string& HTTPTestServer::lastRequest() const
{
return _lastRequest;
}
void HTTPTestServer::run()
{
_ready.set();
Poco::Timespan span(250000);
while (!_stop)
{
if (_socket.poll(span, Socket::SELECT_READ))
{
StreamSocket ss = _socket.acceptConnection();
try
{
_lastRequest.clear();
char buffer[256];
int n = ss.receiveBytes(buffer, sizeof(buffer));
while (n > 0 && !_stop)
{
_lastRequest.append(buffer, n);
if (!requestComplete())
{
n = ss.receiveBytes(buffer, sizeof(buffer));
}
else
n = 0;
}
std::string response = handleRequest();
n = ss.sendBytes(response.data(), (int) response.size());
if (n) Poco::Thread::sleep(1000);
try
{
ss.shutdown();
Poco::Thread::sleep(1000);
}
catch (Poco::Exception&)
{
}
}
catch (Poco::Exception& exc)
{
std::cerr << "HTTPTestServer: " << exc.displayText() << std::endl;
}
}
}
}
bool HTTPTestServer::requestComplete() const
{
return ((_lastRequest.substr(0, 3) == "GET" || _lastRequest.substr(0, 4) == "HEAD") &&
(_lastRequest.find("\r\n\r\n") != std::string::npos)) ||
(_lastRequest.find("\r\n0\r\n") != std::string::npos);
}
std::string HTTPTestServer::handleRequest() const
{
std::string response;
response.reserve(16000);
if (_lastRequest.substr(0, 10) == "GET /small" ||
_lastRequest.substr(0, 11) == "HEAD /small")
{
std::string body(SMALL_BODY);
response.append("HTTP/1.0 200 OK\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n");
response.append("Connection: Close\r\n");
response.append("\r\n");
if (_lastRequest.substr(0, 3) == "GET")
response.append(body);
}
else if (_lastRequest.substr(0, 10) == "GET /large" ||
_lastRequest.substr(0, 11) == "HEAD /large" ||
_lastRequest.substr(0, 36) == "GET http://www.somehost.com:80/large")
{
std::string body(LARGE_BODY);
response.append("HTTP/1.0 200 OK\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n");
response.append("Connection: Close\r\n");
response.append("\r\n");
if (_lastRequest.substr(0, 3) == "GET")
response.append(body);
}
else if (_lastRequest.substr(0, 12) == "POST /expect")
{
std::string::size_type pos = _lastRequest.find("\r\n\r\n");
pos += 4;
std::string body = _lastRequest.substr(pos);
response.append("HTTP/1.1 100 Continue\r\n\r\n");
response.append("HTTP/1.1 200 OK\r\n");
response.append("Content-Type: text/plain\r\n");
if (_lastRequest.find("Content-Length") != std::string::npos)
{
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n");
}
else if (_lastRequest.find("chunked") != std::string::npos)
{
response.append("Transfer-Encoding: chunked\r\n");
}
response.append("Connection: Close\r\n");
response.append("\r\n");
response.append(body);
}
else if (_lastRequest.substr(0, 10) == "POST /fail")
{
response.append("HTTP/1.1 400 Bad Request\r\n");
response.append("Connection: Close\r\n");
response.append("\r\n");
}
else if (_lastRequest.substr(0, 4) == "POST")
{
std::string::size_type pos = _lastRequest.find("\r\n\r\n");
pos += 4;
std::string body = _lastRequest.substr(pos);
response.append("HTTP/1.0 200 OK\r\n");
response.append("Content-Type: text/plain\r\n");
if (_lastRequest.find("Content-Length") != std::string::npos)
{
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n");
}
else if (_lastRequest.find("chunked") != std::string::npos)
{
response.append("Transfer-Encoding: chunked\r\n");
}
response.append("Connection: Close\r\n");
response.append("\r\n");
response.append(body);
}
else if (_lastRequest.substr(0, 15) == "HEAD /keepAlive")
{
std::string body(SMALL_BODY);
response.append("HTTP/1.1 200 OK\r\n");
response.append("Connection: keep-alive\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n\r\n");
response.append("HTTP/1.1 200 OK\r\n");
response.append("Connection: Keep-Alive\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n\r\n");
response.append(body);
body = LARGE_BODY;
response.append("HTTP/1.1 200 OK\r\n");
response.append("Connection: keep-alive\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Transfer-Encoding: chunked\r\n\r\n");
response.append(NumberFormatter::formatHex((unsigned) body.length()));
response.append("\r\n");
response.append(body);
response.append("\r\n0\r\n\r\n");
response.append("HTTP/1.1 200 OK\r\n");
response.append("Connection: close\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n\r\n");
}
else if (_lastRequest.substr(0, 12) == "GET /trailer")
{
std::string body(LARGE_BODY);
response.append("HTTP/1.1 200 OK\r\n");
response.append("Connection: keep-alive\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Transfer-Encoding: chunked\r\n\r\n");
response.append(NumberFormatter::formatHex((unsigned) body.length()));
response.append("\r\n");
response.append(body);
response.append("\r\n0\r\n");
response.append("Trailer-1: Value 1\r\n");
response.append("Trailer-2: Value 2\r\n");
response.append("\r\n");
}
else if (_lastRequest.substr(0, 13) == "GET /redirect")
{
response.append("HTTP/1.0 302 Found\r\n");
response.append("Location: /large\r\n");
response.append("\r\n");
}
else if (_lastRequest.substr(0, 13) == "GET /notfound")
{
response.append("HTTP/1.0 404 Not Found\r\n");
response.append("\r\n");
}
else if (_lastRequest.substr(0, 5) == "GET /" ||
_lastRequest.substr(0, 6) == "HEAD /")
{
std::string body(SMALL_BODY);
response.append("HTTP/1.0 200 OK\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n");
response.append("Connection: Close\r\n");
response.append("\r\n");
if (_lastRequest.substr(0, 3) == "GET")
response.append(body);
}
return response;
}