1 
2 /* Web Polygraph       http://www.web-polygraph.org/
3  * Copyright 2003-2011 The Measurement Factory
4  * Licensed under the Apache License, Version 2.0 */
5 
6 #include "base/polygraph.h"
7 
8 #include "runtime/httpText.h"
9 
10 #include <list>
11 
12 
13 typedef std::list<String> Headers;
14 static Headers TheHeaders;
15 
16 static
LearnHeader(const String & s)17 const String LearnHeader(const String &s) {
18 	Assert(s.str(": "));
19 	TheHeaders.push_back(s);
20 	return s;
21 }
22 
KnownHeader(const String & header)23 bool KnownHeader(const String &header) {
24 	bool exists = false;
25 	typedef Headers::const_iterator HCI;
26 	for (HCI i = TheHeaders.begin(); !exists && i != TheHeaders.end(); ++i) {
27 		if (i->startsWith(header + ':'))
28 			exists = true;
29 	}
30 	return exists;
31 }
32 
33 
34 // protocol strings
35 const String protoHttp1p0 = "HTTP/1.0";
36 const String protoHttp1p1 = "HTTP/1.1";
37 
38 // request line prefixes
39 const String rlpGet = "GET ";
40 const String rlpHead = "HEAD ";
41 const String rlpPost = "POST ";
42 const String rlpPut = "PUT ";
43 const String rlpPatch = "PATCH ";
44 const String rlpConnect = "CONNECT ";
45 
46 // request line suffixes
47 const String rlsHttp1p0 = " HTTP/1.0\r\n";
48 const String rlsHttp1p1 = " HTTP/1.1\r\n";
49 
50 // response line suffix: status code and reason phrase
51 const String rls100Continue = " 100 Continue\r\n";
52 const String rls200Ok = " 200 OK\r\n";
53 const String rls204NoContent = " 204 No Content\r\n";
54 const String rls206PartialContent = " 206 Partial Content\r\n";
55 const String rls302Found = " 302 Found\r\n";
56 const String rls304NotModified = " 304 Not Modified\r\n";
57 const String rls406NotAcceptable = " 406 Not Acceptable\r\n";
58 const String rls416RequestedRangeNotSatisfiable = " 416 Requested Range Not Satisfiable\r\n";
59 const String rls417ExpectationFailed = " 417 Expectation Failed\r\n";
60 
61 // full header fields
62 const String hfAccept       = LearnHeader("Accept: */*\r\n");
63 const String hfConnAliveOrg = LearnHeader("Connection: keep-alive\r\n");
64 const String hfConnCloseOrg = LearnHeader("Connection: close\r\n");
65 const String hfConnAlivePxy = LearnHeader("Proxy-Connection: keep-alive\r\n");
66 const String hfConnClosePxy = LearnHeader("Proxy-Connection: close\r\n");
67 const String hfCcReload     = LearnHeader("Cache-Control: no-cache\r\n");
68 const String hfPragmaReload = LearnHeader("Pragma: no-cache\r\n");
69 const String hfCcCachable   = LearnHeader("Cache-Control: public\r\n");
70 const String hfCcUncachable = LearnHeader("Cache-Control: private,no-cache\r\n");
71 const String hfPragmaUncachable = LearnHeader("Pragma: no-cache\r\n");
72 const String hfExpect100Continue = LearnHeader("Expect: 100-continue\r\n");
73 
74 // header field prefixes
75 const String hfpAcceptEncoding = LearnHeader("Accept-Encoding: ");
76 const String hfpCacheControl = LearnHeader("Cache-Control: ");
77 const String hfpConnection   = LearnHeader("Connection: ");
78 const String hfpCookie       = LearnHeader("Cookie: ");
79 const String hfpPragma       = LearnHeader("Pragma: ");
80 const String hfpProxyConnection = LearnHeader("Proxy-Connection: ");
81 const String hfpProxyAuthenticate = LearnHeader("Proxy-Authenticate: ");
82 const String hfpWwwAuthenticate = LearnHeader("WWW-Authenticate: ");
83 const String hfpTransferEncoding = LearnHeader("Transfer-Encoding: ");
84 const String hfpSetCookie    = LearnHeader("Set-Cookie: ");
85 const String hfpHost         = LearnHeader("Host: ");
86 const String hfpIMS          = LearnHeader("If-Modified-Since: ");
87 const String hfpContentEncoding   = LearnHeader("Content-Encoding: ");
88 const String hfpContLength   = LearnHeader("Content-Length: ");
89 const String hfpContType     = LearnHeader("Content-Type: ");
90 const String hfpContMd5      = LearnHeader("Content-MD5: ");
91 const String hfpDate         = LearnHeader("Date: ");
92 const String hfpExpires      = LearnHeader("Expires: ");
93 const String hfpLmt          = LearnHeader("Last-Modified: ");
94 const String hfpServer       = LearnHeader("Server: ");
95 const String hfpLocation     = LearnHeader("Location: ");
96 const String hfpProxyAuthorization = LearnHeader("Proxy-Authorization: ");
97 const String hfpAuthorization = LearnHeader("Authorization: ");
98 const String hfpXLocWorld    = LearnHeader("X-Loc-World: ");
99 const String hfpXRemWorld    = LearnHeader("X-Rem-World: ");
100 const String hfpXXact        = LearnHeader("X-Xact: ");
101 const String hfpXTarget      = LearnHeader("X-Target: ");
102 const String hfpXAbort       = LearnHeader("X-Abort: ");
103 const String hfpXPhaseSyncPos= LearnHeader("X-Phase-Sync-Pos: ");
104 const String hfpContRange    = LearnHeader("Content-Range: bytes ");
105 const String hfpRange        = LearnHeader("Range: bytes=");
106 const String hfpExpect       = LearnHeader("Expect: ");
107 const String hfpContDisposition = LearnHeader("Content-Disposition: attachment; filename=");
108 
109 // free text
110 const String text302Found  = "please go to ";
111 const String text406NotAcceptable =
112 	"None of client-supported content codings are supported by the server.\n";
113 const String text416RequestedRangeNotSatisfiable =
114 	"Range specified in request is not satisfiable.\n";
115 const String text417ExpectationFailed =
116 	"Expectation failed.\n";
117 const String textMultipartBoundary = "THIS_STRING_SEPARATES";
118 const String textMultipartSep = "--";
119 
120 // combos
121 const String hfGzipContentEncoding = LearnHeader(hfpContentEncoding + "gzip\r\n");
122 const String hfVaryAcceptEncoding = LearnHeader("Vary: Accept-Encoding\r\n");
123 const String hfMultiRangeContType = LearnHeader(hfpContType + "multipart/byteranges; boundary=" + textMultipartBoundary + "\r\n");
124