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-2014 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 _HTTPSOCK_H
18 #define _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, const CString& sHostname, unsigned short uPort, int iTimeout = 60);
30  virtual ~CHTTPSock();
31 
32  // Csocket derived members
33  virtual void ReadData(const char* data, size_t len);
34  virtual void ReadLine(const CString& sData);
35  virtual void Connected();
36  virtual Csock* GetSockObj(const CString& sHost, unsigned short uPort) = 0;
37  // !Csocket derived members
38 
39  // Hooks
40  virtual bool ForceLogin();
41  virtual bool OnLogin(const CString& sUser, const CString& sPass);
42  virtual void OnPageRequest(const CString& sURI) = 0;
43  virtual bool PrintFile(const CString& sFileName, CString sContentType = "");
44  // !Hooks
45 
46  void CheckPost();
47  bool SentHeader() const;
48  bool PrintHeader(off_t uContentLength, const CString& sContentType = "", unsigned int uStatusId = 200, const CString& sStatusMsg = "OK");
49  void AddHeader(const CString& sName, const CString& sValue);
50  void SetContentType(const CString& sContentType);
51 
52  bool PrintNotFound();
53  bool Redirect(const CString& sURL);
54  bool PrintErrorPage(unsigned int uStatusId, const CString& sStatusMsg, const CString& sMessage);
55  static void ParseParams(const CString& sParams, std::map<CString, VCString>& msvsParams);
56  void ParseURI();
57  void GetPage();
58  static CString GetDate(time_t tm = 0);
59  virtual CString GetRemoteIP();
60 
61  // Cookies
62  CString GetRequestCookie(const CString& sKey) const;
63  bool SendCookie(const CString& sKey, const CString& sValue);
64  // Cookies
65 
66  // Setters
67  void SetDocRoot(const CString& s);
68  void SetLoggedIn(bool b) { m_bLoggedIn = b; }
69  // !Setters
70 
71  // Getters
72  CString GetPath() const;
73  bool IsLoggedIn() const { return m_bLoggedIn; }
74  const CString& GetDocRoot() const;
75  const CString& GetUser() const;
76  const CString& GetPass() const;
77  const CString& GetParamString() const;
78  const CString& GetContentType() const;
79  const CString& GetURIPrefix() const;
80  bool IsPost() const;
81  // !Getters
82 
83  // Parameter access
84  CString GetParam(const CString& sName, bool bPost = true, const CString& sFilter = "\r\n") const;
85  CString GetRawParam(const CString& sName, bool bPost = true) const;
86  bool HasParam(const CString& sName, bool bPost = true) const;
87  const std::map<CString, VCString>& GetParams(bool bPost = true) const;
88  size_t GetParamValues(const CString& sName, VCString& vsRet, bool bPost = true, const CString& sFilter = "\r\n") const;
89  size_t GetParamValues(const CString& sName, std::set<CString>& ssRet, bool bPost = true, const CString& sFilter = "\r\n") const;
90  // !Parameter access
91 private:
92  static CString GetRawParam(const CString& sName, const std::map<CString, VCString>& msvsParams);
93  static CString GetParam(const CString& sName, const std::map<CString, VCString>& msvsParams, const CString& sFilter);
94  static size_t GetParamValues(const CString& sName, VCString& vsRet, const std::map<CString, VCString>& msvsParams, const CString& sFilter);
95  static size_t GetParamValues(const CString& sName, std::set<CString>& ssRet, const std::map<CString, VCString>& msvsParams, const CString& sFilter);
96 
97  void WriteFileUncompressed(CFile& File);
98  void WriteFileGzipped(CFile& File);
99 
100 protected:
101  void PrintPage(const CString& sPage);
102  void Init();
103 
107  bool m_bPost;
108  bool m_bDone;
109  unsigned long m_uPostLen;
117  std::map<CString, VCString> m_msvsPOSTParams;
118  std::map<CString, VCString> m_msvsGETParams;
126 };
127 
128 #endif // !_HTTPSOCK_H
const CString & GetURIPrefix() const
virtual void ReadData(const char *data, size_t len)
Override these functions for an easy interface when using the Socket Manager Don&#39;t bother using these...
const CString & GetPass() const
bool m_bSentHeader
Definition: HTTPSock.h:104
CString m_sPostData
Definition: HTTPSock.h:110
bool PrintNotFound()
virtual void OnPageRequest(const CString &sURI)=0
void AddHeader(const CString &sName, const CString &sValue)
virtual Csock * GetSockObj(const CString &sHost, unsigned short uPort)=0
bool m_bAcceptGzip
Definition: HTTPSock.h:122
void CheckPost()
virtual ~CHTTPSock()
void SetDocRoot(const CString &s)
CString GetPath() const
CString m_sURI
Definition: HTTPSock.h:111
CString GetRequestCookie(const CString &sKey) const
MCString m_msResponseCookies
Definition: HTTPSock.h:124
bool PrintErrorPage(unsigned int uStatusId, const CString &sStatusMsg, const CString &sMessage)
bool m_bDone
Definition: HTTPSock.h:108
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:106
bool SentHeader() const
bool SendCookie(const CString &sKey, const CString &sValue)
Base Csock implementation to be used by modules.
Definition: Socket.h:164
CString GetParam(const CString &sName, bool bPost=true, const CString &sFilter="\r\n") const
bool IsLoggedIn() const
Definition: HTTPSock.h:73
void Init()
Basic socket class.
Definition: Csocket.h:537
Definition: FileUtils.h:30
std::map< CString, VCString > m_msvsPOSTParams
Definition: HTTPSock.h:117
std::vector< CString > VCString
Definition: ZNCString.h:37
const CString & GetUser() const
MCString m_msRequestCookies
Definition: HTTPSock.h:123
virtual bool PrintFile(const CString &sFileName, CString sContentType="")
String class that is used inside ZNC.
Definition: ZNCString.h:67
CString m_sURIPrefix
Definition: HTTPSock.h:125
void SetContentType(const CString &sContentType)
virtual bool OnLogin(const CString &sUser, const CString &sPass)
bool HasParam(const CString &sName, bool bPost=true) const
bool m_bGotHeader
Definition: HTTPSock.h:105
bool m_bPost
Definition: HTTPSock.h:107
CString m_sDocRoot
Definition: HTTPSock.h:115
CString m_sPass
Definition: HTTPSock.h:113
static CString GetDate(time_t tm=0)
const CString & GetDocRoot() const
The base class for your own ZNC modules.
Definition: Modules.h:322
CHTTPSock(CModule *pMod, const CString &sURIPrefix)
const CString & GetContentType() const
CString m_sIfNoneMatch
Definition: HTTPSock.h:121
virtual void ReadLine(const CString &sData)
void GetPage()
void SetLoggedIn(bool b)
Definition: HTTPSock.h:68
A dictionary for strings.
Definition: ZNCString.h:538
bool m_bHTTP10Client
Definition: HTTPSock.h:120
const CString & GetParamString() const
MCString m_msHeaders
Definition: HTTPSock.h:119
std::map< CString, VCString > m_msvsGETParams
Definition: HTTPSock.h:118
CString m_sForwardedIP
Definition: HTTPSock.h:116
void ParseURI()
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:109
CString GetRawParam(const CString &sName, bool bPost=true) const
CString m_sContentType
Definition: HTTPSock.h:114
size_t GetParamValues(const CString &sName, VCString &vsRet, bool bPost=true, const CString &sFilter="\r\n") const
CString m_sUser
Definition: HTTPSock.h:112
virtual CString GetRemoteIP()
virtual void Connected()
Override these functions for an easy interface when using the Socket Manager Don&#39;t bother using these...
Definition: HTTPSock.h:26