changed: Add logic to properly handle subtitles for stacked files
[vuplus_xbmc] / xbmc / filesystem / CurlFile.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2013 Team XBMC
4  *      http://xbmc.org
5  *
6  *  This Program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This Program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with XBMC; see the file COPYING.  If not, see
18  *  <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include "IFile.h"
23 #include "utils/RingBuffer.h"
24 #include <map>
25 #include "utils/HttpHeader.h"
26
27 namespace XCURL
28 {
29   typedef void CURL_HANDLE;
30   typedef void CURLM;
31   struct curl_slist;
32 }
33
34 namespace XFILE
35 {
36   class CCurlFile : public IFile
37   {
38     public:
39       typedef enum
40       {
41         PROXY_HTTP = 0,
42         PROXY_SOCKS4,
43         PROXY_SOCKS4A,
44         PROXY_SOCKS5,
45         PROXY_SOCKS5_REMOTE,
46       } ProxyType;
47     
48       CCurlFile();
49       virtual ~CCurlFile();
50       virtual bool Open(const CURL& url);
51       virtual bool OpenForWrite(const CURL& url, bool bOverWrite = false);
52       virtual bool Exists(const CURL& url);
53       virtual int64_t  Seek(int64_t iFilePosition, int iWhence=SEEK_SET);
54       virtual int64_t GetPosition();
55       virtual int64_t  GetLength();
56       virtual int  Stat(const CURL& url, struct __stat64* buffer);
57       virtual void Close();
58       virtual bool ReadString(char *szLine, int iLineLength)     { return m_state->ReadString(szLine, iLineLength); }
59       virtual unsigned int Read(void* lpBuf, int64_t uiBufSize)  { return m_state->Read(lpBuf, uiBufSize); }
60       virtual int Write(const void* lpBuf, int64_t uiBufSize);
61       virtual CStdString GetMimeType()                           { return m_state->m_httpheader.GetMimeType(); }
62       virtual CStdString GetContent()                            { return GetMimeType(); }
63       virtual int IoControl(EIoControl request, void* param);
64       virtual std::string GetContentCharset(void)                { return GetServerReportedCharset(); }
65
66       bool Post(const CStdString& strURL, const CStdString& strPostData, CStdString& strHTML);
67       bool Get(const CStdString& strURL, CStdString& strHTML);
68       bool ReadData(CStdString& strHTML);
69       bool Download(const CStdString& strURL, const CStdString& strFileName, LPDWORD pdwSize = NULL);
70       bool IsInternet(bool checkDNS = true);
71       void Cancel();
72       void Reset();
73       void SetUserAgent(CStdString sUserAgent)                   { m_userAgent = sUserAgent; }
74       void SetProxy(CStdString &proxy)                           { m_proxy = proxy; }
75       void SetProxyUserPass(CStdString &proxyuserpass)           { m_proxyuserpass = proxyuserpass; }
76       void SetProxyType(ProxyType proxytype)                     { m_proxytype = proxytype; }
77       void SetCustomRequest(CStdString &request)                 { m_customrequest = request; }
78       void UseOldHttpVersion(bool bUse)                          { m_useOldHttpVersion = bUse; }
79       void SetContentEncoding(CStdString encoding)               { m_contentencoding = encoding; }
80       void SetAcceptCharset(const std::string& charset)          { m_acceptCharset = charset; }
81       void SetTimeout(int connecttimeout)                        { m_connecttimeout = connecttimeout; }
82       void SetLowSpeedTime(int lowspeedtime)                     { m_lowspeedtime = lowspeedtime; }
83       void SetPostData(CStdString postdata)                      { m_postdata = postdata; }
84       void SetReferer(CStdString referer)                        { m_referer = referer; }
85       void SetCookie(CStdString cookie)                          { m_cookie = cookie; }
86       void SetMimeType(CStdString mimetype)                      { SetRequestHeader("Content-Type", mimetype); }
87       void SetRequestHeader(CStdString header, CStdString value);
88       void SetRequestHeader(CStdString header, long value);
89
90       void ClearRequestHeaders();
91       void SetBufferSize(unsigned int size);
92
93       const CHttpHeader& GetHttpHeader() { return m_state->m_httpheader; }
94       std::string GetServerReportedCharset(void);
95
96       /* static function that will get content type of a file */
97       static bool GetHttpHeader(const CURL &url, CHttpHeader &headers);
98       static bool GetMimeType(const CURL &url, CStdString &content, CStdString useragent="");
99
100       /* static function that will get cookies stored by CURL in RFC 2109 format */
101       static bool GetCookies(const CURL &url, std::string &cookies);
102
103       class CReadState
104       {
105       public:
106           CReadState();
107           ~CReadState();
108           XCURL::CURL_HANDLE*    m_easyHandle;
109           XCURL::CURLM*          m_multiHandle;
110
111           CRingBuffer     m_buffer;           // our ringhold buffer
112           unsigned int    m_bufferSize;
113
114           char *          m_overflowBuffer;   // in the rare case we would overflow the above buffer
115           unsigned int    m_overflowSize;     // size of the overflow buffer
116           int             m_stillRunning;     // Is background url fetch still in progress
117           bool            m_cancelled;
118           int64_t         m_fileSize;
119           int64_t         m_filePos;
120           bool            m_bFirstLoop;
121           bool            m_isPaused;
122           bool            m_sendRange;
123
124           char*           m_readBuffer;
125
126           /* returned http header */
127           CHttpHeader m_httpheader;
128           bool        IsHeaderDone(void)
129           { return m_httpheader.IsHeaderDone(); }
130
131           struct XCURL::curl_slist* m_curlHeaderList;
132           struct XCURL::curl_slist* m_curlAliasList;
133
134           size_t ReadCallback(char *buffer, size_t size, size_t nitems);
135           size_t WriteCallback(char *buffer, size_t size, size_t nitems);
136           size_t HeaderCallback(void *ptr, size_t size, size_t nmemb);
137
138           bool         Seek(int64_t pos);
139           unsigned int Read(void* lpBuf, int64_t uiBufSize);
140           bool         ReadString(char *szLine, int iLineLength);
141           bool         FillBuffer(unsigned int want);
142           void         SetReadBuffer(const void* lpBuf, int64_t uiBufSize);
143
144           void         SetResume(void);
145           long         Connect(unsigned int size);
146           void         Disconnect();
147       };
148
149     protected:
150       void ParseAndCorrectUrl(CURL &url);
151       void SetCommonOptions(CReadState* state);
152       void SetRequestHeaders(CReadState* state);
153       void SetCorrectHeaders(CReadState* state);
154       bool Service(const CStdString& strURL, CStdString& strHTML);
155
156     protected:
157       CReadState*     m_state;
158       CReadState*     m_oldState;
159       unsigned int    m_bufferSize;
160       int64_t         m_writeOffset;
161
162       CStdString      m_url;
163       CStdString      m_userAgent;
164       CStdString      m_proxy;
165       CStdString      m_proxyuserpass;
166       ProxyType       m_proxytype;
167       CStdString      m_customrequest;
168       CStdString      m_contentencoding;
169       std::string     m_acceptCharset;
170       CStdString      m_ftpauth;
171       CStdString      m_ftpport;
172       CStdString      m_binary;
173       CStdString      m_postdata;
174       CStdString      m_referer;
175       CStdString      m_cookie;
176       CStdString      m_username;
177       CStdString      m_password;
178       CStdString      m_httpauth;
179       bool            m_ftppasvip;
180       int             m_connecttimeout;
181       int             m_lowspeedtime;
182       bool            m_opened;
183       bool            m_forWrite;
184       bool            m_inError;
185       bool            m_useOldHttpVersion;
186       bool            m_seekable;
187       bool            m_multisession;
188       bool            m_skipshout;
189       bool            m_postdataset;
190
191       CRingBuffer     m_buffer;           // our ringhold buffer
192       char *          m_overflowBuffer;   // in the rare case we would overflow the above buffer
193       unsigned int    m_overflowSize;     // size of the overflow buffer
194
195       int             m_stillRunning;     // Is background url fetch still in progress?
196
197       typedef std::map<CStdString, CStdString> MAPHTTPHEADERS;
198       MAPHTTPHEADERS m_requestheaders;
199
200       long            m_httpresponse;
201   };
202 }