2 * get_lock.c : obtain single lock information functions for ra_serf
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
21 * ====================================================================
30 #include "svn_pools.h"
33 #include "../libsvn_ra/ra_loader.h"
34 #include "svn_config.h"
37 #include "svn_private_config.h"
43 * This enum represents the current state of our XML parsing for a REPORT.
62 typedef struct lock_info_t {
69 svn_boolean_t read_headers;
71 svn_ra_serf__handler_t *handler;
73 /* The expat handler. We wrap this to do a bit more work. */
74 svn_ra_serf__response_handler_t inner_handler;
80 #define S_ SVN_XML_NAMESPACE
81 static const svn_ra_serf__xml_transition_t locks_ttable[] = {
82 /* The INITIAL state can transition into D:prop (LOCK) or
83 to D:multistatus (PROPFIND) */
84 { INITIAL, D_, "multistatus", MULTISTATUS,
85 FALSE, { NULL }, FALSE },
87 { MULTISTATUS, D_, "response", RESPONSE,
88 FALSE, { NULL }, FALSE },
90 { RESPONSE, D_, "propstat", PROPSTAT,
91 FALSE, { NULL }, FALSE },
93 { PROPSTAT, D_, "prop", PROP,
94 FALSE, { NULL }, FALSE },
96 { PROP, D_, "lockdiscovery", LOCK_DISCOVERY,
97 FALSE, { NULL }, FALSE },
99 { LOCK_DISCOVERY, D_, "activelock", ACTIVE_LOCK,
100 FALSE, { NULL }, FALSE },
103 /* ### we don't really need to parse locktype/lockscope. we know what
104 ### the values are going to be. we *could* validate that the only
105 ### possible children are D:write and D:exclusive. we'd need to
106 ### modify the state transition to tell us about all children
107 ### (ie. maybe support "*" for the name) and then validate. but it
108 ### just isn't important to validate, so disable this for now... */
110 { ACTIVE_LOCK, D_, "locktype", LOCK_TYPE,
111 FALSE, { NULL }, FALSE },
113 { LOCK_TYPE, D_, "write", WRITE,
114 FALSE, { NULL }, TRUE },
116 { ACTIVE_LOCK, D_, "lockscope", LOCK_SCOPE,
117 FALSE, { NULL }, FALSE },
119 { LOCK_SCOPE, D_, "exclusive", EXCLUSIVE,
120 FALSE, { NULL }, TRUE },
123 { ACTIVE_LOCK, D_, "timeout", TIMEOUT,
124 TRUE, { NULL }, TRUE },
126 { ACTIVE_LOCK, D_, "locktoken", LOCK_TOKEN,
127 FALSE, { NULL }, FALSE },
129 { LOCK_TOKEN, D_, "href", HREF,
130 TRUE, { NULL }, TRUE },
132 { ACTIVE_LOCK, D_, "owner", OWNER,
133 TRUE, { NULL }, TRUE },
135 /* ACTIVE_LOCK has a D:depth child, but we can ignore that. */
140 static const int locks_expected_status[] = {
145 /* Conforms to svn_ra_serf__xml_closed_t */
147 locks_closed(svn_ra_serf__xml_estate_t *xes,
150 const svn_string_t *cdata,
152 apr_pool_t *scratch_pool)
154 lock_info_t *lock_ctx = baton;
156 if (leaving_state == TIMEOUT)
158 if (strcasecmp(cdata->data, "Infinite") == 0)
159 lock_ctx->lock->expiration_date = 0;
160 else if (strncasecmp(cdata->data, "Second-", 7) == 0)
163 SVN_ERR(svn_cstring_atoui(&n, cdata->data+7));
165 lock_ctx->lock->expiration_date = apr_time_now() +
166 apr_time_from_sec(n);
169 return svn_error_createf(SVN_ERR_RA_DAV_MALFORMED_DATA, NULL,
170 _("Invalid LOCK timeout value '%s'"),
173 else if (leaving_state == HREF)
177 char *buf = apr_pstrmemdup(lock_ctx->pool, cdata->data, cdata->len);
179 apr_collapse_spaces(buf, buf);
180 lock_ctx->lock->token = buf;
183 else if (leaving_state == OWNER)
187 lock_ctx->lock->comment = apr_pstrmemdup(lock_ctx->pool,
188 cdata->data, cdata->len);
195 /* Implements svn_ra_serf__response_handler_t */
197 handle_lock(serf_request_t *request,
198 serf_bucket_t *response,
202 lock_info_t *ctx = handler_baton;
204 if (!ctx->read_headers)
206 serf_bucket_t *headers;
209 headers = serf_bucket_response_get_headers(response);
211 val = serf_bucket_headers_get(headers, SVN_DAV_LOCK_OWNER_HEADER);
214 ctx->lock->owner = apr_pstrdup(ctx->pool, val);
217 val = serf_bucket_headers_get(headers, SVN_DAV_CREATIONDATE_HEADER);
220 SVN_ERR(svn_time_from_cstring(&ctx->lock->creation_date, val,
224 ctx->read_headers = TRUE;
227 return ctx->inner_handler(request, response, ctx->inner_baton, pool);
230 /* Implements svn_ra_serf__request_body_delegate_t */
232 create_getlock_body(serf_bucket_t **body_bkt,
234 serf_bucket_alloc_t *alloc,
235 apr_pool_t *pool /* request pool */,
236 apr_pool_t *scratch_pool)
238 serf_bucket_t *buckets;
240 buckets = serf_bucket_aggregate_create(alloc);
242 svn_ra_serf__add_xml_header_buckets(buckets, alloc);
243 svn_ra_serf__add_open_tag_buckets(buckets, alloc, "propfind",
246 svn_ra_serf__add_open_tag_buckets(buckets, alloc, "prop", SVN_VA_NULL);
247 svn_ra_serf__add_empty_tag_buckets(buckets, alloc,
248 "lockdiscovery", SVN_VA_NULL);
249 svn_ra_serf__add_close_tag_buckets(buckets, alloc, "prop");
250 svn_ra_serf__add_close_tag_buckets(buckets, alloc, "propfind");
257 setup_getlock_headers(serf_bucket_t *headers,
259 apr_pool_t *pool /* request pool */,
260 apr_pool_t *scratch_pool)
262 serf_bucket_headers_setn(headers, "Depth", "0");
268 svn_ra_serf__get_lock(svn_ra_session_t *ra_session,
271 apr_pool_t *result_pool)
273 svn_ra_serf__session_t *session = ra_session->priv;
274 svn_ra_serf__handler_t *handler;
275 svn_ra_serf__xml_context_t *xmlctx;
276 apr_pool_t *scratch_pool = svn_pool_create(result_pool);
277 lock_info_t *lock_ctx;
281 req_url = svn_path_url_add_component2(session->session_url.path, path,
284 lock_ctx = apr_pcalloc(scratch_pool, sizeof(*lock_ctx));
285 lock_ctx->pool = result_pool;
286 lock_ctx->path = req_url;
287 lock_ctx->lock = svn_lock_create(result_pool);
288 lock_ctx->lock->path = apr_pstrdup(result_pool, path);
290 xmlctx = svn_ra_serf__xml_context_create(locks_ttable,
291 NULL, locks_closed, NULL,
294 handler = svn_ra_serf__create_expat_handler(session, xmlctx,
295 locks_expected_status,
298 handler->method = "PROPFIND";
299 handler->path = req_url;
300 handler->body_type = "text/xml";
302 handler->body_delegate = create_getlock_body;
303 handler->body_delegate_baton = lock_ctx;
305 handler->header_delegate = setup_getlock_headers;
306 handler->header_delegate_baton = lock_ctx;
308 handler->no_dav_headers = TRUE;
310 lock_ctx->inner_handler = handler->response_handler;
311 lock_ctx->inner_baton = handler->response_baton;
312 handler->response_handler = handle_lock;
313 handler->response_baton = lock_ctx;
315 lock_ctx->handler = handler;
317 err = svn_ra_serf__context_run_one(handler, scratch_pool);
319 if ((err && (handler->sline.code == 500 || handler->sline.code == 501))
320 || svn_error_find_cause(err, SVN_ERR_UNSUPPORTED_FEATURE))
321 return svn_error_trace(
322 svn_error_create(SVN_ERR_RA_NOT_IMPLEMENTED, err,
323 _("Server does not support locking features")));
324 else if (svn_error_find_cause(err, SVN_ERR_FS_NOT_FOUND))
325 svn_error_clear(err); /* Behave like the other RA layers */
326 else if (handler->sline.code != 207)
327 return svn_error_trace(svn_ra_serf__unexpected_status(handler));
329 if (lock_ctx->lock && lock_ctx->lock->token)
330 *lock = lock_ctx->lock;
334 svn_pool_destroy(scratch_pool);