libUPnP 22.3.0
webserver.c File Reference

Defines the Web Server and has functions to carry out operations of the Web Server. More...

#include "LinkedList.h"
#include "UpnpGlobal.h"
#include "config.h"
#include "list.h"
#include "sock.h"
#include "upnp.h"
#include "upnpdebug.h"
#include "upnpdebug_internal.h"
#include "uri.h"
#include <ctype.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include "webserver.h"
#include "UpnpExtraHeaders.h"
#include "UpnpFileInfo.h"
#include "VirtualDir.h"
#include "httpparser.h"
#include "httpreadwrite.h"
#include "ithread.h"
#include "membuffer.h"
#include "ssdplib.h"
#include "statcodes.h"
#include "strintmap.h"
#include "upnpapi.h"
#include <assert.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "posix_overwrites.h"
Include dependency graph for webserver.c:

Data Structures

struct  document_type_t
struct  xml_alias_t

Macros

#define AUDIO_STR   "\1"
#define VIDEO_STR   "\2"
#define IMAGE_STR   "\3"
#define APPLICATION_STR   "\4"
#define TEXT_STR   "\5"
#define APPLICATION_INDEX   4
#define TEXT_INDEX   5
#define NUM_MEDIA_TYPES   70
#define NUM_HTTP_HEADER_NAMES   33
#define ASCTIME_R_BUFFER_SIZE   26

Enumerations

enum  resp_type {
  RESP_FILEDOC , RESP_XMLDOC , RESP_HEADERS , RESP_WEBDOC ,
  RESP_POST
}

Functions

static char * web_server_asctime_r (const struct tm *tm, char *buf)
static UPNP_INLINE void media_list_init (void)
 Decodes list and stores it in gMediaTypeList.
static UPNP_INLINE int search_extension (const char *extension, const char **con_type, const char **con_subtype)
 Based on the extension, returns the content type and content subtype.
static UPNP_INLINE int get_content_type (const char *filename, UpnpFileInfo *fileInfo)
 Based on the extension, clones an XML string based on type and content subtype. If content type and sub type are not found, unknown types are used.
static UPNP_INLINE void glob_alias_init (void)
 Initialize the global XML document. Allocate buffers for the XML document.
static UPNP_INLINE int is_valid_alias (const struct xml_alias_t *alias)
 Check for the validity of the XML object buffer.
static int alias_grab (struct xml_alias_t *alias)
 Copy the contents of the global XML document into the local output parameter and take a reference to it.
static void alias_release (struct xml_alias_t *alias)
 Release the XML document referred to by the input parameter. Free the allocated buffers associated with this object.
int web_server_set_alias (const char *alias_name, const char *alias_content, size_t alias_content_length, time_t last_modified)
 Replaces current alias with the given alias. To remove the current alias, set alias_name to NULL.
UPNP_EXPORT_SPEC void web_server_ut_grab_alias (void)
UPNP_EXPORT_SPEC void web_server_ut_release_alias (void)
UPNP_EXPORT_SPEC void web_server_ut_set_post_timeout (int seconds)
UPNP_EXPORT_SPEC int web_server_ut_set_alias (const char *name, const char *content, size_t len)
int web_server_init ()
 Initilialize the different documents. Initialize the memory for root directory for web server. Call to initialize global XML document. Sets bWebServerState to WEB_SERVER_ENABLED.
void web_server_destroy (void)
 Release memory allocated for the global web server root directory and the global XML document. Resets the flag bWebServerState to WEB_SERVER_DISABLED.
static int get_file_info (const char *filename, UpnpFileInfo *info)
 Release memory allocated for the global web server root directory and the global XML document. Resets the flag bWebServerState to WEB_SERVER_DISABLED.
int web_server_set_root_dir (const char *root_dir)
 Assign the path specfied by the input const char* root_dir parameter to the global Document root directory. Also check for path names ending in '/'.
