1
0
mirror of https://github.com/SuperBFG7/ympd synced 2024-11-26 14:57:17 +00:00

http_server.c: reindent, cleanup, fixed memory leak

This commit is contained in:
Andrew Karpow 2014-01-08 15:23:14 +01:00
parent 31f1df974c
commit 993be4a474

View File

@ -32,7 +32,7 @@ static const struct serveable whitelist[] = {
{ "/fonts/glyphicons-halflings-regular.ttf", "application/x-font-ttf"}, { "/fonts/glyphicons-halflings-regular.ttf", "application/x-font-ttf"},
{ "/fonts/glyphicons-halflings-regular.eot", "application/vnd.ms-fontobject"}, { "/fonts/glyphicons-halflings-regular.eot", "application/vnd.ms-fontobject"},
{ "assets/favicon.ico", "image/vnd.microsoft.icon" }, { "/assets/favicon.ico", "image/vnd.microsoft.icon" },
/* last one is the default served if no match */ /* last one is the default served if no match */
{ "/index.html", "text/html" }, { "/index.html", "text/html" },
@ -43,12 +43,6 @@ char from_hex(char ch) {
return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
} }
/* Converts an integer value to its hex character*/
char to_hex(char code) {
static char hex[] = "0123456789abcdef";
return hex[code & 15];
}
/* Returns a url-decoded version of str */ /* Returns a url-decoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */ /* IMPORTANT: be sure to free() the returned string after use */
char *url_decode(char *str) { char *url_decode(char *str) {
@ -75,7 +69,7 @@ int callback_http(struct libwebsocket_context *context,
enum libwebsocket_callback_reasons reason, void *user, enum libwebsocket_callback_reasons reason, void *user,
void *in, size_t len) void *in, size_t len)
{ {
char buf[256], *response_buffer, *p; char *response_buffer, *p;
size_t n, response_size; size_t n, response_size;
switch (reason) { switch (reason) {
@ -89,7 +83,7 @@ int callback_http(struct libwebsocket_context *context,
if(strncmp((const char *)in, "/api/get_browse", 15) == 0) if(strncmp((const char *)in, "/api/get_browse", 15) == 0)
{ {
char *url; char *url;
if(sscanf(in, "/api/get_browse/%m[^\t\n]", &url) && url) if(sscanf(in, "/api/get_browse/%m[^\t\n]", &url))
{ {
char *url_decoded = url_decode(url); char *url_decoded = url_decode(url);
printf("searching for %s", url_decoded); printf("searching for %s", url_decoded);
@ -139,10 +133,9 @@ int callback_http(struct libwebsocket_context *context,
else else
{ {
for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++) for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++)
{
if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0) if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0)
break; break;
}
sprintf(buf, "%s%s", resource_path, whitelist[n].urlpath); sprintf(buf, "%s%s", resource_path, whitelist[n].urlpath);
if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype, NULL)) if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype, NULL))