Dillo v3.1.1-119-g140d9ebd
Loading...
Searching...
No Matches
mime.c
Go to the documentation of this file.
1/*
2 * File: mime.c
3 *
4 * Copyright (C) 2000-2007 Jorge Arellano Cid <jcid@dillo.org>
5 * Copyright (C) 2024 Rodrigo Arias Mallo <rodarima@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13#include "mime.h"
14#include "../list.h"
15
16
17typedef struct {
18 const char *Name; /* MIME type name */
19 Viewer_t Data; /* Pointer to a function */
20} MimeItem_t;
21
22
23/*
24 * Local data
25 */
27static MimeItem_t *MimeMinItems = NULL;
28
30static MimeItem_t *MimeMajItems = NULL;
31
32
38static int Mime_add_minor_type(const char *Key, Viewer_t Method)
39{
42 MimeMinItems[MimeMinItemsSize].Data = Method;
44 return 0;
45}
46
52static int Mime_add_major_type(const char *Key, Viewer_t Method)
53{
56 MimeMajItems[MimeMajItemsSize].Data = Method;
58 return 0;
59}
60
65static Viewer_t Mime_minor_type_fetch(const char *Key, uint_t Size)
66{
67 int i;
68
69 if (Size) {
70 for ( i = 0; i < MimeMinItemsSize; ++i )
71 if (dStrnAsciiCasecmp(Key, MimeMinItems[i].Name, Size) == 0)
72 return MimeMinItems[i].Data;
73 }
74 return NULL;
75}
76
81static Viewer_t Mime_major_type_fetch(const char *Key, uint_t Size)
82{
83 int i;
84
85 if (Size) {
86 for ( i = 0; i < MimeMajItemsSize; ++i )
87 if (dStrnAsciiCasecmp(Key, MimeMajItems[i].Name, Size) == 0)
88 return MimeMajItems[i].Data;
89 }
90 return NULL;
91}
92
93
97void a_Mime_init(void)
98{
99#ifdef ENABLE_GIF
101#endif
102#ifdef ENABLE_JPEG
106#endif
107#ifdef ENABLE_PNG
109 Mime_add_minor_type("image/x-png", a_Dicache_png_image); /* deprecated */
110#endif
111#ifdef ENABLE_WEBP
113#endif
114#ifdef ENABLE_SVG
116#endif
117 Mime_add_minor_type("text/html", a_Html_text);
118 Mime_add_minor_type("application/xhtml+xml", a_Html_text);
119 Mime_add_minor_type("application/json", a_Plain_text);
120
121 /* Add a major type to handle all the text stuff */
123}
124
125
133Viewer_t a_Mime_get_viewer(const char *content_type)
134{
135 Viewer_t viewer;
136 uint_t MinSize, MajSize, i;
137 const char *str = content_type;
138
139 MajSize = 0;
140 for (i = 0; str[i] && str[i] != ' ' && str[i] != ';'; ++i) {
141 if (str[i] == '/' && !MajSize)
142 MajSize = i;
143 }
144 MinSize = i;
145
146 viewer = Mime_minor_type_fetch(content_type, MinSize);
147 if (!viewer)
148 viewer = Mime_major_type_fetch(content_type, MajSize);
149
150 return viewer;
151}
unsigned int uint_t
Definition d_size.h:20
void * a_Dicache_svg_image(const char *Type, void *Ptr, CA_Callback_t *Call, void **Data)
SVG wrapper for Dicache_image()
Definition dicache.c:483
void * a_Dicache_jpeg_image(const char *Type, void *Ptr, CA_Callback_t *Call, void **Data)
JPEG wrapper for Dicache_image()
Definition dicache.c:474
void * a_Dicache_webp_image(const char *Type, void *Ptr, CA_Callback_t *Call, void **Data)
WEBP wrapper for Dicache_image()
Definition dicache.c:456
void * a_Dicache_gif_image(const char *Type, void *Ptr, CA_Callback_t *Call, void **Data)
GIF wrapper for Dicache_image()
Definition dicache.c:465
void * a_Dicache_png_image(const char *Type, void *Ptr, CA_Callback_t *Call, void **Data)
PNG wrapper for Dicache_image()
Definition dicache.c:447
int dStrnAsciiCasecmp(const char *s1, const char *s2, size_t n)
Definition dlib.c:215
#define a_List_add(list, num_items, alloc_step)
Definition cookies.c:68
void * a_Html_text(const char *type, void *P, CA_Callback_t *Call, void **Data)
Set callback function and callback data for the "html/text" MIME type.
Definition html.cc:211
void a_Mime_init(void)
Initializes Mime module and, sets the supported Mime types.
Definition mime.c:97
static Viewer_t Mime_minor_type_fetch(const char *Key, uint_t Size)
Search the list of specific MIME viewers for a Method that matches 'Key'.
Definition mime.c:65
static int Mime_add_minor_type(const char *Key, Viewer_t Method)
Add a specific MIME type (as "image/png") to our viewer list.
Definition mime.c:38
static int Mime_add_major_type(const char *Key, Viewer_t Method)
Add a major MIME type (as "text") to our viewer list.
Definition mime.c:52
static int MimeMinItemsMax
Definition mime.c:26
static Viewer_t Mime_major_type_fetch(const char *Key, uint_t Size)
Search the list of major MIME viewers for a Method that matches 'Key'.
Definition mime.c:81
static MimeItem_t * MimeMinItems
Definition mime.c:27
static int MimeMajItemsMax
Definition mime.c:29
static MimeItem_t * MimeMajItems
Definition mime.c:30
static int MimeMinItemsSize
Definition mime.c:26
static int MimeMajItemsSize
Definition mime.c:29
Viewer_t a_Mime_get_viewer(const char *content_type)
Get the handler for the MIME type.
Definition mime.c:133
void * a_Plain_text(const char *Type, void *web, CA_Callback_t *Call, void **Data)
Set callback function and callback data for "text/" MIME major-type.
Definition plain.cc:205
void *(* Viewer_t)(const char *, void *, CA_Callback_t *, void **)
Definition mime.h:24