VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvhttpserverpeer.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gvhttpserverpeer.h
19 ///
20 
21 #ifndef GV_HTTPSERVERPEER__H
22 #define GV_HTTPSERVERPEER__H
23 
24 #include "gdef.h"
25 #include "gvhttpserver.h"
26 #include "gvimageinput.h"
27 #include "grimagedata.h"
28 #include "gbufferedserverpeer.h"
29 #include "gurl.h"
30 #include <string>
31 #include <memory>
32 #include <vector>
33 #include <utility>
34 
35 namespace Gv
36 {
37  class HttpServerPeer ;
38 }
39 
40 /// \class Gv::HttpServerPeer
41 /// A GNet::ServerPeer class for HTTP servers that serves up image streams. The
42 /// URL sent in the peer's GET request is used to control the image format etc.
43 ///
45 {
46 public:
47  typedef HttpServerConfig Config ;
48  typedef HttpServerSource Source ;
49  typedef HttpServerSources Sources ;
51 
52  HttpServerPeer( GNet::Server::PeerInfo , Sources & , const Resources & , const Config & ) ;
53  ///< Constructor.
54 
55  virtual ~HttpServerPeer() ;
56  ///< Destructor.
57 
58 private:
59  virtual void onDelete( const std::string & ) override ;
60  virtual void onSendComplete() override ;
61  virtual bool onReceive( const std::string & ) override ;
62  virtual void onSecure( const std::string & ) override ;
63  virtual void onImageInput( ImageInputSource & , Gr::Image ) override ; // Gv::ImageInputHandler
64  virtual void onNonImageInput( ImageInputSource & , Gr::Image , const std::string & ) override ; // Gv::ImageInputHandler
65  virtual ImageInputConversion imageInputConversion( ImageInputSource & ) override ; // Gv::ImageInputHandler
66 
67 private:
68  struct Pdu /// Holds data as a head() string followed by a body shared-ptr, accessible as segments.
69  {
70  typedef std::pair<const char*,size_t> Segment ;
71  typedef std::vector<Segment> Segments ;
72  Pdu() ;
73  explicit Pdu( const std::string & ) ;
74  void clear() ;
75  bool empty() const ;
76  size_t size() const ;
77  void append( const std::string & ) ; // appends to head()
78  void assignBody( shared_ptr<const Gr::ImageBuffer> , size_t ) ;
79  void operator=( const std::string & ) ;
80  const Segments & segments() const ;
81  std::string head() const ;
82  void lock() const ;
83  void release() ;
84  //
85  std::string m_head ;
86  shared_ptr<const Gr::ImageBuffer> m_body_ptr ;
87  size_t m_body_ptr_size ;
88  mutable Segments m_segments ;
89  mutable bool m_locked ;
90  } ;
91 
92 private:
93  HttpServerPeer( const HttpServerPeer & ) ;
94  void operator=( const HttpServerPeer & ) ;
95  void selectSource( const std::string & path ) ;
96  void onIdleTimeout() ;
97  void onDataTimeout() ;
98  void buildPduFirst() ;
99  void buildPdu() ;
100  void buildPduSingle() ;
101  void buildPduFromFile() ;
102  void buildPduFromStatus() ;
103  bool sendPdu() ;
104  std::string errorResponse( int e , const std::string & s , const std::string & header = std::string() ) const ;
105  void doSendResponse( int e , const std::string & s , const std::string & header = std::string() ) ;
106  bool doSend( const std::string & ) ;
107  bool doSend( const Pdu & ) ;
108  void doSendLogging( const Pdu & ) const ;
109  void doInput( Gr::Image , const std::string & ) ;
110  void startStreamingTimer() ;
111  static std::string toPdu( std::string & , shared_ptr<char> , const std::string & , G::Url ) ;
112  std::string pnmHeader( Gr::ImageType type ) const ;
113  std::string pnmType( Gr::ImageType type ) const ;
114  std::string fileHeader( size_t content_length , const std::string & type ) const ;
115  std::string simpleHeader( size_t content_length , Gr::ImageType , const std::string & , const std::string & , const std::string & ) const ;
116  std::string streamingHeader( size_t content_length , Gr::ImageType , const std::string & ) const ;
117  std::string streamingSubHeader( size_t , Gr::ImageType , const std::string & ) const ;
118  void doPost() ;
119  void doGatewayMessage( const std::string & ) ;
120  std::string doGatewayMessageImp( const std::string & ) ;
121  bool fileSend() ;
122  bool fileRequest() ;
123  bool fileEnd() ;
124  bool specialRequest() ;
125  static unsigned int headerValue( const std::string & ) ;
126  Gr::Image textToJpeg( const Gr::ImageBuffer & ) ;
127 
128 private:
129  Sources & m_sources ;
130  Source m_source ;
131  const Resources & m_resources ;
132  Config m_config_default ;
133  Config m_config ;
134  G::Url m_url ;
135  Pdu m_pdu ;
136  GNet::Timer<HttpServerPeer> m_idle_timer ;
137  GNet::Timer<HttpServerPeer> m_data_timer ;
138  Gr::Image m_image ;
139  size_t m_image_size ;
140  Gr::ImageType m_image_type ;
141  std::string m_image_type_str ;
142  unsigned int m_sending ;
143  unsigned int m_image_number ;
144  enum {
145  s_init , // waiting for first get request
146  s_idle , // sent single image or an error response, waiting for next get request
147  s_got_get , // got 'get' line, waiting for end-of-headers
148  s_got_get_headers , // got 'get' headers, waiting for request body
149  s_got_post , // got 'post' line, waiting for end-of-headers
150  s_got_post_headers , // got 'post' headers, waiting for request body
151  s_waiting , // got request but no image, waiting for first image
152  s_single_idle , // ready to send single-image pdu, waiting for timeout
153  s_single_busy , // sending single-image pdu, waiting for send-complete
154  s_streaming_first_idle , // ready to sending first pdu, waiting for timeout
155  s_streaming_first_busy , // sending first pdu, waiting for send-complete
156  s_streaming_idle , // waiting for next image
157  s_streaming_busy , // sending pdu, waiting for send-complete
158  s_file , // sending file
159  s_file_busy // sending file, waiting for send-complete
160  } ;
161  int m_state ;
162  unsigned int m_content_length ;
163  Gr::Image m_text_raw_image ;
164  Gr::Image m_text_jpeg_image ;
165 } ;
166 
167 #endif
A configuration structure for Gv::HttpServerPeer holding default settings from the command-line which...
Definition: gvhttpserver.h:275
HttpServerPeer(GNet::Server::PeerInfo, Sources &, const Resources &, const Config &)
Constructor.
An encapsulation of image type, including width, height and number of channels, with support for a st...
Definition: grimagetype.h:43
Vectors ImageBuffer
An ImageBuffer is used to hold raw image data, typically in more than one chunk.
Definition: grimagebuffer.h:47
A callback interface for Gv::ImageInputSource.
Definition: gvimageinput.h:84
A simple parser for URLs.
Definition: gurl.h:42
A class holding shared read-only image data (Gr::ImageBuffer) and its associated image type (Gr::Imag...
Definition: grimage.h:41
A base class for distributing incoming images to multiple client objects, supporting some simple imag...
Definition: gvimageinput.h:116
Used by a ImageInputHandler-derived object to hold a ImageInputSource pointer.
Definition: gvhttpserver.h:79
A GNet::ServerPeer class for HTTP servers that serves up image streams.
virtual ~HttpServerPeer()
Destructor.
A container for ImageInputSource pointers, used by Gv::HttpServerPeer.
Definition: gvhttpserver.h:122
A configuration structure for resources that Gv::HttpServerPeer makes available.
Definition: gvhttpserver.h:169
A structure that describes the preferred image type for the Gv::ImageInputHandler callback...
Definition: gvimageinput.h:47
A timer class template in which the timeout is delivered to the specified method. ...
Definition: gtimer.h:110
A ServerPeer that does line-buffering on input.
A structure used in GNet::Server::newPeer().
Definition: gserver.h:86