Handlers in another function

This commit is contained in:
Senad Uka
2022-11-19 23:18:44 +01:00
parent faebd267ca
commit ee1eb89e9a
6 changed files with 77 additions and 44 deletions

View File

@@ -5,13 +5,18 @@
CC = gcc CC = gcc
CFLAGS = -Wall -g -O2 CFLAGS = -Wall -g -O2
LDFLAGS = -lmicrohttpd LDFLAGS = -lmicrohttpd
OBJFILES = main.o handlers.o
DEPS = handlers.h
all: nasuh-server all: nasuh-server
nasuh-server: main.c %.o: %.c $(DEPS)
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(CC) -c -o $@ $< $(CFLAGS)
nasuh-server: $(OBJFILES)
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
clean: clean:
rm -f nasuh-server rm -f nasuh-server *.o
# end of Makefile # end of Makefile

41
handlers.c Normal file
View File

@@ -0,0 +1,41 @@
//
// Created by hamo on 11/19/22.
//
#include "handlers.h"
enum MHD_Result
handle_all(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 ** ptr) {
static int dummy;
const char * page = cls;
struct MHD_Response * response;
int ret;
if (0 != strcmp(method, "GET"))
return MHD_NO; /* unexpected method */
if (&dummy != *ptr)
{
/* The first time only the headers are valid,
do not respond in the first round... */
*ptr = &dummy;
return MHD_YES;
}
if (0 != *upload_data_size)
return MHD_NO; /* upload data in a GET!? */
*ptr = NULL; /* clear context pointer */
response = MHD_create_response_from_buffer (strlen(page),
(void*) page,
MHD_RESPMEM_PERSISTENT);
ret = MHD_queue_response(connection,
MHD_HTTP_OK,
response);
MHD_destroy_response(response);
return ret;
}

25
handlers.h Normal file
View File

@@ -0,0 +1,25 @@
//
// Created by hamo on 11/19/22.
//
#include <microhttpd.h>
#include <stdlib.h>
#include <string.h>
#ifndef NASUH_HANDLERS_H
#define NASUH_HANDLERS_H
#define PAGE "<html><head><title>nasuh search engine</title>"\
"</head><body>all your programming needs</body></html>"
enum MHD_Result
handle_all(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 ** ptr);
#endif //NASUH_HANDLERS_H

BIN
handlers.o Normal file

Binary file not shown.

44
main.c
View File

@@ -1,46 +1,8 @@
#include <microhttpd.h> #include <microhttpd.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#define PAGE "<html><head><title>nasuh search engine</title>"\ #include "handlers.h"
"</head><body>all your programming needs</body></html>"
static enum MHD_Result
ahc_echo(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 ** ptr) {
static int dummy;
const char * page = cls;
struct MHD_Response * response;
int ret;
if (0 != strcmp(method, "GET"))
return MHD_NO; /* unexpected method */
if (&dummy != *ptr)
{
/* The first time only the headers are valid,
do not respond in the first round... */
*ptr = &dummy;
return MHD_YES;
}
if (0 != *upload_data_size)
return MHD_NO; /* upload data in a GET!? */
*ptr = NULL; /* clear context pointer */
response = MHD_create_response_from_buffer (strlen(page),
(void*) page,
MHD_RESPMEM_PERSISTENT);
ret = MHD_queue_response(connection,
MHD_HTTP_OK,
response);
MHD_destroy_response(response);
return ret;
}
int main(int argc, int main(int argc,
char ** argv) { char ** argv) {
@@ -54,7 +16,7 @@ int main(int argc,
atoi(argv[1]), atoi(argv[1]),
NULL, NULL,
NULL, NULL,
&ahc_echo, &handle_all,
PAGE, PAGE,
MHD_OPTION_END); MHD_OPTION_END);
if (d == NULL) if (d == NULL)

BIN
main.o Normal file

Binary file not shown.