libnavajo
web server for static and dynamic pages developement in C++
PrecompiledRepository.hh
Go to the documentation of this file.
1 //********************************************************
12 //********************************************************
13 
14 #ifndef PRECOMPILEDREPOSITORY_HH_
15 #define PRECOMPILEDREPOSITORY_HH_
16 
17 #include <string>
18 #include <map>
19 #include "nvjThread.h"
20 
22 
23 
25 {
26  pthread_mutex_t _mutex;
27 
28  struct WebStaticPage
29  {
30  const unsigned char* data;
31  size_t length;
32  WebStaticPage(const unsigned char* d,size_t l) : data(d), length(l) {};
33  } ;
34 
35  typedef std::map<std::string, const WebStaticPage> IndexMap;
36  static IndexMap indexMap;
37  static std::string location;
38 
39  public:
40  PrecompiledRepository(const std::string& l="")
41  {
42  location=l;
43  while (location.size() && location[0]=='/') location.erase(0, 1);
44  while (location.size() && location[location.size()-1]=='/') location.erase(location.size() - 1);
45  pthread_mutex_init(&_mutex, NULL);
46  if (!indexMap.size()) initIndexMap();
47  };
48  virtual ~PrecompiledRepository() { indexMap.clear(); };
49 
50  static void initIndexMap();
51 
52  inline void freeFile(unsigned char *webpage) { };
53 
54  inline virtual bool getFile(HttpRequest* request, HttpResponse *response)
55  {
56  std::string url = request->getUrl();
57  if (url.compare(0, location.length(), location) != 0)
58  return false;
59 
60  url.erase(0, location.length());
61  while (url.size() && url[0]=='/') url.erase(0, 1);
62  if (!url.size()) url="index.html";
63 
64  size_t webpageLen;
65  unsigned char *webpage;
66  pthread_mutex_lock( &_mutex );
67  IndexMap::const_iterator i = indexMap.find (url);
68  if (i == indexMap.end())
69  {
70  i = indexMap.find (url + ".gz");
71  if (i == indexMap.end())
72  {
73  pthread_mutex_unlock( &_mutex );
74  return false;
75  }
76  else
77  response->setIsZipped(true);
78  }
79 
80  webpage=(unsigned char*)((i->second).data); webpageLen=(i->second).length;
81  pthread_mutex_unlock( &_mutex );
82  response->setContent (webpage, webpageLen);
83  return true;
84 
85  };
86 };
87 
88 #endif
89 
Definition: WebRepository.hh:21
void setIsZipped(bool b=true)
Definition: HttpResponse.hh:66
Web Repository handler (abstract class)
Definition: PrecompiledRepository.hh:24
Definition: HttpRequest.hh:42
void setContent(unsigned char *content, size_t length)
Definition: HttpResponse.hh:40
Definition: HttpResponse.hh:18
const char * getUrl() const
Definition: HttpRequest.hh:394
thread&#39;s facilities