Simple example using libmicrohttpd
This commit is contained in:
62
src/main.c
Normal file
62
src/main.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user