Simple example using libmicrohttpd

This commit is contained in:
2026-05-01 16:00:53 +02:00
parent b0658f594b
commit 8bb4fb1ab7
11 changed files with 19939 additions and 0 deletions

15
Makefile Normal file
View File

@@ -0,0 +1,15 @@
CFLAGS=-Wall -Wextra
LIBWS=./vendor/libmicrohttpd/
LIBWS_LIB=$(LIBWS)lib/libmicrohttpd.a
LDFLAGS=-lm
INCL_FLAGS=-I$(LIBWS)include
main: src/main.c output
gcc $(CFLAGS) $(INCL_FLAGS) -o output/main src/main.c $(LIBWS_LIB) $(LDFLAGS)
output:
mkdir -p output

20
build.sh Executable file
View File

@@ -0,0 +1,20 @@
#!/usr/bin/env sh
if [ $1 = "build" ]; then
make main -B
elif [ $1 = "run" ]; then
make main
./output/main
elif [ $1 = "debug" ]; then
echo "Debugging"
elif [ $1 = "clean" ]; then
rm -rf ./output/
else
echo "Invalid command."
fi

62
src/main.c Normal file
View File

@@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <microhttpd.h>
static enum MHD_Result request_handler(
void *cls,
struct MHD_Connection *connection,
const char *url, const char *method, const char *version,
const char *upload_data, size_t *upload_data_size,
void **con_cls) {
(void)cls;
(void)url;
(void)method;
(void)version;
(void)upload_data;
(void)upload_data_size;
(void)con_cls;
// Create an HTTP response
const char *response_text = "Hello, World!";
struct MHD_Response *response = MHD_create_response_from_buffer(
strlen(response_text), (void *)response_text, MHD_RESPMEM_PERSISTENT);
if (!response) return MHD_NO;
// Send the response
printf("%s\n", response_text);
enum MHD_Result ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
return ret;
}
int main(void)
{
struct MHD_Daemon *server;
// Start the HTTP server
server = MHD_start_daemon(
MHD_USE_INTERNAL_POLLING_THREAD,
5000,
NULL, NULL,
&request_handler, NULL,
MHD_OPTION_END);
if (!server) {
fprintf(stderr, "Failed to start server\n");
return 1;
}
printf("Server is running on http://localhost:5000\n");
// Keep the server running
getchar();
// Stop the server
MHD_stop_daemon(server);
return 0;
}

1299
src/noh.h Normal file

File diff suppressed because it is too large Load Diff

6586
vendor/libmicrohttpd/include/microhttpd.h vendored Normal file

File diff suppressed because it is too large Load Diff

BIN
vendor/libmicrohttpd/lib/libmicrohttpd.a vendored Normal file

Binary file not shown.

21
vendor/libmicrohttpd/share/info/dir vendored Normal file
View File

@@ -0,0 +1,21 @@
This is the file .../info/dir, which contains the
topmost node of the Info hierarchy, called (dir)Top.
The first time you invoke Info you start off looking at this node.

File: dir, Node: Top This is the top of the INFO tree
This (the Directory node) gives a menu of major topics.
Typing "q" exits, "H" lists all Info commands, "d" returns here,
"h" gives a primer for first-timers,
"mEmacs<Return>" visits the Emacs manual, etc.
In Emacs, you can click mouse button 2 on a menu item or cross reference
to select it.
* Menu:
Software libraries
* libmicrohttpd: (libmicrohttpd).
Embedded HTTP server library.
* libmicrohttpdtutorial: (libmicrohttpd-tutorial).
A tutorial for GNU libmicrohttpd.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

View File

@@ -0,0 +1,46 @@
.Dd June 21, 2013
.Dt LIBMICROHTTPD 3
.Os
.Sh NAME
.Nm libmicrohttpd
.Nd library for embedding HTTP servers
.Sh LIBRARY
.ds doc-str-Lb-libmicrohttpd library for embedding HTTP servers (libmicrohttpd)
.Lb libmicrohttpd
.Sh SYNOPSIS
.In microhttpd.h
.Sh DESCRIPTION
GNU libmicrohttpd (short MHD) allows applications to easily integrate the functionality of a simple HTTP server. MHD is a GNU package.
.sp
The details of the API are described in comments in the header file, a detailed reference documentation in Texinfo, a tutorial, and in brief on the MHD webpage.
.Sh LEGAL NOTICE
libmicrohttpd is released under both the LGPL Version 2.1 or higher and the GNU GPL with eCos extension. For details on both licenses please read the respective appendix in the Texinfo manual.
.Sh FILES
.Bl -tag -width /etc/ttys -compact
.It Pa microhttpd.h
libmicrohttpd include file
.It Pa libmicrohttpd.so
libmicrohttpd library
.El
.Sh SEE ALSO
.Xr curl 1 ,
.Xr libcurl 3 ,
info libmicrohttpd
.Sh AUTHORS
GNU
.Nm
was originally designed by
.An -nosplit
.An Christian Grothoff Aq Mt christian@grothoff.org
and
.An Chris GauthierDickey Aq Mt chrisg@cs.du.edu Ns .
The original implementation was done by
.An Daniel Pittman Aq Mt depittman@gmail.com
and Christian Grothoff.
SSL/TLS support was added by Sagie Amir using code from GnuTLS. See the AUTHORS file in the distribution for a more detailed list of contributors.
.Sh AVAILABILITY
You can obtain the latest version from
.Lk https://www.gnu.org/software/libmicrohttpd/ Ns .
.Sh BUGS
Report bugs by using
.Lk https://bugs.gnunet.org/set_project.php?project_id=10 "Mantis" Ns .