1
0
mirror of https://github.com/SuperBFG7/ympd synced 2024-11-17 11:04:48 +00:00
ympd/src/http_server.c

65 lines
1.7 KiB
C
Raw Normal View History

2013-11-04 17:18:38 +00:00
#include <libwebsockets.h>
#include <stdio.h>
#include <string.h>
2013-11-04 17:18:38 +00:00
#include "http_server.h"
#define LOCAL_RESOURCE_PATH DATADIR"/htdocs"
2013-11-04 17:18:38 +00:00
char *resource_path = LOCAL_RESOURCE_PATH;
struct serveable {
const char *urlpath;
const char *mimetype;
};
static const struct serveable whitelist[] = {
{ "/css/bootstrap.css", "text/css" },
2013-11-04 23:17:28 +00:00
{ "/css/slider.css", "text/css" },
2013-11-07 09:09:40 +00:00
{ "/css/mpd.css", "text/css" },
2013-11-04 23:17:28 +00:00
2013-11-04 17:18:38 +00:00
{ "/js/bootstrap.min.js", "text/javascript" },
{ "/js/mpd.js", "text/javascript" },
2013-11-04 23:17:28 +00:00
{ "/js/jquery-1.10.2.min.js", "text/javascript" },
{ "/js/bootstrap-slider.js", "text/javascript" },
2013-11-07 12:47:31 +00:00
{ "/js/sammy.js", "text/javascript" },
2013-11-04 23:17:28 +00:00
2013-11-04 17:18:38 +00:00
{ "/fonts/glyphicons-halflings-regular.woff", "application/x-font-woff"},
{ "/fonts/glyphicons-halflings-regular.svg", "image/svg+xml"},
{ "/fonts/glyphicons-halflings-regular.ttf", "application/x-font-ttf"},
{ "/fonts/glyphicons-halflings-regular.eot", "application/vnd.ms-fontobject"},
/* last one is the default served if no match */
{ "/index.html", "text/html" },
};
int callback_http(struct libwebsocket_context *context,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason, void *user,
void *in, size_t len)
{
char buf[256];
size_t n;
2013-11-04 17:18:38 +00:00
switch (reason) {
case LWS_CALLBACK_HTTP:
for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++)
{
if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0)
break;
}
sprintf(buf, "%s%s", resource_path, whitelist[n].urlpath);
if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype))
return -1; /* through completion or error, close the socket */
break;
case LWS_CALLBACK_HTTP_FILE_COMPLETION:
/* kill the connection after we sent one file */
return -1;
default:
break;
2013-11-04 17:18:38 +00:00
}
return 0;
}