+static int access_handler(
+ void *cls,
+ struct MHD_Connection *connection,
+ const char *url,
+ const char *methodstr,
+ const char *version,
+ const char *upload_data,
+ size_t *upload_data_size,
+ void **recordreq)
+{
+ int rc;
+ struct afb_hreq *hreq;
+ enum afb_method method;
+ AFB_session *session;
+ struct afb_hsrv_handler *iter;
+ const char *type;
+
+ session = cls;
+ hreq = *recordreq;
+ if (hreq == NULL) {
+ /* create the request */
+ hreq = calloc(1, sizeof *hreq);
+ if (hreq == NULL)
+ goto internal_error;
+ *recordreq = hreq;
+
+ /* get the method */
+ method = get_method(methodstr);
+ method &= afb_method_get | afb_method_post;
+ if (method == afb_method_none)
+ goto bad_request;
+
+ /* init the request */
+ hreq->session = cls;
+ hreq->connection = connection;
+ hreq->method = method;
+ hreq->version = version;
+ hreq->tail = hreq->url = url;
+ hreq->lentail = hreq->lenurl = strlen(url);
+
+ /* init the post processing */
+ if (method == afb_method_post) {
+ type = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONTENT_TYPE);
+ if (type == NULL) {
+ /* an empty post, let's process it as a get */
+ hreq->method = afb_method_get;
+ } else if (strcasestr(type, FORM_CONTENT) != NULL) {
+ hreq->postform = MHD_create_post_processor (connection, 65500, postproc, hreq);
+ if (hreq->postform == NULL)
+ goto internal_error;
+ } else if (strcasestr(type, JSON_CONTENT) == NULL) {
+ afb_hsrv_reply_error(connection, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE);
+ return MHD_YES;
+ }
+ }
+ }
+
+ /* process further data */
+ if (*upload_data_size) {
+ if (hreq->postform != NULL) {
+ if (!MHD_post_process (hreq->postform, upload_data, *upload_data_size))
+ goto internal_error;
+ } else {
+ if (!afb_hreq_post_add(hreq, NULL, upload_data, *upload_data_size))
+ goto internal_error;
+ }
+ *upload_data_size = 0;
+ return MHD_YES;
+ }
+
+ /* flush the data */
+ afb_hreq_post_end(hreq);
+ if (hreq->postform != NULL) {
+ rc = MHD_destroy_post_processor(hreq->postform);
+ hreq->postform = NULL;
+ if (rc == MHD_NO)
+ goto bad_request;
+ }
+
+ /* search an handler for the request */
+ iter = session->handlers;
+ while (iter) {
+ if (afb_hreq_unprefix(hreq, iter->prefix, iter->length)) {
+ if (iter->handler(hreq, iter->data))
+ return MHD_YES;
+ hreq->tail = hreq->url;
+ hreq->lentail = hreq->lenurl;
+ }
+ iter = iter->next;
+ }
+
+ /* no handler */
+ afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
+ return MHD_YES;
+
+bad_request:
+ afb_hsrv_reply_error(connection, MHD_HTTP_BAD_REQUEST);
+ return MHD_YES;
+
+internal_error:
+ afb_hsrv_reply_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR);
+ return MHD_YES;
+}
+
+/* Because of POST call multiple time requestApi we need to free POST handle here */
+static void end_handler(void *cls, struct MHD_Connection *connection, void **recordreq,
+ enum MHD_RequestTerminationCode toe)
+{
+ AFB_session *session;
+ struct afb_hreq *hreq;
+
+ session = cls;
+ hreq = *recordreq;
+
+ if (hreq != NULL) {
+ if (hreq->postform != NULL)
+ MHD_destroy_post_processor(hreq->postform);
+ afb_hreq_drop_data(hreq);
+ free(hreq);
+ }
+}
+
+static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
+{
+ return MHD_YES;
+}
+
+#if defined(USE_MAGIC_MIME_TYPE)
+
+#if !defined(MAGIC_DB)
+#define MAGIC_DB "/usr/share/misc/magic.mgc"
+#endif
+
+static int init_lib_magic (AFB_session *session)
+{
+ /* MAGIC_MIME tells magic to return a mime of the file, but you can specify different things */
+ if (verbose)
+ printf("Loading mimetype default magic database\n");
+
+ session->magic = magic_open(MAGIC_MIME_TYPE);
+ if (session->magic == NULL) {
+ fprintf(stderr,"ERROR: unable to initialize magic library\n");
+ return 0;
+ }
+
+ /* Warning: should not use NULL for DB [libmagic bug wont pass efence check] */
+ if (magic_load(session->magic, MAGIC_DB) != 0) {
+ fprintf(stderr,"cannot load magic database - %s\n", magic_error(session->magic));
+ magic_close(session->magic);
+ session->magic = NULL;
+ return 0;
+ }
+
+ return 1;
+}
+#endif
+
+static int my_default_init(AFB_session * session)
+{
+ int idx;
+
+ if (!afb_hsrv_add_handler(session, session->config->rootapi, afb_hreq_websocket_switch, NULL, 20))
+ return 0;
+
+ if (!afb_hsrv_add_handler(session, session->config->rootapi, afb_hreq_rest_api, NULL, 10))
+ return 0;
+
+ for (idx = 0; session->config->aliasdir[idx].url != NULL; idx++)
+ if (!afb_hsrv_add_alias (session, session->config->aliasdir[idx].url, session->config->aliasdir[idx].path, 0))
+ return 0;
+
+ if (!afb_hsrv_add_alias(session, "", session->config->rootdir, -10))
+ return 0;
+
+ if (!afb_hsrv_add_handler(session, session->config->rootbase, afb_hreq_one_page_api_redirect, NULL, -20))
+ return 0;
+
+ return 1;
+}