Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_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
27
28#include "nilorea/n_http_mime.h"
29
30#include <string.h>
31
32/* Sorted by extension (lowercase, bytewise) for the binary search below;
33 * ex_http_mime checks the order. */
34static const struct {
35 const char* ext;
36 const char* type;
37} _mime_table[] = {
38 {"7z", "application/x-7z-compressed"},
39 {"apk", "application/vnd.android.package-archive"},
40 {"avif", "image/avif"},
41 {"bmp", "image/bmp"},
42 {"css", "text/css"},
43 {"csv", "text/csv"},
44 {"data", "application/octet-stream"},
45 {"deb", "application/vnd.debian.binary-package"},
46 {"exe", "application/vnd.microsoft.portable-executable"},
47 {"gif", "image/gif"},
48 {"gz", "application/gzip"},
49 {"htm", "text/html"},
50 {"html", "text/html"},
51 {"ico", "image/vnd.microsoft.icon"},
52 {"jpeg", "image/jpeg"},
53 {"jpg", "image/jpeg"},
54 {"js", "text/javascript"},
55 {"json", "application/json"},
56 {"map", "application/json"},
57 {"md", "text/markdown"},
58 {"md5", "text/plain"},
59 {"mjs", "text/javascript"},
60 {"mp3", "audio/mpeg"},
61 {"mp4", "video/mp4"},
62 {"oga", "audio/ogg"},
63 {"ogg", "audio/ogg"},
64 {"ogv", "video/ogg"},
65 {"otf", "font/otf"},
66 {"pdf", "application/pdf"},
67 {"png", "image/png"},
68 {"rss", "application/rss+xml"},
69 {"svg", "image/svg+xml"},
70 {"tar", "application/x-tar"},
71 {"ttf", "font/ttf"},
72 {"txt", "text/plain"},
73 {"wasm", "application/wasm"},
74 {"wav", "audio/wav"},
75 {"webm", "video/webm"},
76 {"webmanifest", "application/manifest+json"},
77 {"webp", "image/webp"},
78 {"woff", "font/woff"},
79 {"woff2", "font/woff2"},
80 {"xml", "application/xml"},
81 {"zip", "application/zip"},
82};
83
90const char* n_http_mime_type(const char* path, size_t len) {
91 char ext[16];
92 size_t i, end, seg = 0, dot = (size_t)-1, elen, lo, hi;
93 if (!path)
94 return NULL;
95 for (end = 0; end < len && path[end] != '?' && path[end] != '#'; end++);
96 for (i = 0; i < end; i++) {
97 if (path[i] == '/')
98 seg = i + 1;
99 else if (path[i] == '.')
100 dot = i;
101 }
102 /* no dot in the last segment, a leading dot (".htaccess") or a trailing one */
103 if (dot == (size_t)-1 || dot < seg || dot == seg || dot + 1 >= end)
104 return NULL;
105 elen = end - dot - 1;
106 if (elen >= sizeof(ext))
107 return NULL;
108 for (i = 0; i < elen; i++) {
109 unsigned char c = (unsigned char)path[dot + 1 + i];
110 ext[i] = (char)(c >= 'A' && c <= 'Z' ? c - 'A' + 'a' : c);
111 }
112 ext[elen] = '\0';
113 lo = 0;
114 hi = sizeof(_mime_table) / sizeof(_mime_table[0]);
115 while (lo < hi) {
116 size_t mid = lo + (hi - lo) / 2;
117 int cmp = strcmp(ext, _mime_table[mid].ext);
118 if (cmp == 0)
119 return _mime_table[mid].type;
120 if (cmp < 0)
121 hi = mid;
122 else
123 lo = mid + 1;
124 }
125 return NULL;
126}
127
133 size_t i;
134 for (i = 1; i < sizeof(_mime_table) / sizeof(_mime_table[0]); i++)
135 if (strcmp(_mime_table[i - 1].ext, _mime_table[i].ext) >= 0)
136 return 0;
137 return 1;
138}
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
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
static const struct @242055217114105150047231111160250133146166323032 _mime_table[]
Media type of a file from its extension, for a static file server.