int web_server_set_cors (const char *cors_string)
 Assign the Access-Control-Allow-Origin specfied by the input const char* cors_string parameterto the global CORS string.
static UPNP_INLINE int get_alias (const char *request_file, struct xml_alias_t *alias, UpnpFileInfo *info)
 Compare the files names between the one on the XML alias the one passed in as the input parameter. If equal extract file information.
static int isFileInVirtualDir (char *filePath, const void **cookie)
 Compares filePath with paths from the list of virtual directory lists.
static void ToUpperCase (char *s)
 Converts input string to upper case.
static char * StrStr (char *s1, const char *s2)
 Finds a substring from a string in a case insensitive way.
static char * StrTok (char **Src, const char *Del)
 Finds next token in a string.
static int GetNextRange (char **SrcRangeStr, off_t *FirstByte, off_t *LastByte)
 Returns a range of integers from a string.
static int CreateHTTPRangeResponseHeader (char *ByteRangeSpecifier, off_t FileLength, struct SendInstruction *Instr)
 Fills in the Offset, read size and contents to send out as an HTTP Range Response.
static int CheckOtherHTTPHeaders (http_message_t *Req, struct SendInstruction *RespInstr, off_t FileSize)
 Get header id from the request parameter and take appropriate action based on the ids as an HTTP Range Response.
static void FreeExtraHTTPHeaders (UpnpListHead *extraHeadersList)
static int ExtraHTTPHeaders (http_message_t *Req, UpnpListHead *extraHeadersList)
 Build an array of unrecognized headers.
static int process_request (SOCKINFO *info, http_message_t *req, enum resp_type *rtype, membuffer *headers, membuffer *filename, struct xml_alias_t *alias, struct SendInstruction *RespInstr)
 Processes the request and returns the result in the output parameters.
static int http_RecvPostMessage (http_parser_t *parser, SOCKINFO *info, char *filename, struct SendInstruction *Instr)
 Receives the HTTP post message.
static void send_range_not_satisfiable (SOCKINFO *info, http_message_t *req, struct SendInstruction *RespInstr)
 Sends the 416 response with the Content-Range header that tells the length of the file.
void web_server_callback (http_parser_t *parser, http_message_t *req, SOCKINFO *info)
 Main entry point into web server; Handles HTTP GET and HEAD requests.

Variables

static const char * gMediaTypes []
static const char * gEncodedMediaTypes
static struct document_type_t gMediaTypeList [70]
membuffer gDocumentRootDir
membuffer gWebserverCorsString
static struct xml_alias_t gAliasDoc
static ithread_mutex_t gWebMutex
str_int_entry Http_Header_Names [33]
static int gPostBodyTimeout = HTTP_DEFAULT_TIMEOUT
static struct xml_alias_t g_ut_alias

Detailed Description

Defines the Web Server and has functions to carry out operations of the Web Server.

Enumeration Type Documentation

◆ resp_type

enum resp_type

Response Types.

Function Documentation

◆ alias_grab()

int alias_grab ( struct xml_alias_t * alias)
static

Copy the contents of the global XML document into the local output parameter and take a reference to it.

The application can uninstall or replace the alias at any time (device unregistration or registration), so it is checked under the same lock that takes the reference.

Returns
1 if a reference was taken, 0 if no alias is installed (alias is then left unchanged).
Parameters
[out]aliasXML alias object.

References xml_alias_t::ct, gAliasDoc, and is_valid_alias().

Referenced by process_request().

◆ alias_release()

void alias_release ( struct xml_alias_t * alias)
static

Release the XML document referred to by the input parameter. Free the allocated buffers associated with this object.

Parameters
[in]aliasXML alias object.

References xml_alias_t::ct, xml_alias_t::doc, gAliasDoc, glob_alias_init(), is_valid_alias(), and xml_alias_t::name.

Referenced by process_request(), web_server_callback(), web_server_destroy(), and web_server_set_alias().

