| 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_CORE_CONTENT_PROVIDER_HPP |
| 23 |
#define DAVIX_CORE_CONTENT_PROVIDER_HPP |
| 24 |
|
| 25 |
#include <request/httprequest.hpp> |
| 26 |
#include "stdlib.h" |
| 27 |
#include <string> |
| 28 |
|
| 29 |
namespace Davix { |
| 30 |
|
| 31 |
//------------------------------------------------------------------------------ |
| 32 |
// Abstract ContentProvider interface to provide the raw bytes for HTTP body |
| 33 |
// content. |
| 34 |
// |
| 35 |
// Core methods: |
| 36 |
// - pullBytes for reading through the contents. |
| 37 |
// - rewind for starting back from the beginning. |
| 38 |
// - getSize for getting the total size. |
| 39 |
//------------------------------------------------------------------------------ |
| 40 |
class ContentProvider { |
| 41 |
public: |
| 42 |
//---------------------------------------------------------------------------- |
| 43 |
// Empty constructor |
| 44 |
//---------------------------------------------------------------------------- |
| 45 |
ContentProvider(); |
| 46 |
|
| 47 |
//---------------------------------------------------------------------------- |
| 48 |
// Virtual destructor |
| 49 |
//---------------------------------------------------------------------------- |
| 50 |
virtual ~ContentProvider() {} |
| 51 |
|
| 52 |
//---------------------------------------------------------------------------- |
| 53 |
// Pull the specified number of bytes, write them onto the given buffer. |
| 54 |
// |
| 55 |
// Return value: |
| 56 |
// - Positive: The specified number of bytes were written onto the buffer. |
| 57 |
// |
| 58 |
// - Zero: We've reached EOF, no more bytes for you. |
| 59 |
// |
| 60 |
// - Negative: An unexpected error happened, and we are unable to continue. |
| 61 |
// The returned value is the negative of the errno. Call errc() and |
| 62 |
// errMsg() for more information about the issue. |
| 63 |
//---------------------------------------------------------------------------- |
| 64 |
virtual ssize_t pullBytes(char* target, size_t requestedBytes) = 0; |
| 65 |
|
| 66 |
//---------------------------------------------------------------------------- |
| 67 |
// Rewind the provider to the beginning. We're probably doing a redirect or |
| 68 |
// something, and need to access the contents from the beginning once again. |
| 69 |
// |
| 70 |
// Return false if, for some reason, this is not possible. The request will |
| 71 |
// fail. |
| 72 |
//---------------------------------------------------------------------------- |
| 73 |
virtual bool rewind() = 0; |
| 74 |
|
| 75 |
//---------------------------------------------------------------------------- |
| 76 |
// Get total size - should return a constant throughout the lifetime of this |
| 77 |
// object. |
| 78 |
// |
| 79 |
// Return -1 if size is not known beforehand. davix does not currently |
| 80 |
// support this option with libneon, as we don't have chunked encoding |
| 81 |
// for uploads yet. |
| 82 |
//---------------------------------------------------------------------------- |
| 83 |
virtual ssize_t getSize() = 0; |
| 84 |
|
| 85 |
//---------------------------------------------------------------------------- |
| 86 |
// Is the object ok? |
| 87 |
//---------------------------------------------------------------------------- |
| 88 |
bool ok() const; |
| 89 |
|
| 90 |
//---------------------------------------------------------------------------- |
| 91 |
// Has there been an error? |
| 92 |
//---------------------------------------------------------------------------- |
| 93 |
int getErrc() const; |
| 94 |
|
| 95 |
//---------------------------------------------------------------------------- |
| 96 |
// Get error message |
| 97 |
//---------------------------------------------------------------------------- |
| 98 |
std::string getError() const; |
| 99 |
|
| 100 |
protected: |
| 101 |
int _errc; |
| 102 |
std::string _errMsg; |
| 103 |
}; |
| 104 |
|
| 105 |
//------------------------------------------------------------------------------ |
| 106 |
// Content provider based on a simple buffer. No buffer ownership. |
| 107 |
//------------------------------------------------------------------------------ |
| 108 |
class BufferContentProvider : public ContentProvider { |
| 109 |
public: |
| 110 |
//---------------------------------------------------------------------------- |
| 111 |
// Constructor |
| 112 |
//---------------------------------------------------------------------------- |
| 113 |
BufferContentProvider(const char* buf, size_t count); |
| 114 |
|
| 115 |
//---------------------------------------------------------------------------- |
| 116 |
// pullBytes implementation. |
| 117 |
//---------------------------------------------------------------------------- |
| 118 |
ssize_t pullBytes(char* target, size_t requestedBytes); |
| 119 |
|
| 120 |
//---------------------------------------------------------------------------- |
| 121 |
// Rewind implementation. |
| 122 |
//---------------------------------------------------------------------------- |
| 123 |
bool rewind(); |
| 124 |
|
| 125 |
//---------------------------------------------------------------------------- |
| 126 |
// getSize implementation. |
| 127 |
//---------------------------------------------------------------------------- |
| 128 |
ssize_t getSize(); |
| 129 |
|
| 130 |
private: |
| 131 |
const char* _buffer; |
| 132 |
size_t _count; |
| 133 |
size_t _pos; |
| 134 |
}; |
| 135 |
|
| 136 |
//------------------------------------------------------------------------------ |
| 137 |
// Content provider based on an owned internal buffer - contents are copied |
| 138 |
// on construction. |
| 139 |
//------------------------------------------------------------------------------ |
| 140 |
class OwnedBufferContentProvider : public ContentProvider { |
| 141 |
public: |
| 142 |
//---------------------------------------------------------------------------- |
| 143 |
// Constructor |
| 144 |
//---------------------------------------------------------------------------- |
| 145 |
OwnedBufferContentProvider(const char* buf, size_t count); |
| 146 |
|
| 147 |
//---------------------------------------------------------------------------- |
| 148 |
// Constructor |
| 149 |
//---------------------------------------------------------------------------- |
| 150 |
OwnedBufferContentProvider(const std::string &str); |
| 151 |
|
| 152 |
//---------------------------------------------------------------------------- |
| 153 |
// pullBytes implementation. |
| 154 |
//---------------------------------------------------------------------------- |
| 155 |
ssize_t pullBytes(char* target, size_t requestedBytes); |
| 156 |
|
| 157 |
//---------------------------------------------------------------------------- |
| 158 |
// Rewind implementation. |
| 159 |
//---------------------------------------------------------------------------- |
| 160 |
bool rewind(); |
| 161 |
|
| 162 |
//---------------------------------------------------------------------------- |
| 163 |
// getSize implementation. |
| 164 |
//---------------------------------------------------------------------------- |
| 165 |
ssize_t getSize(); |
| 166 |
|
| 167 |
private: |
| 168 |
std::string _contents; |
| 169 |
BufferContentProvider _provider; |
| 170 |
}; |
| 171 |
|
| 172 |
//------------------------------------------------------------------------------ |
| 173 |
// Content provider based on a file descriptor - no ownership on underlying |
| 174 |
// file descriptor, keep open while this object is alive. |
| 175 |
//------------------------------------------------------------------------------ |
| 176 |
class FdContentProvider : public ContentProvider { |
| 177 |
public: |
| 178 |
//---------------------------------------------------------------------------- |
| 179 |
// Constructor. Start from the given offset, read a maximum of maxLen further |
| 180 |
// bytes. With maxLen = 0, read the entire rest of the fd contents. |
| 181 |
//---------------------------------------------------------------------------- |
| 182 |
FdContentProvider(int fd, off_t offset = 0, size_t maxLen = 0); |
| 183 |
|
| 184 |
//---------------------------------------------------------------------------- |
| 185 |
// pullBytes implementation. |
| 186 |
//---------------------------------------------------------------------------- |
| 187 |
ssize_t pullBytes(char* target, size_t requestedBytes); |
| 188 |
|
| 189 |
//---------------------------------------------------------------------------- |
| 190 |
// Rewind implementation. |
| 191 |
//---------------------------------------------------------------------------- |
| 192 |
bool rewind(); |
| 193 |
|
| 194 |
//---------------------------------------------------------------------------- |
| 195 |
// getSize implementation. |
| 196 |
//---------------------------------------------------------------------------- |
| 197 |
ssize_t getSize(); |
| 198 |
|
| 199 |
private: |
| 200 |
int _fd; |
| 201 |
ssize_t _fd_size; |
| 202 |
off_t _offset; |
| 203 |
size_t _target_len; |
| 204 |
bool eof; |
| 205 |
size_t _bytes_provided; |
| 206 |
}; |
| 207 |
|
| 208 |
//------------------------------------------------------------------------------ |
| 209 |
// Content provider based on a HttpBodyProvider callback. |
| 210 |
//------------------------------------------------------------------------------ |
| 211 |
class CallbackContentProvider : public ContentProvider { |
| 212 |
public: |
| 213 |
//---------------------------------------------------------------------------- |
| 214 |
// Constructor |
| 215 |
//---------------------------------------------------------------------------- |
| 216 |
CallbackContentProvider(HttpBodyProvider provider, dav_size_t len, |
| 217 |
void *udata); |
| 218 |
|
| 219 |
//---------------------------------------------------------------------------- |
| 220 |
// Constructor |
| 221 |
//---------------------------------------------------------------------------- |
| 222 |
CallbackContentProvider(DataProviderFun provider, dav_size_t len); |
| 223 |
|
| 224 |
//---------------------------------------------------------------------------- |
| 225 |
// pullBytes implementation. |
| 226 |
//---------------------------------------------------------------------------- |
| 227 |
ssize_t pullBytes(char* target, size_t requestedBytes); |
| 228 |
|
| 229 |
//---------------------------------------------------------------------------- |
| 230 |
// Rewind implementation. |
| 231 |
//---------------------------------------------------------------------------- |
| 232 |
bool rewind(); |
| 233 |
|
| 234 |
//---------------------------------------------------------------------------- |
| 235 |
// getSize implementation. |
| 236 |
//---------------------------------------------------------------------------- |
| 237 |
ssize_t getSize(); |
| 238 |
|
| 239 |
|
| 240 |
private: |
| 241 |
HttpBodyProvider _provider; |
| 242 |
DataProviderFun _providerFun; |
| 243 |
|
| 244 |
dav_size_t _len; |
| 245 |
void *_udata; |
| 246 |
}; |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
#endif |
| 251 |
|