libhttppp ..
Loading...
Searching...
No Matches
raw_tcp_client.h
1/*******************************************************************************
2Copyright (c) 2026, Jan Koester jan.koester@gmx.net
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the <organization> nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*******************************************************************************/
27
28// Small cross-platform (POSIX + MSVC/Windows) helpers for the hand-rolled
29// "raw socket" client/server code the H1/H2 tests use to drive libhttppp
30// with wire bytes they build themselves (HPACK frames, chunked encoding,
31// etc.) instead of going through HttpClient. Built on netplus::tcp so these
32// tests link and run under MSVC the same as everywhere else libnetplus
33// already abstracts POSIX vs Winsock -- none of the test .cpp files should
34// need to include <sys/socket.h>/<arpa/inet.h>/<netinet/in.h>/<unistd.h> or
35// call ::socket()/::connect()/::send()/::recv()/::close() directly.
36
37#pragma once
38
39#include <cstring>
40#include <stdexcept>
41#include <string>
42
43#include <netplus/exception.h>
44#include <netplus/socket.h>
45
46namespace rawtcp {
47
48// Reads back the OS-assigned port of a socket bound with port 0 (ephemeral
49// port allocation), e.g. right after netplus::tcp::bind(). getsockname()
50// itself is standard Berkeley/Winsock API available on both platforms --
51// sockaddr_in/AF_INET/ntohs all come in transitively via netplus/socket.h,
52// which already includes the right platform headers for each side.
53inline int boundPort(netplus::socket &sock) {
54 sockaddr_in bound{};
55 socklen_t blen = sizeof(bound);
56 ::getsockname(sock.fd(), reinterpret_cast<sockaddr *>(&bound), &blen);
57 return ntohs(bound.sin_port);
58}
59
60// Connects a blocking netplus::tcp client socket to 127.0.0.1:port.
61inline void connectLocal(netplus::tcp &sock, int port) {
62 sock.connect("127.0.0.1", port);
63}
64
65// Sends the entire buffer, loop over short writes like the raw ::send() loops
66// this replaces. Throws std::runtime_error if the peer is gone -- the
67// contract every raw-client call site here relies on (it's a test bug if a
68// hand-built request can't be sent to a server this test itself just
69// started).
70inline void sendAllOrThrow(netplus::socket &sock, const char *data, size_t len) {
71 size_t sent = 0;
72 while (sent < len) {
73 netplus::buffer buf(data + sent, len - sent);
74 size_t n;
75 try {
76 n = sock.sendData(buf);
77 } catch (netplus::NetException &e) {
78 throw std::runtime_error(std::string("rawtcp::sendAllOrThrow: ") + e.what());
79 }
80 if (n == 0) throw std::runtime_error("rawtcp::sendAllOrThrow: send failed");
81 sent += n;
82 }
83}
84inline void sendAllOrThrow(netplus::socket &sock, const std::string &s) {
85 sendAllOrThrow(sock, s.data(), s.size());
86}
87
88} // namespace rawtcp