+/*---------------------------------------------------------
+ | http server
+ | Handles the HTTP server
+ +--------------------------------------------------------- */
+static int init_http_server(struct afb_hsrv *hsrv, struct afb_config * config)
+{
+ int idx, dfd;
+
+ dfd = afb_common_rootdir_get_fd();
+
+ if (!afb_hsrv_add_handler(hsrv, config->rootapi, afb_hswitch_websocket_switch, NULL, 20))
+ return 0;
+
+ if (!afb_hsrv_add_handler(hsrv, config->rootapi, afb_hswitch_apis, NULL, 10))
+ return 0;
+
+ for (idx = 0; idx < config->aliascount; idx++)
+ if (!afb_hsrv_add_alias (hsrv, config->aliasdir[idx].url, dfd, config->aliasdir[idx].path, 0, 0))
+ return 0;
+
+ if (!afb_hsrv_add_alias(hsrv, "", dfd, config->roothttp, -10, 1))
+ return 0;
+
+ if (!afb_hsrv_add_handler(hsrv, config->rootbase, afb_hswitch_one_page_api_redirect, NULL, -20))
+ return 0;
+
+ return 1;
+}
+
+static struct afb_hsrv *start_http_server(struct afb_config * config)
+{
+ int rc;
+ struct afb_hsrv *hsrv;
+
+ if (afb_hreq_init_download_path("/tmp")) { /* TODO: sessiondir? */
+ ERROR("unable to set the tmp directory");
+ return NULL;
+ }
+
+ hsrv = afb_hsrv_create();
+ if (hsrv == NULL) {
+ ERROR("memory allocation failure");
+ return NULL;
+ }
+
+ if (!afb_hsrv_set_cache_timeout(hsrv, config->cacheTimeout)
+ || !init_http_server(hsrv, config)) {
+ ERROR("initialisation of httpd failed");
+ afb_hsrv_put(hsrv);
+ return NULL;
+ }
+
+ NOTICE("Waiting port=%d rootdir=%s", config->httpdPort, config->rootdir);
+ NOTICE("Browser URL= http:/*localhost:%d", config->httpdPort);
+
+ rc = afb_hsrv_start(hsrv, (uint16_t) config->httpdPort, 15);
+ if (!rc) {
+ ERROR("starting of httpd failed");
+ afb_hsrv_put(hsrv);
+ return NULL;
+ }
+
+ return hsrv;
+}
+
+static void start_items(struct afb_config_item *item)
+{
+ if (item != NULL) {
+ /* keeps the order */
+ start_items(item->previous);
+ switch(item->kind) {
+ case DBUS_CLIENT:
+ if (afb_api_dbus_add_client(item->value) < 0) {
+ ERROR("can't start the afb-dbus client of path %s",item->value);
+ exit(1);
+ }
+ break;
+ case DBUS_SERVICE:
+ if (afb_api_dbus_add_server(item->value) < 0) {
+ ERROR("can't start the afb-dbus service of path %s",item->value);
+ exit(1);
+ }
+ break;
+ case WS_CLIENT:
+ if (afb_api_ws_add_client(item->value) < 0) {
+ ERROR("can't start the afb-websocket client of path %s",item->value);
+ exit(1);
+ }
+ break;
+ case WS_SERVICE:
+ if (afb_api_ws_add_server(item->value) < 0) {
+ ERROR("can't start the afb-websocket service of path %s",item->value);
+ exit(1);
+ }
+ break;
+ case SO_BINDING:
+ if (afb_api_so_add_binding(item->value) < 0) {
+ ERROR("can't start the binding of path %s",item->value);
+ exit(1);
+ }
+ break;
+ default:
+ ERROR("unexpected internal error");
+ exit(1);
+ }
+ /* frre the item */
+ free(item);
+ }