+ hsrv->httpd = httpd;
+ hsrv->upoll = upoll;
+ return 1;
+}
+
+void _afb_hsrv_stop(struct afb_hsrv *hsrv)
+{
+ if (hsrv->upoll)
+ upoll_close(hsrv->upoll);
+ hsrv->upoll = NULL;
+ if (hsrv->httpd != NULL)
+ MHD_stop_daemon(hsrv->httpd);
+ hsrv->httpd = NULL;
+}
+
+struct afb_hsrv *afb_hsrv_create()
+{
+ struct afb_hsrv *result = calloc(1, sizeof(struct afb_hsrv));
+ if (result != NULL)
+ result->refcount = 1;
+ return result;
+}
+
+void afb_hsrv_put(struct afb_hsrv *hsrv)
+{
+ assert(hsrv->refcount != 0);
+ if (!--hsrv->refcount) {
+ _afb_hsrv_stop(hsrv);
+ free(hsrv);
+ }
+}
+
+static int my_default_init(struct afb_hsrv *hsrv, AFB_session * session)
+{
+ int idx;
+
+ if (!afb_hsrv_add_handler(hsrv, session->config->rootapi, afb_hreq_websocket_switch, NULL, 20))
+ return 0;
+
+ if (!afb_hsrv_add_handler(hsrv, 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 (hsrv, session->config->aliasdir[idx].url, session->config->aliasdir[idx].path, 0))
+ return 0;
+
+ if (!afb_hsrv_add_alias(hsrv, "", session->config->rootdir, -10))
+ return 0;
+
+ if (!afb_hsrv_add_handler(hsrv, session->config->rootbase, afb_hreq_one_page_api_redirect, NULL, -20))
+ return 0;
+
+ return 1;
+}
+
+int afb_hsrv_start(AFB_session * session)
+{
+ int rc;
+ struct afb_hsrv *hsrv;
+
+ hsrv = afb_hsrv_create();
+ if (hsrv == NULL) {
+ fprintf(stderr, "memory allocation failure\n");
+ return 0;
+ }
+
+ if (!afb_hsrv_set_cache_timeout(hsrv, session->config->cacheTimeout)
+ || !my_default_init(hsrv, session)) {
+ printf("Error: initialisation of httpd failed");
+ afb_hsrv_put(hsrv);
+ return 0;
+ }
+
+ if (verbosity) {
+ printf("AFB:notice Waiting port=%d rootdir=%s\n", session->config->httpdPort, session->config->rootdir);
+ printf("AFB:notice Browser URL= http:/*localhost:%d\n", session->config->httpdPort);
+ }
+
+ rc = _afb_hsrv_start(hsrv, (uint16_t) session->config->httpdPort, 15);
+ if (!rc)
+ return 0;
+
+ session->hsrv = hsrv;