]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/httpd.h
Merge tag 'v2.0.27' into master.
[user/henk/code/inspircd.git] / include / modules / httpd.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6  *   Copyright (C) 2007 John Brooks <john.brooks@dereferenced.net>
7  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8  *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #pragma once
25
26 #include "base.h"
27 #include "event.h"
28
29 #include <string>
30 #include <sstream>
31 #include <map>
32
33 /** A modifyable list of HTTP header fields
34  */
35 class HTTPHeaders
36 {
37  protected:
38         std::map<std::string,std::string> headers;
39  public:
40
41         /** Set the value of a header
42          * Sets the value of the named header. If the header is already present, it will be replaced
43          */
44         void SetHeader(const std::string &name, const std::string &data)
45         {
46                 headers[name] = data;
47         }
48
49         /** Set the value of a header, only if it doesn't exist already
50          * Sets the value of the named header. If the header is already present, it will NOT be updated
51          */
52         void CreateHeader(const std::string &name, const std::string &data)
53         {
54                 if (!IsSet(name))
55                         SetHeader(name, data);
56         }
57
58         /** Remove the named header
59          */
60         void RemoveHeader(const std::string &name)
61         {
62                 headers.erase(name);
63         }
64
65         /** Remove all headers
66          */
67         void Clear()
68         {
69                 headers.clear();
70         }
71
72         /** Get the value of a header
73          * @return The value of the header, or an empty string
74          */
75         std::string GetHeader(const std::string &name)
76         {
77                 std::map<std::string,std::string>::iterator it = headers.find(name);
78                 if (it == headers.end())
79                         return std::string();
80
81                 return it->second;
82         }
83
84         /** Check if the given header is specified
85          * @return true if the header is specified
86          */
87         bool IsSet(const std::string &name)
88         {
89                 std::map<std::string,std::string>::iterator it = headers.find(name);
90                 return (it != headers.end());
91         }
92
93         /** Get all headers, formatted by the HTTP protocol
94          * @return Returns all headers, formatted according to the HTTP protocol. There is no request terminator at the end
95          */
96         std::string GetFormattedHeaders()
97         {
98                 std::string re;
99
100                 for (std::map<std::string,std::string>::iterator i = headers.begin(); i != headers.end(); i++)
101                         re += i->first + ": " + i->second + "\r\n";
102
103                 return re;
104         }
105 };
106
107 class HttpServerSocket;
108
109 /** This class represents a HTTP request.
110  */
111 class HTTPRequest
112 {
113  protected:
114         std::string type;
115         std::string document;
116         std::string ipaddr;
117         std::string postdata;
118
119  public:
120
121         HTTPHeaders *headers;
122         int errorcode;
123
124         /** A socket pointer, which you must return in your HTTPDocument class
125          * if you reply to this request.
126          */
127         HttpServerSocket* sock;
128
129         /** Initialize HTTPRequest.
130          * This constructor is called by m_httpd.so to initialize the class.
131          * @param request_type The request type, e.g. GET, POST, HEAD
132          * @param uri The URI, e.g. /page
133          * @param hdr The headers sent with the request
134          * @param opaque An opaque pointer used internally by m_httpd, which you must pass back to the module in your reply.
135          * @param ip The IP address making the web request.
136          * @param pdata The post data (content after headers) received with the request, up to Content-Length in size
137          */
138         HTTPRequest(const std::string& request_type, const std::string& uri,
139                 HTTPHeaders* hdr, HttpServerSocket* socket, const std::string &ip, const std::string &pdata)
140                 : type(request_type), document(uri), ipaddr(ip), postdata(pdata), headers(hdr), sock(socket)
141         {
142         }
143
144         /** Get the post data (request content).
145          * All post data will be returned, including carriage returns and linefeeds.
146          * @return The postdata
147          */
148         std::string& GetPostData()
149         {
150                 return postdata;
151         }
152
153         /** Get the request type.
154          * Any request type can be intercepted, even ones which are invalid in the HTTP/1.1 spec.
155          * @return The request type, e.g. GET, POST, HEAD
156          */
157         std::string& GetType()
158         {
159                 return type;
160         }
161
162         /** Get URI.
163          * The URI string (URL minus hostname and scheme) will be provided by this function.
164          * @return The URI being requested
165          */
166         std::string& GetURI()
167         {
168                 return document;
169         }
170
171         /** Get IP address of requester.
172          * The requesting system's ip address will be returned.
173          * @return The IP address as a string
174          */
175         std::string& GetIP()
176         {
177                 return ipaddr;
178         }
179 };
180
181 /** If you want to reply to HTTP requests, you must return a HTTPDocumentResponse to
182  * the httpd module via the HTTPdAPI.
183  * When you initialize this class you initialize it with all components required to
184  * form a valid HTTP response: the document data and a response code.
185  * You can add additional HTTP headers, if you want.
186  */
187 class HTTPDocumentResponse
188 {
189  public:
190         /** Module that generated this reply
191          */
192         Module* const module;
193
194         std::stringstream* document;
195         unsigned int responsecode;
196
197         /** Any extra headers to include with the defaults
198          */
199         HTTPHeaders headers;
200
201         HTTPRequest& src;
202
203         /** Initialize a HTTPDocumentResponse ready for sending to the httpd module.
204          * @param mod A pointer to the module who responded to the request
205          * @param req The request you obtained from the HTTPRequest at an earlier time
206          * @param doc A stringstream containing the document body
207          * @param response A valid HTTP/1.0 or HTTP/1.1 response code. The response text will be determined for you
208          * based upon the response code.
209          */
210         HTTPDocumentResponse(Module* mod, HTTPRequest& req, std::stringstream* doc, unsigned int response)
211                 : module(mod), document(doc), responsecode(response), src(req)
212         {
213         }
214 };
215
216 class HTTPdAPIBase : public DataProvider
217 {
218  public:
219         HTTPdAPIBase(Module* parent)
220                 : DataProvider(parent, "m_httpd_api")
221         {
222         }
223
224         /** Answer an incoming HTTP request with the provided document
225          * @param response The response created by your module that will be sent to the client
226          */
227         virtual void SendResponse(HTTPDocumentResponse& response) = 0;
228 };
229
230 /** The API provided by the httpd module that allows other modules to respond to incoming
231  * HTTP requests
232  */
233 class HTTPdAPI : public dynamic_reference<HTTPdAPIBase>
234 {
235  public:
236         HTTPdAPI(Module* parent)
237                 : dynamic_reference<HTTPdAPIBase>(parent, "m_httpd_api")
238         {
239         }
240 };
241
242 class HTTPACLEventListener : public Events::ModuleEventListener
243 {
244  public:
245         HTTPACLEventListener(Module* mod)
246                 : ModuleEventListener(mod, "event/http-acl")
247         {
248         }
249
250         virtual ModResult OnHTTPACLCheck(HTTPRequest& req) = 0;
251 };
252
253 class HTTPRequestEventListener : public Events::ModuleEventListener
254 {
255  public:
256         HTTPRequestEventListener(Module* mod)
257                 : ModuleEventListener(mod, "event/http-request")
258         {
259         }
260
261         virtual ModResult OnHTTPRequest(HTTPRequest& req) = 0;
262 };