myMPD
Internal API documentation
Loading...
Searching...
No Matches
utility.c File Reference

Webserver utility functions. More...

#include "compile_time.h"
#include "src/webserver/utility.h"
#include "src/lib/cache/cache_disk_images.h"
#include "src/lib/config_def.h"
#include "src/lib/filehandler.h"
#include "src/lib/log.h"
#include "src/lib/mimetype.h"
#include "src/lib/sds_extras.h"
#include "src/lib/utility.h"
Include dependency graph for utility.c:

Functions

struct t_listwebserver_parse_arguments (struct mg_http_message *hm)
 
sds print_ip (sds s, struct mg_addr *addr)
 
sds get_uri_param (struct mg_str *query, const char *name)
 
bool get_partition_from_uri (struct mg_connection *nc, struct mg_http_message *hm, struct t_frontend_nc_data *frontend_nc_data)
 
bool check_imagescache (struct mg_connection *nc, struct mg_http_message *hm, struct t_mg_user_data *mg_user_data, const char *type, sds uri_decoded, int offset)
 
sds webserver_find_image_file (sds basefilename)
 
bool find_image_in_folder (sds *coverfile, sds music_directory, sds path, sds *names, int names_len)
 
void webserver_send_error (struct mg_connection *nc, int code, const char *msg)
 
void webserver_send_header_ok (struct mg_connection *nc, size_t len, const char *headers)
 
void webserver_send_data (struct mg_connection *nc, const char *data, size_t len, const char *headers)
 
void webserver_send_raw (struct mg_connection *nc, const char *data, size_t len)
 
void webserver_serve_file (struct mg_connection *nc, struct mg_http_message *hm, const char *headers, const char *file)
 
void webserver_send_header_redirect (struct mg_connection *nc, const char *location, const char *headers)
 
void webserver_send_header_found (struct mg_connection *nc, const char *location, const char *headers)
 
void webserver_send_cors_reply (struct mg_connection *nc)
 
void webserver_handle_connection_close (struct mg_connection *nc)
 

Variables

static const char * image_file_extensions []
 

Detailed Description

Webserver utility functions.

Function Documentation

◆ check_imagescache()

bool check_imagescache ( struct mg_connection *  nc,
struct mg_http_message *  hm,
struct t_mg_user_data mg_user_data,
const char *  type,
sds  uri_decoded,
int  offset 
)

Checks the covercache and serves the image

Parameters
ncmongoose connection
hmhttp message
mg_user_datapointer to mongoose configuration
typecache type: cover or thumbs
uri_decodedimage uri
offsetembedded image offset
Returns
true if an image is served, false if no image was found in cache

◆ find_image_in_folder()

bool find_image_in_folder ( sds *  coverfile,
sds  music_directory,
sds  path,
sds *  names,
int  names_len 
)

Finds an image in a specific subdir in dir

Parameters
coverfilepointer to already allocated sds string to append the found image path
music_directoryparent directory
pathsubdirectory
namessds array of names
names_lenlength of sds array
Returns
true on success, else false

◆ get_partition_from_uri()

bool get_partition_from_uri ( struct mg_connection *  nc,
struct mg_http_message *  hm,
struct t_frontend_nc_data frontend_nc_data 
)

Sets the partition from uri and handles errors

Parameters
ncmongoose connection
hmhttp message
frontend_nc_datafrontend nc data
Returns
true on success, else false

◆ get_uri_param()

sds get_uri_param ( struct mg_str *  query,
const char *  name 
)

Gets and decodes an url parameter. The key will not be url decoded before matching.

Parameters
queryquery string to parse
namename to get, you must append "=" to the name
Returns
url decoded value or NULL on error

◆ print_ip()

sds print_ip ( sds  s,
struct mg_addr *  addr 
)

Prints the ip address from a mg_addr struct

Parameters
salready allocated sds string to append the ip
addrpointer to struct mg_addr
Returns
sds pointer to s

◆ webserver_find_image_file()

sds webserver_find_image_file ( sds  basefilename)

Finds the first image with basefilename by trying out extensions

Parameters
basefilenamebasefilename to append extensions
Returns
pointer to extended basefilename on success, else empty

◆ webserver_handle_connection_close()

void webserver_handle_connection_close ( struct mg_connection *  nc)

Drains the connection if connection is set to close

Parameters
ncmongoose connection

◆ webserver_parse_arguments()

struct t_list * webserver_parse_arguments ( struct mg_http_message *  hm)

Public functions Extract arguments from query parameters

Parameters
hmHttp message
Returns
Parsed arguments as t_list struct.

◆ webserver_send_cors_reply()

void webserver_send_cors_reply ( struct mg_connection *  nc)

Replies to preflighted requests in CORS

Parameters
ncmongoose connection

◆ webserver_send_data()

void webserver_send_data ( struct mg_connection *  nc,
const char *  data,
size_t  len,
const char *  headers 
)

Sends binary data

Parameters
ncmongoose connection
datadata to send
lenlength of the data to send
headersextra headers to add

◆ webserver_send_error()

void webserver_send_error ( struct mg_connection *  nc,
int  code,
const char *  msg 
)

Sends a http error response

Parameters
ncmongoose connection
codehttp error code
msgthe error message

◆ webserver_send_header_found()

void webserver_send_header_found ( struct mg_connection *  nc,
const char *  location,
const char *  headers 
)

Sends a 302 found header

Parameters
ncmongoose connection
locationdestination for the redirect
headersextra headers to add

◆ webserver_send_header_ok()

void webserver_send_header_ok ( struct mg_connection *  nc,
size_t  len,
const char *  headers 
)

Sends a http OK reply with content-length header

Parameters
ncmongoose connection
lenlength for the content-length header
headersextra headers to add

◆ webserver_send_header_redirect()

void webserver_send_header_redirect ( struct mg_connection *  nc,
const char *  location,
const char *  headers 
)

Sends a 301 moved permanently header

Parameters
ncmongoose connection
locationdestination for the redirect
headersextra headers to add

◆ webserver_send_raw()

void webserver_send_raw ( struct mg_connection *  nc,
const char *  data,
size_t  len 
)

Sends a raw reply

Parameters
ncmongoose connection
datadata to send
lenlength of the data to send

◆ webserver_serve_file()

void webserver_serve_file ( struct mg_connection *  nc,
struct mg_http_message *  hm,
const char *  headers,
const char *  file 
)

Serves a file defined by file from path

Parameters
ncmongoose connection
hmmongoose http message
headersextra headers to add
fileabsolute filepath to serve

Variable Documentation

◆ image_file_extensions

const char* image_file_extensions[]
static
Initial value:
= {
"webp", "jpg", "jpeg", "png", "svg", "avif",
"WEBP", "JPG", "JPEG", "PNG", "SVG", "AVIF",
NULL}

Image file extensions to detect