Revision: 3097
http://vexi.svn.sourceforge.net/vexi/?rev=3097&view=rev
Author: mkpg2
Date: 2008-08-28 02:43:51 +0000 (Thu, 28 Aug 2008)
Log Message:
-----------
Fix. Modified http implementation so to handle binary content.
Modified Paths:
--------------
trunk/core/org.ibex.net/src/org/ibex/net/HTTP.jpp
trunk/core/org.ibex.net/src_junit/org/ibex/net/TestHTTP.java
Added Paths:
-----------
trunk/core/org.ibex.net/src_dev/org/ibex/net/DebugOutputStream.java
Modified: trunk/core/org.ibex.net/src/org/ibex/net/HTTP.jpp
===================================================================
--- trunk/core/org.ibex.net/src/org/ibex/net/HTTP.jpp 2008-08-23 13:22:39 UTC
(rev 3096)
+++ trunk/core/org.ibex.net/src/org/ibex/net/HTTP.jpp 2008-08-28 02:43:51 UTC
(rev 3097)
@@ -114,7 +114,7 @@
return makeRequest("HEAD", null, referer, cookies); }
/** Performs an HTTP POST request; content is additional headers, blank
line, and body */
- public InputStream POST(String contentType, String content, String
referer, Cookie.Jar cookies) throws IOException {
+ public InputStream POST(String contentType, byte[] content, String
referer, Cookie.Jar cookies) throws IOException {
return makeRequest(contentType, content, referer, cookies); }
public static class HTTPException extends IOException { public
HTTPException(String s) { super(s); } }
@@ -153,7 +153,7 @@
* This method isn't synchronized; however, only one thread can be in the
inner synchronized block at a time, and the rest of
* the method is protected by in-order one-at-a-time semaphore lock-steps
*/
- private InputStream makeRequest(String contentType, String content, String
referer, Cookie.Jar cookies) throws IOException {
+ private InputStream makeRequest(String contentType, byte[] content, String
referer, Cookie.Jar cookies) throws IOException {
// Step 1: send the request and establish a semaphore to stop any
requests that pipeline after us
Semaphore blockOn = null;
@@ -460,32 +460,36 @@
if (in == null) in = new BufferedInputStream(sock.getInputStream());
}
- private void sendRequest(String contentType, String content, String
referer, Cookie.Jar cookies) throws IOException {
- PrintWriter pw = new PrintWriter(new
OutputStreamWriter(originalUrl.equals("stdio:") ?
- System.out :
sock.getOutputStream()));
+ private void sendRequest(String contentType, byte[] content, String
referer, Cookie.Jar cookies) throws IOException {
+ // REMARK - the BufferedOutputStream is necessary - for the xmlrpc
tests at least.
+ // Not sure exactly why, but there were communication problems without
it.
+ PrintStream ps = new PrintStream(new BufferedOutputStream(
+ originalUrl.equals("stdio:") ?
+ System.out : sock.getOutputStream()));
if (content != null) {
- pw.print("POST " + path + " HTTP/1.0\r\n"); // FIXME chunked
encoding
- int contentLength = content.substring(0, 2).equals("\r\n") ?
- content.length() - 2 :
- (content.length() - content.indexOf("\r\n\r\n") - 4);
- pw.print("Content-Length: " + contentLength + "\r\n");
- if (contentType != null) pw.print("Content-Type: " + contentType +
"\r\n");
+ ps.print("POST " + path + " HTTP/1.0\r\n"); // FIXME chunked
encoding
+ int contentLength = content.length;
+ ps.print("Content-Length: " + contentLength + "\r\n");
+ if (contentType != null) ps.print("Content-Type: " + contentType +
"\r\n");
} else {
- pw.print(contentType + " " + path + " HTTP/1.1\r\n");
+ ps.print(contentType + " " + path + " HTTP/1.1\r\n");
}
- if (cookies != null) pw.print(cookies.getCookieHeader(host, path,
ssl));
- pw.print("User-Agent: " + userAgent + "\r\n");
- pw.print("Accept-encoding: gzip\r\n");
- pw.print("Host: " + (host + (port == 80 ? "" : (":" + port))) +
"\r\n");
- if (proxied) pw.print("X-RequestOrigin: " + originHost + "\r\n");
+ if (cookies != null) ps.print(cookies.getCookieHeader(host, path,
ssl));
+ ps.print("User-Agent: " + userAgent + "\r\n");
+ ps.print("Accept-encoding: gzip\r\n");
+ ps.print("Host: " + (host + (port == 80 ? "" : (":" + port))) +
"\r\n");
+ if (proxied) ps.print("X-RequestOrigin: " + originHost + "\r\n");
- if (Proxy.Authorization.authorization != null)
pw.print("Proxy-Authorization: "+Proxy.Authorization.authorization2+"\r\n");
- if (authCache.get(originalUrl) != null) pw.print("Authorization: " +
authCache.get(originalUrl) + "\r\n");
+ if (Proxy.Authorization.authorization != null)
ps.print("Proxy-Authorization: "+Proxy.Authorization.authorization2+"\r\n");
+ if (authCache.get(originalUrl) != null) ps.print("Authorization: " +
authCache.get(originalUrl) + "\r\n");
- pw.print(content == null ? "\r\n" : content);
- pw.print("\r\n");
- pw.flush();
+ ps.print("\r\n");
+ if(content!=null){
+ ps.write(content);
+ }
+ ps.print("\r\n");
+ ps.flush();
}
private void doWebAuth(Hashtable h0, String method) throws IOException {
Added: trunk/core/org.ibex.net/src_dev/org/ibex/net/DebugOutputStream.java
===================================================================
--- trunk/core/org.ibex.net/src_dev/org/ibex/net/DebugOutputStream.java
(rev 0)
+++ trunk/core/org.ibex.net/src_dev/org/ibex/net/DebugOutputStream.java
2008-08-28 02:43:51 UTC (rev 3097)
@@ -0,0 +1,86 @@
+
+package org.ibex.net;
+
+import java.io.*;
+import java.util.*;
+
+public class DebugOutputStream extends OutputStream{
+
+ OutputStream os;
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ int count = 0;
+ String last = null;
+ List rec = new LinkedList();
+
+ public DebugOutputStream(OutputStream os){
+ this.os = os;
+ }
+
+ private void rec(String meth){
+ if(meth.equals(last)){
+ count++;
+ }else{
+ if(last!=null)
+ rec.add(last +" " + count);
+ count = 0;
+ last = meth;
+ }
+ }
+
+ public void write(int b) throws IOException {
+ rec("write(int b)");
+ baos.write(b);
+ os.write(b);
+ }
+
+ public void write(byte[] b) throws IOException {
+ rec("write(byte[] b)");
+ baos.write(b);
+ os.write(b);
+ }
+
+ public void write(byte[] b, int off, int len) throws IOException {
+ rec("write(byte[] b, int off, int len)");
+ baos.write(b, off, len);
+ os.write(b, off, len);
+ }
+
+ public void close() throws IOException {
+ rec("close()");
+ baos.close();
+ os.close();
+ }
+
+ public void flush() throws IOException {
+ rec("flush()");
+ baos.flush();
+ os.flush();
+ }
+
+
+
+ public String getString(){
+ return baos.toString();
+ }
+
+ public String getByteString(){
+ byte[] bytes = baos.toByteArray();
+ String r = "";
+ for(int i=0; i<bytes.length; i++){
+ if(i>0) r+=",";
+ r+=""+bytes[i];
+ }
+ return r;
+ }
+ public String getRecord(){
+ rec.add(last + " " + count);
+ String r = "";
+ for(int i=0; i<rec.size(); i++){
+ if(i>0) r+=",";
+ r+=""+rec.get(i);
+ }
+ return r;
+ }
+
+}
\ No newline at end of file
Property changes on:
trunk/core/org.ibex.net/src_dev/org/ibex/net/DebugOutputStream.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Modified: trunk/core/org.ibex.net/src_junit/org/ibex/net/TestHTTP.java
===================================================================
--- trunk/core/org.ibex.net/src_junit/org/ibex/net/TestHTTP.java
2008-08-23 13:22:39 UTC (rev 3096)
+++ trunk/core/org.ibex.net/src_junit/org/ibex/net/TestHTTP.java
2008-08-28 02:43:51 UTC (rev 3097)
@@ -1,8 +1,6 @@
package org.ibex.net;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -32,7 +30,8 @@
ServletHandler handler=new ServletHandler();
server.setHandler(handler);
-
handler.addServletWithMapping(CapitalizeServlet.class.getName(),
"/capitalize/*");
+
handler.addServletWithMapping(CapitalizeServlet.class.getName(), "/capitalize");
+ handler.addServletWithMapping(ReorderServlet.class.getName(),
"/reorder");
server.start();
}
@@ -41,7 +40,7 @@
server.stop();
}
- public static class CapitalizeServlet extends HttpServlet
+ static public class CapitalizeServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
@@ -56,6 +55,25 @@
}
+ static public class ReorderServlet extends HttpServlet
+ {
+ protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ IOUtil.pipe(request.getInputStream(),
+ baos);
+ byte[] in = baos.toByteArray();
+ byte[] out = new byte[in.length];
+ for(int i=0; i<in.length; i++) out[in.length-1-i] = in[i];
+ ByteArrayInputStream bais = new ByteArrayInputStream(out);
+
+ response.setContentType("application/octet-stream");
+ response.setStatus(HttpServletResponse.SC_OK);
+ IOUtil.pipe(bais, response.getOutputStream());
+ }
+
+ }
+
protected void setUp() throws Exception {
startServlet();
}
@@ -65,10 +83,26 @@
}
public void testPOST() throws Exception {
- InputStream is = new
HTTP("http://localhost:"+port+"/capitalize/").POST("text/plain", "\r\nabc",
null, null);
+ String content = new String("abc");
+ InputStream is = new
HTTP("http://localhost:"+port+"/capitalize").
+ POST("text/plain", content.getBytes(), null, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtil.pipe(is, baos);
- System.out.print(baos.toString());
+ assertEquals("ABC", baos.toString());
}
+ public void testBinaryPOST() throws Exception {
+ // making sure we are binary by having one of each type of byte!
+ byte[] in = new byte[256];
+ for(int i=0; i<in.length; i++) in[i] = (byte)i;
+ InputStream is = new
HTTP("http://localhost:"+port+"/reorder").POST("application/octet-stream", in,
null, null);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ IOUtil.pipe(is, baos);
+ byte[] out = baos.toByteArray();
+ assertEquals(256, out.length);
+ for(int i=0; i<out.length; i++) {
+ assertEquals((byte)(255-i), out[i]);
+ }
+ }
+
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Vexi-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/vexi-svn