ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HTTPSock.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2018 ZNC, see the NOTICE file for details.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ZNC_HTTPSOCK_H
18 #define ZNC_HTTPSOCK_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/Socket.h>
22 #include <znc/FileUtils.h>
23 
24 class CModule;
25 
26 class CHTTPSock : public CSocket {
27  public:
28  CHTTPSock(CModule* pMod, const CString& sURIPrefix);
29  CHTTPSock(CModule* pMod, const CString& sURIPrefix,
30  const CString& sHostname, unsigned short uPort,
31  int iTimeout = 60);
32  virtual ~CHTTPSock();
33 
34  // Csocket derived members
35  void ReadData(const char* data, size_t len) override;
36  void ReadLine(const CString& sData) override;
37  void Connected() override;
38  Csock* GetSockObj(const CString& sHost, unsigned short uPort) override = 0;
39  // !Csocket derived members
40 
41  // Hooks
42  virtual bool ForceLogin();
43  virtual bool OnLogin(const CString& sUser, const CString& sPass,
44  bool bBasic);
45  virtual void OnPageRequest(const CString& sURI) = 0;
46  virtual bool PrintFile(const CString& sFileName, CString sContentType = "");
47  // !Hooks
48 
49  void CheckPost();
50  bool SentHeader() const;
51  bool PrintHeader(off_t uContentLength, const CString& sContentType = "",
52  unsigned int uStatusId = 200,
53  const CString& sStatusMsg = "OK");
54  void AddHeader(const CString& sName, const CString& sValue);
55  void SetContentType(const CString& sContentType);
56 
57  bool PrintNotFound();
58  bool Redirect(const CString& sURL);
59  bool PrintErrorPage(unsigned int uStatusId, const CString& sStatusMsg,
60  const CString& sMessage);
61  static void ParseParams(const CString& sParams,
62  std::map<CString, VCString>& msvsParams);
63  void ParseURI();
64  void GetPage();
65  static CString GetDate(time_t tm = 0);
66  CString GetRemoteIP() const override;
67 
68  // Cookies
69  CString GetRequestCookie(const CString& sKey) const;
70  bool SendCookie(const CString& sKey, const CString& sValue);
71  // Cookies
72 
73  // Setters
74  void SetDocRoot(const CString& s);
75  void SetLoggedIn(bool b) { m_bLoggedIn = b; }
76  // !Setters
77 
78  // Getters
79  CString GetPath() const;
80  bool IsLoggedIn() const { return m_bLoggedIn; }
81  const CString& GetDocRoot() const;
82  const CString& GetUser() const;
83  const CString& GetPass() const;
84  const CString& GetParamString() const;
85  const CString& GetContentType() const;
86  const CString& GetURI() const;
87  const CString& GetURIPrefix() const;
88  bool IsPost() const;
89  // !Getters
90 
91  // Parameter access
92  CString GetParam(const CString& sName, bool bPost = true,
93  const CString& sFilter = "\r\n") const;
94  CString GetRawParam(const CString& sName, bool bPost = true) const;
95  bool HasParam(const CString& sName, bool bPost = true) const;
96  const std::map<CString, VCString>& GetParams(bool bPost = true) const;
97  size_t GetParamValues(const CString& sName, VCString& vsRet,
98  bool bPost = true,
99  const CString& sFilter = "\r\n") const;
100  size_t GetParamValues(const CString& sName, std::set<CString>& ssRet,
101  bool bPost = true,
102  const CString& sFilter = "\r\n") const;
103  // !Parameter access
104  private:
105  static CString GetRawParam(const CString& sName,
106  const std::map<CString, VCString>& msvsParams);
107  static CString GetParam(const CString& sName,
108  const std::map<CString, VCString>& msvsParams,
109  const CString& sFilter);
110  static size_t GetParamValues(const CString& sName, VCString& vsRet,
111  const std::map<CString, VCString>& msvsParams,
112  const CString& sFilter);
113  static size_t GetParamValues(const CString& sName, std::set<CString>& ssRet,
114  const std::map<CString, VCString>& msvsParams,
115  const CString& sFilter);
116 
117  void WriteFileUncompressed(CFile& File);
118  void WriteFileGzipped(CFile& File);
119 
120  protected:
121  void PrintPage(const CString& sPage);
122  void Init();
123 
127  bool m_bPost;
128  bool m_bDone;
130  unsigned long m_uPostLen;
138  std::map<CString, VCString> m_msvsPOSTParams;
139  std::map<CString, VCString> m_msvsGETParams;
147 };
148 
149 #endif // !ZNC_HTTPSOCK_H
const CString & GetURIPrefix() const
bool m_bBasicAuth
Definition: HTTPSock.h:129
const CString & GetPass() const
bool m_bSentHeader
Definition: HTTPSock.h:124
CString GetRemoteIP() const override
CString m_sPostData
Definition: HTTPSock.h:131
bool PrintNotFound()
virtual void OnPageRequest(const CString &sURI)=0
void AddHeader(const CString &sName, const CString &sValue)
bool m_bAcceptGzip
Definition: HTTPSock.h:143
void CheckPost()
virtual ~CHTTPSock()
void SetDocRoot(const CString &s)
CString GetPath() const
CString m_sURI
Definition: HTTPSock.h:132
CString GetRequestCookie(const CString &sKey) const
void ReadData(const char *data, size_t len) override
Ready to read data event.
MCString m_msResponseCookies
Definition: HTTPSock.h:145
bool PrintErrorPage(unsigned int uStatusId, const CString &sStatusMsg, const CString &sMessage)
bool m_bDone
Definition: HTTPSock.h:128
const std::map< CString, VCString > & GetParams(bool bPost=true) const
bool PrintHeader(off_t uContentLength, const CString &sContentType="", unsigned int uStatusId=200, const CString &sStatusMsg="OK")
bool m_bLoggedIn
Definition: HTTPSock.h:126
bool SentHeader() const
bool SendCookie(const CString &sKey, const CString &sValue)
Base Csock implementation to be used by modules.
Definition: Socket.h:242
Csock * GetSockObj(const CString &sHost, unsigned short uPort) override=0
CString GetParam(const CString &sName, bool bPost=true, const CString &sFilter="\r\n") const
bool IsLoggedIn() const
Definition: HTTPSock.h:80
void Init()
Basic socket class.
Definition: Csocket.h:564
Definition: FileUtils.h:30
std::map< CString, VCString > m_msvsPOSTParams
Definition: HTTPSock.h:138
std::vector< CString > VCString
Definition: ZNCString.h:38
const CString & GetUser() const
MCString m_msRequestCookies
Definition: HTTPSock.h:144
virtual bool PrintFile(const CString &sFileName, CString sContentType="")
String class that is used inside ZNC.
Definition: ZNCString.h:68
CString m_sURIPrefix
Definition: HTTPSock.h:146
void SetContentType(const CString &sContentType)
bool HasParam(const CString &sName, bool bPost=true) const
bool m_bGotHeader
Definition: HTTPSock.h:125
bool m_bPost
Definition: HTTPSock.h:127
CString m_sDocRoot
Definition: HTTPSock.h:136
CString m_sPass
Definition: HTTPSock.h:134
static CString GetDate(time_t tm=0)
const CString & GetDocRoot() const
The base class for your own ZNC modules.
Definition: Modules.h:407
CHTTPSock(CModule *pMod, const CString &sURIPrefix)
const CString & GetContentType() const
CString m_sIfNoneMatch
Definition: HTTPSock.h:142
void GetPage()
void SetLoggedIn(bool b)
Definition: HTTPSock.h:75
A dictionary for strings.
Definition: ZNCString.h:595
bool m_bHTTP10Client
Definition: HTTPSock.h:141
const CString & GetParamString() const
void ReadLine(const CString &sData) override
MCString m_msHeaders
Definition: HTTPSock.h:140
std::map< CString, VCString > m_msvsGETParams
Definition: HTTPSock.h:139
CString m_sForwardedIP
Definition: HTTPSock.h:137
void ParseURI()
virtual bool OnLogin(const CString &sUser, const CString &sPass, bool bBasic)
bool Redirect(const CString &sURL)
virtual bool ForceLogin()
static void ParseParams(const CString &sParams, std::map< CString, VCString > &msvsParams)
bool IsPost() const
void PrintPage(const CString &sPage)
unsigned long m_uPostLen
Definition: HTTPSock.h:130
CString GetRawParam(const CString &sName, bool bPost=true) const
const CString & GetURI() const
void Connected() override
Connected event.
CString m_sContentType
Definition: HTTPSock.h:135
size_t GetParamValues(const CString &sName, VCString &vsRet, bool bPost=true, const CString &sFilter="\r\n") const
CString m_sUser
Definition: HTTPSock.h:133
Definition: HTTPSock.h:26