@@ -528,6 +528,8 @@ class Server {
528528 Server &Options (const char *pattern, Handler handler);
529529
530530 bool set_base_dir (const char *dir, const char *mount_point = nullptr );
531+ void set_file_extension_and_mimetype_mapping (const char *ext,
532+ const char *mime);
531533 void set_file_request_handler (Handler handler);
532534
533535 void set_error_handler (Handler handler);
@@ -597,6 +599,7 @@ class Server {
597599 std::atomic<bool > is_running_;
598600 std::atomic<socket_t > svr_sock_;
599601 std::vector<std::pair<std::string, std::string>> base_dirs_;
602+ std::map<std::string, std::string> file_extension_and_mimetype_map_;
600603 Handler file_request_handler_;
601604 Handlers get_handlers_;
602605 Handlers post_handlers_;
@@ -1526,8 +1529,14 @@ inline std::string get_remote_addr(socket_t sock) {
15261529 return std::string ();
15271530}
15281531
1529- inline const char *find_content_type (const std::string &path) {
1532+ inline const char *
1533+ find_content_type (const std::string &path,
1534+ const std::map<std::string, std::string> &user_data) {
15301535 auto ext = file_extension (path);
1536+
1537+ auto it = user_data.find (ext);
1538+ if (it != user_data.end ()) { return it->second .c_str (); }
1539+
15311540 if (ext == " txt" ) {
15321541 return " text/plain" ;
15331542 } else if (ext == " html" || ext == " htm" ) {
@@ -1550,6 +1559,8 @@ inline const char *find_content_type(const std::string &path) {
15501559 return " application/pdf" ;
15511560 } else if (ext == " js" ) {
15521561 return " application/javascript" ;
1562+ } else if (ext == " wasm" ) {
1563+ return " application/wasm" ;
15531564 } else if (ext == " xml" ) {
15541565 return " application/xml" ;
15551566 } else if (ext == " xhtml" ) {
@@ -2860,6 +2871,11 @@ inline bool Server::set_base_dir(const char *dir, const char *mount_point) {
28602871 return false ;
28612872}
28622873
2874+ inline void Server::set_file_extension_and_mimetype_mapping (const char *ext,
2875+ const char *mime) {
2876+ file_extension_and_mimetype_map_[ext] = mime;
2877+ }
2878+
28632879inline void Server::set_file_request_handler (Handler handler) {
28642880 file_request_handler_ = std::move (handler);
28652881}
@@ -3178,7 +3194,8 @@ inline bool Server::handle_file_request(Request &req, Response &res) {
31783194
31793195 if (detail::is_file (path)) {
31803196 detail::read_file (path, res.body );
3181- auto type = detail::find_content_type (path);
3197+ auto type =
3198+ detail::find_content_type (path, file_extension_and_mimetype_map_);
31823199 if (type) { res.set_header (" Content-Type" , type); }
31833200 res.status = 200 ;
31843201 if (file_request_handler_) { file_request_handler_ (req, res); }
@@ -3666,7 +3683,7 @@ inline bool Client::write_request(Stream &strm, const Request &req,
36663683 BufferStream bstrm;
36673684
36683685 // Request line
3669- const auto & path = detail::encode_url (req.path );
3686+ const auto & path = detail::encode_url (req.path );
36703687
36713688 bstrm.write_format (" %s %s HTTP/1.1\r\n " , req.method .c_str (), path.c_str ());
36723689
0 commit comments