| 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_STANDALONE_CURL_REQUEST_HPP |
| 23 |
#define DAVIX_STANDALONE_CURL_REQUEST_HPP |
| 24 |
|
| 25 |
#include "ResponseBuffer.hpp" |
| 26 |
#include <davix_internal.hpp> |
| 27 |
#include <backend/StandaloneRequest.hpp> |
| 28 |
#include <backend/BoundHooks.hpp> |
| 29 |
#include <params/davixrequestparams.hpp> |
| 30 |
|
| 31 |
struct curl_slist; |
| 32 |
|
| 33 |
namespace Davix { |
| 34 |
|
| 35 |
class CurlSessionFactory; |
| 36 |
class ContentProvider; |
| 37 |
class CurlSession; |
| 38 |
|
| 39 |
//------------------------------------------------------------------------------ |
| 40 |
// Implementation of StandaloneRequest interface based on libcurl. |
| 41 |
//------------------------------------------------------------------------------ |
| 42 |
class StandaloneCurlRequest : public StandaloneRequest { |
| 43 |
public: |
| 44 |
//---------------------------------------------------------------------------- |
| 45 |
// Constructor |
| 46 |
//---------------------------------------------------------------------------- |
| 47 |
StandaloneCurlRequest(CurlSessionFactory &sessionFactory, bool reuseSession, |
| 48 |
const BoundHooks &boundHooks, const Uri &uri, const std::string &verb, const RequestParams ¶ms, |
| 49 |
const std::vector<HeaderLine> &headers, int requestFlag, ContentProvider *contentProvider, |
| 50 |
Chrono::TimePoint deadline); |
| 51 |
|
| 52 |
//---------------------------------------------------------------------------- |
| 53 |
// Destructor |
| 54 |
//---------------------------------------------------------------------------- |
| 55 |
virtual ~StandaloneCurlRequest(); |
| 56 |
|
| 57 |
//---------------------------------------------------------------------------- |
| 58 |
// No evil constructors, no move semantics. |
| 59 |
//---------------------------------------------------------------------------- |
| 60 |
StandaloneCurlRequest(const StandaloneCurlRequest& other) = delete; |
| 61 |
StandaloneCurlRequest(StandaloneCurlRequest&& other) = delete; |
| 62 |
StandaloneCurlRequest& operator=(StandaloneCurlRequest&& other) = delete; |
| 63 |
StandaloneCurlRequest& operator=(const StandaloneCurlRequest& other) = delete; |
| 64 |
|
| 65 |
//---------------------------------------------------------------------------- |
| 66 |
// Get a specific response header |
| 67 |
//---------------------------------------------------------------------------- |
| 68 |
virtual bool getAnswerHeader(const std::string &header_name, std::string &value) const; |
| 69 |
|
| 70 |
//---------------------------------------------------------------------------- |
| 71 |
// Get all response headers |
| 72 |
//---------------------------------------------------------------------------- |
| 73 |
virtual size_t getAnswerHeaders(std::vector<std::pair<std::string, std::string > > & vec_headers) const; |
| 74 |
|
| 75 |
//---------------------------------------------------------------------------- |
| 76 |
// Start request - calling this more than once will not have any effect. |
| 77 |
//---------------------------------------------------------------------------- |
| 78 |
virtual Status startRequest(); |
| 79 |
|
| 80 |
//---------------------------------------------------------------------------- |
| 81 |
// Finish an already started request. |
| 82 |
//---------------------------------------------------------------------------- |
| 83 |
virtual Status endRequest(); |
| 84 |
|
| 85 |
//---------------------------------------------------------------------------- |
| 86 |
// Major read function - read a block of max_size bytes (at max) into buffer. |
| 87 |
//---------------------------------------------------------------------------- |
| 88 |
virtual dav_ssize_t readBlock(char* buffer, dav_size_t max_size, Status& st); |
| 89 |
|
| 90 |
//---------------------------------------------------------------------------- |
| 91 |
// Check request state |
| 92 |
//---------------------------------------------------------------------------- |
| 93 |
virtual RequestState getState() const; |
| 94 |
|
| 95 |
//---------------------------------------------------------------------------- |
| 96 |
// Get status code - returns 0 if impossible to determine |
| 97 |
//---------------------------------------------------------------------------- |
| 98 |
virtual int getStatusCode() const; |
| 99 |
|
| 100 |
//---------------------------------------------------------------------------- |
| 101 |
// Do not re-use underlying session |
| 102 |
//---------------------------------------------------------------------------- |
| 103 |
virtual void doNotReuseSession(); |
| 104 |
|
| 105 |
//---------------------------------------------------------------------------- |
| 106 |
// Has the underlying session been used before? |
| 107 |
//---------------------------------------------------------------------------- |
| 108 |
virtual bool isRecycledSession() const; |
| 109 |
|
| 110 |
//---------------------------------------------------------------------------- |
| 111 |
// Obtain redirected location, store into the given Uri |
| 112 |
//---------------------------------------------------------------------------- |
| 113 |
virtual Status obtainRedirectedLocation(Uri &out); |
| 114 |
|
| 115 |
//---------------------------------------------------------------------------- |
| 116 |
// Get session error, if available |
| 117 |
//---------------------------------------------------------------------------- |
| 118 |
virtual std::string getSessionError() const; |
| 119 |
|
| 120 |
//---------------------------------------------------------------------------- |
| 121 |
// Feed response header |
| 122 |
//---------------------------------------------------------------------------- |
| 123 |
void feedResponseHeader(const std::string &header); |
| 124 |
|
| 125 |
private: |
| 126 |
CurlSessionFactory &_session_factory; |
| 127 |
bool _reuse_session; |
| 128 |
BoundHooks _bound_hooks; |
| 129 |
Uri _uri; |
| 130 |
std::string _verb; |
| 131 |
RequestParams _params; |
| 132 |
std::vector<HeaderLine> _headers; |
| 133 |
int _req_flag; |
| 134 |
ContentProvider *_content_provider; |
| 135 |
Chrono::TimePoint _deadline; |
| 136 |
|
| 137 |
RequestState _state; |
| 138 |
|
| 139 |
std::unique_ptr<CurlSession> _session; |
| 140 |
Status sessionError; |
| 141 |
|
| 142 |
//---------------------------------------------------------------------------- |
| 143 |
// Check if timeout has passed |
| 144 |
//---------------------------------------------------------------------------- |
| 145 |
Status checkTimeout(); |
| 146 |
|
| 147 |
//---------------------------------------------------------------------------- |
| 148 |
// Get remaining number of milliseconds until deadline |
| 149 |
//---------------------------------------------------------------------------- |
| 150 |
uint64_t getRemainingMs() const; |
| 151 |
|
| 152 |
//---------------------------------------------------------------------------- |
| 153 |
// Check internal mhandle errors |
| 154 |
//---------------------------------------------------------------------------- |
| 155 |
Status checkErrors(); |
| 156 |
|
| 157 |
//---------------------------------------------------------------------------- |
| 158 |
// Linked list for storing request headers |
| 159 |
//---------------------------------------------------------------------------- |
| 160 |
struct curl_slist *_chunklist; |
| 161 |
|
| 162 |
//---------------------------------------------------------------------------- |
| 163 |
// Response variables |
| 164 |
//---------------------------------------------------------------------------- |
| 165 |
std::vector<std::pair<std::string, std::string > > _response_headers; |
| 166 |
bool _received_headers; |
| 167 |
|
| 168 |
ResponseBuffer _response_buffer; |
| 169 |
|
| 170 |
//---------------------------------------------------------------------------- |
| 171 |
// Block until all response headers have been received |
| 172 |
//---------------------------------------------------------------------------- |
| 173 |
Status readResponseHeaders(); |
| 174 |
|
| 175 |
//---------------------------------------------------------------------------- |
| 176 |
// Perform a single blocking round of network I/O |
| 177 |
//---------------------------------------------------------------------------- |
| 178 |
Status performBlockingRound(int &still_running); |
| 179 |
|
| 180 |
}; |
| 181 |
|
| 182 |
//------------------------------------------------------------------------------ |
| 183 |
// Get curl version as string |
| 184 |
//------------------------------------------------------------------------------ |
| 185 |
std::string getCurlVersion(); |
| 186 |
} |
| 187 |
|
| 188 |
#endif |
| 189 |
|