Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_http_mime.c

n_http_mime: media types from extensions, last segment only, query ignored, case-insensitive, NULL when unknown

n_http_mime: media types from extensions, last segment only, query ignored, case-insensitive, NULL when unknown

Author
Castagnier Mickael
Version
1.0
Date
23/10/2026
/*
* 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_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");
}
}
int main(int argc, char** argv) {
if (process_args(argc, argv) != 0) {
fprintf(stderr, "usage: %s [-V LOG_LEVEL]\n", argv[0]);
return 1;
}
n_log(LOG_ERR, "FAIL: the type table is not sorted");
}
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");
}
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;
}
void process_args(int argc, char **argv)
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static int log_level
static void expect_type(const char *path, const char *want)
path maps to want (NULL: no type)
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
int n_http_mime_table_check(void)
check that the built-in table is sorted, as its lookup requires
const char * n_http_mime_type(const char *path, size_t len)
media type for a path or file name, from its extension
Definition n_http_mime.c:90
Common headers and low-level functions & define.
Media type of a file from its extension, for a static file server.
Generic log system.