1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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
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;
113 /** A socket pointer, which you must return in your HTTPDocument class
114 * if you reply to this request.
118 /** Initialize HTTPRequest.
119 * This constructor is called by m_httpd.so to initialize the class.
120 * @param request_type The request type, e.g. GET, POST, HEAD
121 * @param uri The URI, e.g. /page
122 * @param hdr The headers sent with the request
123 * @param opaque An opaque pointer used internally by m_httpd, which you must pass back to the module in your reply.
124 * @param ip The IP address making the web request.
125 * @param pdata The post data (content after headers) received with the request, up to Content-Length in size
127 HTTPRequest(const std::string &request_type, const std::string &uri, HTTPHeaders* hdr, void* opaque, const std::string &ip, const std::string &pdata)
128 : type(request_type), document(uri), ipaddr(ip), postdata(pdata), headers(hdr), sock(opaque)
132 /** Get the post data (request content).
133 * All post data will be returned, including carriage returns and linefeeds.
134 * @return The postdata
136 std::string& GetPostData()
141 /** Get the request type.
142 * Any request type can be intercepted, even ones which are invalid in the HTTP/1.1 spec.
143 * @return The request type, e.g. GET, POST, HEAD
145 std::string& GetType()
151 * The URI string (URL minus hostname and scheme) will be provided by this function.
152 * @return The URI being requested
154 std::string& GetURI()
159 /** Get IP address of requester.
160 * The requesting system's ip address will be returned.
161 * @return The IP address as a string
169 /** You must return a HTTPDocument to the httpd module by using the Request class.
170 * When you initialize this class you may initialize it with all components required to
171 * form a valid HTTP response, including document data, headers, and a response code.
173 class HTTPDocument : public classbase
177 std::stringstream* document;
184 /** The socket pointer from an earlier HTTPRequest
188 /** Initialize a HTTPRequest ready for sending to m_httpd.so.
189 * @param opaque The socket pointer you obtained from the HTTPRequest at an earlier time
190 * @param doc A stringstream containing the document body
191 * @param response A valid HTTP/1.0 or HTTP/1.1 response code. The response text will be determined for you
192 * based upon the response code.
193 * @param extra Any extra headers to include with the defaults, seperated by carriage return and linefeed.
195 HTTPDocument(void* opaque, std::stringstream* doc, int response) : document(doc), responsecode(response), sock(opaque)
199 /** Get the document text.
200 * @return The document text
202 std::stringstream* GetDocument()
204 return this->document;
207 /** Get the document size.
208 * @return the size of the document text in bytes
210 unsigned long GetDocumentSize()
212 return this->document->str().length();
215 /** Get the response code.
216 * @return The response code
218 int GetResponseCode()
220 return this->responsecode;