Ejemplo n.º 1
0
std::string Base64ImageFilter::toBase64Image(const boost::cmatch& match)
{
   // extract image reference
   std::string imgRef = match[3];

   // see if this is an image within the base directory. if it is then
   // base64 encode it
   FilePath imagePath = basePath_.childPath(imgRef);
   if (imagePath.exists() &&
       boost::algorithm::starts_with(imagePath.mimeContentType(), "image/"))
   {     
      std::string imageBase64;
      Error error = core::base64::encode(imagePath, &imageBase64);
      if (!error)
      {
         imgRef = "data:" + imagePath.mimeContentType() + ";base64,";
         imgRef.append(imageBase64);
      }
      else
      {
         LOG_ERROR(error);
      }
   }

   // return the filtered result
   return match[1] + match[2] + imgRef + match[4];
}
Ejemplo n.º 2
0
void handleContentRequest(const http::Request& request, http::Response* pResponse)
{
    // get content file info
    std::string title;
    FilePath contentFilePath;
    Error error = contentFileInfo(request.uri(), &title, &contentFilePath);
    if (error)
    {
        pResponse->setError(error);
        return;
    }

    // set private cache forever headers
    pResponse->setPrivateCacheForeverHeaders();

    // set file
    pResponse->setFile(contentFilePath, request);

    bool isUtf8 = true;
    if (boost::algorithm::starts_with(contentFilePath.mimeContentType(), "text/"))
    {
        // If the content looks like valid UTF-8, assume it is. Otherwise, assume
        // it's the system encoding.
        std::string contents;
        error = core::readStringFromFile(contentFilePath, &contents);
        if (!error)
        {
            for (std::string::iterator pos = contents.begin(); pos != contents.end(); )
            {
                error = string_utils::utf8Advance(pos, 1, contents.end(), &pos);
                if (error)
                {
                    isUtf8 = false;
                    break;
                }
            }
        }
    }

    // reset content-type with charset
    pResponse->setContentType(contentFilePath.mimeContentType() +
                              std::string("; charset=") +
                              (isUtf8 ? "UTF-8" : ::locale2charset(NULL)));

    // set title header
    pResponse->setHeader("Title", title);
}
Ejemplo n.º 3
0
// determines the icon data; this could be either a path on disk (if we have
// a suitable icon locally), base64-encoded icon data (if the icon is embedded
// in an R package), or nothing (if we cannot determine an icon at all)
std::string iconData(const std::string& iconGroup, 
      const std::string& iconName,
      const std::string& iconPath)
{
   if (iconPath.empty())
   {
      // convert the icon name into the format of our shipped icons, which is
      // all lowercase with no whitespace (e.g. "SQL Server" => "sqlserver.png")
      std::string iconFilename(string_utils::toLower(iconName));
      iconFilename = boost::regex_replace(iconFilename,
            boost::regex("\\s"), "") + ".png";

      // the package did not supply an icon; see if there's one baked in
      FilePath path = options().rResourcesPath().childPath("connections")
         .childPath(iconGroup)
         .childPath(iconFilename);
      if (path.exists())
         return std::string("connections/") + iconGroup + "/" + iconFilename;

      if (iconGroup == "drivers")
         return std::string("connections/drivers/odbc.png");

      // didn't find anything
      return std::string();
   }

   // expand the path 
   FilePath icon = module_context::resolveAliasedPath(iconPath);
   std::string iconData;

   // ensure that the icon file exists and is a small GIF, JPG, or PNG image
   if (icon.exists() && icon.size() < kMaxIconSize &&
       (icon.hasExtensionLowerCase(".gif") ||
        icon.hasExtensionLowerCase(".png") ||
        icon.hasExtensionLowerCase(".jpg") ||
        icon.hasExtensionLowerCase(".jpeg")))
   {
      Error error = base64::encode(icon, &iconData);
      if (error)
         LOG_ERROR(error);
      else
      {
         iconData = "data:" + icon.mimeContentType("image/png") + 
                    ";base64," + iconData;
      }
   }
   return iconData;
}
Ejemplo n.º 4
0
 void setFile(const FilePath& filePath, 
              const Request& request, 
              const Filter& filter)
 {
    // ensure that the file exists
    if (!filePath.exists())
    {
       setError(http::status::NotFound, request.uri() + " not found");
       return;
    }
    
    // set content type
    setContentType(filePath.mimeContentType());
    
    // gzip if possible
    if (request.acceptsEncoding(kGzipEncoding))
       setContentEncoding(kGzipEncoding);
    
    // set body from file
    Error error = setBody(filePath, filter);
    if (error)
       setError(status::InternalServerError, error.code().message());
 }