Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/netkit-c/sock/sync_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ NETKIT_C_API netkit_recv_result_t* netkit_recv_result_create(void);
NETKIT_C_API void netkit_recv_result_destroy(netkit_recv_result_t* recv_result);

NETKIT_C_API netkit_recv_status_t netkit_sync_sock_recv(netkit_sync_sock_t* sock, netkit_recv_result_t* out, int timeout_seconds, const char* match, size_t eof);
NETKIT_C_API netkit_recv_status_t netkit_sync_sock_basic_recv(netkit_sync_sock_t* sock, netkit_recv_result_t* out);
NETKIT_C_API void netkit_sync_sock_close(netkit_sync_sock_t* sock);
NETKIT_C_API netkit_sock_addr_t* netkit_sync_sock_get_peer(netkit_sync_sock_t* sock);

Expand Down
499 changes: 251 additions & 248 deletions include/netkit/http/request_handler.hpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion include/netkit/sock/basic_sync_sock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace netkit::sock {
[[nodiscard]] virtual recv_result recv(int timeout_seconds, const std::string& match) = 0;
[[nodiscard]] virtual recv_result recv(int timeout_seconds, const std::string& match, size_t eof) = 0;
[[nodiscard]] virtual recv_result recv(int timeout_seconds, size_t eof) = 0;
[[nodiscard]] virtual recv_result primitive_recv() = 0;
[[nodiscard]] virtual recv_result recv() = 0;
[[nodiscard]] virtual std::string overflow_bytes() const = 0;
virtual addr& get_addr() = 0;
[[nodiscard]] virtual const addr& get_addr() const = 0;
Expand Down
7 changes: 6 additions & 1 deletion include/netkit/sock/sync_sock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ namespace netkit::sock {
* @return The received data as a sock_recv_result object.
*/
[[nodiscard]] recv_result recv(int timeout_seconds, const std::string& match, size_t eof) override;
[[nodiscard]] recv_result primitive_recv() override;
/**
* @brief Receive data from the server.
* @note This is a recv() implementation that behaves like a Unix recv() call would.
* @return The received data as a sock_recv_result object.
*/
[[nodiscard]] recv_result recv() override;

/* @brief Receive data from the server.
* @param timeout_seconds The timeout in seconds (-1 means wait indefinitely).
Expand Down
32 changes: 32 additions & 0 deletions src/c/sock/sync_sock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,38 @@ extern "C" NETKIT_C_API netkit_recv_status_t netkit_sync_sock_recv(netkit_sync_s
}
}

extern "C" NETKIT_C_API netkit_recv_status_t netkit_sync_sock_basic_recv(netkit_sync_sock_t* sock, netkit_recv_result_t* out) {
if (!sock || !out) {
return RECV_ERROR;
}

try {
auto ret = sock->impl->recv();
delete[] out->data;

out->size = ret.data.size();
out->data = new char[out->size + 1];

std::memcpy(out->data, ret.data.data(), out->size);
out->data[out->size] = '\0';

switch (ret.status) {
case netkit::sock::recv_status::success:
return RECV_SUCCESS;
case netkit::sock::recv_status::timeout:
return RECV_TIMEOUT;
case netkit::sock::recv_status::closed:
return RECV_CLOSED;
default:
return RECV_ERROR;
}
} catch (const std::exception& e) {
return RECV_ERROR;
} catch (...) {
return RECV_ERROR;
}
}

extern "C" NETKIT_C_API void netkit_sync_sock_close(netkit_sync_sock_t* sock) {
if (!sock) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/sock/openssl/ssl_sync_sock_openssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ void netkit::sock::ssl_sync_sock::drain_write_bio() const {
}

void netkit::sock::ssl_sync_sock::feed_read_bio_blocking() const {
auto res = underlying_sock_->primitive_recv();
auto res = underlying_sock_->recv();

if (res.status == sock::recv_status::closed) {
if (!handshake_complete_) {
Expand Down
6 changes: 3 additions & 3 deletions src/sock/sync_sock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ netkit::sock::recv_result netkit::sock::sync_sock::recv(const int timeout_second
}
}

netkit::sock::recv_result netkit::sock::sync_sock::primitive_recv() {
netkit::sock::recv_result netkit::sock::sync_sock::recv() {
for (;;) {
char buf[8192];
ssize_t n = ::recv(this->sockfd, buf, sizeof(buf), 0);
Expand Down Expand Up @@ -619,7 +619,7 @@ netkit::sock::recv_result netkit::sock::sync_sock::recv(const int timeout_second
}
}

netkit::sock::recv_result netkit::sock::sync_sock::primitive_recv() {
netkit::sock::recv_result netkit::sock::sync_sock::recv() {
constexpr size_t buffer_size = 8192;
char buf[buffer_size];

Expand Down Expand Up @@ -679,4 +679,4 @@ void netkit::sock::sync_sock::close() {
#endif
[[nodiscard]] netkit::sock::addr netkit::sock::sync_sock::get_peer() const {
return netkit::sock::get_peer(this->sockfd);
}
}
Loading