diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index eed32975..92aa435b 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -62,6 +62,14 @@ target_link_libraries(test_cas vitastor_client ) +# http_hello +add_executable(http_hello + http_hello.cpp +) +target_link_libraries(http_hello + vitastor_client +) + # test_crc32 add_executable(test_crc32 test_crc32.cpp diff --git a/src/test/http_hello.cpp b/src/test/http_hello.cpp new file mode 100644 index 00000000..7de17717 --- /dev/null +++ b/src/test/http_hello.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "http_client.h" +#include "ringloop.h" +#include "epoll_manager.h" +#include "addr_util.h" + +int main(int narg, char *args[]) +{ + ring_consumer_t looper; + ring_loop_t *ringloop = new ring_loop_t(RINGLOOP_DEFAULT_SIZE); + epoll_manager_t *epmgr = new epoll_manager_t(ringloop); + // Accept new connections + int listen_fd = create_and_bind_socket("0.0.0.0", 8085, 128, NULL); + fcntl(listen_fd, F_SETFL, fcntl(listen_fd, F_GETFL, 0) | O_NONBLOCK); + std::string error; + auto http_ctx = http_context_init(epmgr->tfd, "", "", "", false, error); + epmgr->set_fd_handler(listen_fd, false, [http_ctx](int listen_fd, int events) + { + sockaddr_storage addr; + socklen_t peer_addr_size = sizeof(addr); + int peer_fd; + while ((peer_fd = accept(listen_fd, (sockaddr*)&addr, &peer_addr_size)) >= 0) + { + assert(peer_fd != 0); + fcntl(peer_fd, F_SETFL, fcntl(peer_fd, F_GETFL, 0) | O_NONBLOCK); + int one = 1; + setsockopt(peer_fd, SOL_TCP, TCP_NODELAY, &one, sizeof(one)); + auto co = http_init(http_ctx); + http_serve(co, peer_fd, (http_options_t){}, [co](http_message_t *msg) + { + if (msg->eof) + { + http_destroy(co); + return; + } + http_reply(co, "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Length: 13\r\n\r\nHello, world!"); + }); + } + }); + while (true) + { + ringloop->loop(); + ringloop->wait(); + } + delete epmgr; + delete ringloop; + return 0; +}