inherited_props.c revision 269847
1/*
2 * inherited_props.c : ra_serf implementation of svn_ra_get_inherited_props
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#include <apr_tables.h>
26#include <apr_xml.h>
27
28#include "svn_hash.h"
29#include "svn_path.h"
30#include "svn_ra.h"
31#include "svn_string.h"
32#include "svn_xml.h"
33#include "svn_props.h"
34#include "svn_base64.h"
35
36#include "private/svn_dav_protocol.h"
37#include "../libsvn_ra/ra_loader.h"
38#include "svn_private_config.h"
39#include "ra_serf.h"
40
41
42/* The current state of our XML parsing. */
43typedef enum iprops_state_e {
44  INITIAL = 0,
45  IPROPS_REPORT,
46  IPROPS_ITEM,
47  IPROPS_PATH,
48  IPROPS_PROPNAME,
49  IPROPS_PROPVAL
50} iprops_state_e;
51
52/* Struct for accumulating inherited props. */
53typedef struct iprops_context_t {
54  /* The depth-first ordered array of svn_prop_inherited_item_t *
55     structures we are building. */
56  apr_array_header_t *iprops;
57
58  /* Pool in which to allocate elements of IPROPS. */
59  apr_pool_t *pool;
60
61  /* The repository's root URL. */
62  const char *repos_root_url;
63
64  /* Current property name */
65  svn_stringbuf_t *curr_propname;
66
67  /* Current element in IPROPS. */
68  svn_prop_inherited_item_t *curr_iprop;
69
70  /* Path we are finding inherited properties for.  This is relative to
71     the RA session passed to svn_ra_serf__get_inherited_props. */
72  const char *path;
73  /* The revision of PATH*/
74  svn_revnum_t revision;
75} iprops_context_t;
76
77#define S_ SVN_XML_NAMESPACE
78static const svn_ra_serf__xml_transition_t iprops_table[] = {
79  { INITIAL, S_, SVN_DAV__INHERITED_PROPS_REPORT, IPROPS_REPORT,
80    FALSE, { NULL }, FALSE },
81
82  { IPROPS_REPORT, S_, SVN_DAV__IPROP_ITEM, IPROPS_ITEM,
83    FALSE, { NULL }, TRUE },
84
85  { IPROPS_ITEM, S_, SVN_DAV__IPROP_PATH, IPROPS_PATH,
86    TRUE, { NULL }, TRUE },
87
88  { IPROPS_ITEM, S_, SVN_DAV__IPROP_PROPNAME, IPROPS_PROPNAME,
89    TRUE, { NULL }, TRUE },
90
91  { IPROPS_ITEM, S_, SVN_DAV__IPROP_PROPVAL, IPROPS_PROPVAL,
92    TRUE, { "?V:encoding", NULL }, TRUE },
93
94  { 0 }
95};
96
97/* Conforms to svn_ra_serf__xml_opened_t */
98static svn_error_t *
99iprops_opened(svn_ra_serf__xml_estate_t *xes,
100              void *baton,
101              int entered_state,
102              const svn_ra_serf__dav_props_t *tag,
103              apr_pool_t *scratch_pool)
104{
105  iprops_context_t *iprops_ctx = baton;
106
107  if (entered_state == IPROPS_ITEM)
108    {
109      svn_stringbuf_setempty(iprops_ctx->curr_propname);
110
111      iprops_ctx->curr_iprop = apr_pcalloc(iprops_ctx->pool,
112                                           sizeof(*iprops_ctx->curr_iprop));
113
114      iprops_ctx->curr_iprop->prop_hash = apr_hash_make(iprops_ctx->pool);
115    }
116  return SVN_NO_ERROR;
117}
118
119/* Conforms to svn_ra_serf__xml_closed_t  */
120static svn_error_t *
121iprops_closed(svn_ra_serf__xml_estate_t *xes,
122              void *baton,
123              int leaving_state,
124              const svn_string_t *cdata,
125              apr_hash_t *attrs,
126              apr_pool_t *scratch_pool)
127{
128  iprops_context_t *iprops_ctx = baton;
129
130  if (leaving_state == IPROPS_ITEM)
131    {
132      APR_ARRAY_PUSH(iprops_ctx->iprops, svn_prop_inherited_item_t *) =
133        iprops_ctx->curr_iprop;
134
135      iprops_ctx->curr_iprop = NULL;
136    }
137  else if (leaving_state == IPROPS_PATH)
138    {
139      /* Every <iprop-item> has a single <iprop-path> */
140      if (iprops_ctx->curr_iprop->path_or_url)
141        return svn_error_create(SVN_ERR_XML_MALFORMED, NULL, NULL);
142
143      iprops_ctx->curr_iprop->path_or_url =
144        svn_path_url_add_component2(iprops_ctx->repos_root_url,
145                                    cdata->data,
146                                    iprops_ctx->pool);
147    }
148  else if (leaving_state == IPROPS_PROPNAME)
149    {
150      if (iprops_ctx->curr_propname->len)
151        return svn_error_create(SVN_ERR_XML_MALFORMED, NULL, NULL);
152
153      /* Store propname for value */
154      svn_stringbuf_set(iprops_ctx->curr_propname, cdata->data);
155    }
156  else if (leaving_state == IPROPS_PROPVAL)
157    {
158      const char *encoding;
159      const svn_string_t *val_str;
160
161      if (! iprops_ctx->curr_propname->len)
162        return svn_error_create(SVN_ERR_XML_MALFORMED, NULL, NULL);
163
164      encoding = svn_hash_gets(attrs, "V:encoding");
165
166      if (encoding)
167        {
168          if (strcmp(encoding, "base64") != 0)
169            return svn_error_createf(SVN_ERR_XML_MALFORMED,
170                                     NULL,
171                                     _("Got unrecognized encoding '%s'"),
172                                     encoding);
173
174          /* Decode into the right pool.  */
175          val_str = svn_base64_decode_string(cdata, iprops_ctx->pool);
176        }
177      else
178        {
179          /* Copy into the right pool.  */
180          val_str = svn_string_dup(cdata, iprops_ctx->pool);
181        }
182
183      svn_hash_sets(iprops_ctx->curr_iprop->prop_hash,
184                    apr_pstrdup(iprops_ctx->pool,
185                                iprops_ctx->curr_propname->data),
186                    val_str);
187      /* Clear current propname. */
188      svn_stringbuf_setempty(iprops_ctx->curr_propname);
189    }
190  else
191    SVN_ERR_MALFUNCTION(); /* Invalid transition table */
192
193  return SVN_NO_ERROR;
194}
195
196static svn_error_t *
197create_iprops_body(serf_bucket_t **bkt,
198                   void *baton,
199                   serf_bucket_alloc_t *alloc,
200                   apr_pool_t *pool)
201{
202  iprops_context_t *iprops_ctx = baton;
203  serf_bucket_t *body_bkt;
204
205  body_bkt = serf_bucket_aggregate_create(alloc);
206
207  svn_ra_serf__add_open_tag_buckets(body_bkt, alloc,
208                                    "S:" SVN_DAV__INHERITED_PROPS_REPORT,
209                                    "xmlns:S", SVN_XML_NAMESPACE,
210                                    NULL);
211  svn_ra_serf__add_tag_buckets(body_bkt,
212                               "S:" SVN_DAV__REVISION,
213                               apr_ltoa(pool, iprops_ctx->revision),
214                               alloc);
215  svn_ra_serf__add_tag_buckets(body_bkt, "S:" SVN_DAV__PATH,
216                               iprops_ctx->path, alloc);
217  svn_ra_serf__add_close_tag_buckets(body_bkt, alloc,
218                                     "S:" SVN_DAV__INHERITED_PROPS_REPORT);
219  *bkt = body_bkt;
220  return SVN_NO_ERROR;
221}
222
223/* Request a inherited-props-report from the URL attached to RA_SESSION,
224   and fill the IPROPS array hash with the results.  */
225svn_error_t *
226svn_ra_serf__get_inherited_props(svn_ra_session_t *ra_session,
227                                 apr_array_header_t **iprops,
228                                 const char *path,
229                                 svn_revnum_t revision,
230                                 apr_pool_t *result_pool,
231                                 apr_pool_t *scratch_pool)
232{
233  svn_error_t *err;
234  iprops_context_t *iprops_ctx;
235  svn_ra_serf__session_t *session = ra_session->priv;
236  svn_ra_serf__handler_t *handler;
237  svn_ra_serf__xml_context_t *xmlctx;
238  const char *req_url;
239
240  SVN_ERR(svn_ra_serf__get_stable_url(&req_url,
241                                      NULL /* latest_revnum */,
242                                      session,
243                                      NULL /* conn */,
244                                      NULL /* url */,
245                                      revision,
246                                      result_pool, scratch_pool));
247
248  SVN_ERR_ASSERT(session->repos_root_str);
249
250  iprops_ctx = apr_pcalloc(scratch_pool, sizeof(*iprops_ctx));
251  iprops_ctx->repos_root_url = session->repos_root_str;
252  iprops_ctx->pool = result_pool;
253  iprops_ctx->curr_propname = svn_stringbuf_create_empty(scratch_pool);
254  iprops_ctx->curr_iprop = NULL;
255  iprops_ctx->iprops = apr_array_make(result_pool, 1,
256                                       sizeof(svn_prop_inherited_item_t *));
257  iprops_ctx->path = path;
258  iprops_ctx->revision = revision;
259
260  xmlctx = svn_ra_serf__xml_context_create(iprops_table,
261                                           iprops_opened, iprops_closed, NULL,
262                                           iprops_ctx,
263                                           scratch_pool);
264  handler = svn_ra_serf__create_expat_handler(xmlctx, scratch_pool);
265
266  handler->method = "REPORT";
267  handler->path = req_url;
268  handler->conn = session->conns[0];
269  handler->session = session;
270  handler->body_delegate = create_iprops_body;
271  handler->body_delegate_baton = iprops_ctx;
272  handler->body_type = "text/xml";
273  handler->handler_pool = scratch_pool;
274
275  err = svn_ra_serf__context_run_one(handler, scratch_pool);
276  SVN_ERR(svn_error_compose_create(
277                    svn_ra_serf__error_on_status(handler->sline,
278                                                 handler->path,
279                                                 handler->location),
280                    err));
281
282  *iprops = iprops_ctx->iprops;
283
284  return SVN_NO_ERROR;
285}
286