Commits

Aristarkh Zagorodnikov committed df33f5a

Improved on wingrunr21's pull request (reduced string copying, coding style/whitespace cleanup)

Comments (0)

Files changed (1)

gridfs/mod_gridfs.cpp

 	bool connect_timeout_set;
 	bool slave_ok;
 	bool slave_ok_set;
-  const std::string *context;
+	const std::string *context;
 };
 
 //	Creates module configuration
-static void *gridfs_create_config(apr_pool_t *const pool, char *const x)
+static void *gridfs_create_config(apr_pool_t *const pool, char *const location)
 {
 	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;
+	if (location != 0 && location[0] == '/' && location[1] != '\0')
+	{
+	  	void *const context_data = apr_palloc(pool, sizeof(std::string));
+	  	if (context_data == 0)
+	    	return config;
+		std::string *context;
+		try
+		{
+			context = new (context_data) std::string(location + 1);
+			if (*context->rbegin() != '/')
+				context->append(1, '/');
+		}
+		catch (...)
+		{
+			return config;
+		}
+		config->context = context;
+	}
 	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;
+	config->context = add->context != 0 ? add->context : base->context;
 	return config;
 }
 
    	request->allowed |= AP_METHOD_BIT << M_GET;
 	if (request->method_number != M_GET)
 		return HTTP_NOT_IMPLEMENTED;
-	if (*request->uri != '/')
+	if (*request->uri != '/' || request->uri[1] == '\0')
 		return HTTP_NOT_FOUND;
-	int result;
-  
-  // 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();
+	const char * filename = request->uri + 1;
+	if (config->context != 0)
+	{
+	  	const std::string& context = *config->context;
+	  	const size_t context_length = context.length();
+	  	if (context.compare(0, context_length, filename, context_length) == 0)
+		  	filename += context_length;
+	}
+	filename = apr_pstrdup(request->pool, filename);
 	if (filename == 0)
 	{
 		ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, request, "mod_gridfs: Failed to allocate filename memory."); 
 		return HTTP_INTERNAL_SERVER_ERROR;
 	}
 	request->filename = const_cast<char *>(filename);
+	int result;
 	apr_bucket_brigade *brigade = 0;
 	try
 	{