]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/httpd.h
Merge insp20
[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
28 #include <string>
29 #include <sstream>
30 #include <map>
31
32 /** A modifyable list of HTTP header fields
33  */
34 class HTTPHeaders
35 {
36  protected:
37         std::map<std::string,std::string> headers;
38  public:
39
40         /** Set the value of a header
41          * Sets the value of the named header. If the header is already present, it will be replaced
42          */
43         void SetHeader(const std::string &name, const std::string &data)
44         {
45                 headers[name] = data;
46         }
47
48         /** Set the value of a header, only if it doesn't exist already
49          * Sets the value of the named header. If the header is already present, it will NOT be updated
50          */
51         void CreateHeader(const std::string &name, const std::string &data)
52         {
53                 if (!IsSet(name))
54                         SetHeader(name, data);
55         }
56
57         /** Remove the named header
58          */
59         void RemoveHeader(const std::string &name)
60         {
61                 headers.erase(name);
62         }
63
64         /** Remove all headers
65          */
66         void Clear()
67         {
68                 headers.clear();
69         }
70
71         /** Get the value of a header
72          * @return The value of the header, or an empty string
73          */
74         std::string GetHeader(const std::string &name)
75         {
76                 std::map<std::string,std::string>::iterator it = headers.find(name);
77                 if (it == headers.end())
78                         return std::string();
79
80                 return it->second;
81         }
82
83         /** Check if the given header is specified
84          * @return true if the header is specified
85          */
86         bool IsSet(const std::string &name)
87         {
88                 std::map<std::string,std::string>::iterator it = headers.find(name);
89                 return (it != headers.end());
90         }
91
92         /** Get all headers, formatted by the HTTP protocol
93          * @return Returns all headers, formatted according to the HTTP protocol. There is no request terminator at the end
94          */
95         std::string GetFormattedHeaders()
96         {
97                 std::string re;
98
99                 for (std::map<std::string,std::string>::iterator i = headers.begin(); i != headers.end(); i++)
100                         re += i->first + ": " + i->second + "\r\n";
101
102                 return re;
103         }
104 };
105
106 class HttpServerSocket;
107
108 /** This class represents a HTTP request.
109  */
110 class HTTPRequest : public Event
111 {
112  protected:
113         std::string type;
114         std::string document;
115         std::string ipaddr;
116         std::string postdata;
117
118  public:
119
120         HTTPHeaders *headers;
121         int errorcode;
122
123         /** A socket pointer, which you must return in your HTTPDocument class
124          * if you reply to this request.
125          */
126         HttpServerSocket* sock;
127
128         /** Initialize HTTPRequest.
129          * This constructor is called by m_httpd.so to initialize the class.
130          * @param request_type The request type, e.g. GET, POST, HEAD
131          * @param uri The URI, e.g. /page
132          * @param hdr The headers sent with the request
133          * @param opaque An opaque pointer used internally by m_httpd, which you must pass back to the module in your reply.
134          * @param ip The IP address making the web request.
135          * @param pdata The post data (content after headers) received with the request, up to Content-Length in size
136          */
137         HTTPRequest(Module* me, const std::string &eventid, const std::string &request_type, const std::string &uri,
138                 HTTPHeaders* hdr, HttpServerSocket* socket, const std::string &ip, const std::string &pdata)
139                 : Event(me, eventid), type(request_type), document(uri), ipaddr(ip), postdata(pdata), headers(hdr), sock(socket)
140         {
141         }
142
143         /** Get the post data (request content).
144          * All post data will be returned, including carriage returns and linefeeds.
145          * @return The postdata
146          */
147         std::string& GetPostData()
148         {
149                 return postdata;
150         }
151
152         /** Get the request type.
153          * Any request type can be intercepted, even ones which are invalid in the HTTP/1.1 spec.
154          * @return The request type, e.g. GET, POST, HEAD
155          */
156         std::string& GetType()
157         {
158                 return type;
159         }
160
161         /** Get URI.
162          * The URI string (URL minus hostname and scheme) will be provided by this function.
163          * @return The URI being requested
164          */
165         std::string& GetURI()
166         {
167                 return document;
168         }
169
170         /** Get IP address of requester.
171          * The requesting system's ip address will be returned.
172          * @return The IP address as a string
173          */
174         std::string& GetIP()
175         {
176                 return ipaddr;
177         }
178 };
179
180 /** You must return a HTTPDocument to the httpd module by using the Request class.
181  * When you initialize this class you may initialize it with all components required to
182  * form a valid HTTP response, including document data, headers, and a response code.
183  */
184 class HTTPDocumentResponse : public Request
185 {
186  public:
187         std::stringstream* document;
188         int responsecode;
189         HTTPHeaders headers;
190         HTTPRequest& src;
191
192         /** Initialize a HTTPRequest ready for sending to m_httpd.so.
193          * @param opaque The socket pointer you obtained from the HTTPRequest at an earlier time
194          * @param doc A stringstream containing the document body
195          * @param response A valid HTTP/1.0 or HTTP/1.1 response code. The response text will be determined for you
196          * based upon the response code.
197          * @param extra Any extra headers to include with the defaults, seperated by carriage return and linefeed.
198          */
199         HTTPDocumentResponse(Module* me, HTTPRequest& req, std::stringstream* doc, int response)
200                 : Request(me, req.source, "HTTP-DOC"), document(doc), responsecode(response), src(req)
201         {
202         }
203 };