altos-mapd: Handle clients failing to send valid JSON
authorKeith Packard <keithp@keithp.com>
Sun, 7 Oct 2018 17:03:31 +0000 (10:03 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 7 Oct 2018 17:03:31 +0000 (10:03 -0700)
Just bail if the data we receive doesn't generate a json value.

Signed-off-by: Keith Packard <keithp@keithp.com>
map-server/altos-mapd/AltosMapdClient.java

index 0983336..1917408 100644 (file)
@@ -69,61 +69,66 @@ public class AltosMapdClient extends Thread implements AltosMapStoreListener {
                try {
                        request = AltosJson.fromInputStream(socket.getInputStream());
 
-                       double  lat = request.get_double("lat", AltosLib.MISSING);
-                       double  lon = request.get_double("lon", AltosLib.MISSING);
-                       int     zoom = request.get_int("zoom", AltosLib.MISSING);
-                       String  addr = request.get_string("remote_addr", null);
-
-                       if (lat == AltosLib.MISSING ||
-                           lon == AltosLib.MISSING ||
-                           zoom == AltosLib.MISSING ||
-                           addr == null)
-                       {
+                       if (request == null) {
                                set_status(400);
-                       } else if (!AltosMapd.check_lat_lon(lat, lon)) {
-                               set_status(403);        /* Forbidden */
+                               System.out.printf("client failed %d\n", http_status);
                        } else {
 
-                               store_ready = new Semaphore(0);
+                               double  lat = request.get_double("lat", AltosLib.MISSING);
+                               double  lon = request.get_double("lon", AltosLib.MISSING);
+                               int     zoom = request.get_int("zoom", AltosLib.MISSING);
+                               String  addr = request.get_string("remote_addr", null);
 
-                               AltosMapStore   map_store = AltosMapStore.get(new AltosLatLon(lat, lon),
-                                                                             zoom,
-                                                                             AltosMapd.maptype,
-                                                                             AltosMapd.px_size,
-                                                                             AltosMapd.scale);
-                               int status;
-
-                               if (map_store == null) {
-                                       status = AltosMapTile.failed;
+                               if (lat == AltosLib.MISSING ||
+                                   lon == AltosLib.MISSING ||
+                                   zoom == AltosLib.MISSING ||
+                                   addr == null)
+                               {
+                                       set_status(400);
+                               } else if (!AltosMapd.check_lat_lon(lat, lon)) {
+                                       set_status(403);        /* Forbidden */
                                } else {
-                                       map_store.add_listener(this);
 
-                                       try {
-                                               store_ready.acquire();
-                                       } catch (Exception ie) {
-                                       }
+                                       store_ready = new Semaphore(0);
 
-                                       status = map_store.status();
-                               }
+                                       AltosMapStore   map_store = AltosMapStore.get(new AltosLatLon(lat, lon),
+                                                                                     zoom,
+                                                                                     AltosMapd.maptype,
+                                                                                     AltosMapd.px_size,
+                                                                                     AltosMapd.scale);
+                                       int status;
 
-                               if (status == AltosMapTile.fetched || status == AltosMapTile.loaded) {
-                                       set_status(200);
-                                       set_file(map_store.file);
-                               } else if (status == AltosMapTile.failed) {
-                                       set_status(404);
-                               } else if (status == AltosMapTile.fetching) {
-                                       set_status(408);
-                               } else if (status == AltosMapTile.bad_request) {
-                                       set_status(400);
-                               } else if (status == AltosMapTile.forbidden) {
-                                       set_status(403);
-                               } else {
-                                       set_status(400);
+                                       if (map_store == null) {
+                                               status = AltosMapTile.failed;
+                                       } else {
+                                               map_store.add_listener(this);
+
+                                               try {
+                                                       store_ready.acquire();
+                                               } catch (Exception ie) {
+                                               }
+
+                                               status = map_store.status();
+                                       }
+
+                                       if (status == AltosMapTile.fetched || status == AltosMapTile.loaded) {
+                                               set_status(200);
+                                               set_file(map_store.file);
+                                       } else if (status == AltosMapTile.failed) {
+                                               set_status(404);
+                                       } else if (status == AltosMapTile.fetching) {
+                                               set_status(408);
+                                       } else if (status == AltosMapTile.bad_request) {
+                                               set_status(400);
+                                       } else if (status == AltosMapTile.forbidden) {
+                                               set_status(403);
+                                       } else {
+                                               set_status(400);
+                                       }
                                }
+                               System.out.printf("%s: %.6f %.6f %d status %d\n",
+                                                 addr, lat, lon, zoom, http_status);
                        }
-                       System.out.printf("%s: %.6f %.6f %d status %d\n",
-                                         addr, lat, lon, zoom, http_status);
-
                } catch (Exception e) {
                        System.out.printf("client exception %s\n", e.toString());
                        e.printStackTrace(System.out);