MICO Platform
 All Classes Namespaces Functions Variables Friends
WebStream.hpp
1 #ifndef HAVE_WEB_STREAM_H
2 #define HAVE_WEB_STREAM_H 1
3 
4 #include <cstdio>
5 #include <iosfwd>
6 #include <streambuf>
7 #include <istream>
8 #include <ostream>
9 
10 #include <fcntl.h>
11 #include <curl/curl.h>
12 
13 
14 
15 namespace mico
16 {
17  namespace io
18  {
19 
23  enum URLMode {
24  URL_MODE_READ, URL_MODE_WRITE
25  };
26 
27 
35  class WebStreambufBase : public std::streambuf
36  {
37 
38  // cURL callbacks; declared here so we can friend them
39  static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *device);
40  static size_t write_callback(void *ptr, size_t size, size_t nmemm, void *device);
41 
42  protected:
43 
44  typedef union {
45  CURL *curl;
46  FILE *file;
47  } handle_t;
48 
49  URLMode mode;
50 
51  handle_t handle;
52 
53  CURLM *multi_handle;
54 
55  int running_handles;
56 
57  char* buffer;
60 
61  bool finishing;
62  bool waiting;
63 
64 
65  void loop();
66 
67 
68 
69  public:
70 
77  WebStreambufBase(const char* url, URLMode mode, int bufsize);
78 
79 
83  virtual ~WebStreambufBase();
84 
85 
86  private:
87 
91  WebStreambufBase(const WebStreambufBase & other);
92 
93 
97  WebStreambufBase & operator=(WebStreambufBase other);
98 
99  };
100 
101 
106 
107  public:
108 
109  WebIStreambuf(const char* url) : WebStreambufBase(url, URL_MODE_READ, CURL_MAX_WRITE_SIZE) {};
110 
111 
112  private:
113 
117  std::streambuf::int_type underflow();
118 
122  WebIStreambuf(const WebIStreambuf & other);
123 
124 
128  WebIStreambuf & operator=(WebIStreambuf other);
129 
130  };
131 
132 
137 
138  public:
139 
140  WebOStreambuf(const char* url) : WebStreambufBase(url, URL_MODE_WRITE, CURL_MAX_WRITE_SIZE) {};
141 
142  private:
143 
147  std::streambuf::int_type overflow(std::streambuf::int_type c);
148 
152  int sync();
153 
157  WebOStreambuf(const WebOStreambuf & other);
158 
159 
163  WebOStreambuf & operator=(WebOStreambuf other);
164 
165  };
166 
171  class web_ostream : public std::ostream {
172  public:
173  web_ostream(const char* url) : std::ostream(new WebOStreambuf(url)) {};
174 
175  web_ostream(std::string url) : std::ostream(new WebOStreambuf(url.c_str())) {};
176 
177  ~web_ostream() { rdbuf()->pubsync(); delete rdbuf(); };
178  };
179 
180 
185  class web_istream : public std::istream {
186  public:
187  web_istream(const char* url) : std::istream(new WebIStreambuf(url)) {};
188 
189  web_istream(std::string url) : std::istream(new WebIStreambuf(url.c_str())) {};
190 
191  ~web_istream() { delete rdbuf(); };
192  };
193 
194  int removeFtpFile(const char* url);
195 
196  namespace {
197  static size_t write_callback(void *ptr, size_t size, size_t nmemb, void *_device);
198  }
199  }
200 }
201 
202 #endif
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in c...
Definition: http_client.cpp:23
void loop()
loop to fetch/send more data
Definition: WebStream.cpp:207
bool waiting
indicate if the device is still waiting for more data
Definition: WebStream.hpp:62
WebStreambufBase(const char *url, URLMode mode, int bufsize)
Open URL device using the given URL and flags.
Definition: WebStream.cpp:126
Base class for Boost Device implementations allowing to access remote (ftp://, http://, https://) URLs as stdio streams, similar to fstream.
Definition: WebStream.hpp:35
Main type for opening an output stream to an URL for writing.
Definition: WebStream.hpp:171
Definition: WebStream.hpp:44
bool finishing
indicate if device is already finishing transfer (i.e. no more reads/writes)
Definition: WebStream.hpp:61
Main type for opening an input stream to an URL for reading.
Definition: WebStream.hpp:185
char * buffer
internal buffer for storing data from last read
Definition: WebStream.hpp:57
A Boost Device implementation allowing read access.
Definition: WebStream.hpp:105
int buffer_size
allocated buffer size
Definition: WebStream.hpp:58
A Boost Device implementation allowing write access.
Definition: WebStream.hpp:136
char * buffer_position
cURL position in buffer (when sending/receiving data)
Definition: WebStream.hpp:59
virtual ~WebStreambufBase()
Clean up resources occupied by device, e.g.
Definition: WebStream.cpp:185