1 /*
2  * HTTPConnection.hpp
3  *****************************************************************************
4  * Copyright (C) 2010 - 2011 Klagenfurt University
5  *               2014 - 2015 VideoLAN and VLC Authors
6  *
7  * Created on: Aug 10, 2010
8  * Authors: Christopher Mueller <christopher.mueller@itec.uni-klu.ac.at>
9  *          Christian Timmerer  <christian.timmerer@itec.uni-klu.ac.at>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published
13  * by the Free Software Foundation; either version 2.1 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25 #ifndef HTTPCONNECTION_H_
26 #define HTTPCONNECTION_H_
27 
28 #include "ConnectionParams.hpp"
29 #include "BytesRange.hpp"
30 #include <vlc_common.h>
31 #include <string>
32 
33 namespace adaptive
34 {
35     namespace http
36     {
37         class Transport;
38         class AuthStorage;
39 
40         class AbstractConnection
41         {
42             public:
43                 AbstractConnection(vlc_object_t *);
44                 virtual ~AbstractConnection();
45 
46                 virtual bool    prepare     (const ConnectionParams &);
47                 virtual bool    canReuse     (const ConnectionParams &) const = 0;
48 
49                 virtual enum RequestStatus
50                                 request     (const std::string& path, const BytesRange & = BytesRange()) = 0;
51                 virtual ssize_t read        (void *p_buffer, size_t len) = 0;
52 
53                 virtual size_t  getContentLength() const;
54                 virtual const std::string & getContentType() const;
55                 virtual void    setUsed( bool ) = 0;
56 
57             protected:
58                 vlc_object_t      *p_object;
59                 ConnectionParams   params;
60                 bool               available;
61                 size_t             contentLength;
62                 std::string        contentType;
63                 BytesRange         bytesRange;
64                 size_t             bytesRead;
65         };
66 
67         class HTTPConnection : public AbstractConnection
68         {
69             public:
70                 HTTPConnection(vlc_object_t *, AuthStorage *,  Transport *,
71                                const ConnectionParams &, bool = false);
72                 virtual ~HTTPConnection();
73 
74                 virtual bool    canReuse     (const ConnectionParams &) const;
75                 virtual enum RequestStatus
76                                 request     (const std::string& path, const BytesRange & = BytesRange());
77                 virtual ssize_t read        (void *p_buffer, size_t len);
78 
79                 void setUsed( bool );
80                 const ConnectionParams &getRedirection() const;
81                 static const unsigned MAX_REDIRECTS = 3;
82 
83             protected:
84                 virtual bool    connected   () const;
85                 virtual bool    connect     ();
86                 virtual void    disconnect  ();
87                 virtual bool    send        (const void *buf, size_t size);
88                 virtual bool    send        (const std::string &data);
89 
90                 virtual void    onHeader    (const std::string &line,
91                                              const std::string &value);
92                 virtual std::string extraRequestHeaders() const;
93                 virtual std::string buildRequestHeader(const std::string &path) const;
94 
95                 ssize_t         readChunk   (void *p_buffer, size_t len);
96                 enum RequestStatus parseReply();
97                 std::string readLine();
98                 std::string useragent;
99 
100                 AuthStorage        *authStorage;
101                 ConnectionParams    locationparams;
102                 ConnectionParams    proxyparams;
103                 bool                connectionClose;
104                 bool                chunked;
105                 bool                chunked_eof;
106                 size_t              chunkLength;
107                 bool                queryOk;
108                 int                 retries;
109                 static const int    retryCount = 5;
110 
111             private:
112                 Transport *transport;
113        };
114 
115        class StreamUrlConnection : public AbstractConnection
116        {
117             public:
118                 StreamUrlConnection(vlc_object_t *);
119                 virtual ~StreamUrlConnection();
120 
121                 virtual bool    canReuse     (const ConnectionParams &) const;
122 
123                 virtual enum RequestStatus
124                                 request     (const std::string& path, const BytesRange & = BytesRange());
125                 virtual ssize_t read        (void *p_buffer, size_t len);
126 
127                 virtual void    setUsed( bool );
128 
129             protected:
130                 void reset();
131                 stream_t *p_streamurl;
132        };
133 
134        class AbstractConnectionFactory
135        {
136            public:
AbstractConnectionFactory()137                AbstractConnectionFactory() {}
~AbstractConnectionFactory()138                virtual ~AbstractConnectionFactory() {}
139                virtual AbstractConnection * createConnection(vlc_object_t *, const ConnectionParams &) = 0;
140        };
141 
142        class NativeConnectionFactory : public AbstractConnectionFactory
143        {
144            public:
145                NativeConnectionFactory( AuthStorage * );
146                virtual ~NativeConnectionFactory();
147                virtual AbstractConnection * createConnection(vlc_object_t *, const ConnectionParams &);
148            private:
149                AuthStorage *authStorage;
150        };
151 
152        class StreamUrlConnectionFactory : public AbstractConnectionFactory
153        {
154            public:
155                StreamUrlConnectionFactory();
~StreamUrlConnectionFactory()156                virtual ~StreamUrlConnectionFactory() {}
157                virtual AbstractConnection * createConnection(vlc_object_t *, const ConnectionParams &);
158        };
159 
160        class ConnectionFactory : public AbstractConnectionFactory
161        {
162            public:
163                ConnectionFactory( AuthStorage * );
164                virtual ~ConnectionFactory();
165                virtual AbstractConnection * createConnection(vlc_object_t *, const ConnectionParams &);
166            private:
167                NativeConnectionFactory *native;
168                StreamUrlConnectionFactory *streamurl;
169        };
170     }
171 }
172 
173 #endif /* HTTPCONNECTION_H_ */
174