◆ CheckOtherHTTPHeaders()

int CheckOtherHTTPHeaders ( http_message_t * Req,
struct SendInstruction * RespInstr,
off_t FileSize )
static

Get header id from the request parameter and take appropriate action based on the ids as an HTTP Range Response.

Returns
  • HTTP_BAD_REQUEST
  • HTTP_INTERNAL_SERVER_ERROR
  • HTTP_REQUEST_RANGE_NOT_SATISFIABLE
  • HTTP_OK
Parameters
[in]ReqHTTP Request message.
[out]RespInstrSend Instruction object to data for the response.
FileSizeSize of the file containing the request document.

References membuffer::buf, memptr::buf, CreateHTTPRangeResponseHeader(), http_message_t::headers, membuffer::length, memptr::length, http_header_t::name, SendInstruction::RecvWriteSize, StrStr(), and http_header_t::value.

Referenced by process_request().

◆ CreateHTTPRangeResponseHeader()

int CreateHTTPRangeResponseHeader ( char * ByteRangeSpecifier,
off_t FileLength,
struct SendInstruction * Instr )
static

Fills in the Offset, read size and contents to send out as an HTTP Range Response.

Returns
  • HTTP_BAD_REQUEST
  • HTTP_INTERNAL_SERVER_ERROR
  • HTTP_REQUEST_RANGE_NOT_SATISFIABLE
  • HTTP_OK
Parameters
ByteRangeSpecifierString containing the range.
FileLengthLength of the file.
[out]InstrSendInstruction object where the range operations will be stored.

References GetNextRange(), SendInstruction::ReadSendSize, and StrStr().

Referenced by CheckOtherHTTPHeaders().

◆ ExtraHTTPHeaders()

int ExtraHTTPHeaders ( http_message_t * Req,
UpnpListHead * extraHeadersList )
static

◆ FreeExtraHTTPHeaders()

void FreeExtraHTTPHeaders ( UpnpListHead * extraHeadersList)
static
Parameters
[in]extraHeadersListextra HTTP headers to free.

References UpnpExtraHeaders_delete().

Referenced by ExtraHTTPHeaders(), and process_request().

◆ get_alias()

UPNP_INLINE int get_alias ( const char * request_file,
struct xml_alias_t * alias,
UpnpFileInfo * info )
static

Compare the files names between the one on the XML alias the one passed in as the input parameter. If equal extract file information.

Returns
  • 1 - On Success
  • 0 if request is not an alias
Parameters
[in]request_filerequest file passed in to be compared with.
[out]aliasxml alias object which has a file name stored.
[out]infoFile information object which will be filled up if the file comparison succeeds.

References membuffer::buf, xml_alias_t::doc, xml_alias_t::last_modified, membuffer::length, xml_alias_t::name, UPNP_INLINE, UpnpFileInfo_set_FileLength(), UpnpFileInfo_set_IsDirectory(), UpnpFileInfo_set_IsReadable(), and UpnpFileInfo_set_LastModified().

Referenced by process_request().

◆ get_content_type()

UPNP_INLINE int get_content_type ( const char * filename,
UpnpFileInfo * fileInfo )
static

Based on the extension, clones an XML string based on type and content subtype. If content type and sub type are not found, unknown types are used.

Returns
  • 0 on success.
  • UPNP_E_OUTOF_MEMORY - on memory allocation failures.
Parameters
[in]filename.
[out]fileInfo.

References search_extension(), UPNP_E_OUTOF_MEMORY, UPNP_INLINE, UpnpFileInfo_get_ContentType(), and UpnpFileInfo_set_ContentType().

Referenced by get_file_info().

◆ get_file_info()

int get_file_info ( const char * filename,
UpnpFileInfo * info )
static

Release memory allocated for the global web server root directory and the global XML document. Resets the flag bWebServerState to WEB_SERVER_DISABLED.

