6. Wiki URL Parser (is_wiki)

First thing the requester does is parse the URL, this determines what to do.

Right now, the only thing the server is programmed to do is parse org pages to HTML and display. Pages start with the URL /wiki. For example, the URL /wiki/foo would display the page foo.

The wiki page is parsed with the function is_wiki. If true, the output will return the name + length.

<<static_funcdefs>>=
static int is_wiki(struct http_string_s *target,
                   const char **name,
                   int *len);
<<functions>>=
static int is_wiki(struct http_string_s *target,
                   const char **name,
                   int *len)
{
    const char *buf;
    int size;
    int pos;
    static const char *s = "index";

    *len = 0;

    /* '/' defaults to index */
    buf = target->buf;
    size = target->len;

    if (size == 1) {
        *name = s;
        *len = 5;
        return 1;
    }

    /* '/wiki' or '/wiki/' defaults to index */
    if (size == 5 || size == 6) {
        if (!strncmp("/wiki", buf, 5)) {
             if (size == 6 && buf[5] != '/') {
                 return 0;
             } else {
                *name = s;
                *len = 5;
                return 1;
             }
        }
    }

    if (size < 7) return 0; /* minimum: /wiki/N */

    pos = 1; /* skip first whack */

    /* for some reason, "wiki/" must be first */
    if (strncmp("wiki/", &buf[pos], 5)) return 0;

    pos += 5;

    *name = &buf[pos];
    *len = size - pos;

    return 1;
}

If it matches, the URL parser will extract the wiki page name and attempt to retrieve it from the database.



prev | home | next