Special handling for favicon.png

This commit is contained in:
Thomas Kolb 2013-01-31 18:15:49 +01:00
parent 0782bc834b
commit 363a16e056
7 changed files with 212 additions and 3 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ fileshare
core
tags
testdir
*.png

View file

@ -6,8 +6,8 @@ CFLAGS+=-Wall -pedantic -std=c99 -D_POSIX_C_SOURCE=20120607L -D_FILE_OFFSET_BITS
LIBS=-lmicrohttpd $(MAGIC_LIBS)
TARGET=fileshare
SOURCE=main.c logger.c dirlisting.c util.c
DEPS=logger.h templates.h dirlisting.h util.h
SOURCE=main.c logger.c dirlisting.c util.c favicon.c
DEPS=logger.h templates.h dirlisting.h util.h favicon.h
OBJ=$(patsubst %.c, %.o, $(SOURCE))

63
favicon.c Normal file
View file

@ -0,0 +1,63 @@
#include <microhttpd.h>
#include "favicon.h"
unsigned char favicon_png[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10,
0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0xf3, 0xff, 0x61, 0x00, 0x00, 0x00,
0x04, 0x73, 0x42, 0x49, 0x54, 0x08, 0x08, 0x08, 0x08, 0x7c, 0x08, 0x64,
0x88, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0d,
0xd7, 0x00, 0x00, 0x0d, 0xd7, 0x01, 0x42, 0x28, 0x9b, 0x78, 0x00, 0x00,
0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61,
0x72, 0x65, 0x00, 0x77, 0x77, 0x77, 0x2e, 0x69, 0x6e, 0x6b, 0x73, 0x63,
0x61, 0x70, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x9b, 0xee, 0x3c, 0x1a, 0x00,
0x00, 0x01, 0x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3, 0x26, 0x18, 0xbe, 0x3b, 0x89, 0xab, 0xbf, 0xb7, 0xa2,
0x65, 0x40, 0x47, 0x5d, 0x67, 0xdc, 0xff, 0xf8, 0x01, 0x31, 0xfd, 0xcd,
0x11, 0xae, 0x58, 0x62, 0xf5, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e,
0x44, 0xae, 0x42, 0x60, 0x82
};
unsigned int favicon_png_len = 521;
struct MHD_Response* create_favicon_response(void) {
struct MHD_Response *faviconResponse = MHD_create_response_from_data(
favicon_png_len,
(void*)favicon_png,
MHD_NO,
MHD_NO);
MHD_add_response_header(faviconResponse, MHD_HTTP_HEADER_CONTENT_TYPE, "image/png");
return faviconResponse;
}

8
favicon.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef FAVICON_H
#define FAVICON_H
struct MHD_Response;
struct MHD_Response* create_favicon_response(void);
#endif // FAVICON_H

119
favicon/favicon.svg Normal file
View file

