-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.c
More file actions
235 lines (226 loc) · 5.17 KB
/
Copy pathfetch.c
File metadata and controls
235 lines (226 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Author: Mo McRoberts <mo.mcroberts@bbc.co.uk>
*
* Copyright (c) 2014-2015 BBC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "p_liblod.h"
static int lod_fetch_curl_(LODCONTEXT *context, const char *uri, LODRESPONSE *response);
static size_t lod_fetch_write_(char *ptr, size_t size, size_t nmemb, void *userdata);
/* Unconditionally fetch some LOD and parse it into the existing model */
int
lod_fetch_(LODCONTEXT *context)
{
LODRESPONSE *response;
LODRESULT rr;
int r, count, followed_link;
const char *uri, *fragment;
char *t, *tempuri;
size_t fraglen;
tempuri = NULL;
if(lod_push_subject_(context, context->subject))
{
return -1;
}
/* Save the fragment in case we need to apply it to a
* a redirect URI
*/
if((fragment = strchr(context->subject, '#')))
{
fraglen = strlen(fragment);
}
else
{
fraglen = 0;
}
r = 0;
followed_link = 0;
response = lod_response_create();
if(!response)
{
lod_set_error_(context, "failed to create response object");
return -1;
}
uri = context->subjects[0];
for(count = 0; count < context->max_redirects; count++)
{
lod_response_reset(response);
r = lod_fetch_curl_(context, uri, response);
if(r || response->status <= 0 || response->errmsg)
{
if(response->errmsg)
{
lod_set_error_(context, response->errmsg);
}
else
{
lod_set_error_(context, "an unknown error occurred while fetching the resource");
}
r = 1;
break;
}
rr = lod_response_process(context, response);
switch(rr)
{
case LODR_FAIL:
r = 1;
break;
case LODR_COMPLETE:
r = 0;
break;
case LODR_FOLLOW:
case LODR_FOLLOW_REPLACE:
free(tempuri);
tempuri = (char *) malloc(strlen(response->target) + fraglen + 1);
if(!tempuri)
{
lod_set_error_(context, strerror(errno));
r = 1;
break;
}
strcpy(tempuri, response->target);
uri = tempuri;
if(rr != LODR_FOLLOW_REPLACE)
{
continue;
}
if(fragment)
{
t = strchr(tempuri, '#');
if(t)
{
strcpy(t, fragment);
}
else
{
strcat(tempuri, fragment);
}
}
lod_push_subject_(context, tempuri);
/* tempuri is now owned by context */
tempuri = NULL;
break;
case LODR_FOLLOW_LINK:
/* This will only happen once per fetch loop */
if(followed_link)
{
lod_set_error_(context, "a <link rel=\"alternate\"> has previously been followed in this resolution session; will not do so again");
r = 1;
break;
}
lod_push_subject_(context, response->target);
/* response->target is now owned by the context */
uri = response->target;
response->target = NULL;
followed_link = 1;
break;
}
if(r || rr == LODR_COMPLETE)
{
break;
}
}
if(count == context->max_redirects)
{
lod_set_error_(context, "too many redirects encountered");
r = 1;
}
free(tempuri);
lod_response_destroy(response);
if(r)
{
context->error = 1;
return -1;
}
return 0;
}
/* The default implementation of a LODFETCHURI callback using cURL */
static int
lod_fetch_curl_(LODCONTEXT *context, const char *uri, LODRESPONSE *response)
{
CURL *ch;
CURLcode e;
long code;
char *str;
ch = lod_curl(context);
if(!ch)
{
return -1;
}
curl_easy_setopt(ch, CURLOPT_WRITEDATA, (void *) response);
curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, lod_fetch_write_);
curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0);
curl_easy_setopt(ch, CURLOPT_URL, uri);
e = curl_easy_perform(ch);
if(e)
{
lod_response_set_error(response, curl_easy_strerror(e));
return -1;
}
if((e = curl_easy_getinfo(context->ch, CURLINFO_RESPONSE_CODE, &code)))
{
lod_response_set_error(response, curl_easy_strerror(e));
return -1;
}
if(lod_response_set_status(response, code))
{
return -1;
}
if((e = curl_easy_getinfo(ch, CURLINFO_EFFECTIVE_URL, &str)))
{
lod_response_set_error(response, curl_easy_strerror(e));
return -1;
}
if(lod_response_set_uri(response, str))
{
return -1;
}
if((e = curl_easy_getinfo(ch, CURLINFO_CONTENT_TYPE, &str)))
{
lod_response_set_error(response, curl_easy_strerror(e));
return -1;
}
if(lod_response_set_type(response, str))
{
return -1;
}
if(code >= 300 && code <= 399)
{
if((e = curl_easy_getinfo(ch, CURLINFO_REDIRECT_URL, &str)))
{
lod_response_set_error(response, curl_easy_strerror(e));
return -1;
}
if(lod_response_set_target(response, str))
{
return -1;
}
}
return 0;
}
/* Invoked by libcurl when payload data is received */
static size_t
lod_fetch_write_(char *ptr, size_t size, size_t nmemb, void *userdata)
{
LODRESPONSE *response;
response = (LODRESPONSE *) userdata;
size *= nmemb;
if(lod_response_append_payload(response, ptr, size))
{
return 0;
}
return size;
}