Handlers in another function
This commit is contained in:
11
Makefile
11
Makefile
@@ -5,13 +5,18 @@
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -g -O2
|
||||
LDFLAGS = -lmicrohttpd
|
||||
OBJFILES = main.o handlers.o
|
||||
DEPS = handlers.h
|
||||
|
||||
all: nasuh-server
|
||||
|
||||
nasuh-server: main.c
|
||||
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
|
||||
%.o: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
nasuh-server: $(OBJFILES)
|
||||
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f nasuh-server
|
||||
rm -f nasuh-server *.o
|
||||
|
||||
# end of Makefile
|
||||
|
||||
41
handlers.c
Normal file
41
handlers.c
Normal 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
25
handlers.h
Normal 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
BIN
handlers.o
Normal file
Binary file not shown.
44
main.c
44
main.c
@@ -1,46 +1,8 @@
|
||||
#include <microhttpd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define PAGE "<html><head><title>nasuh search engine</title>"\
|
||||
"</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;
|
||||
}
|
||||
#include <stdlib.h>
|
||||
#include "handlers.h"
|
||||
|
||||
int main(int argc,
|
||||
char ** argv) {
|
||||
@@ -54,7 +16,7 @@ int main(int argc,
|
||||
atoi(argv[1]),
|
||||
NULL,
|
||||
NULL,
|
||||
&ahc_echo,
|
||||
&handle_all,
|
||||
PAGE,
|
||||
MHD_OPTION_END);
|
||||
if (d == NULL)
|
||||
|
||||
Reference in New Issue
Block a user