# Include <event2/event. h>
# Include <event2/buffer. h>
# Include <event2/http. h>
# Include <Winsock2.h>
# Include <stdlib. h>
# Include <stdio. h>
Int init_win_socket ()
{
WSADATA wsaData;
If (WSAStartup (MAKEWORD (2, 2), & wsaData )! = 0)
{
Return-1;
}
Return 0;
}
Void generic_handler (struct evhttp_request * req, void * arg)
{
Struct evbuffer * buf = evbuffer_new ();
If (! Buf)
{
Puts ("failed to create response buffer \ n ");
Return;
}
Evbuffer_add_printf (buf, "Server Responsed. Requested: % s \ n", evhttp_request_get_uri (req ));
Evhttp_send_reply (req, HTTP_ OK, "OK", buf );
Evbuffer_free (buf );
}
Int main (int argc, char * argv [])
{
# Ifdef WIN32
Init_win_socket ();
# Endif
Short http_port = 8081;
Char * http_addr = "127.0.0.1 ";
Struct event_base * base = event_base_new ();
Struct evhttp * http_server = evhttp_new (base );
If (! Http_server)
{
Return-1;
}
Int ret = evhttp_bind_socket (http_server, http_addr, http_port );
If (ret! = 0)
{
Return-1;
}
Evhttp_set_gencb (http_server, generic_handler, NULL );
Printf ("http server start OK! \ N ");
Event_base_dispatch (base );
Evhttp_free (http_server );
WSACleanup ();
Return 0;
}