[Jrisk-cvs] SF.net SVN: jrisk-code:[1054] Grasshopper
Brought to you by:
yuranet
|
From: <yu...@us...> - 2024-12-27 14:08:23
|
Revision: 1054
http://sourceforge.net/p/jrisk/code/1054
Author: yuranet
Date: 2024-12-27 14:08:21 +0000 (Fri, 27 Dec 2024)
Log Message:
-----------
truncate post data
Modified Paths:
--------------
Grasshopper/src/net/yura/grasshopper/BugSubmitter.java
Grasshopper/src/net/yura/grasshopper/BugSubmitterMultipart.java
Grasshopper/test/net/yura/grasshopper/SimpleBugTest.java
Modified: Grasshopper/src/net/yura/grasshopper/BugSubmitter.java
===================================================================
--- Grasshopper/src/net/yura/grasshopper/BugSubmitter.java 2024-11-24 00:00:47 UTC (rev 1053)
+++ Grasshopper/src/net/yura/grasshopper/BugSubmitter.java 2024-12-27 14:08:21 UTC (rev 1054)
@@ -7,10 +7,15 @@
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import java.util.Properties;
@@ -166,11 +171,51 @@
return error.toString();
}
- public static void doPost(OutputStreamWriter getpostb,Map map) throws IOException {
+ /**
+ * same value as cgi_post_max in FormMail.cgi
+ */
+ private static final int MAX_PAYLOAD_SIZE = 1000000;
+ public static void doPost(String url, Map map) throws IOException {
+
+ URLConnection conn = new URL(url).openConnection();
+ conn.setDoOutput(true);
+ OutputStreamWriter wr = new OutputStreamWriter(new LimitedOutputStream(conn.getOutputStream(), MAX_PAYLOAD_SIZE), "UTF-8");
+
+ doPost(wr,map);
+
+ wr.close();
+
+ // Get the response
+ int code = 200;
+ if (conn instanceof HttpURLConnection) {
+ code = ((HttpURLConnection)conn).getResponseCode();
+ }
+
+ //StringBuffer buffer = new StringBuffer();
+ BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+ String line;
+ while ((line = rd.readLine()) != null) {
+ // buffer.append(line);
+ // buffer.append("\n");
+ }
+
+ rd.close();
+ //return buffer.toString();
+ // do NOT read the responce into a buffer, as we do not need it anyway
+
+ // TODO handle 301 redirect
+
+ if (code / 100 != 2) {
+ throw new IOException("http error " + code);
+ }
+ }
+
+ public static void doPost(OutputStreamWriter getpostb, Map map) throws IOException {
+
boolean firstDone=false;
- Iterator enu = map.keySet().iterator();
+ Iterator enu = getKeysLogTextLast(map).iterator();
while(enu.hasNext()) {
if (firstDone) {
@@ -181,7 +226,7 @@
}
Object key = enu.next();
- getpostb.append( URLEncoder.encode( String.valueOf( key ) , "UTF-8") );
+ getpostb.append(URLEncoder.encode(String.valueOf(key), "UTF-8"));
getpostb.append("=");
@@ -198,31 +243,23 @@
getpostb.append( URLEncoder.encode( value , "UTF-8"));
}
}
-
}
-
-
- public static void doPost(String url, Map map) throws IOException {
-
- URLConnection conn = new URL(url).openConnection();
- conn.setDoOutput(true);
- OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
-
- doPost(wr,map);
-
- wr.close();
-
- // Get the response
- //StringBuffer buffer = new StringBuffer();
- BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = rd.readLine()) != null) {
- // buffer.append(line);
- // buffer.append("\n");
- }
-
- rd.close();
- //return buffer.toString();
- // do NOT read the responce into a buffer, as we do not need it anyway
+
+ public static List getKeysLogTextLast(final Map map) {
+ List keys = new ArrayList(map.keySet());
+ Collections.sort(keys, new Comparator() {
+ public int compare(Object key1, Object key2) {
+ Object value1 = map.get(key1);
+ Object value2 = map.get(key2);
+ if (value1 instanceof LogText && !(value2 instanceof LogText)) {
+ return 1;
+ }
+ if (value2 instanceof LogText && !(value1 instanceof LogText)) {
+ return -1;
+ }
+ return String.valueOf(key1).compareTo(String.valueOf(key2));
+ }
+ });
+ return keys;
}
}
Modified: Grasshopper/src/net/yura/grasshopper/BugSubmitterMultipart.java
===================================================================
--- Grasshopper/src/net/yura/grasshopper/BugSubmitterMultipart.java 2024-11-24 00:00:47 UTC (rev 1053)
+++ Grasshopper/src/net/yura/grasshopper/BugSubmitterMultipart.java 2024-12-27 14:08:21 UTC (rev 1054)
@@ -4,6 +4,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
+import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
@@ -19,6 +20,8 @@
*/
public class BugSubmitterMultipart {
+ private static final int MAX_PAYLOAD_SIZE = 5000000;
+
static void send(String url, Map map) throws IOException {
// create the multipart request and add the parts to it
@@ -27,11 +30,10 @@
// get Screenshots of windows first, so any paint errors will go into the logs
ScreenShot[] images = BugUIInfo.getFrames();
- Iterator it = map.entrySet().iterator();
+ Iterator it = BugSubmitter.getKeysLogTextLast(map).iterator();
while (it.hasNext()) {
- Map.Entry next = (Map.Entry) it.next();
- String key = String.valueOf( next.getKey() );
- Object value = next.getValue();
+ Object key = it.next();
+ Object value = map.get(key);
if (value instanceof LogText) {
// can not use this as unknown length fails on Android
@@ -42,7 +44,7 @@
// });
final LogText logtext = (LogText)value;
- requestContent.addPart(key, new AbstractContentBody("text/plain") {
+ requestContent.addPart(String.valueOf(key), new AbstractContentBody("text/plain") {
public long getContentLength() {
return logtext.getContentLength();
}
@@ -61,7 +63,7 @@
} );
}
else {
- requestContent.addPart(key, new StringBody( String.valueOf(value) ));
+ requestContent.addPart(String.valueOf(key), new StringBody(String.valueOf(value)));
}
}
@@ -103,10 +105,18 @@
conn.setRequestProperty("Content-Length", String.valueOf(requestContent.getContentLength()) );
OutputStream out = conn.getOutputStream();
+
+ //System.out.println("sending " + requestContent.getContentLength());
+
requestContent.writeTo( out );
out.close();
// Get the response
+ int code = 200;
+ if (conn instanceof HttpURLConnection) {
+ code = ((HttpURLConnection)conn).getResponseCode();
+ }
+
//StringBuffer buffer = new StringBuffer();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
@@ -117,6 +127,11 @@
rd.close();
//return buffer.toString();
// do NOT read the responce into a buffer, as we do not need it anyway
+
+ // TODO handle 301 redirect
+
+ if (code / 100 != 2) {
+ throw new IOException("http error " + code);
+ }
}
-
}
Modified: Grasshopper/test/net/yura/grasshopper/SimpleBugTest.java
===================================================================
--- Grasshopper/test/net/yura/grasshopper/SimpleBugTest.java 2024-11-24 00:00:47 UTC (rev 1053)
+++ Grasshopper/test/net/yura/grasshopper/SimpleBugTest.java 2024-12-27 14:08:21 UTC (rev 1054)
@@ -1,5 +1,6 @@
package net.yura.grasshopper;
+import java.util.logging.Logger;
import junit.framework.TestCase;
/**
@@ -46,4 +47,29 @@
new SimpleBugTest("name").testLogFile();
}
+
+ public void testBigLogFile() throws Exception {
+ System.out.println("testBigLogFile - start");
+
+ // this takes over logging and junit stops logging
+ SimpleBug.initLogFile("Test", "1.0", "en");
+
+ Logger logger = Logger.getLogger(SimpleBugTest.class.getName());
+
+ // works 100000 - 3302833 bytes
+ // works 140000 - 4662852 bytes
+ // works 149900 - 4999293 bytes
+ // works 149920 - 4999995 bytes
+ // failed 149921 - 5000068 bytes
+ // failed 149950 - 5001134 bytes
+ // failed 150000 - 5002754 bytes
+ for (int c=0;c<149900;c++) {
+ logger.info("testing some logging " + c);
+ }
+
+ LogTest.testLogger();
+
+ System.out.println("testBigLogFile - done");
+ Thread.sleep(5000);
+ }
}
|