MICO Platform  1.0.0
 All Classes Namespaces Functions Variables Friends
URLStream.hpp
1 #ifndef HAVE_URL_STREAM_H
2 #define HAVE_URL_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 URLType {
24  URL_TYPE_FILE = 1,
25  URL_TYPE_HTTP = 2,
26  URL_TYPE_FTP = 3
27  };
28 
29  enum URLMode {
30  URL_MODE_READ, URL_MODE_WRITE
31  };
32 
33 
41  class URLStreambufBase : public std::streambuf
42  {
43 
44  // cURL callbacks; declared here so we can friend them
45  static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *device);
46  static size_t write_callback(void *ptr, size_t size, size_t nmemm, void *device);
47 
48  protected:
49 
50  typedef union {
51  CURL *curl;
52  FILE *file;
53  } handle_t;
54 
55  URLType type;
56  URLMode mode;
57 
58  handle_t handle;
59 
60  CURLM *multi_handle;
61 
62  int running_handles;
63 
64  char* buffer;
67 
68  bool finishing;
69  bool waiting;
70 
71 
72  void loop();
73 
74 
75 
76  public:
77 
85  URLStreambufBase(const char* url, URLMode mode, int bufsize);
86 
87 
91  virtual ~URLStreambufBase();
92 
93 
94  private:
95 
99  URLStreambufBase(const URLStreambufBase& other);
100 
101 
105  URLStreambufBase& operator=(URLStreambufBase other);
106 
107  };
108 
109 
114 
115  public:
116 
117  URLIStreambuf(const char* url) : URLStreambufBase(url, URL_MODE_READ, CURL_MAX_WRITE_SIZE) {};
118 
119 
120  private:
121 
125  int underflow();
126 
130  URLIStreambuf(const URLIStreambuf& other);
131 
132 
136  URLIStreambuf& operator=(URLIStreambuf other);
137 
138  };
139 
140 
145 
146  public:
147 
148  URLOStreambuf(const char* url) : URLStreambufBase(url, URL_MODE_WRITE, CURL_MAX_WRITE_SIZE) {};
149 
150  private:
151 
155  int overflow(int c);
156 
160  int sync();
161 
165  URLOStreambuf(const URLOStreambuf& other);
166 
167 
171  URLOStreambuf& operator=(URLOStreambuf other);
172 
173  };
174 
179  class url_ostream : public std::ostream {
180  public:
181  url_ostream(const char* url) : std::ostream(new URLOStreambuf(url)) {};
182 
183  url_ostream(std::string url) : std::ostream(new URLOStreambuf(url.c_str())) {};
184 
185  ~url_ostream() { rdbuf()->pubsync(); delete rdbuf(); };
186  };
187 
188 
193  class url_istream : public std::istream {
194  public:
195  url_istream(const char* url) : std::istream(new URLIStreambuf(url)) {};
196 
197  url_istream(std::string url) : std::istream(new URLIStreambuf(url.c_str())) {};
198 
199  ~url_istream() { delete rdbuf(); };
200  };
201 
202 
203  // TODO: deleting of content parts binary data from disk/FTP
204 
205  }
206 }
207 
208 #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
Base class for Boost Device implementations allowing to access local (file://) and remote (ftp://...
Definition: URLStream.hpp:41
int buffer_size
allocated buffer size
Definition: URLStream.hpp:65
char * buffer
internal buffer for storing data from last read
Definition: URLStream.hpp:64
A Boost Device implementation allowing read access to local and remote URLs.
Definition: URLStream.hpp:113
bool waiting
indicate if the device is still waiting for more data
Definition: URLStream.hpp:69
void loop()
loop to fetch/send more data
Definition: URLStream.cpp:272
Main type for opening an output stream to an URL for writing.
Definition: URLStream.hpp:179
A Boost Device implementation allowing write access to local and remote URLs.
Definition: URLStream.hpp:144
virtual ~URLStreambufBase()
Clean up resources occupied by device, e.g.
Definition: URLStream.cpp:247
char * buffer_position
cURL position in buffer (when sending/receiving data)
Definition: URLStream.hpp:66
bool finishing
indicate if device is already finishing transfer (i.e. no more reads/writes)
Definition: URLStream.hpp:68
URLStreambufBase(const char *url, URLMode mode, int bufsize)
Open URL device using the given URL and flags.
Definition: URLStream.cpp:163
Definition: URLStream.hpp:50
Main type for opening an input stream to an URL for reading.
Definition: URLStream.hpp:193