Commit abcd98a0 authored by jan.koester's avatar jan.koester
Browse files

fixes tcp cons

parent 39bd078c
Loading
Loading
Loading
Loading
+4 −3
Changes for src/socket.h: 4 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -188,9 +188,10 @@ namespace netplus {
		virtual size_t sendRaw(buffer& data, int flags = 0) { return sendData(data, flags); }

		// Read raw transport data and buffer it (for SSL/TLS pre-decryption)
		// Returns bytes read. For plain TCP, this is same as recvData.
		// For SSL, reads raw TCP into internal buffer for later decryption.
		virtual size_t prime_recv(buffer& data, int flags = 0) { return recvData(data, flags); }
		// Returns bytes read. For plain TCP, this is a no-op (data is read
		// directly in recvData). For SSL, reads raw TCP into internal buffer
		// for later decryption.
		virtual size_t prime_recv(buffer& data, int flags = 0) { (void)data; (void)flags; return 0; }

		virtual bool getHandshakeDone() { return true; }
		virtual bool hasBufferedData() const { return false; }
+97 −7
Changes for test/http.cpp: 97 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -3,9 +3,12 @@
#include <cassert>
#include <cstring>
#include <memory>
#include <thread>
#include <chrono>

#include "connection.h"
#include "eventapi.h"
#include "exception.h"

using namespace netplus;

@@ -15,8 +18,10 @@ class MyServer : public event {
    MyServer(std::vector<netplus::socket*> serversockets, int timeout=1000)
          : event(serversockets, timeout) {
    }
    // eventapi interface

    void RequestEvent(con &curcon, const int tid, ULONG_PTR args) override {
        std::cerr << "[SERVER] RequestEvent called, RecvData.size()="
                  << curcon.RecvData.size() << std::endl;
        const std::string body = "Hello world\n";
        std::string resp;
        resp += "HTTP/1.1 200 OK\r\n";
@@ -26,31 +31,116 @@ class MyServer : public event {
        resp += "\r\n";
        resp += body;

        // Append to send queue
        curcon.SendData.append(resp.data(), resp.size());
    }

    void ResponseEvent(con &curcon, const int tid, ULONG_PTR args) override {
        
    }

    void ConnectEvent(con &curcon, const int tid, ULONG_PTR args) override {
        // not needed for unit test
        std::cerr << "[SERVER] ConnectEvent called" << std::endl;
    }

    void DisconnectEvent(con &curcon, const int tid, ULONG_PTR args) override {
        // not needed for unit test
    }

    void CreateConnection(std::shared_ptr<con> &res) override {
        // Allocate a connection object bound to this eventapi instance
        res = std::make_shared<con>(this);
    }
};

int main() {
	tcp MySocket("0.0.0.0",8080,1024,-1);
    // Reset global state in case a previous run left it dirty
    event::Running = true;

    tcp MySocket("127.0.0.1", 8080, 1024, -1);

    MyServer srv({&MySocket});

    // Run the server in a background thread
    std::thread serverThread([&srv]() {
        srv.runEventloop();
    });

    // Give the server a moment to start listening
    std::this_thread::sleep_for(std::chrono::milliseconds(2000));

    int result = 1; // default = failure

    try {
        // Connect a client
        tcp client;
        std::cerr << "[DEBUG] Connecting..." << std::endl;
        client.connect("127.0.0.1", 8080);
        std::cerr << "[DEBUG] Connected, fd=" << client.fd() << std::endl;
        client.setTimeout(5000); // 5 second recv/send timeout

        // Send an HTTP request
        const std::string request =
            "GET / HTTP/1.1\r\n"
            "Host: 127.0.0.1\r\n"
            "Connection: close\r\n"
            "\r\n";

        buffer sendbuf(request.data(), request.size());
        client.sendData(sendbuf);

        std::cerr << "[DEBUG] Request sent, reading response..." << std::endl;

        // Read the response (with timeout protection)
        std::string response;
        for (;;) {
            buffer recvbuf(4096);
            size_t n = 0;
            try {
                n = client.recvData(recvbuf);
            } catch (netplus::NetException &ne) {
                std::cerr << "[DEBUG] recvData NetException: " << ne.what()
                          << " type=" << ne.getErrorType() << std::endl;
                break;
            } catch (...) {
                std::cerr << "[DEBUG] recvData unknown exception" << std::endl;
                break;
            }
            if (n == 0) break;
            response.append(recvbuf.data.buf, n);

            // Check if we received complete HTTP response (headers + body)
            auto hdr_end = response.find("\r\n\r\n");
            if (hdr_end != std::string::npos) {
                // Parse Content-Length
                size_t body_start = hdr_end + 4;
                auto cl_pos = response.find("Content-Length: ");
                if (cl_pos != std::string::npos) {
                    size_t cl_val = std::stoul(response.substr(cl_pos + 16));
                    if (response.size() >= body_start + cl_val)
                        break; // got entire response
                }
            }
        }

        client.close();

        // Verify the response
        if (response.find("HTTP/1.1 200 OK") != std::string::npos &&
            response.find("Hello world") != std::string::npos) {
            std::cout << "HTTP test PASSED" << std::endl;
            result = 0;
        } else {
            std::cerr << "HTTP test FAILED: unexpected response:\n"
                      << response << std::endl;
        }
    } catch (const std::exception &e) {
        std::cerr << "HTTP test FAILED with exception: " << e.what() << std::endl;
    } catch (...) {
        std::cerr << "HTTP test FAILED with unknown exception" << std::endl;
    }

    // Stop the server
    event::Running = false;

    if (serverThread.joinable())
        serverThread.join();

    return result;
}