Returns
Integer.
Parameters
[in]filenameFilename having the description document.
[out]infoFile information object having file attributes such as filelength, when was the file last modified, whether a file or a directory and whether the file or directory is readable.

References get_content_type(), UpnpFileInfo_get_FileLength(), UpnpFileInfo_get_IsReadable(), UpnpFileInfo_get_LastModified(), UpnpFileInfo_set_ContentType(), UpnpFileInfo_set_FileLength(), UpnpFileInfo_set_IsDirectory(), UpnpFileInfo_set_IsReadable(), UpnpFileInfo_set_LastModified(), and UpnpPrintf().

Referenced by process_request().

◆ GetNextRange()

int GetNextRange ( char ** SrcRangeStr,
off_t * FirstByte,
off_t * LastByte )
static

Returns a range of integers from a string.

Returns
Always returns 1.
Parameters
SrcRangeStrstring containing the token / range.
FirstBytegets the first byte of the token.
LastBytegets the last byte of the token.

References StrTok().

Referenced by CreateHTTPRangeResponseHeader().

◆ glob_alias_init()

UPNP_INLINE void glob_alias_init ( void )
static

Initialize the global XML document. Allocate buffers for the XML document.

References xml_alias_t::ct, xml_alias_t::doc, gAliasDoc, xml_alias_t::last_modified, xml_alias_t::name, and UPNP_INLINE.

Referenced by alias_release(), and web_server_init().

◆ http_RecvPostMessage()

int http_RecvPostMessage ( http_parser_t * parser,
SOCKINFO * info,
char * filename,
struct SendInstruction * Instr )
static

Receives the HTTP post message.

Returns
  • HTTP_INTERNAL_SERVER_ERROR
  • HTTP_NOT_FOUND
  • HTTP_BAD_REQUEST
  • HTTP_SERVICE_UNAVAILABLE
  • HTTP_OK
Parameters
parserHTTP Parser object.
[in]infoSocket Information object.
filenameFile where received data is copied to.
InstrSend Instruction object which gives information whether the file is a virtual file or not.

References membuffer::buf, SendInstruction::Cookie, http_message_t::entity, http_parser_t::entity_start_position, g_maxContentLength, gPostBodyTimeout, http_parser_t::http_error_code, memptr::length, http_message_t::msg, PARSE_CONTINUE_1, PARSE_INCOMPLETE, PARSE_INCOMPLETE_ENTITY, PARSE_OK, PARSE_SUCCESS, SendInstruction::RequestCookie, sock_read(), UpnpPrintf(), and virtualDirCallback.

Referenced by web_server_callback().

◆ is_valid_alias()

UPNP_INLINE int is_valid_alias ( const struct xml_alias_t * alias)
static

Check for the validity of the XML object buffer.

Returns
int.
Parameters
[in]aliasXML alias object.

References membuffer::buf, xml_alias_t::doc, and UPNP_INLINE.

Referenced by alias_grab(), and alias_release().

◆ isFileInVirtualDir()

int isFileInVirtualDir ( char * filePath,
const void ** cookie )
static

Compares filePath with paths from the list of virtual directory lists.

Returns
int.
Parameters
[in]filePathDirectory path to be tested for virtual directory.
[out]cookieThe cookie registered with this virtual directory, if matched.

References gWebServerSettingsMutex, and pVirtualDirList.

Referenced by process_request().

◆ media_list_init()

UPNP_INLINE void media_list_init ( void )
static

Decodes list and stores it in gMediaTypeList.

References document_type_t::content_subtype, document_type_t::content_type, document_type_t::file_ext, gMediaTypeList, and UPNP_INLINE.

Referenced by web_server_init().

◆ process_request()

int process_request ( SOCKINFO * info,
http_message_t * req,
enum resp_type * rtype,
membuffer * headers,
membuffer * filename,
struct xml_alias_t * alias,
struct SendInstruction * RespInstr )
static

