Change property listing action in the SVN-ACTION DAV logging names to
[svn.git] / subversion / mod_dav_svn / util.c
bloba9148c09b7b57e0b18d937db13cc757de467de62
1 /*
2 * util.c: some handy utility functions
4 * ====================================================================
5 * Copyright (c) 2000-2006 CollabNet. All rights reserved.
7 * This software is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at http://subversion.tigris.org/license-1.html.
10 * If newer versions of this license are posted there, you may use a
11 * newer version instead, at your option.
13 * This software consists of voluntary contributions made by many
14 * individuals. For exact contribution history, see the revision
15 * history and logs, available at http://subversion.tigris.org/.
16 * ====================================================================
19 #include <apr_xml.h>
20 #include <apr_errno.h>
21 #include <apr_uri.h>
23 #include <mod_dav.h>
25 #include "svn_error.h"
26 #include "svn_fs.h"
27 #include "svn_dav.h"
28 #include "svn_base64.h"
30 #include "dav_svn.h"
33 dav_error *
34 dav_svn__new_error_tag(apr_pool_t *pool,
35 int status,
36 int error_id,
37 const char *desc,
38 const char *namespace,
39 const char *tagname)
41 /* dav_new_error_tag will record errno but Subversion makes no attempt
42 to ensure that it is valid. We reset it to avoid putting incorrect
43 information into the error log, at the expense of possibly removing
44 valid information. */
45 errno = 0;
47 return dav_new_error_tag(pool, status, error_id, desc, namespace, tagname);
51 /* Build up a chain of DAV errors that correspond to the underlying SVN
52 errors that caused this problem. */
53 static dav_error *
54 build_error_chain(apr_pool_t *pool, svn_error_t *err, int status)
56 char *msg = err->message ? apr_pstrdup(pool, err->message) : NULL;
58 dav_error *derr = dav_svn__new_error_tag(pool, status, err->apr_err, msg,
59 SVN_DAV_ERROR_NAMESPACE,
60 SVN_DAV_ERROR_TAG);
62 if (err->child)
63 derr->prev = build_error_chain(pool, err->child, status);
65 return derr;
69 dav_error *
70 dav_svn__convert_err(svn_error_t *serr,
71 int status,
72 const char *message,
73 apr_pool_t *pool)
75 dav_error *derr;
77 /* ### someday mod_dav_svn will send back 'rich' error tags, much
78 finer grained than plain old svn_error_t's. But for now, all
79 svn_error_t's are marshalled to the client via the single
80 generic <svn:error/> tag nestled within a <D:error> block. */
82 /* Examine the Subverion error code, and select the most
83 appropriate HTTP status code. If no more appropriate HTTP
84 status code maps to the Subversion error code, use the one
85 suggested status provided by the caller. */
86 switch (serr->apr_err)
88 case SVN_ERR_FS_NOT_FOUND:
89 status = HTTP_NOT_FOUND;
90 break;
91 case SVN_ERR_UNSUPPORTED_FEATURE:
92 status = HTTP_NOT_IMPLEMENTED;
93 break;
94 case SVN_ERR_FS_PATH_ALREADY_LOCKED:
95 status = HTTP_LOCKED;
96 break;
97 /* add other mappings here */
100 derr = build_error_chain(pool, serr, status);
101 if (message != NULL
102 && serr->apr_err != SVN_ERR_REPOS_HOOK_FAILURE)
103 /* Don't hide hook failures; we might hide the error text */
104 derr = dav_push_error(pool, status, serr->apr_err, message, derr);
106 /* Now, destroy the Subversion error. */
107 svn_error_clear(serr);
109 return derr;
113 /* Set *REVISION to the youngest revision in which an interesting
114 history item (a modification, or a copy) occurred for PATH under
115 ROOT. Use POOL for scratchwork. */
116 static svn_error_t *
117 get_last_history_rev(svn_revnum_t *revision,
118 svn_fs_root_t *root,
119 const char *path,
120 apr_pool_t *pool)
122 svn_fs_history_t *history;
123 const char *ignored;
125 /* Get an initial HISTORY baton. */
126 SVN_ERR(svn_fs_node_history(&history, root, path, pool));
128 /* Now get the first *real* point of interesting history. */
129 SVN_ERR(svn_fs_history_prev(&history, history, FALSE, pool));
131 /* Fetch the location information for this history step. */
132 return svn_fs_history_location(&ignored, revision, history, pool);
136 svn_revnum_t
137 dav_svn__get_safe_cr(svn_fs_root_t *root, const char *path, apr_pool_t *pool)
139 svn_revnum_t revision = svn_fs_revision_root_revision(root);
140 svn_revnum_t history_rev;
141 svn_fs_root_t *other_root;
142 svn_fs_t *fs = svn_fs_root_fs(root);
143 const svn_fs_id_t *id, *other_id;
144 svn_error_t *err;
146 if ((err = svn_fs_node_id(&id, root, path, pool)))
148 svn_error_clear(err);
149 return revision; /* couldn't get id of root/path */
152 if ((err = get_last_history_rev(&history_rev, root, path, pool)))
154 svn_error_clear(err);
155 return revision; /* couldn't find last history rev */
158 if ((err = svn_fs_revision_root(&other_root, fs, history_rev, pool)))
160 svn_error_clear(err);
161 return revision; /* couldn't open the history rev */
164 if ((err = svn_fs_node_id(&other_id, other_root, path, pool)))
166 svn_error_clear(err);
167 return revision; /* couldn't get id of other_root/path */
170 if (svn_fs_compare_ids(id, other_id) == 0)
171 return history_rev; /* the history rev is safe! the same node
172 exists at the same path in both revisions. */
174 /* default */
175 return revision;
179 const char *
180 dav_svn__build_uri(const dav_svn_repos *repos,
181 enum dav_svn__build_what what,
182 svn_revnum_t revision,
183 const char *path,
184 int add_href,
185 apr_pool_t *pool)
187 const char *root_path = repos->root_path;
188 const char *special_uri = repos->special_uri;
189 const char *path_uri = path ? svn_path_uri_encode(path, pool) : NULL;
190 const char *href1 = add_href ? "<D:href>" : "";
191 const char *href2 = add_href ? "</D:href>" : "";
193 /* The first character of root_path is guaranteed to be "/". If
194 there's no component beyond that, then just use "", so that
195 appending another "/" later does not result in "//". */
196 if (root_path[1] == '\0')
197 root_path = "";
199 switch (what)
201 case DAV_SVN__BUILD_URI_ACT_COLLECTION:
202 return apr_psprintf(pool, "%s%s/%s/act/%s",
203 href1, root_path, special_uri, href2);
205 case DAV_SVN__BUILD_URI_BASELINE:
206 return apr_psprintf(pool, "%s%s/%s/bln/%ld%s",
207 href1, root_path, special_uri, revision, href2);
209 case DAV_SVN__BUILD_URI_BC:
210 return apr_psprintf(pool, "%s%s/%s/bc/%ld/%s",
211 href1, root_path, special_uri, revision, href2);
213 case DAV_SVN__BUILD_URI_PUBLIC:
214 return apr_psprintf(pool, "%s%s%s%s",
215 href1, root_path, path_uri, href2);
217 case DAV_SVN__BUILD_URI_VERSION:
218 return apr_psprintf(pool, "%s%s/%s/ver/%ld%s%s",
219 href1, root_path, special_uri,
220 revision, path_uri, href2);
222 case DAV_SVN__BUILD_URI_VCC:
223 return apr_psprintf(pool, "%s%s/%s/vcc/" DAV_SVN__DEFAULT_VCC_NAME "%s",
224 href1, root_path, special_uri, href2);
226 default:
227 /* programmer error somewhere */
228 abort();
229 return NULL;
232 /* NOTREACHED */
236 svn_error_t *
237 dav_svn__simple_parse_uri(dav_svn__uri_info *info,
238 const dav_resource *relative,
239 const char *uri,
240 apr_pool_t *pool)
242 apr_uri_t comp;
243 const char *path;
244 apr_size_t len1;
245 apr_size_t len2;
246 const char *slash;
247 const char *created_rev_str;
249 /* parse the input URI, in case it is more than just a path */
250 if (apr_uri_parse(pool, uri, &comp) != APR_SUCCESS)
251 goto malformed_uri;
253 /* ### ignore all URI parts but the path (for now) */
255 /* clean up the URI */
256 if (comp.path == NULL)
257 path = "/";
258 else
260 ap_getparents(comp.path);
261 ap_no2slash(comp.path);
262 path = comp.path;
266 * Does the URI path specify the same repository? It does not if one of:
268 * 1) input is shorter than the path to our repository
269 * 2) input is longer, but there is no separator
270 * [ http://host/repos vs http://host/repository ]
271 * 3) the two paths do not match
273 len1 = strlen(path);
274 len2 = strlen(relative->info->repos->root_path);
275 if (len2 == 1 && relative->info->repos->root_path[0] == '/')
276 len2 = 0;
278 if (len1 < len2
279 || (len1 > len2 && path[len2] != '/')
280 || memcmp(path, relative->info->repos->root_path, len2) != 0)
282 return svn_error_create(SVN_ERR_APMOD_MALFORMED_URI, NULL,
283 "Unusable URI: it does not refer to this "
284 "repository");
287 /* prep the return value */
288 memset(info, 0, sizeof(*info));
289 info->rev = SVN_INVALID_REVNUM;
291 path += len2; /* now points to "/" or "\0" */
292 len1 -= len2;
294 if (len1 <= 1)
296 info->repos_path = "/";
297 return NULL;
300 /* skip over the leading "/" */
301 ++path;
302 --len1;
304 /* is this a special URI? */
305 len2 = strlen(relative->info->repos->special_uri);
306 if (len1 < len2
307 || (len1 > len2 && path[len2] != '/')
308 || memcmp(path, relative->info->repos->special_uri, len2) != 0)
310 /* this is an ordinary "public" URI, so back up to include the
311 leading '/' and just return... no need to parse further. */
312 info->repos_path = svn_path_uri_decode(path - 1, pool);
313 return NULL;
316 path += len2; /* now points to "/" or "\0" just past the special URI */
317 len1 -= len2;
319 /* ### we don't handle the root of the special area yet */
320 if (len1 <= 1)
321 goto unhandled_form;
323 /* Find the next component, and ensure something is there. */
324 slash = ap_strchr_c(path + 1, '/');
325 if (slash == NULL || slash[1] == '\0')
326 goto unhandled_form;
327 len2 = slash - path;
329 /* Figure out what we have here */
330 if (len2 == 4 && memcmp(path, "/act/", 5) == 0)
332 /* an activity */
333 info->activity_id = path + 5;
335 else if (len2 == 4 && memcmp(path, "/ver/", 5) == 0)
337 /* a version resource */
338 path += 5;
339 len1 -= 5;
340 slash = ap_strchr_c(path, '/');
341 if (slash == NULL)
343 created_rev_str = apr_pstrndup(pool, path, len1);
344 info->rev = SVN_STR_TO_REV(created_rev_str);
345 info->repos_path = "/";
347 else
349 created_rev_str = apr_pstrndup(pool, path, slash - path);
350 info->rev = SVN_STR_TO_REV(created_rev_str);
351 info->repos_path = svn_path_uri_decode(slash, pool);
353 if (info->rev == SVN_INVALID_REVNUM)
354 goto malformed_uri;
356 else
357 goto unhandled_form;
359 return NULL;
361 malformed_uri:
362 return svn_error_create(SVN_ERR_APMOD_MALFORMED_URI, NULL,
363 "The specified URI could not be parsed");
365 unhandled_form:
366 return svn_error_create(SVN_ERR_UNSUPPORTED_FEATURE, NULL,
367 "Unsupported URI form");
371 /* ### move this into apr_xml */
373 dav_svn__find_ns(apr_array_header_t *namespaces, const char *uri)
375 int i;
377 for (i = 0; i < namespaces->nelts; ++i)
378 if (strcmp(APR_XML_GET_URI_ITEM(namespaces, i), uri) == 0)
379 return i;
380 return -1;
384 svn_error_t *
385 dav_svn__send_xml(apr_bucket_brigade *bb,
386 ap_filter_t *output,
387 const char *fmt,
388 ...)
390 apr_status_t apr_err;
391 va_list ap;
393 va_start(ap, fmt);
394 apr_err = apr_brigade_vprintf(bb, ap_filter_flush, output, fmt, ap);
395 va_end(ap);
396 if (apr_err)
397 return svn_error_create(apr_err, 0, NULL);
398 /* ### check for an aborted connection, since the brigade functions
399 don't appear to be return useful errors when the connection is
400 dropped. */
401 if (output->c->aborted)
402 return svn_error_create(SVN_ERR_APMOD_CONNECTION_ABORTED, 0, NULL);
403 return SVN_NO_ERROR;
407 dav_error *
408 dav_svn__test_canonical(const char *path, apr_pool_t *pool)
410 if (svn_path_is_canonical(path, pool))
411 return NULL;
413 /* Otherwise, generate a generic HTTP_BAD_REQUEST error. */
414 return dav_svn__new_error_tag
415 (pool, HTTP_BAD_REQUEST, 0,
416 apr_psprintf(pool,
417 "Path '%s' is not canonicalized; "
418 "there is a problem with the client.", path),
419 SVN_DAV_ERROR_NAMESPACE, SVN_DAV_ERROR_TAG);
423 dav_error *
424 dav_svn__sanitize_error(svn_error_t *serr,
425 const char *new_msg,
426 int http_status,
427 request_rec *r)
429 svn_error_t *safe_err = serr;
430 if (new_msg != NULL)
432 /* Sanitization is necessary. Create a new, safe error and
433 log the original error. */
434 safe_err = svn_error_create(serr->apr_err, NULL, new_msg);
435 ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_EGENERAL, r,
436 "%s", serr->message);
437 svn_error_clear(serr);
439 return dav_svn__convert_err(safe_err, http_status,
440 apr_psprintf(r->pool, safe_err->message),
441 r->pool);
445 struct brigade_write_baton
447 apr_bucket_brigade *bb;
448 ap_filter_t *output;
452 /* This implements 'svn_write_fn_t'. */
453 static svn_error_t *
454 brigade_write_fn(void *baton, const char *data, apr_size_t *len)
456 struct brigade_write_baton *wb = baton;
457 apr_status_t apr_err;
459 apr_err = apr_brigade_write(wb->bb, ap_filter_flush, wb->output, data, *len);
461 if (apr_err != APR_SUCCESS)
462 return svn_error_wrap_apr(apr_err, "Error writing base64 data");
464 return SVN_NO_ERROR;
468 svn_stream_t *
469 dav_svn__make_base64_output_stream(apr_bucket_brigade *bb,
470 ap_filter_t *output,
471 apr_pool_t *pool)
473 struct brigade_write_baton *wb = apr_palloc(pool, sizeof(*wb));
474 svn_stream_t *stream = svn_stream_create(wb, pool);
476 wb->bb = bb;
477 wb->output = output;
478 svn_stream_set_write(stream, brigade_write_fn);
480 return svn_base64_encode(stream, pool);
483 void
484 dav_svn__operational_log(struct dav_resource_private *info, const char *line)
486 apr_table_set(info->r->subprocess_env, "SVN-ACTION", line);
487 apr_table_set(info->r->subprocess_env, "SVN-REPOS",
488 svn_path_uri_encode(info->repos->fs_path, info->r->pool));
489 apr_table_set(info->r->subprocess_env, "SVN-REPOS-NAME",
490 svn_path_uri_encode(info->repos->repo_basename, info->r->pool));