Debian Codegraph @

src/backend/StandaloneNeonRequest.cpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) CERN 2019
4 * Author: Georgios Bitzes <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include "StandaloneNeonRequest.hpp"
23 #include <status/davixstatusrequest.hpp>
24 #include <core/ContentProvider.hpp>
25 #include <neon/neonsession.hpp>
26 #include <ne_redirect.h>
27 #include <ne_request.h>
28
29 #define DBG(message) std::cerr << __FILE__ << ":" << __LINE__ << " -- " << #message << " = " << message << std::endl;
30
31 namespace Davix {
32
33 //------------------------------------------------------------------------------
34 // Wrapper class for neon sessions - setup and tear down hooks
35 //------------------------------------------------------------------------------
36 class NeonSessionWrapper {
37 public:
38 NeonSessionWrapper(StandaloneNeonRequest* r, NEONSessionFactory &factory, const Uri &uri, const RequestParams &p, DavixError **err)
39 : _r(r) {
40 _sess = factory.provideNEONSession(uri, p, err);
41
42 if(_sess && _sess->get_ne_sess() != NULL){
43 ne_hook_pre_send(_sess->get_ne_sess(), NeonSessionWrapper::runHookPreSend, (void*) this);
44 ne_hook_post_headers(_sess->get_ne_sess(), NeonSessionWrapper::runHookPreReceive, (void*) this);
45 }
46 }
47
48 virtual ~NeonSessionWrapper() {
49 if(_sess && _sess->get_ne_sess() != NULL){
50 ne_unhook_pre_send(_sess->get_ne_sess(), NeonSessionWrapper::runHookPreSend, (void*) this);
51 ne_unhook_post_headers(_sess->get_ne_sess(), NeonSessionWrapper::runHookPreReceive, (void*) this);
52 }
53 }
54
55 ne_session* get_ne_sess() {
56 if (_sess) {
57 return _sess->get_ne_sess();
58 }
59 return {};
60 }
61
62 bool isRecycledSession() const {
63 return _sess->isRecycledSession();
64 }
65
66 void do_not_reuse_this_session() {
67 if(_sess) {
68 _sess->do_not_reuse_this_session();
69 }
70 }
71
72 private:
73 static void runHookPreSend(ne_request *r, void *userdata, ne_buffer *header) {
74 (void) r;
75
76 NeonSessionWrapper* wrapper = (NeonSessionWrapper*) userdata;
77 BoundHooks &boundHooks = wrapper->_r->_bound_hooks;
78 if(boundHooks.presendHook) {
79 std::string header_line(header->data, (header->used)-1);
80 boundHooks.presendHook(header_line);
81 }
82 }
83
84 static void runHookPreReceive(ne_request *r, void *userdata, const ne_status *status) {
85 (void) r;
86
87 NeonSessionWrapper* wrapper = (NeonSessionWrapper*) userdata;
88 BoundHooks &boundHooks = wrapper->_r->_bound_hooks;
89 if(boundHooks.prereceiveHook){
90 std::ostringstream header_line;
91 HeaderVec headers;
92 wrapper->_r->getAnswerHeaders(headers);
93 header_line << "HTTP/"<< status->major_version << '.' << status->minor_version
94 << ' ' << status->code << ' ' << status->reason_phrase << '\n';
95
96 boundHooks.prereceiveHook(header_line.str(), headers, status->code);
97 }
98 }
99
100 std::unique_ptr<NEONSession> _sess;
101 StandaloneNeonRequest* _r;
102 };
103
104 //------------------------------------------------------------------------------
105 // Callback for libneon to use the content provider
106 //------------------------------------------------------------------------------
107 static ssize_t content_provider_callback(void* userdata, char* buffer, size_t buflen) {
108 ContentProvider *provider = static_cast<ContentProvider*>(userdata);
109
110 if(buflen == 0) {
111 if(provider->rewind()) {
112 // Success
113 return 0;
114 }
115
116 return 1;
117 }
118
119 return provider->pullBytes(buffer, buflen);
120 }
121
122 //------------------------------------------------------------------------------
123 // Constructor
124 //------------------------------------------------------------------------------
125 StandaloneNeonRequest::StandaloneNeonRequest(NEONSessionFactory &sessionFactory, bool reuseSession,
126 const BoundHooks &boundHooks, const Uri &uri, const std::string &verb, const RequestParams &params,
127 const std::vector<HeaderLine> &headers, int reqFlag, ContentProvider *contentProvider,
128 Chrono::TimePoint deadline)
129
130 : _session_factory(sessionFactory), _reuse_session(reuseSession), _bound_hooks(boundHooks),
131 _uri(uri), _verb(verb), _params(params), _state(RequestState::kNotStarted),
132 _headers(headers), _req_flag(reqFlag), _content_provider(contentProvider),
133 _deadline(deadline), _neon_req(NULL), _total_read_size(0), _last_read(-1) {
134 name = "Neon";
135 }
136
137 //------------------------------------------------------------------------------
138 // Destructor
139 //------------------------------------------------------------------------------
140 StandaloneNeonRequest::~StandaloneNeonRequest() {
141 markCompleted();
142
143 if(_neon_req) {
144 ne_request_destroy(_neon_req);
145 _neon_req = NULL;
146 }
147
148 _session.reset();
149 }
150
151 //------------------------------------------------------------------------------
152 // Map a neon error to davix error
153 //------------------------------------------------------------------------------
154 static void neon_error_mapper(int ne_status, StatusCode::Code & code, std::string & str, const std::string &wwwAuth) {
155 switch(ne_status){
156 case NE_OK: {
157 code = StatusCode::OK;
158 str = "Status Ok";
159 break;
160 }
161 case NE_LOOKUP: {
162 code = StatusCode::NameResolutionFailure;
163 str = "Domain name resolution failed";
164 break;
165 }
166 case NE_AUTH: {
167 code = StatusCode::AuthenticationError;
168 str = "Authentication failed on server";
169 break;
170 }
171 case NE_PROXYAUTH: {
172 code = StatusCode::AuthenticationError;
173 str = "Authentication failed on proxy";
174 break;
175 }
176 case NE_CONNECT: {
177 code = StatusCode::ConnectionProblem;
178 str = "Could not connect to server";
179 break;
180 }
181 case NE_TIMEOUT: {
182 code = StatusCode::ConnectionTimeout;
183 str = "Connection timed out";
184 break;
185 }
186 case NE_FAILED: {
187 code = StatusCode::SessionCreationError;
188 str = "The precondition failed";
189 break;
190 }
191 case NE_RETRY: {
192 code = StatusCode::RedirectionNeeded;
193 str = "Retry Request";
194 break;
195 }
196 default: {
197 code = StatusCode::UnknownError;
198 str = "Unknown Error from libneon";
199 }
200 }
201
202 if(!wwwAuth.empty()) {
203 str += " (WWW-Authenticate: ";
204 str += wwwAuth;
205 str += ")";
206 }
207 }
208
209 //------------------------------------------------------------------------------
210 // Start request - calling this multiple times will do nothing.
211 //------------------------------------------------------------------------------
212 Status StandaloneNeonRequest::startRequest() {
213 if(_state != RequestState::kNotStarted) {
214 return Status(); ;
215 }
216
217 //----------------------------------------------------------------------------
218 // Have we timed out already?
219 //----------------------------------------------------------------------------
220 Status st = checkTimeout();
221 if(!st.ok()) {
222 markCompleted();
223 return st;
224 }
225
226 //----------------------------------------------------------------------------
227 // Retrieve a session, create request
228 //----------------------------------------------------------------------------
229 DavixError* tmp_err = NULL;
230 _session.reset(new NeonSessionWrapper(this, _session_factory, _uri, _params, &tmp_err));
231
232 if(tmp_err) {
233 markCompleted();
234 return Status(&tmp_err);
235 }
236
237 _neon_req = ne_request_create(_session->get_ne_sess(), _verb.c_str(), _uri.getPathAndQuery().c_str());
238
239 //----------------------------------------------------------------------------
240 // Setup headers
241 //----------------------------------------------------------------------------
242 for(size_t i = 0; i < _headers.size(); i++) {
243 ne_add_request_header(_neon_req, _headers[i].first.c_str(), _headers[i].second.c_str());
244 }
245
246 //----------------------------------------------------------------------------
247 // Setup flags
248 //----------------------------------------------------------------------------
249 ne_set_request_flag(_neon_req, NE_REQFLAG_EXPECT100, _params.get100ContinueSupport() &&
250 (_req_flag & RequestFlag::SupportContinue100));
251 ne_set_request_flag(_neon_req, NE_REQFLAG_IDEMPOTENT, _req_flag & RequestFlag::IdempotentRequest);
252
253 if( (_req_flag & RequestFlag::SupportContinue100) == true) {
254 _session->do_not_reuse_this_session();
255 }
256
257 //----------------------------------------------------------------------------
258 // Setup HTTP body
259 //----------------------------------------------------------------------------
260 if(_content_provider) {
261 _content_provider->rewind();
262 ne_set_request_body_provider(_neon_req, _content_provider->getSize(),
263 content_provider_callback, _content_provider);
264 }
265
266 //----------------------------------------------------------------------------
267 // Initiate the network connection
268 //----------------------------------------------------------------------------
269 int status = ne_begin_request(_neon_req);
270
271 if(status != NE_OK && status != NE_REDIRECT) {
272 //--------------------------------------------------------------------------
273 // Network trouble, don't re-use session
274 //--------------------------------------------------------------------------
275 Status st = createError(status);
276 _session->do_not_reuse_this_session();
277 markCompleted();
278 return st;
279 }
280
281 //----------------------------------------------------------------------------
282 // Connection OK, we're good to go
283 //----------------------------------------------------------------------------
284 _state = RequestState::kStarted;
285 return Status();
286 }
287
288 //------------------------------------------------------------------------------
289 // Finish an already started request.
290 //------------------------------------------------------------------------------
291 Status StandaloneNeonRequest::endRequest() {
292 markCompleted();
293 return Status();
294 }
295
296 //------------------------------------------------------------------------------
297 // Check if timeout has passed
298 //------------------------------------------------------------------------------
299 Status StandaloneNeonRequest::checkTimeout() {
300 if(_deadline.isValid() && _deadline < Chrono::Clock(Chrono::Clock::Monolitic).now()) {
301 std::ostringstream ss;
302 ss << "timeout of " << _params.getOperationTimeout()->tv_sec << "s";
303 return Status(davix_scope_http_request(), StatusCode::OperationTimeout, ss.str());
304 }
305
306 return Status();
307 }
308
309 //------------------------------------------------------------------------------
310 // Major read function - read a block of max_size bytes (at max) into buffer.
311 //------------------------------------------------------------------------------
312 dav_ssize_t StandaloneNeonRequest::readBlock(char* buffer, dav_size_t max_size, Status& st) {
313
314 if(!_neon_req) {
315 st = Status(davix_scope_http_request(), StatusCode::AlreadyRunning, "Request has not been started yet");
316 return -1;
317 }
318
319 if(max_size == 0) {
320 return 0;
321 }
322
323 if(_last_read == 0) {
324 return 0;
325 }
326
327 st = checkTimeout();
328 if(!st.ok()) {
329 return -1;
330 }
331
332 _last_read = ne_read_response_block(_neon_req, buffer, max_size);
333 if(_last_read < 0) {
334 st = Status(davix_scope_http_request(), StatusCode::ConnectionProblem, "Invalid read in request");
335 _session->do_not_reuse_this_session();
336 markCompleted();
337 return -1;
338 }
339
340 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_HTTP, "StandaloneNeonRequestNeonRequest::readBlock read {} bytes", _last_read);
341
342 _total_read_size += _last_read;
343 return _last_read;
344 }
345
346 //------------------------------------------------------------------------------
347 // Check request state
348 //------------------------------------------------------------------------------
349 RequestState StandaloneNeonRequest::getState() const {
350 return _state;
351 }
352
353 //------------------------------------------------------------------------------
354 // Get a specific response header
355 //------------------------------------------------------------------------------
356 bool StandaloneNeonRequest::getAnswerHeader(const std::string &header_name, std::string &value) const {
357 if(_neon_req){
358 const char* answer_content = ne_get_response_header(_neon_req, header_name.c_str());
359 if(answer_content) {
360 value = answer_content;
361 return true;
362 }
363 }
364
365 return false;
366 }
367
368 //------------------------------------------------------------------------------
369 // Get all response headers
370 //------------------------------------------------------------------------------
371 size_t StandaloneNeonRequest::getAnswerHeaders( HeaderVec & vec_headers) const {
372 if(_neon_req) {
373 void * handle = NULL;
374 const char* name = NULL, *value = NULL;
375 while( (handle = ne_response_header_iterate(_neon_req, handle, &name, &value)) != NULL){
376 vec_headers.push_back(std::pair<std::string, std::string>(name, value));
377 }
378 }
379
380 return vec_headers.size();
381 }
382
383 //------------------------------------------------------------------------------
384 // Mark request as completed, release any resources
385 //------------------------------------------------------------------------------
386 void StandaloneNeonRequest::markCompleted() {
387 if(_state == RequestState::kFinished) {
388 return;
389 }
390
391 _state = RequestState::kFinished;
392
393 if(_neon_req) {
394 if(_last_read == 0) {
395 ne_end_request(_neon_req);
396 }
397 else {
398 ne_abort_request(_neon_req);
399 _session->do_not_reuse_this_session();
400 }
401 }
402 }
403
404 //------------------------------------------------------------------------------
405 // Create davix error object based on errors in the current session,
406 // or request
407 //------------------------------------------------------------------------------
408 Status StandaloneNeonRequest::createError(int ne_status) {
409 StatusCode::Code code;
410 std::string str, wwwAuth;
411 this->getAnswerHeader("WWW-Authenticate", wwwAuth);
412
413 if(ne_status == NE_ERROR && _session) {
414 const char * neon_error = ne_get_error(_session->get_ne_sess());
415 str = std::string("(Neon): ").append((neon_error)?(neon_error):"");
416 if (str.find("SSL handshake failed") == std::string::npos) {
417 code = StatusCode::ConnectionProblem;
418 }
419 else {
420 code = StatusCode::SSLError;
421 }
422 }
423 else {
424 neon_error_mapper(ne_status, code, str, wwwAuth);
425 }
426
427 return Status(davix_scope_http_request(), code, str);
428 }
429
430 //------------------------------------------------------------------------------
431 // Get status code - returns 0 if impossible to determine
432 //------------------------------------------------------------------------------
433 int StandaloneNeonRequest::getStatusCode() const {
434 if(_neon_req) {
435 return ne_get_status(_neon_req)->code;
436 }
437
438 return 0;
439 }
440
441 //------------------------------------------------------------------------------
442 // Do not re-use underlying session
443 //------------------------------------------------------------------------------
444 void StandaloneNeonRequest::doNotReuseSession() {
445 if(_session) {
446 _session->do_not_reuse_this_session();
447 }
448 }
449
450 //------------------------------------------------------------------------------
451 // Has the underlying session been used before?
452 //------------------------------------------------------------------------------
453 bool StandaloneNeonRequest::isRecycledSession() const {
454 if(_session) {
455 return _session->isRecycledSession();
456 }
457
458 // We don't have a session at all, so by definition we can't be using any
459 // recycled session
460 return false;
461 }
462
463 //------------------------------------------------------------------------------
464 // Get session error, if available
465 //------------------------------------------------------------------------------
466 std::string StandaloneNeonRequest::getSessionError() const {
467 if(!_session) {
468 return std::string();
469 }
470
471 const char *neon_error = ne_get_error(_session->get_ne_sess());
472 if(neon_error) {
473 return std::string(neon_error);
474 }
475
476 return std::string();
477 }
478
479 //------------------------------------------------------------------------------
480 // Obtain redirected location, store into the given Uri
481 //------------------------------------------------------------------------------
482 Status StandaloneNeonRequest::obtainRedirectedLocation(Uri &out) {
483 if(!_neon_req) {
484 return Status(davix_scope_http_request(), StatusCode::InvalidArgument, "Request not active, impossible to obtain redirected location");
485 }
486
487 void * handle = NULL;
488 const char* name = NULL, *value = NULL;
489 while( (handle = ne_response_header_iterate(_neon_req, handle, &name, &value)) != NULL) {
490 if(strcasecmp("location", name) == 0) {
491 std::string location(value);
492 // Dealing with relative location [DMC-1209]
493 if (!location.empty() && location[0] == '/') {
494 out = Uri::fromRelativePath(_uri, location);
495 } else {
496 out = Uri(location);
497 }
498 if (out.getStatus() != StatusCode::OK) {
499 return Status(davix_scope_http_request(), out.getStatus(),
500 fmt::format("Failed to parse redirect location: {}", out.getString()));
501 }
502 return Status();
503 }
504 }
505
506 return Status(davix_scope_http_request(), StatusCode::InvalidArgument, "Could not find Location header in answer headers");
507 }
508
509 }