Merge pull request #5039 from CEikermann/patch-1
[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();
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 SetStreamProxy(const CStdString &proxy, ProxyType type);
78       void SetCustomRequest(CStdString &request)                 { m_customrequest = request; }
79       void UseOldHttpVersion(bool bUse)                          { m_useOldHttpVersion = bUse; }
80       void SetContentEncoding(CStdString encoding)               { m_contentencoding = encoding; }
81       void SetAcceptCharset(const std::string& charset)          { m_acceptCharset = charset; }
82       void SetTimeout(int connecttimeout)                        { m_connecttimeout = connecttimeout; }
83       void SetLowSpeedTime(int lowspeedtime)                     { m_lowspeedtime = lowspeedtime; }
84       void SetPostData(CStdString postdata)                      { m_postdata = postdata; }
85       void SetReferer(CStdString referer)                        { m_referer = referer; }
86       void SetCookie(CStdString cookie)                          { m_cookie = cookie; }
87       void SetMimeType(CStdString mimetype)                      { SetRequestHeader("Content-Type", mimetype); }
88       void SetRequestHeader(CStdString header, CStdString value);
89       void SetRequestHeader(CStdString header, long value);
90
91       void ClearRequestHeaders();
92       void SetBufferSize(unsigned int size);
93
94       const CHttpHeader& GetHttpHeader() { return m_state->m_httpheader; }
95       std::string GetServerReportedCharset(void);
96
97       /* static function that will get content type of a file */
98       static bool GetHttpHeader(const CURL &url, CHttpHeader &headers);
99       static bool GetMimeType(const CURL &url, CStdString &content, CStdString useragent="");
100
101       /* static function that will get cookies stored by CURL in RFC 2109 format */
102       static bool GetCookies(const CURL &url, std::string &cookies);
103
104       class CReadState
105       {
106       public:
107           CReadState();
108           ~CReadState();
109           XCURL::CURL_HANDLE*    m_easyHandle;
110           XCURL::CURLM*          m_multiHandle;
111
112           CRingBuffer     m_buffer;           // our ringhold buffer
113           unsigned int    m_bufferSize;
114
115           char *          m_overflowBuffer;   // in the rare case we would overflow the above buffer
116           unsigned int    m_overflowSize;     // size of the overflow buffer
117           int             m_stillRunning;     // Is background url fetch still in progress
118           bool            m_cancelled;
119           int64_t         m_fileSize;
120           int64_t         m_filePos;
121           bool            m_bFirstLoop;
122           bool            m_isPaused;
123           bool            m_sendRange;
124
125           char*           m_readBuffer;
126
127           /* returned http header */
128           CHttpHeader m_httpheader;
129           bool        IsHeaderDone(void)
130           { return m_httpheader.IsHeaderDone(); }
131
132           struct XCURL::curl_slist* m_curlHeaderList;
133           struct XCURL::curl_slist* m_curlAliasList;
134
135           size_t ReadCallback(char *buffer, size_t size, size_t nitems);
136           size_t WriteCallback(char *buffer, size_t size, size_t nitems);
137           size_t HeaderCallback(void *ptr, size_t size, size_t nmemb);
138
139           bool         Seek(int64_t pos);
140           unsigned int Read(void* lpBuf, int64_t uiBufSize);
141           bool         ReadString(char *szLine, int iLineLength);
142           bool         FillBuffer(unsigned int want);
143           void         SetReadBuffer(const void* lpBuf, int64_t uiBufSize);
144
145           void         SetResume(void);
146           long         Connect(unsigned int size);
147           void         Disconnect();
148       };
149
150     protected:
151       void ParseAndCorrectUrl(CURL &url);
152       void SetCommonOptions(CReadState* state);
153       void SetRequestHeaders(CReadState* state);
154       void SetCorrectHeaders(CReadState* state);
155       bool Service(const CStdString& strURL, CStdString& strHTML);
156
157     protected:
158       CReadState*     m_state;
159       CReadState*     m_oldState;
160       unsigned int    m_bufferSize;
161       int64_t         m_writeOffset;
162
163       CStdString      m_url;
164       CStdString      m_userAgent;
165       CStdString      m_proxy;
166       CStdString      m_proxyuserpass;
167       ProxyType       m_proxytype;
168       CStdString      m_customrequest;
169       CStdString      m_contentencoding;
170       std::string     m_acceptCharset;
171       CStdString      m_ftpauth;
172       CStdString      m_ftpport;
173       CStdString      m_binary;
174       CStdString      m_postdata;
175       CStdString      m_referer;
176       CStdString      m_cookie;
177       CStdString      m_username;
178       CStdString      m_password;
179       CStdString      m_httpauth;
180       CStdString      m_cipherlist;
181       bool            m_ftppasvip;
182       int             m_connecttimeout;
183       int             m_lowspeedtime;
184       bool            m_opened;
185       bool            m_forWrite;
186       bool            m_inError;
187       bool            m_useOldHttpVersion;
188       bool            m_seekable;
189       bool            m_multisession;
190       bool            m_skipshout;
191       bool            m_postdataset;
192
193       CRingBuffer     m_buffer;           // our ringhold buffer
194       char *          m_overflowBuffer;   // in the rare case we would overflow the above buffer
195       unsigned int    m_overflowSize;     // size of the overflow buffer
196
197       int             m_stillRunning;     // Is background url fetch still in progress?
198
199       typedef std::map<CStdString, CStdString> MAPHTTPHEADERS;
200       MAPHTTPHEADERS m_requestheaders;
201
202       long            m_httpresponse;
203   };
204 }