|
a/src/mediaserver/cdplugins/curlfetch.h |
|
b/src/mediaserver/cdplugins/curlfetch.h |
|
|
1 |
/* Copyright (C) 2017-2018 J.F.Dockes
|
|
|
2 |
* This program is free software; you can redistribute it and/or modify
|
|
|
3 |
* it under the terms of the GNU General Public License as published by
|
|
|
4 |
* the Free Software Foundation; either version 2 of the License, or
|
|
|
5 |
* (at your option) any later version.
|
|
|
6 |
*
|
|
|
7 |
* This program is distributed in the hope that it will be useful,
|
|
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
10 |
* GNU General Public License for more details.
|
|
|
11 |
*
|
|
|
12 |
* You should have received a copy of the GNU General Public License
|
|
|
13 |
* along with this program; if not, write to the
|
|
|
14 |
* Free Software Foundation, Inc.,
|
|
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
16 |
*/
|
1 |
#ifndef _CURLFETCH_H_INCLUDED_
|
17 |
#ifndef _CURLFETCH_H_INCLUDED_
|
2 |
#define _CURLFETCH_H_INCLUDED_
|
18 |
#define _CURLFETCH_H_INCLUDED_
|
3 |
|
19 |
|
4 |
#include <stddef.h>
|
20 |
#include <stddef.h>
|
5 |
#include <stdint.h>
|
21 |
#include <stdint.h>
|
|
... |
|
... |
8 |
#include <functional>
|
24 |
#include <functional>
|
9 |
#include <memory>
|
25 |
#include <memory>
|
10 |
|
26 |
|
11 |
#include "bufxchange.h"
|
27 |
#include "bufxchange.h"
|
12 |
#include "abuffer.h"
|
28 |
#include "abuffer.h"
|
|
|
29 |
#include "netfetch.h"
|
13 |
|
30 |
|
14 |
//
|
31 |
//
|
15 |
// Wrapper for a libcurl transfer. This uses the curl_easy interface
|
32 |
// Wrapper for a libcurl transfer. This uses the curl_easy interface
|
16 |
// in a separate thread, so any number of transfers may be performed
|
33 |
// in a separate thread, so any number of transfers may be performed
|
17 |
// in parallel.
|
34 |
// in parallel.
|
|
... |
|
... |
22 |
// current waiting).
|
39 |
// current waiting).
|
23 |
//
|
40 |
//
|
24 |
// The end of transfer is signalled by pushing an empty buffer on the queue
|
41 |
// The end of transfer is signalled by pushing an empty buffer on the queue
|
25 |
//
|
42 |
//
|
26 |
// All methods are supposedly thread-safe
|
43 |
// All methods are supposedly thread-safe
|
27 |
class CurlFetch {
|
44 |
class CurlFetch : public NetFetch {
|
28 |
public:
|
45 |
public:
|
29 |
CurlFetch(const std::string& url);
|
46 |
CurlFetch(const std::string& url);
|
30 |
~CurlFetch();
|
47 |
~CurlFetch();
|
31 |
|
48 |
|
32 |
const std::string& url();
|
|
|
33 |
|
|
|
34 |
void setTimeout(int secs);
|
|
|
35 |
|
|
|
36 |
/// Start the transfer to the output queue.
|
49 |
/// Start the transfer to the output queue.
|
37 |
bool start(BufXChange<ABuffer*> *queue, uint64_t offset = 0);
|
50 |
bool start(BufXChange<ABuffer*> *queue, uint64_t offset = 0) override;
|
38 |
|
51 |
|
39 |
// Wait for HTTP headers. This allows, e.g. doing stuff depending
|
52 |
// Wait for HTTP headers. This allows, e.g. doing stuff depending
|
40 |
// on content-type before proceeding with the actual data transfer
|
53 |
// on content-type before proceeding with the actual data transfer
|
41 |
bool waitForHeaders(int maxSecs = 0);
|
54 |
bool waitForHeaders(int maxSecs = 0) override;
|
42 |
// Retrieve header value (after a successful waitForHeaders).
|
55 |
// Retrieve header value (after a successful waitForHeaders).
|
43 |
bool headerValue(const std::string& nm, std::string& val);
|
56 |
bool headerValue(const std::string& nm, std::string& val) override;
|
44 |
|
57 |
|
45 |
// Check if the curl thread is done and retrieve the results if it
|
58 |
// Check if the curl thread is done and retrieve the results if it
|
46 |
// is. This does not wait, it returns false if the transfer is
|
59 |
// is. This does not wait, it returns false if the transfer is
|
47 |
// still running.
|
60 |
// still running.
|
48 |
bool curlDone(int *curlcode, int *http_code);
|
61 |
bool fetchDone(FetchStatus *code, int *http_code) override;
|
49 |
|
62 |
|
50 |
/// Reset after transfer done, for retrying for exemple.
|
63 |
/// Reset after transfer done, for retrying for exemple.
|
51 |
void reset();
|
64 |
bool reset() override;
|
52 |
|
65 |
|
53 |
// Callbacks
|
|
|
54 |
|
|
|
55 |
// A function to create the first buffer (typically for prepending
|
|
|
56 |
// a wav header to a raw pcm stream. If set this is called from
|
|
|
57 |
// the first curl write callback, before processing the curl data,
|
|
|
58 |
// so this happens at a point where the client may have had a look
|
|
|
59 |
// at the headers).
|
|
|
60 |
void setBuf1GenCB(std::function<bool(std::string& buf,void*,int)>);
|
|
|
61 |
// Called after curl_easy_perform returns
|
|
|
62 |
void setEOFetchCB(std::function<void(bool ok, u_int64_t count)> eofcb);
|
|
|
63 |
// Called every time we get new data from curl
|
|
|
64 |
void setFetchBytesCB(std::function<void(u_int64_t count)> fbcb);
|
|
|
65 |
|
|
|
66 |
class Internal;
|
66 |
class Internal;
|
67 |
private:
|
67 |
private:
|
68 |
std::unique_ptr<Internal> m;
|
68 |
std::unique_ptr<Internal> m;
|
69 |
};
|
69 |
};
|
70 |
|
70 |
|