metaproxy  1.21.0
Private Types | Private Member Functions | Private Attributes | Friends | List of all members
metaproxy_1::filter::HttpFile::Rep Class Reference
Collaboration diagram for metaproxy_1::filter::HttpFile::Rep:
Collaboration graph

Private Types

typedef std::list< AreaAreaList
 
typedef std::map< std::string, MimeMimeMap
 

Private Member Functions

void fetch_uri (mp::Session &session, Z_HTTP_Request *req, mp::Package &package)
 
void fetch_file (mp::Session &session, Z_HTTP_Request *req, std::string &fname, mp::Package &package, bool raw, bool passthru)
 
std::string get_mime_type (std::string &fname)
 

Private Attributes

MimeMap m_ext_to_map
 
AreaList m_area_list
 

Friends

class HttpFile
 

Detailed Description

Definition at line 62 of file filter_http_file.cpp.

Member Typedef Documentation

◆ AreaList

Definition at line 65 of file filter_http_file.cpp.

◆ MimeMap

typedef std::map<std::string,Mime> metaproxy_1::filter::HttpFile::Rep::MimeMap
private

Definition at line 66 of file filter_http_file.cpp.

Member Function Documentation

◆ fetch_file()

void mp::filter::HttpFile::Rep::fetch_file ( mp::Session &  session,
Z_HTTP_Request *  req,
std::string &  fname,
mp::Package &  package,
bool  raw,
bool  passthru 
)
private

Definition at line 136 of file filter_http_file.cpp.

140 {
141  mp::odr o(ODR_ENCODE);
142 
143  if (strcmp(req->method, "GET"))
144  {
145  if (passthru)
146  {
147  package.move();
148  }
149  else
150  {
151  Z_GDU *gdu = o.create_HTTP_Response(session, req, 405);
152  package.response() = gdu;
153  }
154  return;
155  }
156 
157  struct stat st;
158  if (stat(fname.c_str(), &st) == -1 || (st.st_mode & S_IFMT) != S_IFREG)
159  {
160  if (passthru)
161  {
162  package.move();
163  }
164  else
165  {
166  Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
167  package.response() = gdu;
168  }
169  return;
170  }
171 
172  FILE *f = fopen(fname.c_str(), "rb");
173  if (!f)
174  {
175  Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
176  package.response() = gdu;
177  return;
178  }
179  if (fseek(f, 0L, SEEK_END) == -1)
180  {
181  fclose(f);
182  Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
183  package.response() = gdu;
184  return;
185  }
186  long sz = ftell(f);
187  if (sz > 1000000L)
188  {
189  fclose(f);
190  Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
191  package.response() = gdu;
192  return;
193  }
194  rewind(f);
195  char *fbuf = (char*) odr_malloc(o, sz);
196  if (sz > 0)
197  {
198  if (fread(fbuf, sz, 1, f) != 1)
199  {
200  Z_GDU *gdu = o.create_HTTP_Response(session, req, 500);
201  package.response() = gdu;
202  fclose(f);
203  return;
204  }
205  }
206  fclose(f);
207 
208  Z_GDU *gdu = 0;
209  mp::odr decode(ODR_DECODE);
210  if (raw)
211  {
212  odr_setbuf(decode, (char *) fbuf, sz, 0);
213  int r = z_GDU(decode, &gdu, 0, 0);
214  if (!r)
215  {
216  gdu = o.create_HTTP_Response(session, req, 500);
217  }
218  }
219  else
220  {
221  gdu = o.create_HTTP_Response(session, req, 200);
222  Z_HTTP_Response *hres = gdu->u.HTTP_Response;
223  hres->content_len = sz;
224  hres->content_buf = fbuf;
225  std::string content_type = get_mime_type(fname);
226  z_HTTP_header_add(o, &hres->headers,
227  "Content-Type", content_type.c_str());
228  }
229  package.response() = gdu;
230 }
std::string get_mime_type(std::string &fname)

◆ fetch_uri()

void mp::filter::HttpFile::Rep::fetch_uri ( mp::Session &  session,
Z_HTTP_Request *  req,
mp::Package &  package 
)
private

Definition at line 232 of file filter_http_file.cpp.

234 {
235  bool sane = true;
236  std::string::size_type p;
237  std::string path = req->path;
238 
239  p = path.find("#");
240  if (p != std::string::npos)
241  path = path.erase(p);
242 
243  p = path.find("?");
244  if (p != std::string::npos)
245  path = path.erase(p);
246 
247  path = mp::util::uri_decode(path);
248 
249  // we don't allow ..
250  p = path.find("..");
251  if (p != std::string::npos)
252  sane = false;
253 
254  if (sane)
255  {
256  AreaList::const_iterator it;
257  for (it = m_area_list.begin(); it != m_area_list.end(); it++)
258  {
259  std::string::size_type l = it->m_url_path_prefix.length();
260 
261  if (path.compare(0, l, it->m_url_path_prefix) == 0)
262  {
263  std::string fname = it->m_file_root + path.substr(l);
264  package.log("http_file", YLOG_LOG, "%s", fname.c_str());
265  fetch_file(session, req, fname, package, it->m_raw,
266  it->m_passthru);
267  return;
268  }
269  }
270  }
271  package.move();
272 }
void fetch_file(mp::Session &session, Z_HTTP_Request *req, std::string &fname, mp::Package &package, bool raw, bool passthru)

◆ get_mime_type()

std::string mp::filter::HttpFile::Rep::get_mime_type ( std::string &  fname)
private

Definition at line 113 of file filter_http_file.cpp.

114 {
115  std::string file_part = fname;
116  std::string::size_type p = fname.find_last_of('/');
117 
118  if (p != std::string::npos)
119  file_part = fname.substr(p+1);
120 
121  p = file_part.find_last_of('.');
122  std::string content_type;
123  if (p != std::string::npos)
124  {
125  std::string ext = file_part.substr(p+1);
126  MimeMap::const_iterator it = m_ext_to_map.find(ext);
127 
128  if (it != m_ext_to_map.end())
129  content_type = it->second.m_type;
130  }
131  if (content_type.length() == 0)
132  content_type = "application/octet-stream";
133  return content_type;
134 }

Friends And Related Function Documentation

◆ HttpFile

friend class HttpFile
friend

Definition at line 63 of file filter_http_file.cpp.

Member Data Documentation

◆ m_area_list

AreaList metaproxy_1::filter::HttpFile::Rep::m_area_list
private

Definition at line 69 of file filter_http_file.cpp.

◆ m_ext_to_map

MimeMap metaproxy_1::filter::HttpFile::Rep::m_ext_to_map
private

Definition at line 68 of file filter_http_file.cpp.


The documentation for this class was generated from the following file: