1 19 20 package com.sslexplorer.replacementproxy.replacers; 21 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.regex.Matcher ; 27 import java.util.regex.Pattern ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import com.sslexplorer.boot.CaseInsensitiveMap; 33 import com.sslexplorer.boot.Replacer; 34 import com.sslexplorer.boot.Util; 35 import com.sslexplorer.policyframework.LaunchSession; 36 import com.sslexplorer.util.Utils; 37 38 public class FormReplacer implements Replacer { 39 40 private URL context; 41 private String ticket; 42 43 static Log log = LogFactory.getLog(FormReplacer.class); 44 45 public FormReplacer(URL context, String ticket) { 46 this.context = context; 47 this.ticket = ticket; 48 } 49 50 56 public String getReplacement(Pattern pattern, Matcher matcher, String replacementPattern) { 57 String attrs = matcher.group(2); 58 return doRepl(attrs); 59 } 60 61 private String doRepl(String attrs) { 62 StringBuffer attrName = new StringBuffer (); 63 char quote = ' '; 64 StringBuffer attrVal = new StringBuffer (); 65 boolean doName = true; 66 boolean doVal = false; 67 CaseInsensitiveMap a = new CaseInsensitiveMap(); 68 for (int i = 0; i < attrs.length(); i++) { 69 char ch = attrs.charAt(i); 70 if (ch == '\'' && quote == ' ') { 71 quote = '\''; 72 } else if (ch == '"' && quote == ' ') { 73 quote = '"'; 74 } else if (((doName && (ch == '\r' || ch == '\n')) || (doVal && ch == ' ' && quote == ' ' && attrVal.length() > 0) 75 || (doVal && ch == '\'' && quote == '\'') || (doVal && ch == '\"' && quote == '\"'))) { 76 quote = ' '; 77 String an = attrName.toString(); 78 if (!an.equals("")) { 79 a.put(attrName.toString(), attrVal.toString()); 80 } 81 attrName.setLength(0); 82 attrVal.setLength(0); 83 doVal = false; 84 doName = true; 85 } else if (ch == '=' && doName) { 86 doName = false; 87 doVal = true; 88 } else { 89 if (doName) { 90 if ((ch != ' ' && ch != '\r' && ch != '\n') || attrName.length() > 0) { 91 attrName.append(ch); 92 } 93 } else if (doVal) { 94 attrVal.append(ch); 95 } 96 } 97 } 98 StringBuffer buf = new StringBuffer ("<form"); 99 String sslexUrl = context.toExternalForm(); 100 if (a.containsKey("action")) { 101 try { 102 String contextPath = context.toExternalForm(); 103 if (contextPath.endsWith("/")) { 104 contextPath = contextPath.substring(0, contextPath.length() - 1); 105 } 106 String originalAction = a.get("action").toString(); 107 try { 108 sslexUrl = new URL (originalAction).toExternalForm(); 109 } 110 catch(MalformedURLException murle) { 111 147 sslexUrl = new URL (context, originalAction).toExternalForm(); 149 } 154 } catch (MalformedURLException e) { 155 log.error("Failed to process FORM action", e); 156 } 157 } 158 159 a.put("action", "/replacementProxyEngine/" + ticket + "/" + Util.urlEncode(Utils.htmlunescape(sslexUrl)) ); 160 161 for (Iterator i = a.entrySet().iterator(); i.hasNext();) { 162 buf.append(" "); 163 Map.Entry entry = (Map.Entry ) i.next(); 164 buf.append(entry.getKey().toString()); 165 buf.append("=\""); 166 buf.append(entry.getValue()); 167 buf.append("\""); 168 } 169 buf.append(">"); 170 171 return buf.toString(); 172 } 173 } 174 | Popular Tags |