3 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
23 #include "utils/RingBuffer.h"
25 #include "utils/HttpHeader.h"
29 typedef void CURL_HANDLE;
36 class CCurlFile : public IFile
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);
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(); }
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);
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);
91 void ClearRequestHeaders();
92 void SetBufferSize(unsigned int size);
94 const CHttpHeader& GetHttpHeader() { return m_state->m_httpheader; }
95 std::string GetServerReportedCharset(void);
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="");
101 /* static function that will get cookies stored by CURL in RFC 2109 format */
102 static bool GetCookies(const CURL &url, std::string &cookies);
109 XCURL::CURL_HANDLE* m_easyHandle;
110 XCURL::CURLM* m_multiHandle;
112 CRingBuffer m_buffer; // our ringhold buffer
113 unsigned int m_bufferSize;
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
127 /* returned http header */
128 CHttpHeader m_httpheader;
129 bool IsHeaderDone(void)
130 { return m_httpheader.IsHeaderDone(); }
132 struct XCURL::curl_slist* m_curlHeaderList;
133 struct XCURL::curl_slist* m_curlAliasList;
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);
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);
145 void SetResume(void);
146 long Connect(unsigned int size);
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);
159 CReadState* m_oldState;
160 unsigned int m_bufferSize;
161 int64_t m_writeOffset;
164 CStdString m_userAgent;
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;
174 CStdString m_postdata;
175 CStdString m_referer;
177 CStdString m_username;
178 CStdString m_password;
179 CStdString m_httpauth;
180 CStdString m_cipherlist;
182 int m_connecttimeout;
187 bool m_useOldHttpVersion;
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
197 int m_stillRunning; // Is background url fetch still in progress?
199 typedef std::map<CStdString, CStdString> MAPHTTPHEADERS;
200 MAPHTTPHEADERS m_requestheaders;