]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/subversion/subversion/libsvn_ra_serf/getdate.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / subversion / subversion / libsvn_ra_serf / getdate.c
1 /*
2  * getdate.c :  entry point for get_dated_revision 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 \f
26 #include <apr_uri.h>
27 #include <serf.h>
28
29 #include "svn_pools.h"
30 #include "svn_ra.h"
31 #include "svn_time.h"
32 #include "svn_xml.h"
33
34 #include "private/svn_dav_protocol.h"
35
36 #include "svn_private_config.h"
37
38 #include "../libsvn_ra/ra_loader.h"
39
40 #include "ra_serf.h"
41
42 \f
43 /*
44  * This enum represents the current state of our XML parsing for a REPORT.
45  */
46 enum date_state_e {
47   INITIAL = 0,
48   REPORT,
49   VERSION_NAME
50 };
51
52
53 typedef struct date_context_t {
54   /* The time asked about. */
55   apr_time_t time;
56
57   /* What was the youngest revision at that time? */
58   svn_revnum_t *revision;
59
60 } date_context_t;
61
62 #define D_ "DAV:"
63 #define S_ SVN_XML_NAMESPACE
64 static const svn_ra_serf__xml_transition_t date_ttable[] = {
65   { INITIAL, S_, "dated-rev-report", REPORT,
66     FALSE, { NULL }, FALSE },
67
68   { REPORT, D_, SVN_DAV__VERSION_NAME, VERSION_NAME,
69     TRUE, { NULL }, TRUE },
70
71   { 0 }
72 };
73
74
75 /* Conforms to svn_ra_serf__xml_closed_t  */
76 static svn_error_t *
77 date_closed(svn_ra_serf__xml_estate_t *xes,
78             void *baton,
79             int leaving_state,
80             const svn_string_t *cdata,
81             apr_hash_t *attrs,
82             apr_pool_t *scratch_pool)
83 {
84   date_context_t *date_ctx = baton;
85
86   SVN_ERR_ASSERT(leaving_state == VERSION_NAME);
87   SVN_ERR_ASSERT(cdata != NULL);
88
89   *date_ctx->revision = SVN_STR_TO_REV(cdata->data);
90
91   return SVN_NO_ERROR;
92 }
93
94
95 /* Implements svn_ra_serf__request_body_delegate_t */
96 static svn_error_t *
97 create_getdate_body(serf_bucket_t **body_bkt,
98                     void *baton,
99                     serf_bucket_alloc_t *alloc,
100                     apr_pool_t *pool)
101 {
102   serf_bucket_t *buckets;
103   date_context_t *date_ctx = baton;
104
105   buckets = serf_bucket_aggregate_create(alloc);
106
107   svn_ra_serf__add_open_tag_buckets(buckets, alloc, "S:dated-rev-report",
108                                     "xmlns:S", SVN_XML_NAMESPACE,
109                                     "xmlns:D", "DAV:",
110                                     NULL);
111
112   svn_ra_serf__add_tag_buckets(buckets,
113                                "D:" SVN_DAV__CREATIONDATE,
114                                svn_time_to_cstring(date_ctx->time, pool),
115                                alloc);
116
117   svn_ra_serf__add_close_tag_buckets(buckets, alloc, "S:dated-rev-report");
118
119   *body_bkt = buckets;
120   return SVN_NO_ERROR;
121 }
122
123 svn_error_t *
124 svn_ra_serf__get_dated_revision(svn_ra_session_t *ra_session,
125                                 svn_revnum_t *revision,
126                                 apr_time_t tm,
127                                 apr_pool_t *pool)
128 {
129   date_context_t *date_ctx;
130   svn_ra_serf__session_t *session = ra_session->priv;
131   svn_ra_serf__handler_t *handler;
132   svn_ra_serf__xml_context_t *xmlctx;
133   const char *report_target;
134   svn_error_t *err;
135
136   date_ctx = apr_palloc(pool, sizeof(*date_ctx));
137   date_ctx->time = tm;
138   date_ctx->revision = revision;
139
140   SVN_ERR(svn_ra_serf__report_resource(&report_target, session, NULL, pool));
141
142   xmlctx = svn_ra_serf__xml_context_create(date_ttable,
143                                            NULL, date_closed, NULL,
144                                            date_ctx,
145                                            pool);
146   handler = svn_ra_serf__create_expat_handler(xmlctx, pool);
147
148   handler->method = "REPORT";
149   handler->path = report_target;
150   handler->body_type = "text/xml";
151   handler->conn = session->conns[0];
152   handler->session = session;
153
154   handler->body_delegate = create_getdate_body;
155   handler->body_delegate_baton = date_ctx;
156
157   *date_ctx->revision = SVN_INVALID_REVNUM;
158
159   err = svn_ra_serf__context_run_one(handler, pool);
160
161   SVN_ERR(svn_error_compose_create(
162               svn_ra_serf__error_on_status(handler->sline,
163                                            report_target,
164                                            handler->location),
165               err));
166
167   SVN_ERR_ASSERT(SVN_IS_VALID_REVNUM(*revision));
168
169   return SVN_NO_ERROR;
170 }