FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
SimpleStreamHelper.h
1 #pragma once
2 #ifndef SimpleStreamHelper_h__
3 #define SimpleStreamHelper_h__
4 
5 /**********************************************************\
6 Original Author: Richard Bateman
7 
8 Created: Jan 24, 2011
9 License: Dual license model; choose one of two:
10  New BSD License
11  http://www.opensource.org/licenses/bsd-license.php
12  - or -
13  GNU Lesser General Public License, version 2.1
14  http://www.gnu.org/licenses/lgpl-2.1.html
15 
16 Copyright 2011 Richard Bateman,
17  Firebreath development team
18 \**********************************************************/
19 
20 #include "DefaultBrowserStreamHandler.h"
21 #include "PluginEvents/StreamEvents.h"
22 #include "BrowserStream.h"
23 #include "URI.h"
24 #include <boost/shared_array.hpp>
25 #include <boost/tuple/tuple.hpp>
26 #include <string>
27 #include <list>
28 #include <map>
29 
30 namespace FB {
31  FB_FORWARD_PTR(BrowserHost);
32  FB_FORWARD_PTR(SimpleStreamHelper);
33 
34  typedef std::multimap<std::string, std::string> HeaderMap;
35  typedef boost::function<void (bool, const FB::HeaderMap&, const boost::shared_array<uint8_t>&, const size_t)> HttpCallback;
36 
48  struct HttpStreamResponse : boost::noncopyable
49  {
50  HttpStreamResponse(bool success, const FB::HeaderMap& headers,
51  const boost::shared_array<uint8_t>& data, const size_t size) : success(success),
52  headers(headers), data(data), size(size) { }
53  // True if the request succeeded
54  bool success;
55  // Multimap of headers
56  const FB::HeaderMap headers;
57  // shared_array containing the data returned by the server
58  const boost::shared_array<uint8_t> data;
59  // The size of data
60  const size_t size;
61  };
62  typedef boost::shared_ptr<HttpStreamResponse> HttpStreamResponsePtr;
63 
75  {
76  public:
93  static FB::SimpleStreamHelperPtr AsyncRequest( const BrowserHostConstPtr& host, const BrowserStreamRequest& req );
109  static FB::SimpleStreamHelperPtr AsyncRequest( const BrowserHostConstPtr& host, const BrowserStreamPtr& stream, const BrowserStreamRequest& req );
149  static FB::SimpleStreamHelperPtr AsyncGet(const FB::BrowserHostPtr& host, const FB::URI& uri, const HttpCallback& callback,
150  bool cache = true, size_t bufferSize = 128*1024);
151 
152 
193  static FB::SimpleStreamHelperPtr AsyncPost(const FB::BrowserHostPtr& host, const FB::URI& uri, const std::string& postdata, const HttpCallback& callback,
194  bool cache = true, size_t bufferSize = 128*1024);
195 
196 
229  static HttpStreamResponsePtr SynchronousGet(const FB::BrowserHostPtr& host, const FB::URI& uri,
230  const bool cache = true, const size_t bufferSize = 128*1024);
231 
244  static HttpStreamResponsePtr SynchronousRequest(const FB::BrowserHostPtr& host, const BrowserStreamRequest& req );
245 
279  static HttpStreamResponsePtr SynchronousPost(const FB::BrowserHostPtr& host, const FB::URI& uri, const std::string& postdata, const bool cache = true, const size_t bufferSize = 128*1024);
280 
281 
282  public:
283  SimpleStreamHelper( const HttpCallback& callback, const size_t blockSize = 128*1024 );
284 
285  virtual bool onStreamDataArrived(FB::StreamDataArrivedEvent *evt, FB::BrowserStream *);
286  virtual bool onStreamOpened(FB::StreamOpenedEvent *evt, FB::BrowserStream *);
288  static FB::HeaderMap parse_http_headers(const std::string& headers );
289 
290  protected:
291  typedef std::list<boost::shared_array<uint8_t> > BlockList;
292  BlockList blocks;
293  boost::shared_array<uint8_t> data;
294  const size_t blockSize;
295  size_t received;
296  HttpCallback callback;
297 
298  private:
299  void keepReference(const SimpleStreamHelperPtr& ptr);
300  SimpleStreamHelperPtr self;
301  BrowserStreamPtr streamPtr;
302  };
303 };
304 #endif // SimpleStreamHelper_h__
305 
static FB::SimpleStreamHelperPtr AsyncRequest(const BrowserHostConstPtr &host, const BrowserStreamRequest &req)
Creates an asynchronous HTTP request from the provided BrowserStreamRequest.
Helper class for making simple HTTP requests.
Data structure to hold the response to an HTTP Get request.
This is the abstract base class (interface class) for a browser stream.
Definition: BrowserStream.h:41
static FB::SimpleStreamHelperPtr AsyncPost(const FB::BrowserHostPtr &host, const FB::URI &uri, const std::string &postdata, const HttpCallback &callback, bool cache=true, size_t bufferSize=128 *1024)
Starts an asynchronous HTTP post request.
Information about an HTTP request to be made.
virtual bool onStreamDataArrived(FB::StreamDataArrivedEvent *evt, FB::BrowserStream *)
Called when data arrives.
Data structure for dealing with URI strings.
Definition: URI.h:42
static HttpStreamResponsePtr SynchronousRequest(const FB::BrowserHostPtr &host, const BrowserStreamRequest &req)
Do not call from the main thread! Starts a Synchronous HTTP request.
Simple implementation of a stream event handler from which you can derive your own stream events hand...
BrowserStreamPtr stream
stream instance, don't use yourself
static FB::SimpleStreamHelperPtr AsyncGet(const FB::BrowserHostPtr &host, const FB::URI &uri, const HttpCallback &callback, bool cache=true, size_t bufferSize=128 *1024)
Starts an asynchronous HTTP get request.
boost::shared_ptr< FB::BrowserHost > BrowserHostPtr
Defines an alias representing a BrowserHost shared_ptr (you should never use a BrowserHost* directly)...
Definition: APITypes.h:117
static HttpStreamResponsePtr SynchronousGet(const FB::BrowserHostPtr &host, const FB::URI &uri, const bool cache=true, const size_t bufferSize=128 *1024)
Do not call from the main thread! Starts a Synchronous HTTP get request.
static HttpStreamResponsePtr SynchronousPost(const FB::BrowserHostPtr &host, const FB::URI &uri, const std::string &postdata, const bool cache=true, const size_t bufferSize=128 *1024)
Do not call from the main thread! Starts a Synchronous HTTP POST request.
virtual bool onStreamOpened(FB::StreamOpenedEvent *evt, FB::BrowserStream *)
Called when the stream was opened successfully.
This event is fired when a stream has completed downloading.
Definition: StreamEvents.h:148
virtual bool onStreamCompleted(FB::StreamCompletedEvent *evt, FB::BrowserStream *)
Called when the stream finished downloading successfully.