| 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 |
#ifndef DAVIX_BACKEND_STANDALONE_NEON_REQUEST_HPP |
| 23 |
#define DAVIX_BACKEND_STANDALONE_NEON_REQUEST_HPP |
| 24 |
|
| 25 |
#include <backend/StandaloneRequest.hpp> |
| 26 |
#include "BoundHooks.hpp" |
| 27 |
#include <davix_internal.hpp> |
| 28 |
#include <utils/davix_uri.hpp> |
| 29 |
#include <ne_request.h> |
| 30 |
#include <params/davixrequestparams.hpp> |
| 31 |
#include <status/DavixStatus.hpp> |
| 32 |
#include <memory> |
| 33 |
|
| 34 |
namespace Davix { |
| 35 |
|
| 36 |
class NEONSessionFactory; |
| 37 |
class Uri; |
| 38 |
class RequestParams; |
| 39 |
class NeonSessionWrapper; |
| 40 |
|
| 41 |
//------------------------------------------------------------------------------ |
| 42 |
// Represent a single, standalone HTTP request with libneon implementation. |
| 43 |
// |
| 44 |
// No magic here. No automatic redirections, no cached redirections, no |
| 45 |
// compatibility hacks, no automatic retries, no automatic URL signing for S3 |
| 46 |
// or Azure -- NOTHING. Just a single HTTP request / response. |
| 47 |
// |
| 48 |
// The magic is to be added by higher-level classes building upon this one. |
| 49 |
//------------------------------------------------------------------------------ |
| 50 |
class StandaloneNeonRequest : public StandaloneRequest { |
| 51 |
public: |
| 52 |
//---------------------------------------------------------------------------- |
| 53 |
// Constructor |
| 54 |
// |
| 55 |
// sessionFactory: Required to obtain an HTTP session. |
| 56 |
// |
| 57 |
// reuseSession: Whether to re-use a previously created session, if available, |
| 58 |
// or create a brand-new one. |
| 59 |
// |
| 60 |
// boundHooks: Sometimes the user will set hooks which activate at certain |
| 61 |
// points during request execution. |
| 62 |
// |
| 63 |
// uri: Target URI to hit. |
| 64 |
// |
| 65 |
// param: Request parameters, authentication, etc. |
| 66 |
//---------------------------------------------------------------------------- |
| 67 |
StandaloneNeonRequest(NEONSessionFactory &sessionFactory, bool reuseSession, |
| 68 |
const BoundHooks &boundHooks, const Uri &uri, const std::string &verb, const RequestParams ¶ms, |
| 69 |
const std::vector<HeaderLine> &headers, int requestFlag, ContentProvider *contentProvider, |
| 70 |
Chrono::TimePoint deadline); |
| 71 |
|
| 72 |
//---------------------------------------------------------------------------- |
| 73 |
// Destructor |
| 74 |
//---------------------------------------------------------------------------- |
| 75 |
virtual ~StandaloneNeonRequest(); |
| 76 |
|
| 77 |
//---------------------------------------------------------------------------- |
| 78 |
// No evil constructors, no move semantics. |
| 79 |
//---------------------------------------------------------------------------- |
| 80 |
StandaloneNeonRequest(const StandaloneNeonRequest& other) = delete; |
| 81 |
StandaloneNeonRequest(StandaloneNeonRequest&& other) = delete; |
| 82 |
StandaloneNeonRequest& operator=(StandaloneNeonRequest&& other) = delete; |
| 83 |
StandaloneNeonRequest& operator=(const StandaloneNeonRequest& other) = delete; |
| 84 |
|
| 85 |
//---------------------------------------------------------------------------- |
| 86 |
// Get a specific response header |
| 87 |
//---------------------------------------------------------------------------- |
| 88 |
virtual bool getAnswerHeader(const std::string &header_name, std::string &value) const; |
| 89 |
|
| 90 |
//---------------------------------------------------------------------------- |
| 91 |
// Get all response headers |
| 92 |
//---------------------------------------------------------------------------- |
| 93 |
virtual size_t getAnswerHeaders(std::vector<std::pair<std::string, std::string > > & vec_headers) const; |
| 94 |
|
| 95 |
//---------------------------------------------------------------------------- |
| 96 |
// Start request - calling this more than once will not have any effect. |
| 97 |
//---------------------------------------------------------------------------- |
| 98 |
virtual Status startRequest(); |
| 99 |
|
| 100 |
//---------------------------------------------------------------------------- |
| 101 |
// Finish an already started request. |
| 102 |
//---------------------------------------------------------------------------- |
| 103 |
virtual Status endRequest(); |
| 104 |
|
| 105 |
//---------------------------------------------------------------------------- |
| 106 |
// Major read function - read a block of max_size bytes (at max) into buffer. |
| 107 |
//---------------------------------------------------------------------------- |
| 108 |
virtual dav_ssize_t readBlock(char* buffer, dav_size_t max_size, Status& st); |
| 109 |
|
| 110 |
//---------------------------------------------------------------------------- |
| 111 |
// Check request state |
| 112 |
//---------------------------------------------------------------------------- |
| 113 |
virtual RequestState getState() const; |
| 114 |
|
| 115 |
//---------------------------------------------------------------------------- |
| 116 |
// Get status code - returns 0 if impossible to determine |
| 117 |
//---------------------------------------------------------------------------- |
| 118 |
virtual int getStatusCode() const; |
| 119 |
|
| 120 |
//---------------------------------------------------------------------------- |
| 121 |
// Do not re-use underlying session |
| 122 |
//---------------------------------------------------------------------------- |
| 123 |
virtual void doNotReuseSession(); |
| 124 |
|
| 125 |
//---------------------------------------------------------------------------- |
| 126 |
// Has the underlying session been used before? |
| 127 |
//---------------------------------------------------------------------------- |
| 128 |
virtual bool isRecycledSession() const; |
| 129 |
|
| 130 |
//---------------------------------------------------------------------------- |
| 131 |
// Obtain redirected location, store into the given Uri |
| 132 |
//---------------------------------------------------------------------------- |
| 133 |
virtual Status obtainRedirectedLocation(Uri &out); |
| 134 |
|
| 135 |
//---------------------------------------------------------------------------- |
| 136 |
// Get session error, if available |
| 137 |
//---------------------------------------------------------------------------- |
| 138 |
virtual std::string getSessionError() const; |
| 139 |
|
| 140 |
private: |
| 141 |
NEONSessionFactory &_session_factory; |
| 142 |
bool _reuse_session; |
| 143 |
BoundHooks _bound_hooks; |
| 144 |
Uri _uri; |
| 145 |
std::string _verb; |
| 146 |
RequestParams _params; |
| 147 |
RequestState _state; |
| 148 |
std::vector<HeaderLine> _headers; |
| 149 |
int _req_flag; |
| 150 |
ContentProvider *_content_provider; |
| 151 |
Chrono::TimePoint _deadline; |
| 152 |
|
| 153 |
friend class NeonSessionWrapper; |
| 154 |
std::unique_ptr<NeonSessionWrapper> _session; |
| 155 |
ne_request* _neon_req; |
| 156 |
dav_ssize_t _total_read_size; |
| 157 |
dav_ssize_t _last_read; |
| 158 |
|
| 159 |
//---------------------------------------------------------------------------- |
| 160 |
// Check if timeout has passed |
| 161 |
//---------------------------------------------------------------------------- |
| 162 |
Status checkTimeout(); |
| 163 |
|
| 164 |
//---------------------------------------------------------------------------- |
| 165 |
// Mark request as completed, release any resources |
| 166 |
//---------------------------------------------------------------------------- |
| 167 |
void markCompleted(); |
| 168 |
|
| 169 |
//---------------------------------------------------------------------------- |
| 170 |
// Create davix error object based on errors in the current session, |
| 171 |
// or request |
| 172 |
//---------------------------------------------------------------------------- |
| 173 |
Status createError(int ne_status); |
| 174 |
|
| 175 |
}; |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
#endif |