Processes the request and returns the result in the output parameters.

Returns
  • HTTP_BAD_REQUEST
  • HTTP_INTERNAL_SERVER_ERROR
  • HTTP_REQUEST_RANGE_NOT_SATISFIABLE
  • HTTP_FORBIDDEN
  • HTTP_NOT_FOUND
  • HTTP_NOT_ACCEPTABLE
  • HTTP_OK
Parameters
[in]infoSocket info.
[in]reqHTTP Request message.
[out]rtypeType of response.
[out]headersHeaders.
[out]filenameGet filename from request document.
[out]aliasXml alias document from the request document.
[out]RespInstrSend Instruction object where the response is set up.

References alias_grab(), alias_release(), membuffer::buf, memptr::buf, CheckOtherHTTPHeaders(), SendInstruction::Cookie, SendInstruction::CorsHeader, ExtraHTTPHeaders(), SOCKINFO::foreign_sockaddr, FreeExtraHTTPHeaders(), gDocumentRootDir, get_alias(), get_file_info(), gWebserverCorsString, gWebServerSettingsMutex, http_MakeMessage(), isFileInVirtualDir(), membuffer::length, memptr::length, http_message_t::method, SendInstruction::ReadSendSize, remove_dots(), remove_escaped_chars(), SendInstruction::RequestCookie, UPNP_E_SUCCESS, UpnpFileInfo_delete(), UpnpFileInfo_get_ContentType(), UpnpFileInfo_get_ExtraHeadersList(), UpnpFileInfo_get_FileLength(), UpnpFileInfo_get_IsDirectory(), UpnpFileInfo_get_IsReadable(), UpnpFileInfo_get_LastModified(), UpnpFileInfo_new(), UpnpFileInfo_set_ContentType(), UpnpFileInfo_set_CtrlPtIPAddr(), UpnpFileInfo_strncpy_Os(), http_message_t::uri, and virtualDirCallback.

Referenced by web_server_callback().

◆ search_extension()

UPNP_INLINE int search_extension ( const char * extension,
const char ** con_type,
const char ** con_subtype )
static

Based on the extension, returns the content type and content subtype.

Returns
  • 0 on success
  • -1 on error
Parameters
[in]extension.
[out]con_type.
[out]con_subtype.

References document_type_t::file_ext, gMediaTypeList, and UPNP_INLINE.

Referenced by get_content_type().

◆ send_range_not_satisfiable()

void send_range_not_satisfiable ( SOCKINFO * info,
http_message_t * req,
struct SendInstruction * RespInstr )
static

Sends the 416 response with the Content-Range header that tells the length of the file.

Parameters
[in]infoSocket info.
[in]reqHTTP request.
[in]RespInstrSend instruction with the range header.

References membuffer::buf, http_MakeMessage(), http_SendMessage(), and membuffer::length.

Referenced by web_server_callback().

◆ StrStr()

char * StrStr ( char * s1,
const char * s2 )
static

Finds a substring from a string in a case insensitive way.

Returns
A pointer to the first occurrence of s2 in s1.
Parameters
s1Input string.
s2Input sub-string.

References ToUpperCase().

Referenced by CheckOtherHTTPHeaders(), and CreateHTTPRangeResponseHeader().

◆ StrTok()

char * StrTok ( char ** Src,
const char * Del )
static

Finds next token in a string.

Returns
Pointer to the next token.
Parameters
SrcString containing the token.
DelSet of delimiter characters.

Referenced by GetNextRange().

◆ ToUpperCase()

void ToUpperCase ( char * s)
static

Converts input string to upper case.

Parameters
sInput string to be converted.

Referenced by StrStr().

◆ web_server_callback()

void web_server_callback ( http_parser_t * parser,
http_message_t * req,
SOCKINFO * info )

Main entry point into web server; Handles HTTP GET and HEAD requests.

