xbmc
CurlFile.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "IFile.h"
12 #include "utils/HttpHeader.h"
13 #include "utils/RingBuffer.h"
14 
15 #include <map>
16 #include <string>
17 
18 typedef void CURL_HANDLE;
19 typedef void CURLM;
20 struct curl_slist;
21 
22 namespace XFILE
23 {
24  class CCurlFile : public IFile
25  {
26  private:
27  typedef enum
28  {
29  PROXY_HTTP = 0,
30  PROXY_SOCKS4,
31  PROXY_SOCKS4A,
32  PROXY_SOCKS5,
33  PROXY_SOCKS5_REMOTE,
34  PROXY_HTTPS,
35  } ProxyType;
36 
37  public:
38  CCurlFile();
39  ~CCurlFile() override;
40  bool Open(const CURL& url) override;
41  bool OpenForWrite(const CURL& url, bool bOverWrite = false) override;
42  bool ReOpen(const CURL& url) override;
43  bool Exists(const CURL& url) override;
44  int64_t Seek(int64_t iFilePosition, int iWhence=SEEK_SET) override;
45  int64_t GetPosition() override;
46  int64_t GetLength() override;
47  int Stat(const CURL& url, struct __stat64* buffer) override;
48  void Close() override;
49  bool ReadString(char *szLine, int iLineLength) override { return m_state->ReadString(szLine, iLineLength); }
50  ssize_t Read(void* lpBuf, size_t uiBufSize) override { return m_state->Read(lpBuf, uiBufSize); }
51  ssize_t Write(const void* lpBuf, size_t uiBufSize) override;
52  const std::string GetProperty(XFILE::FileProperty type, const std::string &name = "") const override;
53  const std::vector<std::string> GetPropertyValues(XFILE::FileProperty type, const std::string &name = "") const override;
54  int IoControl(EIoControl request, void* param) override;
55  double GetDownloadSpeed() override;
56 
57  bool Post(const std::string& strURL, const std::string& strPostData, std::string& strHTML);
58  bool Get(const std::string& strURL, std::string& strHTML);
59  bool ReadData(std::string& strHTML);
60  bool Download(const std::string& strURL, const std::string& strFileName, unsigned int* pdwSize = NULL);
61  bool IsInternet();
62  void Cancel();
63  void Reset();
64  void SetUserAgent(const std::string& sUserAgent) { m_userAgent = sUserAgent; }
65  void SetProxy(const std::string &type, const std::string &host, uint16_t port,
66  const std::string &user, const std::string &password);
67  void SetCustomRequest(const std::string &request) { m_customrequest = request; }
68  void SetAcceptEncoding(const std::string& encoding) { m_acceptencoding = encoding; }
69  void SetAcceptCharset(const std::string& charset) { m_acceptCharset = charset; }
70  void SetTimeout(int connecttimeout) { m_connecttimeout = connecttimeout; }
71  void SetLowSpeedTime(int lowspeedtime) { m_lowspeedtime = lowspeedtime; }
72  void SetPostData(const std::string& postdata) { m_postdata = postdata; }
73  void SetReferer(const std::string& referer) { m_referer = referer; }
74  void SetCookie(const std::string& cookie) { m_cookie = cookie; }
75  void SetMimeType(const std::string& mimetype) { SetRequestHeader("Content-Type", mimetype); }
76  void SetRequestHeader(const std::string& header, const std::string& value);
77  void SetRequestHeader(const std::string& header, long value);
78 
79  void ClearRequestHeaders();
80  void SetBufferSize(unsigned int size);
81 
82  const CHttpHeader& GetHttpHeader() const { return m_state->m_httpheader; }
83  std::string GetURL(void);
84  std::string GetRedirectURL();
85 
86  /* static function that will get content type of a file */
87  static bool GetHttpHeader(const CURL &url, CHttpHeader &headers);
88  static bool GetMimeType(const CURL &url, std::string &content, const std::string &useragent="");
89  static bool GetContentType(const CURL &url, std::string &content, const std::string &useragent = "");
90 
91  /* static function that will get cookies stored by CURL in RFC 2109 format */
92  static bool GetCookies(const CURL &url, std::string &cookies);
93 
94  class CReadState
95  {
96  public:
97  CReadState();
98  ~CReadState();
99  CURL_HANDLE* m_easyHandle;
100  CURLM* m_multiHandle;
101 
102  CRingBuffer m_buffer; // our ringhold buffer
103  unsigned int m_bufferSize;
104 
105  char* m_overflowBuffer; // in the rare case we would overflow the above buffer
106  unsigned int m_overflowSize; // size of the overflow buffer
107  int m_stillRunning; // Is background url fetch still in progress
108  bool m_cancelled;
109  int64_t m_fileSize;
110  int64_t m_filePos;
111  bool m_bFirstLoop;
112  bool m_isPaused;
113  bool m_sendRange;
114  bool m_bLastError;
115  bool m_bRetry;
116 
117  char* m_readBuffer;
118 
119  /* returned http header */
120  CHttpHeader m_httpheader;
121  bool IsHeaderDone(void) { return m_httpheader.IsHeaderDone(); }
122 
123  curl_slist* m_curlHeaderList;
124  curl_slist* m_curlAliasList;
125 
126  size_t ReadCallback(char *buffer, size_t size, size_t nitems);
127  size_t WriteCallback(char *buffer, size_t size, size_t nitems);
128  size_t HeaderCallback(void *ptr, size_t size, size_t nmemb);
129 
130  bool Seek(int64_t pos);
131  ssize_t Read(void* lpBuf, size_t uiBufSize);
132  bool ReadString(char *szLine, int iLineLength);
133  int8_t FillBuffer(unsigned int want);
134  void SetReadBuffer(const void* lpBuf, int64_t uiBufSize);
135 
136  void SetResume(void);
137  long Connect(unsigned int size);
138  void Disconnect();
139  };
140 
141  protected:
142  void ParseAndCorrectUrl(CURL &url);
143  void SetCommonOptions(CReadState* state, bool failOnError = true);
144  void SetRequestHeaders(CReadState* state);
145  void SetCorrectHeaders(CReadState* state);
146  bool Service(const std::string& strURL, std::string& strHTML);
147  std::string GetInfoString(int infoType);
148 
149  protected:
150  CReadState* m_state;
151  CReadState* m_oldState;
152  unsigned int m_bufferSize;
153  int64_t m_writeOffset = 0;
154 
155  std::string m_url;
156  std::string m_userAgent;
157  ProxyType m_proxytype = PROXY_HTTP;
158  std::string m_proxyhost;
159  uint16_t m_proxyport = 3128;
160  std::string m_proxyuser;
161  std::string m_proxypassword;
162  std::string m_customrequest;
163  std::string m_acceptencoding;
164  std::string m_acceptCharset;
165  std::string m_ftpauth;
166  std::string m_ftpport;
167  std::string m_binary;
168  std::string m_postdata;
169  std::string m_referer;
170  std::string m_cookie;
171  std::string m_username;
172  std::string m_password;
173  std::string m_httpauth;
174  std::string m_cipherlist;
175  bool m_ftppasvip;
176  int m_connecttimeout;
177  int m_redirectlimit;
178  int m_lowspeedtime;
179  bool m_opened;
180  bool m_forWrite;
181  bool m_inError;
182  bool m_seekable;
183  bool m_multisession;
184  bool m_skipshout;
185  bool m_postdataset;
186  bool m_allowRetry;
187  bool m_verifyPeer = true;
188  bool m_failOnError = true;
189  curl_slist* m_dnsCacheList = nullptr;
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 = 0; // size of the overflow buffer
194 
195  int m_stillRunning; // Is background url fetch still in progress?
196 
197  typedef std::map<std::string, std::string> MAPHTTPHEADERS;
198  MAPHTTPHEADERS m_requestheaders;
199 
200  long m_httpresponse;
201  };
202 }
Definition: Scraper.h:41
Definition: URL.h:20
ssize_t Read(void *lpBuf, size_t uiBufSize) override
Attempt to read bufSize bytes from currently opened file into buffer bufPtr.
Definition: CurlFile.h:50
Definition: IFile.h:42
int Stat(const CURL &url, struct __stat64 *buffer) override
Fills struct __stat64 with information about file specified by url.
Definition: CurlFile.cpp:1500
Definition: CurlFile.h:94
void ParseAndCorrectUrl(CURL &url)
Definition: CurlFile.cpp:733
size_t WriteCallback(char *buffer, size_t size, size_t nitems)
Definition: CurlFile.cpp:183
Definition: LibInputPointer.h:13
ssize_t Write(const void *lpBuf, size_t uiBufSize) override
Attempt to write bufSize bytes from buffer bufPtr into currently opened file.
Definition: CurlFile.cpp:1221
Definition: RingBuffer.h:13
Definition: CurlFile.h:24
int64_t Seek(int64_t iFilePosition, int iWhence=SEEK_SET) override
Definition: CurlFile.cpp:1390
Definition: HttpHeader.h:15