Webserv
Loading...
Searching...
No Matches
http::MimeTypes Class Reference

#include <MimeTypes.hpp>

Public Member Functions

 MimeTypes (const std::string &path=MIME_TYPES_PATH)
 Constructs the MimeTypes object and loads MIME types from the specified file.
 MimeTypes (const MimeTypes &other)
 Copy constructor.
MimeTypesoperator= (const MimeTypes &other)
 Assignment operator.
const std::string getMimeType (const std::string &extension) const
 Retrieves the MIME type associated with a given file extension.
void reload ()
 Reloads the MIME types from the configuration file.
 ~MimeTypes ()

Private Member Functions

bool wasChanged ()
 checks if the MIME types file has been modified since the last load.
size_t findFirstSpace (const std::string str)
size_t findFirstNonSpace (const std::string str, size_t startPos)

Private Attributes

std::map< std::string, std::string > mimeTypes_
std::string filePath_
struct timespec rtime_

Constructor & Destructor Documentation

◆ MimeTypes() [1/2]

http::MimeTypes::MimeTypes ( const std::string & path = MIME_TYPES_PATH)

Constructs the MimeTypes object and loads MIME types from the specified file.

This constructor initializes the MIME types map by reading from the provided file path. It sets the last reload time to the last modification time of the file. If the file cannot be loaded, throws a runtime_error exception.

Parameters
pathOptional parameter path to the MIME types configuration file. Defaults to "MIME_TYPES_PATH".

◆ MimeTypes() [2/2]

http::MimeTypes::MimeTypes ( const MimeTypes & other)

Copy constructor.

This constructor creates a new MimeTypes object as a copy of an existing one.

Parameters
otherThe MimeTypes object to copy.

◆ ~MimeTypes()

http::MimeTypes::~MimeTypes ( )

Member Function Documentation

◆ findFirstNonSpace()

size_t http::MimeTypes::findFirstNonSpace ( const std::string str,
size_t startPos )
private

◆ findFirstSpace()

size_t http::MimeTypes::findFirstSpace ( const std::string str)
private

◆ getMimeType()

const std::string http::MimeTypes::getMimeType ( const std::string & extension) const

Retrieves the MIME type associated with a given file extension.

This method looks up the MIME type corresponding to the provided file extension in the loaded MIME types map..

Parameters
extensionThe file extension (without the leading dot) for which to retrieve the MIME type.
Returns
The corresponding MIME type as a string, or "text/plain" if the extension is not recognized.

◆ operator=()

MimeTypes & http::MimeTypes::operator= ( const MimeTypes & other)

Assignment operator.

This operator assigns the contents of one MimeTypes object to another.

Parameters
otherThe MimeTypes object to assign from.
Returns
A reference to this MimeTypes object.

◆ reload()

void http::MimeTypes::reload ( )

Reloads the MIME types from the configuration file.

This method reads the MIME types file specified by filePath_ and populates the mimeTypes_ map with the latest MIME type definitions. It updates the rtime_ to reflect the time of the last successful reload.

◆ wasChanged()

bool http::MimeTypes::wasChanged ( )
private

checks if the MIME types file has been modified since the last load.

Member Data Documentation

◆ filePath_

std::string http::MimeTypes::filePath_
private

◆ mimeTypes_

std::map<std::string, std::string> http::MimeTypes::mimeTypes_
private

◆ rtime_

struct timespec http::MimeTypes::rtime_
private

The documentation for this class was generated from the following files: