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 * ====================================================================
20 #include <apr_errno.h>
25 #include "svn_error.h"
28 #include "svn_base64.h"
34 dav_svn__new_error_tag(apr_pool_t
*pool
,
38 const char *namespace,
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
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. */
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
,
63 derr
->prev
= build_error_chain(pool
, err
->child
, status
);
70 dav_svn__convert_err(svn_error_t
*serr
,
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
;
91 case SVN_ERR_UNSUPPORTED_FEATURE
:
92 status
= HTTP_NOT_IMPLEMENTED
;
94 case SVN_ERR_FS_PATH_ALREADY_LOCKED
:
97 /* add other mappings here */
100 derr
= build_error_chain(pool
, serr
, status
);
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
);
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. */
117 get_last_history_rev(svn_revnum_t
*revision
,
122 svn_fs_history_t
*history
;
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
);
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
;
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. */
180 dav_svn__build_uri(const dav_svn_repos
*repos
,
181 enum dav_svn__build_what what
,
182 svn_revnum_t revision
,
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')
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
);
227 /* programmer error somewhere */
237 dav_svn__simple_parse_uri(dav_svn__uri_info
*info
,
238 const dav_resource
*relative
,
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
)
253 /* ### ignore all URI parts but the path (for now) */
255 /* clean up the URI */
256 if (comp
.path
== NULL
)
260 ap_getparents(comp
.path
);
261 ap_no2slash(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
274 len2
= strlen(relative
->info
->repos
->root_path
);
275 if (len2
== 1 && relative
->info
->repos
->root_path
[0] == '/')
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 "
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" */
296 info
->repos_path
= "/";
300 /* skip over the leading "/" */
304 /* is this a special URI? */
305 len2
= strlen(relative
->info
->repos
->special_uri
);
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
);
316 path
+= len2
; /* now points to "/" or "\0" just past the special URI */
319 /* ### we don't handle the root of the special area yet */
323 /* Find the next component, and ensure something is there. */
324 slash
= ap_strchr_c(path
+ 1, '/');
325 if (slash
== NULL
|| slash
[1] == '\0')
329 /* Figure out what we have here */
330 if (len2
== 4 && memcmp(path
, "/act/", 5) == 0)
333 info
->activity_id
= path
+ 5;
335 else if (len2
== 4 && memcmp(path
, "/ver/", 5) == 0)
337 /* a version resource */
340 slash
= ap_strchr_c(path
, '/');
343 created_rev_str
= apr_pstrndup(pool
, path
, len1
);
344 info
->rev
= SVN_STR_TO_REV(created_rev_str
);
345 info
->repos_path
= "/";
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
)
362 return svn_error_create(SVN_ERR_APMOD_MALFORMED_URI
, NULL
,
363 "The specified URI could not be parsed");
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
)
377 for (i
= 0; i
< namespaces
->nelts
; ++i
)
378 if (strcmp(APR_XML_GET_URI_ITEM(namespaces
, i
), uri
) == 0)
385 dav_svn__send_xml(apr_bucket_brigade
*bb
,
390 apr_status_t apr_err
;
394 apr_err
= apr_brigade_vprintf(bb
, ap_filter_flush
, output
, fmt
, ap
);
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
401 if (output
->c
->aborted
)
402 return svn_error_create(SVN_ERR_APMOD_CONNECTION_ABORTED
, 0, NULL
);
408 dav_svn__test_canonical(const char *path
, apr_pool_t
*pool
)
410 if (svn_path_is_canonical(path
, pool
))
413 /* Otherwise, generate a generic HTTP_BAD_REQUEST error. */
414 return dav_svn__new_error_tag
415 (pool
, HTTP_BAD_REQUEST
, 0,
417 "Path '%s' is not canonicalized; "
418 "there is a problem with the client.", path
),
419 SVN_DAV_ERROR_NAMESPACE
, SVN_DAV_ERROR_TAG
);
424 dav_svn__sanitize_error(svn_error_t
*serr
,
429 svn_error_t
*safe_err
= serr
;
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
),
445 struct brigade_write_baton
447 apr_bucket_brigade
*bb
;
452 /* This implements 'svn_write_fn_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");
469 dav_svn__make_base64_output_stream(apr_bucket_brigade
*bb
,
473 struct brigade_write_baton
*wb
= apr_palloc(pool
, sizeof(*wb
));
474 svn_stream_t
*stream
= svn_stream_create(wb
, pool
);
478 svn_stream_set_write(stream
, brigade_write_fn
);
480 return svn_base64_encode(stream
, pool
);
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
));