1/*
2 * getlocks.c :  entry point for get_locks RA functions for ra_serf
3 *
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
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
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
20 *    under the License.
21 * ====================================================================
22 */
23
24
25
26#include <apr_uri.h>
27
28#include <serf.h>
29
30#include "svn_hash.h"
31#include "svn_path.h"
32#include "svn_pools.h"
33#include "svn_ra.h"
34#include "svn_dav.h"
35#include "svn_time.h"
36#include "svn_xml.h"
37
38#include "private/svn_dav_protocol.h"
39#include "private/svn_fspath.h"
40#include "svn_private_config.h"
41
42#include "../libsvn_ra/ra_loader.h"
43
44#include "ra_serf.h"
45
46
47/*
48 * This enum represents the current state of our XML parsing for a REPORT.
49 */
50enum {
51  INITIAL = 0,
52  REPORT,
53  LOCK,
54  PATH,
55  TOKEN,
56  OWNER,
57  COMMENT,
58  CREATION_DATE,
59  EXPIRATION_DATE
60};
61
62typedef struct lock_context_t {
63  apr_pool_t *pool;
64
65  /* target and requested depth of the operation. */
66  const char *path;
67  svn_depth_t requested_depth;
68
69  /* return hash */
70  apr_hash_t *hash;
71
72} lock_context_t;
73
74#define D_ "DAV:"
75#define S_ SVN_XML_NAMESPACE
76static const svn_ra_serf__xml_transition_t getlocks_ttable[] = {
77  { INITIAL, S_, "get-locks-report", REPORT,
78    FALSE, { NULL }, FALSE },
79
80  { REPORT, S_, "lock", LOCK,
81    FALSE, { NULL }, TRUE },
82
83  { LOCK, S_, "path", PATH,
84    TRUE, { NULL }, TRUE },
85
86  { LOCK, S_, "token", TOKEN,
87    TRUE, { NULL }, TRUE },
88
89  { LOCK, S_, "owner", OWNER,
90    TRUE, { NULL }, TRUE },
91
92  { LOCK, S_, "comment", COMMENT,
93    TRUE, { NULL }, TRUE },
94
95  { LOCK, S_, SVN_DAV__CREATIONDATE, CREATION_DATE,
96    TRUE, { NULL }, TRUE },
97
98  { LOCK, S_, "expirationdate", EXPIRATION_DATE,
99    TRUE, { NULL }, TRUE },
100
101  { 0 }
102};
103
104
105/* Conforms to svn_ra_serf__xml_closed_t  */
106static svn_error_t *
107getlocks_closed(svn_ra_serf__xml_estate_t *xes,
108                void *baton,
109                int leaving_state,
110                const svn_string_t *cdata,
111                apr_hash_t *attrs,
112                apr_pool_t *scratch_pool)
113{
114  lock_context_t *lock_ctx = baton;
115
116  if (leaving_state == LOCK)
117    {
118      const char *path = svn_hash_gets(attrs, "path");
119      const char *token = svn_hash_gets(attrs, "token");
120      svn_boolean_t save_lock = FALSE;
121
122      /* Filter out unwanted paths.  Since Subversion only allows
123         locks on files, we can treat depth=immediates the same as
124         depth=files for filtering purposes.  Meaning, we'll keep
125         this lock if:
126
127         a) its path is the very path we queried, or
128         b) we've asked for a fully recursive answer, or
129         c) we've asked for depth=files or depth=immediates, and this
130            lock is on an immediate child of our query path.
131      */
132      if (! token)
133        {
134          /* A lock without a token is not a lock; just an answer that there
135             is no lock on the node. */
136          save_lock = FALSE;
137        }
138      if (strcmp(lock_ctx->path, path) == 0
139          || lock_ctx->requested_depth == svn_depth_infinity)
140        {
141          save_lock = TRUE;
142        }
143      else if (lock_ctx->requested_depth == svn_depth_files
144               || lock_ctx->requested_depth == svn_depth_immediates)
145        {
146          const char *relpath = svn_fspath__skip_ancestor(lock_ctx->path,
147                                                          path);
148          if (relpath && (svn_path_component_count(relpath) == 1))
149            save_lock = TRUE;
150        }
151
152      if (save_lock)
153        {
154          /* We get to put the structure on the stack rather than using
155             svn_lock_create(). Bwahahaha....   */
156          svn_lock_t lock = { 0 };
157          const char *date;
158          svn_lock_t *result_lock;
159
160          /* Note: these "attributes" came from child elements. Some of
161             them may have not been sent, so the value will be NULL.  */
162
163          lock.path = path;
164          lock.token = token;
165          lock.owner = svn_hash_gets(attrs, "owner");
166          lock.comment = svn_hash_gets(attrs, "comment");
167
168          date = svn_hash_gets(attrs, SVN_DAV__CREATIONDATE);
169          if (date)
170            SVN_ERR(svn_time_from_cstring(&lock.creation_date, date,
171                                          scratch_pool));
172
173          date = svn_hash_gets(attrs, "expirationdate");
174          if (date)
175            SVN_ERR(svn_time_from_cstring(&lock.expiration_date, date,
176                                          scratch_pool));
177
178          result_lock = svn_lock_dup(&lock, lock_ctx->pool);
179          svn_hash_sets(lock_ctx->hash, result_lock->path, result_lock);
180        }
181    }
182  else
183    {
184      const char *name;
185
186      SVN_ERR_ASSERT(cdata != NULL);
187
188      if (leaving_state == PATH)
189        name = "path";
190      else if (leaving_state == TOKEN)
191        name = "token";
192      else if (leaving_state == OWNER)
193        name = "owner";
194      else if (leaving_state == COMMENT)
195        name = "comment";
196      else if (leaving_state == CREATION_DATE)
197        name = SVN_DAV__CREATIONDATE;
198      else if (leaving_state == EXPIRATION_DATE)
199        name = "expirationdate";
200      else
201        SVN_ERR_MALFUNCTION();
202
203      /* Store the lock information onto the LOCK elemstate.  */
204      svn_ra_serf__xml_note(xes, LOCK, name, cdata->data);
205    }
206
207  return SVN_NO_ERROR;
208}
209
210
211/* Implements svn_ra_serf__request_body_delegate_t */
212static svn_error_t *
213create_getlocks_body(serf_bucket_t **body_bkt,
214                     void *baton,
215                     serf_bucket_alloc_t *alloc,
216                     apr_pool_t *pool)
217{
218  lock_context_t *lock_ctx = baton;
219  serf_bucket_t *buckets;
220
221  buckets = serf_bucket_aggregate_create(alloc);
222
223  svn_ra_serf__add_open_tag_buckets(
224    buckets, alloc, "S:get-locks-report", "xmlns:S", SVN_XML_NAMESPACE,
225    "depth", svn_depth_to_word(lock_ctx->requested_depth), NULL);
226  svn_ra_serf__add_close_tag_buckets(buckets, alloc, "S:get-locks-report");
227
228  *body_bkt = buckets;
229  return SVN_NO_ERROR;
230}
231
232svn_error_t *
233svn_ra_serf__get_locks(svn_ra_session_t *ra_session,
234                       apr_hash_t **locks,
235                       const char *path,
236                       svn_depth_t depth,
237                       apr_pool_t *pool)
238{
239  lock_context_t *lock_ctx;
240  svn_ra_serf__session_t *session = ra_session->priv;
241  svn_ra_serf__handler_t *handler;
242  svn_ra_serf__xml_context_t *xmlctx;
243  const char *req_url, *rel_path;
244  svn_error_t *err;
245
246  req_url = svn_path_url_add_component2(session->session_url.path, path, pool);
247  SVN_ERR(svn_ra_serf__get_relative_path(&rel_path, req_url, session,
248                                         NULL, pool));
249
250  lock_ctx = apr_pcalloc(pool, sizeof(*lock_ctx));
251  lock_ctx->pool = pool;
252  lock_ctx->path = apr_pstrcat(pool, "/", rel_path, (char *)NULL);
253  lock_ctx->requested_depth = depth;
254  lock_ctx->hash = apr_hash_make(pool);
255
256  xmlctx = svn_ra_serf__xml_context_create(getlocks_ttable,
257                                           NULL, getlocks_closed, NULL,
258                                           lock_ctx,
259                                           pool);
260  handler = svn_ra_serf__create_expat_handler(xmlctx, pool);
261
262  handler->method = "REPORT";
263  handler->path = req_url;
264  handler->body_type = "text/xml";
265  handler->conn = session->conns[0];
266  handler->session = session;
267
268  handler->body_delegate = create_getlocks_body;
269  handler->body_delegate_baton = lock_ctx;
270
271  err = svn_ra_serf__context_run_one(handler, pool);
272
273  /* Wrap the server generated error for an unsupported report with the
274     documented error for this ra function. */
275  if (svn_error_find_cause(err, SVN_ERR_UNSUPPORTED_FEATURE))
276    err = svn_error_create(SVN_ERR_RA_NOT_IMPLEMENTED, err, NULL);
277
278  SVN_ERR(err);
279
280  /* We get a 404 when a path doesn't exist in HEAD, but it might
281     have existed earlier (E.g. 'svn ls http://s/svn/trunk/file@1' */
282  if (handler->sline.code != 404)
283    {
284      SVN_ERR(svn_ra_serf__error_on_status(handler->sline,
285                                           handler->path,
286                                           handler->location));
287    }
288
289  *locks = lock_ctx->hash;
290
291  return SVN_NO_ERROR;
292}
293