aboutsummaryrefslogtreecommitdiffstats
path: root/tileserver/tileserver.c
blob: f64ff209ff2f4190a24e40107fd1f42df1878522 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * tileserver.c:
 * Serve map tiles and information about map tiles.
 *
 * Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved.
 * Email: chris@mysociety.org; WWW: http://www.mysociety.org/
 *
 */

static const char rcsid[] = "$Id: tileserver.c,v 1.1 2006-09-20 10:25:14 chris Exp $";

/* 
 * This is slightly complicated by the fact that we indirect tile references
 * via hashes of the tiles themselves. We support the following queries:
 *
 * http://host/path/tileserver/TILESET/HASH
 *      to get an individual tile image in TILESET identified by HASH;
 * http://host/path/tileserver/TILESET/E,N/FORMAT
 *      to get the identity of the tile at (E, N) in TILESET in the given
 *      FORMAT;
 * http://host/path/tileserver/TILESET/W-E,S-N/FORMAT
 *      to get the identities of the tiles in the block with SW corner (W, S)
 *      and NE corner (E, N) in the given FORMAT.
 * 
 * What FORMATS should we support? RABX and JSON are the obvious ones I guess.
 */

#include "tileset.h"

/* tileset_path
 * The configured path to tilesets. */
char *tileset_path;

#define HTTP_BAD_REQUEST            400
#define HTTP_UNAUTHORIZED           401
#define HTTP_FORBIDDEN              403
#define HTTP_NOT_FOUND              404
#define HTTP_INTERNAL_SERVER_ERROR  500
#define HTTP_NOT_IMPLEMENTED        501
#define HTTP_SERVICE_UNAVAILABLE    503

/* err FORMAT [ARG ...]
 * Write an error message to standard error. */
#define err(...)    \
        do { \
            fprintf(stderr, "tileserver: "); \
            fprintf(stderr, __VA_ARGS__); \
            fprintf(stderr, "\n"); \
        } while (0)

/* die FORMAT [ARG ...]
 * Write an error message to standard error and exit unsuccessfully. */
#define die(...) do { err(__VA_ARGS__); exit(1); } while (0)

/* error STATUS TEXT
 * Send an error to the client with the given HTTP STATUS and TEXT. */
void error(int status, const char *s) {
    if (status < 100 || status > 999)
        status = 500;
    printf(
        "Status: %03d\r\n"
        "Content-Type: text/plain; charset=us-ascii\r\n"
        "Content-Length: %u\r\n"
        "\r\n"
        "%s\n",
        status,
        strlen(s) + 1);
}

/* struct request
 * Definition of a request we handle. */
struct request {
    char *r_tileset;
    enum {
        FN_GET_TILE = 0,
        FN_GET_TILEIDS
    } r_function;

    uint8_t r_tilehash[TILEHASH_LEN];

    int r_west, r_east, r_south, r_north;
    enum {
        F_RABX,
        F_JSON,
        F_TEXT
    } r_format;

    char *r_buf;
};

/* request_parse PATHINFO
 * Parse a request from PATHINFO. Returns a request on success or NULL on
 * failure. */
struct request *request_parse(const char *path_info) {
    const char *p, *q;
    struct request *R = NULL, Rz = {0};

    /* Some trivial syntax checks. */
    if (!*path_info || *path_info == '/' || !strchr(path_info, '/'))
        return NULL;
    
   
    /* 
     * TILESET/HASH
     * TILESET/E,N/FORMAT
     * TILESET/W-E,S-N/FORMAT
     */

    /* Tileset name consists of alphanumerics and hyphen. */
    p = path_info + strspn(path_info,
                            "0123456789"
                            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                            "abcdefghijklmnopqrstuvwxyz"
                            "-");

    if (*p != '/')
        return NULL;

    R = xmalloc(sizeof *R);
    *R = Rz;
    R->r_buf = xmalloc(strlen(path_info) + 1);
    R->r_tileset = R->r_buf;

    strncpy(R->r_tileset, path_info, p - path_info);
    R->r_tileset[p - path_info] = 0;

    ++p;

    /* Mode. */
    if ((q = strchr(p, '/'))) {
        /* Tile IDs request. */
        R->r_function = FN_GET_TILEIDS;

        /* Identify format requested. */
        ++q;
        if (!strcmp(q, "RABX"))
            R->r_format = F_RABX;
        else if (!strcmp(q, "JSON"))
            R->r_format = F_JSON;
        else if (!strcmp(q, "text"))
            R->r_format = F_TEXT;
        else
            goto fail;

        if (4 == sscanf(p, "%d-%d,%d-%d",
                        &R->r_west, &R->r_east, &R->r_south, &R->r_north)) {
            if (R->r_west < 0 || R->r_south < 0
                || R->r_east < R->r_west || R->r_north < R->r_south)
                goto fail;
            else
                return R;
        } else if (2 == sscanf(p, "%d,%d", &R->r_west, &R->r_south)) {
            R->r_east = R->r_west;
            R->r_north = R->r_south;
            if (R->r_west < 0 || R->r_south < 0)
                goto fail;
            else
                return R;
        }
    } else {
        size_t l;

        /* Tile request. */
        R->r_function = FN_GET_TILE;
        if (strlen(p) != TILEID_LEN_B64)
            goto fail;

        /* Decode it. Really this is "base64ish", so that we don't have to
         * deal with '+' or '/' in the URL. */
        base64_decode(p, R->r_tilehash, &l, 1);
        if (l != TILEID_LEN)
            goto fail;

        return R;
    }
    
fail:
    request_free(R);
    return NULL;
}

void request_free(struct request *R) {
    if (!R) return;
    xfree(R->r_buf);
    xfree(R);
}

