2013-11-04 17:18:38 +00:00
|
|
|
#include <libwebsockets.h>
|
|
|
|
#include <stdio.h>
|
2013-11-05 13:59:12 +00:00
|
|
|
#include <string.h>
|
2013-11-04 17:18:38 +00:00
|
|
|
#include "http_server.h"
|
|
|
|
|
|
|
|
char *resource_path = LOCAL_RESOURCE_PATH;
|
|
|
|
|
|
|
|
struct serveable {
|
2013-11-09 01:07:03 +00:00
|
|
|
const char *urlpath;
|
|
|
|
const char *mimetype;
|
2013-11-04 17:18:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct serveable whitelist[] = {
|
2013-11-09 01:07:03 +00:00
|
|
|
{ "/css/bootstrap.css", "text/css" },
|
|
|
|
{ "/css/slider.css", "text/css" },
|
|
|
|
{ "/css/mpd.css", "text/css" },
|
2013-11-04 23:17:28 +00:00
|
|
|
|
2013-11-09 01:07:03 +00:00
|
|
|
{ "/js/bootstrap.min.js", "text/javascript" },
|
|
|
|
{ "/js/mpd.js", "text/javascript" },
|
|
|
|
{ "/js/jquery-1.10.2.min.js", "text/javascript" },
|
|
|
|
{ "/js/bootstrap-slider.js", "text/javascript" },
|
|
|
|
{ "/js/sammy.js", "text/javascript" },
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2013-11-09 01:07:03 +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" },
|
2013-11-04 17:18:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int callback_http(struct libwebsocket_context *context,
|
2013-11-09 01:07:03 +00:00
|
|
|
struct libwebsocket *wsi,
|
|
|
|
enum libwebsocket_callback_reasons reason, void *user,
|
|
|
|
void *in, size_t len)
|
2013-11-04 17:18:38 +00:00
|
|
|
{
|
2013-11-09 01:07:03 +00:00
|
|
|
char buf[256];
|
|
|
|
size_t n;
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2013-11-09 01:07:03 +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);
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2013-11-09 01:07:03 +00:00
|
|
|
if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype))
|
|
|
|
return -1; /* through completion or error, close the socket */
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2013-11-09 01:07:03 +00:00
|
|
|
break;
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2013-11-09 01:07:03 +00:00
|
|
|
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
|
|
|
|
2013-11-09 01:07:03 +00:00
|
|
|
return 0;
|
2013-11-04 17:18:38 +00:00
|
|
|
}
|