Procster  0.1
Procster-ViewProcessesovertheweb
miniserver.c File Reference

Small Framework to make MHD based server side develpment more bearable. More...

#include <microhttpd.h>
#include <jansson.h>
#include "proclister.h"
#include <fcntl.h>
#include <sys/stat.h>
Include dependency graph for miniserver.c:

Functions

int ms_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 **con_cls)
 Miniserver framework main request dispatcher. More...
 
static int ms_on_client_connect (void *cls, const struct sockaddr *addr, socklen_t addrlen)
 MHD Client authorization handler (Hooked as 3rd param to MHD_start_daemon()). More...
 
void ms_req_term_cb (void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
 MHD Post request handler. More...
 
miniserverserver_new (json_t *json)
 App: Instantiate server with basic settings (e.g. More...
 
void server_run (miniserver *s)
 App: Start Running Server and accepting requests. More...
 
void server_actions_load (miniserver *s, action *actarr, int cnt)
 App: Load actions from C Array of actions. More...
 
requestreq_new (miniserver *s, const char *url, const char *method)
 Allocate and initialize request. More...
 
void req_free (request *req)
 Free request and its members. More...
 
responseres_new (action *act)
 
void res_free (response *res)
 
actionaction_find (miniserver *s, const char *url)
 Lookup an action (during dispatching). More...
 
void res_set_str_cont (response *res, char *contstr)
 Set string content as content to respond with. More...
 
void res_set_conttype (response *res, char *conttype)
 
struct MHD_Response * req_trystatic (request *req, response *res)
 Try to resolve url to a static file and return response for it. More...
 

Detailed Description

Small Framework to make MHD based server side develpment more bearable.

Notes on response content allocation

MHD has following enums to hint memory management strategy in microhttpd.h

  • 0 - MHD_RESPMEM_PERSISTENT global/static, wont change (NOT stack). NOT to be free()d
  • 1 - MHD_RESPMEM_MUST_FREE - heap allocated (e.g. by malloc()), should be free()d by MHD
  • 2 - MHD_RESPMEM_MUST_COPY - transient/stack allocated (NON-heap). MHD will make a copy (and free copy)

    MHD Doc on run modes: "Thread modes and even loops"

These are extracted notes from documenation (See first flag param to MHD_start_daemon(flags, ...), see also enum MHD_FLAG). For list of flags in documenation "2 Constants". What is this: MHD_USE_SELECT_INTERNALLY

  • external sockets polling (no threads) - DEFAULT (because simple /universal ?)
  • internal polling ("one thread for all connections" - sounds ambiguous .. one of each or one for all) MHD_USE_INTERNAL_POLLING_THREAD=8
  • polling in thread pool (... used to process all connections)
  • thread-per-connection ( 1 thr for listen sockets and then 1 per accepted conn.) MHD_USE_THREAD_PER_CONNECTION=4 (Reqs: MHD_USE_INTERNAL_POLLING_THREAD.)

Earlier modes must be combined with event loop styles: select, poll and epoll (epoll and poll may not be available on some platforms). Are these: MHD_USE_SELECT_INTERNALLY, MHD_USE_POLL, MHD_USE_EPOLL

Forking to BG

Other web servers:

Function Documentation

◆ action_find()

action* action_find ( miniserver s,
const char *  url 
)

Lookup an action (during dispatching).

Parameters
s- Server
url- URL by which action node should be looked up
Returns
Action node matched or NULL for no action matching passed URL.

References miniserver::actions, and action::url.

Referenced by main(), and ms_answer_to_connection().

◆ ms_answer_to_connection()

int ms_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 **  con_cls 
)

◆ ms_on_client_connect()

static int ms_on_client_connect ( void *  cls,
const struct sockaddr *  addr,
socklen_t  addrlen 
)
static

MHD Client authorization handler (Hooked as 3rd param to MHD_start_daemon()).

Gets called twice per request ? No. But multiple requests can be batched to go through Access check first.

References MHD_YES.

Referenced by server_run().

◆ ms_req_term_cb()

void ms_req_term_cb ( void *  cls,
struct MHD_Connection *  connection,
void **  con_cls,
enum MHD_RequestTerminationCode  toe 
)

MHD Post request handler.

  • Release miniserver request req,res
  • Mark con_cls as NULL

References req_free(), request::res, and res_free().

Referenced by server_run().

◆ req_free()

void req_free ( request req)

Free request and its members.

References request::method, and request::url.

Referenced by ms_req_term_cb().

◆ req_new()

request* req_new ( miniserver s,
const char *  url,
const char *  method 
)

Allocate and initialize request.

References request::method, request::ms, and request::url.

Referenced by ms_answer_to_connection().

◆ req_trystatic()

struct MHD_Response* req_trystatic ( request req,
response res 
)

Try to resolve url to a static file and return response for it.

Parameters
req- Request with URL to test for static content
res- Response to set possible http return codes to (in res->code)
Returns
MHD_Response (pointer) for static file or NULL
Todo:
: Check suffix and try to map to appropriate mime-type

References response::code, miniserver::docroot, MHD_create_response_from_fd(), request::ms, and request::url.

Referenced by ms_answer_to_connection().

◆ res_free()

void res_free ( response res)

References response::conn, and response::conttype.

Referenced by ms_req_term_cb().

◆ res_new()

response* res_new ( action act)

◆ res_set_conttype()

void res_set_conttype ( response res,
char *  conttype 
)

References response::conttype.

Referenced by ms_answer_to_connection().

◆ res_set_str_cont()

void res_set_str_cont ( response res,
char *  contstr 
)

Set string content as content to respond with.

Length is figured out from sting. Binary data not allowed with this methos.

Parameters
res- Response (to which content is set)
contstr- Content string.

References response::cont, and response::contlen.

Referenced by ms_answer_to_connection().

◆ server_actions_load()

void server_actions_load ( miniserver s,
action actarr,
int  cnt 
)

App: Load actions from C Array of actions.

Parameters
s- Miniserver (to/for which actions will be loaded)
actarr- Array of actions (not action pointers) to add
cnt- Number of actions to add
Returns
none

References miniserver::actions, and action::url.

Referenced by main().

◆ server_new()

miniserver* server_new ( json_t json)

App: Instantiate server with basic settings (e.g.

docroot, port).

  • docroot
  • port
  • JSON indent/prettiness

References miniserver::debug, miniserver::docroot, docroot, miniserver::logfh, miniserver::logfname, miniserver::port, and miniserver::reqdebug.

Referenced by main().

◆ server_run()

void server_run ( miniserver s)

App: Start Running Server and accepting requests.

Parameters
s- Server
Returns
None

References ms_answer_to_connection(), ms_on_client_connect(), ms_req_term_cb(), and miniserver::port.

Referenced by main().