/* * Copyright (C) 2017 Jianhui Zhao * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #define port "8000" static void hello_action(struct uh_client *cl) { int body_len = 0; cl->send_header(cl, 200, "OK", -1); cl->append_header(cl, "Myheader", "Hello"); cl->header_end(cl); cl->chunk_printf(cl, "

Hello Libuhttpd %s

", UHTTPD_VERSION_STRING); cl->chunk_printf(cl, "

REMOTE_ADDR: %s

", cl->get_peer_addr(cl)); cl->chunk_printf(cl, "

PATH: %s

", cl->get_path(cl)); cl->chunk_printf(cl, "

QUERY: %s

", cl->get_query(cl)); cl->chunk_printf(cl, "

BODY:%s

", cl->get_body(cl, &body_len)); 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; }