1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
23 /** A modifyable list of HTTP header fields
25 class HTTPHeaders : public classbase
28 std::map<std::string,std::string> headers;
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
34 void SetHeader(const std::string &name, const std::string &data)
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
42 void CreateHeader(const std::string &name, const std::string &data)
45 SetHeader(name, data);
48 /** Remove the named header
50 void RemoveHeader(const std::string &name)
55 /** Remove all headers
62 /** Get the value of a header
63 * @return The value of the header, or an empty string
65 std::string GetHeader(const std::string &name)
67 std::map<std::string,std::string>::iterator it = headers.find(name);
68 if (it == headers.end())
74 /** Check if the given header is specified
75 * @return true if the header is specified
77 bool IsSet(const std::string &name)
79 std::map<std::string,std::string>::iterator it = headers.find(name);
80 return (it != headers.end());
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
86 std::string GetFormattedHeaders()
90 for (std::map<std::string,std::string>::iterator i = headers.begin(); i != headers.end(); i++)
91 re += i->first + ": " + i->second + "\r\n";
97 /** This class represents a HTTP request.
98 * It will be sent to all modules as the data section of
101 class HTTPRequest : public classbase
105 std::string document;
107 std::string postdata;
111 HTTPHeaders *headers;
114 /** A socket pointer, which you must return in your HTTPDocument class
115 * if you reply to this request.
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
128 HTTPRequest(const std::string &request_type, const std::string &uri, HTTPHeaders* hdr, void* opaque, const std::string &ip, const std::string &pdata)
129 : type(request_type), document(uri), ipaddr(ip), postdata(pdata), headers(hdr), sock(opaque)
133 /** Get the post data (request content).
134 * All post data will be returned, including carriage returns and linefeeds.
135 * @return The postdata
137 std::string& GetPostData()
142 /** Get the request type.
143 * Any request type can be intercepted, even ones which are invalid in the HTTP/1.1 spec.
144 * @return The request type, e.g. GET, POST, HEAD
146 std::string& GetType()
152 * The URI string (URL minus hostname and scheme) will be provided by this function.
153 * @return The URI being requested
155 std::string& GetURI()
160 /** Get IP address of requester.
161 * The requesting system's ip address will be returned.
162 * @return The IP address as a string
170 /** You must return a HTTPDocument to the httpd module by using the Request class.
171 * When you initialize this class you may initialize it with all components required to
172 * form a valid HTTP response, including document data, headers, and a response code.
174 class HTTPDocument : public classbase
178 std::stringstream* document;
185 /** The socket pointer from an earlier HTTPRequest
189 /** Initialize a HTTPRequest ready for sending to m_httpd.so.
190 * @param opaque The socket pointer you obtained from the HTTPRequest at an earlier time
191 * @param doc A stringstream containing the document body
192 * @param response A valid HTTP/1.0 or HTTP/1.1 response code. The response text will be determined for you
193 * based upon the response code.
194 * @param extra Any extra headers to include with the defaults, seperated by carriage return and linefeed.
196 HTTPDocument(void* opaque, std::stringstream* doc, int response) : document(doc), responsecode(response), sock(opaque)
200 /** Get the document text.
201 * @return The document text
203 std::stringstream* GetDocument()
205 return this->document;
208 /** Get the document size.
209 * @return the size of the document text in bytes
211 unsigned long GetDocumentSize()
213 return this->document->str().length();
216 /** Get the response code.
217 * @return The response code
219 int GetResponseCode()
221 return this->responsecode;