void handle_request(void) {
    char *path_info;
    struct request *R;
    static char *path;
    static size_t pathlen;
    size_t l;
    tileset T;
    time_t when;
    struct tm tm;
    char timebuf[32];

    /* All requests are given via PATH_INFO. */
    if (!(path_info = getenv("PATH_INFO"))) {
        error(400, "No request path supplied");
        return;
    }

    if (!(R = request_parse(path_info))) {
        error(400, "Bad request");
        return;
    }

    /* So we have a valid request. */
    l = strlen(R->r_tileset) + strlen(tileset_path) + 2;
    if (pathlen < l)
        pathlen = xrealloc(path, pathlen = l);
    sprintf(path, "%s/%s", tileset_path, R->r_tileset);
    
    if (!(T = tileset_open(path))) {
        error(404, "Tileset not found");
            /* XXX assumption about the nature of the error */
        return;
    }

    if (FN_GET_TILE == R->r_function) {
        /* 
         * Send a single tile image to the client.
         */
        void *buf;
        size_t len;

        if ((buf = tileset_get_tile(T, T->r_tileid, &len))) {
            printf(
                "Content-Type: image/png\r\n"
                "Content-Length: %u\r\n"
                "\r\n");
            fwrite(stdout, 1, buf, len);
            xfree(buf);
        } else
            error(404, "Tile not found");
                /* XXX error assumption */
    } else if (FN_GET_TILEIDS = R->r_function) {
        /*
         * Send one or more tile IDs to the client, in some useful format.
         */
        unsigned x, y;
        static char *buf;
        static size_t buflen, n;
        unsigned rows, cols;
        char *p;

        rows = R->r_north + 1 - R->r_south;
        cols = R->r_east + 1 - R->r_west;
        n = cols * rows;
        if (buflen < n * TILEID_LEN_B64 + 256)
            buf = xrealloc(buf, buflen = n * TILEID_LEN_B64 + 256);

        /* Send start of array in whatever format. */
        p = buf;
        switch (R->r_format) {
            case F_RABX:
                /* Format as array of arrays. */
                *(p++) = 'L';
                p += netstring_write_int(p, (int)rows);
                break;

            case F_JSON:
                /* Ditto. */
                *(p++) = '[';
                break;

            case F_TEXT:
                /* Space and LF separated matrix so no special leader. */
                break;
        }

        /* Iterate over tile IDs. */
        for (y = R->r_south; y <= R->r_north; ++y) {
            switch (R->r_format) {
                case F_RABX:
                    *(p++) = 'L';
                    p += netstring_write_int(p, (int)cols);

                case F_JSON:
                    *(p++) = '[';

                case F_TEXT:
                    break;  /* nothing */
            }
            
            for (x = R->r_west; x <= R->r_east; ++x) {
                uint8_t id[TILEID_LEN];
                char idb64[TILEID_LEN_B64 + 1];
                bool isnull = 0;
                size_t l;
                
                if (!(tileset_get_tileid(T, x, y, id)))
                    isnull = 1;
                else
                    base64_encode(id, TILEID_LEN, idb64, 1, 1);

                if (p + 256 > buflen) {
                    size_t n;
                    n = p - buf;
                    buf = xrealloc(buf, buflen *= 2);
                    p = buf + n;
                }

                switch (R->r_format) {
                    case F_RABX:
                        if (isnull) {
                            *(p++) = 'T';
                            p += netstring_write(p, idb64);
                        } else
                            *(p++) = 'N';
                        break;

                    case F_JSON:
                        if (isnull) {
                            strcpy(p, "null");
                            p += 4;
                        } else {
                            *(p++) = '"';
                            strcpy(p, idb64);
                            p += TILEID_LEN_B64;
                            *(p++) = '"';
                        }
                        if (x < R->r_east)
                            *(p++) = ',';
                        break;

                    case F_TEXT:
                        if (isnull)
                            *(p++) = '-';
                        else {
                            strcpy(p, idb64);
                            p += TILEID_LEN_B64;
                        }
                        if (x < R->r_east)
                            *(p++) = ' ';
                        break;
                }
            }

            switch (R->r_format) {
                case F_RABX:
                    break;  /* no row terminator */

                case F_JSON:
                    *(p++) = ']';
                    if (y < R->r_north)
                        *(p++) = ',';
                    break;

                case F_TEXT:
                    *(p++) = '\n';
                    break;
            }
        }

        /* Array terminator. */
        switch (R->r_format) {
            case F_RABX:
                break;

            case F_JSON:
                *(p++) = ']';
                break;
                
            case F_TEXT:
                *(p++) = '\n';
                break;
        }
        *(p++) = 0;

        /* Actually send it. */
        printf("Content-Type: ");
        switch (R->r_format) {
            case F_RABX:
                printf("application/octet-stream");
                break;

            case F_JSON:
                /* Not really clear what CT to use here but Yahoo use
                 * "text/javascript" and presumably they've done more testing
                 * than us.... */
                printf("text/javascript");
                break;

            case F_TEXT:
                printf("text/plain; charset=us-ascii");
                break;
        }
        printf("\r\n"
            "Content-Length: %u\r\n"
            "\r\n",
            (unsigned)(p - buf));

        fwrite(buf, 1, p - buf, stdout);
    }
}

int main(int argc, char *argv[]) {
    struct stat st;
    
    if (2 != argc) {
        die("single argument is path to tile sets");
    }
    tileset_path = argv[1];
    if (-1 == stat(tileset_path, &st))
        die("%s: stat: %s", tileset_path, strerror(errno));
    else if (!S_ISDIR(st.st_mode))
        die("%s: Not a directory", tileset_path);

    while (FCGI_Accept() >= 0)
        handle_request();

    return 0;
}