1/*
2 * getlocationsegments.c :  entry point for get_location_segments
3 *                          RA functions for ra_serf
4 *
5 * ====================================================================
6 *    Licensed to the Apache Software Foundation (ASF) under one
7 *    or more contributor license agreements.  See the NOTICE file
8 *    distributed with this work for additional information
9 *    regarding copyright ownership.  The ASF licenses this file
10 *    to you under the Apache License, Version 2.0 (the
11 *    "License"); you may not use this file except in compliance
12 *    with the License.  You may obtain a copy of the License at
13 *
14 *      http://www.apache.org/licenses/LICENSE-2.0
15 *
16 *    Unless required by applicable law or agreed to in writing,
17 *    software distributed under the License is distributed on an
18 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 *    KIND, either express or implied.  See the License for the
20 *    specific language governing permissions and limitations
21 *    under the License.
22 * ====================================================================
23 */
24
25
26
27#include <apr_uri.h>
28#include <serf.h>
29
30#include "svn_hash.h"
31#include "svn_pools.h"
32#include "svn_ra.h"
33#include "svn_xml.h"
34#include "svn_path.h"
35#include "svn_private_config.h"
36#include "../libsvn_ra/ra_loader.h"
37
38#include "ra_serf.h"
39
40
41
42typedef struct gls_context_t {
43  /* parameters set by our caller */
44  svn_revnum_t peg_revision;
45  svn_revnum_t start_rev;
46  svn_revnum_t end_rev;
47  const char *path;
48
49  /* location segment callback function/baton */
50  svn_location_segment_receiver_t receiver;
51  void *receiver_baton;
52
53} gls_context_t;
54
55enum {
56  INITIAL = 0,
57  REPORT,
58  SEGMENT
59};
60
61#define D_ "DAV:"
62#define S_ SVN_XML_NAMESPACE
63static const svn_ra_serf__xml_transition_t gls_ttable[] = {
64  { INITIAL, S_, "get-location-segments-report", REPORT,
65    FALSE, { NULL }, FALSE },
66
67  { REPORT, S_, "location-segment", SEGMENT,
68    FALSE, { "?path", "range-start", "range-end", NULL }, TRUE },
69
70  { 0 }
71};
72
73
74/* Conforms to svn_ra_serf__xml_closed_t  */
75static svn_error_t *
76gls_closed(svn_ra_serf__xml_estate_t *xes,
77           void *baton,
78           int leaving_state,
79           const svn_string_t *cdata,
80           apr_hash_t *attrs,
81           apr_pool_t *scratch_pool)
82{
83  gls_context_t *gls_ctx = baton;
84  const char *path;
85  const char *start_str;
86  const char *end_str;
87  svn_location_segment_t segment;
88
89  SVN_ERR_ASSERT(leaving_state == SEGMENT);
90
91  path = svn_hash_gets(attrs, "path");
92  start_str = svn_hash_gets(attrs, "range-start");
93  end_str = svn_hash_gets(attrs, "range-end");
94
95  /* The transition table said these must exist.  */
96  SVN_ERR_ASSERT(start_str && end_str);
97
98  segment.path = path;  /* may be NULL  */
99  segment.range_start = SVN_STR_TO_REV(start_str);
100  segment.range_end = SVN_STR_TO_REV(end_str);
101  SVN_ERR(gls_ctx->receiver(&segment, gls_ctx->receiver_baton, scratch_pool));
102
103  return SVN_NO_ERROR;
104}
105
106
107/* Implements svn_ra_serf__request_body_delegate_t */
108static svn_error_t *
109create_gls_body(serf_bucket_t **body_bkt,
110                void *baton,
111                serf_bucket_alloc_t *alloc,
112                apr_pool_t *pool)
113{
114  serf_bucket_t *buckets;
115  gls_context_t *gls_ctx = baton;
116
117  buckets = serf_bucket_aggregate_create(alloc);
118
119  svn_ra_serf__add_open_tag_buckets(buckets, alloc,
120                                    "S:get-location-segments",
121                                    "xmlns:S", SVN_XML_NAMESPACE,
122                                    NULL);
123
124  svn_ra_serf__add_tag_buckets(buckets,
125                               "S:path", gls_ctx->path,
126                               alloc);
127
128  svn_ra_serf__add_tag_buckets(buckets,
129                               "S:peg-revision",
130                               apr_ltoa(pool, gls_ctx->peg_revision),
131                               alloc);
132
133  svn_ra_serf__add_tag_buckets(buckets,
134                               "S:start-revision",
135                               apr_ltoa(pool, gls_ctx->start_rev),
136                               alloc);
137
138  svn_ra_serf__add_tag_buckets(buckets,
139                               "S:end-revision",
140                               apr_ltoa(pool, gls_ctx->end_rev),
141                               alloc);
142
143  svn_ra_serf__add_close_tag_buckets(buckets, alloc,
144                                     "S:get-location-segments");
145
146  *body_bkt = buckets;
147  return SVN_NO_ERROR;
148}
149
150svn_error_t *
151svn_ra_serf__get_location_segments(svn_ra_session_t *ra_session,
152                                   const char *path,
153                                   svn_revnum_t peg_revision,
154                                   svn_revnum_t start_rev,
155                                   svn_revnum_t end_rev,
156                                   svn_location_segment_receiver_t receiver,
157                                   void *receiver_baton,
158                                   apr_pool_t *pool)
159{
160  gls_context_t *gls_ctx;
161  svn_ra_serf__session_t *session = ra_session->priv;
162  svn_ra_serf__handler_t *handler;
163  svn_ra_serf__xml_context_t *xmlctx;
164  const char *req_url;
165  svn_error_t *err;
166
167  gls_ctx = apr_pcalloc(pool, sizeof(*gls_ctx));
168  gls_ctx->path = path;
169  gls_ctx->peg_revision = peg_revision;
170  gls_ctx->start_rev = start_rev;
171  gls_ctx->end_rev = end_rev;
172  gls_ctx->receiver = receiver;
173  gls_ctx->receiver_baton = receiver_baton;
174
175  SVN_ERR(svn_ra_serf__get_stable_url(&req_url, NULL /* latest_revnum */,
176                                      session, NULL /* conn */,
177                                      NULL /* url */, peg_revision,
178                                      pool, pool));
179
180  xmlctx = svn_ra_serf__xml_context_create(gls_ttable,
181                                           NULL, gls_closed, NULL,
182                                           gls_ctx,
183                                           pool);
184  handler = svn_ra_serf__create_expat_handler(xmlctx, pool);
185
186  handler->method = "REPORT";
187  handler->path = req_url;
188  handler->body_delegate = create_gls_body;
189  handler->body_delegate_baton = gls_ctx;
190  handler->body_type = "text/xml";
191  handler->conn = session->conns[0];
192  handler->session = session;
193
194  err = svn_ra_serf__context_run_one(handler, pool);
195
196  err = svn_error_compose_create(
197         svn_ra_serf__error_on_status(handler->sline,
198                                      handler->path,
199                                      handler->location),
200         err);
201
202  if (err && (err->apr_err == SVN_ERR_UNSUPPORTED_FEATURE))
203    return svn_error_create(SVN_ERR_RA_NOT_IMPLEMENTED, err, NULL);
204
205  return svn_error_trace(err);
206}
207