mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-08-19 03:18:15 -04:00
Some fixes:
* using ContentTypes to resolve static files too * added some default content types, in the case of mime.types file not present * resolve extensions with upper case letters in them
This commit is contained in:
parent
b847eea57e
commit
0ef9b22a7e
3 changed files with 37 additions and 27 deletions
|
@ -308,7 +308,7 @@ public:
|
||||||
unsigned int i = info.fname.rfind('.');
|
unsigned int i = info.fname.rfind('.');
|
||||||
if(i != std::string::npos)
|
if(i != std::string::npos)
|
||||||
ext = info.fname.substr(i+1);
|
ext = info.fname.substr(i+1);
|
||||||
MHD_add_response_header(resp, "Content-Type", ContentTypes::ContentTypes::cTypeFromExt(ext).c_str());
|
MHD_add_response_header(resp, "Content-Type", ContentTypes::cTypeFromExt(ext).c_str());
|
||||||
|
|
||||||
secure_queue_response(connection, MHD_HTTP_OK, resp);
|
secure_queue_response(connection, MHD_HTTP_OK, resp);
|
||||||
MHD_destroy_response(resp);
|
MHD_destroy_response(resp);
|
||||||
|
@ -650,27 +650,10 @@ int ApiServerMHD::accessHandlerCallback(MHD_Connection *connection,
|
||||||
{
|
{
|
||||||
extension = filename[i] + extension;
|
extension = filename[i] + extension;
|
||||||
i--;
|
i--;
|
||||||
}
|
};
|
||||||
const char* type = 0;
|
|
||||||
if(extension == "html")
|
|
||||||
type = "text/html";
|
|
||||||
else if(extension == "css")
|
|
||||||
type = "text/css";
|
|
||||||
else if(extension == "js")
|
|
||||||
type = "text/javascript";
|
|
||||||
else if(extension == "jsx") // react.js jsx files
|
|
||||||
type = "text/jsx";
|
|
||||||
else if(extension == "png")
|
|
||||||
type = "image/png";
|
|
||||||
else if(extension == "jpg" || extension == "jpeg")
|
|
||||||
type = "image/jpeg";
|
|
||||||
else if(extension == "gif")
|
|
||||||
type = "image/gif";
|
|
||||||
else
|
|
||||||
type = "application/octet-stream";
|
|
||||||
|
|
||||||
struct MHD_Response* resp = MHD_create_response_from_fd(s.st_size, fd);
|
struct MHD_Response* resp = MHD_create_response_from_fd(s.st_size, fd);
|
||||||
MHD_add_response_header(resp, "Content-Type", type);
|
MHD_add_response_header(resp, "Content-Type", ContentTypes::cTypeFromExt(extension).c_str());
|
||||||
secure_queue_response(connection, MHD_HTTP_OK, resp);
|
secure_queue_response(connection, MHD_HTTP_OK, resp);
|
||||||
MHD_destroy_response(resp);
|
MHD_destroy_response(resp);
|
||||||
return MHD_YES;
|
return MHD_YES;
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
#include "ContentTypes.h"
|
#include "ContentTypes.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <cctype>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
RsMutex ContentTypes::ctmtx = RsMutex("CTMTX");
|
RsMutex ContentTypes::ctmtx = RsMutex("CTMTX");
|
||||||
std::map<std::string, std::string> ContentTypes::cache;
|
std::map<std::string, std::string> ContentTypes::cache;
|
||||||
|
bool ContentTypes::inited = false;
|
||||||
|
|
||||||
#ifdef WINDOWS_SYS
|
#ifdef WINDOWS_SYS
|
||||||
//Next to the executable
|
//Next to the executable
|
||||||
|
@ -18,12 +21,18 @@ std::string ContentTypes::cTypeFromExt(const std::string &extension)
|
||||||
|
|
||||||
RsStackMutex mtx(ctmtx);
|
RsStackMutex mtx(ctmtx);
|
||||||
|
|
||||||
|
if(!inited)
|
||||||
|
addBasic();
|
||||||
|
|
||||||
|
std::string extension2(extension); //lower case
|
||||||
|
std::transform(extension2.begin(), extension2.end(), extension2.begin(),::tolower);
|
||||||
|
|
||||||
//looking into the cache
|
//looking into the cache
|
||||||
std::map<std::string,std::string>::iterator it;
|
std::map<std::string,std::string>::iterator it;
|
||||||
it = cache.find(extension);
|
it = cache.find(extension2);
|
||||||
if (it != cache.end())
|
if (it != cache.end())
|
||||||
{
|
{
|
||||||
std::cout << "Mime " + it->second + " for extension ." + extension + " was found in cache" << std::endl;
|
std::cout << "Mime " + it->second + " for extension ." + extension2 + " was found in cache" << std::endl;
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,19 +54,35 @@ std::string ContentTypes::cTypeFromExt(const std::string &extension)
|
||||||
else
|
else
|
||||||
ext = line.substr(j+1, i-j-1);
|
ext = line.substr(j+1, i-j-1);
|
||||||
|
|
||||||
if(extension == ext)
|
if(extension2 == ext)
|
||||||
{
|
{
|
||||||
std::string mime = line.substr(0, line.find_first_of("\t "));
|
std::string mime = line.substr(0, line.find_first_of("\t "));
|
||||||
cache[extension] = mime;
|
cache[extension2] = mime;
|
||||||
std::cout << "Mime " + mime + " for extension ." + extension + " was found in mime.types" << std::endl;
|
std::cout << "Mime " + mime + " for extension ." + extension2 + " was found in mime.types" << std::endl;
|
||||||
return mime;
|
return mime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//nothing found
|
//nothing found
|
||||||
std::cout << "Mime for " + extension + " was not found in " + filename + " falling back to " << DEFAULTCT << std::endl;
|
std::cout << "Mime for " + extension2 + " was not found in " + filename + " falling back to " << DEFAULTCT << std::endl;
|
||||||
cache[extension] = DEFAULTCT;
|
cache[extension2] = DEFAULTCT;
|
||||||
return DEFAULTCT;
|
return DEFAULTCT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Add some basic content-types before first use.
|
||||||
|
//It keeps webui usable in the case of mime.types file not exists.
|
||||||
|
void ContentTypes::addBasic()
|
||||||
|
{
|
||||||
|
inited = true;
|
||||||
|
|
||||||
|
cache["html"] = "text/html";
|
||||||
|
cache["css"] = "text/css";
|
||||||
|
cache["js"] = "text/javascript";
|
||||||
|
cache["jsx"] = "text/jsx";
|
||||||
|
cache["png"] = "image/png";
|
||||||
|
cache["jpg"] = "image/jpeg";
|
||||||
|
cache["jpeg"] = "image/jpeg";
|
||||||
|
cache["gif"] = "image/gif";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,8 @@ private:
|
||||||
static std::map<std::string, std::string> cache;
|
static std::map<std::string, std::string> cache;
|
||||||
static RsMutex ctmtx;
|
static RsMutex ctmtx;
|
||||||
static const char* filename;
|
static const char* filename;
|
||||||
|
static bool inited;
|
||||||
|
static void addBasic();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CONTENTTYPES_H
|
#endif // CONTENTTYPES_H
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue