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