Add more Google Cast specific behavior.

/apps/ will return 204 if no DIAL apps are running.
/apps/ will return 302 if a DIAL app is running, with Location
set to the app status URL.
Implement DELETE request method for /apps/<app_name> requests.

Change-Id: I620aff3b350694a98da2f6ba9e1f78cf6bd3d303
diff --git a/src/server/dial_server.c b/src/server/dial_server.c
index d31c76b..21f09c6 100644
--- a/src/server/dial_server.c
+++ b/src/server/dial_server.c
@@ -88,6 +88,23 @@
     return ret;
 }
 
+// Returns a pointer to the currently running app, or NULL if nothing is
+// running.
+static DIALApp **get_current_app(DIALServer *ds) {
+    DIALApp *app;
+    DIALApp **ret = &ds->apps;
+    int canStop;
+
+    for (app = ds->apps; app != NULL; ret = &app->next, app = app->next) {
+        app->state = app->callbacks.status_cb(ds, app->name, app->run_id,
+                                              &canStop, app->callback_data);
+        if (app->state == kDIALStatusRunning) {
+            break;
+        }
+    }
+    return ret;
+}
+
 static void url_decode_xml_encode(char *dst, char *src, size_t src_size) {
     char *url_decoded_key = (char *) malloc(src_size + 1);
     urldecode(url_decoded_key, src, src_size);
@@ -193,6 +210,31 @@
     DIALServer *ds = request_info->user_data;
 
     ds_lock(ds);
+    if (strlen(app_name) == 0) {
+        app = *get_current_app(ds);
+        if (app == NULL) {
+            mg_printf(
+                conn,
+                "HTTP/1.1 204 No Content\r\n"
+                "Access-Control-Allow-Origin: %s\r\n"
+                "Content-Length: 0"
+                "\r\n",
+                origin_header);
+        } else {
+            mg_printf(
+                conn,
+                "HTTP/1.1 302 Found\r\n"
+                "Access-Control-Allow-Origin: %s\r\n"
+                "Location: %s%s\r\n"
+                "\r\n",
+                origin_header,
+                request_info->uri,
+                app->name);
+        }
+        ds_unlock(ds);
+        return;
+    }
+
     app = *find_app(ds, app_name);
     if (!app) {
         mg_send_http_error(conn, 404, "Not Found", "Not Found");
@@ -481,6 +523,8 @@
                 // get app status
             } else if (!strcmp(request_info->request_method, "GET")) {
                 handle_app_status(conn, request_info, app_name, origin_header);
+            } else if (!strcmp(request_info->request_method, "DELETE")) {
+                handle_app_stop(conn, request_info, app_name, origin_header);
             } else {
                 mg_send_http_error(conn, 501, "Not Implemented",
                                    "Not Implemented");