1 /*=========================================================
2 * Create file LoadTestCookieManager.java
3 * Created on Jul 20, 2007
4 *=========================================================
6 package org
.spirit
.loadtest
;
8 import java
.io
.BufferedReader
;
9 import java
.io
.BufferedWriter
;
11 import java
.io
.FileReader
;
12 import java
.io
.FileWriter
;
13 import java
.io
.IOException
;
14 import java
.net
.HttpURLConnection
;
15 import java
.util
.ArrayList
;
16 import java
.util
.HashMap
;
17 import java
.util
.Iterator
;
18 import java
.util
.List
;
22 * Simple cookie management; read Set-Cookie cookie key values from response and
23 * utilities for sending cookies in client requests.
25 public class LoadTestCookieManager
{
27 private Map incomingCookieData
= new HashMap();
29 private Map outgoingCookieData
= new HashMap();
31 private static List referer
= new ArrayList();
33 public LoadTestCookieManager() {
36 public final static String
filterAlphaNumeric(String value
) {
40 // Filter out non alphanumeric chars
41 String output
= value
.replaceAll("[^\\s0-9a-zA-Z]", "");
45 protected List
getRefererQueue() {
50 * Normally used for 302 redirects, set the previous or referer URL.
52 public void queueRefererUrl(String referer_str
) {
53 getRefererQueue().add(referer_str
);
54 System
.out
.println("INFO: Setting referering URL=" + referer_str
+ " / sz=" + getRefererQueue().size());
57 public String
getRefererUrl() {
58 System
.out
.println("($) Referer queue size=" + getRefererQueue().size());
59 if (referer
.size() < 1)
63 // There must be at least two values in the
64 int cur_size
= getRefererQueue().size();
65 int query_idx
= cur_size
- 1;
66 System
.out
.println("($) Referer=" + getRefererQueue().get(query_idx
));
67 return (String
) getRefererQueue().get(query_idx
);
70 public static void loadExistingCookies(final String currentHost
, final Map siteCookieData
) {
71 String filename_cookie
= "cookies/" + filterAlphaNumeric(currentHost
) + ".dat";
72 BufferedReader in
= null;
75 in
= new BufferedReader(new FileReader(filename_cookie
));
76 while ((feed
= in
.readLine()) != null) {
78 if ((feed
!= null) && (feed
.length() > 2)) {
79 // Parse the cookie content in the cookie store
80 int eq_index
= feed
.indexOf("=");
82 String key
= feed
.substring(0, eq_index
);
83 String val
= feed
.substring(eq_index
+ 1);
84 System
.out
.println("[+] Loading existing cookie=" + key
);
85 siteCookieData
.put(key
.trim(), val
.trim());
89 } catch (Exception e
) {
90 System
.out
.println("INFO: no existing cookie data");
95 } catch (IOException ie
) {
100 private void parseCookieValue(final String currentHost
, final String cookieContent
, final Map siteCookieData
) {
101 if (cookieContent
== null) {
104 String entries
[] = cookieContent
.split(";");
105 for (int i
= 0; i
< entries
.length
; i
++) {
106 int eq_index
= entries
[i
].indexOf("=");
108 String entry
= entries
[i
];
109 String key
= entry
.substring(0, eq_index
);
110 String val
= entry
.substring(eq_index
+ 1);
111 System
.out
.println("[*] Setting cookie=" + key
);
112 siteCookieData
.put(key
.trim(), val
.trim());
115 // Ignore the cookie meta data for now
120 private Map
getIncomingCookieDataInstance(final String currentHost
) {
121 Map hostCookieData
= null;
122 if (this.getIncomingCookieData(currentHost
) == null) {
123 // Create new cookie value
124 hostCookieData
= new HashMap();
125 incomingCookieData
.put(currentHost
, hostCookieData
);
126 return hostCookieData
;
128 // Load the existing cookie data for this post
129 hostCookieData
= (Map
) incomingCookieData
.get(currentHost
);
130 return hostCookieData
;
134 public void readCookieData(final String currentHost
) {
135 Map siteCookieData
= getIncomingCookieDataInstance(currentHost
);
136 loadExistingCookies(currentHost
, siteCookieData
);
140 * Load data from the response header fields; in particular the
141 * value from the "Set-Cookie". It is possible to have multple Set-Cookie values.
143 * After data is loaded into the map data structure, set the Map data for this particular host in
144 * the outgoing data store.
146 * (Note: a new map datastore is each time this method is called)
148 * Updated: 7/23/2007 - updated with "load existing" switch
153 public void parseCookieData(HttpURLConnection conn
, final String currentHost
, final boolean loadExisting
) {
154 Map siteCookieData
= new HashMap();
155 // Load pre-existing cookie data from file.
157 loadExistingCookies(currentHost
, siteCookieData
);
160 for (int i
= 1; i
< 50; i
++) {
161 // Note: 50 = arbitrary value to invalid headers
162 if (conn
.getHeaderFieldKey(i
) == null)
164 String headerName
= conn
.getHeaderFieldKey(i
);
165 if (headerName
.equalsIgnoreCase("Set-Cookie")) {
166 String cookieContent
= conn
.getHeaderField(i
);
167 parseCookieValue(currentHost
, cookieContent
, siteCookieData
);
170 outgoingCookieData
.put(currentHost
, siteCookieData
);
173 public static String
getCookieDataUtil(final Map hostCookieData
) {
174 StringBuffer buf
= new StringBuffer();
175 for (Iterator it
= hostCookieData
.entrySet().iterator(); it
.hasNext();) {
176 Map
.Entry set
= (Map
.Entry
) it
.next();
177 String key
= (String
) set
.getKey();
178 String val
= (String
) set
.getValue();
179 buf
.append(key
+ "=" + val
+ "; ");
181 System
.out
.println("For client request, cookie data(Cookie:)=" + buf
.toString());
182 return buf
.toString();
185 public String
getIncomingCookieData(final String currentHost
) {
186 if (!this.incomingCookieData
.containsKey(currentHost
))
188 Map host_data
= (Map
) this.incomingCookieData
.get(currentHost
);
189 return getCookieDataUtil(host_data
);
193 * Write the simple cookie database as so; one file per host.
195 public void writeCookieData() throws IOException
{
196 BufferedWriter cookie_out
= null;
198 for (Iterator it
= outgoingCookieData
.entrySet().iterator(); it
.hasNext();) {
199 Map
.Entry set
= (Map
.Entry
) it
.next();
200 System
.out
.println("INFO: writing cookie=(" + set
.getKey() + " [" + set
.getValue() + "])");
202 String filename_cookie
= "cookies/" + filterAlphaNumeric((String
) set
.getKey()) + ".dat";
203 File cur_file
= new File(filename_cookie
);
204 System
.out
.println("INFO: writing cookie file=" + cur_file
.getAbsolutePath());
205 cookie_out
= new BufferedWriter(new FileWriter(filename_cookie
, false));
207 // Write each key value on a line
208 Map host_data
= (Map
) set
.getValue();
209 for (Iterator host_it
= host_data
.entrySet().iterator(); host_it
.hasNext();) {
210 Map
.Entry host_set
= (Map
.Entry
) host_it
.next();
211 cookie_out
.write(host_set
.getKey() + "=" + host_set
.getValue() + "\n");
215 if (cookie_out
!= null) {
219 } // End of the try - finally
222 //=========================================================
224 //=========================================================