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 locseg_state_e {
56  INITIAL = XML_STATE_INITIAL,
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  apr_int64_t start_val;
88  apr_int64_t end_val;
89  svn_location_segment_t segment;
90
91  SVN_ERR_ASSERT(leaving_state == SEGMENT);
92
93  path = svn_hash_gets(attrs, "path");
94  start_str = svn_hash_gets(attrs, "range-start");
95  end_str = svn_hash_gets(attrs, "range-end");
96
97  /* The transition table said these must exist.  */
98  SVN_ERR_ASSERT(start_str && end_str);
99
100  SVN_ERR(svn_cstring_atoi64(&start_val, start_str));
101  SVN_ERR(svn_cstring_atoi64(&end_val, end_str));
102
103  segment.path = path;  /* may be NULL  */
104  segment.range_start = (svn_revnum_t)start_val;
105  segment.range_end = (svn_revnum_t)end_val;
106  SVN_ERR(gls_ctx->receiver(&segment, gls_ctx->receiver_baton, scratch_pool));
107
108  return SVN_NO_ERROR;
109}
110
111
112/* Implements svn_ra_serf__request_body_delegate_t */
113static svn_error_t *
114create_gls_body(serf_bucket_t **body_bkt,
115                void *baton,
116                serf_bucket_alloc_t *alloc,
117                apr_pool_t *pool /* request pool */,
118                apr_pool_t *scratch_pool)
119{
120  serf_bucket_t *buckets;
121  gls_context_t *gls_ctx = baton;
122
123  buckets = serf_bucket_aggregate_create(alloc);
124
125  svn_ra_serf__add_open_tag_buckets(buckets, alloc,
126                                    "S:get-location-segments",
127                                    "xmlns:S", SVN_XML_NAMESPACE,
128                                    SVN_VA_NULL);
129
130  svn_ra_serf__add_tag_buckets(buckets,
131                               "S:path", gls_ctx->path,
132                               alloc);
133
134  svn_ra_serf__add_tag_buckets(buckets,
135                               "S:peg-revision",
136                               apr_ltoa(pool, gls_ctx->peg_revision),
137                               alloc);
138
139  svn_ra_serf__add_tag_buckets(buckets,
140                               "S:start-revision",
141                               apr_ltoa(pool, gls_ctx->start_rev),
142                               alloc);
143
144  svn_ra_serf__add_tag_buckets(buckets,
145                               "S:end-revision",
146                               apr_ltoa(pool, gls_ctx->end_rev),
147                               alloc);
148
149  svn_ra_serf__add_close_tag_buckets(buckets, alloc,
150                                     "S:get-location-segments");
151
152  *body_bkt = buckets;
153  return SVN_NO_ERROR;
154}
155
156svn_error_t *
157svn_ra_serf__get_location_segments(svn_ra_session_t *ra_session,
158                                   const char *path,
159                                   svn_revnum_t peg_revision,
160                                   svn_revnum_t start_rev,
161                                   svn_revnum_t end_rev,
162                                   svn_location_segment_receiver_t receiver,
163                                   void *receiver_baton,
164                                   apr_pool_t *pool)
165{
166  gls_context_t *gls_ctx;
167  svn_ra_serf__session_t *session = ra_session->priv;
168  svn_ra_serf__handler_t *handler;
169  svn_ra_serf__xml_context_t *xmlctx;
170  const char *req_url;
171  svn_error_t *err;
172
173  gls_ctx = apr_pcalloc(pool, sizeof(*gls_ctx));
174  gls_ctx->path = path;
175  gls_ctx->peg_revision = peg_revision;
176  gls_ctx->start_rev = start_rev;
177  gls_ctx->end_rev = end_rev;
178  gls_ctx->receiver = receiver;
179  gls_ctx->receiver_baton = receiver_baton;
180
181  SVN_ERR(svn_ra_serf__get_stable_url(&req_url, NULL /* latest_revnum */,
182                                      session, NULL /* url */, peg_revision,
183                                      pool, pool));
184
185  xmlctx = svn_ra_serf__xml_context_create(gls_ttable,
186                                           NULL, gls_closed, NULL,
187                                           gls_ctx,
188                                           pool);
189  handler = svn_ra_serf__create_expat_handler(session, xmlctx, NULL, pool);
190
191  handler->method = "REPORT";
192  handler->path = req_url;
193  handler->body_delegate = create_gls_body;
194  handler->body_delegate_baton = gls_ctx;
195  handler->body_type = "text/xml";
196
197  err = svn_ra_serf__context_run_one(handler, pool);
198
199  if (!err && handler->sline.code != 200)
200    err = svn_ra_serf__unexpected_status(handler);
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