Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_http_mime.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33
34#include "nilorea/n_common.h"
35#include "nilorea/n_http_mime.h"
36#include "nilorea/n_log.h"
37
39static int log_level = LOG_ERR;
41static int failures = 0;
42
47static int process_args(int argc, char** argv) {
48 int opt = 0;
49
50 while ((opt = getopt(argc, argv, "V:")) != -1) {
51 if (opt == 'V') {
52 log_level = atoi(optarg);
53 } else {
54 return -1;
55 }
56 }
57 return 0;
58}
59
63static void expect_type(const char* path, const char* want) {
64 const char* got = n_http_mime_type(path, strlen(path));
65 if ((want == NULL) != (got == NULL) || (want && strcmp(got, want) != 0)) {
66 n_log(LOG_ERR, "FAIL: %s gave %s, not %s", path, got ? got : "NULL", want ? want : "NULL");
67 failures++;
68 }
69}
70
75int main(int argc, char** argv) {
76 if (process_args(argc, argv) != 0) {
77 fprintf(stderr, "usage: %s [-V LOG_LEVEL]\n", argv[0]);
78 return 1;
79 }
81
83 n_log(LOG_ERR, "FAIL: the type table is not sorted");
84 failures++;
85 }
86 expect_type("/index.html", "text/html");
87 expect_type("/Games/BlockBlaster/index.HTML?x=1#top", "text/html");
88 expect_type("/assets/site.css", "text/css");
89 expect_type("/assets/site.js", "text/javascript");
90 expect_type("game.wasm", "application/wasm");
91 expect_type("game.data", "application/octet-stream");
92 expect_type("/media/0123456789abcdef/960/photo.webp", "image/webp");
93 expect_type("a.JPG", "image/jpeg");
94 expect_type("a.jpeg", "image/jpeg");
95 expect_type("icon.svg", "image/svg+xml");
96 expect_type("favicon.ico", "image/vnd.microsoft.icon");
97 expect_type("font.woff2", "font/woff2");
98 expect_type("sitemap.xml", "application/xml");
99 expect_type("search.js.map", "application/json");
100 expect_type("7z.7z", "application/x-7z-compressed");
101 expect_type("zip.zip", "application/zip");
102 expect_type("site.webmanifest", "application/manifest+json");
103 expect_type("/a.b/file", NULL);
104 expect_type("/dir/.htaccess", NULL);
105 expect_type("file.", NULL);
106 expect_type("noext", NULL);
107 expect_type("x.php", NULL);
108 expect_type("x.html.php", NULL);
109 expect_type("x.averyveryverylongextension", NULL);
110 expect_type("x.?.html", NULL);
111 expect_type("", NULL);
112 if (n_http_mime_type(NULL, 0) != NULL || n_http_mime_type("x.html", 1) != NULL) {
113 n_log(LOG_ERR, "FAIL: NULL / truncated length");
114 failures++;
115 }
116
117 if (failures) {
118 n_log(LOG_ERR, "ex_http_mime: %d failure(s)", failures);
119 return 1;
120 }
121 n_log(LOG_NOTICE, "ex_http_mime: all checks passed");
122 return 0;
123}
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.