libuhttpd/example/helloworld.c

46 lines
1.0 KiB
C
Raw Normal View History

#include <uhttpd.h>
#define port "8000"
static void hello_action(struct uh_client *cl)
{
cl->send_header(cl, 200, "OK", -1);
cl->append_header(cl, "Myheader", "Hello");
cl->header_end(cl);
cl->chunk_printf(cl, "<h1>Hello Libuhttpd %s</h1>", UHTTPD_VERSION_STRING);
cl->chunk_printf(cl, "<h1>REMOTE_ADDR: %s</h1>", cl->get_peer_addr(cl));
cl->chunk_printf(cl, "<h1>PATH: %s</h1>", cl->get_path(cl));
cl->chunk_printf(cl, "<h1>QUERY: %s</h1>", cl->get_query(cl));
cl->request_done(cl);
}
int main(int argc, char **argv)
{
struct uh_server *srv = NULL;
uh_log_debug("libuhttpd version: %s", UHTTPD_VERSION_STRING);
uloop_init();
srv = uh_server_new("0.0.0.0", port);
if (!srv)
goto done;
uh_log_debug("Listen on: *:%s", port);
#if (UHTTPD_SSL_SUPPORT)
if (srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0)
goto done;
#endif
uh_add_action(srv, "/hello", hello_action);
uloop_run();
done:
uloop_done();
srv->free(srv);
return 0;
}