@ -0,0 +1,119 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="16"
height="16"
id="svg2"
version="1.1"
inkscape:version="0.48.4 r9939"
sodipodi:docname="favicon.svg"
inkscape:export-filename="/home/thomas/code/c/fileshare/favicon/favicon.png"
inkscape:export-xdpi="90"
inkscape:export-ydpi="90">
<defs
id="defs4">
<linearGradient
id="linearGradient3765">
<stop
style="stop-color:#ebc000;stop-opacity:1;"
offset="0"
id="stop3767" />
<stop
style="stop-color:#ff0000;stop-opacity:1;"
offset="1"
id="stop3769" />
</linearGradient>
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient3765"
id="linearGradient3771"
x1="8"
y1="2"
x2="8"
y2="15"
gradientUnits="userSpaceOnUse"
gradientTransform="translate(0,1037.3622)" />
</defs>
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="1"
inkscape:pageshadow="2"
inkscape:zoom="35.25"
inkscape:cx="4.5013847"
inkscape:cy="9.2978892"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="true"
inkscape:snap-bbox="true"
inkscape:window-width="1278"
inkscape:window-height="779"
inkscape:window-x="0"
inkscape:window-y="19"
inkscape:window-maximized="1">
<inkscape:grid
type="xygrid"
id="grid2985"
empspacing="4"
visible="true"
enabled="true"
snapvisiblegridlinesonly="true" />
</sodipodi:namedview>
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Ebene 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,-1036.3622)">
<path
style="fill:url(#linearGradient3771);fill-opacity:1;stroke:none"
d="m 8,1052.3622 6,-6 -3,0 0,-8 -6,0 0,8 -3,0 z"
id="path2987"
inkscape:connector-curvature="0"
sodipodi:nodetypes="cccccccc" />
<text
xml:space="preserve"
style="font-size:9.98262786999999996px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#008000;fill-opacity:1;stroke:none;font-family:Ethnocentric;-inkscape-font-specification:Ethnocentric"
x="0.9301216"
y="1043.5996"
id="text3773"
sodipodi:linespacing="125%"><tspan
sodipodi:role="line"
id="tspan3775"
x="0.9301216"
y="1043.5996">F</tspan></text>
<text
sodipodi:linespacing="125%"
id="text3781"
y="1047.5996"
x="5.8502607"
style="font-size:9.98262787000000174px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#002b00;fill-opacity:1;stroke:none;font-family:Ethnocentric;-inkscape-font-specification:Ethnocentric"
xml:space="preserve"><tspan
y="1047.5996"
x="5.8502607"
id="tspan3783"
sodipodi:role="line">S</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.8 KiB

19
main.c
View file

@ -26,6 +26,7 @@
#include "logger.h"
#include "templates.h"
#include "dirlisting.h"
#include "favicon.h"
#include "util.h"
#define DEFAULT_PORT 8888
@ -45,6 +46,7 @@ char *shareRoot;
struct MHD_Response *error403Response;
struct MHD_Response *error404Response;
struct MHD_Response *error500Response;
struct MHD_Response *faviconResponse;
#ifdef HAVE_MAGIC
magic_t magicCookie;
@ -242,7 +244,7 @@ static int connection_handler(void * cls,
strncpy(connstate->cleanedURL, url, PATH_MAX);
remove_trailing_slash(connstate->cleanedURL);
LOG(LVL_INFO, "New %s request for %s (local: %s)",
LOG(LVL_INFO, "%s %s (local: %s)",
method, url, connstate->localFileName);
return MHD_YES;
@ -254,6 +256,12 @@ static int connection_handler(void * cls,
return MHD_NO; // upload data in a GET!?
}
// serv the favicon, if requested
if(strcmp(url, "/favicon.png") == 0) {
LOG(LVL_INFO, "Serving FavIcon request.");
return MHD_queue_response(connection, MHD_HTTP_OK, faviconResponse);
}
// check if url contains a "/.." sequence. If so, block the request as it is
// trying to access the parent directory (and may be an attempt to leave the
// shared space.
@ -368,6 +376,11 @@ int main(int argc, char ** argv) {
logger_init();
#ifndef DEBUG
// don't show debug output in release build
logger_set_verbosity(LVL_INFO);
#endif
if (argc < 2) {
printf("%s <dir> [<port>]\n", argv[0]);
return 1;
@ -451,6 +464,9 @@ int main(int argc, char ** argv) {
MHD_add_response_header(error404Response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html");
MHD_add_response_header(error500Response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html");
// static response for favicon
faviconResponse = create_favicon_response();
d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
port,
NULL,
@ -497,6 +513,7 @@ int main(int argc, char ** argv) {
MHD_destroy_response(error403Response);
MHD_destroy_response(error404Response);
MHD_destroy_response(error500Response);
MHD_destroy_response(faviconResponse);
#ifdef HAVE_MAGIC
magic_close(magicCookie);

View file

@ -10,6 +10,7 @@
#define HEADER2 \
" <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" >" \
" <link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/favicon.png\">" \
" </head>" \
" <body>"