libnavajo
web server for static and dynamic pages developement in C++
DynamicRepository.hh
Go to the documentation of this file.
1 //********************************************************
12 //********************************************************
13 
14 #ifndef DYNAMICREPOSITORY_HH_
15 #define DYNAMICREPOSITORY_HH_
16 
17 #include <string>
18 #include <map>
19 
21 
22 
24 {
25 
26  pthread_mutex_t _mutex;
27  typedef std::map<std::string, DynamicPage *> IndexMap;
28  IndexMap indexMap;
29 
30  public:
31  DynamicRepository() { pthread_mutex_init(&_mutex, NULL); };
32  virtual ~DynamicRepository() { indexMap.clear(); };
33 
34  inline void freeFile(unsigned char *webpage) { ::free (webpage); };
35 
36  inline void add(std::string url, DynamicPage *page)
37  {
38  size_t i=0;
39  while (url.size() && url[i]=='/') i++;
40  indexMap.insert(std::pair<std::string, DynamicPage *>(url.substr(i, url.size()-i), page)); };
41 
42  inline virtual bool getFile(HttpRequest* request, HttpResponse *response)
43  {
44  std::string url = request->getUrl();
45  while (url.size() && url[0]=='/') url.erase(0, 1);
46  pthread_mutex_lock( &_mutex );
47  IndexMap::const_iterator i = indexMap.find (url);
48  if (i == indexMap.end())
49  {
50  pthread_mutex_unlock( &_mutex );
51  return false;
52  }
53  else
54  {
55  pthread_mutex_unlock( &_mutex );
56  bool res = i->second->getPage( request, response );
57  if (request->getSessionId().size())
58  response->addSessionCookie(request->getSessionId());
59  return res;
60  }
61  };
62 
63 };
64 #endif
65 
Definition: WebRepository.hh:21
std::string getSessionId() const
Definition: HttpRequest.hh:357
Web Repository handler (abstract class)
Definition: DynamicPage.hh:23
Definition: HttpRequest.hh:42
Definition: DynamicRepository.hh:23
Definition: HttpResponse.hh:18
const char * getUrl() const
Definition: HttpRequest.hh:403
void addSessionCookie(const std::string &sid)
Definition: HttpResponse.hh:124