source: rtems/cpukit/shttpd/mime_type.c @ c648ca5

4.104.115
Last change on this file since c648ca5 was c648ca5, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/26/10 at 17:42:24

Add HAVE_STRINGS_H for better POSIX compliance.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
3 * All rights reserved
4 *
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * Sergey Lyubka wrote this file.  As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return.
9 */
10
11#include "defs.h"
12
13#ifdef HAVE_STRINGS_H
14#include <strings.h>
15#endif
16
17static const struct mime_type default_mime_types[] = {
18        {"html",        4,      "text/html"                     },
19        {"htm",         3,      "text/html"                     },
20        {"txt",         3,      "text/plain"                    },
21        {"css",         3,      "text/css"                      },
22        {"ico",         3,      "image/x-icon"                  },
23        {"gif",         3,      "image/gif"                     },
24        {"jpg",         3,      "image/jpeg"                    },
25        {"jpeg",        4,      "image/jpeg"                    },
26        {"png",         3,      "image/png"                     },
27        {"svg",         3,      "image/svg+xml"                 },
28        {"torrent",     7,      "application/x-bittorrent"      },
29        {"wav",         3,      "audio/x-wav"                   },
30        {"mp3",         3,      "audio/x-mp3"                   },
31        {"mid",         3,      "audio/mid"                     },
32        {"m3u",         3,      "audio/x-mpegurl"               },
33        {"ram",         3,      "audio/x-pn-realaudio"          },
34        {"ra",          2,      "audio/x-pn-realaudio"          },
35        {"doc",         3,      "application/msword",           },
36        {"exe",         3,      "application/octet-stream"      },
37        {"zip",         3,      "application/x-zip-compressed"  },
38        {"xls",         3,      "application/excel"             },
39        {"tgz",         3,      "application/x-tar-gz"          },
40        {"tar.gz",      6,      "application/x-tar-gz"          },
41        {"tar",         3,      "application/x-tar"             },
42        {"gz",          2,      "application/x-gunzip"          },
43        {"arj",         3,      "application/x-arj-compressed"  },
44        {"rar",         3,      "application/x-arj-compressed"  },
45        {"rtf",         3,      "application/rtf"               },
46        {"pdf",         3,      "application/pdf"               },
47        {"mpg",         3,      "video/mpeg"                    },
48        {"mpeg",        4,      "video/mpeg"                    },
49        {"asf",         3,      "video/x-ms-asf"                },
50        {"avi",         3,      "video/x-msvideo"               },
51        {"bmp",         3,      "image/bmp"                     },
52        {NULL,          0,      NULL                            }
53};
54
55const char *
56get_mime_type(struct shttpd_ctx *ctx, const char *uri, int len)
57{
58        struct llhead           *lp;
59        const struct mime_type  *mt;
60        struct mime_type_link   *mtl;
61        const char              *s;
62
63        /* Firt, loop through the custom mime types if any */
64        LL_FOREACH(&ctx->mime_types, lp) {
65                mtl = LL_ENTRY(lp, struct mime_type_link, link);
66                s = uri + len - mtl->ext_len;
67                if (s > uri && s[-1] == '.' &&
68                    !my_strncasecmp(mtl->ext, s, mtl->ext_len))
69                        return (mtl->mime);
70        }
71
72        /* If no luck, try built-in mime types */
73        for (mt = default_mime_types; mt->ext != NULL; mt++) {
74                s = uri + len - mt->ext_len;
75                if (s > uri && s[-1] == '.' &&
76                    !my_strncasecmp(mt->ext, s, mt->ext_len))
77                        return (mt->mime);
78        }
79
80        /* Oops. This extension is unknown to us. Fallback to text/plain */
81        return ("text/plain");
82}
83
84void
85set_mime_types(struct shttpd_ctx *ctx, const char *path)
86{
87        FILE    *fp;
88        char    line[512], ext[sizeof(line)], mime[sizeof(line)], *s;
89
90        if ((fp = fopen(path, "r")) == NULL)
91                elog(E_FATAL, NULL, "set_mime_types: fopen(%s): %s",
92                    path, strerror(errno));
93
94        while (fgets(line, sizeof(line), fp) != NULL) {
95                /* Skip empty lines */
96                if (line[0] == '#' || line[0] == '\n')
97                        continue;
98                if (sscanf(line, "%s", mime)) {
99                        s = line + strlen(mime);
100                        while (*s && *s != '\n' && sscanf(s, "%s", ext)) {
101                                shttpd_add_mime_type(ctx, ext, mime);
102                                s += strlen(mime);
103                        }
104                }
105        }
106
107        (void) fclose(fp);
108}
Note: See TracBrowser for help on using the repository browser.