Commits

Aristarkh Zagorodnikov  committed 80fc1a2

Used Apache brigade API instead of old ap_rwrite (fixed problems with headers and large files)
Improved error checking

  • Participants
  • Parent commits bc682fd
  • Branches default

Comments (0)

Files changed (1)

File gridfs/mod_gridfs.cpp

 		return HTTP_NOT_IMPLEMENTED;
 	if (*request->uri != '/')
 		return HTTP_NOT_FOUND;
+	int result;
 	const char *const filename = apr_pstrdup(request->pool, request->uri + 1);
 	if (!filename)
 	{
 		ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, request, "mod_gridfs: Failed to allocate filename memory."); 
 		return HTTP_INTERNAL_SERVER_ERROR;
 	}
+	apr_bucket_brigade *const brigade = request->header_only ? 0 : apr_brigade_create(request->pool, request->connection->bucket_alloc);
 	try
 	{
 		mongo::ScopedDbConnection connection(*config->connection_string, config->connect_timeout);
 				connection.done();
 				return HTTP_NOT_MODIFIED;
 			}
-			if (!request->header_only)
+			if (brigade != 0)
 			{
 				const int num_chunks = gridfile.getNumChunks();
 				for (int chunk_index = 0;chunk_index < num_chunks;chunk_index++) 
 					int chunk_length;
 					const char* chunk_data = chunk.data(chunk_length);
 					if (chunk_length != 0)
-						ap_rwrite(chunk_data, chunk_length, request);
+					{
+						if ((result = apr_brigade_write(brigade, 0, 0, chunk_data, chunk_length)) != APR_SUCCESS)
+						{
+							ap_log_rerror(APLOG_MARK, APLOG_ERR, result, request, "mod_gridfs: Failed to write chunk to brigade (length: %d).", chunk_length); 
+							connection.done();
+							apr_brigade_destroy(brigade);
+							return HTTP_INTERNAL_SERVER_ERROR;
+						}
+					}
 				}
 			}
-			else
-				ap_set_content_length(request, file_length);
+			ap_set_content_length(request, file_length);
 			if (content_type.empty())
 			{
 				request->finfo.filetype = APR_REG;
 				request->filename = const_cast<char *>(filename);
-				ap_run_type_checker(request);
+				if ((result = ap_run_type_checker(request)) != APR_SUCCESS)
+					ap_log_rerror(APLOG_MARK, APLOG_WARNING, result, request, "mod_gridfs: Failed to run type checker for file '%s'.", filename); 
 			}
 			else
 				ap_set_content_type(request, content_type.c_str());
 		apr_rfc822_date(datestr, expires_time);
 		apr_table_set(request->headers_out, "Expires", datestr);
 	}
+	if (brigade != 0)
+	    return ap_pass_brigade(request->output_filters, brigade);
 	return OK;
 }