Parameters
[in]parser.
[in]req.
[in,out]info.

References alias_release(), membuffer::buf, xml_alias_t::doc, http_MakeMessage(), http_RecvPostMessage(), http_SendMessage(), membuffer::length, process_request(), SendInstruction::ReadSendSize, send_range_not_satisfiable(), and UpnpPrintf().

Referenced by UpnpEnableWebserver().

◆ web_server_destroy()

void web_server_destroy ( void )

Release memory allocated for the global web server root directory and the global XML document. Resets the flag bWebServerState to WEB_SERVER_DISABLED.

References alias_release(), bWebServerState, gAliasDoc, gDocumentRootDir, gWebserverCorsString, and gWebServerSettingsMutex.

Referenced by UpnpEnableWebserver(), and UpnpFinish().

◆ web_server_init()

int web_server_init ( void )

Initilialize the different documents. Initialize the memory for root directory for web server. Call to initialize global XML document. Sets bWebServerState to WEB_SERVER_ENABLED.

Note
alias_content is not freed here
Returns
  • 0 - OK
  • UPNP_E_OUTOF_MEMORY

References bWebServerState, gDocumentRootDir, glob_alias_init(), gWebserverCorsString, media_list_init(), pVirtualDirList, UPNP_E_OUTOF_MEMORY, UPNP_E_SUCCESS, and virtualDirCallback.

Referenced by UpnpEnableWebserver().

◆ web_server_set_alias()

int web_server_set_alias ( const char * alias_name,
const char * alias_content,
size_t alias_content_length,
time_t last_modified )

Replaces current alias with the given alias. To remove the current alias, set alias_name to NULL.

Note
alias_content is not freed here
Returns
  • 0 - OK
  • UPNP_E_OUTOF_MEMORY
Parameters
[in]alias_nameWebserver name of alias; created by caller and freed by caller (doesn't even have to be malloc()d.
[in]alias_contentThe xml doc; this is allocated by the caller; and freed by the web server.
[in]alias_content_lengthLength of alias body in bytes.
[in]last_modifiedTime when the contents of alias were last changed (local time).

References alias_release(), xml_alias_t::ct, xml_alias_t::doc, gAliasDoc, xml_alias_t::last_modified, xml_alias_t::name, and UPNP_E_OUTOF_MEMORY.

Referenced by UpnpUnRegisterRootDeviceLowPower().

◆ web_server_set_cors()

int web_server_set_cors ( const char * cors_string)

Assign the Access-Control-Allow-Origin specfied by the input const char* cors_string parameterto the global CORS string.

Returns
Integer.
Parameters
[in]cors_stringString having the Access-Control-Allow-Origin string.

References gWebserverCorsString, and gWebServerSettingsMutex.

Referenced by UpnpSetWebServerCorsString().

◆ web_server_set_root_dir()

int web_server_set_root_dir ( const char * root_dir)

Assign the path specfied by the input const char* root_dir parameter to the global Document root directory. Also check for path names ending in '/'.

Returns
Integer.
Parameters
[in]root_dirString having the root directory for the document.

References gDocumentRootDir, and gWebServerSettingsMutex.

Referenced by UpnpSetWebServerRootDir().

Variable Documentation

◆ gAliasDoc

struct xml_alias_t gAliasDoc
static

◆ gMediaTypeList

struct document_type_t gMediaTypeList[70]
static

module variables - Globals, static and externs.

Referenced by media_list_init(), and search_extension().

◆ gMediaTypes

const char* gMediaTypes[]
static
Initial value:
= {
NULL,
"audio",
"video",
"image",
"application",
"text"}

◆ gPostBodyTimeout

int gPostBodyTimeout = HTTP_DEFAULT_TIMEOUT
static

Seconds a POST body to a virtual directory may go without data before the request is dropped; only the unit tests change it, see web_server_ut_set_post_timeout().

Referenced by http_RecvPostMessage().