Changeset 9
- Timestamp:
- 10/07/05 21:33:16 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
feedtree/trunk/src/net/feedtree/util/ByteArrayBuffer.java
r8 r9 5 5 6 6 public class ByteArrayBuffer implements Cloneable { 7 // TODO: add insert/remove/slice methods 8 7 9 protected LinkedList list; 8 10 protected int length; … … 27 29 } 28 30 31 public int length() { 32 return this.length; 33 } 34 29 35 public byte[] toByteArray() { 30 36 flatten(); … … 41 47 byte[] bi = (byte[])(i.next()); 42 48 System.arraycopy(bi, 0, newBuf, p, bi.length); 49 p += bi.length; 43 50 } 44 51 … … 48 55 } 49 56 50 public void append(byte[] buf) { 57 public void attach(byte[] buf) { 58 if (buf.length == 0) return; 59 60 flat = flat && (length == 0); // it's still flat after the first append() 61 62 System.out.println("#### attaching: " + new String(buf)); 63 51 64 list.add(buf); 52 65 length += buf.length; 53 flat = flat && (length == 1);54 66 } 55 67 56 68 public void append(byte[] buf, int bufStart, int bufLen) { 69 if (bufLen == 0) return; 70 57 71 byte[] slice = new byte[bufLen]; 58 72 System.arraycopy(buf, bufStart, slice, 0, bufLen); 59 73 60 append(slice); 74 attach(slice); 75 } 76 77 public void append(byte[] buf) { 78 append(buf, 0, buf.length); 79 } 80 81 public String toString() { 82 String s; 83 byte[] buf = toByteArray(); 84 85 flatten(); 86 if (length < 22) s = new String(buf); 87 else { 88 byte[] prefix = new byte[10]; 89 byte[] suffix = new byte[10]; 90 System.arraycopy(buf, 0, prefix, 0, 10); 91 System.arraycopy(buf, length-11, suffix, 0, 10); 92 s = new String(prefix) + ".." + new String(suffix); 93 } 94 95 return "<ByteArrayBuffer [" + s + "] len=" + length + ">"; 61 96 } 62 97 } feedtree/trunk/src/net/feedtree/web/Poolboy.java
r4 r9 3 3 package net.feedtree.web; 4 4 5 import net.feedtree.util.ByteArrayBuffer; 6 5 7 import java.io.*; 8 import java.nio.ByteBuffer; 6 9 import java.net.*; 7 10 import java.text.*; … … 65 68 66 69 public static class PostRequest extends Request { 67 public Stringbody;70 public ByteArrayBuffer body; 68 71 public String getMethod() { return "POST"; } 69 public PostRequest(String u, Map h, StringBuffer _bodyBuf) {72 public PostRequest(String u, Map h, ByteArrayBuffer _bodyBuf) { 70 73 super(u,h); 71 74 setBody(_bodyBuf); 72 75 } 73 public PostRequest(String u, Map h, String_body) {76 public PostRequest(String u, Map h, byte[] _body) { 74 77 super(u,h); 75 78 setBody(_body); 76 79 } 77 public void setBody( String _body) { body = _body; }78 public void setBody( StringBuffer _bodyBuf) { body = _bodyBuf.toString(); }80 public void setBody(byte[] _body) { body = new ByteArrayBuffer(_body); } 81 public void setBody(ByteArrayBuffer _bodyBuf) { body = _bodyBuf; } 79 82 } 80 83 public static class PutRequest extends PostRequest { 81 84 public String getMethod() { return "PUT"; } 82 public PutRequest(String u, Map h, StringBuffer _bodyBuf) {85 public PutRequest(String u, Map h, ByteArrayBuffer _bodyBuf) { 83 86 super(u,h,_bodyBuf); 84 87 } 85 public PutRequest(String u, Map h, String_body) {88 public PutRequest(String u, Map h, byte[] _body) { 86 89 super(u,h,_body); 87 90 } … … 98 101 public void println() { print("\r\n"); } 99 102 } 103 104 protected String readLineFromStream(BufferedInputStream bis) 105 throws IOException 106 { 107 String s = ""; 108 109 int ch = bis.read(); 110 boolean stop = false; 111 while(!stop) { 112 switch (ch) { 113 case '\r': 114 case '\n': 115 case -1: 116 stop = true; 117 break; 118 default: 119 s += (char)ch; 120 ch = bis.read(); 121 break; 122 } 123 } 124 // possibly eat CRLF newlines 125 if (ch == '\r') { 126 bis.mark(2); 127 int next = bis.read(); 128 if (next != '\n') reset(); 129 } 130 131 return s; 132 } 133 134 protected static final int BUFSIZ = 4096; 100 135 101 136 protected boolean PRINT_EXCEPTIONS = true; 102 137 103 138 protected Socket socket; 104 protected BufferedReader input; 139 protected BufferedInputStream rawInput; 140 //protected BufferedReader input; 105 141 protected OutputStream rawOutput; 106 142 protected WebPrintWriter output; … … 132 168 protected void attach(Socket _socket) throws java.io.IOException { 133 169 socket = _socket; 134 input = new BufferedReader( 135 new InputStreamReader(socket.getInputStream())); 170 rawInput = new BufferedInputStream(socket.getInputStream()); 136 171 rawOutput = socket.getOutputStream(); 137 172 output = new WebPrintWriter( … … 219 254 try { 220 255 String request, uri; 221 request = input.readLine(); 256 257 request = readLineFromStream(rawInput); 222 258 boolean isGet = request.startsWith("GET ") || request.startsWith("HEAD "); 223 259 boolean isPost = request.startsWith("POST ") || request.startsWith("PUT "); … … 225 261 uri = (request.split(" "))[1]; 226 262 Map headers = new HashMap(); 227 String headerLine = input.readLine();263 String headerLine = readLineFromStream(rawInput); 228 264 while (!headerLine.equals("")) { 229 265 Matcher m = kHeaderPattern.matcher(headerLine); … … 231 267 headers.put(m.group(1), m.group(2)); 232 268 } 233 headerLine = input.readLine();269 headerLine = readLineFromStream(rawInput); 234 270 } 235 271 236 StringBuffer postBuf = null;272 ByteArrayBuffer postBuf = null; 237 273 238 274 Request requestObj; 239 275 240 276 if (isPost) { 241 postBuf = new StringBuffer();242 char buf[] = new char[4096];277 postBuf = new ByteArrayBuffer(); 278 byte buf[] = new byte[BUFSIZ]; 243 279 int len; 244 280 int contentLength = -1; … … 249 285 System.out.println("[Poolboy.run] POST/PUT content-length = " + contentLength); 250 286 if (contentLength > 0) { 251 while (contentLength > 0) { 252 len = input.read(buf, 0, 4096); 287 int readLength = 0; 288 while (readLength < contentLength) { 289 len = rawInput.read(buf, 0, BUFSIZ); 290 System.out.println("[Poolboy.run] read " 291 + readLength + "/" + contentLength 292 + " bytes (" + len + " just now)"); 253 293 if (len >= 0) { 254 294 postBuf.append(buf, 0, len); … … 258 298 + " but read() returned " + len 259 299 + " before all content had been read."); 260 break; 300 301 //System.out.println("[Poolboy.run] What we got was: >>>\n" + postBuf + "\n<<<"); 302 break; 261 303 } 262 contentLength -= len;304 readLength += len; 263 305 } 264 306 } else { 265 while ( (len = input.read(buf, 0, 4096)) >= 0 )307 while ( (len = rawInput.read(buf, 0, BUFSIZ)) >= 0 ) 266 308 postBuf.append(buf, 0, len); 267 309 } feedtree/trunk/src/net/feedtree/web/WebProxyHandler.java
r4 r9 75 75 cx.connect(); 76 76 if (request instanceof Poolboy.PostRequest) { 77 PrintWriter out = new PrintWriter(cx.getOutputStream()); 78 out.print(((Poolboy.PostRequest)request).body); 79 out.close(); 77 cx.getOutputStream().write( 78 ((Poolboy.PostRequest)request).body.toByteArray()); 80 79 } 81 80 }
