1 ex__http__mime_8c
gullradriel edited this page 2026-10-25 10:46:33 +01:00

examples/ex_http_mime.c

Functions

Name
int process_args(int argc, char ** argv)
read -V
int main(int argc, char ** argv)
run the checks
void expect_type(const char * path, const char * want)
path maps to want (NULL: no type)

Attributes

Name
int log_level
log level from -V
int failures
failed checks

Functions Documentation

function process_args

static int process_args(
    int argc,
    char ** argv
)

read -V

Parameters:

  • argc argument count
  • argv arguments

Return: 0, or -1 on a bad argument

function main

int main(
    int argc,
    char ** argv
)

run the checks

Parameters:

  • argc argument count
  • argv arguments

Return: 0 when every check passes

function expect_type

static void expect_type(
    const char * path,
    const char * want
)

path maps to want (NULL: no type)

Parameters:

  • path the path
  • want expected type or NULL

Attributes Documentation

variable log_level

static int log_level = 3;

log level from -V

variable failures

static int failures = 0;

failed checks

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "nilorea/n_common.h"
#include "nilorea/n_http_mime.h"
#include "nilorea/n_log.h"

static int log_level = LOG_ERR;
static int failures = 0;

static int process_args(int argc, char** argv) {
    int opt = 0;

    while ((opt = getopt(argc, argv, "V:")) != -1) {
        if (opt == 'V') {
            log_level = atoi(optarg);
        } else {
            return -1;
        }
    }
    return 0;
}

static void expect_type(const char* path, const char* want) {
    const char* got = n_http_mime_type(path, strlen(path));
    if ((want == NULL) != (got == NULL) || (want && strcmp(got, want) != 0)) {
        n_log(LOG_ERR, "FAIL: %s gave %s, not %s", path, got ? got : "NULL", want ? want : "NULL");
        failures++;
    }
}

int main(int argc, char** argv) {
    if (process_args(argc, argv) != 0) {
        fprintf(stderr, "usage: %s [-V LOG_LEVEL]\n", argv[0]);
        return 1;
    }
    set_log_level(log_level);

    if (!n_http_mime_table_check()) {
        n_log(LOG_ERR, "FAIL: the type table is not sorted");
        failures++;
    }
    expect_type("/index.html", "text/html");
    expect_type("/Games/BlockBlaster/index.HTML?x=1#top", "text/html");
    expect_type("/assets/site.css", "text/css");
    expect_type("/assets/site.js", "text/javascript");
    expect_type("game.wasm", "application/wasm");
    expect_type("game.data", "application/octet-stream");
    expect_type("/media/0123456789abcdef/960/photo.webp", "image/webp");
    expect_type("a.JPG", "image/jpeg");
    expect_type("a.jpeg", "image/jpeg");
    expect_type("icon.svg", "image/svg+xml");
    expect_type("favicon.ico", "image/vnd.microsoft.icon");
    expect_type("font.woff2", "font/woff2");
    expect_type("sitemap.xml", "application/xml");
    expect_type("search.js.map", "application/json");
    expect_type("7z.7z", "application/x-7z-compressed");
    expect_type("zip.zip", "application/zip");
    expect_type("site.webmanifest", "application/manifest+json");
    expect_type("/a.b/file", NULL);
    expect_type("/dir/.htaccess", NULL);
    expect_type("file.", NULL);
    expect_type("noext", NULL);
    expect_type("x.php", NULL);
    expect_type("x.html.php", NULL);
    expect_type("x.averyveryverylongextension", NULL);
    expect_type("x.?.html", NULL);
    expect_type("", NULL);
    if (n_http_mime_type(NULL, 0) != NULL || n_http_mime_type("x.html", 1) != NULL) {
        n_log(LOG_ERR, "FAIL: NULL / truncated length");
        failures++;
    }

    if (failures) {
        n_log(LOG_ERR, "ex_http_mime: %d failure(s)", failures);
        return 1;
    }
    n_log(LOG_NOTICE, "ex_http_mime: all checks passed");
    return 0;
}