1. Aristarkh Zagorodnikov
  2. mod_gridfs

Commits

Stafford Brunk  committed 609245e

Implement context awareness in mod_gridfs

  • Participants
  • Parent commits 4b04880
  • Branches default

Comments (0)

Files changed (1)

File gridfs/mod_gridfs.cpp

View file
  • Ignore whitespace
 	bool connect_timeout_set;
 	bool slave_ok;
 	bool slave_ok_set;
+  const std::string *context;
 };
 
 //	Creates module configuration
-static void *gridfs_create_config(apr_pool_t *const pool, char *const dummy)
+static void *gridfs_create_config(apr_pool_t *const pool, char *const x)
 {
 	gridfs_config *const config = static_cast<gridfs_config *>(apr_pcalloc(pool, sizeof(gridfs_config)));
 	if (config == 0)
 		return 0;
+  void *const context_data = apr_palloc(pool, sizeof(std::string));
+  if (context_data == 0)
+    return config;
+
+  // Copy the current context to the config
+  std::string *context;
+  try
+  {
+    context = new (context_data) std::string(x);
+  }
+  catch (...)
+  {
+    return config;
+  }
+
+  config->context = context;
 	config->cache_max_age = DEFAULT_CACHE_MAX_AGE;
 	config->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
 	return config;
 	config->connect_timeout_set = add->connect_timeout_set || base->connect_timeout_set;
 	config->slave_ok = add->slave_ok_set ? add->slave_ok : base->slave_ok;
 	config->slave_ok_set = add->slave_ok_set || base->slave_ok_set;
+  config->context = add->context != 0 ? add->context : base->context;
 	return config;
 }
 
 	if (*request->uri != '/')
 		return HTTP_NOT_FOUND;
 	int result;
-	const char *const filename = apr_pstrdup(request->pool, request->uri + 1);
+  
+  // Get current context from config
+  std::string context = "";
+  if (config->context != 0) {
+    context = *config->context;
+  }
+  std::string uri = request->uri;
+
+  if (uri.length() > 1) {
+    // Remove context from uri
+    size_t start = uri.find(context);
+    if (start != std::string::npos) {
+      uri.replace(start, context.length(), "");
+    }
+
+    start = uri.find("/");
+    if (start == 0) {
+      uri.erase(0, 1);
+    }
+  }
+
+	const char *const filename = uri.c_str();
 	if (filename == 0)
 	{
 		ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, request, "mod_gridfs: Failed to allocate filename memory.");