ZNC  trunk
HTTPSock.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2024 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
std::vector< CString > VCString
Definition: ZNCString.h:38
Definition: FileUtils.h:30
Definition: HTTPSock.h:26
Csock * GetSockObj(const CString &sHost, unsigned short uPort) override=0
void GetPage()
std::map< CString, VCString > m_msvsPOSTParams
Definition: HTTPSock.h:138
MCString m_msRequestCookies
Definition: HTTPSock.h:144
void AddHeader(const CString &sName, const CString &sValue)
bool HasParam(const CString &sName, bool bPost=true) const
CString m_sUser
Definition: HTTPSock.h:133
void ReadData(const char *data, size_t len) override
Ready to read data event.
bool PrintNotFound()
bool m_bHTTP10Client
Definition: HTTPSock.h:141
CString m_sPostData
Definition: HTTPSock.h:131
const CString & GetContentType() const
const CString & GetURIPrefix() const
bool m_bAcceptGzip
Definition: HTTPSock.h:143
CString m_sDocRoot
Definition: HTTPSock.h:136
bool m_bDone
Definition: HTTPSock.h:128
bool PrintErrorPage(unsigned int uStatusId, const CString &sStatusMsg, const CString &sMessage)
CString m_sPass
Definition: HTTPSock.h:134
CHTTPSock(CModule *pMod, const CString &sURIPrefix, const CString &sHostname, unsigned short uPort, int iTimeout=60)
CHTTPSock(CModule *pMod, const CString &sURIPrefix)
bool Redirect(const CString &sURL)
CString GetPath() const
bool SentHeader() const
CString GetRawParam(const CString &sName, bool bPost=true) const
CString m_sIfNoneMatch
Definition: HTTPSock.h:142
void CheckPost()
virtual bool OnLogin(const CString &sUser, const CString &sPass, bool bBasic)
virtual ~CHTTPSock()
void Connected() override
Connected event.
const CString & GetURI() const
MCString m_msResponseCookies
Definition: HTTPSock.h:145
const CString & GetParamString() const
bool PrintHeader(off_t uContentLength, const CString &sContentType="", unsigned int uStatusId=200, const CString &sStatusMsg="OK")
virtual bool ForceLogin()
bool m_bSentHeader
Definition: HTTPSock.h:124
std::map< CString, VCString > m_msvsGETParams
Definition: HTTPSock.h:139
virtual void OnPageRequest(const CString &sURI)=0
CString m_sURIPrefix
Definition: HTTPSock.h:146
bool IsLoggedIn() const
Definition: HTTPSock.h:80
void ParseURI()
void SetDocRoot(const CString &s)
virtual bool PrintFile(const CString &sFileName, CString sContentType="")
const CString & GetUser() const
static CString GetDate(time_t tm=0)
void SetContentType(const CString &sContentType)
const std::map< CString, VCString > & GetParams(bool bPost=true) const
void ReadLine(const CString &sData) override
bool IsPost() const
size_t GetParamValues(const CString &sName, std::set< CString > &ssRet, bool bPost=true, const CString &sFilter="\r\n") const
CString m_sURI
Definition: HTTPSock.h:132
unsigned long m_uPostLen
Definition: HTTPSock.h:130
const CString & GetDocRoot() const
void Init()
bool m_bBasicAuth
Definition: HTTPSock.h:129
bool m_bGotHeader
Definition: HTTPSock.h:125
CString m_sContentType
Definition: HTTPSock.h:135
const CString & GetPass() const
void SetLoggedIn(bool b)
Definition: HTTPSock.h:75
bool SendCookie(const CString &sKey, const CString &sValue)
bool m_bLoggedIn
Definition: HTTPSock.h:126
void PrintPage(const CString &sPage)
bool m_bPost
Definition: HTTPSock.h:127
static void ParseParams(const CString &sParams, std::map< CString, VCString > &msvsParams)
size_t GetParamValues(const CString &sName, VCString &vsRet, bool bPost=true, const CString &sFilter="\r\n") const
CString GetRemoteIP() const override
MCString m_msHeaders
Definition: HTTPSock.h:140
CString m_sForwardedIP
Definition: HTTPSock.h:137
CString GetRequestCookie(const CString &sKey) const
CString GetParam(const CString &sName, bool bPost=true, const CString &sFilter="\r\n") const
The base class for your own ZNC modules.
Definition: Modules.h:420
Base Csock implementation to be used by modules.
Definition: Socket.h:247
String class that is used inside ZNC.
Definition: ZNCString.h:68
Basic socket class.
Definition: Csocket.h:564
A dictionary for strings.
Definition: ZNCString.h:595