HTTP "hello, world!"
This commit is contained in:
@@ -62,6 +62,14 @@ target_link_libraries(test_cas
|
|||||||
vitastor_client
|
vitastor_client
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# http_hello
|
||||||
|
add_executable(http_hello
|
||||||
|
http_hello.cpp
|
||||||
|
)
|
||||||
|
target_link_libraries(http_hello
|
||||||
|
vitastor_client
|
||||||
|
)
|
||||||
|
|
||||||
# test_crc32
|
# test_crc32
|
||||||
add_executable(test_crc32
|
add_executable(test_crc32
|
||||||
test_crc32.cpp
|
test_crc32.cpp
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <netinet/tcp.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user