Dillo v3.1.1-98-g318d1f14
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_SVG
113#endif
114 Mime_add_minor_type("text/html", a_Html_text);
115 Mime_add_minor_type("application/xhtml+xml", a_Html_text);
116 Mime_add_minor_type("application/json", a_Plain_text);
117
118 /* Add a major type to handle all the text stuff */
120}
121
122
130Viewer_t a_Mime_get_viewer(const char *content_type)
131{
132 Viewer_t viewer;
133 uint_t MinSize, MajSize, i;
134 const char *str = content_type;
135
136 MajSize = 0;
137 for (i = 0; str[i] && str[i] != ' ' && str[i] != ';'; ++i) {
138 if (str[i] == '/' && !MajSize)
139 MajSize = i;
140 }
141 MinSize = i;
142
143 viewer = Mime_minor_type_fetch(content_type, MinSize);
144 if (!viewer)
145 viewer = Mime_major_type_fetch(content_type, MajSize);
146
147 return viewer;
148}
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:445
void * a_Dicache_jpeg_image(const char *Type, void *Ptr, CA_Callback_t *Call, void **Data)
JPEG wrapper for Dicache_image()
Definition dicache.c:436
void * a_Dicache_gif_image(const char *Type, void *Ptr, CA_Callback_t *Call, void **Data)
GIF wrapper for Dicache_image()
Definition dicache.c:427
void * a_Dicache_png_image(const char *Type, void *Ptr, CA_Callback_t *Call, void **Data)
PNG wrapper for Dicache_image()
Definition dicache.c:418
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:130
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