| 1 |
|
| 2 |
// http://e2guardian.org/ |
| 3 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 4 |
|
| 5 |
#ifndef __HPP_HTTPHeader |
| 6 |
#define __HPP_HTTPHeader |
| 7 |
|
| 8 |
// DEFINES |
| 9 |
|
| 10 |
#define __E2HEADER_SENDALL 0 |
| 11 |
#define __E2HEADER_SENDFIRSTLINE 1 |
| 12 |
#define __E2HEADER_SENDREST 2 |
| 13 |
|
| 14 |
#define __HEADER_REQUEST 1 |
| 15 |
#define __HEADER_RESPONSE 2 |
| 16 |
|
| 17 |
// INCLUDES |
| 18 |
|
| 19 |
#include <deque> |
| 20 |
|
| 21 |
#include "String.hpp" |
| 22 |
//#include "DataBuffer.hpp" |
| 23 |
#include "Socket.hpp" |
| 24 |
#include "RegExp.hpp" |
| 25 |
#include "ListMeta.hpp" |
| 26 |
#include "FOptionContainer.hpp" |
| 27 |
|
| 28 |
// DECLARATIONS |
| 29 |
|
| 30 |
class HTTPHeader |
| 31 |
{ |
| 32 |
public: |
| 33 |
std::deque<String> header; |
| 34 |
//DataBuffer postdata; |
| 35 |
unsigned int port; |
| 36 |
bool is_response = false; |
| 37 |
bool icap; |
| 38 |
bool first_line_sent = false; |
| 39 |
bool header_all_sent = false; |
| 40 |
String redirect; |
| 41 |
String useragent; |
| 42 |
String contenttype; |
| 43 |
String contentencoding; |
| 44 |
String transferencoding; |
| 45 |
|
| 46 |
// reset header object for future use |
| 47 |
void reset(); |
| 48 |
|
| 49 |
// network communication funcs |
| 50 |
|
| 51 |
void setTimeout(int t); |
| 52 |
bool in(Socket *sock, bool allowpersistent = false ); |
| 53 |
bool in_handle_100(Socket *sock, bool allowpersistent = false, bool expect_100 = false ); |
| 54 |
|
| 55 |
void setClientIP(String &ip); |
| 56 |
String getClientIP(); |
| 57 |
|
| 58 |
String stringHeader(); // output header as a String (used by ICAP) |
| 59 |
|
| 60 |
// send headers out over the given socket |
| 61 |
// "reconnect" flag gives permission to reconnect to the socket on write error |
| 62 |
// - this allows us to re-open the proxy connection on pconns if squid's end has |
| 63 |
// timed out but the client's end hasn't. not much use with NTLM, since squid |
| 64 |
// will throw a 407 and restart negotiation, but works well with basic & others. |
| 65 |
//void out(Socket *peersock, Socket *sock, int sendflag, bool reconnect = false) throw(std::exception); |
| 66 |
bool out(Socket *peersock, Socket *sock, int sendflag, bool reconnect = false); |
| 67 |
|
| 68 |
// discard remainder of POST data |
| 69 |
// amount to discard can be passed in, or will default to contentLength() |
| 70 |
void discard(Socket *sock, off_t cl = -2); |
| 71 |
|
| 72 |
// header value and type checks |
| 73 |
|
| 74 |
// request type: GET, HEAD, POST etc. |
| 75 |
String requestType(); |
| 76 |
String requesttype; |
| 77 |
int returnCode(); |
| 78 |
int returncode = 0; |
| 79 |
bool onepointone = false; |
| 80 |
|
| 81 |
// get content length - returns -1 if undetermined |
| 82 |
off_t contentLength(); |
| 83 |
String getContentType(); |
| 84 |
bool OKtoFilterMime(FOptionContainer* &foc); |
| 85 |
String getMIMEBoundary(); |
| 86 |
// check received content type against given content type |
| 87 |
//bool isContentType(const String &t,int filtergroup); |
| 88 |
bool isContentType(const String &t,FOptionContainer* &foc); |
| 89 |
// check HTTP message code to see if it's an auth required message |
| 90 |
bool authRequired(); |
| 91 |
// Content-Disposition |
| 92 |
String disposition(); |
| 93 |
String userAgent(); |
| 94 |
// grab contents of X-Forwarded-For |
| 95 |
std::string getXForwardedForIP(); |
| 96 |
// check HTTP message code to see if it's a redirect |
| 97 |
bool isRedirection(); |
| 98 |
// see if content-type is something other than "identity" |
| 99 |
bool isCompressed(); |
| 100 |
bool addHeader(String & xheader); |
| 101 |
//bool isHeaderAdded(int filtergroup); |
| 102 |
bool isHeaderAdded(FOptionContainer* &foc); |
| 103 |
bool addheaderchecked; |
| 104 |
bool isheaderadded; |
| 105 |
String *plogheadervalue; |
| 106 |
String *pheaderident; |
| 107 |
String *ptransfercoding; |
| 108 |
String *ptransferencoding; |
| 109 |
std::string getAuthHeader(); |
| 110 |
// see if search usl and set searchwords |
| 111 |
bool isSearch(FOptionContainer* &foc); |
| 112 |
String searchwords(); |
| 113 |
String searchterms(); |
| 114 |
bool searchchecked; |
| 115 |
bool chunked; |
| 116 |
bool expects_100 = false; |
| 117 |
String contentEncoding(); |
| 118 |
String transferEncoding(); |
| 119 |
// grab the contents of Proxy-Authorization header |
| 120 |
// returns base64-decoding of the chunk of data after the auth type string |
| 121 |
std::string getAuthData(); |
| 122 |
// grab raw contents of Proxy-Authorization header, without b64 decode |
| 123 |
std::string getRawAuthData(); |
| 124 |
// Debug show header |
| 125 |
void dbshowheader(String *url, const char *clientip); |
| 126 |
void dbshowheader(bool outgoing); |
| 127 |
// check whether a connection is persistent |
| 128 |
bool isPersistent() |
| 129 |
{ |
| 130 |
return ispersistent; |
| 131 |
}; |
| 132 |
bool wasPersistent() |
| 133 |
{ |
| 134 |
return waspersistent; |
| 135 |
}; |
| 136 |
|
| 137 |
// set POST data for outgoing requests. |
| 138 |
// assumes that existing POST data has already been discarded |
| 139 |
// or retrieved elsewhere, and sends this data instead when ::out |
| 140 |
// is called. |
| 141 |
void setPostData(const char *data, size_t len); |
| 142 |
|
| 143 |
void setDirect(); |
| 144 |
|
| 145 |
|
| 146 |
// detailed value/type checks |
| 147 |
|
| 148 |
bool malformedURL(const String &url); |
| 149 |
String getAuthType(); |
| 150 |
String getUrl(bool withport = false, bool isssl = false); |
| 151 |
String getLogUrl(bool withport = false, bool isssl = false); |
| 152 |
String url(); |
| 153 |
|
| 154 |
String redirecturl(); |
| 155 |
|
| 156 |
// header modifications |
| 157 |
|
| 158 |
void addXForwardedFor(const std::string &clientip); |
| 159 |
// strip content-encoding, and simultaneously set content-length to newlen |
| 160 |
void removeEncoding(int newlen); |
| 161 |
void setContentLength(int newlen); |
| 162 |
|
| 163 |
//bool DenySSL(FOptionContainer* &foc); |
| 164 |
// make a connection persistent - or not |
| 165 |
void makePersistent(bool persist = true); |
| 166 |
// make the request look as if its coming from the origin server |
| 167 |
void makeTransparent(bool incoming); |
| 168 |
// modifies the URL in all relevant header lines after a regexp search and replace |
| 169 |
// setURL Code originally from from Ton Gorter 2004 |
| 170 |
void setURL(String &url); |
| 171 |
|
| 172 |
// modifies connect site only - leaves other headers alone |
| 173 |
void setConnect(String &con_site); |
| 174 |
// do URL decoding (%xx) on string |
| 175 |
// decode everything, or just numbers, letters and - |
| 176 |
static String decode(const String &s, bool decodeAll = false); |
| 177 |
|
| 178 |
// Bypass URL & Cookie funcs |
| 179 |
|
| 180 |
bool isBypassCookie(String url, const char *magic, const char *clientip, const char *user); |
| 181 |
//void chopBypass(String url, bool infectionbypass); |
| 182 |
void chopBypass(String url,std::string bp_type); |
| 183 |
//void chopScanBypass(String url); |
| 184 |
// add cookie to outgoing headers with given name & value |
| 185 |
void setCookie(const char *cookie, const char *domain, const char *value); |
| 186 |
bool isProxyRequest; |
| 187 |
|
| 188 |
// encode url |
| 189 |
String URLEncode(); |
| 190 |
|
| 191 |
// grab referer url from headers |
| 192 |
String getReferer(); |
| 193 |
|
| 194 |
HTTPHeader() |
| 195 |
// : port(0), timeout(120000), contentlength(0), postdata(NULL), dirty(true), is_response(false) |
| 196 |
: port(0), timeout(120000), contentlength(0), postdata(NULL), dirty(true) |
| 197 |
{ |
| 198 |
reset(); |
| 199 |
}; |
| 200 |
HTTPHeader(int type) |
| 201 |
// : port(0), timeout(120000), contentlength(0), postdata(NULL), dirty(true), is_response(false) |
| 202 |
: port(0), timeout(120000), contentlength(0), postdata(NULL), dirty(true) |
| 203 |
{ |
| 204 |
reset(); |
| 205 |
setType(type); |
| 206 |
}; |
| 207 |
|
| 208 |
~HTTPHeader() |
| 209 |
{ |
| 210 |
delete postdata; |
| 211 |
}; |
| 212 |
|
| 213 |
void setType(int type) { |
| 214 |
if (type == __HEADER_RESPONSE) |
| 215 |
is_response = true; |
| 216 |
else |
| 217 |
is_response = false; |
| 218 |
}; |
| 219 |
|
| 220 |
// generate bypass hashed url |
| 221 |
String hashedURL(String *url, std::string *clientip, |
| 222 |
bool infectionbypass, std::string *user, FOptionContainer &fdl, bool fakecgi = false); |
| 223 |
// generate bypass hashed cookie |
| 224 |
String hashedCookie(String *url, const char *magic, std::string *clientip, int bypasstimestamp, std::string user); |
| 225 |
|
| 226 |
// base64 decode a complete string |
| 227 |
std::string decodeb64(const String &line); |
| 228 |
|
| 229 |
private: |
| 230 |
// timeout for socket operations |
| 231 |
int timeout; |
| 232 |
|
| 233 |
|
| 234 |
// header index pointers |
| 235 |
String *phost; |
| 236 |
String *pport; |
| 237 |
String *pcontentlength; |
| 238 |
String *pcontenttype; |
| 239 |
String *pproxyauthorization; |
| 240 |
String *pauthorization; |
| 241 |
String *pproxyauthenticate; |
| 242 |
String *pcontentdisposition; |
| 243 |
String *puseragent; |
| 244 |
String *pxforwardedfor; |
| 245 |
String *pcontentencoding; |
| 246 |
String *pproxyconnection; |
| 247 |
String *pkeepalive; |
| 248 |
|
| 249 |
// cached result of getUrl() |
| 250 |
std::string cachedurl; |
| 251 |
// used to record if it is a header within a MITM |
| 252 |
bool mitm = false; |
| 253 |
// is direct rather than via proxy |
| 254 |
bool isdirect = false; |
| 255 |
|
| 256 |
String searchwds; |
| 257 |
//std::string searchwds; |
| 258 |
//std::string searchtms; |
| 259 |
String searchtms; |
| 260 |
bool issearch; |
| 261 |
|
| 262 |
// cached result of contentLength() |
| 263 |
off_t contentlength; |
| 264 |
bool clcached; |
| 265 |
|
| 266 |
// replacement POST data for sending during ::out |
| 267 |
char *postdata; |
| 268 |
size_t postdata_len = 0; |
| 269 |
|
| 270 |
bool ispersistent, waspersistent; |
| 271 |
|
| 272 |
bool dirty; |
| 273 |
|
| 274 |
std::string s_clientip; |
| 275 |
|
| 276 |
// check & fix headers from servers that don't obey standards |
| 277 |
void checkheader(bool allowpersistent); |
| 278 |
bool checkfirstheaderline(); |
| 279 |
|
| 280 |
// convert %xx back to original character |
| 281 |
static String hexToChar(const String &n, bool all = false); |
| 282 |
// base64 decode an individual char |
| 283 |
int decode1b64(char c); |
| 284 |
|
| 285 |
// modify supplied accept-encoding header, adding "identity" and stripping unsupported compression types |
| 286 |
String modifyEncodings(String e); |
| 287 |
|
| 288 |
// Generic search & replace code, called by urlRegExp and headerRegExp |
| 289 |
// urlRegExp Code originally from from Ton Gorter 2004 |
| 290 |
bool regExp(String &line, std::deque<RegExp> ®exp_list, std::deque<String> &replacement_list); |
| 291 |
|
| 292 |
// grab cookies from headers |
| 293 |
String getCookie(const char *cookie); |
| 294 |
|
| 295 |
}; |
| 296 |
|
| 297 |
#endif |