add http server

This commit is contained in:
SimonFJ20 2026-03-20 13:15:03 +01:00
parent c1f4f75fec
commit 7222da0c74
3 changed files with 89 additions and 5 deletions

View File

@ -2,4 +2,6 @@
- [Notion - H5](https://mercantec.notion.site/h5-2026) - [Notion - H5](https://mercantec.notion.site/h5-2026)
- [ESP32-S3 Get started](https://docs.espressif.com/projects/esp-idf/en/stable/esp32s3/get-started/index.html) - [ESP32-S3 Get started](https://docs.espressif.com/projects/esp-idf/en/stable/esp32s3/get-started/index.html)
- [libmicrohttpd](https://www.gnu.org/software/libmicrohttpd/manual/libmicrohttpd.html)
- [libmicrohttpd tutorial](https://www.gnu.org/software/libmicrohttpd/tutorial.html)

View File

@ -7,10 +7,25 @@ LD=gcc
CFLAGS = -std=c23 -pedantic-errors -Wall -Wextra -Wconversion CFLAGS = -std=c23 -pedantic-errors -Wall -Wextra -Wconversion
LDFLAGS = LDFLAGS =
CFLAGS+=-g libraries = libmicrohttpd
# CFLAGS+=-ggdb CFLAGS += $(shell pkg-config $(libraries) --cflags)
LDFLAGS += $(shell pkg-config $(libraries) --libs)
RELEASE = 0
GDB = 0
ifeq ($(RELEASE),1)
CFLAGS += -O3
else
CFLAGS += -g -Wno-unused
ifeq ($(GDB),1)
CFLAGS += -ggdb
else
CFLAGS += -fsanitize=address CFLAGS += -fsanitize=address
endif
endif
build_dir = build build_dir = build
obj_dir = $(build_dir)/obj obj_dir = $(build_dir)/obj

View File

@ -1,7 +1,74 @@
#include <string.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <microhttpd.h>
#include <stdio.h> #include <stdio.h>
#define PORT 8888
int print_out_key(
void *cls,
enum MHD_ValueKind kind,
const char *key,
const char *value)
{
printf ("%s: %s\n", key, value);
return MHD_YES;
}
int answer_to_connection(
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 **req_cls)
{
const char *page = "<html><body>Hello, browser!</body></html>";
struct MHD_Response *response;
int ret;
MHD_get_connection_values(
connection,
MHD_HEADER_KIND,
(MHD_KeyValueIterator)&print_out_key,
NULL);
printf ("New %s request for %s using version %s\n", method, url, version);
response = MHD_create_response_from_buffer(
strlen(page),
(void*)page,
MHD_RESPMEM_PERSISTENT);
ret = (int)MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
return ret;
}
int main(void) int main(void)
{ {
printf("hello\n"); printf("hello\n");
struct MHD_Daemon *daemon;
daemon = MHD_start_daemon(
MHD_USE_INTERNAL_POLLING_THREAD,
PORT,
NULL,
NULL,
(MHD_AccessHandlerCallback)&answer_to_connection,
NULL,
MHD_OPTION_END);
if (NULL == daemon) return 1;
getchar();
MHD_stop_daemon(daemon);
return 0;
} }