mirror of
https://github.com/SuperBFG7/ympd
synced 2024-11-26 06:47:17 +00:00
http_server.c: reindent, cleanup, fixed memory leak
This commit is contained in:
parent
31f1df974c
commit
993be4a474
@ -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" },
|
||||||
@ -40,34 +40,28 @@ static const struct serveable whitelist[] = {
|
|||||||
|
|
||||||
/* Converts a hex character to its integer value */
|
/* Converts a hex character to its integer value */
|
||||||
char from_hex(char ch) {
|
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) {
|
||||||
char *pstr = str, *buf = malloc(strlen(str) + 1), *pbuf = buf;
|
char *pstr = str, *buf = malloc(strlen(str) + 1), *pbuf = buf;
|
||||||
while (*pstr) {
|
while (*pstr) {
|
||||||
if (*pstr == '%') {
|
if (*pstr == '%') {
|
||||||
if (pstr[1] && pstr[2]) {
|
if (pstr[1] && pstr[2]) {
|
||||||
*pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
|
*pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
|
||||||
pstr += 2;
|
pstr += 2;
|
||||||
}
|
}
|
||||||
} else if (*pstr == '+') {
|
} else if (*pstr == '+') {
|
||||||
*pbuf++ = ' ';
|
*pbuf++ = ' ';
|
||||||
} else {
|
} else {
|
||||||
*pbuf++ = *pstr;
|
*pbuf++ = *pstr;
|
||||||
|
}
|
||||||
|
pstr++;
|
||||||
}
|
}
|
||||||
pstr++;
|
*pbuf = '\0';
|
||||||
}
|
return buf;
|
||||||
*pbuf = '\0';
|
|
||||||
return buf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int callback_http(struct libwebsocket_context *context,
|
int callback_http(struct libwebsocket_context *context,
|
||||||
@ -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);
|
||||||
@ -119,15 +113,15 @@ int callback_http(struct libwebsocket_context *context,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
p += response_size + sprintf(p, "HTTP/1.0 200 OK\x0d\x0a"
|
p += response_size + sprintf(p, "HTTP/1.0 200 OK\x0d\x0a"
|
||||||
"Server: libwebsockets\x0d\x0a"
|
"Server: libwebsockets\x0d\x0a"
|
||||||
"Content-Type: application/json\x0d\x0a"
|
"Content-Type: application/json\x0d\x0a"
|
||||||
"Content-Length: %6lu\x0d\x0a\x0d\x0a",
|
"Content-Length: %6lu\x0d\x0a\x0d\x0a",
|
||||||
response_size
|
response_size
|
||||||
);
|
);
|
||||||
response_buffer[98] = '{';
|
response_buffer[98] = '{';
|
||||||
|
|
||||||
n = libwebsocket_write(wsi, (unsigned char *)response_buffer,
|
n = libwebsocket_write(wsi, (unsigned char *)response_buffer,
|
||||||
p - response_buffer, LWS_WRITE_HTTP);
|
p - response_buffer, LWS_WRITE_HTTP);
|
||||||
|
|
||||||
free(response_buffer);
|
free(response_buffer);
|
||||||
/*
|
/*
|
||||||
@ -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))
|
||||||
|
Loading…
Reference in New Issue
Block a user