diff UnitTestsSources/RestApiTests.cpp @ 1783:dbb07eb1a2f3

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 18 Nov 2015 09:15:30 +0100
parents 9f2df9bb2cdf
children b1291df2f780
line wrap: on
line diff
--- a/UnitTestsSources/RestApiTests.cpp	Wed Nov 18 09:05:43 2015 +0100
+++ b/UnitTestsSources/RestApiTests.cpp	Wed Nov 18 09:15:30 2015 +0100
@@ -45,6 +45,7 @@
 #include "../Core/OrthancException.h"
 #include "../Core/Compression/ZlibCompressor.h"
 #include "../Core/RestApi/RestApiHierarchy.h"
+#include "../Core/HttpServer/HttpContentNegociation.h"
 
 using namespace Orthanc;
 
@@ -341,268 +342,6 @@
 
 
 
-namespace Orthanc
-{
-  class HttpContentNegociation : public boost::noncopyable
-  {
-  public:
-    typedef std::map<std::string, std::string>  HttpHeaders;
-
-    class IHandler : public boost::noncopyable
-    {
-    public:
-      virtual ~IHandler()
-      {
-      }
-
-      virtual void Handle(const std::string& type,
-                          const std::string& subtype) = 0;
-    };
-
-  private:
-    struct Handler
-    {
-      std::string  type_;
-      std::string  subtype_;
-      IHandler&    handler_;
-
-      Handler(const std::string& type,
-              const std::string& subtype,
-              IHandler& handler) :
-        type_(type),
-        subtype_(subtype),
-        handler_(handler)
-      {
-      }
-
-      bool IsMatch(const std::string& type,
-                   const std::string& subtype) const
-      {
-        if (type == "*" && subtype == "*")
-        {
-          return true;
-        }
-        
-        if (subtype == "*" && type == type_)
-        {
-          return true;
-        }
-
-        return type == type_ && subtype == subtype_;
-      }
-
-      void Call() const
-      {
-        handler_.Handle(type_, subtype_);
-      }
-    };
-
-
-    struct Reference : public boost::noncopyable
-    {
-      const Handler&  handler_;
-      uint8_t         level_;
-      float           quality_;
-
-      Reference(const Handler& handler,
-                const std::string& type,
-                const std::string& subtype,
-                float quality) :
-        handler_(handler),
-        quality_(quality)
-      {
-        if (type == "*" && subtype == "*")
-        {
-          level_ = 0;
-        }
-        else if (subtype == "*")
-        {
-          level_ = 1;
-        }
-        else
-        {
-          level_ = 2;
-        }
-      }
-      
-      bool operator< (const Reference& other) const
-      {
-        if (level_ < other.level_)
-        {
-          return true;
-        }
-
-        if (level_ > other.level_)
-        {
-          return false;
-        }
-
-        return quality_ < other.quality_;
-      }
-    };
-
-
-    typedef std::vector<std::string>  Tokens;
-    typedef std::list<Handler>   Handlers;
-
-    Handlers  handlers_;
-
-
-    static bool SplitPair(std::string& first /* out */,
-                          std::string& second /* out */,
-                          const std::string& source,
-                          char separator)
-    {
-      size_t pos = source.find(separator);
-
-      if (pos == std::string::npos)
-      {
-        return false;
-      }
-      else
-      {
-        first = Toolbox::StripSpaces(source.substr(0, pos));
-        second = Toolbox::StripSpaces(source.substr(pos + 1));
-        return true;      
-      }
-    }
-
-
-    static float GetQuality(const Tokens& parameters)
-    {
-      for (size_t i = 1; i < parameters.size(); i++)
-      {
-        std::string key, value;
-        if (SplitPair(key, value, parameters[i], '=') &&
-            key == "q")
-        {
-          float quality;
-          bool ok = false;
-
-          try
-          {
-            quality = boost::lexical_cast<float>(value);
-            ok = (quality >= 0.0f && quality <= 1.0f);
-          }
-          catch (boost::bad_lexical_cast&)
-          {
-          }
-
-          if (ok)
-          {
-            return quality;
-          }
-          else
-          {
-            LOG(ERROR) << "Quality parameter out of range in a HTTP request (must be between 0 and 1): " << value;
-            throw OrthancException(ErrorCode_BadRequest);
-          }
-        }
-      }
-
-      return 1.0f;  // Default quality
-    }
-
-
-    static void SelectBestMatch(std::auto_ptr<Reference>& best,
-                                const Handler& handler,
-                                const std::string& type,
-                                const std::string& subtype,
-                                float quality)
-    {
-      std::auto_ptr<Reference> match(new Reference(handler, type, subtype, quality));
-
-      if (best.get() == NULL ||
-          *best < *match)
-      {
-        best = match;
-      }
-    }
-
-
-  public:
-    void Register(const std::string& mime,
-                  IHandler& handler)
-    {
-      std::string type, subtype;
-
-      if (SplitPair(type, subtype, mime, '/') &&
-          type != "*" &&
-          subtype != "*")
-      {
-        handlers_.push_back(Handler(type, subtype, handler));
-      }
-      else
-      {
-        throw OrthancException(ErrorCode_ParameterOutOfRange);
-      }
-    }
-
-    
-    bool Apply(const HttpHeaders& headers)
-    {
-      HttpHeaders::const_iterator accept = headers.find("accept");
-      if (accept != headers.end())
-      {
-        return Apply(accept->second);
-      }
-      else
-      {
-        return Apply("*/*");
-      }
-    }
-
-
-    bool Apply(const std::string& accept)
-    {
-      // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
-      // https://en.wikipedia.org/wiki/Content_negotiation
-      // http://www.newmediacampaigns.com/blog/browser-rest-http-accept-headers
-
-      Tokens mediaRanges;
-      Toolbox::TokenizeString(mediaRanges, accept, ',');
-
-      std::auto_ptr<Reference> bestMatch;
-
-      for (Tokens::const_iterator it = mediaRanges.begin();
-           it != mediaRanges.end(); ++it)
-      {
-        Tokens parameters;
-        Toolbox::TokenizeString(parameters, *it, ';');
-
-        if (parameters.size() > 0)
-        {
-          float quality = GetQuality(parameters);
-
-          std::string type, subtype;
-          if (SplitPair(type, subtype, parameters[0], '/'))
-          {
-            for (Handlers::const_iterator it2 = handlers_.begin();
-                 it2 != handlers_.end(); ++it2)
-            {
-              if (it2->IsMatch(type, subtype))
-              {
-                SelectBestMatch(bestMatch, *it2, type, subtype, quality);
-              }
-            }
-          }
-        }
-      }
-
-      if (bestMatch.get() == NULL)  // No match was found
-      {
-        return false;
-      }
-      else
-      {
-        bestMatch->handler_.Call();
-        return true;
-      }
-    }
-  };
-}
-
-
 
 namespace
 {